]> matita.cs.unibo.it Git - helm.git/blob - components/thread/extThread.ml
tagged 0.5.0-rc1
[helm.git] / components / thread / extThread.ml
1 (*
2  * Copyright (C) 2003-2004:
3  *    Stefano Zacchiroli <zack@cs.unibo.it>
4  *    for the HELM Team http://helm.cs.unibo.it/
5  *
6  *  This file is part of HELM, an Hypertextual, Electronic
7  *  Library of Mathematics, developed at the Computer Science
8  *  Department, University of Bologna, Italy.
9  *
10  *  HELM is free software; you can redistribute it and/or
11  *  modify it under the terms of the GNU General Public License
12  *  as published by the Free Software Foundation; either version 2
13  *  of the License, or (at your option) any later version.
14  *
15  *  HELM is distributed in the hope that it will be useful,
16  *  but WITHOUT ANY WARRANTY; without even the implied warranty of
17  *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
18  *  GNU General Public License for more details.
19  *
20  *  You should have received a copy of the GNU General Public License
21  *  along with HELM; if not, write to the Free Software
22  *  Foundation, Inc., 59 Temple Place - Suite 330, Boston,
23  *  MA  02111-1307, USA.
24  *
25  *  For details, see the HELM World-Wide-Web page,
26  *  http://helm.cs.unibo.it/
27  *)
28
29 (* $Id$ *)
30
31 let debug = true
32 let debug_print s = if debug then prerr_endline (Lazy.force s)
33
34 exception Can_t_kill of Thread.t * string (* thread, reason *)
35 exception Thread_not_found of Thread.t
36
37 module OrderedPid =
38   struct
39     type t = int
40     let compare = Pervasives.compare
41   end
42 module PidSet = Set.Make (OrderedPid)
43
44  (* perform an action inside a critical section controlled by given mutex *)
45 let do_critical mutex =
46   fun action ->
47     try
48       Mutex.lock mutex;
49       let res = Lazy.force action in
50       Mutex.unlock mutex;
51       res 
52     with e -> Mutex.unlock mutex; raise e
53
54 let kill_signal = Sys.sigusr2   (* signal used to kill children *)
55 let chan = Event.new_channel () (* communication channel between threads *)
56 let creation_mutex = Mutex.create ()
57 let dead_threads_walking = ref PidSet.empty
58 let pids: (Thread.t, int) Hashtbl.t = Hashtbl.create 17
59
60   (* given a thread body (i.e. first argument of a Thread.create invocation)
61   return a new thread body which unblock the kill signal and send its pid to
62   parent over "chan" *)
63 let wrap_thread body =
64   fun arg ->
65     ignore (Unix.sigprocmask Unix.SIG_UNBLOCK [ kill_signal ]);
66     Event.sync (Event.send chan (Unix.getpid ()));
67     body arg
68
69 (*
70 (* FAKE IMPLEMENTATION *)
71 let create = Thread.create
72 let kill _ = ()
73 *)
74
75 let create body arg =
76   do_critical creation_mutex (lazy (
77     let thread_t = Thread.create (wrap_thread body) arg in
78     let pid = Event.sync (Event.receive chan) in
79     Hashtbl.add pids thread_t pid;
80     thread_t
81   ))
82
83 let kill thread_t =
84   try
85     let pid =
86       try
87         Hashtbl.find pids thread_t
88       with Not_found -> raise (Thread_not_found thread_t)
89     in
90     dead_threads_walking := PidSet.add pid !dead_threads_walking;
91     Unix.kill pid kill_signal
92   with e -> raise (Can_t_kill (thread_t, Printexc.to_string e))
93
94   (* "kill_signal" handler, check if current process must die, if this is the
95   case exits with Thread.exit *)
96 let _ =
97   ignore (Sys.signal kill_signal (Sys.Signal_handle
98     (fun signal ->
99       let myself = Unix.getpid () in
100       match signal with
101       | sg when (sg = kill_signal) &&
102                 (PidSet.mem myself !dead_threads_walking) ->
103           dead_threads_walking := PidSet.remove myself !dead_threads_walking;
104           debug_print (lazy "AYEEEEH!");
105           Thread.exit ()
106       | _ -> ())))
107
108   (* block kill signal in main process *)
109 let _ = ignore (Unix.sigprocmask Unix.SIG_BLOCK [ kill_signal ])
110