2 * Console control handler support.
7 #include "ConsoleHandler.h"
12 #include "StablePriv.h"
14 extern int stg_InstallConsoleEvent(int action, StgStablePtr *handler);
16 static BOOL WINAPI shutdown_handler(DWORD dwCtrlType);
17 static BOOL WINAPI generic_handler(DWORD dwCtrlType);
19 static rtsBool deliver_event = rtsTrue;
20 static StgInt console_handler = STG_SIG_DFL;
22 #define N_PENDING_EVENTS 16
23 StgInt stg_pending_events = 0; /* number of undelivered events */
24 DWORD stg_pending_buf[N_PENDING_EVENTS]; /* their associated event numbers. */
27 * Function: initUserSignals()
29 * Initialize the console handling substrate.
34 stg_pending_events = 0;
35 console_handler = STG_SIG_DFL;
40 * Function: shutdown_handler()
42 * Local function that performs the default handling of Ctrl+C kind
43 * events; gently shutting down the RTS
45 * To repeat Signals.c remark -- user code may choose to override the
46 * default handler. Which is fine, assuming they put back the default
47 * handler when/if they de-install the custom handler.
50 static BOOL WINAPI shutdown_handler(DWORD dwCtrlType)
55 case CTRL_BREAK_EVENT:
56 case CTRL_CLOSE_EVENT:
58 // If we're already trying to interrupt the RTS, terminate with
59 // extreme prejudice. So the first ^C tries to exit the program
60 // cleanly, and the second one just kills it.
62 stg_exit(EXIT_INTERRUPTED);
68 /* shutdown + logoff events are not handled here. */
76 * Function: initDefaultHandlers()
78 * Install any default signal/console handlers. Currently we install a
79 * Ctrl+C handler that shuts down the RTS in an orderly manner.
81 void initDefaultHandlers(void)
83 if ( !SetConsoleCtrlHandler(shutdown_handler, TRUE) ) {
84 prog_belch("warning: failed to install default console handler");
91 * Function: blockUserSignals()
93 * Temporarily block the delivery of further console events. Needed to
94 * avoid race conditions when GCing the stack of outstanding handlers or
95 * when emptying the stack by running the handlers.
99 blockUserSignals(void)
101 deliver_event = rtsFalse;
106 * Function: unblockUserSignals()
108 * The inverse of blockUserSignals(); re-enable the deliver of console events.
111 unblockUserSignals(void)
113 deliver_event = rtsTrue;
118 * Function: awaitUserSignals()
120 * Wait for the next console event. Currently a NOP (returns immediately.)
122 void awaitUserSignals(void)
129 * Function: startSignalHandlers()
131 * Run the handlers associated with the stacked up console events. Console
132 * event delivery is blocked for the duration of this call.
134 void startSignalHandlers(void)
136 StgStablePtr handler;
138 if (console_handler < 0) {
143 handler = deRefStablePtr((StgStablePtr)console_handler);
144 while (stg_pending_events > 0) {
145 stg_pending_events--;
147 createIOThread(RtsFlags.GcFlags.initialStkSize,
148 rts_apply((StgClosure *)handler,
149 rts_mkInt(stg_pending_buf[stg_pending_events]))));
151 unblockUserSignals();
156 * Function: markSignalHandlers()
158 * Evacuate the handler stack. _Assumes_ that console event delivery
159 * has already been blocked.
161 void markSignalHandlers (evac_fn evac)
163 if (console_handler >= 0) {
164 StgPtr p = deRefStablePtr((StgStablePtr)console_handler);
165 evac((StgClosure**)&p);
171 * Function: handleSignalsInThisThread()
173 * Have current (OS) thread assume responsibility of handling console events/signals.
174 * Currently not used (by the console event handling code.)
176 void handleSignalsInThisThread(void)
182 * Function: generic_handler()
184 * Local function which handles incoming console event (done in a sep OS thread),
185 * recording the event in stg_pending_events.
187 static BOOL WINAPI generic_handler(DWORD dwCtrlType)
189 /* Ultra-simple -- up the counter + signal a switch. */
190 if ( stg_pending_events < N_PENDING_EVENTS ) {
191 stg_pending_buf[stg_pending_events] = dwCtrlType;
192 stg_pending_events++;
200 * Function: stg_InstallConsoleEvent()
202 * Install/remove a console event handler.
205 stg_InstallConsoleEvent(int action, StgStablePtr *handler)
207 StgInt previous_hdlr = console_handler;
211 console_handler = STG_SIG_IGN;
212 if ( !SetConsoleCtrlHandler(NULL, TRUE) ) {
213 prog_belch("warning: unable to ignore console events");
217 console_handler = STG_SIG_IGN;
218 if ( !SetConsoleCtrlHandler(NULL, FALSE) ) {
219 prog_belch("warning: unable to restore default console event handling");
223 console_handler = (StgInt)*handler;
224 if ( !SetConsoleCtrlHandler(generic_handler, TRUE) ) {
225 prog_belch("warning: unable to install console event handler");
230 if (previous_hdlr == STG_SIG_DFL ||
231 previous_hdlr == STG_SIG_IGN) {
232 return previous_hdlr;
234 *handler = (StgStablePtr)previous_hdlr;