コード例 #1
0
        public void ShouldEnterAndLeaveInSingleThreadWithTry()
        {
            var @lock = new SlimLock();

            Assert.IsTrue(@lock.TryEnter());
            @lock.Leave();
        }
コード例 #2
0
        public virtual MruNode <V> Add(V item)
        {
            var node = new MruNode <V>(item, this);

            try
            {
                SlimLock.EnterWriteLock();
                node.Next = Head;
                Tail      = Tail ?? node;
                if (Head != null)
                {
                    Head.Previous = node;
                }
                Head = node;
                ItemCount++;

                if (ItemCount > Limit)
                {
                    var deleted = Tail;
                    if (Tail != null)
                    {
                        Tail = Tail.Previous;
                        deleted.Erase();
                    }
                    ItemCount--;
                }
            }
            finally
            {
                SlimLock.ExitWriteLock();
            }
            return(node);
        }
コード例 #3
0
        public TValue ReadOrWrite(TKey key, Func <TValue> valueProvider)
        {
            TValue value = default(TValue);

            if (!Dictionary.TryGetValue(key, out value))
            {
                try
                {
                    if (SlimLock.TryEnterWriteLock(1000))
                    {
                        UpdateWaiting();
                        if (!Dictionary.TryGetValue(key, out value))
                        {
                            value = Dictionary.GetOrAdd(key, valueProvider());
                        }
                    }
                    else
                    {
                        throw new Exception("THIS IS CRAP");
                    }
                }
                finally
                {
                    SlimLock.ExitWriteLock();
                }
            }
            return(value);
        }
コード例 #4
0
ファイル: SystemTimerFactory.cs プロジェクト: valmac/nesper
 /// <summary>
 /// Initializes a new instance of the <see cref="InternalTimer"/> class.
 /// </summary>
 internal InternalTimer(long period, TimerCallback callback)
 {
     SlimLock      = new SlimLock();
     TimerCallback = callback;
     Interval      = period * 1000;
     NextTime      = PerformanceObserver.MicroTime;
 }
コード例 #5
0
        public void ShouldEnterAndLeaveInSingleThread()
        {
            var @lock = new SlimLock();

            @lock.Enter();
            @lock.Leave();
        }
コード例 #6
0
        public void ShouldFailForDoubleLeave()
        {
            var @lock = new SlimLock();

            @lock.Enter();
            @lock.Leave();
            Assert.Catch <Exception>(() => @lock.Leave(), "Enter/Leave should have balance");
        }
コード例 #7
0
 /// <summary>
 /// Initializes a new instance of the <see cref="InternalTimer"/> class.
 /// </summary>
 internal InternalTimer(
     long offsetInMillis,
     long intervalInMillis,
     TimerCallback callback)
 {
     SlimLock      = new SlimLock();
     TimerCallback = callback;
     Interval      = intervalInMillis * 1000;
     NextTime      = PerformanceObserver.MicroTime + offsetInMillis;
 }
コード例 #8
0
 public void Remove(TKey key)
 {
     try
     {
         SlimLock.EnterWriteLock();
         TValue value;
         Dictionary.TryRemove(key, out value);
     }
     finally
     {
         SlimLock.ExitWriteLock();
     }
 }
コード例 #9
0
        /// <summary>
        /// Creates webhook listner.
        /// </summary>
        /// <param name="numberOfThreads">Number of listener threads.</param>
        public WebhookListener(int numberOfThreads = DEFAULT_NUMBER_OF_THREADS)
        {
            this.slimLock            = new SlimLock();
            this.notificationManager = new WebhookNotificationManager();
            this.httpListener        = new HttpListener();

            this.httpListener.IgnoreWriteExceptions = true;

            this.numberOfThreads   = numberOfThreads;
            this.activeThreadCount = 0;

            this.pathPrefix = String.Format(PATH_PREFIX_PATTERN, Guid.NewGuid().ToString("N"));
            this.Path       = String.Format(PATH_PATTERN, this.pathPrefix);
        }
コード例 #10
0
        /// <summary>
        /// Pushes an item onto the queue.  If the queue has reached
        /// capacity, the call will pend until the queue has space to
        /// receive the request.
        /// </summary>
        /// <param name="item"></param>
        public void Push(T item)
        {
            if (_maxLength != int.MaxValue)
            {
                if (!BoundBlockingQueueOverride.IsEngaged)
                {
                    for (int ii = 0; Interlocked.Read(ref _count) > _maxLength;)
                    {
                        SlimLock.SmartWait(++ii);
                    }
                }
            }

            // Create the new node
            var node = new Node(item);
            // Get the write node for the thread
            Node branch = _wnode;

            for (; ;)
            {
                Node temp;
                while ((temp = branch.Next) != null)
                {
                    branch = temp; // temp is guaranteed to not be null
                }

                var pnode = Interlocked.CompareExchange(
                    ref branch.Next,
                    node,
                    null);
                if (pnode == null)
                {
                    _wnode = node;
                    // Check for threads that have been waiting a long time ... these
                    // threads will be using a slowLockInterest rather than a tight spin
                    // loop.
                    if (Interlocked.Read(ref _slowLockInterest) > 0)
                    {
                        lock (_slowLock)
                        {
                            Monitor.Pulse(_slowLock);
                        }
                    }
                    // Increment the counter
                    Interlocked.Increment(ref _count);
                    return;
                }
            }
        }
コード例 #11
0
ファイル: FutureImpl.cs プロジェクト: lanicon/nesper
        /// <summary>
        /// Waits for completion of the future.
        /// </summary>
        /// <param name="timeOut"></param>
        /// <returns></returns>
        public bool Wait(TimeSpan timeOut)
        {
            var timeCur = PerformanceObserver.MilliTime;
            var timeEnd = timeCur + timeOut.TotalMilliseconds;

            for (var ii = 0; !HasValue; ii++)
            {
                timeCur = PerformanceObserver.MilliTime;
                if (timeCur > timeEnd)
                {
                    return(false);
                }

                SlimLock.SmartWait(ii);
            }

            return(true);
        }
コード例 #12
0
 public virtual void Clear()
 {
     try
     {
         SlimLock.EnterWriteLock();
         var node = Head;
         while (node != null)
         {
             var old = node;
             node = node.Next;
             old.Erase();
         }
         ItemCount = 0;
         Head      = null;
         Tail      = null;
     }
     finally
     {
         SlimLock.ExitWriteLock();
     }
 }
コード例 #13
0
        /// <summary>
        /// Creates Webhook notification manager.
        /// </summary>
        public WebhookNotificationManager()
        {
            this.notificationInfoDatabase = new Dictionary <string, NotificationInfo>();

            this.lockForDatabase = new SlimLock();
        }
コード例 #14
0
ファイル: SlimThreadLocal.cs プロジェクト: lanicon/nesper
 /// <summary>
 /// Initializes a new instance of the <see cref="SlimThreadLocal{T}"/> class.
 /// </summary>
 /// <param name="factory">The factory.</param>
 public SlimThreadLocal(Func <T> factory)
 {
     _threadTable  = new Dictionary <Thread, T>(new ThreadEq());
     _valueFactory = factory;
     _wLock        = new SlimLock();
 }