5 static GMutex *abort_lock = NULL;
6 static gboolean abort_signalled = FALSE;
7 static void (*interrupt_handler)(void) = NULL;
9 /* Internal function: initialize the interrupt handling system. */
13 abort_lock = g_mutex_new();
16 /* Internal function: free the resources allocated in interrupt_init(). */
20 g_mutex_lock(abort_lock);
21 /* Make sure no other thread is busy with this */
22 g_mutex_unlock(abort_lock);
23 g_mutex_free(abort_lock);
28 * glk_set_interrupt_handler:
29 * @func: A pointer to a function which takes no argument and returns no result.
31 * Specifies an interrupt handler function for cleaning up critical resources.
32 * If Glk receives an interrupt, and you have set an interrupt handler, your
33 * handler will be called, before the process is shut down.
35 * Initially there is no interrupt handler. You can reset to not having any by
36 * calling glk_set_interrupt_handler(%NULL).
38 * If you call glk_set_interrupt_handler() with a new handler function while an
39 * older one is set, the new one replaces the old one. Glk does not try to queue
42 * You should not try to interact with the player in your interrupt handler. Do
43 * not call glk_select() or glk_select_poll(). Anything you print to a window
44 * may not be visible to the player.
47 glk_set_interrupt_handler(void (*func)(void))
49 interrupt_handler = func;
52 /* Internal function: Free all Glk resources. */
60 /* Internal function: abort this Glk program, freeing resources and calling the
61 user's interrupt handler. */
66 (*interrupt_handler)();
71 /* Internal function: Signal this Glk thread to abort. Does nothing if the abort
72 mutex has already been freed. (That means the thread already ended.) */
77 g_mutex_lock(abort_lock);
78 abort_signalled = TRUE;
79 g_mutex_unlock(abort_lock);
80 /* Stop blocking on the event queue condition */
81 event_throw(evtype_Abort, NULL, 0, 0);
85 /* Internal function: check if the Glk program has been interrupted. */
89 g_mutex_lock(abort_lock);
92 g_mutex_unlock(abort_lock);
95 g_mutex_unlock(abort_lock);