package stdune

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

Source file flock.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
type t = Unix.file_descr

let create x = x

external gen_lock : t -> block:bool -> exclusive:bool -> unit = "dune_flock_lock"

type lock =
  | Shared
  | Exclusive

let is_exclusive = function
  | Exclusive -> true
  | Shared -> false
;;

let lock_block t lock =
  match gen_lock t ~block:true ~exclusive:(is_exclusive lock) with
  | () -> Ok ()
  | exception Unix.Unix_error (err, _, _) -> Error err
;;

let lock_non_block t lock =
  match gen_lock t ~block:false ~exclusive:(is_exclusive lock) with
  | () -> Ok `Success
  | exception Unix.Unix_error ((EWOULDBLOCK | EAGAIN | EACCES), _, _) -> Ok `Failure
  | exception Unix.Unix_error (err, _, _) -> Error err
;;

external unlock : t -> unit = "dune_flock_unlock"

let unlock t =
  match unlock t with
  | () -> Ok ()
  | exception Unix.Unix_error (err, _, _) -> Error err
;;

let fd x = x