AotInterlocked.cs
Go to the documentation of this file.
1 using System;
2 
3 namespace System.Threading
4 {
15  public static class AotInterlocked
16  {
17  static SpinLock spinLock = new SpinLock(false);
18 
19  public static T Exchange<T>(ref T target, T newValue)
20  {
21  bool lockTaken = false;
22  try
23  {
24  spinLock.Enter(ref lockTaken);
25 
26  T originalValue = target;
27 
28  target = newValue;
29 
30  return originalValue;
31  }
32  finally
33  {
34  if(lockTaken)
35  spinLock.Exit(false);
36  }
37  }
38 
39  public static T CompareExchange<T>(ref T target, T newValue, T comparand)
40  {
41  bool lockTaken = false;
42  try
43  {
44  spinLock.Enter(ref lockTaken);
45 
46  T originalValue = target;
47 
48  if(Object.ReferenceEquals(target, comparand))
49  target = newValue;
50 
51  return originalValue;
52  }
53  finally
54  {
55  if(lockTaken)
56  spinLock.Exit(false);
57  }
58  }
59  }
60 }
61 
Interlocked reference exchanges do not work with the older Mono AOT compiler so this type fudges arou...
void Enter(ref bool lockTaken)
Definition: SpinLock.cs:118