package sel

All checked builds pass
  1. Overview
  2. Docs
Simple Event Library

Install

dune-project
 Dependency

Authors

Maintainers

Sources

sel-0.10.0.tbz
sha256=9151431abaec30aeb93a4f5618587516023f9945ade84555d6e96cc517616cda
sha512=4d2401609602da9f08cfdab75ae4bc97205e44b7284ef979f3a7ba9acf9d378ada40fe6fd4f4a4a09457ea43cd3aa7a33eea601cf9fa914b4a99a3a52989dadd

doc/sel.core/Sel_core/index.html

Module Sel_coreSource

Simple event library, core part.

sel.core is a virtual library, it does not depend on Unix and can be used, for example, in a JavaScript or WebAssembly program. A program picks an implementation by linking it:

  • the sel library (module Sel), the default, provides a backend based on Unix.select and extends this API with events on Unix file descriptors and processes;
  • the sel.js library (module Sel_js) provides a backend and a driver for programs compiled with js_of_ocaml or wasm_of_ocaml.

Libraries only creating events (queues, promises, now) can depend on sel.core and work with both.

With sel.js the blocking APIs (pop, wait, ...) fail when they would have to block: use the non blocking ones (pop_ready, wait_ready) or Sel_js.run instead.

The main loop goes like this


type top_event =
  | NotForMe of Component.Event.t
  | Echo of string

let echo : top_event Event.t =
  On.queue input_queue (fun s -> Echo s)

let handle_event = function
  | NotForMe e ->
      Component.handle_event e |>
      List.map (Event.map (fun x -> NotForMe x))
  | Echo text ->
      Printf.eprintf "echo: %s\n" text;
      [echo]

let rec loop evs =
  let ready, evs = Todo.pop evs in
  let new_evs = handle_event ready in
  loop (Todo.add evs new_evs)

let main () =
  loop (Todo.add Todo.empty [echo; ...])
module Event : sig ... end

The type of events and operations (setting priority, cancelling, ...)

Sourcemodule System_event : sig ... end

Extension point for system events, for example input on a file descriptor. The sel library uses it to implement the Unix specific events.

module Promise : sig ... end

Simple promise library for synchronization. There is no critical section support: if two threads run if not (is_resolved p) then fulfill r the behavior is undefined. Use only in a 1 producer 1 consumer scenario.

module On : sig ... end

Events one can wait for (read data, pull from queues, ...)

val now : ?priority:int -> ?name:string -> ?undup:('a -> 'a -> bool) -> 'a -> 'a Event.t

mix a regular computations with blocking events. E.g. to make a fold interruptible one can do something like:

type event =
  | Fold of (int -> int -> int) * int * int list
  | Other

let handle_event = function
  | Other -> ...
  | Fold(_,acc,[]) -> Printf "done: %s\n" acc; []
  | Fold(f,acc,x::xs) -> [now (Fold (f, f acc x, xs))

let main =
  let rec loop todo =
    match pop_opt todo with
    | None -> exit 0
    | Some e ->
        let es = handle_event e in
        loop (Todo.add todo es)
  in
    loop (Todo.add Todo.empty [now (Fold((+),0,[1;2;3])); ...])

  The [~undup] comparison test, when provided, is used to filter out
  duplicates from the list of tasks.
Sourcemodule Todo : sig ... end

Set of events being waited for

Sourceval pop_ready : 'a Todo.t -> 'a option * 'a Todo.t

pop_ready todo returns an event if one is ready, never blocks.

Sourceval wait_ready : 'a Todo.t -> 'a list * 'a Todo.t

wait_ready todo returns all ready events with the lowest priority, never blocks.

Sourceval pop : 'a Todo.t -> 'a * 'a Todo.t

wait for one event. If more are ready, return the one with higher priority.

  • raises Failure

    when there is nothing left to do

Sourceval pop_opt : 'a Todo.t -> 'a option * 'a Todo.t

same as pop but retuning an option

Sourceval pop_timeout : stop_after_being_idle_for:float -> 'a Todo.t -> 'a option * 'a Todo.t

same as pop_opt but retuning a None if no event is ready in stop_after_being_idle_for seconds. Precision is about a tenth of a second.

Sourceval wait : 'a Todo.t -> 'a list * 'a Todo.t

waits until some event is ready. All ready events with the lowest priority are returned.

Sourceval wait_timeout : stop_after_being_idle_for:float -> 'a Todo.t -> 'a list * 'a Todo.t

Same as wait but returns empty lists if no event is ready in stop_after_being_idle_for seconds. Precision is about a tenth of a second.