]> git.sesse.net Git - vlc/blob - src/control/event_async.c
libvlc: cond_wait can wake up without being signaled.
[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 #include <assert.h>
26
27 #include <vlc/libvlc.h>
28
29 #include "libvlc_internal.h"
30 #include "event_internal.h"
31
32 struct queue_elmt {
33     libvlc_event_listener_t listener;
34     libvlc_event_t event;
35     struct queue_elmt * next;
36 };
37
38 struct libvlc_event_async_queue {
39     struct queue_elmt * elements;
40     vlc_mutex_t lock;
41     vlc_cond_t signal;
42     vlc_thread_t thread;
43     bool is_idle;
44     vlc_cond_t signal_idle;
45     vlc_threadvar_t is_asynch_dispatch_thread_var;
46 };
47
48 /*
49  * Utilities
50  */
51
52 static void*  event_async_loop(void * arg);
53
54 static inline struct libvlc_event_async_queue * queue(libvlc_event_manager_t * p_em)
55 {
56     return p_em->async_event_queue;
57 }
58
59 static inline bool is_queue_initialized(libvlc_event_manager_t * p_em)
60 {
61     return queue(p_em) != NULL;
62 }
63
64 static inline bool current_thread_is_asynch_thread(libvlc_event_manager_t * p_em)
65 {
66     return vlc_threadvar_get(queue(p_em)->is_asynch_dispatch_thread_var);
67 }
68
69 /* Lock must be held */
70 static void push(libvlc_event_manager_t * p_em, libvlc_event_listener_t * listener, libvlc_event_t * event)
71 {
72 #ifndef NDEBUG
73     static const long MaxQueuedItem = 300000;
74     long count = 0;
75 #endif
76     
77     struct queue_elmt * elmt = malloc(sizeof(struct queue_elmt));
78     elmt->listener = *listener;
79     elmt->event = *event;
80     elmt->next = NULL;
81     
82     /* Append to the end of the queue */
83     struct queue_elmt * iter = queue(p_em)->elements;
84     if(!iter)
85     {
86         queue(p_em)->elements = elmt;
87         return;
88     }
89
90     while (iter->next) {
91         iter = iter->next;
92 #ifndef NDEBUG
93         if(count++ > MaxQueuedItem)
94         {
95             fprintf(stderr, "Warning: libvlc event overflow.\n");
96             abort();
97         }
98 #endif
99     }
100     iter->next = elmt;
101 }
102
103 static inline void queue_lock(libvlc_event_manager_t * p_em)
104 {
105     vlc_mutex_lock(&queue(p_em)->lock);
106 }
107
108 static inline void queue_unlock(libvlc_event_manager_t * p_em)
109 {
110     vlc_mutex_unlock(&queue(p_em)->lock);
111 }
112
113 /* Lock must be held */
114 static bool pop(libvlc_event_manager_t * p_em, libvlc_event_listener_t * listener, libvlc_event_t * event)
115 {
116     if(!queue(p_em)->elements)
117         return false; /* No elements */
118
119     *listener = queue(p_em)->elements->listener;
120     *event = queue(p_em)->elements->event;
121     
122     struct queue_elmt * elmt = queue(p_em)->elements;
123     queue(p_em)->elements = elmt->next;
124     free(elmt);
125     return true;
126 }
127
128 /* Lock must be held */
129 static void pop_listener(libvlc_event_manager_t * p_em, libvlc_event_listener_t * listener)
130 {
131     struct queue_elmt * iter = queue(p_em)->elements;
132     struct queue_elmt * prev = NULL;
133     while (iter) {
134         if(listeners_are_equal(&iter->listener, listener))
135         {
136             if(!prev)
137                 queue(p_em)->elements = iter->next;
138             else
139                 prev->next = iter->next;
140             free(iter);
141         }
142         prev = iter;
143         iter = iter->next;
144     }
145 }
146
147 /**************************************************************************
148  *       libvlc_event_async_fini (internal) :
149  *
150  * Destroy what might have been created by.
151  **************************************************************************/
152 void
153 libvlc_event_async_fini(libvlc_event_manager_t * p_em)
154 {    
155     if(!is_queue_initialized(p_em)) return;
156     
157     vlc_thread_t thread = queue(p_em)->thread;
158     if(thread)
159     {
160         vlc_cancel(thread);
161         vlc_join(thread, NULL);
162     }
163
164     vlc_mutex_destroy(&queue(p_em)->lock);
165     vlc_cond_destroy(&queue(p_em)->signal);
166     vlc_cond_destroy(&queue(p_em)->signal_idle);
167     vlc_threadvar_delete(&queue(p_em)->is_asynch_dispatch_thread_var);
168
169     struct queue_elmt * iter = queue(p_em)->elements;
170     while (iter) {
171         struct queue_elmt * elemt_to_delete = iter;
172         iter = iter->next;
173         free(elemt_to_delete);
174     }
175     
176     free(queue(p_em));
177 }
178
179 /**************************************************************************
180  *       libvlc_event_async_init (private) :
181  *
182  * Destroy what might have been created by.
183  **************************************************************************/
184 static void
185 libvlc_event_async_init(libvlc_event_manager_t * p_em)
186 {
187     p_em->async_event_queue = calloc(1, sizeof(struct libvlc_event_async_queue));
188
189     int error = vlc_clone (&queue(p_em)->thread, event_async_loop, p_em, VLC_THREAD_PRIORITY_LOW);
190     if(error)
191     {
192         free(p_em->async_event_queue);
193         p_em->async_event_queue = NULL;
194         return;
195     }
196
197     vlc_mutex_init(&queue(p_em)->lock);
198     vlc_cond_init(&queue(p_em)->signal);
199     vlc_cond_init(&queue(p_em)->signal_idle);
200     error = vlc_threadvar_create(&queue(p_em)->is_asynch_dispatch_thread_var, NULL);
201     assert(!error);
202 }
203
204 /**************************************************************************
205  *       libvlc_event_async_ensure_listener_removal (internal) :
206  *
207  * Make sure no more message will be issued to the listener.
208  **************************************************************************/
209 void
210 libvlc_event_async_ensure_listener_removal(libvlc_event_manager_t * p_em, libvlc_event_listener_t * listener)
211 {
212     if(!is_queue_initialized(p_em)) return;
213
214     queue_lock(p_em);
215     pop_listener(p_em, listener);
216     
217     // Wait for the asynch_loop to have processed all events.
218     if(!current_thread_is_asynch_thread(p_em))
219     {
220         while(!queue(p_em)->is_idle)
221             vlc_cond_wait(&queue(p_em)->signal_idle, &queue(p_em)->lock);
222     }
223     queue_unlock(p_em);
224 }
225
226 /**************************************************************************
227  *       libvlc_event_async_dispatch (internal) :
228  *
229  * Send an event in an asynchronous way.
230  **************************************************************************/
231 void
232 libvlc_event_async_dispatch(libvlc_event_manager_t * p_em, libvlc_event_listener_t * listener, libvlc_event_t * event)
233 {
234     // We do a lazy init here, to prevent constructing the thread when not needed.
235     vlc_mutex_lock(&p_em->object_lock);
236     if(!queue(p_em))
237         libvlc_event_async_init(p_em);
238     vlc_mutex_unlock(&p_em->object_lock);
239
240     queue_lock(p_em);
241     push(p_em, listener, event);
242     vlc_cond_signal(&queue(p_em)->signal);
243     queue_unlock(p_em);
244 }
245
246 /**************************************************************************
247  *       event_async_loop (private) :
248  *
249  * Send queued events.
250  **************************************************************************/
251 static void * event_async_loop(void * arg)
252 {
253     libvlc_event_manager_t * p_em = arg;
254     libvlc_event_listener_t listener;
255     libvlc_event_t event;
256
257     vlc_threadvar_set(queue(p_em)->is_asynch_dispatch_thread_var, (void*)true);
258
259     queue_lock(p_em);
260     while (true) {
261         int has_listener = pop(p_em, &listener, &event);
262
263         if (has_listener)
264         {
265             queue_unlock(p_em);
266             listener.pf_callback(&event, listener.p_user_data); // This might edit the queue
267             queue_lock(p_em);
268         }
269         else
270         {
271             queue(p_em)->is_idle = true;
272
273             mutex_cleanup_push(&queue(p_em)->lock);
274             vlc_cond_broadcast(&queue(p_em)->signal_idle); // We'll be idle
275             vlc_cond_wait(&queue(p_em)->signal, &queue(p_em)->lock);
276             vlc_cleanup_pop();
277             
278             queue(p_em)->is_idle = false;
279         }
280     }
281     queue_unlock(p_em);
282     return NULL;
283 }