Skip to content

Commit

Permalink
[mbr] Extend console sample
Browse files Browse the repository at this point in the history
Add a busy thread to demonstrate that interp frames since the last managed
frame are visible to the metadata update mechanism and the active method bodies are
copied before being invalidated.
  • Loading branch information
lambdageek committed Jan 9, 2021
1 parent 945b060 commit d40eb92
Showing 1 changed file with 40 additions and 0 deletions.
40 changes: 40 additions & 0 deletions src/mono/netcore/sample/mbr/console/Program.cs
Original file line number Diff line number Diff line change
Expand Up @@ -14,11 +14,24 @@ internal class Program
class State {
public readonly ManualResetEventSlim mreIn;
public readonly ManualResetEventSlim mreOut;
public readonly ManualResetEventSlim mreBusy;
public string res;
private volatile bool busyChanged;

public State() {
mreIn = new ManualResetEventSlim ();
mreOut = new ManualResetEventSlim ();
mreBusy = new ManualResetEventSlim ();
res = "";
busyChanged = false;
}


public bool BusyChanged {get => busyChanged ; set { busyChanged = value; mreBusy.Set ();} }

public void WaitForBusy () {
mreBusy.Wait ();
mreBusy.Reset ();
}

public string ConsumerStep () {
Expand Down Expand Up @@ -50,6 +63,8 @@ private static int Main(string[] args)
var st = new State ();
var t = new Thread (MutatorThread);
t.Start (st);
var t2 = new Thread (BusyThread) { IsBackground = true };
t2.Start (st);

string res = st.ConsumerStep ();
if (res != "OLD STRING")
Expand All @@ -61,6 +76,9 @@ private static int Main(string[] args)
if (res != "NEW STRING")
return 2;

st.WaitForBusy ();
Console.WriteLine ("BusyChanged: {0}", st.BusyChanged);

return 0;
}

Expand All @@ -70,6 +88,28 @@ private static void MutatorThread (object o)
static string Step () => TestClass.TargetMethod ();
st.ProducerStep (Step);
st.ProducerStep (Step);
}

// This method is not affected by the update, but it calls the target
// method which is. Still we expect to see "BusyThread" and its
// callees show up in the trace output when it safepoints during an
// update.
private static void BusyThread (object o)
{
State st = (State)o;
string prev = TestClass.TargetMethod ();
while (true) {
Thread.Sleep (0);
for (int i = 0; i < 5000; ++i) {
if (i % 1000 == 0) {
string cur = TestClass.TargetMethod ();
if (cur != prev) {
st.BusyChanged = true;
prev = cur;
}
}
}
}
}

}
Expand Down

0 comments on commit d40eb92

Please sign in to comment.