package riot

  1. Overview
  2. Docs
Legend:
Page
Library
Module
Module type
Parameter
Class
Class type
Source

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 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 (Already_closed (op_name ^ ": fd already closed"))
OCaml

Innovation. Community. Security.