package domain-local-timeout
Install
Dune Dependency
Authors
Maintainers
Sources
sha256=513a9c1dd18037f2ebbee98f856e02cba458f1127f8953b9cd324aae13d14e39
sha512=ceb9af7dec7a677d320ffbbe6e0d7f70d2501552f727a852d1d5e16d24c1ed441bea764cfc7ae52a60d4f185cebb2de5bee272769327415bdb546b78e2a189dc
Description
Published: 21 Jun 2023
README
README.md
domain-local-timeout — Scheduler independent timeout
This is an experimental library to provide a scheduler independent timeout mechanism.
Example
First we require some libraries we are using:
# #thread
# #require "domain-local-timeout"
# #require "domain-local-await"
Here is how one could implement a scheduler independent and friendly way to sleep:
# let sleepf seconds =
let t = Domain_local_await.prepare_for_await () in
let cancel = Domain_local_timeout.set_timeoutf seconds t.release in
try t.await ()
with exn ->
cancel ();
raise exn
val sleepf : float -> unit = <fun>
Note that the above is careful to call cancel
in case await
raises an exception. That could happen when the fiber on which sleepf
was called is canceled, in which case it makes sense to cancel the timeout.
To actually use domain-local-timeout we need an implementation. There is a default implementation that uses the Stdlib.Thread
and Stdlib.Unix
modules, but it is also possible to implement the facility in other ways and it is recommended for schedulers to provide their own optimized implementations. Both of those system modules are optional and are not provided on all platforms. For these reasons domain-local-timeout does not directly depend on those libraries. To use the default implementation, we need to require those libraries and tell domain-local-await that it can use those system libraries:
# Domain_local_timeout.set_system (module Thread) (module Unix)
- : unit = ()
Now we are ready to try setting timeouts:
# let cancel =
Domain_local_timeout.set_timeoutf 0.1 @@ fun () ->
Printf.printf "world!\n%!"
in
Printf.printf "Hello, %!";
try sleepf 0.2
with exn ->
cancel ();
raise exn
Hello, world!
- : unit = ()
The above example first registers a timeout to print the end of the message and then immediately prints the beginning. Finally the example sleeps a bit to wait for the end to be printed.
Dependencies (5)
-
thread-table
>= "0.1.0"
-
mtime
>= "2.0.0"
-
psq
>= "0.2.1"
-
ocaml
>= "4.12.0"
-
dune
>= "3.3"
Dev Dependencies (4)
-
odoc
with-doc
-
alcotest
>= "1.7.0" & with-test
-
domain-local-await
>= "0.1.0" & with-test
-
mdx
>= "1.10.0" & with-test
Used by (1)
-
kcas
>= "0.6.0" & < "0.7.0"
Conflicts
None