annotate Implab/Parallels/SharedLock.cs @ 129:471f596b2603 v2

Added SharedLock to synchronization routines
author cin
date Thu, 29 Jan 2015 18:31:06 +0300
parents
children 671f60cd0250
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
129
471f596b2603 Added SharedLock to synchronization routines
cin
parents:
diff changeset
1 using System;
471f596b2603 Added SharedLock to synchronization routines
cin
parents:
diff changeset
2 using System.Threading;
471f596b2603 Added SharedLock to synchronization routines
cin
parents:
diff changeset
3 using System.Diagnostics;
471f596b2603 Added SharedLock to synchronization routines
cin
parents:
diff changeset
4
471f596b2603 Added SharedLock to synchronization routines
cin
parents:
diff changeset
5 namespace Implab.Parallels {
471f596b2603 Added SharedLock to synchronization routines
cin
parents:
diff changeset
6 /// <summary>
471f596b2603 Added SharedLock to synchronization routines
cin
parents:
diff changeset
7 /// Implements a lightweight mechanism to aquire a shared or an exclusive lock.
471f596b2603 Added SharedLock to synchronization routines
cin
parents:
diff changeset
8 /// </summary>
471f596b2603 Added SharedLock to synchronization routines
cin
parents:
diff changeset
9 public class SharedLock {
471f596b2603 Added SharedLock to synchronization routines
cin
parents:
diff changeset
10 readonly object m_lock = new object();
471f596b2603 Added SharedLock to synchronization routines
cin
parents:
diff changeset
11 int m_locks;
471f596b2603 Added SharedLock to synchronization routines
cin
parents:
diff changeset
12 bool m_exclusive;
471f596b2603 Added SharedLock to synchronization routines
cin
parents:
diff changeset
13
471f596b2603 Added SharedLock to synchronization routines
cin
parents:
diff changeset
14 public bool LockExclusive(int timeout) {
471f596b2603 Added SharedLock to synchronization routines
cin
parents:
diff changeset
15 lock (m_lock) {
471f596b2603 Added SharedLock to synchronization routines
cin
parents:
diff changeset
16 if (m_locks > 0 && !Monitor.Wait(m_lock, timeout))
471f596b2603 Added SharedLock to synchronization routines
cin
parents:
diff changeset
17 return false;
471f596b2603 Added SharedLock to synchronization routines
cin
parents:
diff changeset
18 m_exclusive = true;
471f596b2603 Added SharedLock to synchronization routines
cin
parents:
diff changeset
19 m_locks = 1;
471f596b2603 Added SharedLock to synchronization routines
cin
parents:
diff changeset
20 }
471f596b2603 Added SharedLock to synchronization routines
cin
parents:
diff changeset
21 }
471f596b2603 Added SharedLock to synchronization routines
cin
parents:
diff changeset
22
471f596b2603 Added SharedLock to synchronization routines
cin
parents:
diff changeset
23 public void LockExclusive() {
471f596b2603 Added SharedLock to synchronization routines
cin
parents:
diff changeset
24 LockExclusive(-1);
471f596b2603 Added SharedLock to synchronization routines
cin
parents:
diff changeset
25 }
471f596b2603 Added SharedLock to synchronization routines
cin
parents:
diff changeset
26
471f596b2603 Added SharedLock to synchronization routines
cin
parents:
diff changeset
27 public bool LockShared(int timeout) {
471f596b2603 Added SharedLock to synchronization routines
cin
parents:
diff changeset
28 lock (m_lock) {
471f596b2603 Added SharedLock to synchronization routines
cin
parents:
diff changeset
29 if (!m_exclusive) {
471f596b2603 Added SharedLock to synchronization routines
cin
parents:
diff changeset
30 m_locks++;
471f596b2603 Added SharedLock to synchronization routines
cin
parents:
diff changeset
31 return true;
471f596b2603 Added SharedLock to synchronization routines
cin
parents:
diff changeset
32 }
471f596b2603 Added SharedLock to synchronization routines
cin
parents:
diff changeset
33
471f596b2603 Added SharedLock to synchronization routines
cin
parents:
diff changeset
34 if (m_lock == 0) {
471f596b2603 Added SharedLock to synchronization routines
cin
parents:
diff changeset
35 m_exclusive = false;
471f596b2603 Added SharedLock to synchronization routines
cin
parents:
diff changeset
36 m_locks = 1;
471f596b2603 Added SharedLock to synchronization routines
cin
parents:
diff changeset
37 return true;
471f596b2603 Added SharedLock to synchronization routines
cin
parents:
diff changeset
38 }
471f596b2603 Added SharedLock to synchronization routines
cin
parents:
diff changeset
39
471f596b2603 Added SharedLock to synchronization routines
cin
parents:
diff changeset
40 if (Monitor.Wait(m_lock, timeout)) {
471f596b2603 Added SharedLock to synchronization routines
cin
parents:
diff changeset
41 Debug.Assert(m_locks == 0);
471f596b2603 Added SharedLock to synchronization routines
cin
parents:
diff changeset
42 m_locks = 1;
471f596b2603 Added SharedLock to synchronization routines
cin
parents:
diff changeset
43 m_exclusive = false;
471f596b2603 Added SharedLock to synchronization routines
cin
parents:
diff changeset
44 return true;
471f596b2603 Added SharedLock to synchronization routines
cin
parents:
diff changeset
45 }
471f596b2603 Added SharedLock to synchronization routines
cin
parents:
diff changeset
46 return false;
471f596b2603 Added SharedLock to synchronization routines
cin
parents:
diff changeset
47 }
471f596b2603 Added SharedLock to synchronization routines
cin
parents:
diff changeset
48 }
471f596b2603 Added SharedLock to synchronization routines
cin
parents:
diff changeset
49
471f596b2603 Added SharedLock to synchronization routines
cin
parents:
diff changeset
50 public void LockShared() {
471f596b2603 Added SharedLock to synchronization routines
cin
parents:
diff changeset
51 LockShared(-1);
471f596b2603 Added SharedLock to synchronization routines
cin
parents:
diff changeset
52 }
471f596b2603 Added SharedLock to synchronization routines
cin
parents:
diff changeset
53
471f596b2603 Added SharedLock to synchronization routines
cin
parents:
diff changeset
54 public void ReleaseShared() {
471f596b2603 Added SharedLock to synchronization routines
cin
parents:
diff changeset
55 lock (m_lock) {
471f596b2603 Added SharedLock to synchronization routines
cin
parents:
diff changeset
56 if (m_exclusive || m_locks <= 0)
471f596b2603 Added SharedLock to synchronization routines
cin
parents:
diff changeset
57 throw new InvalidOperationException();
471f596b2603 Added SharedLock to synchronization routines
cin
parents:
diff changeset
58 m_locks--;
471f596b2603 Added SharedLock to synchronization routines
cin
parents:
diff changeset
59 if (m_locks == 0)
471f596b2603 Added SharedLock to synchronization routines
cin
parents:
diff changeset
60 Monitor.PulseAll(m_lock);
471f596b2603 Added SharedLock to synchronization routines
cin
parents:
diff changeset
61 }
471f596b2603 Added SharedLock to synchronization routines
cin
parents:
diff changeset
62 }
471f596b2603 Added SharedLock to synchronization routines
cin
parents:
diff changeset
63
471f596b2603 Added SharedLock to synchronization routines
cin
parents:
diff changeset
64 public void ReleaseExclusive() {
471f596b2603 Added SharedLock to synchronization routines
cin
parents:
diff changeset
65 lock (m_lock) {
471f596b2603 Added SharedLock to synchronization routines
cin
parents:
diff changeset
66 if (!m_exclusive && m_locks != 1)
471f596b2603 Added SharedLock to synchronization routines
cin
parents:
diff changeset
67 throw new InvalidOperationException();
471f596b2603 Added SharedLock to synchronization routines
cin
parents:
diff changeset
68 m_locks = 0;
471f596b2603 Added SharedLock to synchronization routines
cin
parents:
diff changeset
69 Monitor.PulseAll(m_lock);
471f596b2603 Added SharedLock to synchronization routines
cin
parents:
diff changeset
70 }
471f596b2603 Added SharedLock to synchronization routines
cin
parents:
diff changeset
71 }
471f596b2603 Added SharedLock to synchronization routines
cin
parents:
diff changeset
72
471f596b2603 Added SharedLock to synchronization routines
cin
parents:
diff changeset
73 }
471f596b2603 Added SharedLock to synchronization routines
cin
parents:
diff changeset
74 }
471f596b2603 Added SharedLock to synchronization routines
cin
parents:
diff changeset
75