public void ShouldEnterAndLeaveInSingleThreadWithTry() { var @lock = new SlimLock(); Assert.IsTrue(@lock.TryEnter()); @lock.Leave(); }
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); }
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); }
/// <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; }
public void ShouldEnterAndLeaveInSingleThread() { var @lock = new SlimLock(); @lock.Enter(); @lock.Leave(); }
public void ShouldFailForDoubleLeave() { var @lock = new SlimLock(); @lock.Enter(); @lock.Leave(); Assert.Catch <Exception>(() => @lock.Leave(), "Enter/Leave should have balance"); }
/// <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; }
public void Remove(TKey key) { try { SlimLock.EnterWriteLock(); TValue value; Dictionary.TryRemove(key, out value); } finally { SlimLock.ExitWriteLock(); } }
/// <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); }
/// <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; } } }
/// <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); }
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(); } }
/// <summary> /// Creates Webhook notification manager. /// </summary> public WebhookNotificationManager() { this.notificationInfoDatabase = new Dictionary <string, NotificationInfo>(); this.lockForDatabase = new SlimLock(); }
/// <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(); }