]> git.sesse.net Git - vlc/blob - src/control/event_async.c
libvlc: include config.h when needed
[vlc] / src / control / event_async.c
1 /*****************************************************************************
2  * event.c: New libvlc event control API
3  *****************************************************************************
4  * Copyright (C) 2007 the VideoLAN team
5  * $Id $
6  *
7  * Authors: Filippo Carone <filippo@carone.org>
8  *          Pierre d'Herbemont <pdherbemont # videolan.org>
9  *
10  * This program is free software; you can redistribute it and/or modify
11  * it under the terms of the GNU General Public License as published by
12  * the Free Software Foundation; either version 2 of the License, or
13  * (at your option) any later version.
14  *
15  * This program is distributed in the hope that it will be useful,
16  * but WITHOUT ANY WARRANTY; without even the implied warranty of
17  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
18  * GNU General Public License for more details.
19  *
20  * You should have received a copy of the GNU General Public License
21  * along with this program; if not, write to the Free Software
22  * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston MA 02110-1301, USA.
23  *****************************************************************************/
24
25 #ifdef HAVE_CONFIG_H
26 # include "config.h"
27 #endif
28
29 #include <assert.h>
30
31 #include <vlc/libvlc.h>
32
33 #include "libvlc_internal.h"
34 #include "event_internal.h"
35
36 struct queue_elmt {
37     libvlc_event_listener_t listener;
38     libvlc_event_t event;
39     struct queue_elmt * next;
40 };
41
42 struct libvlc_event_async_queue {
43     struct queue_elmt * elements;
44     vlc_mutex_t lock;
45     vlc_cond_t signal;
46     vlc_thread_t thread;
47     bool is_idle;
48     vlc_cond_t signal_idle;
49     vlc_threadvar_t is_asynch_dispatch_thread_var;
50 };
51
52 /*
53  * Utilities
54  */
55
56 static void*  event_async_loop(void * arg);
57
58 static inline struct libvlc_event_async_queue * queue(libvlc_event_manager_t * p_em)
59 {
60     return p_em->async_event_queue;
61 }
62
63 static inline bool is_queue_initialized(libvlc_event_manager_t * p_em)
64 {
65     return queue(p_em) != NULL;
66 }
67
68 static inline bool current_thread_is_asynch_thread(libvlc_event_manager_t * p_em)
69 {
70     return vlc_threadvar_get(queue(p_em)->is_asynch_dispatch_thread_var);
71 }
72
73 /* Lock must be held */
74 static void push(libvlc_event_manager_t * p_em, libvlc_event_listener_t * listener, libvlc_event_t * event)
75 {
76 #ifndef NDEBUG
77     static const long MaxQueuedItem = 300000;
78     long count = 0;
79 #endif
80
81     struct queue_elmt * elmt = malloc(sizeof(struct queue_elmt));
82     elmt->listener = *listener;
83     elmt->event = *event;
84     elmt->next = NULL;
85
86     /* Append to the end of the queue */
87     struct queue_elmt * iter = queue(p_em)->elements;
88     if(!iter)
89     {
90         queue(p_em)->elements = elmt;
91         return;
92     }
93
94     while (iter->next) {
95         iter = iter->next;
96 #ifndef NDEBUG
97         if(count++ > MaxQueuedItem)
98         {
99             fprintf(stderr, "Warning: libvlc event overflow.\n");
100             abort();
101         }
102 #endif
103     }
104     iter->next = elmt;
105 }
106
107 static inline void queue_lock(libvlc_event_manager_t * p_em)
108 {
109     vlc_mutex_lock(&queue(p_em)->lock);
110 }
111
112 static inline void queue_unlock(libvlc_event_manager_t * p_em)
113 {
114     vlc_mutex_unlock(&queue(p_em)->lock);
115 }
116
117 /* Lock must be held */
118 static bool pop(libvlc_event_manager_t * p_em, libvlc_event_listener_t * listener, libvlc_event_t * event)
119 {
120     if(!queue(p_em)->elements)
121         return false; /* No elements */
122
123     *listener = queue(p_em)->elements->listener;
124     *event = queue(p_em)->elements->event;
125
126     struct queue_elmt * elmt = queue(p_em)->elements;
127     queue(p_em)->elements = elmt->next;
128     free(elmt);
129     return true;
130 }
131
132 /* Lock must be held */
133 static void pop_listener(libvlc_event_manager_t * p_em, libvlc_event_listener_t * listener)
134 {
135     struct queue_elmt * iter = queue(p_em)->elements;
136     struct queue_elmt * prev = NULL;
137     while (iter) {
138         if(listeners_are_equal(&iter->listener, listener))
139         {
140             struct queue_elmt * to_delete = iter;
141             if(!prev)
142                 queue(p_em)->elements = to_delete->next;
143             else
144                 prev->next = to_delete->next;
145             iter = to_delete->next;
146             free(to_delete);
147         }
148         else {
149             prev = iter;
150             iter = iter->next;
151         }
152     }
153 }
154
155 /**************************************************************************
156  *       libvlc_event_async_fini (internal) :
157  *
158  * Destroy what might have been created by.
159  **************************************************************************/
160 void
161 libvlc_event_async_fini(libvlc_event_manager_t * p_em)
162 {
163     if(!is_queue_initialized(p_em)) return;
164
165     if(current_thread_is_asynch_thread(p_em))
166     {
167         fprintf(stderr, "*** Error: releasing the last reference of the observed object from its callback thread is not (yet!) supported\n");
168         abort();
169     }
170
171     vlc_thread_t thread = queue(p_em)->thread;
172     if(thread)
173     {
174         vlc_cancel(thread);
175         vlc_join(thread, NULL);
176     }
177
178     vlc_mutex_destroy(&queue(p_em)->lock);
179     vlc_cond_destroy(&queue(p_em)->signal);
180     vlc_cond_destroy(&queue(p_em)->signal_idle);
181     vlc_threadvar_delete(&queue(p_em)->is_asynch_dispatch_thread_var);
182
183     struct queue_elmt * iter = queue(p_em)->elements;
184     while (iter) {
185         struct queue_elmt * elemt_to_delete = iter;
186         iter = iter->next;
187         free(elemt_to_delete);
188     }
189
190     free(queue(p_em));
191 }
192
193 /**************************************************************************
194  *       libvlc_event_async_init (private) :
195  *
196  * Destroy what might have been created by.
197  **************************************************************************/
198 static void
199 libvlc_event_async_init(libvlc_event_manager_t * p_em)
200 {
201     p_em->async_event_queue = calloc(1, sizeof(struct libvlc_event_async_queue));
202
203     int error = vlc_threadvar_create(&queue(p_em)->is_asynch_dispatch_thread_var, NULL);
204     assert(!error);
205
206     vlc_mutex_init(&queue(p_em)->lock);
207     vlc_cond_init(&queue(p_em)->signal);
208     vlc_cond_init(&queue(p_em)->signal_idle);
209
210     error = vlc_clone (&queue(p_em)->thread, event_async_loop, p_em, VLC_THREAD_PRIORITY_LOW);
211     if(error)
212     {
213         free(p_em->async_event_queue);
214         p_em->async_event_queue = NULL;
215         return;
216     }
217
218 }
219
220 /**************************************************************************
221  *       libvlc_event_async_ensure_listener_removal (internal) :
222  *
223  * Make sure no more message will be issued to the listener.
224  **************************************************************************/
225 void
226 libvlc_event_async_ensure_listener_removal(libvlc_event_manager_t * p_em, libvlc_event_listener_t * listener)
227 {
228     if(!is_queue_initialized(p_em)) return;
229
230     queue_lock(p_em);
231     pop_listener(p_em, listener);
232
233     // Wait for the asynch_loop to have processed all events.
234     if(!current_thread_is_asynch_thread(p_em))
235     {
236         while(!queue(p_em)->is_idle)
237             vlc_cond_wait(&queue(p_em)->signal_idle, &queue(p_em)->lock);
238     }
239     queue_unlock(p_em);
240 }
241
242 /**************************************************************************
243  *       libvlc_event_async_dispatch (internal) :
244  *
245  * Send an event in an asynchronous way.
246  **************************************************************************/
247 void
248 libvlc_event_async_dispatch(libvlc_event_manager_t * p_em, libvlc_event_listener_t * listener, libvlc_event_t * event)
249 {
250     // We do a lazy init here, to prevent constructing the thread when not needed.
251     vlc_mutex_lock(&p_em->object_lock);
252     if(!queue(p_em))
253         libvlc_event_async_init(p_em);
254     vlc_mutex_unlock(&p_em->object_lock);
255
256     queue_lock(p_em);
257     push(p_em, listener, event);
258     vlc_cond_signal(&queue(p_em)->signal);
259     queue_unlock(p_em);
260 }
261
262 /**************************************************************************
263  *       event_async_loop (private) :
264  *
265  * Send queued events.
266  **************************************************************************/
267 static void * event_async_loop(void * arg)
268 {
269     libvlc_event_manager_t * p_em = arg;
270     libvlc_event_listener_t listener;
271     libvlc_event_t event;
272
273     vlc_threadvar_set(queue(p_em)->is_asynch_dispatch_thread_var, p_em);
274
275     queue_lock(p_em);
276     while (true) {
277         int has_listener = pop(p_em, &listener, &event);
278
279         if (has_listener)
280         {
281             queue_unlock(p_em);
282             listener.pf_callback(&event, listener.p_user_data); // This might edit the queue
283             queue_lock(p_em);
284         }
285         else
286         {
287             queue(p_em)->is_idle = true;
288
289             mutex_cleanup_push(&queue(p_em)->lock);
290             vlc_cond_broadcast(&queue(p_em)->signal_idle); // We'll be idle
291             vlc_cond_wait(&queue(p_em)->signal, &queue(p_em)->lock);
292             vlc_cleanup_pop();
293
294             queue(p_em)->is_idle = false;
295         }
296     }
297     queue_unlock(p_em);
298     return NULL;
299 }