]> git.sesse.net Git - vlc/blob - lib/event_async.c
Upnp: remove trailing space
[vlc] / lib / event_async.c
1 /*****************************************************************************
2  * event_async.c: New libvlc event control API
3  *****************************************************************************
4  * Copyright (C) 2007 VLC authors and VideoLAN
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 it
11  * under the terms of the GNU Lesser General Public License as published by
12  * the Free Software Foundation; either version 2.1 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 Lesser General Public License for more details.
19  *
20  * You should have received a copy of the GNU Lesser General Public License
21  * along with this program; if not, write to the Free Software Foundation,
22  * Inc., 51 Franklin Street, Fifth Floor, Boston MA 02110-1301, USA.
23  *****************************************************************************/
24
25 #ifdef HAVE_CONFIG_H
26 # include "config.h"
27 #endif
28
29 #include <assert.h>
30
31 #include <vlc/libvlc.h>
32
33 #include "libvlc_internal.h"
34 #include "event_internal.h"
35
36 struct queue_elmt {
37     libvlc_event_listener_t listener;
38     libvlc_event_t event;
39     struct queue_elmt * next;
40 };
41
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 };
51
52 /*
53  * Utilities
54  */
55
56 static void*  event_async_loop(void * arg);
57
58 static inline struct libvlc_event_async_queue * queue(libvlc_event_manager_t * p_em)
59 {
60     return p_em->async_event_queue;
61 }
62
63 static inline bool is_queue_initialized(libvlc_event_manager_t * p_em)
64 {
65     return queue(p_em) != NULL;
66 }
67
68 static inline bool current_thread_is_asynch_thread(libvlc_event_manager_t * p_em)
69 {
70     return vlc_threadvar_get(queue(p_em)->is_asynch_dispatch_thread_var)
71             != NULL;
72 }
73
74 /* Lock must be held */
75 static void push(libvlc_event_manager_t * p_em,
76                  libvlc_event_listener_t * listener, libvlc_event_t * event)
77 {
78     struct queue_elmt * elmt = malloc(sizeof(struct queue_elmt));
79     elmt->listener = *listener;
80     elmt->event = *event;
81     elmt->next = NULL;
82
83     /* Append to the end of the queue */
84     if(!queue(p_em)->first_elmt)
85         queue(p_em)->first_elmt = elmt;
86     else
87         queue(p_em)->last_elmt->next = elmt;
88     queue(p_em)->last_elmt = elmt;
89 }
90
91 static inline void queue_lock(libvlc_event_manager_t * p_em)
92 {
93     vlc_mutex_lock(&queue(p_em)->lock);
94 }
95
96 static inline void queue_unlock(libvlc_event_manager_t * p_em)
97 {
98     vlc_mutex_unlock(&queue(p_em)->lock);
99 }
100
101 /* Lock must be held */
102 static bool pop(libvlc_event_manager_t * p_em,
103                 libvlc_event_listener_t * listener, libvlc_event_t * event)
104 {
105     if(!queue(p_em)->first_elmt)
106         return false; /* No first_elmt */
107
108     struct queue_elmt * elmt = queue(p_em)->first_elmt;
109     *listener = elmt->listener;
110     *event = elmt->event;
111
112     queue(p_em)->first_elmt = elmt->next;
113     if( !elmt->next ) queue(p_em)->last_elmt=NULL;
114
115     free(elmt);
116     return true;
117 }
118
119 /* Lock must be held */
120 static void pop_listener(libvlc_event_manager_t * p_em, libvlc_event_listener_t * listener)
121 {
122     struct queue_elmt * iter = queue(p_em)->first_elmt;
123     struct queue_elmt * prev = NULL;
124     while (iter) {
125         if(listeners_are_equal(&iter->listener, listener))
126         {
127             struct queue_elmt * to_delete = iter;
128             if(!prev)
129                 queue(p_em)->first_elmt = to_delete->next;
130             else
131                 prev->next = to_delete->next;
132             iter = to_delete->next;
133             free(to_delete);
134         }
135         else {
136             prev = iter;
137             iter = iter->next;
138         }
139     }
140     queue(p_em)->last_elmt=prev;
141 }
142
143 /**************************************************************************
144  *       libvlc_event_async_fini (internal) :
145  *
146  * Destroy what might have been created by.
147  **************************************************************************/
148 void
149 libvlc_event_async_fini(libvlc_event_manager_t * p_em)
150 {
151     if(!is_queue_initialized(p_em)) return;
152
153     if(current_thread_is_asynch_thread(p_em))
154     {
155         fprintf(stderr, "*** Error: releasing the last reference of the observed object from its callback thread is not (yet!) supported\n");
156         abort();
157     }
158
159     vlc_thread_t thread = queue(p_em)->thread;
160     if(thread)
161     {
162         vlc_cancel(thread);
163         vlc_join(thread, NULL);
164     }
165
166     vlc_mutex_destroy(&queue(p_em)->lock);
167     vlc_cond_destroy(&queue(p_em)->signal);
168     vlc_cond_destroy(&queue(p_em)->signal_idle);
169     vlc_threadvar_delete(&queue(p_em)->is_asynch_dispatch_thread_var);
170
171     struct queue_elmt * iter = queue(p_em)->first_elmt;
172     while (iter) {
173         struct queue_elmt * elemt_to_delete = iter;
174         iter = iter->next;
175         free(elemt_to_delete);
176     }
177
178     free(queue(p_em));
179 }
180
181 /**************************************************************************
182  *       libvlc_event_async_init (private) :
183  *
184  * Destroy what might have been created by.
185  **************************************************************************/
186 static void
187 libvlc_event_async_init(libvlc_event_manager_t * p_em)
188 {
189     p_em->async_event_queue = calloc(1, sizeof(struct libvlc_event_async_queue));
190
191     int error = vlc_threadvar_create(&queue(p_em)->is_asynch_dispatch_thread_var, NULL);
192     assert(!error);
193
194     vlc_mutex_init(&queue(p_em)->lock);
195     vlc_cond_init(&queue(p_em)->signal);
196     vlc_cond_init(&queue(p_em)->signal_idle);
197
198     error = vlc_clone (&queue(p_em)->thread, event_async_loop, p_em, VLC_THREAD_PRIORITY_LOW);
199     if(error)
200     {
201         free(p_em->async_event_queue);
202         p_em->async_event_queue = NULL;
203         return;
204     }
205
206 }
207
208 /**************************************************************************
209  *       libvlc_event_async_ensure_listener_removal (internal) :
210  *
211  * Make sure no more message will be issued to the listener.
212  **************************************************************************/
213 void
214 libvlc_event_async_ensure_listener_removal(libvlc_event_manager_t * p_em, libvlc_event_listener_t * listener)
215 {
216     if(!is_queue_initialized(p_em)) return;
217
218     queue_lock(p_em);
219     pop_listener(p_em, listener);
220
221     // Wait for the asynch_loop to have processed all events.
222     if(!current_thread_is_asynch_thread(p_em))
223     {
224         while(!queue(p_em)->is_idle)
225             vlc_cond_wait(&queue(p_em)->signal_idle, &queue(p_em)->lock);
226     }
227     queue_unlock(p_em);
228 }
229
230 /**************************************************************************
231  *       libvlc_event_async_dispatch (internal) :
232  *
233  * Send an event in an asynchronous way.
234  **************************************************************************/
235 void
236 libvlc_event_async_dispatch(libvlc_event_manager_t * p_em, libvlc_event_listener_t * listener, libvlc_event_t * event)
237 {
238     // We do a lazy init here, to prevent constructing the thread when not needed.
239     vlc_mutex_lock(&p_em->object_lock);
240     if(!queue(p_em))
241         libvlc_event_async_init(p_em);
242     vlc_mutex_unlock(&p_em->object_lock);
243
244     queue_lock(p_em);
245     push(p_em, listener, event);
246     vlc_cond_signal(&queue(p_em)->signal);
247     queue_unlock(p_em);
248 }
249
250 /**************************************************************************
251  *       event_async_loop (private) :
252  *
253  * Send queued events.
254  **************************************************************************/
255 static void * event_async_loop(void * arg)
256 {
257     libvlc_event_manager_t * p_em = arg;
258     libvlc_event_listener_t listener;
259     libvlc_event_t event;
260
261     vlc_threadvar_set(queue(p_em)->is_asynch_dispatch_thread_var, p_em);
262
263     queue_lock(p_em);
264     while (true) {
265         int has_listener = pop(p_em, &listener, &event);
266
267         if (has_listener)
268         {
269             queue_unlock(p_em);
270             listener.pf_callback(&event, listener.p_user_data); // This might edit the queue
271             queue_lock(p_em);
272         }
273         else
274         {
275             queue(p_em)->is_idle = true;
276
277             mutex_cleanup_push(&queue(p_em)->lock);
278             vlc_cond_broadcast(&queue(p_em)->signal_idle); // We'll be idle
279             vlc_cond_wait(&queue(p_em)->signal, &queue(p_em)->lock);
280             vlc_cleanup_pop();
281
282             queue(p_em)->is_idle = false;
283         }
284     }
285     queue_unlock(p_em);
286     return NULL;
287 }