]> git.sesse.net Git - vlc/blob - include/vlc_threads_funcs.h
a3152589982094f5be71ca6d6b9e411ea3784478
[vlc] / include / vlc_threads_funcs.h
1 /*****************************************************************************
2  * vlc_threads_funcs.h : threads implementation for the VideoLAN client
3  * This header provides a portable threads implementation.
4  *****************************************************************************
5  * Copyright (C) 1999-2007 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 #if !defined( __LIBVLC__ )
29   #error You are not libvlc or one of its plugins. You cannot include this file
30 #endif
31
32 #ifndef _VLC_THREADFUNCS_H_
33 #define _VLC_THREADFUNCS_H_
34
35 #if defined( WIN32 ) && !defined ETIMEDOUT
36 #  define ETIMEDOUT 10060 /* This is the value in winsock.h. */
37 #endif
38
39 /*****************************************************************************
40  * Function definitions
41  *****************************************************************************/
42 VLC_EXPORT( int,  __vlc_mutex_init,    ( vlc_mutex_t * ) );
43 VLC_EXPORT( int,  __vlc_mutex_init_recursive, ( vlc_mutex_t * ) );
44 VLC_EXPORT( void,  __vlc_mutex_destroy, ( const char *, int, vlc_mutex_t * ) );
45 VLC_EXPORT( int,  __vlc_cond_init,     ( vlc_cond_t * ) );
46 VLC_EXPORT( void,  __vlc_cond_destroy,  ( const char *, int, vlc_cond_t * ) );
47 VLC_EXPORT( int, __vlc_threadvar_create, (vlc_threadvar_t * ) );
48 VLC_EXPORT( int,  __vlc_thread_create, ( vlc_object_t *, const char *, int, const char *, void * ( * ) ( void * ), int, bool ) );
49 VLC_EXPORT( int,  __vlc_thread_set_priority, ( vlc_object_t *, const char *, int, int ) );
50 VLC_EXPORT( void, __vlc_thread_ready,  ( vlc_object_t * ) );
51 VLC_EXPORT( void, __vlc_thread_join,   ( vlc_object_t *, const char *, int ) );
52
53 /*****************************************************************************
54  * vlc_threads_init: initialize threads system
55  *****************************************************************************/
56 #define vlc_threads_init( P_THIS )                                          \
57     __vlc_threads_init( VLC_OBJECT(P_THIS) )
58
59 /*****************************************************************************
60  * vlc_threads_end: deinitialize threads system
61  *****************************************************************************/
62 #define vlc_threads_end( P_THIS )                                           \
63     __vlc_threads_end( VLC_OBJECT(P_THIS) )
64
65 /*****************************************************************************
66  * vlc_mutex_init: initialize a mutex
67  *****************************************************************************/
68 #define vlc_mutex_init( P_THIS, P_MUTEX )                                   \
69     __vlc_mutex_init( P_MUTEX )
70
71 /*****************************************************************************
72  * vlc_mutex_init: initialize a recursive mutex (Don't use it)
73  *****************************************************************************/
74 #define vlc_mutex_init_recursive( P_THIS, P_MUTEX )                         \
75     __vlc_mutex_init_recursive( P_MUTEX )
76
77 /*****************************************************************************
78  * vlc_mutex_lock: lock a mutex
79  *****************************************************************************/
80 #define vlc_mutex_lock( P_MUTEX )                                           \
81     __vlc_mutex_lock( __FILE__, __LINE__, P_MUTEX )
82
83 #if defined(LIBVLC_USE_PTHREAD)
84 VLC_EXPORT(void, vlc_pthread_fatal, (const char *action, int error, const char *file, unsigned line));
85
86 # define VLC_THREAD_ASSERT( action ) \
87     if (val) \
88         vlc_pthread_fatal (action, val, psz_file, i_line)
89 #else
90 # define VLC_THREAD_ASSERT (void)0
91 #endif
92
93 static inline void __vlc_mutex_lock( const char * psz_file, int i_line,
94                                     vlc_mutex_t * p_mutex )
95 {
96 #if defined( UNDER_CE )
97     VLC_UNUSED( psz_file); VLC_UNUSED( i_line );
98
99     EnterCriticalSection( &p_mutex->csection );
100
101 #elif defined( WIN32 )
102     VLC_UNUSED( psz_file); VLC_UNUSED( i_line );
103
104     WaitForSingleObject( *p_mutex, INFINITE );
105
106 #elif defined( HAVE_KERNEL_SCHEDULER_H )
107     acquire_sem( p_mutex->lock );
108
109 #elif defined(LIBVLC_USE_PTHREAD)
110 #   define vlc_assert_locked( m ) \
111            assert (pthread_mutex_lock (m) == EDEADLK)
112     int val = pthread_mutex_lock( p_mutex );
113     VLC_THREAD_ASSERT ("locking mutex");
114
115 #endif
116 }
117
118 #ifndef vlc_assert_locked
119 # define vlc_assert_locked( m ) (void)0
120 #endif
121
122 /*****************************************************************************
123  * vlc_mutex_unlock: unlock a mutex
124  *****************************************************************************/
125 #define vlc_mutex_unlock( P_MUTEX )                                         \
126     __vlc_mutex_unlock( __FILE__, __LINE__, P_MUTEX )
127
128 static inline void __vlc_mutex_unlock( const char * psz_file, int i_line,
129                                       vlc_mutex_t *p_mutex )
130 {
131 #if defined( UNDER_CE )
132     VLC_UNUSED( psz_file); VLC_UNUSED( i_line );
133
134     LeaveCriticalSection( &p_mutex->csection );
135
136 #elif defined( WIN32 )
137     VLC_UNUSED( psz_file); VLC_UNUSED( i_line );
138
139     ReleaseMutex( *p_mutex );
140
141 #elif defined( HAVE_KERNEL_SCHEDULER_H )
142     release_sem( p_mutex->lock );
143
144 #elif defined(LIBVLC_USE_PTHREAD)
145     int val = pthread_mutex_unlock( p_mutex );
146     VLC_THREAD_ASSERT ("unlocking mutex");
147
148 #endif
149 }
150
151 /*****************************************************************************
152  * vlc_mutex_destroy: destroy a mutex
153  *****************************************************************************/
154 #define vlc_mutex_destroy( P_MUTEX )                                        \
155     __vlc_mutex_destroy( __FILE__, __LINE__, P_MUTEX )
156
157 /*****************************************************************************
158  * vlc_cond_init: initialize a condition
159  *****************************************************************************/
160 #define vlc_cond_init( P_THIS, P_COND )                                     \
161     __vlc_cond_init( P_COND )
162
163 /*****************************************************************************
164  * vlc_cond_signal: start a thread on condition completion
165  *****************************************************************************/
166 #define vlc_cond_signal( P_COND )                                           \
167     __vlc_cond_signal( __FILE__, __LINE__, P_COND )
168
169 static inline void __vlc_cond_signal( const char * psz_file, int i_line,
170                                       vlc_cond_t *p_condvar )
171 {
172 #if defined( UNDER_CE ) || defined( WIN32 )
173     VLC_UNUSED( psz_file); VLC_UNUSED( i_line );
174
175     /* Release one waiting thread if one is available. */
176     /* For this trick to work properly, the vlc_cond_signal must be surrounded
177      * by a mutex. This will prevent another thread from stealing the signal */
178     /* PulseEvent() only works if none of the waiting threads is suspended.
179      * This is particularily problematic under a debug session.
180      * as documented in http://support.microsoft.com/kb/q173260/ */
181     PulseEvent( p_condvar->event );
182
183 #elif defined( HAVE_KERNEL_SCHEDULER_H )
184     while( p_condvar->thread != -1 )
185     {
186         thread_info info;
187         if( get_thread_info(p_condvar->thread, &info) == B_BAD_VALUE )
188             return;
189
190         if( info.state != B_THREAD_SUSPENDED )
191         {
192             /* The  waiting thread is not suspended so it could
193              * have been interrupted beetwen the unlock and the
194              * suspend_thread line. That is why we sleep a little
195              * before retesting p_condver->thread. */
196             snooze( 10000 );
197         }
198         else
199         {
200             /* Ok, we have to wake up that thread */
201             resume_thread( p_condvar->thread );
202         }
203     }
204
205 #elif defined(LIBVLC_USE_PTHREAD)
206     int val = pthread_cond_signal( p_condvar );
207     VLC_THREAD_ASSERT ("signaling condition variable");
208
209 #endif
210 }
211
212 /*****************************************************************************
213  * vlc_cond_wait: wait until condition completion
214  *****************************************************************************/
215 #define vlc_cond_wait( P_COND, P_MUTEX )                                     \
216     __vlc_cond_wait( __FILE__, __LINE__, P_COND, P_MUTEX  )
217
218 static inline void __vlc_cond_wait( const char * psz_file, int i_line,
219                                     vlc_cond_t *p_condvar, vlc_mutex_t *p_mutex )
220 {
221 #if defined( UNDER_CE )
222     p_condvar->i_waiting_threads++;
223     LeaveCriticalSection( &p_mutex->csection );
224     WaitForSingleObject( p_condvar->event, INFINITE );
225     p_condvar->i_waiting_threads--;
226
227     /* Reacquire the mutex before returning. */
228     vlc_mutex_lock( p_mutex );
229
230 #elif defined( WIN32 )
231     VLC_UNUSED( psz_file); VLC_UNUSED( i_line );
232
233     /* Increase our wait count */
234     p_condvar->i_waiting_threads++;
235     SignalObjectAndWait( *p_mutex, p_condvar->event, INFINITE, FALSE );
236     p_condvar->i_waiting_threads--;
237
238     /* Reacquire the mutex before returning. */
239     vlc_mutex_lock( p_mutex );
240
241 #elif defined( HAVE_KERNEL_SCHEDULER_H )
242     /* The p_condvar->thread var is initialized before the unlock because
243      * it enables to identify when the thread is interrupted beetwen the
244      * unlock line and the suspend_thread line */
245     p_condvar->thread = find_thread( NULL );
246     vlc_mutex_unlock( p_mutex );
247     suspend_thread( p_condvar->thread );
248     p_condvar->thread = -1;
249
250     vlc_mutex_lock( p_mutex );
251
252 #elif defined(LIBVLC_USE_PTHREAD)
253     int val = pthread_cond_wait( p_condvar, p_mutex );
254     VLC_THREAD_ASSERT ("waiting on condition");
255
256 #endif
257 }
258
259
260 /*****************************************************************************
261  * vlc_cond_timedwait: wait until condition completion or expiration
262  *****************************************************************************
263  * Returns 0 if object signaled, an error code in case of timeout or error.
264  *****************************************************************************/
265 #define vlc_cond_timedwait( P_COND, P_MUTEX, DEADLINE )                      \
266     __vlc_cond_timedwait( __FILE__, __LINE__, P_COND, P_MUTEX, DEADLINE  )
267
268 static inline int __vlc_cond_timedwait( const char * psz_file, int i_line,
269                                         vlc_cond_t *p_condvar,
270                                         vlc_mutex_t *p_mutex,
271                                         mtime_t deadline )
272 {
273 #if defined( UNDER_CE )
274     mtime_t delay_ms = (deadline - mdate())/1000;
275
276     DWORD result;
277     if( delay_ms < 0 )
278         delay_ms = 0;
279
280     p_condvar->i_waiting_threads++;
281     LeaveCriticalSection( &p_mutex->csection );
282     result = WaitForSingleObject( p_condvar->event, delay_ms );
283     p_condvar->i_waiting_threads--;
284
285     /* Reacquire the mutex before returning. */
286     vlc_mutex_lock( p_mutex );
287
288     if(result == WAIT_TIMEOUT)
289        return ETIMEDOUT; /* this error is perfectly normal */
290
291 #elif defined( WIN32 )
292     VLC_UNUSED( psz_file); VLC_UNUSED( i_line );
293
294     DWORD result;
295
296     mtime_t delay_ms = (deadline - mdate())/1000;
297     if( delay_ms < 0 )
298         delay_ms = 0;
299
300     /* Increase our wait count */
301     p_condvar->i_waiting_threads++;
302     result = SignalObjectAndWait( *p_mutex, p_condvar->event,
303                                   delay_ms, FALSE );
304     p_condvar->i_waiting_threads--;
305
306     /* Reacquire the mutex before returning. */
307     vlc_mutex_lock( p_mutex );
308     if(result == WAIT_TIMEOUT)
309        return ETIMEDOUT; /* this error is perfectly normal */
310
311 #elif defined( HAVE_KERNEL_SCHEDULER_H )
312 #   error Unimplemented
313
314 #elif defined(LIBVLC_USE_PTHREAD)
315     lldiv_t d = lldiv( deadline, 1000000 );
316     struct timespec ts = { d.quot, d.rem * 1000 };
317
318     int val = pthread_cond_timedwait (p_condvar, p_mutex, &ts);
319     if (val == ETIMEDOUT)
320         return ETIMEDOUT; /* this error is perfectly normal */
321     VLC_THREAD_ASSERT ("timed-waiting on condition");
322
323 #endif
324
325     return 0;
326 }
327
328 /*****************************************************************************
329  * vlc_cond_destroy: destroy a condition
330  *****************************************************************************/
331 #define vlc_cond_destroy( P_COND )                                          \
332     __vlc_cond_destroy( __FILE__, __LINE__, P_COND )
333
334 /*****************************************************************************
335  * vlc_threadvar_create: create a thread-local variable
336  *****************************************************************************/
337 #define vlc_threadvar_create( PTHIS, P_TLS )                                 \
338    __vlc_threadvar_create( P_TLS )
339
340 /*****************************************************************************
341  * vlc_threadvar_set: create: set the value of a thread-local variable
342  *****************************************************************************/
343 static inline int vlc_threadvar_set( vlc_threadvar_t * p_tls, void *p_value )
344 {
345     int i_ret;
346
347 #if defined( HAVE_KERNEL_SCHEDULER_H )
348     i_ret = EINVAL;
349
350 #elif defined( UNDER_CE ) || defined( WIN32 )
351     i_ret = TlsSetValue( *p_tls, p_value ) ? EINVAL : 0;
352
353 #elif defined(LIBVLC_USE_PTHREAD)
354     i_ret = pthread_setspecific( *p_tls, p_value );
355
356 #endif
357
358     return i_ret;
359 }
360
361 /*****************************************************************************
362  * vlc_threadvar_get: create: get the value of a thread-local variable
363  *****************************************************************************/
364 static inline void* vlc_threadvar_get( vlc_threadvar_t * p_tls )
365 {
366     void *p_ret;
367
368 #if defined( HAVE_KERNEL_SCHEDULER_H )
369     p_ret = NULL;
370
371 #elif defined( UNDER_CE ) || defined( WIN32 )
372     p_ret = TlsGetValue( *p_tls );
373
374 #elif defined(LIBVLC_USE_PTHREAD)
375     p_ret = pthread_getspecific( *p_tls );
376
377 #endif
378
379     return p_ret;
380 }
381
382 # if defined (_POSIX_SPIN_LOCKS) && ((_POSIX_SPIN_LOCKS - 0) > 0)
383 typedef struct
384 {
385     pthread_spinlock_t  spin;
386 } vlc_spinlock_t;
387
388 /**
389  * Initializes a spinlock.
390  */
391 static inline int vlc_spin_init (vlc_spinlock_t *spin)
392 {
393     return pthread_spin_init (&spin->spin, PTHREAD_PROCESS_PRIVATE);
394 }
395
396 /**
397  * Acquires a spinlock.
398  */
399 static inline void vlc_spin_lock (vlc_spinlock_t *spin)
400 {
401     int val = pthread_spin_lock (&spin->spin);
402     assert (val == 0);
403     (void)val;
404 }
405
406 /**
407  * Releases a spinlock.
408  */
409 static inline void vlc_spin_unlock (vlc_spinlock_t *spin)
410 {
411     int val = pthread_spin_unlock (&spin->spin);
412     assert (val == 0);
413     (void)val;
414 }
415
416 /**
417  * Deinitializes a spinlock.
418  */
419 static inline void vlc_spin_destroy (vlc_spinlock_t *spin)
420 {
421     int val = pthread_spin_destroy (&spin->spin);
422     assert (val == 0);
423     (void)val;
424 }
425
426 #elif defined( WIN32 )
427
428 typedef CRITICAL_SECTION vlc_spinlock_t;
429
430 /**
431  * Initializes a spinlock.
432  */
433 static inline int vlc_spin_init (vlc_spinlock_t *spin)
434 {
435     return !InitializeCriticalSectionAndSpinCount(spin, 4000);
436 }
437
438 /**
439  * Acquires a spinlock.
440  */
441 static inline void vlc_spin_lock (vlc_spinlock_t *spin)
442 {
443     EnterCriticalSection(spin);
444 }
445
446 /**
447  * Releases a spinlock.
448  */
449 static inline void vlc_spin_unlock (vlc_spinlock_t *spin)
450 {
451     LeaveCriticalSection(spin);
452 }
453
454 /**
455  * Deinitializes a spinlock.
456  */
457 static inline void vlc_spin_destroy (vlc_spinlock_t *spin)
458 {
459     DeleteCriticalSection(spin);
460 }
461
462
463 #else
464
465 /* Fallback to plain mutexes if spinlocks are not available */
466 typedef vlc_mutex_t vlc_spinlock_t;
467
468 static inline int vlc_spin_init (vlc_spinlock_t *spin)
469 {
470     return __vlc_mutex_init (spin);
471 }
472
473 # define vlc_spin_lock    vlc_mutex_lock
474 # define vlc_spin_unlock  vlc_mutex_unlock
475 # define vlc_spin_destroy vlc_mutex_destroy
476 #endif
477
478 /*****************************************************************************
479  * vlc_thread_create: create a thread
480  *****************************************************************************/
481 #define vlc_thread_create( P_THIS, PSZ_NAME, FUNC, PRIORITY, WAIT )         \
482     __vlc_thread_create( VLC_OBJECT(P_THIS), __FILE__, __LINE__, PSZ_NAME, (void * ( * ) ( void * ))FUNC, PRIORITY, WAIT )
483
484 /*****************************************************************************
485  * vlc_thread_set_priority: set the priority of the calling thread
486  *****************************************************************************/
487 #define vlc_thread_set_priority( P_THIS, PRIORITY )                         \
488     __vlc_thread_set_priority( VLC_OBJECT(P_THIS), __FILE__, __LINE__, PRIORITY )
489
490 /*****************************************************************************
491  * vlc_thread_ready: tell the parent thread we were successfully spawned
492  *****************************************************************************/
493 #define vlc_thread_ready( P_THIS )                                          \
494     __vlc_thread_ready( VLC_OBJECT(P_THIS) )
495
496 /*****************************************************************************
497  * vlc_thread_join: wait until a thread exits
498  *****************************************************************************/
499 #define vlc_thread_join( P_THIS )                                           \
500     __vlc_thread_join( VLC_OBJECT(P_THIS), __FILE__, __LINE__ )
501
502 #endif