]> git.sesse.net Git - vlc/blob - src/control/event_async.c
libvlc: Get rid of the recursive lock.
[vlc] / src / control / event_async.c
1 /*****************************************************************************
2  * event.c: New libvlc event control API
3  *****************************************************************************
4  * Copyright (C) 2007 the VideoLAN team
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
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.
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 General Public License for more details.
19  *
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  *****************************************************************************/
24
25 #include <vlc/libvlc.h>
26
27 #include "libvlc_internal.h"
28 #include "event_internal.h"
29
30 struct queue_elmt {
31     libvlc_event_listener_t listener;
32     libvlc_event_t event;
33     struct queue_elmt * next;
34 };
35
36 struct libvlc_event_async_queue {
37     struct queue_elmt * elements;
38     vlc_mutex_t lock;
39     vlc_cond_t signal;
40     vlc_thread_t thread;
41 };
42
43 /*
44  * Utilities
45  */
46
47 static void*  event_async_loop(void * arg);
48
49 static inline struct libvlc_event_async_queue * queue(libvlc_event_manager_t * p_em)
50 {
51     return p_em->async_event_queue;
52 }
53
54 static inline bool is_queue_initialized(libvlc_event_manager_t * p_em)
55 {
56     return queue(p_em) != NULL;
57 }
58
59 /* Lock must be held */
60 static void push(libvlc_event_manager_t * p_em, libvlc_event_listener_t * listener, libvlc_event_t * event)
61 {
62 #ifndef NDEBUG
63     static const long MaxQueuedItem = 300000;
64     long count = 0;
65 #endif
66     
67     struct queue_elmt * elmt = malloc(sizeof(struct queue_elmt));
68     elmt->listener = *listener;
69     elmt->event = *event;
70     elmt->next = NULL;
71     
72     /* Append to the end of the queue */
73     struct queue_elmt * iter = queue(p_em)->elements;
74     if(!iter)
75     {
76         queue(p_em)->elements = elmt;
77         return;
78     }
79
80     while (iter->next) {
81         iter = iter->next;
82 #ifndef NDEBUG
83         if(count++ > MaxQueuedItem)
84         {
85             fprintf(stderr, "Warning: libvlc event overflow.\n");
86             abort();
87         }
88 #endif
89     }
90     iter->next = elmt;
91 }
92
93 static inline void queue_lock(libvlc_event_manager_t * p_em)
94 {
95     vlc_mutex_lock(&queue(p_em)->lock);
96 }
97
98 static inline void queue_unlock(libvlc_event_manager_t * p_em)
99 {
100     vlc_mutex_unlock(&queue(p_em)->lock);
101 }
102
103 /* Lock must be held */
104 static bool pop(libvlc_event_manager_t * p_em, libvlc_event_listener_t * listener, libvlc_event_t * event)
105 {
106     if(!queue(p_em)->elements)
107         return false; /* No elements */
108
109     *listener = queue(p_em)->elements->listener;
110     *event = queue(p_em)->elements->event;
111     
112     struct queue_elmt * elmt = queue(p_em)->elements;
113     queue(p_em)->elements = elmt->next;
114     free(elmt);
115     return true;
116 }
117
118 /* Lock must be held */
119 static void pop_listener(libvlc_event_manager_t * p_em, libvlc_event_listener_t * listener)
120 {
121     struct queue_elmt * iter = queue(p_em)->elements;
122     struct queue_elmt * prev = NULL;
123     while (iter) {
124         if(listeners_are_equal(&iter->listener, listener))
125         {
126             if(!prev)
127                 queue(p_em)->elements = iter->next;
128             else
129                 prev->next = iter->next;
130             free(iter);
131         }
132         prev = iter;
133         iter = iter->next;
134     }
135 }
136
137 /**************************************************************************
138  *       libvlc_event_async_fini (internal) :
139  *
140  * Destroy what might have been created by.
141  **************************************************************************/
142 void
143 libvlc_event_async_fini(libvlc_event_manager_t * p_em)
144 {    
145     if(!is_queue_initialized(p_em)) return;
146     
147     vlc_thread_t thread = queue(p_em)->thread;
148     if(thread)
149     {
150         vlc_cancel(thread);
151         vlc_join(thread, NULL);
152     }
153
154     vlc_mutex_destroy(&queue(p_em)->lock);
155     vlc_cond_destroy(&queue(p_em)->signal);
156
157     struct queue_elmt * iter = queue(p_em)->elements;
158     while (iter) {
159         struct queue_elmt * elemt_to_delete = iter;
160         iter = iter->next;
161         free(elemt_to_delete);
162     }
163     
164     free(queue(p_em));
165 }
166
167 /**************************************************************************
168  *       libvlc_event_async_init (private) :
169  *
170  * Destroy what might have been created by.
171  **************************************************************************/
172 static void
173 libvlc_event_async_init(libvlc_event_manager_t * p_em)
174 {
175     p_em->async_event_queue = calloc(1, sizeof(struct libvlc_event_async_queue));
176
177     int error = vlc_clone (&queue(p_em)->thread, event_async_loop, p_em, VLC_THREAD_PRIORITY_LOW);
178     if(error)
179     {
180         free(p_em->async_event_queue);
181         p_em->async_event_queue = NULL;
182         return;
183     }
184
185     vlc_mutex_init(&queue(p_em)->lock);
186     vlc_cond_init(&queue(p_em)->signal);
187 }
188
189 /**************************************************************************
190  *       libvlc_event_async_ensure_listener_removal (internal) :
191  *
192  * Make sure no more message will be issued to the listener.
193  **************************************************************************/
194 void
195 libvlc_event_async_ensure_listener_removal(libvlc_event_manager_t * p_em, libvlc_event_listener_t * listener)
196 {
197     if(!is_queue_initialized(p_em)) return;
198
199     queue_lock(p_em);
200     pop_listener(p_em, listener);
201     queue_unlock(p_em);
202 }
203
204 /**************************************************************************
205  *       libvlc_event_async_dispatch (internal) :
206  *
207  * Send an event in an asynchronous way.
208  **************************************************************************/
209 void
210 libvlc_event_async_dispatch(libvlc_event_manager_t * p_em, libvlc_event_listener_t * listener, libvlc_event_t * event)
211 {
212     // We do a lazy init here, to prevent constructing the thread when not needed.
213     vlc_mutex_lock(&p_em->object_lock);
214     if(!queue(p_em))
215         libvlc_event_async_init(p_em);
216     vlc_mutex_unlock(&p_em->object_lock);
217
218     queue_lock(p_em);
219     push(p_em, listener, event);
220     vlc_cond_signal(&queue(p_em)->signal);
221     queue_unlock(p_em);
222 }
223
224 /**************************************************************************
225  *       event_async_loop (private) :
226  *
227  * Send queued events.
228  **************************************************************************/
229 static void * event_async_loop(void * arg)
230 {
231     libvlc_event_manager_t * p_em = arg;
232     libvlc_event_listener_t listener;
233     libvlc_event_t event;
234
235     queue_lock(p_em);
236     while (true) {
237         int has_listener = pop(p_em, &listener, &event);
238
239         if (has_listener)
240         {
241             queue_unlock(p_em);
242             listener.pf_callback(&event, listener.p_user_data); // This might edit the queue
243             queue_lock(p_em);
244         }
245         else
246         {
247             mutex_cleanup_push(&queue(p_em)->lock);
248             vlc_cond_wait(&queue(p_em)->signal, &queue(p_em)->lock);
249             vlc_cleanup_pop();
250         }
251     }
252     queue_unlock(p_em);
253     return NULL;
254 }