package domain-local-timeout
Install
Dune Dependency
Authors
Maintainers
Sources
sha256=eac0aa5243a337c13eece2d4c15427b64bf4beb4039060d5f0a36a0e1566d1df
sha512=32ecb0c41a10e3a68f5a8774c48d6b8598cbc81494dd1fb716c7d2d97ca14cbe2b59a02aa64d2ee6d53aa57b4e16c8992b6a592938bb3c70a2517273bde340f3
Description
A low level mechanism intended for writing higher level libraries that need to be able to have scheduler friendly timeouts.
Published: 06 Feb 2024
README
README.md
domain-local-timeout — Scheduler independent timeout
A low level mechanism intended for writing higher level libraries that need to be able to have scheduler friendly timeouts.
A library that needs timeouts may simply call set_timeoutf
.
To provide an efficient scheduler specific implementation of the mechanism, schedulers may install an implementation by wrapping the scheduler main loop with a call to using
. The implementation is then stored in a domain, and optionally thread, local variable. The overhead that this imposes on a scheduler should be insignificant.
An application can then choose to use schedulers that provide the necessary implementation or, for example, use the default implementation by calling set_system
.
The end result is effective interoperability between schedulers and concurrent programming libraries.
References
DLT is used by the following libraries:
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.
Let's try it:
# sleepf 1.0
Exception: Failure "Domain_local_timeout.set_timeoutf not implemented".
Oops!
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-timeout 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
>= "1.0.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
-
mdx
>= "2.3.0" & with-test
-
domain-local-await
>= "1.0.0" & with-test
Used by (1)
-
kcas
>= "0.6.0"
Conflicts
None