add start_thread_on_some_domain

This commit is contained in:
Simon Cruanes 2023-06-01 20:59:25 -04:00
parent 5840ba0981
commit d7220c75f5
No known key found for this signature in database
GPG key ID: EBFFF6F283F3A2B4
2 changed files with 16 additions and 0 deletions

View file

@ -69,8 +69,19 @@ module D_pool_ = struct
let (lazy arr) = domains_ in let (lazy arr) = domains_ in
assert (i < Array.length arr); assert (i < Array.length arr);
S_queue.push arr.(i).q f S_queue.push arr.(i).q f
let run_on_and_wait (i : int) (f : unit -> 'a) : 'a =
let q = S_queue.create () in
run_on i (fun () ->
let x = f () in
S_queue.push q x);
S_queue.pop q
end end
let start_thread_on_some_domain f x =
let did = Random.int (D_pool_.n_domains ()) in
D_pool_.run_on_and_wait did (fun () -> Thread.create f x)
module Pool = struct module Pool = struct
(* TODO: use a better queue for the tasks *) (* TODO: use a better queue for the tasks *)

View file

@ -31,6 +31,11 @@ module Pool : sig
in one of the threads. *) in one of the threads. *)
end end
val start_thread_on_some_domain : ('a -> unit) -> 'a -> Thread.t
(** Similar to {!Thread.create}, but it picks a background domain at random
to run the thread. This ensures that we don't always pick the same domain
to run all the various threads needed in an application (timers, event loops, etc.) *)
(** Futures *) (** Futures *)
module Fut : sig module Fut : sig
type 'a t type 'a t