2 * Console control handler support.
7 #include "ConsoleHandler.h"
11 #include "RtsSignals.h"
13 extern int stg_InstallConsoleEvent(int action, StgStablePtr *handler);
15 static BOOL WINAPI shutdown_handler(DWORD dwCtrlType);
16 static BOOL WINAPI generic_handler(DWORD dwCtrlType);
18 static rtsBool deliver_event = rtsTrue;
19 StgInt console_handler = STG_SIG_DFL;
21 #if !defined(THREADED_RTS)
23 static HANDLE hConsoleEvent = INVALID_HANDLE_VALUE;
25 #define N_PENDING_EVENTS 16
26 StgInt stg_pending_events = 0; /* number of undelivered events */
27 DWORD stg_pending_buf[N_PENDING_EVENTS]; /* their associated event numbers. */
32 * Function: initUserSignals()
34 * Initialize the console handling substrate.
39 console_handler = STG_SIG_DFL;
40 #if !defined (THREADED_RTS)
41 stg_pending_events = 0;
42 if (hConsoleEvent == INVALID_HANDLE_VALUE) {
44 CreateEvent ( NULL, /* default security attributes */
45 TRUE, /* manual-reset event */
46 FALSE, /* initially non-signalled */
54 freeSignalHandlers(void) {
58 /* Seems to be a bit of an orphan...where used? */
62 #if !defined (THREADED_RTS)
63 if (hConsoleEvent != INVALID_HANDLE_VALUE) {
64 CloseHandle(hConsoleEvent);
70 * Function: shutdown_handler()
72 * Local function that performs the default handling of Ctrl+C kind
73 * events; gently shutting down the RTS
75 * To repeat Signals.c remark -- user code may choose to override the
76 * default handler. Which is fine, assuming they put back the default
77 * handler when/if they de-install the custom handler.
80 static BOOL WINAPI shutdown_handler(DWORD dwCtrlType)
84 case CTRL_CLOSE_EVENT:
85 /* see generic_handler() comment re: this event */
88 case CTRL_BREAK_EVENT:
90 // If we're already trying to interrupt the RTS, terminate with
91 // extreme prejudice. So the first ^C tries to exit the program
92 // cleanly, and the second one just kills it.
93 if (sched_state >= SCHED_INTERRUPTING) {
94 stg_exit(EXIT_INTERRUPTED);
100 /* shutdown + logoff events are not handled here. */
108 * Function: initDefaultHandlers()
110 * Install any default signal/console handlers. Currently we install a
111 * Ctrl+C handler that shuts down the RTS in an orderly manner.
113 void initDefaultHandlers(void)
115 if ( !SetConsoleCtrlHandler(shutdown_handler, TRUE) ) {
116 errorBelch("warning: failed to install default console handler");
120 void resetDefaultHandlers(void)
122 if ( !SetConsoleCtrlHandler(shutdown_handler, FALSE) ) {
123 errorBelch("warning: failed to uninstall default console handler");
128 * Function: blockUserSignals()
130 * Temporarily block the delivery of further console events. Needed to
131 * avoid race conditions when GCing the stack of outstanding handlers or
132 * when emptying the stack by running the handlers.
136 blockUserSignals(void)
138 deliver_event = rtsFalse;
143 * Function: unblockUserSignals()
145 * The inverse of blockUserSignals(); re-enable the deliver of console events.
148 unblockUserSignals(void)
150 deliver_event = rtsTrue;
155 * Function: awaitUserSignals()
157 * Wait for the next console event. Currently a NOP (returns immediately.)
159 void awaitUserSignals(void)
165 #if !defined (THREADED_RTS)
167 * Function: startSignalHandlers()
169 * Run the handlers associated with the stacked up console events. Console
170 * event delivery is blocked for the duration of this call.
172 void startSignalHandlers(Capability *cap)
174 StgStablePtr handler;
176 if (console_handler < 0) {
181 ACQUIRE_LOCK(&sched_mutex);
183 handler = deRefStablePtr((StgStablePtr)console_handler);
184 while (stg_pending_events > 0) {
185 stg_pending_events--;
188 RtsFlags.GcFlags.initialStkSize,
190 (StgClosure *)handler,
192 stg_pending_buf[stg_pending_events]))));
195 RELEASE_LOCK(&sched_mutex);
196 unblockUserSignals();
198 #endif /* !THREADED_RTS */
201 * Function: markSignalHandlers()
203 * Evacuate the handler stack. _Assumes_ that console event delivery
204 * has already been blocked.
206 void markSignalHandlers (evac_fn evac STG_UNUSED, void *user STG_UNUSED)
208 // nothing to mark; the console handler is a StablePtr which is
209 // already treated as a root by the GC.
214 * Function: generic_handler()
216 * Local function which handles incoming console event (done in a sep OS thread),
217 * recording the event in stg_pending_events.
219 static BOOL WINAPI generic_handler(DWORD dwCtrlType)
221 /* Ultra-simple -- up the counter + signal a switch. */
223 case CTRL_CLOSE_EVENT:
224 /* Don't support the delivery of this event; if we
225 * indicate that we've handled it here and the Haskell handler
226 * doesn't take proper action (e.g., terminate the OS process),
227 * the user of the app will be unable to kill/close it. Not
228 * good, so disable the delivery for now.
232 if (!deliver_event) return TRUE;
234 #if defined(THREADED_RTS)
235 sendIOManagerEvent((StgWord8) ((dwCtrlType<<1) | 1));
237 if ( stg_pending_events < N_PENDING_EVENTS ) {
238 stg_pending_buf[stg_pending_events] = dwCtrlType;
239 stg_pending_events++;
242 // we need to wake up awaitEvent()
243 abandonRequestWait();
251 * Function: rts_InstallConsoleEvent()
253 * Install/remove a console event handler.
256 rts_InstallConsoleEvent(int action, StgStablePtr *handler)
258 StgInt previous_hdlr = console_handler;
262 console_handler = STG_SIG_IGN;
263 if ( !SetConsoleCtrlHandler(NULL, TRUE) ) {
264 errorBelch("warning: unable to ignore console events");
268 console_handler = STG_SIG_IGN;
269 if ( !SetConsoleCtrlHandler(NULL, FALSE) ) {
270 errorBelch("warning: unable to restore default console event handling");
275 // handler is stored in an MVar in the threaded RTS
276 console_handler = STG_SIG_HAN;
278 console_handler = (StgInt)*handler;
280 if (previous_hdlr < 0 || previous_hdlr == STG_SIG_HAN) {
281 /* Only install generic_handler() once */
282 if ( !SetConsoleCtrlHandler(generic_handler, TRUE) ) {
283 errorBelch("warning: unable to install console event handler");
289 if (previous_hdlr == STG_SIG_DFL ||
290 previous_hdlr == STG_SIG_IGN ||
291 previous_hdlr == STG_SIG_HAN) {
292 return previous_hdlr;
294 if (handler != NULL) {
295 *handler = (StgStablePtr)previous_hdlr;
302 * Function: rts_HandledConsoleEvent()
304 * Signal that a Haskell console event handler has completed its run.
305 * The explicit notification that a Haskell handler has completed is
306 * required to better handle the delivery of Ctrl-C/Break events whilst
307 * an async worker thread is handling a read request on stdin. The
308 * Win32 console implementation will abort such a read request when Ctrl-C
309 * is delivered. That leaves the worker thread in a bind: should it
310 * abandon the request (the Haskell thread reading from stdin has been
311 * thrown an exception to signal the delivery of Ctrl-C & hence have
312 * aborted the I/O request) or simply ignore the aborted read and retry?
313 * (the Haskell thread reading from stdin isn't concerned with the
314 * delivery and handling of Ctrl-C.) With both scenarios being
315 * possible, the worker thread needs to be told -- that is, did the
316 * console event handler cause the IO request to be abandoned?
320 rts_ConsoleHandlerDone (int ev USED_IF_NOT_THREADS)
322 #if !defined(THREADED_RTS)
323 if ( (DWORD)ev == CTRL_BREAK_EVENT ||
324 (DWORD)ev == CTRL_C_EVENT ) {
325 /* only these two cause stdin system calls to abort.. */
326 SetEvent(hConsoleEvent); /* event is manual-reset */
327 Sleep(0); /* yield */
328 ResetEvent(hConsoleEvent); /* turn it back off again */
329 // SDM: yeuch, this can't possibly work reliably.
330 // I'm not having it in THREADED_RTS.
335 #if !defined(THREADED_RTS)
337 * Function: rts_waitConsoleHandlerCompletion()
339 * Esoteric entry point used by worker thread that got woken
340 * up as part Ctrl-C delivery.
343 rts_waitConsoleHandlerCompletion()
345 /* As long as the worker doesn't need to do a multiple wait,
346 * let's keep this HANDLE private to this 'module'.
348 return (WaitForSingleObject(hConsoleEvent, INFINITE) == WAIT_OBJECT_0);