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