LockQueue.cs 936 B

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061
  1. //
  2. // System.Threading.LockQueue
  3. //
  4. // Author(s):
  5. // Jackson Harper ([email protected])
  6. //
  7. // (C) 2004 Novell, Inc (http://www.novell.com)
  8. //
  9. using System;
  10. namespace System.Threading {
  11. // Used for queueing on a reader writer lock
  12. internal class LockQueue {
  13. private ReaderWriterLock rwlock;
  14. private int lockCount = 0;
  15. public LockQueue (ReaderWriterLock rwlock)
  16. {
  17. this.rwlock = rwlock;
  18. }
  19. public bool Wait (int timeout)
  20. {
  21. bool _lock = false;
  22. try {
  23. lock (this) {
  24. lockCount++;
  25. Monitor.Exit (rwlock);
  26. _lock = true;
  27. return Monitor.Wait (this, timeout);
  28. }
  29. } finally {
  30. if (_lock) {
  31. Monitor.Enter (rwlock);
  32. lockCount--;
  33. }
  34. }
  35. }
  36. public bool IsEmpty
  37. {
  38. get { lock (this) return (lockCount == 0); }
  39. }
  40. public void Pulse ()
  41. {
  42. lock (this) Monitor.Pulse (this);
  43. }
  44. public void PulseAll ()
  45. {
  46. lock (this) Monitor.PulseAll (this);
  47. }
  48. }
  49. }