123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109 |
- #region --------------- Copyright Dresser Wayne Pignone -------------
- #endregion
- using System;
- using System.Collections.Generic;
- using System.Text;
- using System.Threading;
- namespace Wayne.Lib.StateEngine
- {
-
-
-
-
- class ReentrancyMutex : IDisposable
- {
- #region Fields
- bool owned;
- object mutexLock = new object();
- Mutex mutex = new Mutex(false);
- #endregion
- ~ReentrancyMutex()
- {
- Dispose(false);
- }
- public bool TryAquire()
- {
- lock (mutexLock)
- {
- if ((mutex != null) && mutex.WaitOne(0, false))
- {
- if (owned)
- {
- mutex.ReleaseMutex();
- return false;
- }
- else
- {
- owned = true;
- return true;
- }
- }
- else
- {
- return false;
- }
- }
- }
- public void Release()
- {
- owned = false;
- lock (mutexLock)
- {
- if (mutex != null)
- mutex.ReleaseMutex();
- }
- }
- internal void Close()
- {
- ((IDisposable)this).Dispose();
- }
- #region IDisposable Members
- protected virtual void Dispose(bool disposing)
- {
- if (disposing)
- {
- lock (mutexLock)
- {
- if (mutex != null)
- {
- mutex.Close();
- mutex = null;
- }
- }
- }
- }
- void IDisposable.Dispose()
- {
- Dispose(true);
- GC.SuppressFinalize(this);
- }
- #endregion
- }
- }
|