bugfix in the scheduler

This commit is contained in:
Simon Cruanes 2013-03-19 18:09:56 +01:00
parent 81e7ee2c04
commit 9a66e90a02

View file

@ -66,15 +66,12 @@ module Pool = struct
(* Internal function, which is run by the threads of the pool *) (* Internal function, which is run by the threads of the pool *)
let serve pool limit = let serve pool limit =
(* loop, to get the next job *) (* loop, to get the next job (in locked environment) *)
let rec poll limit = let rec check limit =
Mutex.lock pool.mutex;
Condition.wait pool.condition pool.mutex;
if Queue.is_empty pool.jobs if Queue.is_empty pool.jobs
then begin (* caramba! try again *) then wait limit (* wait for someone to add a job *)
Mutex.unlock pool.mutex;
if not pool.stop then poll limit end
else begin else begin
(* process one job *)
let job = Queue.pop pool.jobs in let job = Queue.pop pool.jobs in
Mutex.unlock pool.mutex; Mutex.unlock pool.mutex;
(* run the job *) (* run the job *)
@ -83,12 +80,20 @@ module Pool = struct
with _ -> with _ ->
()); ());
match limit with match limit with
| None -> if not pool.stop then poll limit (* I am immortal! *) | None -> if not pool.stop then enter limit (* I am immortal! *)
| Some 0 -> () (* stop, reached limit *) | Some 0 -> () (* stop, reached limit *)
| Some n -> if not pool.stop then poll (Some (n-1)) (* continue serving *) | Some n -> if not pool.stop then enter (Some (n-1)) (* continue serving *)
end end
(* enter the loop *)
and enter limit =
Mutex.lock pool.mutex;
check limit
(* wait for someone to push an item on the queue *)
and wait limit =
Condition.wait pool.condition pool.mutex;
check limit
in in
poll limit enter limit
(** Add a thread to the pool, that will serve at most [limit] jobs *) (** Add a thread to the pool, that will serve at most [limit] jobs *)
let add_thread ?limit pool = let add_thread ?limit pool =