-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathAtomicBool.cs
39 lines (38 loc) · 1.16 KB
/
AtomicBool.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
namespace SimpleHttpListener
{
using System;
using System.Threading;
internal sealed class AtomicBool
{
private const Int32 TRUE = 1;
private const Int32 FALSE = 0;
private Int32 value;
public AtomicBool() : this(false)
{
}
public AtomicBool(bool value)
{
this.value = value ? TRUE : FALSE;
}
public bool Value { get { return value == TRUE; } }
public bool GetAndSet(bool value)
{
Int32 t = value ? TRUE : FALSE;
return Interlocked.Exchange(ref this.value, t) == TRUE;
}
public bool CompareAndSet(bool oldValue, bool newValue)
{
Int32 _old = oldValue ? TRUE : FALSE;
Int32 _new = newValue ? TRUE : FALSE;
return Interlocked.CompareExchange(ref this.value, _new, _old) == TRUE;
}
public static bool operator true(AtomicBool value)
{
return value.Value == true;
}
public static bool operator false(AtomicBool value)
{
return value.Value == false;
}
}
}