Exemple #1
0
        /// <summary>
        /// Releases a reference to a doorman. If the ref-count hits zero, then the doorman is
        /// returned to the pool (or is simply left for the garbage collector to cleanup if the
        /// pool is already full).
        /// </summary>
        /// <param name="doorman">The <see cref="Doorman"/>.</param>
        private static void ReleaseDoorman(AsyncKeyLockDoorman doorman)
        {
            bool lockTaken = false;

            try
            {
                _spinLock.Enter(ref lockTaken);

                if (--doorman.RefCount == 0)
                {
                    Keys.Remove(doorman.Key);
                    if (Pool.Count < MaxPoolSize)
                    {
                        doorman.Key = null;
                        Pool.Push(doorman);
                    }
                }
            }
            finally
            {
                if (lockTaken)
                {
                    _spinLock.Exit();
                }
            }
        }
 internal Releaser(AsyncKeyLockDoorman toRelease, bool writer)
 {
     this._toRelease = toRelease;
     this._writer    = writer;
 }
Exemple #3
0
        /// <summary>
        /// Locks the current thread in write mode asynchronously.
        /// </summary>
        /// <param name="key">The key identifying the specific object to lock against.</param>
        /// <returns>
        /// The <see cref="Task{IDisposable}"/> that will release the lock.
        /// </returns>
        public async Task <IDisposable> WriterLockAsync(string key)
        {
            AsyncKeyLockDoorman doorman = GetDoorman(key);

            return(await doorman.WriterLockAsync().ConfigureAwait(false));
        }