Module Async_unix.Thread_safe
The Thread_safe module has functions that are safe to call from threads outside Async, such as the ones spawned by In_thread.run.
This is in contrast with the rest of Async library which is generally not thread-safe.
All the Thread_safe.block* and Thread_safe.run* functions wake up the Async scheduler to ensure that it continues in a timely manner with whatever jobs got started. Some functions take an optional ?wakeup_scheduler:bool argument, which defaults to true. One can cause the scheduler to not be woken up by supplying ~wakeup_scheduler:false, which can reduce CPU use, but increase latency, because the scheduler may not wake up for a while to process jobs.
val am_holding_async_lock : unit -> boolam_holding_async_lock ()returns true if the currently running thread is holding the Async lock.
val deferred : unit -> 'a Async_kernel.Deferred.t * ('a -> unit)deferred ()returns(d, fill)wheredis a deferred that will become determined with valuevoncefill vis called.It is ok to call
deferredfrom inside or outside Async.fillmust be called from outside Async.
val run_in_async_with_optional_cycle : ?wakeup_scheduler:bool -> (unit -> [ `Run_a_cycle | `Do_not_run_a_cycle ] * 'a) -> ('a, exn) Core.Result.trun_in_async_with_optional_cycle facquires the Async lock and runsf ()while holding the lock. Depending on the result off, it may also run a cycle.
val run_in_async : ?wakeup_scheduler:bool -> (unit -> 'a) -> ('a, exn) Core.Result.trun_in_async facquires the Async lock and runsf ()while holding the lock. It returns the result off ()to the outside world. The scheduler is woken up to ensure the code that depends onf ()is run soon enough.run_in_asyncdoesn't run a cycle.run_in_asyncdoes not automatically start the Async scheduler. You still need to callScheduler.goelsewhere in your program.
val run_in_async_exn : ?wakeup_scheduler:bool -> (unit -> 'a) -> 'aval block_on_async : (unit -> 'a Async_kernel.Deferred.t) -> ('a, exn) Core.Result.tblock_on_async frunsf ()in the Async world and blocks until the result becomes determined. This function can be called from the main thread (before Async is started) or from a thread outside Async.block_on_asyncwill run a cycle if the deferred isn't determined, in the hope that running the cycle will cause the deferred to become determined.block_on_asyncwill automatically start the scheduler if it isn't already running.
val block_on_async_exn : (unit -> 'a Async_kernel.Deferred.t) -> 'aval run_in_async_wait : (unit -> 'a Async_kernel.Deferred.t) -> ('a, exn) Core.Result.trun_in_async_wait fis likeblock_on_async f, except that it must be called from a thread outside Async. Upon returning fromrun_in_async_wait, it is guaranteed that the caller does not have the Async lock.
val run_in_async_wait_exn : (unit -> 'a Async_kernel.Deferred.t) -> 'aval reset_scheduler : unit -> unitreset_schedulerstops the scheduler thread and any associated threads, and resets Async's global state to its initial state. This is useful if you need to first use Async to compute a value and then to daemonize (in which case you shoulddaemonizewith~allow_threads_to_have_been_created:true).reset_schedulercan be called from the main thread (before Async is started) or from a thread outside Async.reset_scheduleris known to be imperfect, and to have races in which there are still threads running after it returns.