public static ResultType GetWriteLock <ResultType>(ReaderWriterLock lockObj, int timeout, DoWorkFunc <ResultType> doWork) { RWLockStatus status = (lockObj.IsWriterLockHeld ? RWLockStatus.WRITE_LOCK : (lockObj.IsReaderLockHeld ? RWLockStatus.READ_LOCK : RWLockStatus.UNLOCKED)); LockCookie writeLock = default(LockCookie); if (status == RWLockStatus.READ_LOCK) { writeLock = lockObj.UpgradeToWriterLock(timeout); } else if (status == RWLockStatus.UNLOCKED) { lockObj.AcquireWriterLock(timeout); } try { return(doWork()); } finally { if (status == RWLockStatus.READ_LOCK) { lockObj.DowngradeFromWriterLock(ref writeLock); } else if (status == RWLockStatus.UNLOCKED) { lockObj.ReleaseWriterLock(); } } }
public RWLock(ReaderWriterLock lockObj, RWLockStatus status, int timeoutMS) { this.lockObj = lockObj; this.timeout = timeoutMS; this.Status = status; }