module Task:sig
..end
type 'a
task
type 'a
status =
| |
Timeout of |
| |
Canceled |
| |
Result of |
| |
Failed of |
val error : exn -> string
val wait : 'a task -> 'a status
val map : ('a -> 'b) -> 'a status -> 'b status
val pretty : (Format.formatter -> 'a -> unit) ->
Format.formatter -> 'a status -> unit
val nop : unit task
val return : 'a -> 'a task
val raised : exn -> 'a task
val canceled : unit -> 'a task
val failed : ('a, Format.formatter, unit, 'b task) Pervasives.format4 -> 'a
Failure
exception.
Typically: [let exit d : 'a task = failed "exit status %d" k]
val call : ('a -> 'b) -> 'a -> 'b task
val todo : (unit -> 'a task) -> 'a task
val status : 'a status -> 'a task
val bind : 'a task -> ('a status -> 'b task) -> 'b task
bind t k
first runs t
. Then, when t
exit with status s
,
it starts task k s
.
Remark: If t
was cancelled, k s
is still evaluated, but
immediately canceled as well. This allows finally
-like behaviors to
be implemented. To evaluate k r
only when t
terminates normally,
make use of the sequence
operator.
val sequence : 'a task -> ('a -> 'b task) -> 'b task
sequence t k
first runs t
. If t
terminates with Result r
,
then task k r
is started.
Otherwise, failure or cancelation of t
is returned.val job : 'a task -> unit task
val finally : 'a task -> ('a status -> unit) -> 'a task
finally t cb
runs task t
and always calls cb s
when t
exits
with status s
. Then s
is returned. If the callback cb
raises an exception, the returned status is emitted.val callback : 'a task -> ('a status -> unit) -> unit task
finally
but the status of the task is discarded.val (>>>) : 'a task -> ('a status -> 'b task) -> 'b task
bind
infix.val (>>=) : 'a task -> ('a -> 'b task) -> 'b task
sequence
infix.val (>>?) : 'a task -> ('a status -> unit) -> 'a task
finally
infix.val (>>!) : 'a task -> ('a status -> unit) -> unit task
callback
infix.type
mutex
val mutex : unit -> mutex
val sync : mutex -> (unit -> 'a task) -> 'a task
val command : ?timeout:int ->
?time:float Pervasives.ref ->
?stdout:Buffer.t ->
?stderr:Buffer.t -> string -> string array -> int task
0
, which means no-timeout at all.
Standard outputs are discarded unless optional buffers are provided.
To make the task start later, simply use todo (command ...)
.
When two tasks A
and B
share a common sub-task S
,
cancelling A
will make B
fail either. To prevent this, it is
necessary to make S
shareable and to use two distinct instances of S
in A
and B
.
Shared tasks manage the number of their instance and actually run
or cancel a unique task on demand. In particular, shared tasks can
be canceled and re-started later.
descr:string -> retry:bool -> (unit -> 'a task) -> 'a shared
: retry
is true
.
Otherwize, further instances will return Failed
status.'a shared -> 'a task
: type
thread
val thread : 'a task -> thread
val cancel : thread -> unit
val running : thread -> bool
val run : thread -> unit
on_idle
.type
pool
val pool : unit -> pool
val add : pool -> thread -> unit
val iter : (thread -> unit) -> pool -> unit
val flush : pool -> unit
val size : pool -> int
type
server
val server : ?stages:int -> ?procs:int -> unit -> server
stages
: number of queues in the server.
Stage 0 tasks are issued first. Default is 1.procs
: maximum number of running tasks. Default is 4.val spawn : server -> ?pool:pool -> ?stage:int -> thread -> unit
val launch : server -> unit
val cancel_all : server -> unit
val set_procs : server -> int -> unit
val on_server_activity : server -> (unit -> unit) -> unit
val on_server_start : server -> (unit -> unit) -> unit
val on_server_stop : server -> (unit -> unit) -> unit
val on_server_wait : server -> (unit -> unit) -> unit
val scheduled : server -> int
val terminated : server -> int
val waiting : server -> int option
val on_idle : ((unit -> bool) -> unit) Pervasives.ref
!on_idle f
should repeatedly calls f
until it returns false
.
Default implementation rely on Unix.sleep 1
and Db.progress
.
See also Gtk_helper
module implementation.