Class ConcurrentUtils
java.lang.Object
io.servicetalk.concurrent.internal.ConcurrentUtils
Utilities which can be used for concurrency.
-
Method Summary
Modifier and TypeMethodDescriptionstatic <T> long
calculateSourceRequested
(AtomicLongFieldUpdater<T> requestNUpdater, AtomicLongFieldUpdater<T> sourceRequestedUpdater, AtomicLongFieldUpdater<T> emittedUpdater, int limit, T owner) Attempts to incrementsourceRequestedUpdater
in order to make it the same asrequestNUpdater
while not exceeding thelimit
.static <T> boolean
releaseLock
(AtomicIntegerFieldUpdater<T> lockUpdater, T owner) Release a lock that was previously acquired viatryAcquireLock(AtomicIntegerFieldUpdater, Object)
.static <T> boolean
releaseReentrantLock
(AtomicLongFieldUpdater<T> lockUpdater, long acquireId, T owner) Release a lock that was previously acquired viatryAcquireReentrantLock(AtomicLongFieldUpdater, Object)
.static <T> boolean
tryAcquireLock
(AtomicIntegerFieldUpdater<T> lockUpdater, T owner) Acquire a lock that is exclusively held with no re-entry, but attempts to acquire the lock while it is held can be detected byreleaseLock(AtomicIntegerFieldUpdater, Object)
.static <T> long
tryAcquireReentrantLock
(AtomicLongFieldUpdater<T> lockUpdater, T owner) Acquire a lock that allows reentry and attempts to acquire the lock while it is held can be detected byreleaseReentrantLock(AtomicLongFieldUpdater, long, Object)
.
-
Method Details
-
tryAcquireLock
Acquire a lock that is exclusively held with no re-entry, but attempts to acquire the lock while it is held can be detected byreleaseLock(AtomicIntegerFieldUpdater, Object)
.- Type Parameters:
T
- The type of object that owns the lock.- Parameters:
lockUpdater
- TheAtomicIntegerFieldUpdater
used to control the lock state.owner
- The owner of the lock object.- Returns:
true
if the lock was acquired,false
otherwise.
-
releaseLock
Release a lock that was previously acquired viatryAcquireLock(AtomicIntegerFieldUpdater, Object)
.- Type Parameters:
T
- The type of object that owns the lock.- Parameters:
lockUpdater
- TheAtomicIntegerFieldUpdater
used to control the lock state.owner
- The owner of the lock object.- Returns:
true
if the lock was released, and no other attempts were made to acquire the lock while it was held.false
if the lock was released but another attempt was made to acquire the lock before it was released.
-
tryAcquireReentrantLock
Acquire a lock that allows reentry and attempts to acquire the lock while it is held can be detected byreleaseReentrantLock(AtomicLongFieldUpdater, long, Object)
.This lock must eventually be released by the same thread that acquired the lock. If the thread that acquires this lock is terminated before releasing the lock state is undefined.
- Type Parameters:
T
- The type of object that owns the lock.- Parameters:
lockUpdater
- TheAtomicLongFieldUpdater
used to control the lock state.owner
- The owner of the lock object.- Returns:
0
if the acquire was unsuccessful, otherwise an identifier that must be passed to a subsequent call ofreleaseReentrantLock(AtomicLongFieldUpdater, long, Object)
.
-
releaseReentrantLock
public static <T> boolean releaseReentrantLock(AtomicLongFieldUpdater<T> lockUpdater, long acquireId, T owner) Release a lock that was previously acquired viatryAcquireReentrantLock(AtomicLongFieldUpdater, Object)
.- Type Parameters:
T
- The type of object that owns the lock.- Parameters:
lockUpdater
- TheAtomicLongFieldUpdater
used to control the lock state.acquireId
- The value returned from the previous call totryAcquireReentrantLock(AtomicLongFieldUpdater, Object)
.owner
- The owner of the lock object.- Returns:
true
if the lock was released, or releases a prior re-entrant acquire, and no other attempts were made to acquire the lock while it was held.false
if the lock was released but another attempt was made to acquire the lock before it was released.
-
calculateSourceRequested
public static <T> long calculateSourceRequested(AtomicLongFieldUpdater<T> requestNUpdater, AtomicLongFieldUpdater<T> sourceRequestedUpdater, AtomicLongFieldUpdater<T> emittedUpdater, int limit, T owner) Attempts to incrementsourceRequestedUpdater
in order to make it the same asrequestNUpdater
while not exceeding thelimit
. Note that the return value maybe larger thanlimit
if there is "stolen" demand (e.g.emitting > sourceRequested
). In this case the assumed peer sources have requested signals, and instead the signals were delivered to the local source.- Type Parameters:
T
- The type of object which owns the atomic updater parameters.- Parameters:
requestNUpdater
- The total number which has been requested (typically fromPublisherSource.Subscription.request(long)
).sourceRequestedUpdater
- The total number which has actually been passed toPublisherSource.Subscription.request(long)
. This outstanding countsourceRequestedUpdater - emittedUpdater
should not exceedlimit
unless there are peer sources which may result in "unsolicited" emissions.emittedUpdater
- The amount of data that has been emitted/delivered by the source.limit
- The maximum outstanding demand from the source at any given time.owner
- The object which all atomic updater parameters are associated with.- Returns:
- The amount which
sourceRequestedUpdater
was increased plus any "stolen" demand. This value is typically used for upstreamPublisherSource.Subscription.request(long)
calls.
-