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 *****************************************************************************/
31 #include <vlc/libvlc.h>
33 #include "libvlc_internal.h"
34 #include "event_internal.h"
37 libvlc_event_listener_t listener;
39 struct queue_elmt * next;
42 struct libvlc_event_async_queue {
43 struct queue_elmt * elements;
48 vlc_cond_t signal_idle;
49 vlc_threadvar_t is_asynch_dispatch_thread_var;
56 static void* event_async_loop(void * arg);
58 static inline struct libvlc_event_async_queue * queue(libvlc_event_manager_t * p_em)
60 return p_em->async_event_queue;
63 static inline bool is_queue_initialized(libvlc_event_manager_t * p_em)
65 return queue(p_em) != NULL;
68 static inline bool current_thread_is_asynch_thread(libvlc_event_manager_t * p_em)
70 return vlc_threadvar_get(queue(p_em)->is_asynch_dispatch_thread_var);
73 /* Lock must be held */
74 static void push(libvlc_event_manager_t * p_em, libvlc_event_listener_t * listener, libvlc_event_t * event)
77 static const long MaxQueuedItem = 300000;
81 struct queue_elmt * elmt = malloc(sizeof(struct queue_elmt));
82 elmt->listener = *listener;
86 /* Append to the end of the queue */
87 struct queue_elmt * iter = queue(p_em)->elements;
90 queue(p_em)->elements = elmt;
97 if(count++ > MaxQueuedItem)
99 fprintf(stderr, "Warning: libvlc event overflow.\n");
107 static inline void queue_lock(libvlc_event_manager_t * p_em)
109 vlc_mutex_lock(&queue(p_em)->lock);
112 static inline void queue_unlock(libvlc_event_manager_t * p_em)
114 vlc_mutex_unlock(&queue(p_em)->lock);
117 /* Lock must be held */
118 static bool pop(libvlc_event_manager_t * p_em, libvlc_event_listener_t * listener, libvlc_event_t * event)
120 if(!queue(p_em)->elements)
121 return false; /* No elements */
123 *listener = queue(p_em)->elements->listener;
124 *event = queue(p_em)->elements->event;
126 struct queue_elmt * elmt = queue(p_em)->elements;
127 queue(p_em)->elements = elmt->next;
132 /* Lock must be held */
133 static void pop_listener(libvlc_event_manager_t * p_em, libvlc_event_listener_t * listener)
135 struct queue_elmt * iter = queue(p_em)->elements;
136 struct queue_elmt * prev = NULL;
138 if(listeners_are_equal(&iter->listener, listener))
140 struct queue_elmt * to_delete = iter;
142 queue(p_em)->elements = to_delete->next;
144 prev->next = to_delete->next;
145 iter = to_delete->next;
155 /**************************************************************************
156 * libvlc_event_async_fini (internal) :
158 * Destroy what might have been created by.
159 **************************************************************************/
161 libvlc_event_async_fini(libvlc_event_manager_t * p_em)
163 if(!is_queue_initialized(p_em)) return;
165 if(current_thread_is_asynch_thread(p_em))
167 fprintf(stderr, "*** Error: releasing the last reference of the observed object from its callback thread is not (yet!) supported\n");
171 vlc_thread_t thread = queue(p_em)->thread;
175 vlc_join(thread, NULL);
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);
183 struct queue_elmt * iter = queue(p_em)->elements;
185 struct queue_elmt * elemt_to_delete = iter;
187 free(elemt_to_delete);
193 /**************************************************************************
194 * libvlc_event_async_init (private) :
196 * Destroy what might have been created by.
197 **************************************************************************/
199 libvlc_event_async_init(libvlc_event_manager_t * p_em)
201 p_em->async_event_queue = calloc(1, sizeof(struct libvlc_event_async_queue));
203 int error = vlc_threadvar_create(&queue(p_em)->is_asynch_dispatch_thread_var, NULL);
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);
210 error = vlc_clone (&queue(p_em)->thread, event_async_loop, p_em, VLC_THREAD_PRIORITY_LOW);
213 free(p_em->async_event_queue);
214 p_em->async_event_queue = NULL;
220 /**************************************************************************
221 * libvlc_event_async_ensure_listener_removal (internal) :
223 * Make sure no more message will be issued to the listener.
224 **************************************************************************/
226 libvlc_event_async_ensure_listener_removal(libvlc_event_manager_t * p_em, libvlc_event_listener_t * listener)
228 if(!is_queue_initialized(p_em)) return;
231 pop_listener(p_em, listener);
233 // Wait for the asynch_loop to have processed all events.
234 if(!current_thread_is_asynch_thread(p_em))
236 while(!queue(p_em)->is_idle)
237 vlc_cond_wait(&queue(p_em)->signal_idle, &queue(p_em)->lock);
242 /**************************************************************************
243 * libvlc_event_async_dispatch (internal) :
245 * Send an event in an asynchronous way.
246 **************************************************************************/
248 libvlc_event_async_dispatch(libvlc_event_manager_t * p_em, libvlc_event_listener_t * listener, libvlc_event_t * event)
250 // We do a lazy init here, to prevent constructing the thread when not needed.
251 vlc_mutex_lock(&p_em->object_lock);
253 libvlc_event_async_init(p_em);
254 vlc_mutex_unlock(&p_em->object_lock);
257 push(p_em, listener, event);
258 vlc_cond_signal(&queue(p_em)->signal);
262 /**************************************************************************
263 * event_async_loop (private) :
265 * Send queued events.
266 **************************************************************************/
267 static void * event_async_loop(void * arg)
269 libvlc_event_manager_t * p_em = arg;
270 libvlc_event_listener_t listener;
271 libvlc_event_t event;
273 vlc_threadvar_set(queue(p_em)->is_asynch_dispatch_thread_var, p_em);
277 int has_listener = pop(p_em, &listener, &event);
282 listener.pf_callback(&event, listener.p_user_data); // This might edit the queue
287 queue(p_em)->is_idle = true;
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);
294 queue(p_em)->is_idle = false;