Worker

inner class Worker(firstTask: Runnable?) : AbstractQueuedSynchronizer, CoroutineRunnable

Class Worker mainly maintains interrupt control state for threads running tasks, along with other minor bookkeeping. This class opportunistically extends AbstractQueuedSynchronizer to simplify acquiring and releasing a lock surrounding each task execution. This protects against interrupts that are intended to wake up a worker thread waiting for a task from instead interrupting a task being run. We implement a simple non-reentrant mutual exclusion lock rather than use ReentrantLock because we do not want worker tasks to be able to reacquire the lock when they invoke pool control methods like setCorePoolSize. Additionally, to suppress interrupts until the thread actually starts running tasks, we initialize lock state to a negative value, and clear it upon start (in runWorker).

Constructors

Link copied to clipboard
constructor(firstTask: Runnable?)

Properties

Link copied to clipboard

Per-thread task counter

Link copied to clipboard
Link copied to clipboard
var firstTask: Runnable?

Initial task to run. Possibly null.

Link copied to clipboard
Link copied to clipboard
Link copied to clipboard
val thread: Job

Thread this worker is running in. Null if factory fails.

Functions

Link copied to clipboard
suspend fun acquire(arg: Int)

Acquires in exclusive mode, ignoring interrupts. Implemented by invoking at least once .tryAcquire, returning on success. Otherwise the thread is queued, possibly repeatedly blocking and unblocking, invoking .tryAcquire until success. This method can be used to implement method Lock.lock.

suspend fun acquire(node: AbstractQueuedSynchronizer.Node?, arg: Int, shared: Boolean, interruptible: Boolean, timed: Boolean, time: Long): Int

Main acquire method, invoked by all exported acquire methods.

Link copied to clipboard
suspend fun acquireInterruptibly(arg: Int)

Acquires in exclusive mode, aborting if interrupted. Implemented by first checking interrupt status, then invoking at least once .tryAcquire, returning on success. Otherwise the thread is queued, possibly repeatedly blocking and unblocking, invoking .tryAcquire until success or the thread is interrupted. This method can be used to implement method Lock.lockInterruptibly.

Link copied to clipboard
suspend fun acquireShared(arg: Int)

Acquires in shared mode, ignoring interrupts. Implemented by first invoking at least once .tryAcquireShared, returning on success. Otherwise the thread is queued, possibly repeatedly blocking and unblocking, invoking .tryAcquireShared until success.

Link copied to clipboard

Acquires in shared mode, aborting if interrupted. Implemented by first checking interrupt status, then invoking at least once .tryAcquireShared, returning on success. Otherwise the thread is queued, possibly repeatedly blocking and unblocking, invoking .tryAcquireShared until success or the thread is interrupted.

Link copied to clipboard

Returns true if the apparent first queued thread, if one exists, is waiting in exclusive mode. If this method returns true, and the current thread is attempting to acquire in shared mode (that is, this method is invoked from .tryAcquireShared) then it is guaranteed that the current thread is not the first queued thread. Used only as a heuristic in ReentrantReadWriteLock.

Link copied to clipboard

Enqueues the node unless null. (Currently used only for ConditionNodes; other cases are interleaved with acquires.)

Link copied to clipboard

Returns a collection containing those threads that may be waiting on the given condition associated with this synchronizer. Because the actual set of threads may change dynamically while constructing this result, the returned collection is only a best-effort estimate. The elements of the returned collection are in no particular order.

Link copied to clipboard

Returns an estimate of the number of threads waiting on the given condition associated with this synchronizer. Note that because timeouts and interrupts may occur at any time, the estimate serves only as an upper bound on the actual number of waiters. This method is designed for use in monitoring system state, not for synchronization control.

Link copied to clipboard

Queries whether any threads have ever contended to acquire this synchronizer; that is, if an acquire method has ever blocked.

Link copied to clipboard

Queries whether any threads have been waiting to acquire longer than the current thread.

Link copied to clipboard

Queries whether any threads are waiting to acquire. Note that because cancellations due to interrupts and timeouts may occur at any time, a true return does not guarantee that any other thread will ever acquire.

Link copied to clipboard

Queries whether any threads are waiting on the given condition associated with this synchronizer. Note that because timeouts and interrupts may occur at any time, a true return does not guarantee that a future signal will awaken any threads. This method is designed primarily for use in monitoring of the system state.

Link copied to clipboard
Link copied to clipboard

Returns true if node is found in traversal from tail

Link copied to clipboard
fun isQueued(thread: Job): Boolean

Returns true if the given thread is currently queued.

Link copied to clipboard
suspend fun lock()
Link copied to clipboard

Queries whether the given ConditionObject uses this synchronizer as its lock.

Link copied to clipboard
fun release(arg: Int): Boolean

Releases in exclusive mode. Implemented by unblocking one or more threads if .tryRelease returns true. This method can be used to implement method Lock.unlock.

Link copied to clipboard

Releases in shared mode. Implemented by unblocking one or more threads if .tryReleaseShared returns true.

Link copied to clipboard
open override fun run()

Delegates main run loop to outer runWorker.

Link copied to clipboard
open suspend override fun runSuspending()
Link copied to clipboard
open override fun toString(): String

Returns a string identifying this synchronizer, as well as its state. The state, in brackets, includes the String "State =" followed by the current value of .getState, and either "nonempty" or "empty" depending on whether the queue is empty.

Link copied to clipboard
suspend fun tryAcquireNanos(arg: Int, nanosTimeout: Long): Boolean

Attempts to acquire in exclusive mode, aborting if interrupted, and failing if the given timeout elapses. Implemented by first checking interrupt status, then invoking at least once .tryAcquire, returning on success. Otherwise, the thread is queued, possibly repeatedly blocking and unblocking, invoking .tryAcquire until success or the thread is interrupted or the timeout elapses. This method can be used to implement method Lock.tryLock.

Link copied to clipboard
suspend fun tryAcquireSharedNanos(arg: Int, nanosTimeout: Long): Boolean

Attempts to acquire in shared mode, aborting if interrupted, and failing if the given timeout elapses. Implemented by first checking interrupt status, then invoking at least once .tryAcquireShared, returning on success. Otherwise, the thread is queued, possibly repeatedly blocking and unblocking, invoking .tryAcquireShared until success or the thread is interrupted or the timeout elapses.

Link copied to clipboard
suspend fun tryLock(): Boolean
Link copied to clipboard
fun unlock()
Link copied to clipboard
fun <T> withSimpleLock(block: () -> T): T