Skip to content

Commit b910b92

Browse files
committed
Upgrade to thread-local-storage>=0.2
1 parent 82c7fe9 commit b910b92

File tree

8 files changed

+71
-41
lines changed

8 files changed

+71
-41
lines changed

bench/bench_tls.ml

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
open Multicore_bench
22

3-
let key = Picos_thread.TLS.new_key (fun () -> -1)
3+
let key = Picos_thread.TLS.create ()
44

55
let run_one ~budgetf ~n_domains ~op () =
66
let n_ops =
@@ -9,7 +9,10 @@ let run_one ~budgetf ~n_domains ~op () =
99

1010
let n_ops_todo = Countdown.create ~n_domains () in
1111

12-
let init _ = Countdown.non_atomic_set n_ops_todo n_ops in
12+
let init _ =
13+
Picos_thread.TLS.set key (-1);
14+
Countdown.non_atomic_set n_ops_todo n_ops
15+
in
1316
let work domain_index () =
1417
match op with
1518
| `Get ->
@@ -18,7 +21,7 @@ let run_one ~budgetf ~n_domains ~op () =
1821
if n <> 0 then
1922
let rec loop n =
2023
if 0 < n then begin
21-
let d = Picos_thread.TLS.get key in
24+
let d = Picos_thread.TLS.get_exn key in
2225
loop (n + d)
2326
end
2427
else work ()

dune-project

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,7 @@
2929
(>= 0.1.0))
3030
;; For OCaml 4 compatible defaults that are not used on OCaml 5 by default
3131
(thread-local-storage
32-
(>= 0.1))
32+
(>= 0.2))
3333
(mtime
3434
(>= 2.0.0))
3535
(psq

lib/picos/ocaml4/picos_ocaml.ml

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -14,11 +14,14 @@ module Handler = struct
1414
and await _ = error in
1515
E { context = (); handler = { current; spawn; yield; cancel_after; await } }
1616

17-
let key = Picos_thread.TLS.new_key @@ fun () -> default
18-
let get () = Picos_thread.TLS.get key
17+
let key = Picos_thread.TLS.create ()
18+
let get () = Picos_thread.TLS.get_exn key
1919

2020
let using handler context main =
21-
let old = Picos_thread.TLS.get key in
21+
let old =
22+
try Picos_thread.TLS.get_exn key
23+
with Picos_thread.TLS.Not_set -> default
24+
in
2225
Picos_thread.TLS.set key (E { context; handler });
2326
match main (handler.current context) with
2427
| value ->

lib/picos_randos/picos_randos.ml

Lines changed: 19 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -53,7 +53,16 @@ type t = {
5353
mutable run : bool;
5454
}
5555

56-
let fiber_key = Picos_thread.TLS.new_key @@ fun () -> ref Fiber.Maybe.nothing
56+
let fiber_key : Fiber.Maybe.t ref Picos_thread.TLS.t =
57+
Picos_thread.TLS.create ()
58+
59+
let get () =
60+
match Picos_thread.TLS.get_exn fiber_key with
61+
| p -> p
62+
| exception Picos_thread.TLS.Not_set ->
63+
let p = ref Fiber.Maybe.nothing in
64+
Picos_thread.TLS.set fiber_key p;
65+
p
5766

5867
let rec next p t =
5968
match Collection.pop_exn t.ready with
@@ -158,21 +167,21 @@ let context ?fatal_exn_handler () =
158167
and current =
159168
Some
160169
(fun k ->
161-
let p = Picos_thread.TLS.get fiber_key in
170+
let p = Picos_thread.TLS.get_exn fiber_key in
162171
let fiber = Fiber.Maybe.to_fiber !p in
163172
Collection.push t.ready (Current (fiber, k));
164173
next p t)
165174
and yield =
166175
Some
167176
(fun k ->
168-
let p = Picos_thread.TLS.get fiber_key in
177+
let p = Picos_thread.TLS.get_exn fiber_key in
169178
let fiber = Fiber.Maybe.to_fiber !p in
170179
Collection.push t.ready (Continue (fiber, k));
171180
next p t)
172181
and return =
173182
Some
174183
(fun k ->
175-
let p = Picos_thread.TLS.get fiber_key in
184+
let p = Picos_thread.TLS.get_exn fiber_key in
176185
let fiber = Fiber.Maybe.to_fiber !p in
177186
Collection.push t.ready (Return (fiber, k));
178187
next p t)
@@ -182,7 +191,7 @@ let context ?fatal_exn_handler () =
182191
function
183192
| Fiber.Current -> t.current
184193
| Fiber.Spawn r ->
185-
let p = Picos_thread.TLS.get fiber_key in
194+
let p = Picos_thread.TLS.get_exn fiber_key in
186195
let fiber = Fiber.Maybe.to_fiber !p in
187196
if Fiber.is_canceled fiber then t.yield
188197
else begin
@@ -193,7 +202,7 @@ let context ?fatal_exn_handler () =
193202
end
194203
| Fiber.Yield -> t.yield
195204
| Computation.Cancel_after r -> begin
196-
let p = Picos_thread.TLS.get fiber_key in
205+
let p = Picos_thread.TLS.get_exn fiber_key in
197206
let fiber = Fiber.Maybe.to_fiber !p in
198207
if Fiber.is_canceled fiber then t.yield
199208
else
@@ -211,7 +220,7 @@ let context ?fatal_exn_handler () =
211220
| Trigger.Await trigger ->
212221
Some
213222
(fun k ->
214-
let p = Picos_thread.TLS.get fiber_key in
223+
let p = Picos_thread.TLS.get_exn fiber_key in
215224
let fiber = Fiber.Maybe.to_fiber !p in
216225
if Fiber.try_suspend fiber trigger fiber k t.resume then next p t
217226
else begin
@@ -221,14 +230,14 @@ let context ?fatal_exn_handler () =
221230
| _ -> None
222231
and retc () =
223232
Atomic.decr t.num_alive_fibers;
224-
let p = Picos_thread.TLS.get fiber_key in
233+
let p = Picos_thread.TLS.get_exn fiber_key in
225234
next p t
226235
in
227236
t
228237

229238
let runner_on_this_thread t =
230239
Select.check_configured ();
231-
next (Picos_thread.TLS.get fiber_key) t
240+
next (get ()) t
232241

233242
let rec await t =
234243
if !(t.num_waiters_non_zero) then begin
@@ -257,7 +266,7 @@ let run_fiber ?context:t_opt fiber main =
257266
t.run <- true;
258267
Mutex.unlock t.mutex;
259268
Collection.push t.ready (Spawn (fiber, main));
260-
next (Picos_thread.TLS.get fiber_key) t;
269+
next (get ()) t;
261270
Mutex.lock t.mutex;
262271
await t
263272
end

lib/picos_select/picos_select.ml

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -101,9 +101,7 @@ let cleared =
101101
Computation.return computation Cleared;
102102
computation
103103

104-
let intr_key =
105-
Picos_thread.TLS.new_key @@ fun () : [ `Req ] tdt ->
106-
invalid_arg "has not been configured"
104+
let intr_key : [ `Req ] tdt Picos_thread.TLS.t = Picos_thread.TLS.create ()
107105

108106
let key =
109107
Picos_domain.DLS.new_key @@ fun () ->
@@ -289,7 +287,7 @@ let handle_signal signal =
289287
Computation.return r.computation r.value
290288
end
291289
else if signal = config.intr_sig then
292-
let (Req r) = Picos_thread.TLS.get intr_key in
290+
let (Req r) = Picos_thread.TLS.get_exn intr_key in
293291
Computation.return r.computation Signaled
294292

295293
let reconfigure_signal_handlers () =

lib/picos_thread/picos_thread.mli

Lines changed: 24 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -12,21 +12,28 @@ module TLS : sig
1212
[threads.posix] library, is not available, this will use
1313
{!Picos_domain.DLS}. *)
1414

15-
type 'a key
16-
(** Represents a key for storing values of type ['a] in storage associated
17-
with threads. *)
18-
19-
val new_key : (unit -> 'a) -> 'a key
20-
(** [new_key compute] allocates a new key for associating values in storage
21-
associated with threads. The initial value for each thread is [compute]d
22-
by calling the given function if the [key] is {{!get}read} before it has
23-
been {{!set}written}. *)
24-
25-
val get : 'a key -> 'a
26-
(** [get key] returns the value associated with the [key] in the storage
27-
associated with the current thread. *)
28-
29-
val set : 'a key -> 'a -> unit
30-
(** [set key value] sets the [value] associated with the [key] in the storage
31-
associated with the current thread. *)
15+
type 'a t
16+
(** Represents a key for associating values with threads. *)
17+
18+
val create : unit -> 'a t
19+
(** [create ()] allocates a new key for associating values with threads.
20+
21+
⚠️ Keys should not be created dynamically as each key will potentially
22+
increase the space taken by every thread. *)
23+
24+
exception Not_set
25+
(** Exception raised by {!get_exn} when no value is associated with the
26+
specified key for the current thread. *)
27+
28+
val get_exn : 'a t -> 'a
29+
(** [get_exn key] returns the value associated with the specified key for the
30+
current thread or raises {!Not_set} in case no value has been {!set} for
31+
the key.
32+
33+
⚠️ The {!Not_set} exception is raised with no backtrace. Always catch the
34+
exception unless it is known that a value has been set. *)
35+
36+
val set : 'a t -> 'a -> unit
37+
(** [set key value] associates the value with the specified key for the
38+
current thread. *)
3239
end
Lines changed: 12 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,13 @@
1-
let is_main_thread = Picos_domain.is_main_domain
1+
open Picos_domain
22

3-
module TLS = Picos_domain.DLS
3+
let is_main_thread = is_main_domain
4+
5+
module TLS = struct
6+
type 'a t = 'a DLS.key
7+
8+
exception Not_set
9+
10+
let create () = DLS.new_key @@ fun () -> raise_notrace Not_set
11+
let get_exn = DLS.get
12+
let set = DLS.set
13+
end

picos.opam

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@ bug-reports: "https://github.com/ocaml-multicore/picos/issues"
1111
depends: [
1212
"dune" {>= "3.14"}
1313
"backoff" {>= "0.1.0"}
14-
"thread-local-storage" {>= "0.1"}
14+
"thread-local-storage" {>= "0.2"}
1515
"mtime" {>= "2.0.0"}
1616
"psq" {>= "0.2.1"}
1717
"multicore-magic" {>= "2.3.0"}

0 commit comments

Comments
 (0)