package picos
Pico scheduler framework
Install
Dune Dependency
Authors
Maintainers
Sources
picos-0.3.0.tbz
sha256=544804c0bde4b29764f82f04e7defed7c06bc43e5a6ce3f7fdc326cb54a7f066
sha512=4c93427e477fb52374a554a8b9c4c92836a9b5899161275d1473269ab526a1f59177209140631ed763a55be375855dea12f076e18bf4124522414986c0e257be
doc/src/picos.stdio/picos_stdio.ml.html
Source file picos_stdio.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 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548
open Picos let nonblock_fds = Picos_htbl.create ~hashed_type:(module Picos_fd.Resource) () module Unix = struct include Unix type file_descr = Picos_fd.t let is_oob flag = MSG_OOB == flag let is_nonblock flag = O_NONBLOCK == flag (* The retry wrappers below are written to avoid closure allocations. *) (* [EAGAIN] (and [EWOULDBLOCK]) indicates that the operation would have blocked and so we await using the [select] thread for the file descriptor. [EINTR] indicates that the operation was interrupted by a signal, which usually shouldn't happen with non-blocking operations. We don't want to block, so we do the same thing as with [EAGAIN]. *) let[@inline] intr_req fd = if Sys.win32 || Picos_htbl.mem nonblock_fds (Picos_fd.unsafe_get fd) then Picos_select.Intr.nothing else Picos_select.Intr.req ~seconds:0.000_01 (* 10μs - TODO *) let rec again_0 fd fn op = let intr = intr_req fd in match fn (Picos_fd.unsafe_get fd) with | result -> Picos_select.Intr.clr intr; result | exception Unix.Unix_error ((EAGAIN | EINTR | EWOULDBLOCK), _, _) -> Picos_select.Intr.clr intr; again_0 (Picos_select.await_on fd op) fn op | exception exn -> Picos_select.Intr.clr intr; raise exn let rec again_cloexec_0 ?cloexec fd fn op = let intr = intr_req fd in match fn ?cloexec (Picos_fd.unsafe_get fd) with | result -> Picos_select.Intr.clr intr; result | exception Unix.Unix_error ((EAGAIN | EINTR | EWOULDBLOCK), _, _) -> Picos_select.Intr.clr intr; again_cloexec_0 ?cloexec (Picos_select.await_on fd op) fn op | exception exn -> Picos_select.Intr.clr intr; raise exn let rec again_3 fd x1 x2 x3 fn op = let intr = intr_req fd in match fn (Picos_fd.unsafe_get fd) x1 x2 x3 with | result -> Picos_select.Intr.clr intr; result | exception Unix.Unix_error ((EAGAIN | EINTR | EWOULDBLOCK), _, _) -> Picos_select.Intr.clr intr; again_3 (Picos_select.await_on fd op) x1 x2 x3 fn op | exception exn -> Picos_select.Intr.clr intr; raise exn let rec again_4 fd x1 x2 x3 x4 fn op = let intr = intr_req fd in match fn (Picos_fd.unsafe_get fd) x1 x2 x3 x4 with | result -> Picos_select.Intr.clr intr; result | exception Unix.Unix_error ((EAGAIN | EINTR | EWOULDBLOCK), _, _) -> Picos_select.Intr.clr intr; again_4 (Picos_select.await_on fd op) x1 x2 x3 x4 fn op | exception exn -> Picos_select.Intr.clr intr; raise exn let rec again_5 fd x1 x2 x3 x4 x5 fn op = let intr = intr_req fd in match fn (Picos_fd.unsafe_get fd) x1 x2 x3 x4 x5 with | result -> Picos_select.Intr.clr intr; result | exception Unix.Unix_error ((EAGAIN | EINTR | EWOULDBLOCK), _, _) -> Picos_select.Intr.clr intr; again_5 (Picos_select.await_on fd op) x1 x2 x3 x4 x5 fn op | exception exn -> Picos_select.Intr.clr intr; raise exn (* [EINPROGRESS] indicates that a socket operation is being performed asynchronously. We await using the [select] thread for the operation to complete and then get the error from the socket. *) let progress_1 fd x1 fn op name = let intr = intr_req fd in match fn (Picos_fd.unsafe_get fd) x1 with | () -> Picos_select.Intr.clr intr | exception Unix.Unix_error ((EAGAIN | EINPROGRESS | EINTR | EWOULDBLOCK), _, _) -> begin (* The documentation of [bind] and [connect] does not mention [EAGAIN] (or [EWOULDBLOCK]), but on Windows we do seem to get those errors from [connect]. The documentation of [bind] does not mention [EINTR]. Matching on that shouldn't cause issues with [bind]. For [connect] both [EINPROGRESS] and [EINTR] mean that connection will be established asynchronously and we use [select] to wait. *) Picos_select.Intr.clr intr; let fd = Picos_select.await_on fd op in match Unix.getsockopt_error (Picos_fd.unsafe_get fd) with | None -> () | Some error -> raise (Unix.Unix_error (error, name, "")) end | exception exn -> Picos_select.Intr.clr intr; raise exn let stdin = Picos_fd.create ~dispose:false Unix.stdin and stdout = Picos_fd.create ~dispose:false Unix.stdout and stderr = Picos_fd.create ~dispose:false Unix.stderr (* https://pubs.opengroup.org/onlinepubs/9699919799/functions/open.html *) let openfile path flags file_perm = let fd = Picos_fd.create (Unix.openfile path flags file_perm) in if List.exists is_nonblock flags then begin let if_not_added_fd_has_been_closed_outside = Picos_htbl.try_add nonblock_fds (Picos_fd.unsafe_get fd) () in assert if_not_added_fd_has_been_closed_outside end; fd (* https://pubs.opengroup.org/onlinepubs/9699919799/functions/close.html *) let close fd = let _ : bool = Picos_htbl.try_remove nonblock_fds (Picos_fd.unsafe_get fd) in Picos_fd.decr ~close:true fd let close_pair (fd1, fd2) = close fd1; close fd2 (* https://pubs.opengroup.org/onlinepubs/9699919799/functions/fsync.html *) let fsync fd = again_0 fd Unix.fsync `W (* https://pubs.opengroup.org/onlinepubs/9699919799/functions/read.html *) let read fd bytes pos len = again_3 fd bytes pos len Unix.read `R (* https://pubs.opengroup.org/onlinepubs/9699919799/functions/write.html *) let write fd bytes pos len = again_3 fd bytes pos len Unix.write `W let single_write fd bytes pos len = again_3 fd bytes pos len Unix.single_write `W let write_substring fd string pos len = again_3 fd string pos len Unix.write_substring `W let single_write_substring fd string pos len = again_3 fd string pos len Unix.single_write_substring `W (* *) (*let in_channel_of_descr _ = failwith "TODO"*) (*let out_channel_of_descr _ = failwith "TODO"*) (*let descr_of_in_channel _ = failwith "TODO"*) (*let descr_of_out_channel _ = failwith "TODO"*) (* *) (* https://pubs.opengroup.org/onlinepubs/9699919799/functions/lseek.html *) let lseek fd amount seek_command = Unix.lseek (Picos_fd.unsafe_get fd) amount seek_command (* https://pubs.opengroup.org/onlinepubs/9699919799/functions/ftruncate.html *) let ftruncate fd size = Unix.ftruncate (Picos_fd.unsafe_get fd) size (* *) (* https://pubs.opengroup.org/onlinepubs/9699919799/functions/fstat.html *) let fstat fd = Unix.fstat (Picos_fd.unsafe_get fd) (* https://pubs.opengroup.org/onlinepubs/9699919799/functions/isatty.html *) let isatty fd = Unix.isatty (Picos_fd.unsafe_get fd) (* *) module LargeFile = struct include Unix.LargeFile (* https://pubs.opengroup.org/onlinepubs/9699919799/functions/lseek.html *) let lseek fd amount seek_command = Unix.LargeFile.lseek (Picos_fd.unsafe_get fd) amount seek_command (* https://pubs.opengroup.org/onlinepubs/9699919799/functions/ftruncate.html *) let ftruncate fd size = Unix.LargeFile.ftruncate (Picos_fd.unsafe_get fd) size (* https://pubs.opengroup.org/onlinepubs/9699919799/functions/fstat.html *) let fstat fd = Unix.LargeFile.fstat (Picos_fd.unsafe_get fd) end (* *) (* https://pubs.opengroup.org/onlinepubs/9699919799/functions/mmap.html This can raise EAGAIN, but it probably should not be handled? *) let map_file fd ?pos kind layout dims = Unix.map_file (Picos_fd.unsafe_get fd) ?pos kind layout shared dims (* *) (* https://pubs.opengroup.org/onlinepubs/9699919799/functions/fchmod.html *) let fchmod fd file_perm = Unix.fchmod (Picos_fd.unsafe_get fd) file_perm (* https://pubs.opengroup.org/onlinepubs/9699919799/functions/fchown.html *) let fchown fd uid gid = Unix.fchown (Picos_fd.unsafe_get fd) uid gid (* *) (* https://pubs.opengroup.org/onlinepubs/9699919799/functions/dup.html *) let dup ?cloexec fd = Picos_fd.create (Unix.dup ?cloexec (Picos_fd.unsafe_get fd)) (* https://pubs.opengroup.org/onlinepubs/9699919799/functions/dup.html *) let dup2 ?cloexec src dst = Unix.dup2 ?cloexec (Picos_fd.unsafe_get src) (Picos_fd.unsafe_get dst) let set_nonblock fd = Unix.set_nonblock (Picos_fd.unsafe_get fd); Picos_htbl.try_add nonblock_fds (Picos_fd.unsafe_get fd) () |> ignore let clear_nonblock fd = Unix.clear_nonblock (Picos_fd.unsafe_get fd); Picos_htbl.try_remove nonblock_fds (Picos_fd.unsafe_get fd) |> ignore (* https://pubs.opengroup.org/onlinepubs/9699919799/functions/fcntl.html *) let set_close_on_exec fd = Unix.set_close_on_exec (Picos_fd.unsafe_get fd) (* https://pubs.opengroup.org/onlinepubs/9699919799/functions/fcntl.html *) let clear_close_on_exec fd = Unix.clear_close_on_exec (Picos_fd.unsafe_get fd) (* *) (* https://pubs.opengroup.org/onlinepubs/9699919799/functions/pipe.html *) let pipe ?cloexec () = let inn, out = Unix.pipe ?cloexec () in (Picos_fd.create inn, Picos_fd.create out) (* *) let create_process prog args stdin stdout stderr = Unix.create_process prog args (Picos_fd.unsafe_get stdin) (Picos_fd.unsafe_get stdout) (Picos_fd.unsafe_get stderr) let create_process_env prog args env stdin stdout stderr = Unix.create_process_env prog args env (Picos_fd.unsafe_get stdin) (Picos_fd.unsafe_get stdout) (Picos_fd.unsafe_get stderr) (*let open_process_in _ = failwith "TODO"*) (*let open_process_out _ = failwith "TODO"*) (*let open_process _ = failwith "TODO"*) (*let open_process_full _ = failwith "TODO"*) (*let open_process_args _ = failwith "TODO"*) (*let open_process_args_in _ = failwith "TODO"*) (*let open_process_args_out _ = failwith "TODO"*) (*let open_process_args_full _ = failwith "TODO"*) (*let process_in_pid _ = failwith "TODO"*) (*let process_out_pid _ = failwith "TODO"*) (*let process_pid _ = failwith "TODO"*) (*let process_full_pid _ = failwith "TODO"*) (*let close_process_in _ = failwith "TODO"*) (*let close_process_out _ = failwith "TODO"*) (*let close_process _ = failwith "TODO"*) (*let close_process_full _ = failwith "TODO"*) (* *) module Wait_flag = struct let nohang_bit = 0b10 let untraced_bit = 0b01 (* Note that this is optimized to the identity function. *) let to_int = function WNOHANG -> 0 | WUNTRACED -> 1 let to_bit flag = nohang_bit - to_int flag let () = assert (to_bit WNOHANG = nohang_bit) let () = assert (to_bit WUNTRACED = untraced_bit) let rec to_bits flags bits = match flags with | [] -> bits | flag :: flags -> to_bits flags (bits lor to_bit flag) let to_bits flags = to_bits flags 0 let to_flags = [| []; [ WUNTRACED ]; [ WNOHANG ]; [ WNOHANG; WUNTRACED ] |] let to_flags bits = Array.unsafe_get to_flags bits end let rec waitpid_unix ~bits ~pid = if bits land Wait_flag.nohang_bit <> 0 then Unix.waitpid (Wait_flag.to_flags bits) pid else let computation = Computation.create () in Picos_select.return_on_sigchld computation (); match Unix.waitpid (Wait_flag.to_flags (bits lor Wait_flag.nohang_bit)) pid with | exception Unix_error (EINTR, _, _) -> waitpid_unix ~bits ~pid | (pid_or_0, _) as result -> if pid_or_0 = 0 then begin Computation.await computation; waitpid_unix ~bits ~pid end else begin Computation.finish computation; result end | exception exn -> Computation.finish computation; raise exn let waitpid_win32 ~bits ~pid = if bits land Wait_flag.nohang_bit <> 0 then Unix.waitpid (Wait_flag.to_flags bits) pid else (* One way to provide a scheduler friendly [waitpid] on Windows would be to use a thread pool to run blocking operations on. PR for a thread pool implemented in Picos would be welcome! *) invalid_arg "currently not supported on Windows without WNOHANG" let waitpid flags pid = let bits = Wait_flag.to_bits flags in if Sys.win32 then if pid <> -1 then waitpid_win32 ~bits ~pid else begin (* This should raise? *) Unix.waitpid flags pid end else waitpid_unix ~bits ~pid let wait () = if not Sys.win32 then waitpid_unix ~bits:0 ~pid:(-1) else begin (* This should raise [Invalid_argument] *) Unix.wait () end (* *) let sh = "/bin/sh" let system cmd = if Sys.win32 then (* One way to provide a scheduler friendly [system] on Windows would be to use a thread pool to run blocking operations on. PR for a thread pool implemented in Picos would be welcome! *) invalid_arg "currently not supported on Windows" else create_process sh [| sh; "-c"; cmd |] stdin stdout stderr |> waitpid [] |> snd (* *) let sleepf seconds = Fiber.sleep ~seconds let sleep seconds = Fiber.sleep ~seconds:(Float.of_int seconds) (* *) exception Done let done_bt = Exn_bt.get_callstack 0 Done let[@alert "-handler"] select rds wrs exs seconds = let overall = Computation.create () in let canceler = Trigger.from_action overall () @@ fun _ overall _ -> Picos_select.cancel_after overall ~seconds:0.0 done_bt in let prepare op fd = let computation = Computation.create () in if Computation.try_attach computation canceler then Picos_select.return_on computation fd op true; computation in let rdcs = List.map (prepare `R) rds in let wrcs = List.map (prepare `W) wrs in let excs = List.map (prepare `E) exs in let finisher = Trigger.from_action rdcs wrcs @@ fun _ rdcs wrcs -> let return_false computation = Computation.return computation false in List.iter return_false rdcs; List.iter return_false wrcs; List.iter return_false excs in if not (Computation.try_attach overall finisher) then Trigger.signal finisher else if 0.0 <= seconds then Picos_select.cancel_after overall ~seconds done_bt; match Computation.await overall with | () -> assert false | exception Done -> let[@tail_mod_cons] rec zip_filter pred xs ys = match (xs, ys) with | x :: xs, y :: ys -> if pred y then x :: zip_filter pred xs ys else zip_filter pred xs ys | _, _ -> [] in ( zip_filter Computation.await rds rdcs, zip_filter Computation.await wrs wrcs, zip_filter Computation.await exs excs ) | exception cancelation_exn -> Computation.cancel overall done_bt; raise cancelation_exn (* *) (* https://pubs.opengroup.org/onlinepubs/9699919799/functions/lockf.html *) let lockf fd lock_command length = Unix.lockf (Picos_fd.unsafe_get fd) lock_command length (* *) (* https://pubs.opengroup.org/onlinepubs/9699919799/functions/socket.html *) let socket ?cloexec socket_domain socket_type protocol = Picos_fd.create (Unix.socket ?cloexec socket_domain socket_type protocol) (* https://pubs.opengroup.org/onlinepubs/9699919799/functions/socketpair.html *) let socketpair ?cloexec socket_domain socket_type mystery = let fst, snd = Unix.socketpair ?cloexec socket_domain socket_type mystery in (Picos_fd.create fst, Picos_fd.create snd) (* https://pubs.opengroup.org/onlinepubs/9699919799/functions/accept.html *) let accept ?cloexec fd = let fd, sockaddr = again_cloexec_0 ?cloexec fd Unix.accept `R in (Picos_fd.create fd, sockaddr) (* https://pubs.opengroup.org/onlinepubs/9699919799/functions/bind.html *) let bind fd sockaddr = progress_1 fd sockaddr Unix.bind `W "bind" (* https://pubs.opengroup.org/onlinepubs/9699919799/functions/connect.html *) let connect fd sockaddr = progress_1 fd sockaddr Unix.connect `W "connect" (* https://pubs.opengroup.org/onlinepubs/9699919799/functions/listen.html *) let listen fd max_pending = Unix.listen (Picos_fd.unsafe_get fd) max_pending (* https://pubs.opengroup.org/onlinepubs/9699919799/functions/shutdown.html *) let shutdown fd shutdown_command = Unix.shutdown (Picos_fd.unsafe_get fd) shutdown_command (* https://pubs.opengroup.org/onlinepubs/9699919799/functions/getsockname.html *) let getsockname fd = Unix.getsockname (Picos_fd.unsafe_get fd) (* https://pubs.opengroup.org/onlinepubs/9699919799/functions/getpeername.html *) let getpeername fd = Unix.getpeername (Picos_fd.unsafe_get fd) (* https://pubs.opengroup.org/onlinepubs/9699919799/functions/recv.html *) let recv fd bytes offset length flags = again_4 fd bytes offset length flags Unix.recv (if List.exists is_oob flags then `E else `R) (* https://pubs.opengroup.org/onlinepubs/9699919799/functions/recvfrom.html *) let recvfrom fd bytes offset length flags = again_4 fd bytes offset length flags Unix.recvfrom (if List.exists is_oob flags then `E else `R) (* https://pubs.opengroup.org/onlinepubs/9699919799/functions/send.html *) let send fd bytes offset length flags = again_4 fd bytes offset length flags Unix.send `W (* https://pubs.opengroup.org/onlinepubs/9699919799/functions/send.html *) let send_substring fd string offset length flags = again_4 fd string offset length flags Unix.send_substring `W (* https://pubs.opengroup.org/onlinepubs/9699919799/functions/sendto.html *) let sendto fd bytes offset length flags sockaddr = again_5 fd bytes offset length flags sockaddr Unix.sendto `W (* https://pubs.opengroup.org/onlinepubs/9699919799/functions/sendto.html *) let sendto_substring fd string offset length flags sockaddr = again_5 fd string offset length flags sockaddr Unix.sendto_substring `W (* *) (* https://pubs.opengroup.org/onlinepubs/9699919799/functions/getsockopt.html *) let getsockopt fd option = Unix.getsockopt (Picos_fd.unsafe_get fd) option (* https://pubs.opengroup.org/onlinepubs/9699919799/functions/setsockopt.html *) let setsockopt fd option bool = Unix.setsockopt (Picos_fd.unsafe_get fd) option bool (* https://pubs.opengroup.org/onlinepubs/9699919799/functions/getsockopt.html *) let getsockopt_int fd option = Unix.getsockopt_int (Picos_fd.unsafe_get fd) option let setsockopt_int fd option int = Unix.setsockopt_int (Picos_fd.unsafe_get fd) option int let getsockopt_optint fd option = Unix.getsockopt_optint (Picos_fd.unsafe_get fd) option let setsockopt_optint fd option optint = Unix.setsockopt_optint (Picos_fd.unsafe_get fd) option optint let getsockopt_float fd option = Unix.getsockopt_float (Picos_fd.unsafe_get fd) option let setsockopt_float fd option float = Unix.setsockopt_float (Picos_fd.unsafe_get fd) option float let getsockopt_error fd = Unix.getsockopt_error (Picos_fd.unsafe_get fd) (* *) (*let open_connection _ = failwith "TODO"*) (*let shutdown_connection _ = failwith "TODO"*) (*let establish_server _ = failwith "TODO"*) (* *) (* https://pubs.opengroup.org/onlinepubs/9699919799/functions/tcgetattr.html *) let tcgetattr fd = Unix.tcgetattr (Picos_fd.unsafe_get fd) (* https://pubs.opengroup.org/onlinepubs/9699919799/functions/tcsetattr.html *) let tcsetattr fd setattr_when terminal_io = Unix.tcsetattr (Picos_fd.unsafe_get fd) setattr_when terminal_io (* https://pubs.opengroup.org/onlinepubs/9699919799/functions/tcsendbreak.html *) let tcsendbreak fd duration = Unix.tcsendbreak (Picos_fd.unsafe_get fd) duration (* https://pubs.opengroup.org/onlinepubs/9699919799/functions/tcdrain.html *) let tcdrain fd = Unix.tcdrain (Picos_fd.unsafe_get fd) (* https://pubs.opengroup.org/onlinepubs/9699919799/functions/tcflush.html *) let tcflush fd flush_queue = Unix.tcflush (Picos_fd.unsafe_get fd) flush_queue (* https://pubs.opengroup.org/onlinepubs/9699919799/functions/tcflow.html *) let tcflow fd flow_action = Unix.tcflow (Picos_fd.unsafe_get fd) flow_action end
sectionYPositions = computeSectionYPositions($el), 10)"
x-init="setTimeout(() => sectionYPositions = computeSectionYPositions($el), 10)"
>