File: Signal.sml

package info (click to toggle)
polyml 5.6-8
  • links: PTS, VCS
  • area: main
  • in suites: stretch
  • size: 31,892 kB
  • ctags: 34,453
  • sloc: cpp: 44,983; ansic: 24,520; asm: 14,850; sh: 11,730; makefile: 551; exp: 484; python: 253; awk: 91; sed: 9
file content (91 lines) | stat: -rw-r--r-- 3,437 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
(*
    Title:      Signal structure and signature.
    Author:     David Matthews
    Copyright   David Matthews 2000, 2008


    This library is free software; you can redistribute it and/or
    modify it under the terms of the GNU Lesser General Public
    License as published by the Free Software Foundation; either
    version 2.1 of the License, or (at your option) any later version.
    
    This library is distributed in the hope that it will be useful,
    but WITHOUT ANY WARRANTY; without even the implied warranty of
    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
    Lesser General Public License for more details.
    
    You should have received a copy of the GNU Lesser General Public
    License along with this library; if not, write to the Free Software
    Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301  USA
*)
signature SIGNAL =
sig
    datatype sig_handle = SIG_DFL | SIG_IGN | SIG_HANDLE of int->unit
    val signal: int * sig_handle -> sig_handle
end;

structure Signal: SIGNAL =
struct
    datatype sig_handle = SIG_DFL | SIG_IGN | SIG_HANDLE of int->unit
    local
        val doSig = RunCall.run_call2 RuntimeCalls.POLY_SYS_signal_handler
    in
        fun signal(s, cmd) =
        let
            val c =
                case cmd of
                    SIG_DFL => 0
                |   SIG_IGN => 1
                |   SIG_HANDLE f => RunCall.unsafeCast f
        in
            case doSig(0, (s, c)) of
                0 => SIG_DFL
            |   1 => SIG_IGN
            |   f => SIG_HANDLE(RunCall.unsafeCast f)
        end
    end
    
    local
        datatype sigHandle = SigHandle of (int->unit) * int | WeakMarker
        val doSig = RunCall.run_call2 RuntimeCalls.POLY_SYS_signal_handler
        open Thread

        fun sigThread(): unit =
        let
            (* This call to the RTS returns either a pair of a signal
               and a handler or a flag indicating that some wek reference
               has been set to NONE.  These aren't logically related but
               it's convenient to use a single thread for both. *)
            val nextSig: sigHandle = doSig(1, ())

            (* When we get a WeakMarker message we need to broadcast
               on this condition variable. *)
            fun broadCastWeak haveLock () =
            (
                if haveLock then () else Mutex.lock Weak.weakLock;
                ConditionVar.broadcast Weak.weakSignal;
                Mutex.unlock Weak.weakLock
            )
                
        in
            case nextSig of
                SigHandle (handler, signal) => (handler signal handle _ => ())
            |   WeakMarker =>
                    (* If the lock is free we can do the broadcast now but
                       to avoid waiting and being unable to handle any signals
                       we fork off a thread if we can't. *)
                    if Mutex.trylock Weak.weakLock
                    then broadCastWeak true ()
                    else (Thread.fork(broadCastWeak false, []); ());
            sigThread() (* Forever. *)
        end
        
        fun forkThread() =
            (Thread.fork(sigThread, []); ()) handle Thread _ => print "Unable to create signal thread\n"

    in
        (* Run this thread now and also start one each time we start up. *)
        val _ = forkThread()
        val _ = PolyML.onEntry forkThread
    end
end;