chromecast: add protobuf file
[vlc.git] / include / vlc_threads.h
blob00435cb68952591b4fb4938d271387ed5165426b
1 /*****************************************************************************
2 * vlc_threads.h : threads implementation for the VideoLAN client
3 * This header provides portable declarations for mutexes & conditions
4 *****************************************************************************
5 * Copyright (C) 1999, 2002 VLC authors and VideoLAN
6 * Copyright © 2007-2008 Rémi Denis-Courmont
8 * Authors: Jean-Marc Dressler <polux@via.ecp.fr>
9 * Samuel Hocevar <sam@via.ecp.fr>
10 * Gildas Bazin <gbazin@netcourrier.com>
11 * Christophe Massiot <massiot@via.ecp.fr>
13 * This program is free software; you can redistribute it and/or modify it
14 * under the terms of the GNU Lesser General Public License as published by
15 * the Free Software Foundation; either version 2.1 of the License, or
16 * (at your option) any later version.
18 * This program is distributed in the hope that it will be useful,
19 * but WITHOUT ANY WARRANTY; without even the implied warranty of
20 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
21 * GNU Lesser General Public License for more details.
23 * You should have received a copy of the GNU Lesser General Public License
24 * along with this program; if not, write to the Free Software Foundation,
25 * Inc., 51 Franklin Street, Fifth Floor, Boston MA 02110-1301, USA.
26 *****************************************************************************/
28 #ifndef VLC_THREADS_H_
29 #define VLC_THREADS_H_
31 /**
32 * \file
33 * This file defines structures and functions for handling threads in vlc
37 #if defined (_WIN32)
38 # include <process.h>
39 # ifndef ETIMEDOUT
40 # define ETIMEDOUT 10060 /* This is the value in winsock.h. */
41 # endif
43 typedef struct vlc_thread *vlc_thread_t;
44 typedef struct
46 bool dynamic;
47 union
49 struct
51 bool locked;
52 unsigned long contention;
54 CRITICAL_SECTION mutex;
56 } vlc_mutex_t;
57 #define VLC_STATIC_MUTEX { false, { { false, 0 } } }
58 typedef struct
60 HANDLE handle;
61 unsigned clock;
62 } vlc_cond_t;
63 #define VLC_STATIC_COND { 0, 0 }
64 typedef HANDLE vlc_sem_t;
65 #define LIBVLC_NEED_RWLOCK
66 typedef struct vlc_threadvar *vlc_threadvar_t;
67 typedef struct vlc_timer *vlc_timer_t;
69 # define VLC_THREAD_PRIORITY_LOW 0
70 # define VLC_THREAD_PRIORITY_INPUT THREAD_PRIORITY_ABOVE_NORMAL
71 # define VLC_THREAD_PRIORITY_AUDIO THREAD_PRIORITY_HIGHEST
72 # define VLC_THREAD_PRIORITY_VIDEO 0
73 # define VLC_THREAD_PRIORITY_OUTPUT THREAD_PRIORITY_ABOVE_NORMAL
74 # define VLC_THREAD_PRIORITY_HIGHEST THREAD_PRIORITY_TIME_CRITICAL
76 #elif defined (__OS2__)
77 # include <errno.h>
79 typedef struct vlc_thread *vlc_thread_t;
80 typedef struct
82 bool dynamic;
83 union
85 struct
87 bool locked;
88 unsigned long contention;
90 HMTX hmtx;
92 } vlc_mutex_t;
93 #define VLC_STATIC_MUTEX { false, { { false, 0 } } }
94 typedef struct
96 HEV hev;
97 unsigned clock;
98 } vlc_cond_t;
99 #define VLC_STATIC_COND { 0, 0 }
100 #define LIBVLC_NEED_SEMAPHORE
101 #define LIBVLC_NEED_RWLOCK
102 typedef struct vlc_threadvar *vlc_threadvar_t;
103 typedef struct vlc_timer *vlc_timer_t;
105 # define VLC_THREAD_PRIORITY_LOW 0
106 # define VLC_THREAD_PRIORITY_INPUT \
107 MAKESHORT(PRTYD_MAXIMUM / 2, PRTYC_REGULAR)
108 # define VLC_THREAD_PRIORITY_AUDIO MAKESHORT(PRTYD_MAXIMUM, PRTYC_REGULAR)
109 # define VLC_THREAD_PRIORITY_VIDEO 0
110 # define VLC_THREAD_PRIORITY_OUTPUT \
111 MAKESHORT(PRTYD_MAXIMUM / 2, PRTYC_REGULAR)
112 # define VLC_THREAD_PRIORITY_HIGHEST MAKESHORT(0, PRTYC_TIMECRITICAL)
114 # define pthread_sigmask sigprocmask
116 #elif defined (__ANDROID__) /* pthreads subset without pthread_cancel() */
117 # include <unistd.h>
118 # include <pthread.h>
119 # include <poll.h>
120 # define LIBVLC_USE_PTHREAD_CLEANUP 1
121 # define LIBVLC_NEED_SEMAPHORE
122 # define LIBVLC_NEED_RWLOCK
124 typedef struct vlc_thread *vlc_thread_t;
125 typedef pthread_mutex_t vlc_mutex_t;
126 #define VLC_STATIC_MUTEX PTHREAD_MUTEX_INITIALIZER
127 typedef struct
129 pthread_cond_t cond;
130 unsigned clock;
131 } vlc_cond_t;
132 #define VLC_STATIC_COND { PTHREAD_COND_INITIALIZER, CLOCK_REALTIME }
134 typedef pthread_key_t vlc_threadvar_t;
135 typedef struct vlc_timer *vlc_timer_t;
137 # define VLC_THREAD_PRIORITY_LOW 0
138 # define VLC_THREAD_PRIORITY_INPUT 0
139 # define VLC_THREAD_PRIORITY_AUDIO 0
140 # define VLC_THREAD_PRIORITY_VIDEO 0
141 # define VLC_THREAD_PRIORITY_OUTPUT 0
142 # define VLC_THREAD_PRIORITY_HIGHEST 0
144 #elif defined (__APPLE__)
145 # define _APPLE_C_SOURCE 1 /* Proper pthread semantics on OSX */
146 # include <unistd.h>
147 # include <pthread.h>
148 /* Unnamed POSIX semaphores not supported on Mac OS X */
149 # include <mach/semaphore.h>
150 # include <mach/task.h>
151 # define LIBVLC_USE_PTHREAD 1
152 # define LIBVLC_USE_PTHREAD_CLEANUP 1
153 # define LIBVLC_USE_PTHREAD_CANCEL 1
155 typedef pthread_t vlc_thread_t;
156 typedef pthread_mutex_t vlc_mutex_t;
157 #define VLC_STATIC_MUTEX PTHREAD_MUTEX_INITIALIZER
158 typedef struct
160 pthread_cond_t cond;
161 unsigned clock;
162 } vlc_cond_t;
163 #define VLC_STATIC_COND { PTHREAD_COND_INITIALIZER, 0 }
164 typedef semaphore_t vlc_sem_t;
165 typedef pthread_rwlock_t vlc_rwlock_t;
166 #define VLC_STATIC_RWLOCK PTHREAD_RWLOCK_INITIALIZER
167 typedef pthread_key_t vlc_threadvar_t;
168 typedef struct vlc_timer *vlc_timer_t;
170 # define VLC_THREAD_PRIORITY_LOW 0
171 # define VLC_THREAD_PRIORITY_INPUT 22
172 # define VLC_THREAD_PRIORITY_AUDIO 22
173 # define VLC_THREAD_PRIORITY_VIDEO 0
174 # define VLC_THREAD_PRIORITY_OUTPUT 22
175 # define VLC_THREAD_PRIORITY_HIGHEST 22
177 #else /* POSIX threads */
178 # include <unistd.h> /* _POSIX_SPIN_LOCKS */
179 # include <pthread.h>
180 # include <semaphore.h>
181 # define LIBVLC_USE_PTHREAD 1
182 # define LIBVLC_USE_PTHREAD_CLEANUP 1
183 # define LIBVLC_USE_PTHREAD_CANCEL 1
185 typedef pthread_t vlc_thread_t;
186 typedef pthread_mutex_t vlc_mutex_t;
187 #define VLC_STATIC_MUTEX PTHREAD_MUTEX_INITIALIZER
188 typedef pthread_cond_t vlc_cond_t;
189 #define VLC_STATIC_COND PTHREAD_COND_INITIALIZER
190 typedef sem_t vlc_sem_t;
191 typedef pthread_rwlock_t vlc_rwlock_t;
192 #define VLC_STATIC_RWLOCK PTHREAD_RWLOCK_INITIALIZER
193 typedef pthread_key_t vlc_threadvar_t;
194 typedef struct vlc_timer *vlc_timer_t;
196 # define VLC_THREAD_PRIORITY_LOW 0
197 # define VLC_THREAD_PRIORITY_INPUT 10
198 # define VLC_THREAD_PRIORITY_AUDIO 5
199 # define VLC_THREAD_PRIORITY_VIDEO 0
200 # define VLC_THREAD_PRIORITY_OUTPUT 15
201 # define VLC_THREAD_PRIORITY_HIGHEST 20
203 #endif
205 #ifdef LIBVLC_NEED_SEMAPHORE
206 typedef struct vlc_sem
208 vlc_mutex_t lock;
209 vlc_cond_t wait;
210 unsigned value;
211 } vlc_sem_t;
212 #endif
214 #ifdef LIBVLC_NEED_RWLOCK
215 typedef struct vlc_rwlock
217 vlc_mutex_t mutex;
218 vlc_cond_t wait;
219 long state;
220 } vlc_rwlock_t;
221 # define VLC_STATIC_RWLOCK { VLC_STATIC_MUTEX, VLC_STATIC_COND, 0 }
222 #endif
224 /*****************************************************************************
225 * Function definitions
226 *****************************************************************************/
227 VLC_API void vlc_mutex_init( vlc_mutex_t * );
228 VLC_API void vlc_mutex_init_recursive( vlc_mutex_t * );
229 VLC_API void vlc_mutex_destroy( vlc_mutex_t * );
230 VLC_API void vlc_mutex_lock( vlc_mutex_t * );
231 VLC_API int vlc_mutex_trylock( vlc_mutex_t * ) VLC_USED;
232 VLC_API void vlc_mutex_unlock( vlc_mutex_t * );
233 VLC_API void vlc_cond_init( vlc_cond_t * );
234 VLC_API void vlc_cond_init_daytime( vlc_cond_t * );
235 VLC_API void vlc_cond_destroy( vlc_cond_t * );
236 VLC_API void vlc_cond_signal(vlc_cond_t *);
237 VLC_API void vlc_cond_broadcast(vlc_cond_t *);
238 VLC_API void vlc_cond_wait(vlc_cond_t *, vlc_mutex_t *);
239 VLC_API int vlc_cond_timedwait(vlc_cond_t *, vlc_mutex_t *, mtime_t);
240 VLC_API void vlc_sem_init(vlc_sem_t *, unsigned);
241 VLC_API void vlc_sem_destroy(vlc_sem_t *);
242 VLC_API int vlc_sem_post(vlc_sem_t *);
243 VLC_API void vlc_sem_wait(vlc_sem_t *);
245 VLC_API void vlc_rwlock_init(vlc_rwlock_t *);
246 VLC_API void vlc_rwlock_destroy(vlc_rwlock_t *);
247 VLC_API void vlc_rwlock_rdlock(vlc_rwlock_t *);
248 VLC_API void vlc_rwlock_wrlock(vlc_rwlock_t *);
249 VLC_API void vlc_rwlock_unlock(vlc_rwlock_t *);
250 VLC_API int vlc_threadvar_create(vlc_threadvar_t * , void (*) (void *) );
251 VLC_API void vlc_threadvar_delete(vlc_threadvar_t *);
252 VLC_API int vlc_threadvar_set(vlc_threadvar_t, void *);
253 VLC_API void * vlc_threadvar_get(vlc_threadvar_t);
255 VLC_API int vlc_clone(vlc_thread_t *, void * (*) (void *), void *, int) VLC_USED;
256 VLC_API void vlc_cancel(vlc_thread_t);
257 VLC_API void vlc_join(vlc_thread_t, void **);
258 VLC_API void vlc_control_cancel (int cmd, ...);
260 VLC_API mtime_t mdate(void);
261 VLC_API void mwait(mtime_t deadline);
262 VLC_API void msleep(mtime_t delay);
264 #define VLC_HARD_MIN_SLEEP 10000 /* 10 milliseconds = 1 tick at 100Hz */
265 #define VLC_SOFT_MIN_SLEEP 9000000 /* 9 seconds */
267 #if VLC_GCC_VERSION(4,3)
268 /* Linux has 100, 250, 300 or 1000Hz
270 * HZ=100 by default on FreeBSD, but some architectures use a 1000Hz timer
273 static
274 __attribute__((unused))
275 __attribute__((noinline))
276 __attribute__((error("sorry, cannot sleep for such short a time")))
277 mtime_t impossible_delay( mtime_t delay )
279 (void) delay;
280 return VLC_HARD_MIN_SLEEP;
283 static
284 __attribute__((unused))
285 __attribute__((noinline))
286 __attribute__((warning("use proper event handling instead of short delay")))
287 mtime_t harmful_delay( mtime_t delay )
289 return delay;
292 # define check_delay( d ) \
293 ((__builtin_constant_p(d < VLC_HARD_MIN_SLEEP) \
294 && (d < VLC_HARD_MIN_SLEEP)) \
295 ? impossible_delay(d) \
296 : ((__builtin_constant_p(d < VLC_SOFT_MIN_SLEEP) \
297 && (d < VLC_SOFT_MIN_SLEEP)) \
298 ? harmful_delay(d) \
299 : d))
301 static
302 __attribute__((unused))
303 __attribute__((noinline))
304 __attribute__((error("deadlines can not be constant")))
305 mtime_t impossible_deadline( mtime_t deadline )
307 return deadline;
310 # define check_deadline( d ) \
311 (__builtin_constant_p(d) ? impossible_deadline(d) : d)
312 #else
313 # define check_delay(d) (d)
314 # define check_deadline(d) (d)
315 #endif
317 #define msleep(d) msleep(check_delay(d))
318 #define mwait(d) mwait(check_deadline(d))
320 VLC_API int vlc_timer_create(vlc_timer_t *, void (*) (void *), void *) VLC_USED;
321 VLC_API void vlc_timer_destroy(vlc_timer_t);
322 VLC_API void vlc_timer_schedule(vlc_timer_t, bool, mtime_t, mtime_t);
323 VLC_API unsigned vlc_timer_getoverrun(vlc_timer_t) VLC_USED;
325 VLC_API unsigned vlc_GetCPUCount(void);
327 VLC_API int vlc_savecancel(void);
328 VLC_API void vlc_restorecancel(int state);
329 VLC_API void vlc_testcancel(void);
331 #if defined (LIBVLC_USE_PTHREAD_CLEANUP)
333 * Registers a new procedure to run if the thread is cancelled (or otherwise
334 * exits prematurely). Any call to vlc_cleanup_push() <b>must</b> paired with a
335 * call to either vlc_cleanup_pop() or vlc_cleanup_run(). Branching into or out
336 * of the block between these two function calls is not allowed (read: it will
337 * likely crash the whole process). If multiple procedures are registered,
338 * they are handled in last-in first-out order.
340 * @param routine procedure to call if the thread ends
341 * @param arg argument for the procedure
343 # define vlc_cleanup_push( routine, arg ) pthread_cleanup_push (routine, arg)
346 * Removes a cleanup procedure that was previously registered with
347 * vlc_cleanup_push().
349 # define vlc_cleanup_pop( ) pthread_cleanup_pop (0)
352 * Removes a cleanup procedure that was previously registered with
353 * vlc_cleanup_push(), and executes it.
355 # define vlc_cleanup_run( ) pthread_cleanup_pop (1)
357 #else
358 enum
360 VLC_CLEANUP_PUSH,
361 VLC_CLEANUP_POP,
363 typedef struct vlc_cleanup_t vlc_cleanup_t;
365 struct vlc_cleanup_t
367 vlc_cleanup_t *next;
368 void (*proc) (void *);
369 void *data;
372 /* This macros opens a code block on purpose. This is needed for multiple
373 * calls within a single function. This also prevent Win32 developers from
374 * writing code that would break on POSIX (POSIX opens a block as well). */
375 # define vlc_cleanup_push( routine, arg ) \
376 do { \
377 vlc_cleanup_t vlc_cleanup_data = { NULL, routine, arg, }; \
378 vlc_control_cancel (VLC_CLEANUP_PUSH, &vlc_cleanup_data)
380 # define vlc_cleanup_pop( ) \
381 vlc_control_cancel (VLC_CLEANUP_POP); \
382 } while (0)
384 # define vlc_cleanup_run( ) \
385 vlc_control_cancel (VLC_CLEANUP_POP); \
386 vlc_cleanup_data.proc (vlc_cleanup_data.data); \
387 } while (0)
389 #endif /* !LIBVLC_USE_PTHREAD_CLEANUP */
391 #ifndef LIBVLC_USE_PTHREAD_CANCEL
392 /* poll() with cancellation */
393 # ifdef __OS2__
394 int vlc_poll (struct pollfd *fds, unsigned nfds, int timeout);
395 # else
396 static inline int vlc_poll (struct pollfd *fds, unsigned nfds, int timeout)
398 int val;
402 int ugly_timeout = ((unsigned)timeout >= 50) ? 50 : timeout;
403 if (timeout >= 0)
404 timeout -= ugly_timeout;
406 vlc_testcancel ();
407 val = poll (fds, nfds, ugly_timeout);
409 while (val == 0 && timeout != 0);
411 return val;
413 # endif
415 # define poll(u,n,t) vlc_poll(u, n, t)
417 #endif /* LIBVLC_USE_PTHREAD_CANCEL */
419 static inline void vlc_cleanup_lock (void *lock)
421 vlc_mutex_unlock ((vlc_mutex_t *)lock);
423 #define mutex_cleanup_push( lock ) vlc_cleanup_push (vlc_cleanup_lock, lock)
425 #ifdef __cplusplus
427 * Helper C++ class to lock a mutex.
428 * The mutex is locked when the object is created, and unlocked when the object
429 * is destroyed.
431 class vlc_mutex_locker
433 private:
434 vlc_mutex_t *lock;
435 public:
436 vlc_mutex_locker (vlc_mutex_t *m) : lock (m)
438 vlc_mutex_lock (lock);
441 ~vlc_mutex_locker (void)
443 vlc_mutex_unlock (lock);
446 #endif
448 enum
450 VLC_AVCODEC_MUTEX = 0,
451 VLC_GCRYPT_MUTEX,
452 VLC_XLIB_MUTEX,
453 VLC_MOSAIC_MUTEX,
454 VLC_HIGHLIGHT_MUTEX,
455 VLC_ATOMIC_MUTEX,
456 /* Insert new entry HERE */
457 VLC_MAX_MUTEX
460 VLC_API void vlc_global_mutex( unsigned, bool );
461 #define vlc_global_lock( n ) vlc_global_mutex( n, true )
462 #define vlc_global_unlock( n ) vlc_global_mutex( n, false )
464 #endif /* !_VLC_THREADS_H */