1 /*****************************************************************************
2 * event.c: New libvlc event control API
3 *****************************************************************************
4 * Copyright (C) 2007 the VideoLAN team
7 * Authors: Filippo Carone <filippo@carone.org>
8 * Pierre d'Herbemont <pdherbemont # videolan.org>
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.
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.
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 *****************************************************************************/
27 #include <vlc/libvlc.h>
29 #include "libvlc_internal.h"
30 #include "event_internal.h"
33 libvlc_event_listener_t listener;
35 struct queue_elmt * next;
38 struct libvlc_event_async_queue {
39 struct queue_elmt * elements;
44 vlc_cond_t signal_idle;
45 vlc_threadvar_t is_asynch_dispatch_thread_var;
52 static void* event_async_loop(void * arg);
54 static inline struct libvlc_event_async_queue * queue(libvlc_event_manager_t * p_em)
56 return p_em->async_event_queue;
59 static inline bool is_queue_initialized(libvlc_event_manager_t * p_em)
61 return queue(p_em) != NULL;
64 static inline bool current_thread_is_asynch_thread(libvlc_event_manager_t * p_em)
66 return vlc_threadvar_get(queue(p_em)->is_asynch_dispatch_thread_var);
69 /* Lock must be held */
70 static void push(libvlc_event_manager_t * p_em, libvlc_event_listener_t * listener, libvlc_event_t * event)
73 static const long MaxQueuedItem = 300000;
77 struct queue_elmt * elmt = malloc(sizeof(struct queue_elmt));
78 elmt->listener = *listener;
82 /* Append to the end of the queue */
83 struct queue_elmt * iter = queue(p_em)->elements;
86 queue(p_em)->elements = elmt;
93 if(count++ > MaxQueuedItem)
95 fprintf(stderr, "Warning: libvlc event overflow.\n");
103 static inline void queue_lock(libvlc_event_manager_t * p_em)
105 vlc_mutex_lock(&queue(p_em)->lock);
108 static inline void queue_unlock(libvlc_event_manager_t * p_em)
110 vlc_mutex_unlock(&queue(p_em)->lock);
113 /* Lock must be held */
114 static bool pop(libvlc_event_manager_t * p_em, libvlc_event_listener_t * listener, libvlc_event_t * event)
116 if(!queue(p_em)->elements)
117 return false; /* No elements */
119 *listener = queue(p_em)->elements->listener;
120 *event = queue(p_em)->elements->event;
122 struct queue_elmt * elmt = queue(p_em)->elements;
123 queue(p_em)->elements = elmt->next;
128 /* Lock must be held */
129 static void pop_listener(libvlc_event_manager_t * p_em, libvlc_event_listener_t * listener)
131 struct queue_elmt * iter = queue(p_em)->elements;
132 struct queue_elmt * prev = NULL;
134 if(listeners_are_equal(&iter->listener, listener))
136 struct queue_elmt * to_delete = iter;
138 queue(p_em)->elements = to_delete->next;
140 prev->next = to_delete->next;
141 iter = to_delete->next;
151 /**************************************************************************
152 * libvlc_event_async_fini (internal) :
154 * Destroy what might have been created by.
155 **************************************************************************/
157 libvlc_event_async_fini(libvlc_event_manager_t * p_em)
159 if(!is_queue_initialized(p_em)) return;
161 if(current_thread_is_asynch_thread(p_em))
163 fprintf(stderr, "*** Error: releasing the last reference of the observed object from its callback thread is not (yet!) supported\n");
167 vlc_thread_t thread = queue(p_em)->thread;
171 vlc_join(thread, NULL);
174 vlc_mutex_destroy(&queue(p_em)->lock);
175 vlc_cond_destroy(&queue(p_em)->signal);
176 vlc_cond_destroy(&queue(p_em)->signal_idle);
177 vlc_threadvar_delete(&queue(p_em)->is_asynch_dispatch_thread_var);
179 struct queue_elmt * iter = queue(p_em)->elements;
181 struct queue_elmt * elemt_to_delete = iter;
183 free(elemt_to_delete);
189 /**************************************************************************
190 * libvlc_event_async_init (private) :
192 * Destroy what might have been created by.
193 **************************************************************************/
195 libvlc_event_async_init(libvlc_event_manager_t * p_em)
197 p_em->async_event_queue = calloc(1, sizeof(struct libvlc_event_async_queue));
199 int error = vlc_threadvar_create(&queue(p_em)->is_asynch_dispatch_thread_var, NULL);
202 vlc_mutex_init(&queue(p_em)->lock);
203 vlc_cond_init(&queue(p_em)->signal);
204 vlc_cond_init(&queue(p_em)->signal_idle);
206 error = vlc_clone (&queue(p_em)->thread, event_async_loop, p_em, VLC_THREAD_PRIORITY_LOW);
209 free(p_em->async_event_queue);
210 p_em->async_event_queue = NULL;
216 /**************************************************************************
217 * libvlc_event_async_ensure_listener_removal (internal) :
219 * Make sure no more message will be issued to the listener.
220 **************************************************************************/
222 libvlc_event_async_ensure_listener_removal(libvlc_event_manager_t * p_em, libvlc_event_listener_t * listener)
224 if(!is_queue_initialized(p_em)) return;
227 pop_listener(p_em, listener);
229 // Wait for the asynch_loop to have processed all events.
230 if(!current_thread_is_asynch_thread(p_em))
232 while(!queue(p_em)->is_idle)
233 vlc_cond_wait(&queue(p_em)->signal_idle, &queue(p_em)->lock);
238 /**************************************************************************
239 * libvlc_event_async_dispatch (internal) :
241 * Send an event in an asynchronous way.
242 **************************************************************************/
244 libvlc_event_async_dispatch(libvlc_event_manager_t * p_em, libvlc_event_listener_t * listener, libvlc_event_t * event)
246 // We do a lazy init here, to prevent constructing the thread when not needed.
247 vlc_mutex_lock(&p_em->object_lock);
249 libvlc_event_async_init(p_em);
250 vlc_mutex_unlock(&p_em->object_lock);
253 push(p_em, listener, event);
254 vlc_cond_signal(&queue(p_em)->signal);
258 /**************************************************************************
259 * event_async_loop (private) :
261 * Send queued events.
262 **************************************************************************/
263 static void * event_async_loop(void * arg)
265 libvlc_event_manager_t * p_em = arg;
266 libvlc_event_listener_t listener;
267 libvlc_event_t event;
269 vlc_threadvar_set(queue(p_em)->is_asynch_dispatch_thread_var, p_em);
273 int has_listener = pop(p_em, &listener, &event);
278 listener.pf_callback(&event, listener.p_user_data); // This might edit the queue
283 queue(p_em)->is_idle = true;
285 mutex_cleanup_push(&queue(p_em)->lock);
286 vlc_cond_broadcast(&queue(p_em)->signal_idle); // We'll be idle
287 vlc_cond_wait(&queue(p_em)->signal, &queue(p_em)->lock);
290 queue(p_em)->is_idle = false;