package picos_io

  1. Overview
  2. Docs
Asynchronous IO system for Picos

Install

dune-project
 Dependency

Authors

Maintainers

Sources

picos-0.5.0.tbz
sha256=862d61383e2df93a876bedcffb1fd1ddc0f96c50b0e9c07943a2aee1f0e182be
sha512=87805379017ef4a7f2c11b954625a3757a0f1431bb9ba59132202de278b3e41adbe0cdc20e3ab23b7c9a8c5a15faeb7ec79348e7d80f2b14274b00df0893b8c0

doc/picos_io/Picos_io/index.html

Module Picos_ioSource

Basic IO facilities based on OCaml standard libraries for Picos.

Modules

Sourcemodule Unix : sig ... end

A transparently asynchronous replacement for a subset of the Unix module that comes with OCaml.

Examples

First we open some modules for convenience:

  open Picos_io
  open Picos_std_finally
  open Picos_std_structured

A pair of pipes

Here is a simple example of two fibers communicating through a pair of pipes:

  # Picos_mux_random.run_on ~n_domains:2 @@ fun () ->

    let@ msg_i, msg_o =
      finally Unix.close_pair @@ fun () ->
      Unix.socketpair ~cloexec:true
        PF_UNIX SOCK_STREAM 0
    in
    let@ syn_i, syn_o =
      finally Unix.close_pair @@ fun () ->
      Unix.socketpair ~cloexec:true
        PF_UNIX SOCK_STREAM 0
    in

    Unix.set_nonblock msg_i;
    Unix.set_nonblock msg_o;
    Unix.set_nonblock syn_i;
    Unix.set_nonblock syn_o;

    Flock.join_after ~on_return:`Terminate begin fun () ->
      Flock.fork begin fun () ->
        let bytes = Bytes.create 100 in
        while true do
          let n =
            Unix.read msg_i bytes 0 100
          in
          if n > 0 then begin
            Printf.printf "%s\n%!"
              (Bytes.sub_string bytes 0 n);
            let w =
              Unix.write_substring
                syn_o "!" 0 1
            in
            assert (w = 1)
          end
        done
      end;

      let send_string s =
        let n = String.length s in
        let w =
          Unix.write_substring msg_o s 0 n
        in
        assert (w = n);
        let r =
          Unix.read syn_i
            (Bytes.create 1) 0 1
        in
        assert (r = 1)
      in

      send_string "Hello, world!";
      send_string "POSIX with OCaml";
    end
  Hello, world!
  POSIX with OCaml
  - : unit = ()