@@ -14,11 +14,24 @@ internal class Program
1414 class State {
1515 public readonly ManualResetEventSlim mreIn ;
1616 public readonly ManualResetEventSlim mreOut ;
17+ public readonly ManualResetEventSlim mreBusy ;
1718 public string res ;
19+ private volatile bool busyChanged ;
20+
1821 public State ( ) {
1922 mreIn = new ManualResetEventSlim ( ) ;
2023 mreOut = new ManualResetEventSlim ( ) ;
24+ mreBusy = new ManualResetEventSlim ( ) ;
2125 res = "" ;
26+ busyChanged = false ;
27+ }
28+
29+
30+ public bool BusyChanged { get => busyChanged ; set { busyChanged = value ; mreBusy . Set ( ) ; } }
31+
32+ public void WaitForBusy ( ) {
33+ mreBusy . Wait ( ) ;
34+ mreBusy . Reset ( ) ;
2235 }
2336
2437 public string ConsumerStep ( ) {
@@ -50,6 +63,8 @@ private static int Main(string[] args)
5063 var st = new State ( ) ;
5164 var t = new Thread ( MutatorThread ) ;
5265 t . Start ( st ) ;
66+ var t2 = new Thread ( BusyThread ) { IsBackground = true } ;
67+ t2 . Start ( st ) ;
5368
5469 string res = st . ConsumerStep ( ) ;
5570 if ( res != "OLD STRING" )
@@ -61,6 +76,9 @@ private static int Main(string[] args)
6176 if ( res != "NEW STRING" )
6277 return 2 ;
6378
79+ st . WaitForBusy ( ) ;
80+ Console . WriteLine ( "BusyChanged: {0}" , st . BusyChanged ) ;
81+
6482 return 0 ;
6583 }
6684
@@ -70,6 +88,28 @@ private static void MutatorThread (object o)
7088 static string Step ( ) => TestClass . TargetMethod ( ) ;
7189 st . ProducerStep ( Step ) ;
7290 st . ProducerStep ( Step ) ;
91+ }
92+
93+ // This method is not affected by the update, but it calls the target
94+ // method which is. Still we expect to see "BusyThread" and its
95+ // callees show up in the trace output when it safepoints during an
96+ // update.
97+ private static void BusyThread ( object o )
98+ {
99+ State st = ( State ) o ;
100+ string prev = TestClass . TargetMethod ( ) ;
101+ while ( true ) {
102+ Thread . Sleep ( 0 ) ;
103+ for ( int i = 0 ; i < 5000 ; ++ i ) {
104+ if ( i % 1000 == 0 ) {
105+ string cur = TestClass . TargetMethod ( ) ;
106+ if ( cur != prev ) {
107+ st . BusyChanged = true ;
108+ prev = cur ;
109+ }
110+ }
111+ }
112+ }
73113 }
74114
75115 }
0 commit comments