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;
4
6{
15 public static class Semaphores
16 {
17 private static readonly Cache<string, MultiReadSingleWriteObject> semaphores;
18
19 static Semaphores()
20 {
21 semaphores = new Cache<string, MultiReadSingleWriteObject>(int.MaxValue, TimeSpan.MaxValue, TimeSpan.FromHours(1), true);
22 semaphores.Removed += Semaphores_Removed;
23 }
24
25 private static Task Semaphores_Removed(object Sender, CacheItemEventArgs<string, MultiReadSingleWriteObject> e)
26 {
27 e.Value.Dispose();
28
29 return Task.CompletedTask;
30 }
31
32 private static MultiReadSingleWriteObject GetSemaphore(string Key)
33 {
34 lock (semaphores)
35 {
36 if (semaphores.TryGetValue(Key, out MultiReadSingleWriteObject Result))
37 return Result;
38
39 Result = new MultiReadSingleWriteObject(Key);
40 semaphores[Key] = Result;
41
42 return Result;
43 }
44 }
45
53 public static async Task<Semaphore> BeginRead(string Key)
54 {
55 await GetSemaphore(Key).BeginRead();
56 return new Semaphore(Key, 1);
57 }
58
65 public static Task EndRead(string Key)
66 {
67 return GetSemaphore(Key).EndRead();
68 }
69
78 public static Task<bool> TryBeginRead(string Key, int Timeout)
79 {
80 return GetSemaphore(Key).TryBeginRead(Timeout);
81 }
82
90 public static async Task<Semaphore> BeginWrite(string Key)
91 {
92 await GetSemaphore(Key).BeginWrite();
93 return new Semaphore(Key, true);
94 }
95
102 public static Task EndWrite(string Key)
103 {
104 return GetSemaphore(Key).EndWrite();
105 }
106
115 public static Task<bool> TryBeginWrite(string Key, int Timeout)
116 {
117 return GetSemaphore(Key).TryBeginWrite(Timeout);
118 }
119 }
120}
Implements an in-memory cache.
Definition: Cache.cs:15
Event arguments for cache item removal events.
ValueType Value
Value of item that was removed.
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...
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:16
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:78
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:53
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:115
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:65
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:102
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:90