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 /* Lock must be held */
65 static void push(libvlc_event_manager_t * p_em, libvlc_event_listener_t * listener, libvlc_event_t * event)
68 static const long MaxQueuedItem = 300000;
72 struct queue_elmt * elmt = malloc(sizeof(struct queue_elmt));
73 elmt->listener = *listener;
77 /* Append to the end of the queue */
78 struct queue_elmt * iter = queue(p_em)->elements;
81 queue(p_em)->elements = elmt;
88 if(count++ > MaxQueuedItem)
90 fprintf(stderr, "Warning: libvlc event overflow.\n");
98 static inline void queue_lock(libvlc_event_manager_t * p_em)
100 vlc_mutex_lock(&queue(p_em)->lock);
103 static inline void queue_unlock(libvlc_event_manager_t * p_em)
105 vlc_mutex_unlock(&queue(p_em)->lock);
108 /* Lock must be held */
109 static bool pop(libvlc_event_manager_t * p_em, libvlc_event_listener_t * listener, libvlc_event_t * event)
111 if(!queue(p_em)->elements)
112 return false; /* No elements */
114 *listener = queue(p_em)->elements->listener;
115 *event = queue(p_em)->elements->event;
117 struct queue_elmt * elmt = queue(p_em)->elements;
118 queue(p_em)->elements = elmt->next;
123 /* Lock must be held */
124 static void pop_listener(libvlc_event_manager_t * p_em, libvlc_event_listener_t * listener)
126 struct queue_elmt * iter = queue(p_em)->elements;
127 struct queue_elmt * prev = NULL;
129 if(listeners_are_equal(&iter->listener, listener))
132 queue(p_em)->elements = iter->next;
134 prev->next = iter->next;
142 /**************************************************************************
143 * libvlc_event_async_fini (internal) :
145 * Destroy what might have been created by.
146 **************************************************************************/
148 libvlc_event_async_fini(libvlc_event_manager_t * p_em)
150 if(!is_queue_initialized(p_em)) return;
152 vlc_thread_t thread = queue(p_em)->thread;
156 vlc_join(thread, NULL);
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);
164 struct queue_elmt * iter = queue(p_em)->elements;
166 struct queue_elmt * elemt_to_delete = iter;
168 free(elemt_to_delete);
174 /**************************************************************************
175 * libvlc_event_async_init (private) :
177 * Destroy what might have been created by.
178 **************************************************************************/
180 libvlc_event_async_init(libvlc_event_manager_t * p_em)
182 p_em->async_event_queue = calloc(1, sizeof(struct libvlc_event_async_queue));
184 int error = vlc_clone (&queue(p_em)->thread, event_async_loop, p_em, VLC_THREAD_PRIORITY_LOW);
187 free(p_em->async_event_queue);
188 p_em->async_event_queue = NULL;
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);
199 /**************************************************************************
200 * libvlc_event_async_ensure_listener_removal (internal) :
202 * Make sure no more message will be issued to the listener.
203 **************************************************************************/
205 libvlc_event_async_ensure_listener_removal(libvlc_event_manager_t * p_em, libvlc_event_listener_t * listener)
207 if(!is_queue_initialized(p_em)) return;
210 pop_listener(p_em, listener);
212 bool is_asynch_dispatch_thread = vlc_threadvar_get(queue(p_em)->is_asynch_dispatch_thread_var);
214 // Wait for the asynch_loop to have processed all events.
215 if(!queue(p_em)->is_idle && !is_asynch_dispatch_thread)
217 vlc_cond_wait(&queue(p_em)->signal_idle, &queue(p_em)->lock);
218 assert(queue(p_em)->is_idle);
223 /**************************************************************************
224 * libvlc_event_async_dispatch (internal) :
226 * Send an event in an asynchronous way.
227 **************************************************************************/
229 libvlc_event_async_dispatch(libvlc_event_manager_t * p_em, libvlc_event_listener_t * listener, libvlc_event_t * event)
231 // We do a lazy init here, to prevent constructing the thread when not needed.
232 vlc_mutex_lock(&p_em->object_lock);
234 libvlc_event_async_init(p_em);
235 vlc_mutex_unlock(&p_em->object_lock);
238 push(p_em, listener, event);
239 vlc_cond_signal(&queue(p_em)->signal);
243 /**************************************************************************
244 * event_async_loop (private) :
246 * Send queued events.
247 **************************************************************************/
248 static void * event_async_loop(void * arg)
250 libvlc_event_manager_t * p_em = arg;
251 libvlc_event_listener_t listener;
252 libvlc_event_t event;
254 vlc_threadvar_set(queue(p_em)->is_asynch_dispatch_thread_var, (void*)true);
258 int has_listener = pop(p_em, &listener, &event);
263 listener.pf_callback(&event, listener.p_user_data); // This might edit the queue
268 queue(p_em)->is_idle = true;
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);
275 queue(p_em)->is_idle = false;