tur/fiber
lightweight cooperative fibers backed by the FiberBlock runtime.
Since: Phase T21-C
fiber-new
(fiber-new [fn :ptr<void> stack-size :int] :ptr<void>)
create a new fiber from a function pointer and an optional stack size.
| fn | zero-argument function pointer to run as the fiber body | |
| stack-size | requested stack size in bytes; 0 uses the default size |
An opaque ptr<void> handle to the FiberBlock.
(let [f (fiber-new my-fn 0)] ...) ; => ptr<void>
Since: Phase T21-C
fiber-resume
(fiber-resume [f :ptr<void> arg :int] :int)
resume a suspended fiber, passing it a value, and return the yielded or final value.
| f | fiber handle returned by fiber-new | |
| arg | value passed to the fiber as the resume argument |
The int64_t value yielded by the fiber, or its final return value.
(fiber-resume f 0) ; => int
Since: Phase T21-C
fiber-yield
(fiber-yield [value :int] :nil)
suspend the current fiber and return a value to its caller.
| value | int64_t value to hand back to the fiber-resume caller |
(fiber-yield 42)
Since: Phase T21-C
fiber-done?
(fiber-done? [f :ptr<void>] :int)
check whether a fiber has run to completion.
| f | fiber handle returned by fiber-new |
Non-zero (truthy) if the fiber has returned; 0 if it is still runnable or suspended.
(if (fiber-done? f) ...) ; => int
Since: Phase T21-C
fiber-arg
(fiber-arg [f :ptr<void>] :int)
retrieve the most recent argument value passed to a fiber via fiber-resume.
| f | fiber handle returned by fiber-new |
The int64_t argument last supplied to fiber-resume, or 0 if the handle is NULL.
(fiber-arg f) ; => int
Since: Phase T21-C
fiber-free
(fiber-free [f :ptr<void>] :nil)
release all memory associated with a fiber.
| f | fiber handle returned by fiber-new |
(fiber-free f)
Since: Phase T21-C
fiber-local-get
(fiber-local-get [f :ptr<void> key :int] :int)
retrieve a fiber-local value by key from a specific fiber.
| f | fiber handle returned by fiber-new | |
| key | int64_t key identifying the slot |
The int64_t value stored at key, or 0 if not set.
(fiber-local-get f 1) ; => int
Since: Phase T21
fiber-local-set!
(fiber-local-set! [f :ptr<void> key :int value :int] :nil)
store a fiber-local value by key in a specific fiber.
| f | fiber handle returned by fiber-new | |
| key | int64_t key identifying the slot | |
| value | int64_t value to store |
(fiber-local-set! f 1 99)
Since: Phase T21
fiber-local-current-get
(fiber-local-current-get [key :int] :int)
retrieve a fiber-local value by key from the currently running fiber.
| key | int64_t key identifying the slot |
The int64_t value stored at key in the current fiber, or 0 if not set.
(fiber-local-current-get 1) ; => int
Since: Phase T21
fiber-local-current-set!
(fiber-local-current-set! [key :int value :int] :nil)
store a fiber-local value by key in the currently running fiber.
| key | int64_t key identifying the slot | |
| value | int64_t value to store |
(fiber-local-current-set! 1 99)
Since: Phase T21
scheduler-yield!
(scheduler-yield! :nil)
yield the current fiber back to the scheduler, re-enqueueing it for the next turn.
(scheduler-yield!)
Since: Phase T21
scheduler-park!
(scheduler-park! :nil)
park the current fiber, suspending it without re-enqueueing.
(scheduler-park!)
Since: Phase T21
scheduler-unpark!
(scheduler-unpark! [f :ptr<void>] :nil)
unpark a specific fiber and add it back to the scheduler run queue.
| f | fiber handle to unpark |
(scheduler-unpark! f)
Since: Phase T21
async-sleep
(async-sleep [ms :int] :nil)
sleep for approximately ms milliseconds without blocking the OS thread.
| ms | duration in milliseconds; values <= 0 return immediately |
(async-sleep 100)
Since: Phase T24
fiber-thread-id
(fiber-thread-id :int)
return an integer identifier for the OS thread running the current fiber.
The current pthread_t cast to int64_t.
(fiber-thread-id) ; => int
Since: Phase T23