-
Notifications
You must be signed in to change notification settings - Fork 76
/
Copy pathSemaphores.cs
120 lines (107 loc) · 4.3 KB
/
Semaphores.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
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
using System;
using System.Threading.Tasks;
using Waher.Runtime.Cache;
namespace Waher.Runtime.Threading
{
/// <summary>
/// Static class of application-wide semaphores that can be used to order access
/// to editable objects.
///
/// Semaphores are created dynamically, and identified by a string Key, that must
/// be unique for each semaphore. A semaphore that has not been used for an hour
/// is automatically disposed (but can be recreated later).
/// </summary>
public static class Semaphores
{
private static readonly Cache<string, MultiReadSingleWriteObject> semaphores;
static Semaphores()
{
semaphores = new Cache<string, MultiReadSingleWriteObject>(int.MaxValue, TimeSpan.MaxValue, TimeSpan.FromHours(1), true);
semaphores.Removed += Semaphores_Removed;
}
private static Task Semaphores_Removed(object Sender, CacheItemEventArgs<string, MultiReadSingleWriteObject> e)
{
e.Value.Dispose();
return Task.CompletedTask;
}
private static MultiReadSingleWriteObject GetSemaphore(string Key)
{
lock (semaphores)
{
if (semaphores.TryGetValue(Key, out MultiReadSingleWriteObject Result))
return Result;
Result = new MultiReadSingleWriteObject(Key);
semaphores[Key] = Result;
return Result;
}
}
/// <summary>
/// Waits until the semaphore identified by <paramref name="Key"/> is ready for reading.
/// Each call to <see cref="BeginRead"/> must be followed by exactly one call to <see cref="EndRead"/>
/// with the same <paramref name="Key"/>.
/// </summary>
/// <param name="Key">Semaphore key.</param>
/// <returns>Semaphore object that can be used for managing the semaphore.</returns>
public static async Task<Semaphore> BeginRead(string Key)
{
await GetSemaphore(Key).BeginRead();
return new Semaphore(Key, 1);
}
/// <summary>
/// Ends a reading session of the semaphore identified by <paramref name="Key"/>.
/// Must be called once for each call to <see cref="BeginRead"/> or successful call to <see cref="TryBeginRead(string, int)"/>
/// with the same <paramref name="Key"/>.
/// </summary>
/// <param name="Key">Semaphore key.</param>
public static Task EndRead(string Key)
{
return GetSemaphore(Key).EndRead();
}
/// <summary>
/// Waits, at most <paramref name="Timeout"/> milliseconds, until
/// the semaphore identified by <paramref name="Key"/> is ready for reading.
/// Each successful call to <see cref="TryBeginRead"/> must be followed by
/// exactly one call to <see cref="EndRead"/> with the same <paramref name="Key"/>.
/// </summary>
/// <param name="Timeout">Timeout, in milliseconds.</param>
/// <param name="Key">Semaphore key.</param>
public static Task<bool> TryBeginRead(string Key, int Timeout)
{
return GetSemaphore(Key).TryBeginRead(Timeout);
}
/// <summary>
/// Waits until the semaphore identified by <paramref name="Key"/> is ready for writing.
/// Each call to <see cref="BeginWrite"/> must be followed by exactly one call to <see cref="EndWrite"/>
/// with the same <paramref name="Key"/>.
/// </summary>
/// <param name="Key">Semaphore key.</param>
/// <returns>Semaphore object that can be used for managing the semaphore.</returns>
public static async Task<Semaphore> BeginWrite(string Key)
{
await GetSemaphore(Key).BeginWrite();
return new Semaphore(Key, true);
}
/// <summary>
/// Ends a writing session of the semaphore identified by <paramref name="Key"/>.
/// Must be called once for each call to <see cref="BeginWrite"/> or successful call to <see cref="TryBeginWrite(string, int)"/>
/// with the same <paramref name="Key"/>.
/// </summary>
/// <param name="Key">Semaphore key.</param>
public static Task EndWrite(string Key)
{
return GetSemaphore(Key).EndWrite();
}
/// <summary>
/// Waits, at most <paramref name="Timeout"/> milliseconds, until
/// the semaphore identified by <paramref name="Key"/> is ready for writing.
/// Each successful call to <see cref="TryBeginWrite"/> must be followed by
/// exactly one call to <see cref="EndWrite"/> with the same <paramref name="Key"/>.
/// </summary>
/// <param name="Timeout">Timeout, in milliseconds.</param>
/// <param name="Key">Semaphore key.</param>
public static Task<bool> TryBeginWrite(string Key, int Timeout)
{
return GetSemaphore(Key).TryBeginWrite(Timeout);
}
}
}