package domain-local-timeout

All checked builds pass
  1. Overview
  2. Docs
A scheduler independent timeout mechanism

Install

dune-project
 Dependency

Authors

Maintainers

Sources

domain-local-timeout-1.0.1.tbz
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.

Added to opam-repository:

README

API reference

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)

  1. thread-table >= "1.0.0"
  2. mtime >= "2.0.0"
  3. psq >= "0.2.1"
  4. ocaml >= "4.12.0"
  5. dune >= "3.3"

Dev Dependencies (4)

  1. odoc with-doc
  2. alcotest >= "1.7.0" & with-test
  3. mdx >= "2.3.0" & with-test
  4. domain-local-await >= "1.0.0" & with-test

Optional Dependencies

None

Used by (1)

  1. kcas >= "0.6.0"

Conflicts

None

Build status

Results of building domain-local-timeout with each compiler on check.ci.ocaml.org, run of against opam-repository 8a77e58. All results for this package.

Each result links to its build log. To keep crawlers from overloading the site, build logs require a GitHub sign-in (see ocurrent/opam-health-check). On your first log, GitHub asks you to “Authorize opam-health-check”, the OCurrent app that runs these checks; it sees only your public profile, not your repositories. The sign-in lasts 30 days.

Version OCaml 4.11 OCaml 4.14 OCaml 5.2 OCaml 5.4 OCaml 5.5
1.0.1 (this version) n/a good good good good
  • good Package successfully built
  • partial One of the dependencies failed to build
  • bad Package failed to build
  • n/a Package is not available in this environment
  • internal Internal failure