Skip to content

Commit ba710d1

Browse files
more refs become atomics
1 parent 4478ace commit ba710d1

File tree

4 files changed

+41
-36
lines changed

4 files changed

+41
-36
lines changed

CHANGES

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,9 @@
1+
===== 6.0.0 =====
2+
3+
* Support multiple scheduler running in parallel in separate domains.
4+
5+
* Exception filter defaults to letting systems exceptions through.
6+
17
===== 5.9.0 =====
28

39
====== Additions ======

src/core/lwt.ml

Lines changed: 5 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -770,11 +770,9 @@ module Exception_filter = struct
770770
| Out_of_memory -> false
771771
| Stack_overflow -> false
772772
| _ -> true
773-
let v =
774-
(* Default value: the legacy behaviour to avoid breaking programs *)
775-
ref handle_all
776-
let set f = v := f
777-
let run e = !v e
773+
let v = Atomic.make handle_all_except_runtime
774+
let set f = Atomic.set v f
775+
let run e = (Atomic.get v) e
778776
end
779777

780778
module Sequence_associated_storage :
@@ -820,11 +818,10 @@ struct
820818
mutable value : 'v option;
821819
}
822820

823-
let next_key_id = ref 0
821+
let next_key_id = Atomic.make 0
824822

825823
let new_key () =
826-
let id = !next_key_id in
827-
next_key_id := id + 1;
824+
let id = Atomic.fetch_and_add next_key_id 1 in
828825
{id = id; value = None}
829826

830827
let current_storage = Domain.DLS.new_key (fun () -> Storage_map.empty)

src/unix/lwt_preemptive.ml

Lines changed: 20 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -17,23 +17,23 @@ open Lwt.Infix
1717
+-----------------------------------------------------------------+ *)
1818

1919
(* Minimum number of preemptive threads: *)
20-
let min_threads : int ref = ref 0
20+
let min_threads : int Atomic.t = Atomic.make 0
2121

2222
(* Maximum number of preemptive threads: *)
23-
let max_threads : int ref = ref 0
23+
let max_threads : int Atomic.t = Atomic.make 0
2424

2525
(* Size of the waiting queue: *)
26-
let max_thread_queued = ref 1000
26+
let max_thread_queued = Atomic.make 1000
2727

2828
let get_max_number_of_threads_queued _ =
29-
!max_thread_queued
29+
Atomic.get max_thread_queued
3030

3131
let set_max_number_of_threads_queued n =
3232
if n < 0 then invalid_arg "Lwt_preemptive.set_max_number_of_threads_queued";
33-
max_thread_queued := n
33+
Atomic.set max_thread_queued n
3434

3535
(* The total number of preemptive threads currently running: *)
36-
let threads_count = ref 0
36+
let threads_count = Atomic.make 0
3737

