Neuron®
The Neuron® is the basis for the creation of open and secure federated networks for smart societies.
Loading...
Searching...
No Matches
Semaphores.cs
1using System;
2using System.Threading.Tasks;
5
7{
16 public static class Semaphores
17 {
18 private static readonly Cache<string, MultiReadSingleWriteObject> semaphores;
19
20 static Semaphores()
21 {
22 semaphores = new Cache<string, MultiReadSingleWriteObject>(int.MaxValue, TimeSpan.MaxValue, TimeSpan.FromHours(1), true);
23 semaphores.Removed += Semaphores_Removed;
24 }
25
26 private static Task Semaphores_Removed(object Sender, CacheItemEventArgs<string, MultiReadSingleWriteObject> e)
27 {
28 e.Value.Dispose();
29
30 return Task.CompletedTask;
31 }
32
33 private static MultiReadSingleWriteObject GetSemaphore(string Key)
34 {
35 lock (semaphores)
36 {
37 if (semaphores.TryGetValue(Key, out MultiReadSingleWriteObject Result))
38 return Result;
39
40 Result = new MultiReadSingleWriteObject(Key);
41 semaphores[Key] = Result;
42
43 return Result;
44 }
45 }
46
54 public static async Task<Semaphore> BeginRead(string Key)
55 {
56 await GetSemaphore(Key).BeginRead();
57 return new Semaphore(Key, 1);
58 }
59
66 public static Task EndRead(string Key)
67 {
68 return GetSemaphore(Key).EndRead();
69 }
70
79 public static Task<bool> TryBeginRead(string Key, int Timeout)
80 {
81 return GetSemaphore(Key).TryBeginRead(Timeout);
82 }
83
91 public static async Task<Semaphore> BeginWrite(string Key)
92 {
93 await GetSemaphore(Key).BeginWrite();
94 return new Semaphore(Key, true);
95 }
96
103 public static Task EndWrite(string Key)
104 {
105 return GetSemaphore(Key).EndWrite();
106 }
107
116 public static Task<bool> TryBeginWrite(string Key, int Timeout)
117 {
118 return GetSemaphore(Key).TryBeginWrite(Timeout);
119 }
120
127 public static MultiReadSingleWriteObject[] FindLockedObjects(int MillisecondsThreshold)
128 {
129 return FindLockedObjects(MillisecondsThreshold, true);
130 }
131
140 public static MultiReadSingleWriteObject[] FindLockedObjects(int MillisecondsThreshold,
141 bool OnlyIfWaitingTasks)
142 {
143 if (MillisecondsThreshold <= 0)
144 throw new ArgumentOutOfRangeException("MilliSecondsThreshold must be greater than zero.", nameof(MillisecondsThreshold));
145
147
148 foreach (MultiReadSingleWriteObject Obj in semaphores.Values)
149 {
150 if (Obj.MillisecondsLocked > MillisecondsThreshold)
151 {
152 if (OnlyIfWaitingTasks && Obj.QueueSize == 0)
153 continue;
154
155 Result.Add(Obj);
156 }
157 }
158
159 return Result.ToArray();
160 }
161 }
162}
Implements an in-memory cache.
Definition: Cache.cs:17
Event arguments for cache item removal events.
ValueType Value
Value of item that was removed.
A chunked list is a linked list of chunks of objects of type T .
Definition: ChunkedList.cs:54
Represents an object that allows single concurrent writers but multiple concurrent readers....
virtual async Task< bool > TryBeginRead(int Timeout)
Waits, at most Timeout milliseconds, until object ready for reading. Each successful call to TryBegi...
virtual Task EndWrite()
Ends a writing session of the object. Must be called once for each call to BeginWrite or successful c...
double MillisecondsLocked
Number of milliseconds the object has been locked.
virtual async Task< bool > TryBeginWrite(int Timeout)
Waits, at most Timeout milliseconds, until object ready for writing. Each successful call to TryBegi...
virtual async Task BeginWrite()
Waits until object ready for writing. Each call to BeginWrite must be followed by exactly one call to...
virtual Task< int > EndRead()
Ends a reading session of the object. Must be called once for each call to BeginRead or successful ca...
virtual async Task< int > BeginRead()
Waits until object ready for reading. Each call to BeginRead must be followed by exactly one call to ...
Represents a named semaphore, i.e. an object, identified by a name, that allows single concurrent wri...
Definition: Semaphore.cs:19
Static class of application-wide semaphores that can be used to order access to editable objects.
Definition: Semaphores.cs:17
static MultiReadSingleWriteObject[] FindLockedObjects(int MillisecondsThreshold, bool OnlyIfWaitingTasks)
Returns an array of objects that have been locked for more time than the specified threshold.
Definition: Semaphores.cs:140
static MultiReadSingleWriteObject[] FindLockedObjects(int MillisecondsThreshold)
Returns an array of objects that have been locked for more time than the specified threshold.
Definition: Semaphores.cs:127
static Task< bool > TryBeginRead(string Key, int Timeout)
Waits, at most Timeout milliseconds, until the semaphore identified by Key is ready for reading....
Definition: Semaphores.cs:79
static async Task< Semaphore > BeginRead(string Key)
Waits until the semaphore identified by Key is ready for reading. Each call to BeginRead must be fol...
Definition: Semaphores.cs:54
static Task< bool > TryBeginWrite(string Key, int Timeout)
Waits, at most Timeout milliseconds, until the semaphore identified by Key is ready for writing....
Definition: Semaphores.cs:116
static Task EndRead(string Key)
Ends a reading session of the semaphore identified by Key . Must be called once for each call to Begi...
Definition: Semaphores.cs:66
static Task EndWrite(string Key)
Ends a writing session of the semaphore identified by Key . Must be called once for each call to Begi...
Definition: Semaphores.cs:103
static async Task< Semaphore > BeginWrite(string Key)
Waits until the semaphore identified by Key is ready for writing. Each call to BeginWrite must be fo...
Definition: Semaphores.cs:91