package miou
sectionYPositions = computeSectionYPositions($el), 10)"
x-init="setTimeout(() => sectionYPositions = computeSectionYPositions($el), 10)"
>
Composable concurrency primitives for OCaml
Install
dune-project
Dependency
Authors
Maintainers
Sources
miou-0.5.0.tbz
sha256=50aa6aa6767d3fe1875e35c69e4b1d92346a5f3a0349ac065f2efce6378c89c3
sha512=58736c04b4c5f11165fc04b7b1ffe7183852e994a3617cebc7ecaa6e88244b583712c3d04e5f82bc52241317b71c8c3262d188a6c8f398a3f7813c75a8fc52dd
doc/src/miou.unix/miou_unix.ml.html
Source file miou_unix.ml
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478
module Bitv = Miou_bitv module Poll = Miou_poll module Logs = Miou.Logs.Make (struct let src = "unix" end) type file_descr = { fd: Unix.file_descr; non_blocking: bool } let of_file_descr ?(non_blocking = true) fd = if non_blocking then Unix.set_nonblock fd else Unix.clear_nonblock fd; { fd; non_blocking } let to_file_descr { fd; _ } = fd let tcpv4 () = let fd = Unix.socket Unix.PF_INET Unix.SOCK_STREAM 0 in Unix.set_nonblock fd; { fd; non_blocking= true } let tcpv6 () = let fd = Unix.socket Unix.PF_INET6 Unix.SOCK_STREAM 0 in Unix.set_nonblock fd; { fd; non_blocking= true } let bind_and_listen ?(backlog = 64) ?(reuseaddr = true) ?(reuseport = true) { fd; _ } sockaddr = Unix.setsockopt fd Unix.SO_REUSEADDR reuseaddr; Unix.setsockopt fd Unix.SO_REUSEPORT reuseport; Unix.bind fd sockaddr; Unix.listen fd backlog module File_descrs = struct type nonrec 'a t = { mutable contents: (Unix.file_descr * 'a) list } let find tbl fd = List.assq fd tbl.contents let find_opt tbl fd = List.assq_opt fd tbl.contents let remove tbl fd = let contents = List.fold_left (fun acc (k, v) -> if k == fd then acc else (k, v) :: acc) [] tbl.contents in tbl.contents <- contents let replace tbl fd v' = let contents = List.fold_left (fun acc (k, v) -> if k == fd then (k, v') :: acc else (k, v) :: acc) [] tbl.contents in tbl.contents <- contents let add tbl k v = tbl.contents <- (k, v) :: tbl.contents let clear tbl = tbl.contents <- [] let create _ = { contents= [] } end type elt = { time: float; syscall: Miou.syscall; mutable cancelled: bool } module Heapq = Miou.Pqueue.Make (struct type t = elt let dummy = { time= 0.0; syscall= Obj.magic (); cancelled= false } let compare { time= a; _ } { time= b; _ } = Float.compare a b end) type domain = { readers: Miou.syscall list File_descrs.t ; writers: Miou.syscall list File_descrs.t ; sleepers: Heapq.t ; revert: (Miou.uid, Unix.file_descr * int) Hashtbl.t ; poll: Poll.t ; bitv: Bitv.t } let clean domain uids = let clean uid' ((fd : Unix.file_descr), index) tbl = match File_descrs.find tbl fd with | exception Not_found -> () | syscalls -> ( Poll.invalidate_index domain.poll index; Bitv.set domain.bitv index false; match List.filter (fun s -> uid' <> Miou.uid s) syscalls with | [] -> File_descrs.remove tbl fd | syscalls -> File_descrs.replace tbl fd syscalls) in let clean uid = match Hashtbl.find domain.revert uid with | fd -> clean uid fd domain.readers; clean uid fd domain.writers | exception Not_found -> () in List.iter clean uids; List.iter (Hashtbl.remove domain.revert) uids; let clean ({ syscall; _ } as elt) = if List.exists (( = ) (Miou.uid syscall)) uids then elt.cancelled <- true in Heapq.iter clean domain.sleepers let rec drop_heapq heapq = try Heapq.delete_min_exn heapq; drop_heapq heapq with _ -> () let domain = (* NOTE(dinosaure): the semantic of [DLS.new_key] is a bit odd. Here, we call [make ()] when we would like to [DLS.get] our global value. Then, for domains other than [dom0], we use [split_from_parent]. We must _clear_ everything except for the file descriptor used for the interrupt (still used by our [dom0]). We have two cases: - the usual case where the user call [Miou_unix.run]. In that case, we dont't clean anything because everything has just been freshly created. - the case where the user calls [Miou_unix.run] again. Here, we must clean everything from [dom0] and create new values for the new domains via [split_from_parent]. TODO(dinosaure): we probably should clean our [dom0] via our [events.finaliser]... *) let rec split_from_parent v = File_descrs.clear v.readers; File_descrs.clear v.writers; drop_heapq v.sleepers; Hashtbl.clear v.revert; for i = 1 to Bitv.length v.bitv - 1 do Poll.invalidate_index v.poll i; Bitv.set v.bitv i false done; make () and make () = let poll = Poll.create () in Logs.debug (fun m -> m "create a poll on [%d] with %d file-descriptors" (Stdlib.Domain.self () :> int) (Poll.maxfds poll)); { readers= File_descrs.create 0x100 ; writers= File_descrs.create 0x100 ; sleepers= Heapq.create () ; revert= Hashtbl.create 0x100 ; poll ; bitv= Bitv.create (Poll.maxfds poll) false } in let key = Stdlib.Domain.DLS.new_key ~split_from_parent make in fun () -> Stdlib.Domain.DLS.get key let append tbl fd syscall = try let syscalls = File_descrs.find tbl fd in File_descrs.replace tbl fd (syscall :: syscalls) with Not_found -> File_descrs.add tbl fd [ syscall ] let blocking_read fd = let syscall = Miou.syscall () in let uid = Miou.uid syscall in let domain = domain () in let fn () = match Bitv.next domain.bitv with | Some next -> Logs.debug (fun m -> m "poll.set-index on [%d:%d] with POLLIN" (Stdlib.Domain.self () :> int) next); Hashtbl.replace domain.revert uid (fd, next); append domain.readers fd syscall; Poll.set_index domain.poll next fd Poll.Flags.pollin; Bitv.set domain.bitv next true | None -> failwith "Too many open files" in Miou.suspend ~fn syscall let blocking_write fd = let syscall = Miou.syscall () in let uid = Miou.uid syscall in let domain = domain () in let fn () = match Bitv.next domain.bitv with | Some next -> Logs.debug (fun m -> m "poll.set-index on [%d:%d] with POLLIN" (Stdlib.Domain.self () :> int) next); Hashtbl.replace domain.revert uid (fd, next); append domain.writers fd syscall; Poll.set_index domain.poll next fd Poll.Flags.pollout; Bitv.set domain.bitv next true | None -> failwith "Too many open files" in Miou.suspend ~fn syscall let rec unsafe_read ({ fd; non_blocking } as file_descr) off len buf = if non_blocking then match Unix.read fd buf off len with | exception Unix.(Unix_error (EINTR, _, _)) -> unsafe_read file_descr off len buf | exception Unix.(Unix_error ((EAGAIN | EWOULDBLOCK), _, _)) -> blocking_read fd; unsafe_read file_descr off len buf | value -> value else let rec go () = match Unix.read fd buf off len with | exception Unix.(Unix_error (EINTR, _, _)) -> go () | value -> value in blocking_read fd; go () let read file_descr ?(off = 0) ?len buf = let len = match len with None -> Bytes.length buf - off | Some len -> len in if off < 0 || len < 0 || off > Bytes.length buf - len then invalid_arg "Miou_unix.read"; unsafe_read file_descr off len buf let rec really_read_go file_descr off len buf = let len' = unsafe_read file_descr off len buf in if len' == 0 then raise End_of_file else if len - len' > 0 then really_read_go file_descr (off + len') (len - len') buf let really_read file_descr ?(off = 0) ?len buf = let len = match len with None -> Bytes.length buf - off | Some len -> len in if off < 0 || len < 0 || off > Bytes.length buf - len then invalid_arg "Miou_unix.really_read"; if len > 0 then really_read_go file_descr off len buf let rec unsafe_write ({ fd; non_blocking } as file_descr) off len str = if non_blocking then match Unix.write fd (Bytes.unsafe_of_string str) off len with | exception Unix.(Unix_error (EINTR, _, _)) -> unsafe_write file_descr off len str | exception Unix.(Unix_error ((EAGAIN | EWOULDBLOCK), _, _)) -> blocking_write fd; unsafe_write file_descr off len str | len' when len' < len -> unsafe_write file_descr (off + len') (len - len') str | _ -> () else let rec go () = match Unix.write fd (Bytes.unsafe_of_string str) off len with | exception Unix.(Unix_error (EINTR, _, _)) -> go () | len' when len' < len -> unsafe_write file_descr (off + len') (len - len') str | _ -> () in blocking_write fd; go () let write file_descr ?(off = 0) ?len str = let len = match len with None -> String.length str - off | Some len -> len in if off < 0 || len < 0 || off > String.length str - len then invalid_arg "Miou_unix.write"; unsafe_write file_descr off len str let rec accept ?cloexec ({ fd; non_blocking } as file_descr) = if non_blocking then ( match Unix.accept ?cloexec fd with | exception Unix.(Unix_error (EINTR, _, _)) -> accept ?cloexec file_descr | exception Unix.(Unix_error ((EAGAIN | EWOULDBLOCK), _, _)) -> blocking_read fd; accept ?cloexec file_descr | fd, sockaddr -> Unix.set_nonblock fd; let file_descr = { fd; non_blocking= true } in (file_descr, sockaddr)) else let rec go () = match Unix.accept ?cloexec fd with | exception Unix.(Unix_error (EINTR, _, _)) -> go () | fd, sockaddr -> Unix.set_nonblock fd; let file_descr = { fd; non_blocking= true } in (file_descr, sockaddr) in blocking_read fd; go () let rec connect ({ fd; non_blocking } as file_descr) sockaddr = if not non_blocking then invalid_arg "Miou_unix.connect: we expect a file descriptor in the non-blocking mode"; match Unix.connect fd sockaddr with | () -> () | exception Unix.(Unix_error (EINTR, _, _)) -> connect file_descr sockaddr | exception Unix.(Unix_error (EINPROGRESS, _, _)) -> ( blocking_write fd; match Unix.getsockopt_error fd with | None -> () | Some err -> raise (Unix.Unix_error (err, "connect", ""))) let close { fd; _ } = Unix.close fd let sleep until = let syscall = Miou.syscall () in let domain = domain () in let elt = { time= Unix.gettimeofday () +. until; syscall; cancelled= false } in let fn () = Heapq.insert elt domain.sleepers in Miou.suspend ~fn syscall module Ownership = struct type old = file_descr type file_descr = { fd: Unix.file_descr ; non_blocking: bool ; resource: Miou.Ownership.t } let bind_and_listen ?backlog { fd; non_blocking; resource } sockaddr = Miou.Ownership.check resource; bind_and_listen ?backlog { fd; non_blocking } sockaddr let read { fd; non_blocking; resource } ?off ?len buf = Miou.Ownership.check resource; read { fd; non_blocking } ?off ?len buf let really_read { fd; non_blocking; resource } ?off ?len buf = Miou.Ownership.check resource; really_read { fd; non_blocking } ?off ?len buf let write { fd; non_blocking; resource } ?off ?len str = Miou.Ownership.check resource; write { fd; non_blocking } ?off ?len str let accept ?cloexec { fd; non_blocking; resource } = Miou.Ownership.check resource; let ({ fd; non_blocking } : old), sockaddr = accept ?cloexec { fd; non_blocking } in let resource = Miou.Ownership.create ~finally:Unix.close fd in Miou.Ownership.own resource; ({ fd; non_blocking; resource }, sockaddr) let connect { fd; non_blocking; resource } sockaddr = Miou.Ownership.check resource; connect { fd; non_blocking } sockaddr let close { fd; resource; _ } = (* NOTE(dinosaure): a world exists when we would like to cancel the current task which tries to perform [disown]. In that case, the [finally] is called and we should not continue the current task. In that case, [Unix.close] should not be called (because the task is cancelled). So it's "safe" to, first, call [disown] and, if we continue, call [Unix.close]. *) Miou.Ownership.disown resource; Unix.close fd let of_file_descr ?(non_blocking = true) fd = let resource = Miou.Ownership.create ~finally:Unix.close fd in if non_blocking then Unix.set_nonblock fd else Unix.clear_nonblock fd; Miou.Ownership.own resource; { fd; non_blocking; resource } let to_file_descr { fd; _ } = fd let resource { resource; _ } = resource let tcpv4 () = let fd = Unix.socket Unix.PF_INET Unix.SOCK_STREAM 0 in let resource = Miou.Ownership.create ~finally:Unix.close fd in Unix.set_nonblock fd; Miou.Ownership.own resource; { fd; non_blocking= true; resource } let tcpv6 () = let fd = Unix.socket Unix.PF_INET6 Unix.SOCK_STREAM 0 in let resource = Miou.Ownership.create ~finally:Unix.close fd in Unix.set_nonblock fd; Miou.Ownership.own resource; { fd; non_blocking= true; resource } end let rec sleeper domain = match Heapq.find_min_exn domain.sleepers with | exception Heapq.Empty -> None | { cancelled= true; _ } -> Heapq.delete_min_exn domain.sleepers; sleeper domain | { time; _ } -> Some time let in_the_past t = t = 0. || t <= Unix.gettimeofday () let rec collect domain signals = match Heapq.find_min_exn domain.sleepers with | exception Heapq.Empty -> signals | { cancelled= true; _ } -> Heapq.delete_min_exn domain.sleepers; collect domain signals | { time; syscall; _ } when in_the_past time -> Heapq.delete_min_exn domain.sleepers; collect domain (Miou.signal syscall :: signals) | _ -> signals let intr fd = let buf = Bytes.create 0x100 in match Unix.read fd buf 0 (Bytes.length buf) with | _ -> () | exception Unix.(Unix_error (EAGAIN, _, _)) -> () let transmit_and_clean domain syscall = Hashtbl.remove domain.revert (Miou.uid syscall); Miou.signal syscall let select uid ic ~block cancelled_syscalls = let domain = domain () in clean domain cancelled_syscalls; let timeout : Poll.ppoll_timeout = match (sleeper domain, block) with | None, true -> Poll.Infinite | (None | Some _), false -> Poll.No_wait | Some value, true -> let value = value -. Unix.gettimeofday () in let value = Float.max value 0.0 *. 1e9 in Poll.Nanoseconds (Int64.of_float value) in Logs.debug (fun m -> m "[%a] [readers:%dw, writers:%dw, sleepers:%dw, revert:%dw]" Miou.Domain.Uid.pp uid Obj.(reachable_words (repr domain.readers)) Obj.(reachable_words (repr domain.writers)) Obj.(reachable_words (repr domain.sleepers)) Obj.(reachable_words (repr domain.revert))); let nready = Bitv.max domain.bitv in Logs.debug (fun m -> m "[%a] [nready:%d]" Miou.Domain.Uid.pp uid nready); match Poll.ppoll_or_poll domain.poll nready timeout with | exception Unix.(Unix_error (EINTR, _, _)) -> Logs.debug (fun m -> m "[%a] interrupted by the system" Miou.Domain.Uid.pp uid); collect domain [] | nready -> let acc = ref (collect domain []) in let fn index fd flags = if index == 0 then intr ic else if Poll.Flags.mem flags Poll.Flags.pollin then ( Poll.invalidate_index domain.poll index; Bitv.set domain.bitv index false; match File_descrs.find_opt domain.readers fd with | None -> () | Some [] -> File_descrs.remove domain.readers fd | Some syscalls -> File_descrs.remove domain.readers fd; acc := List.rev_append (List.rev_map (transmit_and_clean domain) syscalls) !acc) else if Poll.Flags.mem flags Poll.Flags.pollout then ( Poll.invalidate_index domain.poll index; Bitv.set domain.bitv index false; match File_descrs.find_opt domain.writers fd with | None -> () | Some [] -> File_descrs.remove domain.writers fd | Some syscalls -> File_descrs.remove domain.writers fd; acc := List.rev_append (List.rev_map (transmit_and_clean domain) syscalls) !acc) in Poll.iter domain.poll nready fn; !acc let signal = Bytes.make 1 '\000' let interrupt oc () = match Unix.single_write oc signal 0 1 with | n -> assert (n = 1) (* XXX(dinosaure): paranoid mode. *) | exception Unix.(Unix_error (EAGAIN, _, _)) -> () let events uid = let domain = domain () in let ic, oc = Unix.pipe ~cloexec:true () in Unix.set_nonblock ic; Unix.set_nonblock oc; Bitv.set domain.bitv 0 true; Poll.set_index domain.poll 0 ic Poll.Flags.pollin; let select = select uid ic in let finaliser () = Unix.close ic; Unix.close oc in { Miou.interrupt= interrupt oc; select; finaliser } let run ?g ?domains fn = Miou.run ~events ?g ?domains fn
sectionYPositions = computeSectionYPositions($el), 10)"
x-init="setTimeout(() => sectionYPositions = computeSectionYPositions($el), 10)"
>