3838
(* +-----------------------------------------------------------------+
3939
| Preemptive threads management |
@@ -102,14 +102,14 @@ let rec worker_loop worker =
102102
task ();
103103
(* If there is too much threads, exit. This can happen if the user
104104
decreased the maximum: *)
105-
if !threads_count > !max_threads then worker.reuse <- false;
105+
if Atomic.get threads_count > Atomic.get max_threads then worker.reuse <- false;
106106
(* Tell the main thread that work is done: *)
107107
Lwt_unix.send_notification (Domain.self ()) id;
108108
if worker.reuse then worker_loop worker
109109

110110
(* create a new worker: *)
111111
let make_worker () =
112-
incr threads_count;
112+
Atomic.incr threads_count;
113113
let worker = {
114114
task_cell = CELL.make ();
115115
thread = Thread.self ();
@@ -130,7 +130,7 @@ let add_worker worker =
130130
let get_worker () =
131131
if not (Queue.is_empty workers) then
132132
Lwt.return (Queue.take workers)
133-
else if !threads_count < !max_threads then
133+
else if Atomic.get threads_count < Atomic.get max_threads then
134134
Lwt.return (make_worker ())
135135
else
136136
(Lwt.add_task_r [@ocaml.warning "-3"]) waiters
@@ -139,33 +139,33 @@ let get_worker () =
139139
| Initialisation, and dynamic parameters reset |
140140
+-----------------------------------------------------------------+ *)
141141

142-
let get_bounds () = (!min_threads, !max_threads)
142+
let get_bounds () = (Atomic.get min_threads, Atomic.get max_threads)
143143

144144
let set_bounds (min, max) =
145145
if min < 0 || max < min then invalid_arg "Lwt_preemptive.set_bounds";
146-
let diff = min - !threads_count in
147-
min_threads := min;
148-
max_threads := max;
146+
let diff = min - Atomic.get threads_count in
147+
Atomic.set min_threads min;
148+
Atomic.set max_threads max;
149149
(* Launch new workers: *)
150150
for _i = 1 to diff do
151151
add_worker (make_worker ())
152152
done
153153

154-
let initialized = ref false
154+
let initialized = Atomic.make false
155155

156156
let init min max _errlog =
157-
initialized := true;
157+
Atomic.set initialized true;
158158
set_bounds (min, max)
159159

160160
let simple_init () =
161-
if not !initialized then begin
162-
initialized := true;
161+
if not (Atomic.get initialized) then begin
162+
Atomic.set initialized true;
163163
set_bounds (0, 4)
164164
end
165165

166-
let nbthreads () = !threads_count
166+
let nbthreads () = Atomic.get threads_count
167167
let nbthreadsqueued () = Lwt_sequence.fold_l (fun _ x -> x + 1) waiters 0
168-
let nbthreadsbusy () = !threads_count - Queue.length workers
168+
let nbthreadsbusy () = Atomic.get threads_count - Queue.length workers
169169

170170
(* +-----------------------------------------------------------------+
171171
| Detaching |
@@ -199,7 +199,7 @@ let detach f args =
199199
(* Put back the worker to the pool: *)
200200
add_worker worker
201201
else begin
202-
decr threads_count;
202+
Atomic.decr threads_count;
203203
(* Or wait for the thread to terminates, to free its associated
204204
resources: *)
205205
Thread.join worker.thread

src/unix/lwt_unix.cppo.ml

Lines changed: 10 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -21,33 +21,33 @@ type async_method =
2121
| Async_detach
2222
| Async_switch
2323

24-
let default_async_method_var = ref Async_detach
24+
let default_async_method_var = Atomic.make Async_detach
2525

2626
let () =
2727
try
2828
match Sys.getenv "LWT_ASYNC_METHOD" with
2929
| "none" ->
30-
default_async_method_var := Async_none
30+
Atomic.set default_async_method_var Async_none
3131
| "detach" ->
32-
default_async_method_var := Async_detach
32+
Atomic.set default_async_method_var Async_detach
3333
| "switch" ->
34-
default_async_method_var := Async_switch
34+
Atomic.set default_async_method_var Async_switch
3535
| str ->
3636
Printf.eprintf
3737
"%s: invalid lwt async method: '%s', must be 'none', 'detach' or 'switch'\n%!"
3838
(Filename.basename Sys.executable_name) str
3939
with Not_found ->
4040
()
4141

42-
let default_async_method () = !default_async_method_var
43-
let set_default_async_method am = default_async_method_var := am
42+
let default_async_method () = Atomic.get default_async_method_var
43+
let set_default_async_method am = Atomic.set default_async_method_var am
4444

4545
let async_method_key = Lwt.new_key ()
4646

4747
let async_method () =
4848
match Lwt.get async_method_key with
4949
| Some am -> am
50-
| None -> !default_async_method_var
50+
| None -> Atomic.get default_async_method_var
5151

5252
let with_async_none f =
5353
Lwt.with_value async_method_key (Some Async_none) f
@@ -232,7 +232,7 @@ let choose_async_method = function
232232
| None ->
233233
match Lwt.get async_method_key with
234234
| Some am -> am
235-
| None -> !default_async_method_var
235+
| None -> Atomic.get default_async_method_var
236236

237237
external self_result : 'a job -> 'a = "lwt_unix_self_result"
238238
(* returns the result of a job using the [result] field of the C
@@ -2260,6 +2260,7 @@ type signal_handler = {
22602260

22612261
and signal_handler_id = signal_handler option ref
22622262

2263+
(* TODO: what to do about signals? *)
22632264
let signals = ref Signal_map.empty
22642265
let signal_count () =
22652266
Signal_map.fold
@@ -2375,6 +2376,7 @@ let do_wait4 flags pid =
23752376
let wait_children = Lwt_sequence.create ()
23762377
let wait_count () = Lwt_sequence.length wait_children
23772378

2379+
(* TODO: what to do about signals? especially sigchld signal? *)
23782380
let sigchld_handler_installed = ref false
23792381

23802382
let install_sigchld_handler () =

0 commit comments

Comments
 (0)