63ef3bd1c15a7dfa2e453b70b85a7036a0c76777
[rodin/chimara.git] / src / abort.c
1 #include "event.h"
2 #include <glib.h>
3 #include <gtk/gtk.h>
4
5 static GMutex *abort_lock = NULL;
6 static gboolean abort_signalled = FALSE;
7 static void (*interrupt_handler)(void) = NULL;
8
9 /* Internal function: initialize the interrupt handling system. */
10 void
11 interrupt_init()
12 {
13         abort_lock = g_mutex_new();
14 }
15
16 /* Internal function: free the resources allocated in interrupt_init(). */
17 void
18 interrupt_free()
19 {
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);
24         abort_lock = NULL;
25 }
26
27 /**
28  * glk_set_interrupt_handler:
29  * @func: A pointer to a function which takes no argument and returns no result.
30  *
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.
34  * 
35  * Initially there is no interrupt handler. You can reset to not having any by
36  * calling glk_set_interrupt_handler(%NULL).
37  * 
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
40  * interrupt handlers.
41  *
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. 
45  */
46 void
47 glk_set_interrupt_handler(void (*func)(void))
48 {
49         interrupt_handler = func;
50 }
51
52 /* Internal function: Free all Glk resources. */
53 void
54 cleanup()
55 {
56         events_free();
57         interrupt_free();
58 }
59
60 /* Internal function: abort this Glk program, freeing resources and calling the
61 user's interrupt handler. */
62 void
63 abort_glk()
64 {
65         if(interrupt_handler)
66                 (*interrupt_handler)();
67         cleanup();
68         g_thread_exit(NULL);
69 }
70
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.) */
73 void
74 signal_abort()
75 {
76         if(abort_lock) {
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);
82         }
83 }
84
85 /* Internal function: check if the Glk program has been interrupted. */
86 void
87 check_for_abort()
88 {
89         g_mutex_lock(abort_lock);
90         if(abort_signalled) 
91         {
92                 g_mutex_unlock(abort_lock);
93                 abort_glk();
94         }
95         g_mutex_unlock(abort_lock);
96 }
97
98