@@ -58,29 +58,32 @@ let[@inline] get_per_thread () : per_thread =
5858 | Nothing as any -> not_worker any
5959 | Per_thread _ as pt -> pt
6060
61- let get_thread t i : per_thread =
62- match Array. unsafe_get t.threads i with
63- | Nothing as any -> not_worker any
64- | Per_thread _ as pt -> pt
65-
6661let any_fibers_alive t =
6762 (* We read the number of stopped fibers first. *)
6863 let stopped = ref 0 in
69- for i = 0 to t.threads_num - 1 do
70- match Array. unsafe_get t.threads i with
71- | Nothing -> ()
72- | Per_thread p -> stopped := ! stopped + p.num_stopped
73- done ;
64+ begin
65+ let threads_num = t.threads_num in
66+ let threads = t.threads in
67+ for i = 0 to threads_num - 1 do
68+ match Array. unsafe_get threads i with
69+ | Nothing -> ()
70+ | Per_thread p -> stopped := ! stopped + p.num_stopped
71+ done
72+ end ;
7473 (* Then we read the number of started fibers.
7574
7675 The [Atomic.get] below provides a fence that ensures we really read the
7776 number of started fibers after reading the number of stopped fibers. *)
7877 let started = ref (Atomic. get t.num_started) in
79- for i = 0 to t.threads_num - 1 do
80- match Array. unsafe_get t.threads i with
81- | Nothing -> ()
82- | Per_thread p -> started := ! started + p.num_started
83- done ;
78+ begin
79+ let threads_num = t.threads_num in
80+ let threads = t.threads in
81+ for i = 0 to threads_num - 1 do
82+ match Array. unsafe_get threads i with
83+ | Nothing -> ()
84+ | Per_thread p -> started := ! started + p.num_started
85+ done
86+ end ;
8487 (* Is the difference positive? *)
8588 0 < ! started - ! stopped
8689
@@ -129,10 +132,13 @@ let rec next (Per_thread p as pt : per_thread) =
129132 else begin
130133 let t = p.context in
131134 if 1 < t.threads_num then begin
132- let ( Per_thread v) = get_thread t ( Random. int t.threads_num) in
135+ let i = Random. int t.threads_num in
133136 p.countdown_to_steal < - Mpmcq. length p.ready + 1 ;
134- if p.countdown_to_steal < = Mpmcq. length v.ready then v.ready
135- else p.ready
137+ match Array. unsafe_get t.threads i with
138+ | Nothing -> p.ready
139+ | Per_thread v ->
140+ if p.countdown_to_steal < = Mpmcq. length v.ready then v.ready
141+ else p.ready
136142 end
137143 else begin
138144 p.countdown_to_steal < - 1_000 ;
@@ -152,12 +158,15 @@ let rec next (Per_thread p as pt : per_thread) =
152158
153159and try_steal (Per_thread p as pt : per_thread ) t i =
154160 if p.index <> i then begin
155- let (Per_thread other_p) = get_thread t i in
156- match Mpmcq. pop_exn other_p.ready with
157- | ready ->
158- relaxed_wakeup t ~known_not_empty: false other_p.ready;
159- exec ready pt t
160- | exception Mpmcq. Empty -> try_steal pt t (next_index t i)
161+ match Array. unsafe_get t.threads i with
162+ | Nothing -> try_steal pt t (next_index t i)
163+ | Per_thread other_p -> begin
164+ match Mpmcq. pop_exn other_p.ready with
165+ | ready ->
166+ relaxed_wakeup t ~known_not_empty: false other_p.ready;
167+ exec ready pt t
168+ | exception Mpmcq. Empty -> try_steal pt t (next_index t i)
169+ end
161170 end
162171 else wait pt t
163172
0 commit comments