public LockToken(ILockToken src) : base(src) { this.Value = src.Value; this.Resource = src.Resource; this.Timeout = src.Timeout; }
public IList<IResource> DeleteAll(ILockToken proto) { lock (_token) { var temp = base.DeleteAll(proto); foreach (var item in temp) { var input = item as ILockToken; if (input != null) { if (!Resources.Contains(input.Resource)) { Resources.Remove(input.Resource); } } } return temp; } }
/// <summary> /// Releases the specified token and potentially disposes it if no other thread is waiting on it. /// </summary> /// <param name="token">The token to be released.</param> internal void Release(ILockToken token) { LockToken lockToken = (LockToken)token; this.rw.EnterWriteLock(); if (lockToken.NumWaiting == 0) { this.tokens.Remove(lockToken.Id); lockToken.Sem.Release(); lockToken.Sem.Close(); lockToken.Sem = null; } else { lockToken.Sem.Release(); } this.rw.ExitWriteLock(); }
/// <summary> /// locates or creates a semaphore for the given Id, and tries to acquire it up to the given timeout /// </summary> /// <param name="id">The identifier for the semaphore to acquire.</param> /// <param name="timeoutInMillis">The timeout in milliseconds.</param> /// <param name="releaseToken">The release token if acquired.</param> /// <returns><c>true</c> if true acquisition happened, <c>false</c> otherwise.</returns> internal bool FindAndTryAcquire(Guid id, int timeoutInMillis, out ILockToken releaseToken) { this.rw.EnterWriteLock(); LockToken ev; if (!this.tokens.TryGetValue(id, out ev)) { ev = new LockToken { Id = id, Sem = new Semaphore(1, 1), NumWaiting = 0 }; this.tokens[id] = ev; } Interlocked.Increment(ref ev.NumWaiting); this.rw.ExitWriteLock(); bool acq = ev.Sem.WaitOne(timeoutInMillis); Interlocked.Decrement(ref ev.NumWaiting); releaseToken = acq ? ev : null; return(acq); }
public CollectionLockToken(ILockToken src, CollectionLockTokenDepth depth) : base(src) { this.Depth = depth; }
public void Save(ILockToken input) { lock (_token) { base.Save(input); if (!Resources.Contains(input.Resource)) { Resources.Add(input.Resource); } } }