package domain-local-timeout

  1. Overview
  2. Docs
A scheduler independent timeout mechanism

Install

Dune Dependency

Authors

Maintainers

Sources

domain-local-timeout-1.0.0.tbz
sha256=8a2f719e34341bc83af774fcd8c21c7f3583b87f4dff10d01e820a2ae47e2855
sha512=4d2885077857b0266361192f07a86a9b2f7c7d2f5510adc175aa5e010e82e424404d8081286f22da6fc368489d82a247230f2d63d419d0c437c736b6ac4cbb52

Description

A low level mechanism intended for writing higher level libraries that need to be able to have scheduler friendly timeouts.

Published: 16 Aug 2023

README

README.md

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. domain-local-await >= "1.0.0" & with-test
  4. mdx >= "1.10.0" & with-test

Used by (1)

  1. kcas >= "0.6.0" & < "0.7.0"

Conflicts

None

OCaml

Innovation. Community. Security.