예제 #1
0
파일: LockToken.cs 프로젝트: keithb-/Valley
 public LockToken(ILockToken src)
     : base(src)
 {
     this.Value = src.Value;
     this.Resource = src.Resource;
     this.Timeout = src.Timeout;
 }
예제 #2
0
 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;
     }
 }
예제 #3
0
        /// <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();
        }
예제 #4
0
        /// <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);
        }
예제 #5
0
 public CollectionLockToken(ILockToken src, CollectionLockTokenDepth depth)
     : base(src)
 {
     this.Depth = depth;
 }
예제 #6
0
 public void Save(ILockToken input)
 {
     lock (_token)
     {
         base.Save(input);
         if (!Resources.Contains(input.Resource))
         {
             Resources.Add(input.Resource);
         }
     }
 }