Got Gtk-Doc working. Now all the fancy /** comments before the functions
[projects/chimara/chimara.git] / src / abort.c
index 63ef3bd1c15a7dfa2e453b70b85a7036a0c76777..4ae157addacc1eeed2b0ac92710c2d04cc8a446e 100644 (file)
@@ -2,38 +2,21 @@
 #include <glib.h>
 #include <gtk/gtk.h>
 
-static GMutex *abort_lock = NULL;
-static gboolean abort_signalled = FALSE;
-static void (*interrupt_handler)(void) = NULL;
+#include "chimara-glk-private.h"
 
-/* Internal function: initialize the interrupt handling system. */
-void
-interrupt_init()
-{
-       abort_lock = g_mutex_new();
-}
-
-/* Internal function: free the resources allocated in interrupt_init(). */
-void
-interrupt_free()
-{
-       g_mutex_lock(abort_lock);
-       /* Make sure no other thread is busy with this */
-       g_mutex_unlock(abort_lock);
-       g_mutex_free(abort_lock);
-       abort_lock = NULL;
-}
+extern ChimaraGlkPrivate *glk_data;
 
 /**
  * glk_set_interrupt_handler:
- * @func: A pointer to a function which takes no argument and returns no result.
+ * @func: A pointer to an interrupt handler function.
  *
- * Specifies an interrupt handler function for cleaning up critical resources.
- * If Glk receives an interrupt, and you have set an interrupt handler, your
- * handler will be called, before the process is shut down.
+ * Sets @func to be the interrupt handler. @func should be a pointer to a 
+ * function which takes no argument and returns no result. If Glk receives an
+ * interrupt, and you have set an interrupt handler, your handler will be 
+ * called, before the process is shut down.
  * 
  * Initially there is no interrupt handler. You can reset to not having any by
- * calling glk_set_interrupt_handler(%NULL).
+ * calling <code>#glk_set_interrupt_handler(%NULL)</code>.
  * 
  * If you call glk_set_interrupt_handler() with a new handler function while an
  * older one is set, the new one replaces the old one. Glk does not try to queue
@@ -46,15 +29,7 @@ interrupt_free()
 void
 glk_set_interrupt_handler(void (*func)(void))
 {
-       interrupt_handler = func;
-}
-
-/* Internal function: Free all Glk resources. */
-void
-cleanup()
-{
-       events_free();
-       interrupt_free();
+       glk_data->interrupt_handler = func;
 }
 
 /* Internal function: abort this Glk program, freeing resources and calling the
@@ -62,9 +37,9 @@ user's interrupt handler. */
 void
 abort_glk()
 {
-       if(interrupt_handler)
-               (*interrupt_handler)();
-       cleanup();
+       if(glk_data->interrupt_handler)
+               (*(glk_data->interrupt_handler))();
+       g_signal_emit_by_name(glk_data->self, "stopped");
        g_thread_exit(NULL);
 }
 
@@ -73,10 +48,10 @@ mutex has already been freed. (That means the thread already ended.) */
 void
 signal_abort()
 {
-       if(abort_lock) {
-               g_mutex_lock(abort_lock);
-               abort_signalled = TRUE;
-               g_mutex_unlock(abort_lock);
+       if(glk_data && glk_data->abort_lock) {
+               g_mutex_lock(glk_data->abort_lock);
+               glk_data->abort_signalled = TRUE;
+               g_mutex_unlock(glk_data->abort_lock);
                /* Stop blocking on the event queue condition */
                event_throw(evtype_Abort, NULL, 0, 0);
        }
@@ -86,13 +61,13 @@ signal_abort()
 void
 check_for_abort()
 {
-       g_mutex_lock(abort_lock);
-       if(abort_signalled) 
+       g_mutex_lock(glk_data->abort_lock);
+       if(glk_data->abort_signalled) 
        {
-               g_mutex_unlock(abort_lock);
+               g_mutex_unlock(glk_data->abort_lock);
                abort_glk();
        }
-       g_mutex_unlock(abort_lock);
+       g_mutex_unlock(glk_data->abort_lock);
 }