Started using thread-private data. Multisession still doesn't work, but regular opera...
[projects/chimara/chimara.git] / libchimara / abort.c
1 #include "event.h"
2 #include <glib.h>
3 #include <gtk/gtk.h>
4
5 #include "chimara-glk-private.h"
6
7 extern GPrivate *glk_data_key;
8
9 /**
10  * glk_set_interrupt_handler:
11  * @func: A pointer to an interrupt handler function.
12  *
13  * Sets @func to be the interrupt handler. @func should be a pointer to a 
14  * function which takes no argument and returns no result. If Glk receives an
15  * interrupt, and you have set an interrupt handler, your handler will be 
16  * called, before the process is shut down.
17  * 
18  * Initially there is no interrupt handler. You can reset to not having any by
19  * calling <code>#glk_set_interrupt_handler(%NULL)</code>.
20  * 
21  * If you call glk_set_interrupt_handler() with a new handler function while an
22  * older one is set, the new one replaces the old one. Glk does not try to queue
23  * interrupt handlers.
24  *
25  * You should not try to interact with the player in your interrupt handler. Do
26  * not call glk_select() or glk_select_poll(). Anything you print to a window
27  * may not be visible to the player. 
28  */
29 void
30 glk_set_interrupt_handler(void (*func)(void))
31 {
32         ChimaraGlkPrivate *glk_data = g_private_get(glk_data_key);
33         glk_data->interrupt_handler = func;
34 }
35
36 /* Internal function: abort this Glk program, freeing resources and calling the
37 user's interrupt handler. */
38 static void
39 abort_glk()
40 {
41         ChimaraGlkPrivate *glk_data = g_private_get(glk_data_key);
42         if(glk_data->interrupt_handler)
43                 (*(glk_data->interrupt_handler))();
44         g_signal_emit_by_name(glk_data->self, "stopped");
45         g_thread_exit(NULL);
46 }
47
48 /* Internal function: check if the Glk program has been interrupted. */
49 void
50 check_for_abort()
51 {
52         ChimaraGlkPrivate *glk_data = g_private_get(glk_data_key);
53         g_mutex_lock(glk_data->abort_lock);
54         if(glk_data->abort_signalled) 
55         {
56                 g_mutex_unlock(glk_data->abort_lock);
57                 abort_glk();
58         }
59         g_mutex_unlock(glk_data->abort_lock);
60 }