package containers-thread

  1. Overview
  2. Docs

Source file CCLock.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

(* This file is free software, part of containers. See file "license" for more details. *)

(** {1 Utils around Mutex} *)

type 'a t = {
  mutex : Mutex.t;
  mutable content : 'a;
}

type 'a lock = 'a t

let create content = {
  mutex = Mutex.create();
  content;
}

let with_lock l f =
  Mutex.lock l.mutex;
  try
    let x = f l.content in
    Mutex.unlock l.mutex;
    x
  with e ->
    Mutex.unlock l.mutex;
    raise e

(*$R
  let l = create 0 in
  let try_incr l =
    update l (fun x -> Thread.yield(); x+1)
  in
  for i = 1 to 10 do ignore (Thread.create try_incr l) done;
  Thread.delay 0.10 ;
  assert_equal 10 (get l)
*)

let try_with_lock l f =
  if Mutex.try_lock l.mutex
  then
    try
      let x = f l.content in
      Mutex.unlock l.mutex;
      Some x
    with e ->
      Mutex.unlock l.mutex;
      raise e
  else None

module LockRef = struct
  type 'a t = 'a lock
  let get t = t.content
  let set t x = t.content <- x
  let update t f = t.content <- f t.content
end

let with_lock_as_ref l ~f =
  Mutex.lock l.mutex;
  try
    let x = f l in
    Mutex.unlock l.mutex;
    x
  with e ->
    Mutex.unlock l.mutex;
    raise e

(*$R
  let l = create 0 in
  let test_it l =
    with_lock_as_ref l
      ~f:(fun r ->
        (* increment and decrement *)
        for j = 0 to 100 do
          let x = LockRef.get r in
          LockRef.set r (x+10);
          if j mod 5=0 then Thread.yield ();
          let y = LockRef.get r in
          LockRef.set r (y - 10);
        done
      )
  in
  for i = 1 to 100 do ignore (Thread.create test_it l) done;
  Thread.delay 0.10;
  assert_equal 0 (get l)
*)

let mutex l = l.mutex

let update l f =
  with_lock l (fun x -> l.content <- f x)

(*$T
  let l = create 5 in update l (fun x->x+1); get l = 6
*)

let update_map l f =
  with_lock l
    (fun x ->
       let x', y = f x in
       l.content <- x';
       y)

(*$T
  let l = create 5 in update_map l (fun x->x+1, string_of_int x) = "5" && get l = 6
*)

let get l =
  Mutex.lock l.mutex;
  let x = l.content in
  Mutex.unlock l.mutex;
  x

let set l x =
  Mutex.lock l.mutex;
  l.content <- x;
  Mutex.unlock l.mutex

(*$T
  let l = create 0 in set l 4; get l = 4
  let l = create 0 in set l 4; set l 5; get l = 5
*)

let incr l = update l Stdlib.succ

let decr l = update l Stdlib.pred


(*$R
  let l = create 0 in
  let a = Array.init 100 (fun _ -> Thread.create (fun _ -> incr l) ()) in
  Array.iter Thread.join a;
  assert_equal ~printer:CCInt.to_string 100 (get l)
*)

(*$T
  let l = create 0 in incr l ; get l = 1
  let l = create 0 in decr l ; get l = ~-1
*)

let incr_then_get l =
  Mutex.lock l.mutex;
  l.content <- l.content + 1;
  let x = l.content in
  Mutex.unlock l.mutex;
  x

let get_then_incr l =
  Mutex.lock l.mutex;
  let x = l.content in
  l.content <- l.content + 1;
  Mutex.unlock l.mutex;
  x

let decr_then_get l =
  Mutex.lock l.mutex;
  l.content <- l.content - 1;
  let x = l.content in
  Mutex.unlock l.mutex;
  x

let get_then_decr l =
  Mutex.lock l.mutex;
  let x = l.content in
  l.content <- l.content - 1;
  Mutex.unlock l.mutex;
  x

(*$T
  let l = create 0 in 1 = incr_then_get l && 1 = get l
  let l = create 0 in 0 = get_then_incr l && 1 = get l
  let l = create 10 in 9 = decr_then_get l && 9 = get l
  let l = create 10 in 10 = get_then_decr l && 9 = get l
*)

let get_then_set l =
  Mutex.lock l.mutex;
  let x = l.content in
  l.content <- true;
  Mutex.unlock l.mutex;
  x

let get_then_clear l =
  Mutex.lock l.mutex;
  let x = l.content in
  l.content <- false;
  Mutex.unlock l.mutex;
  x

OCaml

Innovation. Community. Security.