Module Tutils


module Tutils: sig .. end
Multithreading utilities

val create : ('a -> unit) -> 'a -> string -> Thread.t

Thread wrapper

Give names to threads, and forbid them to raise an exception; if that happens, the thread dies anyway but it is logged and main will notice it. The main process is expected to run main after having launched the needed threads: that function will sleep until a thread raises an exception.
val main : unit -> unit
val shutdown : unit -> unit
exception Exit
Special exception allowed for "clean" termination of Tutils threads. All other exceptions are reported as bugs.
val join_all : unit -> unit
Wait for the threads to terminate, never return if some thread keeps running.
val running : string -> Thread.t -> bool
Check if a thread is running.

Multi-tasking scheduler



type priority =
| Blocking (*For example a last.fm submission.*)
| Maybe_blocking (*Request resolutions vary a lot.*)
| Non_blocking (*Non-blocking tasks like the server.*)
Priorities for the different scheduler usages.
val scheduler : priority Duppy.scheduler
task scheduler
val need_non_blocking_queue : unit -> unit
Ask for a special queue that treats exclusively non blocking tasks. This is need for things such as the server or harbor to run smoothly.

Misc


val wait : Condition.t -> Mutex.t -> (unit -> bool) -> unit
Waits for f() to become true on condition c. The mutex m protecting data accessed by f is in the same state before and after the call.
val mutexify : Mutex.t -> ('a -> 'b) -> 'a -> 'b
Make a function work in critical section, protected by a given lock.
exception Timeout
val wait_for : ?mutex:Mutex.t ->
?log:(string -> unit) ->
[ `Both | `Read | `Write ] -> file_descr -> float -> unit
val finalize : k:(unit -> unit) -> (unit -> 'a) -> 'a
finalize ~k f calls f and returns it result, and always executes k, even when f raises an exception.
val seems_locked : Mutex.t -> bool
Tests whether a mutex is locked, without blocking. We cannot check on Win32, where true is always returned: it always "seems" OK, we don't raise false alarms. This is meant to be used for assertions.
val lazy_cell : (unit -> 'a) -> unit -> 'a
Thread-safe equivalent to Lazy.lazy_from_fun.
val stoppable_thread : ((unit -> bool) * (unit -> unit) -> unit) ->
string -> (unit -> unit) * (unit -> unit)
Preemptive stoppable thread.

The thread function receives a should_stop,has_stop pair on startup. It should regularly poll the should_stop and stop when asked to. Before stopping it should call has_stopped.

The function returns a kill,wait pair. The first function should be called to request that the thread stops, and the second to wait that it has effectively stopped.