mirror of
https://github.com/c-cube/moonpool.git
synced 2025-12-06 03:05:30 -05:00
Merge pull request #36 from c-cube/simon/fix-35
fix domain pool: block signals in background threads
This commit is contained in:
commit
58a0f891f7
3 changed files with 38 additions and 33 deletions
|
|
@ -144,22 +144,7 @@ module Fine_grained (Args : FINE_GRAINED_ARGS) () = struct
|
|||
if !state <> New then invalid_arg "worker_loop.setup: not a new instance";
|
||||
state := Ready;
|
||||
|
||||
if block_signals then (
|
||||
try
|
||||
ignore
|
||||
(Unix.sigprocmask SIG_BLOCK
|
||||
[
|
||||
Sys.sigterm;
|
||||
Sys.sigpipe;
|
||||
Sys.sigint;
|
||||
Sys.sigchld;
|
||||
Sys.sigalrm;
|
||||
Sys.sigusr1;
|
||||
Sys.sigusr2;
|
||||
]
|
||||
: _ list)
|
||||
with _ -> ()
|
||||
);
|
||||
if block_signals then Signals_.ignore_signals_ ();
|
||||
|
||||
TLS.set Runner.For_runner_implementors.k_cur_runner runner;
|
||||
|
||||
|
|
|
|||
|
|
@ -15,19 +15,23 @@ module Bb_queue = struct
|
|||
if was_empty then Condition.broadcast self.cond;
|
||||
Mutex.unlock self.mutex
|
||||
|
||||
let pop (self : 'a t) : 'a =
|
||||
let pop (type a) (self : a t) : a =
|
||||
let module M = struct
|
||||
exception Found of a
|
||||
end in
|
||||
try
|
||||
Mutex.lock self.mutex;
|
||||
let rec loop () =
|
||||
if Queue.is_empty self.q then (
|
||||
Condition.wait self.cond self.mutex;
|
||||
(loop [@tailcall]) ()
|
||||
) else (
|
||||
while true do
|
||||
if Queue.is_empty self.q then
|
||||
Condition.wait self.cond self.mutex
|
||||
else (
|
||||
let x = Queue.pop self.q in
|
||||
Mutex.unlock self.mutex;
|
||||
x
|
||||
raise (M.Found x)
|
||||
)
|
||||
in
|
||||
loop ()
|
||||
done;
|
||||
assert false
|
||||
with M.Found x -> x
|
||||
end
|
||||
|
||||
module Lock = struct
|
||||
|
|
@ -38,13 +42,13 @@ module Lock = struct
|
|||
|
||||
let create content : _ t = { mutex = Mutex.create (); content }
|
||||
|
||||
let with_ (self : _ t) f =
|
||||
let[@inline never] with_ (self : _ t) f =
|
||||
Mutex.lock self.mutex;
|
||||
try
|
||||
let x = f self.content in
|
||||
match f self.content with
|
||||
| x ->
|
||||
Mutex.unlock self.mutex;
|
||||
x
|
||||
with e ->
|
||||
| exception e ->
|
||||
Mutex.unlock self.mutex;
|
||||
raise e
|
||||
|
||||
|
|
@ -95,6 +99,7 @@ let domains_ : (worker_state option * Domain_.t option) Lock.t array =
|
|||
a [Pool.with_] or [Pool.create() … Pool.shutdown()] in a tight loop), and
|
||||
if nothing happens it tries to stop to free resources. *)
|
||||
let work_ idx (st : worker_state) : unit =
|
||||
Signals_.ignore_signals_ ();
|
||||
let main_loop () =
|
||||
let continue = ref true in
|
||||
while !continue do
|
||||
|
|
|
|||
15
src/private/signals_.ml
Normal file
15
src/private/signals_.ml
Normal file
|
|
@ -0,0 +1,15 @@
|
|||
let ignore_signals_ () =
|
||||
try
|
||||
Thread.sigmask SIG_BLOCK
|
||||
[
|
||||
Sys.sigpipe;
|
||||
Sys.sigbus;
|
||||
Sys.sigterm;
|
||||
Sys.sigchld;
|
||||
Sys.sigalrm;
|
||||
Sys.sigint;
|
||||
Sys.sigusr1;
|
||||
Sys.sigusr2;
|
||||
]
|
||||
|> ignore
|
||||
with _ -> ()
|
||||
Loading…
Add table
Reference in a new issue