RegisteredWaitHandle.cs 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117
  1. //
  2. // System.Threading.RegisteredWaitHandle.cs
  3. //
  4. // Author:
  5. // Dick Porter ([email protected])
  6. // Lluis Sanchez Gual ([email protected])
  7. //
  8. // (C) Ximian, Inc. http://www.ximian.com
  9. //
  10. //
  11. // Copyright (C) 2004 Novell, Inc (http://www.novell.com)
  12. //
  13. // Permission is hereby granted, free of charge, to any person obtaining
  14. // a copy of this software and associated documentation files (the
  15. // "Software"), to deal in the Software without restriction, including
  16. // without limitation the rights to use, copy, modify, merge, publish,
  17. // distribute, sublicense, and/or sell copies of the Software, and to
  18. // permit persons to whom the Software is furnished to do so, subject to
  19. // the following conditions:
  20. //
  21. // The above copyright notice and this permission notice shall be
  22. // included in all copies or substantial portions of the Software.
  23. //
  24. // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
  25. // EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
  26. // MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
  27. // NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
  28. // LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
  29. // OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
  30. // WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
  31. //
  32. namespace System.Threading
  33. {
  34. public sealed class RegisteredWaitHandle : MarshalByRefObject
  35. {
  36. WaitHandle _waitObject;
  37. WaitOrTimerCallback _callback;
  38. TimeSpan _timeout;
  39. object _state;
  40. bool _executeOnlyOnce;
  41. WaitHandle _finalEvent;
  42. ManualResetEvent _cancelEvent;
  43. int _callsInProcess;
  44. bool _unregistered;
  45. internal RegisteredWaitHandle (WaitHandle waitObject, WaitOrTimerCallback callback, object state, TimeSpan timeout, bool executeOnlyOnce)
  46. {
  47. _waitObject = waitObject;
  48. _callback = callback;
  49. _state = state;
  50. _timeout = timeout;
  51. _executeOnlyOnce = executeOnlyOnce;
  52. _finalEvent = null;
  53. _cancelEvent = new ManualResetEvent (false);
  54. _callsInProcess = 0;
  55. _unregistered = false;
  56. }
  57. internal void Wait (object state)
  58. {
  59. try
  60. {
  61. WaitHandle[] waits = new WaitHandle[] {_waitObject, _cancelEvent};
  62. do
  63. {
  64. int signal = WaitHandle.WaitAny (waits, _timeout, false);
  65. if (!_unregistered)
  66. {
  67. lock (this) { _callsInProcess++; }
  68. ThreadPool.QueueUserWorkItem (new WaitCallback (DoCallBack), (signal == WaitHandle.WaitTimeout));
  69. }
  70. }
  71. while (!_unregistered && !_executeOnlyOnce);
  72. }
  73. catch {}
  74. lock (this) {
  75. _unregistered = true;
  76. if (_callsInProcess == 0 && _finalEvent != null)
  77. NativeEventCalls.SetEvent_internal (_finalEvent.Handle);
  78. }
  79. }
  80. private void DoCallBack (object timedOut)
  81. {
  82. if (_callback != null)
  83. _callback (_state, (bool)timedOut);
  84. lock (this)
  85. {
  86. _callsInProcess--;
  87. if (_unregistered && _callsInProcess == 0 && _finalEvent != null)
  88. NativeEventCalls.SetEvent_internal (_finalEvent.Handle);
  89. }
  90. }
  91. public bool Unregister(WaitHandle waitObject)
  92. {
  93. lock (this)
  94. {
  95. if (_unregistered) return false;
  96. _finalEvent = waitObject;
  97. _unregistered = true;
  98. _cancelEvent.Set();
  99. return true;
  100. }
  101. }
  102. [MonoTODO]
  103. ~RegisteredWaitHandle() {
  104. // FIXME
  105. }
  106. }
  107. }