2 // System.Threading.ReaderWriterLock.cs
5 // Dick Porter (dick@ximian.com)
6 // Jackson Harper (jackson@ximian.com)
7 // Lluis Sanchez Gual (lluis@ximian.com)
9 // (C) Ximian, Inc. http://www.ximian.com
10 // (C) 2004 Novell, Inc (http://www.novell.com)
14 // Copyright (C) 2004 Novell, Inc (http://www.novell.com)
16 // Permission is hereby granted, free of charge, to any person obtaining
17 // a copy of this software and associated documentation files (the
18 // "Software"), to deal in the Software without restriction, including
19 // without limitation the rights to use, copy, modify, merge, publish,
20 // distribute, sublicense, and/or sell copies of the Software, and to
21 // permit persons to whom the Software is furnished to do so, subject to
22 // the following conditions:
24 // The above copyright notice and this permission notice shall be
25 // included in all copies or substantial portions of the Software.
27 // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
28 // EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
29 // MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
30 // NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
31 // LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
32 // OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
33 // WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
36 using System.Collections;
38 namespace System.Threading
40 public sealed class ReaderWriterLock
42 private int seq_num = 1;
45 private LockQueue writer_queue;
46 private Hashtable reader_locks;
47 private int writer_lock_owner;
49 public ReaderWriterLock()
51 writer_queue = new LockQueue (this);
52 reader_locks = new Hashtable ();
55 public bool IsReaderLockHeld {
57 lock (this) return reader_locks.ContainsKey (Thread.CurrentThreadId);
61 public bool IsWriterLockHeld {
63 lock (this) return (state < 0 && Thread.CurrentThreadId == writer_lock_owner);
67 public int WriterSeqNum {
69 lock (this) return seq_num;
73 public void AcquireReaderLock (int millisecondsTimeout)
75 AcquireReaderLock (millisecondsTimeout, 1);
78 void AcquireReaderLock (int millisecondsTimeout, int initialLockCount)
81 if (HasWriterLock ()) {
82 AcquireWriterLock (millisecondsTimeout, initialLockCount);
86 object nlocks = reader_locks [Thread.CurrentThreadId];
89 // Not currently holding a reader lock
90 // Wait if there is a write lock
93 if (state < 0 || !writer_queue.IsEmpty) {
95 if (!Monitor.Wait (this, millisecondsTimeout))
96 throw new ApplicationException ("Timeout expired");
104 reader_locks [Thread.CurrentThreadId] = initialLockCount;
105 state += initialLockCount;
108 reader_locks [Thread.CurrentThreadId] = ((int)nlocks) + 1;
114 public void AcquireReaderLock(TimeSpan timeout)
116 int ms = CheckTimeout (timeout);
117 AcquireReaderLock (ms, 1);
120 public void AcquireWriterLock (int millisecondsTimeout)
122 AcquireWriterLock (millisecondsTimeout, 1);
125 void AcquireWriterLock (int millisecondsTimeout, int initialLockCount)
128 if (HasWriterLock ()) {
133 // wait while there are reader locks or another writer lock, or
134 // other threads waiting for the writer lock
135 if (state != 0 || !writer_queue.IsEmpty) {
137 if (!writer_queue.Wait (millisecondsTimeout))
138 throw new ApplicationException ("Timeout expired");
139 } while (state != 0);
142 state = -initialLockCount;
143 writer_lock_owner = Thread.CurrentThreadId;
148 public void AcquireWriterLock(TimeSpan timeout) {
149 int ms = CheckTimeout (timeout);
150 AcquireWriterLock (ms, 1);
153 public bool AnyWritersSince(int seqNum) {
155 return (this.seq_num > seqNum);
159 public void DowngradeFromWriterLock(ref LockCookie lockCookie)
162 if (!HasWriterLock())
163 throw new ApplicationException ("The thread does not have the writer lock.");
165 state = lockCookie.ReaderLocks;
166 reader_locks [Thread.CurrentThreadId] = state;
168 Monitor.PulseAll (this);
171 // MSDN: A thread does not block when downgrading from the writer lock,
172 // even if other threads are waiting for the writer lock
176 public LockCookie ReleaseLock()
180 cookie = GetLockCookie ();
181 if (cookie.WriterLocks != 0)
182 ReleaseWriterLock (cookie.WriterLocks);
183 else if (cookie.ReaderLocks != 0) {
184 ReleaseReaderLock (cookie.ReaderLocks, cookie.ReaderLocks);
190 public void ReleaseReaderLock()
193 if (HasWriterLock ()) {
194 ReleaseWriterLock ();
197 else if (state > 0) {
198 object read_lock_count = reader_locks [Thread.CurrentThreadId];
199 if (read_lock_count != null) {
200 ReleaseReaderLock ((int)read_lock_count, 1);
205 throw new ApplicationException ("The thread does not have any reader or writer locks.");
209 void ReleaseReaderLock (int currentCount, int releaseCount)
211 int new_count = currentCount - releaseCount;
214 reader_locks.Remove (Thread.CurrentThreadId);
216 reader_locks [Thread.CurrentThreadId] = new_count;
218 state -= releaseCount;
219 if (state == 0 && !writer_queue.IsEmpty)
220 writer_queue.Pulse ();
223 public void ReleaseWriterLock()
226 if (!HasWriterLock())
227 throw new ApplicationException ("The thread does not have the writer lock.");
229 ReleaseWriterLock (1);
233 void ReleaseWriterLock (int releaseCount)
235 state += releaseCount;
238 Monitor.PulseAll (this);
240 else if (!writer_queue.IsEmpty)
241 writer_queue.Pulse ();
245 public void RestoreLock(ref LockCookie lockCookie)
248 if (lockCookie.WriterLocks != 0)
249 AcquireWriterLock (-1, lockCookie.WriterLocks);
250 else if (lockCookie.ReaderLocks != 0)
251 AcquireReaderLock (-1, lockCookie.ReaderLocks);
255 public LockCookie UpgradeToWriterLock(int millisecondsTimeout)
259 cookie = GetLockCookie ();
260 if (cookie.WriterLocks != 0) {
265 if (cookie.ReaderLocks != 0)
266 ReleaseReaderLock (cookie.ReaderLocks, cookie.ReaderLocks);
269 // Don't do this inside the lock, since it can cause a deadlock.
270 AcquireWriterLock (millisecondsTimeout);
274 public LockCookie UpgradeToWriterLock(TimeSpan timeout)
276 int ms = CheckTimeout (timeout);
277 return UpgradeToWriterLock (ms);
280 LockCookie GetLockCookie ()
282 LockCookie cookie = new LockCookie (Thread.CurrentThreadId);
285 cookie.WriterLocks = -state;
287 object locks = reader_locks [Thread.CurrentThreadId];
288 if (locks != null) cookie.ReaderLocks = (int)locks;
293 bool HasWriterLock ()
295 return (state < 0 && Thread.CurrentThreadId == writer_lock_owner);
298 private int CheckTimeout (TimeSpan timeout)
300 int ms = (int) timeout.TotalMilliseconds;
303 throw new ArgumentOutOfRangeException ("timeout",
304 "Number must be either non-negative or -1");