Neuron®
The Neuron® is the basis for the creation of open and secure federated networks for smart societies.
Loading...
Searching...
No Matches
AsyncMutex.cs
1using System;
2using System.Threading;
3using System.Threading.Tasks;
4
6{
10 public class AsyncMutex : IDisposable
11 {
12 private readonly object synchObj = new object();
13 private ThreadExecutor executor = null;
14 private Mutex mutex;
15
19 public AsyncMutex()
20 : this(new Mutex())
21 {
22 }
23
28 public AsyncMutex(bool InitiallyOwned)
29 : this(new Mutex(InitiallyOwned))
30 {
31 }
32
38 public AsyncMutex(bool InitiallyOwned, string Name)
39 : this(new Mutex(InitiallyOwned, Name))
40 {
41 }
42
49 public AsyncMutex(bool InitiallyOwned, string Name, out bool CreatedNew)
50 : this(new Mutex(InitiallyOwned, Name, out CreatedNew))
51 {
52 }
53
58 public AsyncMutex(Mutex Mutex)
59 : base()
60 {
61 this.mutex = Mutex;
62 }
63
69 public static AsyncMutex OpenExisting(string Name)
70 {
71 return new AsyncMutex(Mutex.OpenExisting(Name));
72 }
73
80 public static bool TryOpenExisting(string Name, out AsyncMutex Result)
81 {
82 if (!Mutex.TryOpenExisting(Name, out Mutex MutexObj))
83 {
84 Result = null;
85 return false;
86 }
87
88 Result = new AsyncMutex(MutexObj);
89
90 return true;
91 }
92
93 private ThreadExecutor Executor
94 {
95 get
96 {
97 lock (this.synchObj)
98 {
99 if (this.executor is null)
100 this.executor = new ThreadExecutor();
101
102 return this.executor;
103 }
104 }
105 }
106
110 public void Dispose()
111 {
112 this.executor?.Dispose();
113 this.executor = null;
114
115 this.mutex?.Dispose();
116 this.mutex = null;
117 }
118
123 public Task<bool> WaitOne()
124 {
125 return this.Executor.Execute(this.WaitOneSync);
126 }
127
128 private bool WaitOneSync()
129 {
130 return this.mutex.WaitOne();
131 }
132
138 public Task<bool> WaitOne(int MillisecondsTimeout)
139 {
140 return this.Executor.Execute(this.WaitOneSync, MillisecondsTimeout);
141 }
142
143 private bool WaitOneSync(int MillisecondsTimeout)
144 {
145 return this.mutex.WaitOne(MillisecondsTimeout);
146 }
147
155 public Task<bool> WaitOne(int MillisecondsTimeout, bool ExitContext)
156 {
157 return this.Executor.Execute(this.WaitOneSync, MillisecondsTimeout, ExitContext);
158 }
159
160 private bool WaitOneSync(int MillisecondsTimeout, bool ExitContext)
161 {
162 return this.mutex.WaitOne(MillisecondsTimeout, ExitContext);
163 }
164
170 public Task<bool> WaitOne(TimeSpan Timeout)
171 {
172 return this.Executor.Execute<bool, TimeSpan>(this.WaitOneSync, Timeout);
173 }
174
175 private bool WaitOneSync(TimeSpan Timeout)
176 {
177 return this.mutex.WaitOne(Timeout);
178 }
179
187 public Task<bool> WaitOne(TimeSpan Timeout, bool ExitContext)
188 {
189 return this.Executor.Execute(this.WaitOneSync, Timeout, ExitContext);
190 }
191
192 private bool WaitOneSync(TimeSpan Timeout, bool ExitContext)
193 {
194 return this.mutex.WaitOne(Timeout, ExitContext);
195 }
196
200 public async Task ReleaseMutex()
201 {
202 await this.Executor.Execute(this.ReleaseMutexSync);
203 }
204
205 private bool ReleaseMutexSync()
206 {
207 this.mutex.ReleaseMutex();
208 return true;
209 }
210 }
211}
Asynchronous mutex class.
Definition: AsyncMutex.cs:11
Task< bool > WaitOne(int MillisecondsTimeout, bool ExitContext)
Waits for the Mutex to be free, and locks it.
Definition: AsyncMutex.cs:155
AsyncMutex(bool InitiallyOwned)
Asynchronous mutex class.
Definition: AsyncMutex.cs:28
static bool TryOpenExisting(string Name, out AsyncMutex Result)
Tries to open an existing Mutex.
Definition: AsyncMutex.cs:80
async Task ReleaseMutex()
Releases the mutex earlier aquired via a call to WaitOne.
Definition: AsyncMutex.cs:200
Task< bool > WaitOne(TimeSpan Timeout)
Waits for the Mutex to be free, and locks it.
Definition: AsyncMutex.cs:170
AsyncMutex(Mutex Mutex)
Asynchronous mutex class.
Definition: AsyncMutex.cs:58
static AsyncMutex OpenExisting(string Name)
Opens an existing Mutex.
Definition: AsyncMutex.cs:69
Task< bool > WaitOne(int MillisecondsTimeout)
Waits for the Mutex to be free, and locks it.
Definition: AsyncMutex.cs:138
AsyncMutex(bool InitiallyOwned, string Name, out bool CreatedNew)
Asynchronous mutex class.
Definition: AsyncMutex.cs:49
Task< bool > WaitOne(TimeSpan Timeout, bool ExitContext)
Waits for the Mutex to be free, and locks it.
Definition: AsyncMutex.cs:187
void Dispose()
IDisposable.Dispose
Definition: AsyncMutex.cs:110
AsyncMutex()
Asynchronous mutex class.
Definition: AsyncMutex.cs:19
Task< bool > WaitOne()
Waits for the Mutex to be free, and locks it.
Definition: AsyncMutex.cs:123
AsyncMutex(bool InitiallyOwned, string Name)
Asynchronous mutex class.
Definition: AsyncMutex.cs:38
Class that executes tasks from the same the same thread, and that provides an asynchronous interface ...
void Dispose()
Disposes of the object and terminates the thread executor.