@@ -0,0 +1,75 | |||||
|
1 | using System; | |||
|
2 | using System.Threading; | |||
|
3 | using System.Diagnostics; | |||
|
4 | ||||
|
5 | namespace Implab.Parallels { | |||
|
6 | /// <summary> | |||
|
7 | /// Implements a lightweight mechanism to aquire a shared or an exclusive lock. | |||
|
8 | /// </summary> | |||
|
9 | public class SharedLock { | |||
|
10 | readonly object m_lock = new object(); | |||
|
11 | int m_locks; | |||
|
12 | bool m_exclusive; | |||
|
13 | ||||
|
14 | public bool LockExclusive(int timeout) { | |||
|
15 | lock (m_lock) { | |||
|
16 | if (m_locks > 0 && !Monitor.Wait(m_lock, timeout)) | |||
|
17 | return false; | |||
|
18 | m_exclusive = true; | |||
|
19 | m_locks = 1; | |||
|
20 | } | |||
|
21 | } | |||
|
22 | ||||
|
23 | public void LockExclusive() { | |||
|
24 | LockExclusive(-1); | |||
|
25 | } | |||
|
26 | ||||
|
27 | public bool LockShared(int timeout) { | |||
|
28 | lock (m_lock) { | |||
|
29 | if (!m_exclusive) { | |||
|
30 | m_locks++; | |||
|
31 | return true; | |||
|
32 | } | |||
|
33 | ||||
|
34 | if (m_lock == 0) { | |||
|
35 | m_exclusive = false; | |||
|
36 | m_locks = 1; | |||
|
37 | return true; | |||
|
38 | } | |||
|
39 | ||||
|
40 | if (Monitor.Wait(m_lock, timeout)) { | |||
|
41 | Debug.Assert(m_locks == 0); | |||
|
42 | m_locks = 1; | |||
|
43 | m_exclusive = false; | |||
|
44 | return true; | |||
|
45 | } | |||
|
46 | return false; | |||
|
47 | } | |||
|
48 | } | |||
|
49 | ||||
|
50 | public void LockShared() { | |||
|
51 | LockShared(-1); | |||
|
52 | } | |||
|
53 | ||||
|
54 | public void ReleaseShared() { | |||
|
55 | lock (m_lock) { | |||
|
56 | if (m_exclusive || m_locks <= 0) | |||
|
57 | throw new InvalidOperationException(); | |||
|
58 | m_locks--; | |||
|
59 | if (m_locks == 0) | |||
|
60 | Monitor.PulseAll(m_lock); | |||
|
61 | } | |||
|
62 | } | |||
|
63 | ||||
|
64 | public void ReleaseExclusive() { | |||
|
65 | lock (m_lock) { | |||
|
66 | if (!m_exclusive && m_locks != 1) | |||
|
67 | throw new InvalidOperationException(); | |||
|
68 | m_locks = 0; | |||
|
69 | Monitor.PulseAll(m_lock); | |||
|
70 | } | |||
|
71 | } | |||
|
72 | ||||
|
73 | } | |||
|
74 | } | |||
|
75 |
@@ -143,8 +143,6 | |||||
143 | <Compile Include="Diagnostics\Extensions.cs" /> |
|
143 | <Compile Include="Diagnostics\Extensions.cs" /> | |
144 | <Compile Include="IComponentContainer.cs" /> |
|
144 | <Compile Include="IComponentContainer.cs" /> | |
145 | <Compile Include="PromiseEventType.cs" /> |
|
145 | <Compile Include="PromiseEventType.cs" /> | |
146 | <Compile Include="Parallels\MTCustomQueue.cs" /> |
|
|||
147 | <Compile Include="Parallels\MTCustomQueueNode.cs" /> |
|
|||
148 | <Compile Include="ComponentContainer.cs" /> |
|
146 | <Compile Include="ComponentContainer.cs" /> | |
149 | <Compile Include="DisposablePool.cs" /> |
|
147 | <Compile Include="DisposablePool.cs" /> | |
150 | <Compile Include="ObjectPool.cs" /> |
|
148 | <Compile Include="ObjectPool.cs" /> | |
@@ -156,6 +154,7 | |||||
156 | <Compile Include="Promise.cs" /> |
|
154 | <Compile Include="Promise.cs" /> | |
157 | <Compile Include="PromiseTransientException.cs" /> |
|
155 | <Compile Include="PromiseTransientException.cs" /> | |
158 | <Compile Include="Parallels\Signal.cs" /> |
|
156 | <Compile Include="Parallels\Signal.cs" /> | |
|
157 | <Compile Include="Parallels\SharedLock.cs" /> | |||
159 | </ItemGroup> |
|
158 | </ItemGroup> | |
160 | <Import Project="$(MSBuildBinPath)\Microsoft.CSharp.targets" /> |
|
159 | <Import Project="$(MSBuildBinPath)\Microsoft.CSharp.targets" /> | |
161 | <ItemGroup /> |
|
160 | <ItemGroup /> |
@@ -495,11 +495,11 namespace Implab.Parallels { | |||||
495 | #region ICollection implementation |
|
495 | #region ICollection implementation | |
496 |
|
496 | |||
497 | public void Add(T item) { |
|
497 | public void Add(T item) { | |
498 |
throw new |
|
498 | throw new NotSupportedException(); | |
499 | } |
|
499 | } | |
500 |
|
500 | |||
501 | public void Clear() { |
|
501 | public void Clear() { | |
502 |
throw new |
|
502 | throw new NotSupportedException(); | |
503 | } |
|
503 | } | |
504 |
|
504 | |||
505 | public bool Contains(T item) { |
|
505 | public bool Contains(T item) { | |
@@ -511,7 +511,7 namespace Implab.Parallels { | |||||
511 | } |
|
511 | } | |
512 |
|
512 | |||
513 | public bool Remove(T item) { |
|
513 | public bool Remove(T item) { | |
514 |
throw new Not |
|
514 | throw new NotSupportedException(); | |
515 | } |
|
515 | } | |
516 |
|
516 | |||
517 | public int Count { |
|
517 | public int Count { |
@@ -3,7 +3,7 using System.Threading; | |||||
3 |
|
3 | |||
4 | namespace Implab.Parallels { |
|
4 | namespace Implab.Parallels { | |
5 | /// <summary> |
|
5 | /// <summary> | |
6 | /// Implements simple signalling logic using <see cref="Monitor.PulseAll(object)"/>. |
|
6 | /// Implements a simple signalling logic using <see cref="Monitor.PulseAll(object)"/>. | |
7 | /// </summary> |
|
7 | /// </summary> | |
8 | public class Signal { |
|
8 | public class Signal { | |
9 | readonly object m_lock = new object(); |
|
9 | readonly object m_lock = new object(); |
@@ -7,7 +7,7 namespace Implab.Parallels { | |||||
7 | public class WorkerPool : DispatchPool<Action> { |
|
7 | public class WorkerPool : DispatchPool<Action> { | |
8 |
|
8 | |||
9 | AsyncQueue<Action> m_queue = new AsyncQueue<Action>(); |
|
9 | AsyncQueue<Action> m_queue = new AsyncQueue<Action>(); | |
10 |
int m_queueLength |
|
10 | int m_queueLength; | |
11 | readonly int m_threshold = 1; |
|
11 | readonly int m_threshold = 1; | |
12 |
|
12 | |||
13 | public WorkerPool(int minThreads, int maxThreads, int threshold) |
|
13 | public WorkerPool(int minThreads, int maxThreads, int threshold) | |
@@ -40,7 +40,7 namespace Implab.Parallels { | |||||
40 |
|
40 | |||
41 | var lop = TraceContext.Instance.CurrentOperation; |
|
41 | var lop = TraceContext.Instance.CurrentOperation; | |
42 |
|
42 | |||
43 |
EnqueueTask(delegate |
|
43 | EnqueueTask(delegate { | |
44 | TraceContext.Instance.EnterLogicalOperation(lop, false); |
|
44 | TraceContext.Instance.EnterLogicalOperation(lop, false); | |
45 | try { |
|
45 | try { | |
46 | promise.Resolve(task()); |
|
46 | promise.Resolve(task()); |
1 | NO CONTENT: file was removed |
|
NO CONTENT: file was removed |
1 | NO CONTENT: file was removed |
|
NO CONTENT: file was removed |
General Comments 0
You need to be logged in to leave comments.
Login now