Skip to content

Commit

Permalink
Expose on_{read,write} to let the user to suspend on events
Browse files Browse the repository at this point in the history
This API is experimental but can be useful (mainly if we want to
read/write on bigstring via Miou without an extension of the core API).
  • Loading branch information
dinosaure committed Jan 10, 2024
1 parent aab9996 commit 2717401
Show file tree
Hide file tree
Showing 2 changed files with 16 additions and 6 deletions.
17 changes: 11 additions & 6 deletions lib/miou_unix.ml
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,7 @@ module Table = Hashtbl.Make (struct
type t = Unix.file_descr

let equal a b = Int.equal (Obj.magic a) (Obj.magic b)
let hash v = (Obj.magic v) land max_int
let hash v = Obj.magic v land max_int
end)

type unix_scheduler = {
Expand Down Expand Up @@ -138,8 +138,8 @@ let rec smallest_sleeper ~now sleepers =
let delta = time -. now in
if delta <= 0. then None else Some delta

let blocking_read fd =
let syscall = Miou.make (Fun.const ()) in
let on_read fd fn =
let syscall = Miou.make fn in
let uid = Miou.uid syscall in
let unix = get () in
Hashtbl.add unix.revert uid fd;
Expand All @@ -152,8 +152,10 @@ let blocking_read fd =
set unix;
Miou.suspend syscall

let blocking_write fd =
let syscall = Miou.make (Fun.const ()) in
let blocking_read fd = on_read fd (Fun.const ())

let on_write fd fn =
let syscall = Miou.make fn in
let uid = Miou.uid syscall in
let unix = get () in
Hashtbl.add unix.revert uid fd;
Expand All @@ -166,6 +168,8 @@ let blocking_write fd =
set unix;
Miou.suspend syscall

let blocking_write fd = on_write fd (Fun.const ())

let with_lock ~lock fn =
Mutex.lock lock;
Fun.protect ~finally:(fun () -> Mutex.unlock lock) fn
Expand Down Expand Up @@ -374,7 +378,8 @@ let no_events unix =

let[@inline always] keys_of_table tbl =
let res = ref [] in
Table.iter (fun k _ -> res := k :: !res) tbl; !res
Table.iter (fun k _ -> res := k :: !res) tbl;
!res

let select _domain interrupt ~poll (cancelled_syscalls : Miou.uid list) =
let unix = get () in
Expand Down
5 changes: 5 additions & 0 deletions lib/miou_unix.mli
Original file line number Diff line number Diff line change
Expand Up @@ -135,3 +135,8 @@ val bind_and_listen : ?backlog:int -> file_descr -> Unix.sockaddr -> unit
[backlog] is the maximal number of pending requests. *)

val run : ?g:Random.State.t -> ?domains:int -> (unit -> 'a) -> 'a

(**/**)

val on_read : Unix.file_descr -> (unit -> unit) -> unit
val on_write : Unix.file_descr -> (unit -> unit) -> unit

0 comments on commit 2717401

Please sign in to comment.