Audacious  $Id:Doxyfile42802007-03-2104:39:00Znenolod$
eventqueue.c
Go to the documentation of this file.
00001 /*
00002  * eventqueue.c
00003  * Copyright 2011 John Lindgren
00004  *
00005  * Redistribution and use in source and binary forms, with or without
00006  * modification, are permitted provided that the following conditions are met:
00007  *
00008  * 1. Redistributions of source code must retain the above copyright notice,
00009  *    this list of conditions, and the following disclaimer.
00010  *
00011  * 2. Redistributions in binary form must reproduce the above copyright notice,
00012  *    this list of conditions, and the following disclaimer in the documentation
00013  *    provided with the distribution.
00014  *
00015  * This software is provided "as is" and without any warranty, express or
00016  * implied. In no event shall the authors be liable for any damages arising from
00017  * the use of this software.
00018  */
00019 
00020 #include <glib.h>
00021 #include <pthread.h>
00022 #include <string.h>
00023 
00024 #include "config.h"
00025 #include "core.h"
00026 #include "hook.h"
00027 
00028 typedef struct {
00029     char * name;
00030     void * data;
00031     void (* destroy) (void *);
00032     int source;
00033 } Event;
00034 
00035 static pthread_mutex_t mutex = PTHREAD_MUTEX_INITIALIZER;
00036 static GList * events;
00037 
00038 static bool_t event_execute (Event * event)
00039 {
00040     pthread_mutex_lock (& mutex);
00041 
00042     g_source_remove (event->source);
00043     events = g_list_remove (events, event);
00044 
00045     pthread_mutex_unlock (& mutex);
00046 
00047     hook_call (event->name, event->data);
00048 
00049     g_free (event->name);
00050     if (event->destroy)
00051         event->destroy (event->data);
00052 
00053     g_slice_free (Event, event);
00054     return FALSE;
00055 }
00056 
00057 EXPORT void event_queue_full (int time, const char * name, void * data, void (* destroy) (void *))
00058 {
00059     Event * event = g_slice_new (Event);
00060     event->name = g_strdup (name);
00061     event->data = data;
00062     event->destroy = destroy;
00063 
00064     pthread_mutex_lock (& mutex);
00065 
00066     event->source = g_timeout_add (time, (GSourceFunc) event_execute, event);
00067     events = g_list_prepend (events, event);
00068 
00069     pthread_mutex_unlock (& mutex);
00070 }
00071 
00072 EXPORT void event_queue_cancel (const char * name, void * data)
00073 {
00074     pthread_mutex_lock (& mutex);
00075 
00076     GList * node = events;
00077     while (node)
00078     {
00079         Event * event = node->data;
00080         GList * next = node->next;
00081 
00082         if (! strcmp (event->name, name) && (! data || event->data == data))
00083         {
00084             g_source_remove (event->source);
00085             events = g_list_delete_link (events, node);
00086 
00087             g_free (event->name);
00088             if (event->destroy)
00089                 event->destroy (event->data);
00090 
00091             g_slice_free (Event, event);
00092         }
00093 
00094         node = next;
00095     }
00096 
00097     pthread_mutex_unlock (& mutex);
00098 }