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 "config.h"
25 #include "core.h"
26 #include "hook.h"
27 
28 typedef struct {
29  char * name;
30  void * data;
31  void (* destroy) (void *);
32  int source;
33 } Event;
34 
35 static pthread_mutex_t mutex = PTHREAD_MUTEX_INITIALIZER;
36 static GList * events;
37 
38 static bool_t event_execute (Event * event)
39 {
40  pthread_mutex_lock (& mutex);
41 
42  g_source_remove (event->source);
43  events = g_list_remove (events, event);
44 
45  pthread_mutex_unlock (& mutex);
46 
47  hook_call (event->name, event->data);
48 
49  g_free (event->name);
50  if (event->destroy)
51  event->destroy (event->data);
52 
53  g_slice_free (Event, event);
54  return FALSE;
55 }
56 
57 EXPORT void event_queue_full (int time, const char * name, void * data, void (* destroy) (void *))
58 {
59  Event * event = g_slice_new (Event);
60  event->name = g_strdup (name);
61  event->data = data;
62  event->destroy = destroy;
63 
64  pthread_mutex_lock (& mutex);
65 
66  event->source = g_timeout_add (time, (GSourceFunc) event_execute, event);
67  events = g_list_prepend (events, event);
68 
69  pthread_mutex_unlock (& mutex);
70 }
71 
72 EXPORT void event_queue_cancel (const char * name, void * data)
73 {
74  pthread_mutex_lock (& mutex);
75 
76  GList * node = events;
77  while (node)
78  {
79  Event * event = node->data;
80  GList * next = node->next;
81 
82  if (! strcmp (event->name, name) && (! data || event->data == data))
83  {
84  g_source_remove (event->source);
85  events = g_list_delete_link (events, node);
86 
87  g_free (event->name);
88  if (event->destroy)
89  event->destroy (event->data);
90 
91  g_slice_free (Event, event);
92  }
93 
94  node = next;
95  }
96 
97  pthread_mutex_unlock (& mutex);
98 }