2 // System.Threading.ReaderWriterLockSlim.cs
5 // Miguel de Icaza (miguel@novell.com)
6 // Dick Porter (dick@ximian.com)
7 // Jackson Harper (jackson@ximian.com)
8 // Lluis Sanchez Gual (lluis@ximian.com)
9 // Marek Safar (marek.safar@gmail.com)
11 // Copyright 2004-2008 Novell, Inc (http://www.novell.com)
12 // Copyright 2003, Ximian, Inc.
14 // NoRecursion code based on the blog post from Vance Morrison:
15 // http://blogs.msdn.com/vancem/archive/2006/03/28/563180.aspx
17 // Recursion code based on Mono's implementation of ReaderWriterLock.
19 // Permission is hereby granted, free of charge, to any person obtaining
20 // a copy of this software and associated documentation files (the
21 // "Software"), to deal in the Software without restriction, including
22 // without limitation the rights to use, copy, modify, merge, publish,
23 // distribute, sublicense, and/or sell copies of the Software, and to
24 // permit persons to whom the Software is furnished to do so, subject to
25 // the following conditions:
27 // The above copyright notice and this permission notice shall be
28 // included in all copies or substantial portions of the Software.
30 // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
31 // EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
32 // MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
33 // NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
34 // LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
35 // OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
36 // WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
40 using System.Collections;
41 using System.Collections.Generic;
42 using System.Security.Permissions;
43 using System.Diagnostics;
44 using System.Threading;
46 namespace System.Threading {
49 // This implementation is based on the light-weight
50 // Reader/Writer lock sample from Vance Morrison's blog:
52 // http://blogs.msdn.com/vancem/archive/2006/03/28/563180.aspx
54 // And in Mono's ReaderWriterLock
56 [HostProtectionAttribute(SecurityAction.LinkDemand, MayLeakOnAbort = true)]
57 [HostProtectionAttribute(SecurityAction.LinkDemand, Synchronization = true, ExternalThreading = true)]
58 public class ReaderWriterLockSlim : IDisposable {
59 // Are we on a multiprocessor?
60 static readonly bool smp;
62 // Lock specifiation for myLock: This lock protects exactly the local fields associted
63 // instance of MyReaderWriterLock. It does NOT protect the memory associted with the
64 // the events that hang off this lock (eg writeEvent, readEvent upgradeEvent).
67 // Who owns the lock owners > 0 => readers
68 // owners = -1 means there is one writer, Owners must be >= -1.
70 Thread upgradable_thread;
72 // These variables allow use to avoid Setting events (which is expensive) if we don't have to.
73 uint numWriteWaiters; // maximum number of threads that can be doing a WaitOne on the writeEvent
74 uint numReadWaiters; // maximum number of threads that can be doing a WaitOne on the readEvent
75 uint numUpgradeWaiters; // maximum number of threads that can be doing a WaitOne on the upgradeEvent (at most 1).
77 // conditions we wait on.
78 EventWaitHandle writeEvent; // threads waiting to aquire a write lock go here.
79 EventWaitHandle readEvent; // threads waiting to aquire a read lock go here (will be released in bulk)
80 EventWaitHandle upgradeEvent; // thread waiting to upgrade a read lock to a write lock go here (at most one)
84 // Only set if we are a recursive lock
85 //Dictionary<int,int> reader_locks;
87 readonly LockRecursionPolicy recursionPolicy;
90 static ReaderWriterLockSlim ()
92 smp = Environment.ProcessorCount > 1;
95 public ReaderWriterLockSlim ()
97 // NoRecursion (0) is the default value
100 public ReaderWriterLockSlim (LockRecursionPolicy recursionPolicy)
102 this.recursionPolicy = recursionPolicy;
104 if (recursionPolicy != LockRecursionPolicy.NoRecursion){
105 //reader_locks = new Dictionary<int,int> ();
106 throw new NotImplementedException ("recursionPolicy != NoRecursion not currently implemented");
110 public void EnterReadLock ()
112 TryEnterReadLock (-1);
115 public bool TryEnterReadLock (int millisecondsTimeout)
117 if (millisecondsTimeout < Timeout.Infinite)
118 throw new ArgumentOutOfRangeException ("millisecondsTimeout");
121 throw new ObjectDisposedException (null);
126 // Easy case, no contention
127 // owners >= 0 means there might be readers (but no writer)
128 if (owners >= 0 && numWriteWaiters == 0){
133 // If the request is to probe.
134 if (millisecondsTimeout == 0){
139 // We need to wait. Mark that we have waiters and wait.
140 if (readEvent == null) {
141 LazyCreateEvent (ref readEvent, false);
142 // since we left the lock, start over.
146 if (!WaitOnEvent (readEvent, ref numReadWaiters, millisecondsTimeout))
154 public bool TryEnterReadLock (TimeSpan timeout)
156 int ms = CheckTimeout (timeout);
157 return TryEnterReadLock (ms);
161 // TODO: What to do if we are releasing a ReadLock and we do not own it?
163 public void ExitReadLock ()
166 Debug.Assert(owners > 0, "ReleasingReaderLock: releasing lock and no read lock taken");
168 ExitAndWakeUpAppropriateWaiters ();
171 public void EnterWriteLock ()
173 TryEnterWriteLock (-1);
176 public bool TryEnterWriteLock (int millisecondsTimeout)
181 // There is no contention, we are done
183 // Indicate that we have a writer
188 // If we are the thread that took the Upgradable read lock
189 if (owners == 1 && upgradable_thread == Thread.CurrentThread){
194 // If the request is to probe.
195 if (millisecondsTimeout == 0){
200 // We need to wait, figure out what kind of waiting.
202 if (upgradable_thread == Thread.CurrentThread){
203 // We are the upgradable thread, register our interest.
205 if (upgradeEvent == null){
206 LazyCreateEvent (ref upgradeEvent, false);
208 // since we left the lock, start over.
212 if (numUpgradeWaiters > 0){
214 throw new ApplicationException ("Upgrading lock to writer lock already in process, deadlock");
217 if (!WaitOnEvent (upgradeEvent, ref numUpgradeWaiters, millisecondsTimeout))
220 if (writeEvent == null){
221 LazyCreateEvent (ref writeEvent, true);
223 // since we left the lock, retry
226 if (!WaitOnEvent (writeEvent, ref numWriteWaiters, millisecondsTimeout))
231 Debug.Assert (owners == -1, "Owners is not -1");
236 public bool TryEnterWriteLock (TimeSpan timeout)
238 return TryEnterWriteLock (CheckTimeout (timeout));
241 public void ExitWriteLock ()
244 Debug.Assert (owners == -1, "Calling ReleaseWriterLock when no write lock is held");
245 Debug.Assert (numUpgradeWaiters > 0);
246 upgradable_thread = null;
248 ExitAndWakeUpAppropriateWaiters ();
251 public void EnterUpgradeableReadLock ()
253 TryEnterUpgradeableReadLock (-1);
257 // Taking the Upgradable read lock is like taking a read lock
258 // but we limit it to a single upgradable at a time.
260 public bool TryEnterUpgradeableReadLock (int millisecondsTimeout)
264 if (owners == 0 && numWriteWaiters == 0 && upgradable_thread == null){
266 upgradable_thread = Thread.CurrentThread;
270 // If the request is to probe
271 if (millisecondsTimeout == 0){
276 if (readEvent == null){
277 LazyCreateEvent (ref readEvent, false);
278 // since we left the lock, start over.
282 if (!WaitOnEvent (readEvent, ref numReadWaiters, millisecondsTimeout))
290 public bool TryEnterUpgradeableReadLock (TimeSpan timeout)
292 return TryEnterUpgradeableReadLock (CheckTimeout (timeout));
295 public void ExitUpgradeableReadLock ()
298 Debug.Assert (owners > 0, "Releasing an upgradable lock, but there was no reader!");
300 upgradable_thread = null;
301 ExitAndWakeUpAppropriateWaiters ();
304 public void Dispose ()
309 public bool IsReadLockHeld {
310 get { throw new NotImplementedException (); }
313 public bool IsWriteLockHeld {
314 get { throw new NotImplementedException (); }
317 public bool IsUpgradeableReadLockHeld {
318 get { throw new NotImplementedException (); }
321 public int CurrentReadCount {
322 get { throw new NotImplementedException (); }
325 public int RecursiveReadCount {
326 get { throw new NotImplementedException (); }
329 public int RecursiveUpgradeCount {
330 get { throw new NotImplementedException (); }
333 public int RecursiveWriteCount {
334 get { throw new NotImplementedException (); }
337 public int WaitingReadCount {
338 get { throw new NotImplementedException (); }
341 public int WaitingUpgradeCount {
342 get { throw new NotImplementedException (); }
345 public int WaitingWriteCount {
346 get { throw new NotImplementedException (); }
349 public LockRecursionPolicy RecursionPolicy {
350 get { return recursionPolicy; }
353 #region Private methods
356 if (Interlocked.CompareExchange(ref myLock, 1, 0) != 0)
360 void EnterMyLockSpin ()
363 for (int i = 0; ;i++) {
365 Thread.SpinWait (20); // Wait a few dozen instructions to let another processor release lock.
367 Thread.Sleep (0); // Give up my quantum.
369 if (Interlocked.CompareExchange(ref myLock, 1, 0) == 0)
376 Debug.Assert (myLock != 0, "Exiting spin lock that is not held");
380 bool MyLockHeld { get { return myLock != 0; } }
383 /// Determines the appropriate events to set, leaves the locks, and sets the events.
385 private void ExitAndWakeUpAppropriateWaiters()
387 Debug.Assert (MyLockHeld);
389 // First a writing thread waiting on being upgraded
390 if (owners == 1 && numUpgradeWaiters != 0){
391 // Exit before signaling to improve efficiency (wakee will need the lock)
393 // release all upgraders (however there can be at most one).
396 // TODO: What does the following comment mean?
397 // two threads upgrading is a guarenteed deadlock, so we throw in that case.
398 } else if (owners == 0 && numWriteWaiters > 0) {
399 // Exit before signaling to improve efficiency (wakee will need the lock)
401 // release one writer.
404 else if (owners >= 0 && numReadWaiters != 0) {
405 // Exit before signaling to improve efficiency (wakee will need the lock)
407 // release all readers.
414 /// A routine for lazily creating a event outside the lock (so if errors
415 /// happen they are outside the lock and that we don't do much work
416 /// while holding a spin lock). If all goes well, reenter the lock and
419 void LazyCreateEvent(ref EventWaitHandle waitEvent, bool makeAutoResetEvent)
421 Debug.Assert (MyLockHeld);
422 Debug.Assert (waitEvent == null);
425 EventWaitHandle newEvent;
426 if (makeAutoResetEvent)
427 newEvent = new AutoResetEvent (false);
429 newEvent = new ManualResetEvent (false);
433 // maybe someone snuck in.
434 if (waitEvent == null)
435 waitEvent = newEvent;
439 /// Waits on 'waitEvent' with a timeout of 'millisceondsTimeout.
440 /// Before the wait 'numWaiters' is incremented and is restored before leaving this routine.
442 bool WaitOnEvent (EventWaitHandle waitEvent, ref uint numWaiters, int millisecondsTimeout)
444 Debug.Assert (MyLockHeld);
449 bool waitSuccessful = false;
451 // Do the wait outside of any lock
454 waitSuccessful = waitEvent.WaitOne (millisecondsTimeout, false);
461 return waitSuccessful;
464 static int CheckTimeout (TimeSpan timeout)
467 return checked((int) timeout.TotalMilliseconds);
468 } catch (System.OverflowException) {
469 throw new ArgumentOutOfRangeException ("timeout");