Landin library reference discuss source

hosted/events/windows_signals.ldn

1--  The event provider's `signal.signals` on Windows, which has no signals:
2--  its console's control events stand for the three kinds a console
3--  program is asked to stop by.  Ctrl+C and Ctrl+Break are `interrupt`,
4--  the console's closing `hangup`, as a terminal's hanging up is, and the
5--  user's logging off and the system's shutting down `terminate`.  The
6--  event's code is the `number` `next_signal` gives: 0, 1, 2, 5 or 6.
7--  `child`, `user1` and `user2` name nothing Windows sends, and watching
8--  one fails `io_failed` with `ERROR_NOT_SUPPORTED`.
9--
10--  Watching the first kind adds a control handler to the process, which
11--  the system calls on a thread of its own: for a watched kind it puts a
12--  packet on the loop's port and says the event is handled, and for any
13--  other it hands the event on, to the system's default, which ends the
14--  process.  After the handler returns for a closing, a logging off or a
15--  shutdown the system ends the process anyway, so for those the handler
16--  does not return: the program has until the system's limit, five
17--  seconds for a closing, to read the event and end.  `stop_watching`
18--  removes the handler.  A child inherits no handler.  The handler is the
19--  process's, so the port it posts to is the last that began watching.
20
21import core/atomic
22import core/io
23import core/io/signal
24import platform/windows
25
26fixed if compiler.os == windows then
27    --  The port the control handler posts to, and the kinds it takes, as
28    --  bits by `signal.kind`'s index; the handler reads both on its own
29    --  thread.
30    mut control_port: atomic.cell(u64) = zeroed
31    mut control_kinds: atomic.cell(u32) = zeroed
32
33    --  The index of the kind the control event `event` stands for, `kinds`
34    --  for one that stands for none.
35    kind_of_event: (event: u32) -> (index: usize) =
36        index = if event == windows.control_c
37          or event == windows.control_break then 0
38                elsif event == windows.control_logoff
39                  or event == windows.control_shutdown then 1
40                elsif event == windows.control_close then 2
41                else kinds end if
42    end kind_of_event
43
44    --  The control handler, on the system's thread.
45    extern(c) control_arrived: (event: u32) -> (handled: i32) =
46        handled = 0
47        index: usize = kind_of_event(event)
48        return when index >= kinds
49        bits: u32 = atomic.load(item: u32, at: addr control_kinds)
50        return when (bits & (u32(1) << u32(index))) == 0
51        port: u64 = atomic.load(item: u64, at: addr control_port)
52        return when windows.post_completion(usize(port), event, control_key,
53                                            0) == 0
54        handled = 1
55        if event == windows.control_close or event == windows.control_logoff
56          or event == windows.control_shutdown then
57            windows.sleep(windows.infinite)
58        end if
59    end control_arrived
60
61    --  Keep the control event `event`, unless one of its kind waits to be
62    --  read already or it stands for no kind.
63    arrive: (state: ptr mut loop_state, event: u32) -> none =
64        index: usize = kind_of_event(event)
65        return when index >= kinds
66        for at in usize(0)..<state.val.queued do
67            return when kind_of_event(u32(state.val.arrived[at])) == index
68        end for
69        state.val.arrived[state.val.queued] = u8(event)
70        inc state.val.queued
71    end arrive
72
73    events_watch: (self: host, which: signal.kind) -> none ! io.io_failed =
74        cleared(self)
75        state: ptr mut loop_state = self.state
76        index: usize = slot(which)
77        if index > 2 then
78            record(self, windows.error_not_supported)
79            fail io.io_failed
80        end if
81        bit: u32 = u32(1) << u32(index)
82        state.val.stopped = false
83        return when (state.val.watched & bit) <> 0
84        atomic.store(item: u64, at: addr control_port,
85                     value: u64(state.val.queue))
86        if state.val.watched == 0 then
87            if windows.set_control_handler(usize(control_arrived), 1) == 0 then
88                record(self, windows.last_error())
89                fail io.io_failed
90            end if
91        end if
92        state.val.watched = state.val.watched | bit
93        atomic.store(item: u32, at: addr control_kinds,
94                     value: state.val.watched)
95    end events_watch
96
97    events_next_signal: (self: host) -> (received: signal.event)
98                        ! io.io_failed =
99        cleared(self)
100        state: ptr mut loop_state = self.state
101        loop do
102            fail io.io_failed when state.val.stopped
103            if state.val.queued > 0 then
104                event: u8 = state.val.arrived[0]
105                dec state.val.queued
106                for at in usize(0)..<state.val.queued do
107                    state.val.arrived[at] = state.val.arrived[at + 1]
108                end for
109                received = (which: kind_at(kind_of_event(u32(event))),
110                            number: u32(event))
111                return
112            end if
113            mut entry: waiting = on_deadline(0)
114            entry.signalled = true
115            parked(self, addr entry)
116            fail io.io_failed when entry.answer == failed
117        end loop
118    end events_next_signal
119
120    events_stop_watching: (self: host) -> none ! io.io_failed =
121        cleared(self)
122        state: ptr mut loop_state = self.state
123        mut returned: bool = true
124        atomic.store(item: u32, at: addr control_kinds, value: 0)
125        if state.val.watched <> 0 then
126            if windows.set_control_handler(usize(control_arrived), 0) == 0 then
127                record(self, windows.last_error())
128                returned = false
129            end if
130        end if
131        state.val.watched = 0
132        state.val.queued = 0
133        state.val.stopped = true
134        answer_signals(state, failed)
135        fail io.io_failed when not returned
136    end events_stop_watching
137
138    host is signal.signals
139        (watch: events_watch, next_signal: events_next_signal,
140         stop_watching: events_stop_watching)
141end if