package sel
All checked builds passInstall
dune-project
Dependency
Authors
Maintainers
Sources
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
sellibrary (moduleSel), the default, provides a backend based onUnix.selectand extends this API with events on Unix file descriptors and processes; - the
sel.jslibrary (moduleSel_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 ... endThe type of events and operations (setting priority, cancelling, ...)
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 ... endSimple 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 ... endEvents one can wait for (read data, pull from queues, ...)
val now :
?priority:int ->
?name:string ->
?undup:('a -> 'a -> bool) ->
'a ->
'a Event.tmix 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.pop_ready todo returns an event if one is ready, never blocks.
wait_ready todo returns all ready events with the lowest priority, never blocks.
wait for one event. If more are ready, return the one with higher priority.
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.
waits until some event is ready. All ready events with the lowest priority are returned.