tur/thread
POSIX thread spawn, join, and detach wrappers.
Since: Phase T19-C
thread-spawn-fn
(thread-spawn-fn [fn-ptr :ptr<void> arg :ptr<void>])
spawn a new OS thread calling fn-ptr with arg.
| fn-ptr | C function pointer of type void *(*)(void *) | |
| arg | opaque argument forwarded to fn-ptr |
A ptr<void> thread handle that must be passed to thread-join or thread-detach.
(let [t (thread-spawn-fn my-fn arg)] (thread-join t)) ; => ptr<void>
Since: Phase T19-C
thread-join
(thread-join [t :ptr<void>])
wait for a thread to finish and free its handle.
| t | thread handle returned by thread-spawn-fn |
(thread-join t)
Since: Phase T19-C
thread-detach
(thread-detach [t :ptr<void>])
detach a thread (it cleans up itself on exit) and free the handle.
| t | thread handle returned by thread-spawn-fn |
(thread-detach t)
Since: Phase T19-C
thread-id
(thread-id)
return an integer identifier for the calling OS thread.
The current thread's pthread_t value cast to int64_t.
(thread-id) ; => int
Since: Phase T19-C
cancel-thread
(cancel-thread [t :ptr<void>])
request cooperative cancellation of a thread handle.
| t | a thread handle returned by thread-spawn-fn |
:void
(let [t (thread-spawn-fn my-fn arg)]
(cancel-thread t)
(thread-join t))
Since: Phase TC0
cancelled?
(cancelled?)
return true if the current thread has been cancelled.
bool -- true if a cancellation has been requested for this thread
(when (cancelled?) (cleanup-and-return))
Since: Phase TC0
with-cancel-guard
(with-cancel-guard [cleanup :ptr<void> body :ptr<void>])
run a body closure; on cancellation, run cleanup and return nil.
| cleanup | zero-argument closure: (fn [] ...) to run on cancellation | |
| body | zero-argument closure: (fn [] ...) to run normally |
:nil (always; body result is discarded)
(with-cancel-guard
(fn [] (release resource))
(fn []
(let [v (chan-recv ch)]
(process v))))
Since: Phase TC1
yield-point
(yield-point)
check for a pending cancellation and exit if one is set.
:void (never returns if the thread has been cancelled)
(defn crunch [n] :int
(let [i 0]
(while (< i n)
(yield-point)
(do-work i)
(set! i (+ i 1)))))
Since: Phase TC2
Internal definitions
__yield-point-impl-- internal: check cancel and exit if cancelled.