Exemple #1
0
            /// <inheritdoc />
            public void Dispose()
            {
                lock (Keys)
                {
                    Doorman doorman = Keys[this.key];
                    --doorman.RefCount;
                    if (doorman.RefCount == 0)
                    {
                        Keys.Remove(this.key);
                        doorman.Reset();
                        DoormanPool.Return(doorman);
                    }

                    doorman.Semaphore.Release();
                }
            }
Exemple #2
0
        /// <summary>
        /// Returns a <see cref="SemaphoreSlim"/> matching on the given key
        ///  or a new one if none is found.
        /// </summary>
        /// <param name="key">The key identifying the semaphore.</param>
        /// <returns>
        /// The <see cref="SemaphoreSlim"/>.
        /// </returns>
        private static SemaphoreSlim GetOrCreate(string key)
        {
            Doorman item;

            lock (Keys)
            {
                if (Keys.TryGetValue(key, out item))
                {
                    ++item.RefCount;
                }
                else
                {
                    item      = DoormanPool.Rent();
                    Keys[key] = item;
                }
            }

            return(item.Semaphore);
        }