]> git.sesse.net Git - vlc/blob - src/control/event_async.c
libvlc: Allow event to be dispatched asynchronously.
[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 /* Lock must be held */
94 static bool pop(libvlc_event_manager_t * p_em, libvlc_event_listener_t * listener, libvlc_event_t * event)
95 {
96     if(!queue(p_em)->elements)
97         return false; /* No elements */
98
99     *listener = queue(p_em)->elements->listener;
100     *event = queue(p_em)->elements->event;
101     
102     struct queue_elmt * elmt = queue(p_em)->elements;
103     queue(p_em)->elements = elmt->next;
104     free(elmt);
105     return true;
106 }
107
108 /* Lock must be held */
109 static void pop_listener(libvlc_event_manager_t * p_em, libvlc_event_listener_t * listener)
110 {
111     struct queue_elmt * iter = queue(p_em)->elements;
112     struct queue_elmt * prev = NULL;
113     while (iter) {
114         if(listeners_are_equal(&iter->listener, listener))
115         {
116             if(!prev)
117                 queue(p_em)->elements = iter->next;
118             else
119                 prev->next = iter->next;
120             free(iter);
121         }
122         prev = iter;
123         iter = iter->next;
124     }
125 }
126
127 /**************************************************************************
128  *       libvlc_event_async_fini (internal) :
129  *
130  * Destroy what might have been created by.
131  **************************************************************************/
132 void
133 libvlc_event_async_fini(libvlc_event_manager_t * p_em)
134 {    
135     if(!is_queue_initialized(p_em)) return;
136     
137     vlc_thread_t thread = queue(p_em)->thread;
138     if(thread)
139     {
140         vlc_cancel(thread);
141         vlc_join(thread, NULL);
142     }
143
144     vlc_mutex_destroy(&queue(p_em)->lock);
145     vlc_cond_destroy(&queue(p_em)->signal);
146
147     struct queue_elmt * iter = queue(p_em)->elements;
148     while (iter) {
149         struct queue_elmt * elemt_to_delete = iter;
150         iter = iter->next;
151         free(elemt_to_delete);
152     }
153     
154     free(queue(p_em));
155 }
156
157 /**************************************************************************
158  *       libvlc_event_async_init (private) :
159  *
160  * Destroy what might have been created by.
161  **************************************************************************/
162 static void
163 libvlc_event_async_init(libvlc_event_manager_t * p_em)
164 {
165     p_em->async_event_queue = calloc(1, sizeof(struct libvlc_event_async_queue));
166
167     int error = vlc_clone (&queue(p_em)->thread, event_async_loop, p_em, VLC_THREAD_PRIORITY_LOW);
168     if(error)
169     {
170         free(p_em->async_event_queue);
171         p_em->async_event_queue = NULL;
172         return;
173     }
174
175     vlc_mutex_init_recursive(&queue(p_em)->lock); // Beware, this is re-entrant
176     vlc_cond_init(&queue(p_em)->signal);
177 }
178
179 /**************************************************************************
180  *       libvlc_event_async_ensure_listener_removal (internal) :
181  *
182  * Make sure no more message will be issued to the listener.
183  **************************************************************************/
184 void
185 libvlc_event_async_ensure_listener_removal(libvlc_event_manager_t * p_em, libvlc_event_listener_t * listener)
186 {
187     if(!is_queue_initialized(p_em)) return;
188
189     vlc_mutex_lock(&queue(p_em)->lock);
190     pop_listener(p_em, listener);
191     vlc_mutex_unlock(&queue(p_em)->lock);
192 }
193
194 /**************************************************************************
195  *       libvlc_event_async_dispatch (internal) :
196  *
197  * Send an event in an asynchronous way.
198  **************************************************************************/
199 void
200 libvlc_event_async_dispatch(libvlc_event_manager_t * p_em, libvlc_event_listener_t * listener, libvlc_event_t * event)
201 {
202     // We do a lazy init here, to prevent constructing the thread when not needed.
203     vlc_mutex_lock(&p_em->object_lock);
204     if(!queue(p_em))
205         libvlc_event_async_init(p_em);
206     vlc_mutex_unlock(&p_em->object_lock);
207
208     vlc_mutex_lock(&queue(p_em)->lock);
209     push(p_em, listener, event);
210     vlc_cond_signal(&queue(p_em)->signal);
211     vlc_mutex_unlock(&queue(p_em)->lock);
212 }
213
214 /**************************************************************************
215  *       event_async_loop (private) :
216  *
217  * Send queued events.
218  **************************************************************************/
219 static void * event_async_loop(void * arg)
220 {
221     libvlc_event_manager_t * p_em = arg;
222     libvlc_event_listener_t listener;
223     libvlc_event_t event;
224
225     vlc_mutex_lock(&queue(p_em)->lock);
226     vlc_cleanup_push(vlc_cleanup_lock, &queue(p_em)->lock);
227     while (true) {
228         int has_listener = pop(p_em, &listener, &event);
229         if (has_listener)
230             listener.pf_callback( &event, listener.p_user_data ); // This might edit the queue, ->lock is recursive
231         else
232             vlc_cond_wait(&queue(p_em)->signal, &queue(p_em)->lock);
233     }
234     vlc_cleanup_pop();
235     vlc_mutex_unlock(&queue(p_em)->lock);
236     return NULL;
237 }