annotate Implab/Parallels/SharedLock.cs @ 130:671f60cd0250 v2

fixed Resove method bug when calling it on already cancelled promise
author cin
date Fri, 30 Jan 2015 17:07:17 +0300
parents 471f596b2603
children e9e7940c7d98
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;
130
671f60cd0250 fixed Resove method bug when calling it on already cancelled promise
cin
parents: 129
diff changeset
20 return true;
129
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
471f596b2603 Added SharedLock to synchronization routines
cin
parents:
diff changeset
24 public void LockExclusive() {
471f596b2603 Added SharedLock to synchronization routines
cin
parents:
diff changeset
25 LockExclusive(-1);
471f596b2603 Added SharedLock to synchronization routines
cin
parents:
diff changeset
26 }
471f596b2603 Added SharedLock to synchronization routines
cin
parents:
diff changeset
27
471f596b2603 Added SharedLock to synchronization routines
cin
parents:
diff changeset
28 public bool LockShared(int timeout) {
471f596b2603 Added SharedLock to synchronization routines
cin
parents:
diff changeset
29 lock (m_lock) {
471f596b2603 Added SharedLock to synchronization routines
cin
parents:
diff changeset
30 if (!m_exclusive) {
471f596b2603 Added SharedLock to synchronization routines
cin
parents:
diff changeset
31 m_locks++;
471f596b2603 Added SharedLock to synchronization routines
cin
parents:
diff changeset
32 return true;
471f596b2603 Added SharedLock to synchronization routines
cin
parents:
diff changeset
33 }
471f596b2603 Added SharedLock to synchronization routines
cin
parents:
diff changeset
34
130
671f60cd0250 fixed Resove method bug when calling it on already cancelled promise
cin
parents: 129
diff changeset
35 if (m_locks == 0) {
129
471f596b2603 Added SharedLock to synchronization routines
cin
parents:
diff changeset
36 m_exclusive = false;
471f596b2603 Added SharedLock to synchronization routines
cin
parents:
diff changeset
37 m_locks = 1;
471f596b2603 Added SharedLock to synchronization routines
cin
parents:
diff changeset
38 return true;
471f596b2603 Added SharedLock to synchronization routines
cin
parents:
diff changeset
39 }
471f596b2603 Added SharedLock to synchronization routines
cin
parents:
diff changeset
40
471f596b2603 Added SharedLock to synchronization routines
cin
parents:
diff changeset
41 if (Monitor.Wait(m_lock, timeout)) {
471f596b2603 Added SharedLock to synchronization routines
cin
parents:
diff changeset
42 Debug.Assert(m_locks == 0);
471f596b2603 Added SharedLock to synchronization routines
cin
parents:
diff changeset
43 m_locks = 1;
471f596b2603 Added SharedLock to synchronization routines
cin
parents:
diff changeset
44 m_exclusive = false;
471f596b2603 Added SharedLock to synchronization routines
cin
parents:
diff changeset
45 return true;
471f596b2603 Added SharedLock to synchronization routines
cin
parents:
diff changeset
46 }
471f596b2603 Added SharedLock to synchronization routines
cin
parents:
diff changeset
47 return false;
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
471f596b2603 Added SharedLock to synchronization routines
cin
parents:
diff changeset
51 public void LockShared() {
471f596b2603 Added SharedLock to synchronization routines
cin
parents:
diff changeset
52 LockShared(-1);
471f596b2603 Added SharedLock to synchronization routines
cin
parents:
diff changeset
53 }
471f596b2603 Added SharedLock to synchronization routines
cin
parents:
diff changeset
54
471f596b2603 Added SharedLock to synchronization routines
cin
parents:
diff changeset
55 public void ReleaseShared() {
471f596b2603 Added SharedLock to synchronization routines
cin
parents:
diff changeset
56 lock (m_lock) {
471f596b2603 Added SharedLock to synchronization routines
cin
parents:
diff changeset
57 if (m_exclusive || m_locks <= 0)
471f596b2603 Added SharedLock to synchronization routines
cin
parents:
diff changeset
58 throw new InvalidOperationException();
471f596b2603 Added SharedLock to synchronization routines
cin
parents:
diff changeset
59 m_locks--;
471f596b2603 Added SharedLock to synchronization routines
cin
parents:
diff changeset
60 if (m_locks == 0)
471f596b2603 Added SharedLock to synchronization routines
cin
parents:
diff changeset
61 Monitor.PulseAll(m_lock);
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
471f596b2603 Added SharedLock to synchronization routines
cin
parents:
diff changeset
65 public void ReleaseExclusive() {
471f596b2603 Added SharedLock to synchronization routines
cin
parents:
diff changeset
66 lock (m_lock) {
471f596b2603 Added SharedLock to synchronization routines
cin
parents:
diff changeset
67 if (!m_exclusive && m_locks != 1)
471f596b2603 Added SharedLock to synchronization routines
cin
parents:
diff changeset
68 throw new InvalidOperationException();
471f596b2603 Added SharedLock to synchronization routines
cin
parents:
diff changeset
69 m_locks = 0;
471f596b2603 Added SharedLock to synchronization routines
cin
parents:
diff changeset
70 Monitor.PulseAll(m_lock);
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 }
471f596b2603 Added SharedLock to synchronization routines
cin
parents:
diff changeset
76