/// <summary>Close the DomainSocketWatcher and wait for its thread to terminate.</summary> /// <remarks> /// Close the DomainSocketWatcher and wait for its thread to terminate. /// If there is more than one close, all but the first will be ignored. /// </remarks> /// <exception cref="System.IO.IOException"/> public void Close() { Lock.Lock(); try { if (closed) { return; } if (Log.IsDebugEnabled()) { Log.Debug(this + ": closing"); } closed = true; } finally { Lock.Unlock(); } // Close notificationSockets[0], so that notificationSockets[1] gets an EOF // event. This will wake up the thread immediately if it is blocked inside // the select() system call. notificationSockets[0].Close(); // Wait for the select thread to terminate. Uninterruptibles.JoinUninterruptibly(watcherThread); }
public void Dispose() { lock (this) { //System.out.println("NRT: set finish"); Finish = true; // So thread wakes up and notices it should finish: ReopenLock.Lock(); try { ReopenCond.Set(); } finally { ReopenLock.Unlock(); } try { Join(); } catch (ThreadInterruptedException ie) { throw new ThreadInterruptedException(ie); } // Max it out so any waiting search threads will return: SearchingGen = long.MaxValue; Monitor.PulseAll(this); } }
/// <summary> /// This method handles the following conditions: /// <ul> /// <li>If retry is not to be processed, return null</li> /// <li>If there is no cache entry, add a new entry /// <paramref name="newEntry"/> /// and return /// it.</li> /// <li>If there is an existing entry, wait for its completion. If the /// completion state is /// <see cref="CacheEntry.Failed"/> /// , the expectation is that the /// thread that waited for completion, retries the request. the /// <see cref="CacheEntry"/> /// state is set to /// <see cref="CacheEntry.Inprogress"/> /// again. /// <li>If the completion state is /// <see cref="CacheEntry.Success"/> /// , the entry is /// returned so that the thread that waits for it can can return previous /// response.</li> /// <ul> /// </summary> /// <returns> /// /// <see cref="CacheEntry"/> /// . /// </returns> private RetryCache.CacheEntry WaitForCompletion(RetryCache.CacheEntry newEntry) { RetryCache.CacheEntry mapEntry = null; Lock.Lock(); try { mapEntry = set.Get(newEntry); // If an entry in the cache does not exist, add a new one if (mapEntry == null) { if (Log.IsTraceEnabled()) { Log.Trace("Adding Rpc request clientId " + newEntry.clientIdMsb + newEntry.clientIdLsb + " callId " + newEntry.callId + " to retryCache"); } set.Put(newEntry); retryCacheMetrics.IncrCacheUpdated(); return(newEntry); } else { retryCacheMetrics.IncrCacheHit(); } } finally { Lock.Unlock(); } // Entry already exists in cache. Wait for completion and return its state Preconditions.CheckNotNull(mapEntry, "Entry from the cache should not be null"); // Wait for in progress request to complete lock (mapEntry) { while (mapEntry.state == RetryCache.CacheEntry.Inprogress) { try { Runtime.Wait(mapEntry); } catch (Exception) { // Restore the interrupted status Thread.CurrentThread().Interrupt(); } } // Previous request has failed, the expectation is is that it will be // retried again. if (mapEntry.state != RetryCache.CacheEntry.Success) { mapEntry.state = RetryCache.CacheEntry.Inprogress; } } return(mapEntry); }
// Configuration Keys /* The queues */ /* Read locks */ private void SignalNotEmpty() { takeLock.Lock(); try { notEmpty.Signal(); } finally { takeLock.Unlock(); } }
internal override bool CanCache(int length, ObjectToPack src, ObjectToPack res) { Lock.Lock(); try { return(base.CanCache(length, src, res)); } finally { Lock.Unlock(); } }
/// <summary> /// Returns an array containing all of the elements in this queue; the /// runtime type of the returned array is that of the specified array. /// The returned array elements are in no particular order. /// If the queue fits in the specified array, it is returned therein. /// Otherwise, a new array is allocated with the runtime type of the /// specified array and the size of this queue. /// /// <p>If this queue fits in the specified array with room to spare /// (i.e., the array has more elements than this queue), the element in /// the array immediately following the end of the queue is set to /// <tt>null</tt>.</p> /// /// <p>Like the {@link #toArray()} method, this method acts as bridge between /// array-based and collection-based APIs. Further, this method allows /// precise control over the runtime type of the output array, and may, /// under certain circumstances, be used to save allocation costs.</p> /// /// <p>Suppose <tt>x</tt> is a queue known to contain only strings. /// The following code can be used to dump the queue into a newly /// allocated array of <tt>String</tt>:</p> /// /// <pre> /// String[] y = x.toArray(new String[0]);</pre> /// /// Note that <tt>toArray(new Object[0])</tt> is identical in function to /// <tt>toArray()</tt>. /// </summary> /// <param name="target">the array into which the elements of the queue are to /// be stored, if it is big enough; otherwise, a new array of the /// same runtime type is allocated for this purpose</param> /// <exception cref="ArgumentNullException">if <paramref name="target"/> is null</exception> public T[] ToArray(T[] target) { if (target == null) { throw new ArgumentNullException("target must not be null"); } ReentrantLock rl = _lock; rl.Lock(); try { int targetSize = target.Length; int sourceSize = Count; if (targetSize < sourceSize) { target = new T[sourceSize]; } int k = 0; foreach (T item in _innerQueue) { target[k++] = item; } for (; targetSize < sourceSize; targetSize++) { target[targetSize] = default(T); } return(target); } finally { rl.Unlock(); } }
/** * @throws UnsupportedOperationException {@inheritDoc} * @throws ClassCastException {@inheritDoc} * @throws NullPointerException {@inheritDoc} * @throws IllegalArgumentException {@inheritDoc} */ //TODO: do we really need this? can we leave it to base class? public override int DrainTo(ICollection <T> collection) { if (collection == null) { throw new ArgumentNullException("collection", "must not be null"); } if (collection == this) { throw new ArgumentException("cannot DrainTo this"); } ReentrantLock rl = _lock; rl.Lock(); try { int n = 0; T element; while (_innerQueue.Poll(out element)) { collection.Add(element); ++n; } return(n); } finally { rl.Unlock(); } }
/// <summary> /// Retrieves and removes the head of this queue, waiting up to the /// specified wait time if necessary for an element to become available. /// </summary> /// <param name="timeout">how long to wait before giving up</param> /// <param name="element"></param> /// <returns> /// the head of this queue, or <see lang="default(T)"/> if the /// specified waiting time elapses before an element is available. /// </returns> public override bool Poll(TimeSpan timeout, out T element) { ReentrantLock rl = _lock; rl.LockInterruptibly(); try { DateTime deadline = DateTime.Now + timeout; for (; ;) { if (_innerQueue.Poll(out element)) { return(true); } if (timeout.TotalMilliseconds <= 0) { return(false); } try { notEmpty.Await(timeout); timeout = deadline - DateTime.Now; } catch (ThreadInterruptedException) { notEmpty.Signal(); // propagate to non-interrupted thread throw; } } } finally { rl.Unlock(); } }
/// <summary> /// Retrieves and removes the head of this queue, waiting if necessary /// until an element becomes available. /// </summary> /// <returns> the head of this queue</returns> public override T Take() { ReentrantLock rl = _lock; rl.LockInterruptibly(); try { try { while (_innerQueue.Count == 0) { notEmpty.Await(); } } catch (ThreadInterruptedException) { notEmpty.Signal(); // propagate to non-interrupted thread throw; } T element; if (!_innerQueue.Poll(out element)) { throw new InvalidOperationException("Poll returns unexpected false"); } return(element); } finally { rl.Unlock(); } }
/** * Retrieves and removes the head of this queue, if another thread * is currently making an element available. * * @return the head of this queue, or <tt>null</tt> if no * element is available. */ public override bool Poll(out T element) { ReentrantLock qlock = _qlock; for (; ;) { Node node; qlock.Lock(); try { node = _waitingProducers.Dequeue(); } finally { qlock.Unlock(); } if (node == null) { element = default(T); return(false); } else { T x; if (node.GetItem(out x)) { element = x; return(true); } // else retry } } }
// Untimed nonblocking versions /// <summary> /// Inserts the specified element into this queue, if another thread is /// waiting to receive it. /// </summary> /// <param name="element">the element to add</param> /// <returns><tt>true</tt> if the element was added to this queue, else <tt>false</tt></returns> /// <exception cref="ArgumentNullException" /> public override bool Offer(T element) { if (element == null) { throw new ArgumentNullException("element"); } ReentrantLock qlock = _qlock; for (; ;) { Node node; qlock.Lock(); try { node = _waitingConsumers.Dequeue(); } finally { qlock.Unlock(); } if (node == null) { return(false); } else if (node.SetItem(element)) { return(true); } // else retry } }
/// <summary> /// Retrieves and removes the head of this queue, waiting up to the /// specified wait time if necessary for an element to become available. /// </summary> /// <param name="element"> /// Set to the head of this queue. <c>default(T)</c> if queue is empty. /// </param> /// <param name="duration">How long to wait before giving up.</param> /// <returns> /// <c>false</c> if the queue is still empty after waited for the time /// specified by the <paramref name="duration"/>. Otherwise <c>true</c>. /// </returns> public override bool Poll(TimeSpan duration, out T element) { ReentrantLock currentLock = _lock; currentLock.LockInterruptibly(); try { TimeSpan durationToWait = duration; DateTime deadline = DateTime.Now.Add(durationToWait); while (!_wrapped.Poll(out element)) { if (durationToWait.Ticks <= 0) { element = default(T); return(false); } try { _notEmptyCondition.Await(durationToWait); durationToWait = deadline.Subtract(DateTime.Now); } catch (ThreadInterruptedException) { _notEmptyCondition.Signal(); throw; } } _notFullCondition.Signal(); return(true); } finally { currentLock.Unlock(); } }
public TopFieldDocs Call() { Debug.Assert(slice.Leaves.Length == 1); TopFieldDocs docs = searcher.Search(Arrays.AsList(slice.Leaves), weight, after, nDocs, sort, true, doDocScores || sort.NeedsScores, doMaxScore); @lock.Lock(); try { AtomicReaderContext ctx = slice.Leaves[0]; int @base = ctx.DocBase; hq.SetNextReader(ctx); hq.SetScorer(fakeScorer); foreach (ScoreDoc scoreDoc in docs.ScoreDocs) { fakeScorer.doc = scoreDoc.Doc - @base; fakeScorer.score = scoreDoc.Score; hq.Collect(scoreDoc.Doc - @base); } // Carry over maxScore from sub: if (doMaxScore && docs.MaxScore > hq.maxScore) { hq.maxScore = docs.MaxScore; } } finally { @lock.Unlock(); } return(docs); }
/// <summary> /// Inserts the specified element into this queue, waiting if necessary /// for space to become available. /// </summary> /// <param name="element">the element to add</param> /// <exception cref="System.ArgumentNullException"> /// If the specified element is <see langword="null"/> and this queue /// does not permit <see langword="null"/> elements. /// </exception> /// <exception cref="System.ArgumentException"> /// If some property of the supplied <paramref name="element"/> prevents /// it from being added to this queue. /// </exception> public override void Put(T element) { ReentrantLock currentLock = _lock; currentLock.LockInterruptibly(); try { try { while (!_wrapped.Offer(element)) { _notFullCondition.Await(); } _notEmptyCondition.Signal(); } catch (ThreadInterruptedException) { _notFullCondition.Signal(); throw; } } finally { currentLock.Unlock(); } }
/// <summary> /// Synchronously acquire access token, must be called in non-main thread /// </summary> /// <returns>accessToken or null</returns> public string RefreshAccessToken() { Log.Logger.Info(Tag, "refreshAccessToken begin"); try { if (service != null) { getATLock.Lock(); try { GetAccessToken(); } finally { getATLock.Unlock(); } Log.Logger.Debug(Tag, "refreshAccessToken return new"); } else { Log.Logger.Error(Tag, "refreshAccessToken client is null, return null"); } } catch (Exception e) { Log.Logger.Error(Tag, "refreshAccessToken exception, return null"); } Log.Logger.Info(Tag, "refreshAccessToken end"); return(accessToken); }
private void Run() { if (!IsAlive) { SignalStop(); return; } Stopwatch timer = Stopwatch.StartNew(); @lock.Lock(); try { doUpdate(); } catch (Exception exception) { handleException(exception); } finally { @lock.Unlock(); timer.Stop(); long driftAdjusted = Math.Max(interval - timer.ElapsedMilliseconds, 0); if (IsAlive) RegisterWait(driftAdjusted); else SignalStop(); } }
/// <summary> /// Does the real work for all <c>Drain</c> methods. Caller must /// guarantee the <paramref name="action"/> is not <c>null</c> and /// <paramref name="maxElements"/> is greater then zero (0). /// </summary> /// <seealso cref="IBlockingQueue{T}.DrainTo(ICollection{T})"/> /// <seealso cref="IBlockingQueue{T}.DrainTo(ICollection{T}, int)"/> /// <seealso cref="IBlockingQueue{T}.Drain(System.Action{T})"/> /// <seealso cref="IBlockingQueue{T}.DrainTo(ICollection{T},int)"/> protected override int DoDrainTo(Action <T> action, int maxElements) { ReentrantLock currentLock = _lock; currentLock.Lock(); try { T element; int n; for (n = 0; n < maxElements && _wrapped.Poll(out element); n++) { action(element); } if (n == 1) { _notFullCondition.Signal(); } else if (n != 0) { _notFullCondition.SignalAll(); } return(n); } finally { currentLock.Unlock(); } }
/// <summary> /// Retrieves and removes the head of this queue, waiting if necessary /// until an element becomes available. /// </summary> /// <returns> the head of this queue</returns> public override T Take() { ReentrantLock currentLock = _lock; currentLock.LockInterruptibly(); try { try { T element; while (!_wrapped.Poll(out element)) { _notEmptyCondition.Await(); } _notFullCondition.Signal(); return(element); } catch (ThreadInterruptedException) { _notEmptyCondition.Signal(); throw; } } finally { currentLock.Unlock(); } }
public TopFieldDocs Call() { Debug.Assert(Slice.Leaves.Length == 1); TopFieldDocs docs = Searcher.Search(Arrays.AsList(Slice.Leaves), Weight, After, NDocs, Sort, true, DoDocScores || Sort.NeedsScores(), DoMaxScore); @lock.Lock(); try { AtomicReaderContext ctx = Slice.Leaves[0]; int @base = ctx.DocBase; Hq.NextReader = ctx; Hq.Scorer = FakeScorer; foreach (ScoreDoc scoreDoc in docs.ScoreDocs) { FakeScorer.doc = scoreDoc.Doc - @base; FakeScorer.score = scoreDoc.Score; Hq.Collect(scoreDoc.Doc - @base); } // Carry over maxScore from sub: if (DoMaxScore && docs.MaxScore > Hq.MaxScore) { Hq.MaxScore = docs.MaxScore; } } finally { @lock.Unlock(); } return(docs); }
private void DoMaybeRefresh() { // it's ok to call lock() here (blocking) because we're supposed to get here // from either maybeRefreh() or maybeRefreshBlocking(), after the lock has // already been obtained. Doing that protects us from an accidental bug // where this method will be called outside the scope of refreshLock. // Per ReentrantLock's javadoc, calling lock() by the same thread more than // once is ok, as long as unlock() is called a matching number of times. refreshLock.Lock(); bool refreshed = false; try { G reference = Acquire(); try { NotifyRefreshListenersBefore(); G newReference = RefreshIfNeeded(reference); if (newReference != null) { Debug.Assert((object)newReference != (object)reference, "refreshIfNeeded should return null if refresh wasn't needed"); try { SwapReference(newReference); refreshed = true; } finally { if (!refreshed) { Release(newReference); } } } } finally { Release(reference); NotifyRefreshListenersRefreshed(refreshed); } AfterMaybeRefresh(); } finally { refreshLock.Unlock(); } }
public bool AnyChanges() { GlobalBufferLock.@Lock(); try { /* * check if all items in the global slice were applied * and if the global slice is up-to-date * and if globalBufferedUpdates has changes */ return(GlobalBufferedUpdates.Any() || !GlobalSlice.Empty || GlobalSlice.SliceTail != Tail || Tail.Next != null); } finally { GlobalBufferLock.Unlock(); } }
internal bool AnyChanges() { globalBufferLock.@Lock(); try { /* * check if all items in the global slice were applied * and if the global slice is up-to-date * and if globalBufferedUpdates has changes */ return(globalBufferedUpdates.Any() || !globalSlice.IsEmpty || globalSlice.sliceTail != tail || tail.next != null); } finally { globalBufferLock.Unlock(); } }
public void Start() { lifecycleLock.Lock(); try { if (!running) { DoStart(); running = true; if (logger.IsInfoEnabled) { logger.Info("started " + this); } } } finally { lifecycleLock.Unlock(); } }
/** * \brief Deinitialization, unload stuff and release surface texture. */ public void deinit() { unload(); mSurfaceTextureLock.Lock(); mSurfaceTexture = null; mSurfaceTextureLock.Unlock(); }
/** * Retrieves and removes the head of this queue, waiting * if necessary up to the specified wait time, for another thread * to insert it. * * @return the head of this queue, or <tt>null</tt> if the * specified waiting time elapses before an element is present. * @throws InterruptedException {@inheritDoc} */ public override bool Poll(TimeSpan timeout, out T element) { long nanos = timeout.Ticks; // unit.toNanos(timeout); ReentrantLock qlock = _qlock; for (; ;) { Node node; bool mustWait; //if (Thread.interrupted()) throw new InterruptedException(); qlock.Lock(); try { node = _waitingProducers.Dequeue(); mustWait = (node == null); if (mustWait) { node = _waitingConsumers.Enqueue(default(T)); } } finally { qlock.Unlock(); } if (mustWait) { try { T x; if (!node.WaitForPut(nanos, out x)) { UnlinkCancelledConsumer(node); } element = x; return(true); } catch (ThreadInterruptedException) { UnlinkCancelledConsumer(node); throw; } } else { T x; if (node.GetItem(out x)) { element = x; return(true); } // else cancelled, so retry } } }
/** * Inserts the specified element into this queue, waiting if necessary * up to the specified wait time for another thread to receive it. * * @return <tt>true</tt> if successful, or <tt>false</tt> if the * specified waiting time elapses before a consumer appears. * @throws InterruptedException {@inheritDoc} * @throws NullPointerException {@inheritDoc} */ public override bool Offer(T e, TimeSpan timeout) { if (e == null) { throw new ArgumentNullException(); } long nanos = DateTime.Now.Ticks;// unit.toNanos(timeout); ReentrantLock qlock = _qlock; for (; ;) { Node node; bool mustWait; //if (Thread.interrupted()) throw new InterruptedException(); qlock.Lock(); try { node = _waitingConsumers.Dequeue(); mustWait = (node == null); if (mustWait) { node = _waitingProducers.Enqueue(e); } } finally { qlock.Unlock(); } if (mustWait) { try { bool x = node.WaitForTake(nanos); if (!x) { UnlinkCancelledProducer(node); } return(x); } catch (ThreadInterruptedException) { UnlinkCancelledProducer(node); throw; } } if (node.SetItem(e)) { return(true); } // else consumer cancelled, so retry } }
public ZKRebalancerListener( ZookeeperConsumerConnector parent, string group, string consumerIdString, IDictionary <string, IList <KafkaStream <TKey, TValue> > > kafkaMessageAndMetadataStreams) { this.parent = parent; this.group = group; this.consumerIdString = consumerIdString; this.KafkaMessageAndMetadataStreams = kafkaMessageAndMetadataStreams; this.@lock = new ReentrantLock(); this.cond = [email protected](); this.watcherExecutorThread = new Thread(() => { Logger.InfoFormat("starting watcher executor thread for consumer {0}", consumerIdString); bool doRebalance; while (!parent.isShuttingDown.Get()) { try { [email protected](); try { if (!isWatcherTriggered) { cond.Await(TimeSpan.FromMilliseconds(1000)); // wake up periodically so that it can check the shutdown flag } } finally { doRebalance = isWatcherTriggered; isWatcherTriggered = false; @lock.Unlock(); } if (doRebalance) { this.SyncedRebalance(); } } catch (Exception e) { Logger.Error("Error during syncedRebalance", e); } } Logger.InfoFormat("Stoppping watcher executer thread for consumer {0}", consumerIdString); }); this.watcherExecutorThread.Name = consumerIdString + "_watcher_executor"; this.watcherExecutorThread.Start(); }
public void HandleDataDeleted(string dataPath) { dataLock.Lock(); try { dataExistsOrChanged.Signal(); } finally { dataLock.Unlock(); } }
/// <summary> /// Atomically removes all of the elements from this queue. /// The queue will be empty after this call returns. /// </summary> public override void Clear() { ReentrantLock rl = _lock; rl.Lock(); try { _innerQueue.Clear(); } finally { rl.Unlock(); } }
public override bool IsCancelled() { Lock.Lock(); try { return(pm.IsCancelled()); } finally { Lock.Unlock(); } }