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, 2002 the VideoLAN team
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
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.
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.
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 *****************************************************************************/
28 /*****************************************************************************
29 * Function definitions
30 *****************************************************************************/
31 VLC_EXPORT( int, __vlc_threads_init, ( vlc_object_t * ) );
32 VLC_EXPORT( int, __vlc_threads_end, ( vlc_object_t * ) );
33 VLC_EXPORT( int, __vlc_mutex_init, ( vlc_object_t *, vlc_mutex_t * ) );
34 VLC_EXPORT( int, __vlc_mutex_destroy, ( const char *, int, vlc_mutex_t * ) );
35 VLC_EXPORT( int, __vlc_cond_init, ( vlc_object_t *, vlc_cond_t * ) );
36 VLC_EXPORT( int, __vlc_cond_destroy, ( const char *, int, vlc_cond_t * ) );
37 VLC_EXPORT( int, __vlc_thread_create, ( vlc_object_t *, const char *, int, const char *, void * ( * ) ( void * ), int, vlc_bool_t ) );
38 VLC_EXPORT( int, __vlc_thread_set_priority, ( vlc_object_t *, const char *, int, int ) );
39 VLC_EXPORT( void, __vlc_thread_ready, ( vlc_object_t * ) );
40 VLC_EXPORT( void, __vlc_thread_join, ( vlc_object_t *, const char *, int ) );
43 /*****************************************************************************
44 * vlc_threads_init: initialize threads system
45 *****************************************************************************/
46 #define vlc_threads_init( P_THIS ) \
47 __vlc_threads_init( VLC_OBJECT(P_THIS) )
49 /*****************************************************************************
50 * vlc_threads_end: deinitialize threads system
51 *****************************************************************************/
52 #define vlc_threads_end( P_THIS ) \
53 __vlc_threads_end( VLC_OBJECT(P_THIS) )
55 /*****************************************************************************
56 * vlc_mutex_init: initialize a mutex
57 *****************************************************************************/
58 #define vlc_mutex_init( P_THIS, P_MUTEX ) \
59 __vlc_mutex_init( VLC_OBJECT(P_THIS), P_MUTEX )
61 /*****************************************************************************
62 * vlc_mutex_lock: lock a mutex
63 *****************************************************************************/
64 #define vlc_mutex_lock( P_MUTEX ) \
65 __vlc_mutex_lock( __FILE__, __LINE__, P_MUTEX )
68 # define CAST_PTHREAD_TO_INT(t) (unsigned long int)(uintptr_t)(void *)t
70 # define CAST_PTHREAD_TO_INT(t) (unsigned long int)t
73 static inline int __vlc_mutex_lock( const char * psz_file, int i_line,
74 vlc_mutex_t * p_mutex )
77 /* In case of error : */
78 unsigned long int i_thread = 0;
79 const char * psz_error = "";
81 #if defined( PTH_INIT_IN_PTH_H )
82 i_result = ( pth_mutex_acquire( &p_mutex->mutex, FALSE, NULL ) == FALSE );
84 #elif defined( ST_INIT_IN_ST_H )
85 i_result = st_mutex_lock( p_mutex->mutex );
87 #elif defined( UNDER_CE )
88 EnterCriticalSection( &p_mutex->csection );
91 #elif defined( WIN32 )
94 WaitForSingleObject( p_mutex->mutex, INFINITE );
98 EnterCriticalSection( &p_mutex->csection );
102 #elif defined( HAVE_KERNEL_SCHEDULER_H )
103 if( p_mutex == NULL )
105 i_result = B_BAD_VALUE;
107 else if( p_mutex->init < 2000 )
109 i_result = B_NO_INIT;
113 i_result = acquire_sem( p_mutex->lock );
116 #elif defined( PTHREAD_COND_T_IN_PTHREAD_H )
117 i_result = pthread_mutex_lock( &p_mutex->mutex );
120 i_thread = CAST_PTHREAD_TO_INT(pthread_self());
121 psz_error = strerror(i_result);
124 #elif defined( HAVE_CTHREADS_H )
125 mutex_lock( p_mutex->mutex );
132 msg_Err( p_mutex->p_this,
133 "thread %li: mutex_lock failed at %s:%d (%d:%s)",
134 i_thread, psz_file, i_line, i_result, psz_error );
139 /*****************************************************************************
140 * vlc_mutex_unlock: unlock a mutex
141 *****************************************************************************/
142 #define vlc_mutex_unlock( P_MUTEX ) \
143 __vlc_mutex_unlock( __FILE__, __LINE__, P_MUTEX )
145 static inline int __vlc_mutex_unlock( const char * psz_file, int i_line,
146 vlc_mutex_t *p_mutex )
149 /* In case of error : */
150 unsigned long int i_thread = 0;
151 const char * psz_error = "";
153 #if defined( PTH_INIT_IN_PTH_H )
154 i_result = ( pth_mutex_release( &p_mutex->mutex ) == FALSE );
156 #elif defined( ST_INIT_IN_ST_H )
157 i_result = st_mutex_unlock( p_mutex->mutex );
159 #elif defined( UNDER_CE )
160 LeaveCriticalSection( &p_mutex->csection );
163 #elif defined( WIN32 )
166 ReleaseMutex( p_mutex->mutex );
170 LeaveCriticalSection( &p_mutex->csection );
174 #elif defined( HAVE_KERNEL_SCHEDULER_H )
175 if( p_mutex == NULL )
177 i_result = B_BAD_VALUE;
179 else if( p_mutex->init < 2000 )
181 i_result = B_NO_INIT;
185 release_sem( p_mutex->lock );
189 #elif defined( PTHREAD_COND_T_IN_PTHREAD_H )
190 i_result = pthread_mutex_unlock( &p_mutex->mutex );
193 i_thread = CAST_PTHREAD_TO_INT(pthread_self());
194 psz_error = strerror(i_result);
197 #elif defined( HAVE_CTHREADS_H )
198 mutex_unlock( p_mutex );
205 msg_Err( p_mutex->p_this,
206 "thread %li: mutex_unlock failed at %s:%d (%d:%s)",
207 i_thread, psz_file, i_line, i_result, psz_error );
213 /*****************************************************************************
214 * vlc_mutex_destroy: destroy a mutex
215 *****************************************************************************/
216 #define vlc_mutex_destroy( P_MUTEX ) \
217 __vlc_mutex_destroy( __FILE__, __LINE__, P_MUTEX )
219 /*****************************************************************************
220 * vlc_cond_init: initialize a condition
221 *****************************************************************************/
222 #define vlc_cond_init( P_THIS, P_COND ) \
223 __vlc_cond_init( VLC_OBJECT(P_THIS), P_COND )
225 /*****************************************************************************
226 * vlc_cond_signal: start a thread on condition completion
227 *****************************************************************************/
228 #define vlc_cond_signal( P_COND ) \
229 __vlc_cond_signal( __FILE__, __LINE__, P_COND )
231 static inline int __vlc_cond_signal( const char * psz_file, int i_line,
232 vlc_cond_t *p_condvar )
235 /* In case of error : */
236 unsigned long int i_thread = 0;
237 const char * psz_error = "";
239 #if defined( PTH_INIT_IN_PTH_H )
240 i_result = ( pth_cond_notify( &p_condvar->cond, FALSE ) == FALSE );
242 #elif defined( ST_INIT_IN_ST_H )
243 i_result = st_cond_signal( p_condvar->cond );
245 #elif defined( UNDER_CE )
246 PulseEvent( p_condvar->event );
249 #elif defined( WIN32 )
250 /* Release one waiting thread if one is available. */
251 /* For this trick to work properly, the vlc_cond_signal must be surrounded
252 * by a mutex. This will prevent another thread from stealing the signal */
253 if( !p_condvar->semaphore )
255 PulseEvent( p_condvar->event );
257 else if( p_condvar->i_win9x_cv == 1 )
259 /* Wait for the gate to be open */
260 WaitForSingleObject( p_condvar->event, INFINITE );
262 if( p_condvar->i_waiting_threads )
264 /* Using a semaphore exposes us to a race condition. It is
265 * possible for another thread to start waiting on the semaphore
266 * just after we signaled it and thus steal the signal.
267 * We have to prevent new threads from entering the cond_wait(). */
268 ResetEvent( p_condvar->event );
270 /* A semaphore is used here because Win9x doesn't have
271 * SignalObjectAndWait() and thus a race condition exists
272 * during the time we release the mutex and the time we start
273 * waiting on the event (more precisely, the signal can sometimes
274 * be missed by the waiting thread if we use PulseEvent()). */
275 ReleaseSemaphore( p_condvar->semaphore, 1, 0 );
280 if( p_condvar->i_waiting_threads )
282 ReleaseSemaphore( p_condvar->semaphore, 1, 0 );
284 /* Wait for the last thread to be awakened */
285 WaitForSingleObject( p_condvar->event, INFINITE );
290 #elif defined( HAVE_KERNEL_SCHEDULER_H )
291 if( p_condvar == NULL )
293 i_result = B_BAD_VALUE;
295 else if( p_condvar->init < 2000 )
297 i_result = B_NO_INIT;
301 while( p_condvar->thread != -1 )
304 if( get_thread_info(p_condvar->thread, &info) == B_BAD_VALUE )
309 if( info.state != B_THREAD_SUSPENDED )
311 /* The waiting thread is not suspended so it could
312 * have been interrupted beetwen the unlock and the
313 * suspend_thread line. That is why we sleep a little
314 * before retesting p_condver->thread. */
319 /* Ok, we have to wake up that thread */
320 resume_thread( p_condvar->thread );
327 #elif defined( PTHREAD_COND_T_IN_PTHREAD_H )
328 i_result = pthread_cond_signal( &p_condvar->cond );
331 i_thread = CAST_PTHREAD_TO_INT(pthread_self());
332 psz_error = strerror(i_result);
335 #elif defined( HAVE_CTHREADS_H )
336 /* condition_signal() */
337 if ( p_condvar->queue.head || p_condvar->implications )
339 cond_signal( (condition_t)p_condvar );
347 msg_Err( p_condvar->p_this,
348 "thread %li: cond_signal failed at %s:%d (%d:%s)",
349 i_thread, psz_file, i_line, i_result, psz_error );
355 /*****************************************************************************
356 * vlc_cond_wait: wait until condition completion
357 *****************************************************************************/
358 #define vlc_cond_wait( P_COND, P_MUTEX ) \
359 __vlc_cond_wait( __FILE__, __LINE__, P_COND, P_MUTEX )
361 static inline int __vlc_cond_wait( const char * psz_file, int i_line,
362 vlc_cond_t *p_condvar, vlc_mutex_t *p_mutex )
365 /* In case of error : */
366 unsigned long int i_thread = 0;
367 const char * psz_error = "";
369 #if defined( PTH_INIT_IN_PTH_H )
370 i_result = ( pth_cond_await( &p_condvar->cond, &p_mutex->mutex, NULL )
373 #elif defined( ST_INIT_IN_ST_H )
374 st_mutex_unlock( p_mutex->mutex );
375 i_result = st_cond_wait( p_condvar->cond );
376 st_mutex_lock( p_mutex->mutex );
378 #elif defined( UNDER_CE )
379 p_condvar->i_waiting_threads++;
380 LeaveCriticalSection( &p_mutex->csection );
381 WaitForSingleObject( p_condvar->event, INFINITE );
382 p_condvar->i_waiting_threads--;
384 /* Reacquire the mutex before returning. */
385 vlc_mutex_lock( p_mutex );
389 #elif defined( WIN32 )
390 if( !p_condvar->semaphore )
392 /* Increase our wait count */
393 p_condvar->i_waiting_threads++;
397 /* It is only possible to atomically release the mutex and
398 * initiate the waiting on WinNT/2K/XP. Win9x doesn't have
399 * SignalObjectAndWait(). */
400 p_condvar->SignalObjectAndWait( p_mutex->mutex,
406 LeaveCriticalSection( &p_mutex->csection );
407 WaitForSingleObject( p_condvar->event, INFINITE );
410 p_condvar->i_waiting_threads--;
412 else if( p_condvar->i_win9x_cv == 1 )
414 int i_waiting_threads;
416 /* Wait for the gate to be open */
417 WaitForSingleObject( p_condvar->event, INFINITE );
419 /* Increase our wait count */
420 p_condvar->i_waiting_threads++;
422 LeaveCriticalSection( &p_mutex->csection );
423 WaitForSingleObject( p_condvar->semaphore, INFINITE );
425 /* Decrement and test must be atomic */
426 EnterCriticalSection( &p_condvar->csection );
428 /* Decrease our wait count */
429 i_waiting_threads = --p_condvar->i_waiting_threads;
431 LeaveCriticalSection( &p_condvar->csection );
433 /* Reopen the gate if we were the last waiting thread */
434 if( !i_waiting_threads )
435 SetEvent( p_condvar->event );
439 int i_waiting_threads;
441 /* Increase our wait count */
442 p_condvar->i_waiting_threads++;
444 LeaveCriticalSection( &p_mutex->csection );
445 WaitForSingleObject( p_condvar->semaphore, INFINITE );
447 /* Decrement and test must be atomic */
448 EnterCriticalSection( &p_condvar->csection );
450 /* Decrease our wait count */
451 i_waiting_threads = --p_condvar->i_waiting_threads;
453 LeaveCriticalSection( &p_condvar->csection );
455 /* Signal that the last waiting thread just went through */
456 if( !i_waiting_threads )
457 SetEvent( p_condvar->event );
460 /* Reacquire the mutex before returning. */
461 vlc_mutex_lock( p_mutex );
465 #elif defined( HAVE_KERNEL_SCHEDULER_H )
466 if( p_condvar == NULL )
468 i_result = B_BAD_VALUE;
470 else if( p_mutex == NULL )
472 i_result = B_BAD_VALUE;
474 else if( p_condvar->init < 2000 )
476 i_result = B_NO_INIT;
479 /* The p_condvar->thread var is initialized before the unlock because
480 * it enables to identify when the thread is interrupted beetwen the
481 * unlock line and the suspend_thread line */
482 p_condvar->thread = find_thread( NULL );
483 vlc_mutex_unlock( p_mutex );
484 suspend_thread( p_condvar->thread );
485 p_condvar->thread = -1;
487 vlc_mutex_lock( p_mutex );
490 #elif defined( PTHREAD_COND_T_IN_PTHREAD_H )
493 /* In debug mode, timeout */
495 struct timespec timeout;
497 gettimeofday( &now, NULL );
498 timeout.tv_sec = now.tv_sec + THREAD_COND_TIMEOUT;
499 timeout.tv_nsec = now.tv_usec * 1000;
501 i_result = pthread_cond_timedwait( &p_condvar->cond, &p_mutex->mutex,
504 if( i_result == ETIMEDOUT )
506 /* People keep pissing me off with this. --Meuuh */
507 msg_Dbg( p_condvar->p_this,
508 "thread %li: secret message triggered "
509 "at %s:%d (%s)", CAST_PTHREAD_TO_INT(pthread_self()),
510 psz_file, i_line, strerror(i_result) );
512 i_result = pthread_cond_wait( &p_condvar->cond, &p_mutex->mutex );
516 i_result = pthread_cond_wait( &p_condvar->cond, &p_mutex->mutex );
521 i_thread = CAST_PTHREAD_TO_INT(pthread_self());
522 psz_error = strerror(i_result);
525 #elif defined( HAVE_CTHREADS_H )
526 condition_wait( (condition_t)p_condvar, (mutex_t)p_mutex );
533 msg_Err( p_condvar->p_this,
534 "thread %li: cond_wait failed at %s:%d (%d:%s)",
535 i_thread, psz_file, i_line, i_result, psz_error );
541 /*****************************************************************************
542 * vlc_cond_destroy: destroy a condition
543 *****************************************************************************/
544 #define vlc_cond_destroy( P_COND ) \
545 __vlc_cond_destroy( __FILE__, __LINE__, P_COND )
547 /*****************************************************************************
548 * vlc_thread_create: create a thread
549 *****************************************************************************/
550 #define vlc_thread_create( P_THIS, PSZ_NAME, FUNC, PRIORITY, WAIT ) \
551 __vlc_thread_create( VLC_OBJECT(P_THIS), __FILE__, __LINE__, PSZ_NAME, (void * ( * ) ( void * ))FUNC, PRIORITY, WAIT )
553 /*****************************************************************************
554 * vlc_thread_set_priority: set the priority of the calling thread
555 *****************************************************************************/
556 #define vlc_thread_set_priority( P_THIS, PRIORITY ) \
557 __vlc_thread_set_priority( VLC_OBJECT(P_THIS), __FILE__, __LINE__, PRIORITY )
559 /*****************************************************************************
560 * vlc_thread_ready: tell the parent thread we were successfully spawned
561 *****************************************************************************/
562 #define vlc_thread_ready( P_THIS ) \
563 __vlc_thread_ready( VLC_OBJECT(P_THIS) )
565 /*****************************************************************************
566 * vlc_thread_join: wait until a thread exits
567 *****************************************************************************/
568 #define vlc_thread_join( P_THIS ) \
569 __vlc_thread_join( VLC_OBJECT(P_THIS), __FILE__, __LINE__ )