]> git.sesse.net Git - vlc/blob - include/vlc_threads.h
mkv: Translation fix.
[vlc] / include / vlc_threads.h
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 the VideoLAN team
6  * $Id$
7  *
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>
12  *
13  * This program is free software; you can redistribute it and/or modify
14  * it under the terms of the GNU General Public License as published by
15  * the Free Software Foundation; either version 2 of the License, or
16  * (at your option) any later version.
17  *
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 General Public License for more details.
22  *
23  * You should have received a copy of the GNU General Public License
24  * along with this program; if not, write to the Free Software
25  * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston MA 02110-1301, USA.
26  *****************************************************************************/
27
28 #ifndef _VLC_THREADS_H_
29 #define _VLC_THREADS_H_
30
31 #if defined( UNDER_CE )
32                                                                 /* WinCE API */
33 #elif defined( WIN32 )
34 #   include <process.h>                                         /* Win32 API */
35 #   include <errno.h>
36
37 #elif defined( SYS_BEOS )                                            /* BeOS */
38 #   include <kernel/OS.h>
39 #   include <kernel/scheduler.h>
40 #   include <byteorder.h>
41
42 #else                                         /* pthreads (like Linux & BSD) */
43 #   define LIBVLC_USE_PTHREAD 1
44 #   define _APPLE_C_SOURCE    1 /* Proper pthread semantics on OSX */
45
46 #   include <stdlib.h> /* lldiv_t definition (only in C99) */
47 #   include <unistd.h> /* _POSIX_SPIN_LOCKS */
48 #   include <pthread.h>
49     /* Needed for pthread_cond_timedwait */
50 #   include <errno.h>
51 #   include <time.h>
52
53 #endif
54
55 /*****************************************************************************
56  * Constants
57  *****************************************************************************/
58
59 /* Thread priorities */
60 #ifdef __APPLE__
61 #   define VLC_THREAD_PRIORITY_LOW      0
62 #   define VLC_THREAD_PRIORITY_INPUT   22
63 #   define VLC_THREAD_PRIORITY_AUDIO   22
64 #   define VLC_THREAD_PRIORITY_VIDEO    0
65 #   define VLC_THREAD_PRIORITY_OUTPUT  22
66 #   define VLC_THREAD_PRIORITY_HIGHEST 22
67
68 #elif defined(SYS_BEOS)
69 #   define VLC_THREAD_PRIORITY_LOW 5
70 #   define VLC_THREAD_PRIORITY_INPUT 10
71 #   define VLC_THREAD_PRIORITY_AUDIO 10
72 #   define VLC_THREAD_PRIORITY_VIDEO 5
73 #   define VLC_THREAD_PRIORITY_OUTPUT 15
74 #   define VLC_THREAD_PRIORITY_HIGHEST 15
75
76 #elif defined(LIBVLC_USE_PTHREAD)
77 #   define VLC_THREAD_PRIORITY_LOW      0
78 #   define VLC_THREAD_PRIORITY_INPUT   10
79 #   define VLC_THREAD_PRIORITY_AUDIO    5
80 #   define VLC_THREAD_PRIORITY_VIDEO    0
81 #   define VLC_THREAD_PRIORITY_OUTPUT  15
82 #   define VLC_THREAD_PRIORITY_HIGHEST 20
83
84 #elif defined(WIN32) || defined(UNDER_CE)
85 /* Define different priorities for WinNT/2K/XP and Win9x/Me */
86 #   define VLC_THREAD_PRIORITY_LOW 0
87 #   define VLC_THREAD_PRIORITY_INPUT \
88         (IS_WINNT ? THREAD_PRIORITY_ABOVE_NORMAL : 0)
89 #   define VLC_THREAD_PRIORITY_AUDIO \
90         (IS_WINNT ? THREAD_PRIORITY_HIGHEST : 0)
91 #   define VLC_THREAD_PRIORITY_VIDEO \
92         (IS_WINNT ? 0 : THREAD_PRIORITY_BELOW_NORMAL )
93 #   define VLC_THREAD_PRIORITY_OUTPUT \
94         (IS_WINNT ? THREAD_PRIORITY_ABOVE_NORMAL : 0)
95 #   define VLC_THREAD_PRIORITY_HIGHEST \
96         (IS_WINNT ? THREAD_PRIORITY_TIME_CRITICAL : 0)
97
98 #else
99 #   define VLC_THREAD_PRIORITY_LOW 0
100 #   define VLC_THREAD_PRIORITY_INPUT 0
101 #   define VLC_THREAD_PRIORITY_AUDIO 0
102 #   define VLC_THREAD_PRIORITY_VIDEO 0
103 #   define VLC_THREAD_PRIORITY_OUTPUT 0
104 #   define VLC_THREAD_PRIORITY_HIGHEST 0
105
106 #endif
107
108 /*****************************************************************************
109  * Type definitions
110  *****************************************************************************/
111
112 #if defined (LIBVLC_USE_PTHREAD)
113 typedef pthread_t       vlc_thread_t;
114 typedef pthread_mutex_t vlc_mutex_t;
115 typedef pthread_cond_t  vlc_cond_t;
116 typedef pthread_key_t   vlc_threadvar_t;
117
118 #elif defined( WIN32 ) || defined( UNDER_CE )
119 typedef HANDLE  vlc_thread_t;
120
121 typedef BOOL (WINAPI *SIGNALOBJECTANDWAIT) ( HANDLE, HANDLE, DWORD, BOOL );
122
123 typedef HANDLE  vlc_mutex_t;
124
125 typedef struct
126 {
127     volatile int        i_waiting_threads;
128     HANDLE              event;
129 } vlc_cond_t;
130
131 typedef DWORD   vlc_threadvar_t;
132
133 #elif defined( SYS_BEOS )
134 /* This is the BeOS implementation of the vlc threads, note that the mutex is
135  * not a real mutex and the cond_var is not like a pthread cond_var but it is
136  * enough for what we need */
137
138 typedef thread_id vlc_thread_t;
139
140 typedef struct
141 {
142     int32_t         init;
143     sem_id          lock;
144 } vlc_mutex_t;
145
146 typedef struct
147 {
148     int32_t         init;
149     thread_id       thread;
150 } vlc_cond_t;
151
152 typedef struct
153 {
154 } vlc_threadvar_t;
155
156 #endif
157
158 #if defined( WIN32 ) && !defined ETIMEDOUT
159 #  define ETIMEDOUT 10060 /* This is the value in winsock.h. */
160 #endif
161
162 /*****************************************************************************
163  * Function definitions
164  *****************************************************************************/
165 VLC_EXPORT( int,  vlc_mutex_init,    ( vlc_mutex_t * ) );
166 VLC_EXPORT( int,  vlc_mutex_init_recursive, ( vlc_mutex_t * ) );
167 VLC_EXPORT( void,  __vlc_mutex_destroy, ( const char *, int, vlc_mutex_t * ) );
168 VLC_EXPORT( int,  __vlc_cond_init,     ( vlc_cond_t * ) );
169 VLC_EXPORT( void,  __vlc_cond_destroy,  ( const char *, int, vlc_cond_t * ) );
170 VLC_EXPORT( int, vlc_threadvar_create, (vlc_threadvar_t * , void (*) (void *) ) );
171 VLC_EXPORT( void, vlc_threadvar_delete, (vlc_threadvar_t *) );
172 VLC_EXPORT( int,  __vlc_thread_create, ( vlc_object_t *, const char *, int, const char *, void * ( * ) ( void * ), int, bool ) );
173 VLC_EXPORT( int,  __vlc_thread_set_priority, ( vlc_object_t *, const char *, int, int ) );
174 VLC_EXPORT( void, __vlc_thread_join,   ( vlc_object_t *, const char *, int ) );
175
176 #define vlc_thread_ready vlc_object_signal
177
178 /*****************************************************************************
179  * vlc_mutex_lock: lock a mutex
180  *****************************************************************************/
181 #define vlc_mutex_lock( P_MUTEX )                                           \
182     __vlc_mutex_lock( __FILE__, __LINE__, P_MUTEX )
183
184 VLC_EXPORT(void, vlc_pthread_fatal, (const char *action, int error, const char *file, unsigned line));
185
186 #if defined(LIBVLC_USE_PTHREAD)
187 # define VLC_THREAD_ASSERT( action ) \
188     if (val) \
189         vlc_pthread_fatal (action, val, psz_file, i_line)
190 #else
191 # define VLC_THREAD_ASSERT ((void)(val))
192 #endif
193
194 static inline void __vlc_mutex_lock( const char * psz_file, int i_line,
195                                     vlc_mutex_t * p_mutex )
196 {
197 #if defined(LIBVLC_USE_PTHREAD)
198 #   define vlc_assert_locked( m ) \
199            assert (pthread_mutex_lock (m) == EDEADLK)
200     int val = pthread_mutex_lock( p_mutex );
201     VLC_THREAD_ASSERT ("locking mutex");
202
203 #elif defined( UNDER_CE )
204     (void)psz_file; (void)i_line;
205
206     EnterCriticalSection( &p_mutex->csection );
207
208 #elif defined( WIN32 )
209     (void)psz_file; (void)i_line;
210
211     WaitForSingleObject( *p_mutex, INFINITE );
212
213 #elif defined( SYS_BEOS )
214     acquire_sem( p_mutex->lock );
215
216 #endif
217 }
218
219 #ifndef vlc_assert_locked
220 # define vlc_assert_locked( m ) (void)0
221 #endif
222
223 /*****************************************************************************
224  * vlc_mutex_unlock: unlock a mutex
225  *****************************************************************************/
226 #define vlc_mutex_unlock( P_MUTEX )                                         \
227     __vlc_mutex_unlock( __FILE__, __LINE__, P_MUTEX )
228
229 static inline void __vlc_mutex_unlock( const char * psz_file, int i_line,
230                                       vlc_mutex_t *p_mutex )
231 {
232 #if defined(LIBVLC_USE_PTHREAD)
233     int val = pthread_mutex_unlock( p_mutex );
234     VLC_THREAD_ASSERT ("unlocking mutex");
235
236 #elif defined( UNDER_CE )
237     (void)psz_file; (void)i_line;
238
239     LeaveCriticalSection( &p_mutex->csection );
240
241 #elif defined( WIN32 )
242     (void)psz_file; (void)i_line;
243
244     ReleaseMutex( *p_mutex );
245
246 #elif defined( SYS_BEOS )
247     release_sem( p_mutex->lock );
248
249 #endif
250 }
251
252 /*****************************************************************************
253  * vlc_mutex_destroy: destroy a mutex
254  *****************************************************************************/
255 #define vlc_mutex_destroy( P_MUTEX )                                        \
256     __vlc_mutex_destroy( __FILE__, __LINE__, P_MUTEX )
257
258 /*****************************************************************************
259  * vlc_cond_init: initialize a condition
260  *****************************************************************************/
261 #define vlc_cond_init( P_THIS, P_COND )                                     \
262     __vlc_cond_init( P_COND )
263
264 /*****************************************************************************
265  * vlc_cond_signal: start a thread on condition completion
266  *****************************************************************************/
267 #define vlc_cond_signal( P_COND )                                           \
268     __vlc_cond_signal( __FILE__, __LINE__, P_COND )
269
270 static inline void __vlc_cond_signal( const char * psz_file, int i_line,
271                                       vlc_cond_t *p_condvar )
272 {
273 #if defined(LIBVLC_USE_PTHREAD)
274     int val = pthread_cond_signal( p_condvar );
275     VLC_THREAD_ASSERT ("signaling condition variable");
276
277 #elif defined( UNDER_CE ) || defined( WIN32 )
278     (void)psz_file; (void)i_line;
279
280     /* Release one waiting thread if one is available. */
281     /* For this trick to work properly, the vlc_cond_signal must be surrounded
282      * by a mutex. This will prevent another thread from stealing the signal */
283     /* PulseEvent() only works if none of the waiting threads is suspended.
284      * This is particularily problematic under a debug session.
285      * as documented in http://support.microsoft.com/kb/q173260/ */
286     PulseEvent( p_condvar->event );
287
288 #elif defined( SYS_BEOS )
289     while( p_condvar->thread != -1 )
290     {
291         thread_info info;
292         if( get_thread_info(p_condvar->thread, &info) == B_BAD_VALUE )
293             return;
294
295         if( info.state != B_THREAD_SUSPENDED )
296         {
297             /* The  waiting thread is not suspended so it could
298              * have been interrupted beetwen the unlock and the
299              * suspend_thread line. That is why we sleep a little
300              * before retesting p_condver->thread. */
301             snooze( 10000 );
302         }
303         else
304         {
305             /* Ok, we have to wake up that thread */
306             resume_thread( p_condvar->thread );
307         }
308     }
309
310 #endif
311 }
312
313 /*****************************************************************************
314  * vlc_cond_wait: wait until condition completion
315  *****************************************************************************/
316 #define vlc_cond_wait( P_COND, P_MUTEX )                                     \
317     __vlc_cond_wait( __FILE__, __LINE__, P_COND, P_MUTEX  )
318
319 static inline void __vlc_cond_wait( const char * psz_file, int i_line,
320                                     vlc_cond_t *p_condvar, vlc_mutex_t *p_mutex )
321 {
322 #if defined(LIBVLC_USE_PTHREAD)
323     int val = pthread_cond_wait( p_condvar, p_mutex );
324     VLC_THREAD_ASSERT ("waiting on condition");
325
326 #elif defined( UNDER_CE )
327     p_condvar->i_waiting_threads++;
328     LeaveCriticalSection( &p_mutex->csection );
329     WaitForSingleObject( p_condvar->event, INFINITE );
330     p_condvar->i_waiting_threads--;
331
332     /* Reacquire the mutex before returning. */
333     vlc_mutex_lock( p_mutex );
334
335 #elif defined( WIN32 )
336     (void)psz_file; (void)i_line;
337
338     /* Increase our wait count */
339     p_condvar->i_waiting_threads++;
340     SignalObjectAndWait( *p_mutex, p_condvar->event, INFINITE, FALSE );
341     p_condvar->i_waiting_threads--;
342
343     /* Reacquire the mutex before returning. */
344     vlc_mutex_lock( p_mutex );
345
346 #elif defined( SYS_BEOS )
347     /* The p_condvar->thread var is initialized before the unlock because
348      * it enables to identify when the thread is interrupted beetwen the
349      * unlock line and the suspend_thread line */
350     p_condvar->thread = find_thread( NULL );
351     vlc_mutex_unlock( p_mutex );
352     suspend_thread( p_condvar->thread );
353     p_condvar->thread = -1;
354
355     vlc_mutex_lock( p_mutex );
356
357 #endif
358 }
359
360
361 /*****************************************************************************
362  * vlc_cond_timedwait: wait until condition completion or expiration
363  *****************************************************************************
364  * Returns 0 if object signaled, an error code in case of timeout or error.
365  *****************************************************************************/
366 #define vlc_cond_timedwait( P_COND, P_MUTEX, DEADLINE )                      \
367     __vlc_cond_timedwait( __FILE__, __LINE__, P_COND, P_MUTEX, DEADLINE  )
368
369 static inline int __vlc_cond_timedwait( const char * psz_file, int i_line,
370                                         vlc_cond_t *p_condvar,
371                                         vlc_mutex_t *p_mutex,
372                                         mtime_t deadline )
373 {
374 #if defined(LIBVLC_USE_PTHREAD)
375     lldiv_t d = lldiv( deadline, 1000000 );
376     struct timespec ts = { d.quot, d.rem * 1000 };
377
378     int val = pthread_cond_timedwait (p_condvar, p_mutex, &ts);
379     if (val == ETIMEDOUT)
380         return ETIMEDOUT; /* this error is perfectly normal */
381     VLC_THREAD_ASSERT ("timed-waiting on condition");
382
383 #elif defined( UNDER_CE )
384     mtime_t delay_ms = (deadline - mdate())/1000;
385     DWORD result;
386     if( delay_ms < 0 )
387         delay_ms = 0;
388
389     p_condvar->i_waiting_threads++;
390     LeaveCriticalSection( &p_mutex->csection );
391     result = WaitForSingleObject( p_condvar->event, delay_ms );
392     p_condvar->i_waiting_threads--;
393
394     /* Reacquire the mutex before returning. */
395     vlc_mutex_lock( p_mutex );
396
397     if(result == WAIT_TIMEOUT)
398        return ETIMEDOUT; /* this error is perfectly normal */
399
400     (void)psz_file; (void)i_line;
401
402 #elif defined( WIN32 )
403     mtime_t delay_ms = (deadline - mdate())/1000;
404     DWORD result;
405     if( delay_ms < 0 )
406         delay_ms = 0;
407
408     /* Increase our wait count */
409     p_condvar->i_waiting_threads++;
410     result = SignalObjectAndWait( *p_mutex, p_condvar->event,
411                                   delay_ms, FALSE );
412     p_condvar->i_waiting_threads--;
413
414     /* Reacquire the mutex before returning. */
415     vlc_mutex_lock( p_mutex );
416     if(result == WAIT_TIMEOUT)
417        return ETIMEDOUT; /* this error is perfectly normal */
418
419     (void)psz_file; (void)i_line;
420
421 #elif defined( SYS_BEOS )
422 #   error Unimplemented
423
424 #endif
425
426     return 0;
427 }
428
429 /*****************************************************************************
430  * vlc_cond_destroy: destroy a condition
431  *****************************************************************************/
432 #define vlc_cond_destroy( P_COND )                                          \
433     __vlc_cond_destroy( __FILE__, __LINE__, P_COND )
434
435 /*****************************************************************************
436  * vlc_threadvar_set: create: set the value of a thread-local variable
437  *****************************************************************************/
438 static inline int vlc_threadvar_set( vlc_threadvar_t * p_tls, void *p_value )
439 {
440     int i_ret;
441
442 #if defined(LIBVLC_USE_PTHREAD)
443     i_ret = pthread_setspecific( *p_tls, p_value );
444
445 #elif defined( SYS_BEOS )
446     i_ret = EINVAL;
447
448 #elif defined( UNDER_CE ) || defined( WIN32 )
449     i_ret = TlsSetValue( *p_tls, p_value ) ? EINVAL : 0;
450
451 #endif
452
453     return i_ret;
454 }
455
456 /*****************************************************************************
457  * vlc_threadvar_get: create: get the value of a thread-local variable
458  *****************************************************************************/
459 static inline void* vlc_threadvar_get( vlc_threadvar_t * p_tls )
460 {
461     void *p_ret;
462
463 #if defined(LIBVLC_USE_PTHREAD)
464     p_ret = pthread_getspecific( *p_tls );
465
466 #elif defined( SYS_BEOS )
467     p_ret = NULL;
468
469 #elif defined( UNDER_CE ) || defined( WIN32 )
470     p_ret = TlsGetValue( *p_tls );
471
472 #endif
473
474     return p_ret;
475 }
476
477 # if defined (_POSIX_SPIN_LOCKS) && ((_POSIX_SPIN_LOCKS - 0) > 0)
478 typedef pthread_spinlock_t vlc_spinlock_t;
479
480 /**
481  * Initializes a spinlock.
482  */
483 static inline int vlc_spin_init (vlc_spinlock_t *spin)
484 {
485     return pthread_spin_init (spin, PTHREAD_PROCESS_PRIVATE);
486 }
487
488 /**
489  * Acquires a spinlock.
490  */
491 static inline void vlc_spin_lock (vlc_spinlock_t *spin)
492 {
493     pthread_spin_lock (spin);
494 }
495
496 /**
497  * Releases a spinlock.
498  */
499 static inline void vlc_spin_unlock (vlc_spinlock_t *spin)
500 {
501     pthread_spin_unlock (spin);
502 }
503
504 /**
505  * Deinitializes a spinlock.
506  */
507 static inline void vlc_spin_destroy (vlc_spinlock_t *spin)
508 {
509     pthread_spin_destroy (spin);
510 }
511
512 #elif defined( WIN32 )
513
514 typedef CRITICAL_SECTION vlc_spinlock_t;
515
516 /**
517  * Initializes a spinlock.
518  */
519 static inline int vlc_spin_init (vlc_spinlock_t *spin)
520 {
521     return !InitializeCriticalSectionAndSpinCount(spin, 4000);
522 }
523
524 /**
525  * Acquires a spinlock.
526  */
527 static inline void vlc_spin_lock (vlc_spinlock_t *spin)
528 {
529     EnterCriticalSection(spin);
530 }
531
532 /**
533  * Releases a spinlock.
534  */
535 static inline void vlc_spin_unlock (vlc_spinlock_t *spin)
536 {
537     LeaveCriticalSection(spin);
538 }
539
540 /**
541  * Deinitializes a spinlock.
542  */
543 static inline void vlc_spin_destroy (vlc_spinlock_t *spin)
544 {
545     DeleteCriticalSection(spin);
546 }
547
548 #else
549
550 /* Fallback to plain mutexes if spinlocks are not available */
551 typedef vlc_mutex_t vlc_spinlock_t;
552
553 static inline int vlc_spin_init (vlc_spinlock_t *spin)
554 {
555     return vlc_mutex_init (spin);
556 }
557
558 # define vlc_spin_lock    vlc_mutex_lock
559 # define vlc_spin_unlock  vlc_mutex_unlock
560 # define vlc_spin_destroy vlc_mutex_destroy
561 #endif
562
563 /**
564  * Issues a full memory barrier.
565  */
566 #if defined (__APPLE__)
567 # include <libkern/OSAtomic.h> /* OSMemoryBarrier() */
568 #endif
569 static inline void barrier (void)
570 {
571 #if defined (__GNUC__) && (__GNUC__ > 4) || (__GNUC__ == 4 && __GNUC_MINOR__ >= 1)
572     __sync_synchronize ();
573 #elif defined(__APPLE__)
574     OSMemoryBarrier ();
575 #elif defined(__powerpc__)
576     asm volatile ("sync":::"memory");
577 #elif defined(__i386__)
578     asm volatile ("mfence":::"memory");
579 #elif defined (LIBVLC_USE_PTHREAD)
580     static pthread_mutex_t lock = PTHREAD_MUTEX_INITIALIZER;
581     pthread_mutex_lock (&lock);
582     pthread_mutex_unlock (&lock);
583 #else
584 # error barrier not implemented!
585 #endif
586 }
587
588 /*****************************************************************************
589  * vlc_thread_create: create a thread
590  *****************************************************************************/
591 #define vlc_thread_create( P_THIS, PSZ_NAME, FUNC, PRIORITY, WAIT )         \
592     __vlc_thread_create( VLC_OBJECT(P_THIS), __FILE__, __LINE__, PSZ_NAME, (void * ( * ) ( void * ))FUNC, PRIORITY, WAIT )
593
594 /*****************************************************************************
595  * vlc_thread_set_priority: set the priority of the calling thread
596  *****************************************************************************/
597 #define vlc_thread_set_priority( P_THIS, PRIORITY )                         \
598     __vlc_thread_set_priority( VLC_OBJECT(P_THIS), __FILE__, __LINE__, PRIORITY )
599
600 /*****************************************************************************
601  * vlc_thread_join: wait until a thread exits
602  *****************************************************************************/
603 #define vlc_thread_join( P_THIS )                                           \
604     __vlc_thread_join( VLC_OBJECT(P_THIS), __FILE__, __LINE__ )
605
606 #endif /* !_VLC_THREADS_H */