2 * Console control handler support.
7 #include "ConsoleHandler.h"
13 #include "RtsSignals.h"
15 extern int stg_InstallConsoleEvent(int action, StgStablePtr *handler);
17 static BOOL WINAPI shutdown_handler(DWORD dwCtrlType);
18 static BOOL WINAPI generic_handler(DWORD dwCtrlType);
20 static rtsBool deliver_event = rtsTrue;
21 StgInt console_handler = STG_SIG_DFL;
23 #if !defined(THREADED_RTS)
25 static HANDLE hConsoleEvent = INVALID_HANDLE_VALUE;
27 #define N_PENDING_EVENTS 16
28 StgInt stg_pending_events = 0; /* number of undelivered events */
29 DWORD stg_pending_buf[N_PENDING_EVENTS]; /* their associated event numbers. */
34 * Function: initUserSignals()
36 * Initialize the console handling substrate.
41 console_handler = STG_SIG_DFL;
42 #if !defined (THREADED_RTS)
43 stg_pending_events = 0;
44 if (hConsoleEvent == INVALID_HANDLE_VALUE) {
46 CreateEvent ( NULL, /* default security attributes */
47 TRUE, /* manual-reset event */
48 FALSE, /* initially non-signalled */
55 /* Seems to be a bit of an orphan...where used? */
59 #if !defined (THREADED_RTS)
60 if (hConsoleEvent != INVALID_HANDLE_VALUE) {
61 CloseHandle(hConsoleEvent);
67 * Function: shutdown_handler()
69 * Local function that performs the default handling of Ctrl+C kind
70 * events; gently shutting down the RTS
72 * To repeat Signals.c remark -- user code may choose to override the
73 * default handler. Which is fine, assuming they put back the default
74 * handler when/if they de-install the custom handler.
77 static BOOL WINAPI shutdown_handler(DWORD dwCtrlType)
81 case CTRL_CLOSE_EVENT:
82 /* see generic_handler() comment re: this event */
85 case CTRL_BREAK_EVENT:
87 // If we're already trying to interrupt the RTS, terminate with
88 // extreme prejudice. So the first ^C tries to exit the program
89 // cleanly, and the second one just kills it.
90 if (sched_state >= SCHED_INTERRUPTING) {
91 stg_exit(EXIT_INTERRUPTED);
97 /* shutdown + logoff events are not handled here. */
105 * Function: initDefaultHandlers()
107 * Install any default signal/console handlers. Currently we install a
108 * Ctrl+C handler that shuts down the RTS in an orderly manner.
110 void initDefaultHandlers(void)
112 if ( !SetConsoleCtrlHandler(shutdown_handler, TRUE) ) {
113 errorBelch("warning: failed to install default console handler");
119 * Function: blockUserSignals()
121 * Temporarily block the delivery of further console events. Needed to
122 * avoid race conditions when GCing the stack of outstanding handlers or
123 * when emptying the stack by running the handlers.
127 blockUserSignals(void)
129 deliver_event = rtsFalse;
134 * Function: unblockUserSignals()
136 * The inverse of blockUserSignals(); re-enable the deliver of console events.
139 unblockUserSignals(void)
141 deliver_event = rtsTrue;
146 * Function: awaitUserSignals()
148 * Wait for the next console event. Currently a NOP (returns immediately.)
150 void awaitUserSignals(void)
156 #if !defined (THREADED_RTS)
158 * Function: startSignalHandlers()
160 * Run the handlers associated with the stacked up console events. Console
161 * event delivery is blocked for the duration of this call.
163 void startSignalHandlers(Capability *cap)
165 StgStablePtr handler;
167 if (console_handler < 0) {
172 ACQUIRE_LOCK(&sched_mutex);
174 handler = deRefStablePtr((StgStablePtr)console_handler);
175 while (stg_pending_events > 0) {
176 stg_pending_events--;
179 RtsFlags.GcFlags.initialStkSize,
181 (StgClosure *)handler,
183 stg_pending_buf[stg_pending_events]))));
186 RELEASE_LOCK(&sched_mutex);
187 unblockUserSignals();
189 #endif /* !THREADED_RTS */
192 * Function: markSignalHandlers()
194 * Evacuate the handler stack. _Assumes_ that console event delivery
195 * has already been blocked.
197 void markSignalHandlers (evac_fn evac STG_UNUSED)
199 // nothing to mark; the console handler is a StablePtr which is
200 // already treated as a root by the GC.
205 * Function: generic_handler()
207 * Local function which handles incoming console event (done in a sep OS thread),
208 * recording the event in stg_pending_events.
210 static BOOL WINAPI generic_handler(DWORD dwCtrlType)
212 /* Ultra-simple -- up the counter + signal a switch. */
214 case CTRL_CLOSE_EVENT:
215 /* Don't support the delivery of this event; if we
216 * indicate that we've handled it here and the Haskell handler
217 * doesn't take proper action (e.g., terminate the OS process),
218 * the user of the app will be unable to kill/close it. Not
219 * good, so disable the delivery for now.
223 if (!deliver_event) return TRUE;
225 #if defined(THREADED_RTS)
226 sendIOManagerEvent((StgWord8) ((dwCtrlType<<1) | 1));
228 if ( stg_pending_events < N_PENDING_EVENTS ) {
229 stg_pending_buf[stg_pending_events] = dwCtrlType;
230 stg_pending_events++;
239 * Function: rts_InstallConsoleEvent()
241 * Install/remove a console event handler.
244 rts_InstallConsoleEvent(int action, StgStablePtr *handler)
246 StgInt previous_hdlr = console_handler;
250 console_handler = STG_SIG_IGN;
251 if ( !SetConsoleCtrlHandler(NULL, TRUE) ) {
252 errorBelch("warning: unable to ignore console events");
256 console_handler = STG_SIG_IGN;
257 if ( !SetConsoleCtrlHandler(NULL, FALSE) ) {
258 errorBelch("warning: unable to restore default console event handling");
262 console_handler = (StgInt)*handler;
263 if ( previous_hdlr < 0 ) {
264 /* Only install generic_handler() once */
265 if ( !SetConsoleCtrlHandler(generic_handler, TRUE) ) {
266 errorBelch("warning: unable to install console event handler");
272 if (previous_hdlr == STG_SIG_DFL ||
273 previous_hdlr == STG_SIG_IGN) {
274 return previous_hdlr;
276 *handler = (StgStablePtr)previous_hdlr;
282 * Function: rts_HandledConsoleEvent()
284 * Signal that a Haskell console event handler has completed its run.
285 * The explicit notification that a Haskell handler has completed is
286 * required to better handle the delivery of Ctrl-C/Break events whilst
287 * an async worker thread is handling a read request on stdin. The
288 * Win32 console implementation will abort such a read request when Ctrl-C
289 * is delivered. That leaves the worker thread in a bind: should it
290 * abandon the request (the Haskell thread reading from stdin has been
291 * thrown an exception to signal the delivery of Ctrl-C & hence have
292 * aborted the I/O request) or simply ignore the aborted read and retry?
293 * (the Haskell thread reading from stdin isn't concerned with the
294 * delivery and handling of Ctrl-C.) With both scenarios being
295 * possible, the worker thread needs to be told -- that is, did the
296 * console event handler cause the IO request to be abandoned?
300 rts_ConsoleHandlerDone (int ev USED_IF_NOT_THREADS)
302 #if !defined(THREADED_RTS)
303 if ( (DWORD)ev == CTRL_BREAK_EVENT ||
304 (DWORD)ev == CTRL_C_EVENT ) {
305 /* only these two cause stdin system calls to abort.. */
306 SetEvent(hConsoleEvent); /* event is manual-reset */
307 Sleep(0); /* yield */
308 ResetEvent(hConsoleEvent); /* turn it back off again */
309 // SDM: yeuch, this can't possibly work reliably.
310 // I'm not having it in THREADED_RTS.
315 #if !defined(THREADED_RTS)
317 * Function: rts_waitConsoleHandlerCompletion()
319 * Esoteric entry point used by worker thread that got woken
320 * up as part Ctrl-C delivery.
323 rts_waitConsoleHandlerCompletion()
325 /* As long as the worker doesn't need to do a multiple wait,
326 * let's keep this HANDLE private to this 'module'.
328 return (WaitForSingleObject(hConsoleEvent, INFINITE) == WAIT_OBJECT_0);