package riot

  1. Overview
  2. Docs
An actor-model multi-core scheduler for OCaml 5

Install

Dune Dependency

Authors

Maintainers

Sources

riot-0.0.3.tbz
sha256=6201ce27997ec1c4b4509782c6be2fa2bf102b804b11dcbf9ebdb49a123c19c3
sha512=ad70a67601a892700e461efe57484d109b1d08e30d15464ad8611e71dd568c934d3f948afd645e096e4f97ad1935aaeaf5d9b6d9d59c52a82eeb5c4995421646

doc/src/riot.util/fd.ml.html

Source file fd.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
module Mode = struct
  type t = [ `r | `rw | `w ]

  let equal a b =
    match (a, b) with
    | `r, `r -> true
    | `w, `w -> true
    | `rw, `rw -> true
    | _ -> false

  let pp fmt t =
    let mode = match t with `r -> "r" | `w -> "w" | `rw -> "rw" in
    Format.fprintf fmt "%s" mode
end

type fd = Unix.file_descr
type state = [ `Open of fd | `Closed ]
type t = { state : state Atomic.t; fd : fd }

exception Fd_already_closed of string

let is_closed t = Atomic.get t.state = `Closed
let is_open t = not (is_closed t)

let get t =
  match Atomic.get t.state with `Open sock -> Some sock | `Closed -> None

let equal a b =
  match (get a, get b) with
  | Some fd1, Some fd2 -> Int.equal (Obj.magic fd1) (Obj.magic fd2)
  | _ -> false

let to_int t =
  match Atomic.get t.state with
  | `Open fd -> Obj.magic fd
  | `Closed -> Obj.magic t.fd

let pp fmt t =
  if is_closed t then Format.fprintf fmt "Fd.closed(%d)" (to_int t)
  else Format.fprintf fmt "Fd(%d)" (to_int t)

let rec close t =
  match Atomic.get t.state with
  | `Closed -> ()
  | `Open fd as prev ->
      let next = `Closed in
      if Atomic.compare_and_set t.state prev next then Unix.close fd
      else close t

let make fd = { state = Atomic.make (`Open fd); fd }

let use ~op_name t fn =
  match get t with
  | Some sock -> fn sock
  | None -> raise (Fd_already_closed (op_name ^ ": fd already closed"))
OCaml

Innovation. Community. Security.