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)
10 // Copyright 2004-2008 Novell, Inc (http://www.novell.com)
11 // Copyright 2003, Ximian, Inc.
13 // NoRecursion code based on the blog post from Vance Morrison:
14 // http://blogs.msdn.com/vancem/archive/2006/03/28/563180.aspx
16 // Recursion code based on Mono's implementation of ReaderWriterLock.
18 // Permission is hereby granted, free of charge, to any person obtaining
19 // a copy of this software and associated documentation files (the
20 // "Software"), to deal in the Software without restriction, including
21 // without limitation the rights to use, copy, modify, merge, publish,
22 // distribute, sublicense, and/or sell copies of the Software, and to
23 // permit persons to whom the Software is furnished to do so, subject to
24 // the following conditions:
26 // The above copyright notice and this permission notice shall be
27 // included in all copies or substantial portions of the Software.
29 // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
30 // EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
31 // MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
32 // NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
33 // LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
34 // OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
35 // WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
39 using System.Collections;
40 using System.Collections.Generic;
41 using System.Security.Permissions;
42 using System.Diagnostics;
43 using System.Threading;
45 namespace System.Threading {
48 // This implementation is based on the light-weight
49 // Reader/Writer lock sample from Vance Morrison's blog:
51 // http://blogs.msdn.com/vancem/archive/2006/03/28/563180.aspx
53 // And in Mono's ReaderWriterLock
55 [HostProtectionAttribute(SecurityAction.LinkDemand, MayLeakOnAbort = true)]
56 [HostProtectionAttribute(SecurityAction.LinkDemand, Synchronization = true, ExternalThreading = true)]
57 public class ReaderWriterLockSlim : IDisposable {
58 // Are we on a multiprocessor?
61 // Lock specifiation for myLock: This lock protects exactly the local fields associted
62 // instance of MyReaderWriterLock. It does NOT protect the memory associted with the
63 // the events that hang off this lock (eg writeEvent, readEvent upgradeEvent).
66 // Who owns the lock owners > 0 => readers
67 // owners = -1 means there is one writer, Owners must be >= -1.
69 Thread upgradable_thread;
71 // These variables allow use to avoid Setting events (which is expensive) if we don't have to.
72 uint numWriteWaiters; // maximum number of threads that can be doing a WaitOne on the writeEvent
73 uint numReadWaiters; // maximum number of threads that can be doing a WaitOne on the readEvent
74 uint numUpgradeWaiters; // maximum number of threads that can be doing a WaitOne on the upgradeEvent (at most 1).
76 // conditions we wait on.
77 EventWaitHandle writeEvent; // threads waiting to aquire a write lock go here.
78 EventWaitHandle readEvent; // threads waiting to aquire a read lock go here (will be released in bulk)
79 EventWaitHandle upgradeEvent; // thread waiting to upgrade a read lock to a write lock go here (at most one)
83 // Only set if we are a recursive lock
84 Dictionary<int,int> reader_locks;
86 static ReaderWriterLockSlim ()
88 smp = Environment.ProcessorCount > 1;
91 public ReaderWriterLockSlim () : this (LockRecursionPolicy.NoRecursion)
95 public ReaderWriterLockSlim (LockRecursionPolicy recursionPolicy)
97 if (recursionPolicy != LockRecursionPolicy.NoRecursion){
98 reader_locks = new Dictionary<int,int> ();
99 throw new NotImplementedException ("recursionPolicy != NoRecursion not currently implemented");
103 public void EnterReadLock ()
105 TryEnterReadLock (-1);
108 public bool TryEnterReadLock (int millisecondsTimeout)
113 // Easy case, no contention
114 // owners >= 0 means there might be readers (but no writer)
115 if (owners >= 0 && numWriteWaiters == 0){
120 // If the request is to probe.
121 if (millisecondsTimeout == 0){
126 // We need to wait. Mark that we have waiters and wait.
127 if (readEvent == null) {
128 LazyCreateEvent (ref readEvent, false);
129 // since we left the lock, start over.
133 if (!WaitOnEvent (readEvent, ref numReadWaiters, millisecondsTimeout))
141 public bool TryEnterReadLock (TimeSpan timeout)
143 int ms = CheckTimeout (timeout);
144 return TryEnterReadLock (ms);
148 // TODO: What to do if we are releasing a ReadLock and we do not own it?
150 public void ExitReadLock ()
153 Debug.Assert(owners > 0, "ReleasingReaderLock: releasing lock and no read lock taken");
155 ExitAndWakeUpAppropriateWaiters ();
158 public void EnterWriteLock ()
160 TryEnterWriteLock (-1);
163 public bool TryEnterWriteLock (int millisecondsTimeout)
168 // There is no contention, we are done
170 // Indicate that we have a writer
175 // If we are the thread that took the Upgradable read lock
176 if (owners == 1 && upgradable_thread == Thread.CurrentThread){
181 // If the request is to probe.
182 if (millisecondsTimeout == 0){
187 // We need to wait, figure out what kind of waiting.
189 if (upgradable_thread == Thread.CurrentThread){
190 // We are the upgradable thread, register our interest.
192 if (upgradeEvent == null){
193 LazyCreateEvent (ref upgradeEvent, false);
195 // since we left the lock, start over.
199 if (numUpgradeWaiters > 0){
201 throw new ApplicationException ("Upgrading lock to writer lock already in process, deadlock");
204 if (!WaitOnEvent (upgradeEvent, ref numUpgradeWaiters, millisecondsTimeout))
207 if (writeEvent == null){
208 LazyCreateEvent (ref writeEvent, true);
210 // since we left the lock, retry
213 if (!WaitOnEvent (writeEvent, ref numWriteWaiters, millisecondsTimeout))
218 Debug.Assert (owners == -1, "Owners is not -1");
223 public bool TryEnterWriteLock (TimeSpan timeout)
225 int ms = CheckTimeout (timeout);
226 return TryEnterWriteLock (ms);
229 public void ExitWriteLock ()
232 Debug.Assert (owners == -1, "Calling ReleaseWriterLock when no write lock is held");
233 Debug.Assert (numUpgradeWaiters > 0);
234 upgradable_thread = null;
236 ExitAndWakeUpAppropriateWaiters ();
239 public void EnterUpgradableReadLock ()
241 TryEnterUpgradableReadLock (-1);
245 // Taking the Upgradable read lock is like taking a read lock
246 // but we limit it to a single upgradable at a time.
248 public bool TryEnterUpgradableReadLock (int millisecondsTimeout)
252 if (owners == 0 && numWriteWaiters == 0 && upgradable_thread == null){
254 upgradable_thread = Thread.CurrentThread;
258 // If the request is to probe
259 if (millisecondsTimeout == 0){
264 if (readEvent == null){
265 LazyCreateEvent (ref readEvent, false);
266 // since we left the lock, start over.
270 if (!WaitOnEvent (readEvent, ref numReadWaiters, millisecondsTimeout))
278 public bool TryEnterUpgradableReadLock (TimeSpan timeout)
280 int ms = CheckTimeout (timeout);
281 return TryEnterUpgradableReadLock (ms);
284 public void ExitUpgradeableReadLock ()
287 Debug.Assert (owners > 0, "Releasing an upgradable lock, but there was no reader!");
289 upgradable_thread = null;
290 ExitAndWakeUpAppropriateWaiters ();
293 public void Dispose ()
295 throw new NotImplementedException ();
298 #region Private methods
301 if (Interlocked.CompareExchange(ref myLock, 1, 0) != 0)
305 void EnterMyLockSpin ()
308 for (int i = 0; ;i++) {
310 Thread.SpinWait (20); // Wait a few dozen instructions to let another processor release lock.
312 Thread.Sleep (0); // Give up my quantum.
314 if (Interlocked.CompareExchange(ref myLock, 1, 0) == 0)
321 Debug.Assert (myLock != 0, "Exiting spin lock that is not held");
325 bool MyLockHeld { get { return myLock != 0; } }
328 /// Determines the appropriate events to set, leaves the locks, and sets the events.
330 private void ExitAndWakeUpAppropriateWaiters()
332 Debug.Assert (MyLockHeld);
334 // First a writing thread waiting on being upgraded
335 if (owners == 1 && numUpgradeWaiters != 0){
336 // Exit before signaling to improve efficiency (wakee will need the lock)
338 // release all upgraders (however there can be at most one).
341 // TODO: What does the following comment mean?
342 // two threads upgrading is a guarenteed deadlock, so we throw in that case.
343 } else if (owners == 0 && numWriteWaiters > 0) {
344 // Exit before signaling to improve efficiency (wakee will need the lock)
346 // release one writer.
349 else if (owners >= 0 && numReadWaiters != 0) {
350 // Exit before signaling to improve efficiency (wakee will need the lock)
352 // release all readers.
359 /// A routine for lazily creating a event outside the lock (so if errors
360 /// happen they are outside the lock and that we don't do much work
361 /// while holding a spin lock). If all goes well, reenter the lock and
364 void LazyCreateEvent(ref EventWaitHandle waitEvent, bool makeAutoResetEvent)
366 Debug.Assert (MyLockHeld);
367 Debug.Assert (waitEvent == null);
370 EventWaitHandle newEvent;
371 if (makeAutoResetEvent)
372 newEvent = new AutoResetEvent (false);
374 newEvent = new ManualResetEvent (false);
378 // maybe someone snuck in.
379 if (waitEvent == null)
380 waitEvent = newEvent;
384 /// Waits on 'waitEvent' with a timeout of 'millisceondsTimeout.
385 /// Before the wait 'numWaiters' is incremented and is restored before leaving this routine.
387 bool WaitOnEvent (EventWaitHandle waitEvent, ref uint numWaiters, int millisecondsTimeout)
389 Debug.Assert (MyLockHeld);
394 bool waitSuccessful = false;
396 // Do the wait outside of any lock
399 waitSuccessful = waitEvent.WaitOne (millisecondsTimeout, false);
406 return waitSuccessful;
409 int CheckTimeout (TimeSpan timeout)
411 int ms = (int) timeout.TotalMilliseconds;
414 throw new ArgumentOutOfRangeException ("timeout",
415 "Number must be either non-negative or -1");