]> rtime.felk.cvut.cz Git - l4.git/blob - l4/pkg/ocaml/ocaml/contrib/otherlibs/threads/condition.ml
Update
[l4.git] / l4 / pkg / ocaml / ocaml / contrib / otherlibs / threads / condition.ml
1 (***********************************************************************)
2 (*                                                                     *)
3 (*                           Objective Caml                            *)
4 (*                                                                     *)
5 (*         Xavier Leroy and Damien Doligez, INRIA Rocquencourt         *)
6 (*                                                                     *)
7 (*  Copyright 1996 Institut National de Recherche en Informatique et   *)
8 (*  en Automatique.  All rights reserved.  This file is distributed    *)
9 (*  under the terms of the GNU Library General Public License, with    *)
10 (*  the special exception on linking described in file ../../LICENSE.  *)
11 (*                                                                     *)
12 (***********************************************************************)
13
14 (* $Id: condition.ml 4144 2001-12-07 13:41:02Z xleroy $ *)
15
16 type t = { mutable waiting: Thread.t list }
17
18 let create () = { waiting = [] }
19
20 let wait cond mut =
21   Thread.critical_section := true;
22   Mutex.unlock mut;
23   cond.waiting <- Thread.self() :: cond.waiting;
24   Thread.sleep();
25   Mutex.lock mut
26
27 let signal cond =
28   match cond.waiting with               (* atomic *)
29     [] -> ()
30   | th :: rem -> cond.waiting <- rem (* atomic *); Thread.wakeup th
31
32 let broadcast cond =
33   let w = cond.waiting in                  (* atomic *)
34   cond.waiting <- [];                      (* atomic *)
35   List.iter Thread.wakeup w
36