libvlc_version.h: build-time version macros
[vlc/asuraparaju-public.git] / src / control / event_async.c
blob5dce2a3231b203376c18a9be88ea7ecedb3bb953
1 /*****************************************************************************
2 * event.c: New libvlc event control API
3 *****************************************************************************
4 * Copyright (C) 2007 the VideoLAN team
5 * $Id $
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 along
21 * with this program; if not, write to the Free Software Foundation, Inc.,
22 * 51 Franklin Street, Fifth Floor, Boston MA 02110-1301, USA.
23 *****************************************************************************/
25 #ifdef HAVE_CONFIG_H
26 # include "config.h"
27 #endif
29 #include <assert.h>
31 #include <vlc/libvlc.h>
33 #include "libvlc_internal.h"
34 #include "event_internal.h"
36 struct queue_elmt {
37 libvlc_event_listener_t listener;
38 libvlc_event_t event;
39 struct queue_elmt * next;
42 struct libvlc_event_async_queue {
43 struct queue_elmt *first_elmt, *last_elmt;
44 vlc_mutex_t lock;
45 vlc_cond_t signal;
46 vlc_thread_t thread;
47 bool is_idle;
48 vlc_cond_t signal_idle;
49 vlc_threadvar_t is_asynch_dispatch_thread_var;
50 #ifndef NDEBUG
51 long count;
52 #endif
56 * Utilities
59 static void* event_async_loop(void * arg);
61 static inline struct libvlc_event_async_queue * queue(libvlc_event_manager_t * p_em)
63 return p_em->async_event_queue;
66 static inline bool is_queue_initialized(libvlc_event_manager_t * p_em)
68 return queue(p_em) != NULL;
71 static inline bool current_thread_is_asynch_thread(libvlc_event_manager_t * p_em)
73 return vlc_threadvar_get(queue(p_em)->is_asynch_dispatch_thread_var)
74 != NULL;
77 /* Lock must be held */
78 static void push(libvlc_event_manager_t * p_em,
79 libvlc_event_listener_t * listener, libvlc_event_t * event)
81 struct queue_elmt * elmt = malloc(sizeof(struct queue_elmt));
82 elmt->listener = *listener;
83 elmt->event = *event;
84 elmt->next = NULL;
86 /* Append to the end of the queue */
87 if(!queue(p_em)->first_elmt)
88 queue(p_em)->first_elmt = elmt;
89 else
90 queue(p_em)->last_elmt->next = elmt;
91 queue(p_em)->last_elmt = elmt;
93 #ifndef NDEBUG
94 enum { MaxQueueSize = 300000 };
95 if(queue(p_em)->count++ > MaxQueueSize)
97 fprintf(stderr, "Warning: libvlc event overflow.\n");
98 abort();
100 #endif
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,
115 libvlc_event_listener_t * listener, libvlc_event_t * event)
117 if(!queue(p_em)->first_elmt)
118 return false; /* No first_elmt */
120 struct queue_elmt * elmt = queue(p_em)->first_elmt;
121 *listener = elmt->listener;
122 *event = elmt->event;
124 queue(p_em)->first_elmt = elmt->next;
125 if( !elmt->next ) queue(p_em)->last_elmt=NULL;
127 #ifndef NDEBUG
128 queue(p_em)->count--;
129 #endif
131 free(elmt);
132 return true;
135 /* Lock must be held */
136 static void pop_listener(libvlc_event_manager_t * p_em, libvlc_event_listener_t * listener)
138 struct queue_elmt * iter = queue(p_em)->first_elmt;
139 struct queue_elmt * prev = NULL;
140 while (iter) {
141 if(listeners_are_equal(&iter->listener, listener))
143 struct queue_elmt * to_delete = iter;
144 if(!prev)
145 queue(p_em)->first_elmt = to_delete->next;
146 else
147 prev->next = to_delete->next;
148 iter = to_delete->next;
149 free(to_delete);
150 #ifndef NDEBUG
151 queue(p_em)->count--;
152 #endif
154 else {
155 prev = iter;
156 iter = iter->next;
159 queue(p_em)->last_elmt=prev;
162 /**************************************************************************
163 * libvlc_event_async_fini (internal) :
165 * Destroy what might have been created by.
166 **************************************************************************/
167 void
168 libvlc_event_async_fini(libvlc_event_manager_t * p_em)
170 if(!is_queue_initialized(p_em)) return;
172 if(current_thread_is_asynch_thread(p_em))
174 fprintf(stderr, "*** Error: releasing the last reference of the observed object from its callback thread is not (yet!) supported\n");
175 abort();
178 vlc_thread_t thread = queue(p_em)->thread;
179 if(thread)
181 vlc_cancel(thread);
182 vlc_join(thread, NULL);
185 vlc_mutex_destroy(&queue(p_em)->lock);
186 vlc_cond_destroy(&queue(p_em)->signal);
187 vlc_cond_destroy(&queue(p_em)->signal_idle);
188 vlc_threadvar_delete(&queue(p_em)->is_asynch_dispatch_thread_var);
190 struct queue_elmt * iter = queue(p_em)->first_elmt;
191 while (iter) {
192 struct queue_elmt * elemt_to_delete = iter;
193 iter = iter->next;
194 free(elemt_to_delete);
197 free(queue(p_em));
200 /**************************************************************************
201 * libvlc_event_async_init (private) :
203 * Destroy what might have been created by.
204 **************************************************************************/
205 static void
206 libvlc_event_async_init(libvlc_event_manager_t * p_em)
208 p_em->async_event_queue = calloc(1, sizeof(struct libvlc_event_async_queue));
210 int error = vlc_threadvar_create(&queue(p_em)->is_asynch_dispatch_thread_var, NULL);
211 assert(!error);
213 vlc_mutex_init(&queue(p_em)->lock);
214 vlc_cond_init(&queue(p_em)->signal);
215 vlc_cond_init(&queue(p_em)->signal_idle);
217 error = vlc_clone (&queue(p_em)->thread, event_async_loop, p_em, VLC_THREAD_PRIORITY_LOW);
218 if(error)
220 free(p_em->async_event_queue);
221 p_em->async_event_queue = NULL;
222 return;
227 /**************************************************************************
228 * libvlc_event_async_ensure_listener_removal (internal) :
230 * Make sure no more message will be issued to the listener.
231 **************************************************************************/
232 void
233 libvlc_event_async_ensure_listener_removal(libvlc_event_manager_t * p_em, libvlc_event_listener_t * listener)
235 if(!is_queue_initialized(p_em)) return;
237 queue_lock(p_em);
238 pop_listener(p_em, listener);
240 // Wait for the asynch_loop to have processed all events.
241 if(!current_thread_is_asynch_thread(p_em))
243 while(!queue(p_em)->is_idle)
244 vlc_cond_wait(&queue(p_em)->signal_idle, &queue(p_em)->lock);
246 queue_unlock(p_em);
249 /**************************************************************************
250 * libvlc_event_async_dispatch (internal) :
252 * Send an event in an asynchronous way.
253 **************************************************************************/
254 void
255 libvlc_event_async_dispatch(libvlc_event_manager_t * p_em, libvlc_event_listener_t * listener, libvlc_event_t * event)
257 // We do a lazy init here, to prevent constructing the thread when not needed.
258 vlc_mutex_lock(&p_em->object_lock);
259 if(!queue(p_em))
260 libvlc_event_async_init(p_em);
261 vlc_mutex_unlock(&p_em->object_lock);
263 queue_lock(p_em);
264 push(p_em, listener, event);
265 vlc_cond_signal(&queue(p_em)->signal);
266 queue_unlock(p_em);
269 /**************************************************************************
270 * event_async_loop (private) :
272 * Send queued events.
273 **************************************************************************/
274 static void * event_async_loop(void * arg)
276 libvlc_event_manager_t * p_em = arg;
277 libvlc_event_listener_t listener;
278 libvlc_event_t event;
280 vlc_threadvar_set(queue(p_em)->is_asynch_dispatch_thread_var, p_em);
282 queue_lock(p_em);
283 while (true) {
284 int has_listener = pop(p_em, &listener, &event);
286 if (has_listener)
288 queue_unlock(p_em);
289 listener.pf_callback(&event, listener.p_user_data); // This might edit the queue
290 queue_lock(p_em);
292 else
294 queue(p_em)->is_idle = true;
296 mutex_cleanup_push(&queue(p_em)->lock);
297 vlc_cond_broadcast(&queue(p_em)->signal_idle); // We'll be idle
298 vlc_cond_wait(&queue(p_em)->signal, &queue(p_em)->lock);
299 vlc_cleanup_pop();
301 queue(p_em)->is_idle = false;
304 queue_unlock(p_em);
305 return NULL;