Worker

Native
1.3
class Worker
(source)

Properties

Native
1.3

id

val id: Int
Native
1.3

name

Name of the worker, as specified in Worker.start or "worker $id" by default,

val name: String
Native
1.3

platformThreadId

Get platform thread id of the worker thread.

val platformThreadId: ULong

Functions

Native
1.3

asCPointer

Convert worker to a COpaquePointer value that could be passed via native void* pointer. Can be used as an argument of Worker.fromCPointer.

fun asCPointer(): COpaquePointer?
Native
1.3

execute

Plan job for further execution in the worker. Execute is a two-phase operation:

fun <T1, T2> execute(
    mode: TransferMode,
    producer: () -> T1,
    job: (T1) -> T2
): Future<T2>
Native
1.3

executeAfter

Plan job for further execution in the worker.

fun executeAfter(
    afterMicroseconds: Long = 0,
    operation: () -> Unit)
Native
1.3

park

Park execution of the current worker until a new request arrives or timeout specified in timeoutMicroseconds elapsed. If process is true, pending queue elements are processed, including delayed requests. Note that multiple requests could be processed this way.

fun park(
    timeoutMicroseconds: Long,
    process: Boolean = false
): Boolean
Native
1.3

processQueue

Process pending job(s) on the queue of this worker. Note that jobs scheduled with executeAfter using non-zero timeout are not processed this way. If termination request arrives while processing the queue via this API, worker is marked as terminated and will exit once the current request is done with.

fun processQueue(): Boolean
Native
1.3

requestTermination

Requests termination of the worker.

fun requestTermination(
    processScheduledJobs: Boolean = true
): Future<Unit>
Native
1.3

toString

String representation of the worker.

fun toString(): String

Companion Object Properties

Native
1.3

activeWorkers

Get a list of all unterminated workers.

val activeWorkers: List<Worker>
Native
1.3

current

Return the current worker. Worker context is accessible to any valid Kotlin context, but only actual active worker produced with Worker.start automatically processes execution requests. For other situations processQueue must be called explicitly to process request queue.

val current: Worker

Companion Object Functions

Native
1.3

fromCPointer

Create worker object from a C pointer.

fun fromCPointer(pointer: COpaquePointer?): Worker
Native
1.3

start

Start new scheduling primitive, such as thread, to accept new tasks via execute interface. Typically new worker may be needed for computations offload to another core, for IO it may be better to use non-blocking IO combined with more lightweight coroutines.

fun start(
    errorReporting: Boolean = true,
    name: String? = null
): Worker