Audacious  $Id:Doxyfile42802007-03-2104:39:00Znenolod$
eventqueue.c
Go to the documentation of this file.
1 /*
2  * eventqueue.c
3  * Copyright 2011 John Lindgren
4  *
5  * Redistribution and use in source and binary forms, with or without
6  * modification, are permitted provided that the following conditions are met:
7  *
8  * 1. Redistributions of source code must retain the above copyright notice,
9  * this list of conditions, and the following disclaimer.
10  *
11  * 2. Redistributions in binary form must reproduce the above copyright notice,
12  * this list of conditions, and the following disclaimer in the documentation
13  * provided with the distribution.
14  *
15  * This software is provided "as is" and without any warranty, express or
16  * implied. In no event shall the authors be liable for any damages arising from
17  * the use of this software.
18  */
19 
20 #include <glib.h>
21 #include <pthread.h>
22 #include <string.h>
23 
24 #include "core.h"
25 #include "hook.h"
26 
27 typedef struct {
28  char * name;
29  void * data;
30  void (* destroy) (void *);
31  int source;
32 } Event;
33 
34 static pthread_mutex_t mutex = PTHREAD_MUTEX_INITIALIZER;
35 static GList * events;
36 
37 static bool_t event_execute (Event * event)
38 {
39  pthread_mutex_lock (& mutex);
40 
41  g_source_remove (event->source);
42  events = g_list_remove (events, event);
43 
44  pthread_mutex_unlock (& mutex);
45 
46  hook_call (event->name, event->data);
47 
48  g_free (event->name);
49  if (event->destroy)
50  event->destroy (event->data);
51 
52  g_slice_free (Event, event);
53  return FALSE;
54 }
55 
56 EXPORT void event_queue_full (int time, const char * name, void * data, void (* destroy) (void *))
57 {
58  Event * event = g_slice_new (Event);
59  event->name = g_strdup (name);
60  event->data = data;
61  event->destroy = destroy;
62 
63  pthread_mutex_lock (& mutex);
64 
65  event->source = g_timeout_add (time, (GSourceFunc) event_execute, event);
66  events = g_list_prepend (events, event);
67 
68  pthread_mutex_unlock (& mutex);
69 }
70 
71 EXPORT void event_queue_cancel (const char * name, void * data)
72 {
73  pthread_mutex_lock (& mutex);
74 
75  GList * node = events;
76  while (node)
77  {
78  Event * event = node->data;
79  GList * next = node->next;
80 
81  if (! strcmp (event->name, name) && (! data || event->data == data))
82  {
83  g_source_remove (event->source);
84  events = g_list_delete_link (events, node);
85 
86  g_free (event->name);
87  if (event->destroy)
88  event->destroy (event->data);
89 
90  g_slice_free (Event, event);
91  }
92 
93  node = next;
94  }
95 
96  pthread_mutex_unlock (& mutex);
97 }