]> git.sesse.net Git - vlc/blob - include/vlc_threads_funcs.h
Add facilities to report and enrich error messages.
[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, 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 #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 /*****************************************************************************
36  * Function definitions
37  *****************************************************************************/
38 VLC_EXPORT( int,  __vlc_threads_init,  ( vlc_object_t * ) );
39 VLC_EXPORT( int,  __vlc_threads_end,   ( vlc_object_t * ) );
40 VLC_EXPORT( int,  __vlc_mutex_init,    ( vlc_object_t *, vlc_mutex_t * ) );
41 VLC_EXPORT( int,  __vlc_mutex_destroy, ( const char *, int, vlc_mutex_t * ) );
42 VLC_EXPORT( int,  __vlc_cond_init,     ( vlc_object_t *, vlc_cond_t * ) );
43 VLC_EXPORT( int,  __vlc_cond_destroy,  ( const char *, int, vlc_cond_t * ) );
44 VLC_EXPORT( int, __vlc_threadvar_create, (vlc_object_t *, vlc_threadvar_t * ) );
45 VLC_EXPORT( int,  __vlc_thread_create, ( vlc_object_t *, const char *, int, const char *, void * ( * ) ( void * ), int, vlc_bool_t ) );
46 VLC_EXPORT( int,  __vlc_thread_set_priority, ( vlc_object_t *, const char *, int, int ) );
47 VLC_EXPORT( void, __vlc_thread_ready,  ( vlc_object_t * ) );
48 VLC_EXPORT( void, __vlc_thread_join,   ( vlc_object_t *, const char *, int ) );
49
50
51 /*****************************************************************************
52  * vlc_threads_init: initialize threads system
53  *****************************************************************************/
54 #define vlc_threads_init( P_THIS )                                          \
55     __vlc_threads_init( VLC_OBJECT(P_THIS) )
56
57 /*****************************************************************************
58  * vlc_threads_end: deinitialize threads system
59  *****************************************************************************/
60 #define vlc_threads_end( P_THIS )                                           \
61     __vlc_threads_end( VLC_OBJECT(P_THIS) )
62
63 /*****************************************************************************
64  * vlc_mutex_init: initialize a mutex
65  *****************************************************************************/
66 #define vlc_mutex_init( P_THIS, P_MUTEX )                                   \
67     __vlc_mutex_init( VLC_OBJECT(P_THIS), P_MUTEX )
68
69 /*****************************************************************************
70  * vlc_mutex_lock: lock a mutex
71  *****************************************************************************/
72 #define vlc_mutex_lock( P_MUTEX )                                           \
73     __vlc_mutex_lock( __FILE__, __LINE__, P_MUTEX )
74
75 #ifdef __APPLE__
76 #   define CAST_PTHREAD_TO_INT(t) (unsigned long int)(uintptr_t)(void *)t
77 #else
78 #   define CAST_PTHREAD_TO_INT(t) (unsigned long int)t
79 #endif
80
81 static inline int __vlc_mutex_lock( const char * psz_file, int i_line,
82                                     vlc_mutex_t * p_mutex )
83 {
84     int i_result;
85     /* In case of error : */
86     unsigned long int i_thread = 0;
87     const char * psz_error = "";
88
89 #if defined( PTH_INIT_IN_PTH_H )
90     i_result = ( pth_mutex_acquire( &p_mutex->mutex, FALSE, NULL ) == FALSE );
91
92 #elif defined( ST_INIT_IN_ST_H )
93     i_result = st_mutex_lock( p_mutex->mutex );
94
95 #elif defined( UNDER_CE )
96     EnterCriticalSection( &p_mutex->csection );
97     i_result = 0;
98
99 #elif defined( WIN32 )
100     if( p_mutex->mutex )
101     {
102         WaitForSingleObject( p_mutex->mutex, INFINITE );
103     }
104     else
105     {
106         EnterCriticalSection( &p_mutex->csection );
107     }
108     i_result = 0;
109
110 #elif defined( HAVE_KERNEL_SCHEDULER_H )
111     if( p_mutex == NULL )
112     {
113         i_result = B_BAD_VALUE;
114     }
115     else if( p_mutex->init < 2000 )
116     {
117         i_result = B_NO_INIT;
118     }
119     else
120     {
121         i_result = acquire_sem( p_mutex->lock );
122     }
123
124 #elif defined( PTHREAD_COND_T_IN_PTHREAD_H )
125     i_result = pthread_mutex_lock( &p_mutex->mutex );
126     if ( i_result )
127     {
128         i_thread = CAST_PTHREAD_TO_INT(pthread_self());
129         psz_error = strerror(i_result);
130     }
131
132 #elif defined( HAVE_CTHREADS_H )
133     mutex_lock( p_mutex->mutex );
134     i_result = 0;
135
136 #endif
137
138     if( i_result )
139     {
140         msg_Err( p_mutex->p_this,
141                  "thread %li: mutex_lock failed at %s:%d (%d:%s)",
142                  i_thread, psz_file, i_line, i_result, psz_error );
143     }
144     return i_result;
145 }
146
147 /*****************************************************************************
148  * vlc_mutex_unlock: unlock a mutex
149  *****************************************************************************/
150 #define vlc_mutex_unlock( P_MUTEX )                                         \
151     __vlc_mutex_unlock( __FILE__, __LINE__, P_MUTEX )
152
153 static inline int __vlc_mutex_unlock( const char * psz_file, int i_line,
154                                       vlc_mutex_t *p_mutex )
155 {
156     int i_result;
157     /* In case of error : */
158     unsigned long int i_thread = 0;
159     const char * psz_error = "";
160
161 #if defined( PTH_INIT_IN_PTH_H )
162     i_result = ( pth_mutex_release( &p_mutex->mutex ) == FALSE );
163
164 #elif defined( ST_INIT_IN_ST_H )
165     i_result = st_mutex_unlock( p_mutex->mutex );
166
167 #elif defined( UNDER_CE )
168     LeaveCriticalSection( &p_mutex->csection );
169     i_result = 0;
170
171 #elif defined( WIN32 )
172     if( p_mutex->mutex )
173     {
174         ReleaseMutex( p_mutex->mutex );
175     }
176     else
177     {
178         LeaveCriticalSection( &p_mutex->csection );
179     }
180     i_result = 0;
181
182 #elif defined( HAVE_KERNEL_SCHEDULER_H )
183     if( p_mutex == NULL )
184     {
185         i_result = B_BAD_VALUE;
186     }
187     else if( p_mutex->init < 2000 )
188     {
189         i_result = B_NO_INIT;
190     }
191     else
192     {
193         release_sem( p_mutex->lock );
194         return B_OK;
195     }
196
197 #elif defined( PTHREAD_COND_T_IN_PTHREAD_H )
198     i_result = pthread_mutex_unlock( &p_mutex->mutex );
199     if ( i_result )
200     {
201         i_thread = CAST_PTHREAD_TO_INT(pthread_self());
202         psz_error = strerror(i_result);
203     }
204
205 #elif defined( HAVE_CTHREADS_H )
206     mutex_unlock( p_mutex );
207     i_result = 0;
208
209 #endif
210
211     if( i_result )
212     {
213         msg_Err( p_mutex->p_this,
214                  "thread %li: mutex_unlock failed at %s:%d (%d:%s)",
215                  i_thread, psz_file, i_line, i_result, psz_error );
216     }
217
218     return i_result;
219 }
220
221 /*****************************************************************************
222  * vlc_mutex_destroy: destroy a mutex
223  *****************************************************************************/
224 #define vlc_mutex_destroy( P_MUTEX )                                        \
225     __vlc_mutex_destroy( __FILE__, __LINE__, P_MUTEX )
226
227 /*****************************************************************************
228  * vlc_cond_init: initialize a condition
229  *****************************************************************************/
230 #define vlc_cond_init( P_THIS, P_COND )                                     \
231     __vlc_cond_init( VLC_OBJECT(P_THIS), P_COND )
232
233 /*****************************************************************************
234  * vlc_cond_signal: start a thread on condition completion
235  *****************************************************************************/
236 #define vlc_cond_signal( P_COND )                                           \
237     __vlc_cond_signal( __FILE__, __LINE__, P_COND )
238
239 static inline int __vlc_cond_signal( const char * psz_file, int i_line,
240                                      vlc_cond_t *p_condvar )
241 {
242     int i_result;
243     /* In case of error : */
244     unsigned long int i_thread = 0;
245     const char * psz_error = "";
246
247 #if defined( PTH_INIT_IN_PTH_H )
248     i_result = ( pth_cond_notify( &p_condvar->cond, FALSE ) == FALSE );
249
250 #elif defined( ST_INIT_IN_ST_H )
251     i_result = st_cond_signal( p_condvar->cond );
252
253 #elif defined( UNDER_CE )
254     PulseEvent( p_condvar->event );
255     i_result = 0;
256
257 #elif defined( WIN32 )
258     /* Release one waiting thread if one is available. */
259     /* For this trick to work properly, the vlc_cond_signal must be surrounded
260      * by a mutex. This will prevent another thread from stealing the signal */
261     if( !p_condvar->semaphore )
262     {
263         PulseEvent( p_condvar->event );
264     }
265     else if( p_condvar->i_win9x_cv == 1 )
266     {
267         /* Wait for the gate to be open */
268         WaitForSingleObject( p_condvar->event, INFINITE );
269
270         if( p_condvar->i_waiting_threads )
271         {
272             /* Using a semaphore exposes us to a race condition. It is
273              * possible for another thread to start waiting on the semaphore
274              * just after we signaled it and thus steal the signal.
275              * We have to prevent new threads from entering the cond_wait(). */
276             ResetEvent( p_condvar->event );
277
278             /* A semaphore is used here because Win9x doesn't have
279              * SignalObjectAndWait() and thus a race condition exists
280              * during the time we release the mutex and the time we start
281              * waiting on the event (more precisely, the signal can sometimes
282              * be missed by the waiting thread if we use PulseEvent()). */
283             ReleaseSemaphore( p_condvar->semaphore, 1, 0 );
284         }
285     }
286     else
287     {
288         if( p_condvar->i_waiting_threads )
289         {
290             ReleaseSemaphore( p_condvar->semaphore, 1, 0 );
291
292             /* Wait for the last thread to be awakened */
293             WaitForSingleObject( p_condvar->event, INFINITE );
294         }
295     }
296     i_result = 0;
297
298 #elif defined( HAVE_KERNEL_SCHEDULER_H )
299     if( p_condvar == NULL )
300     {
301         i_result = B_BAD_VALUE;
302     }
303     else if( p_condvar->init < 2000 )
304     {
305         i_result = B_NO_INIT;
306     }
307     else
308     {
309         while( p_condvar->thread != -1 )
310         {
311             thread_info info;
312             if( get_thread_info(p_condvar->thread, &info) == B_BAD_VALUE )
313             {
314                 return 0;
315             }
316
317             if( info.state != B_THREAD_SUSPENDED )
318             {
319                 /* The  waiting thread is not suspended so it could
320                  * have been interrupted beetwen the unlock and the
321                  * suspend_thread line. That is why we sleep a little
322                  * before retesting p_condver->thread. */
323                 snooze( 10000 );
324             }
325             else
326             {
327                 /* Ok, we have to wake up that thread */
328                 resume_thread( p_condvar->thread );
329                 return 0;
330             }
331         }
332         i_result = 0;
333     }
334
335 #elif defined( PTHREAD_COND_T_IN_PTHREAD_H )
336     i_result = pthread_cond_signal( &p_condvar->cond );
337     if ( i_result )
338     {
339         i_thread = CAST_PTHREAD_TO_INT(pthread_self());
340         psz_error = strerror(i_result);
341     }
342
343 #elif defined( HAVE_CTHREADS_H )
344     /* condition_signal() */
345     if ( p_condvar->queue.head || p_condvar->implications )
346     {
347         cond_signal( (condition_t)p_condvar );
348     }
349     i_result = 0;
350
351 #endif
352
353     if( i_result )
354     {
355         msg_Err( p_condvar->p_this,
356                  "thread %li: cond_signal failed at %s:%d (%d:%s)",
357                  i_thread, psz_file, i_line, i_result, psz_error );
358     }
359
360     return i_result;
361 }
362
363 /*****************************************************************************
364  * vlc_cond_wait: wait until condition completion
365  *****************************************************************************/
366 #define vlc_cond_wait( P_COND, P_MUTEX )                                     \
367     __vlc_cond_wait( __FILE__, __LINE__, P_COND, P_MUTEX  )
368
369 static inline int __vlc_cond_wait( const char * psz_file, int i_line,
370                                    vlc_cond_t *p_condvar, vlc_mutex_t *p_mutex )
371 {
372     int i_result;
373     /* In case of error : */
374     unsigned long int i_thread = 0;
375     const char * psz_error = "";
376
377 #if defined( PTH_INIT_IN_PTH_H )
378     i_result = ( pth_cond_await( &p_condvar->cond, &p_mutex->mutex, NULL )
379                  == FALSE );
380
381 #elif defined( ST_INIT_IN_ST_H )
382     st_mutex_unlock( p_mutex->mutex );
383     i_result = st_cond_wait( p_condvar->cond );
384     st_mutex_lock( p_mutex->mutex );
385
386 #elif defined( UNDER_CE )
387     p_condvar->i_waiting_threads++;
388     LeaveCriticalSection( &p_mutex->csection );
389     WaitForSingleObject( p_condvar->event, INFINITE );
390     p_condvar->i_waiting_threads--;
391
392     /* Reacquire the mutex before returning. */
393     vlc_mutex_lock( p_mutex );
394
395     i_result = 0;
396
397 #elif defined( WIN32 )
398     if( !p_condvar->semaphore )
399     {
400         /* Increase our wait count */
401         p_condvar->i_waiting_threads++;
402
403         if( p_mutex->mutex )
404         {
405             /* It is only possible to atomically release the mutex and
406              * initiate the waiting on WinNT/2K/XP. Win9x doesn't have
407              * SignalObjectAndWait(). */
408             p_condvar->SignalObjectAndWait( p_mutex->mutex,
409                                             p_condvar->event,
410                                             INFINITE, FALSE );
411         }
412         else
413         {
414             LeaveCriticalSection( &p_mutex->csection );
415             WaitForSingleObject( p_condvar->event, INFINITE );
416         }
417
418         p_condvar->i_waiting_threads--;
419     }
420     else if( p_condvar->i_win9x_cv == 1 )
421     {
422         int i_waiting_threads;
423
424         /* Wait for the gate to be open */
425         WaitForSingleObject( p_condvar->event, INFINITE );
426
427         /* Increase our wait count */
428         p_condvar->i_waiting_threads++;
429
430         LeaveCriticalSection( &p_mutex->csection );
431         WaitForSingleObject( p_condvar->semaphore, INFINITE );
432
433         /* Decrement and test must be atomic */
434         EnterCriticalSection( &p_condvar->csection );
435
436         /* Decrease our wait count */
437         i_waiting_threads = --p_condvar->i_waiting_threads;
438
439         LeaveCriticalSection( &p_condvar->csection );
440
441         /* Reopen the gate if we were the last waiting thread */
442         if( !i_waiting_threads )
443             SetEvent( p_condvar->event );
444     }
445     else
446     {
447         int i_waiting_threads;
448
449         /* Increase our wait count */
450         p_condvar->i_waiting_threads++;
451
452         LeaveCriticalSection( &p_mutex->csection );
453         WaitForSingleObject( p_condvar->semaphore, INFINITE );
454
455         /* Decrement and test must be atomic */
456         EnterCriticalSection( &p_condvar->csection );
457
458         /* Decrease our wait count */
459         i_waiting_threads = --p_condvar->i_waiting_threads;
460
461         LeaveCriticalSection( &p_condvar->csection );
462
463         /* Signal that the last waiting thread just went through */
464         if( !i_waiting_threads )
465             SetEvent( p_condvar->event );
466     }
467
468     /* Reacquire the mutex before returning. */
469     vlc_mutex_lock( p_mutex );
470
471     i_result = 0;
472
473 #elif defined( HAVE_KERNEL_SCHEDULER_H )
474     if( p_condvar == NULL )
475     {
476         i_result = B_BAD_VALUE;
477     }
478     else if( p_mutex == NULL )
479     {
480         i_result = B_BAD_VALUE;
481     }
482     else if( p_condvar->init < 2000 )
483     {
484         i_result = B_NO_INIT;
485     }
486
487     /* The p_condvar->thread var is initialized before the unlock because
488      * it enables to identify when the thread is interrupted beetwen the
489      * unlock line and the suspend_thread line */
490     p_condvar->thread = find_thread( NULL );
491     vlc_mutex_unlock( p_mutex );
492     suspend_thread( p_condvar->thread );
493     p_condvar->thread = -1;
494
495     vlc_mutex_lock( p_mutex );
496     i_result = 0;
497
498 #elif defined( PTHREAD_COND_T_IN_PTHREAD_H )
499
500 #   ifdef DEBUG
501     /* In debug mode, timeout */
502     struct timeval now;
503     struct timespec timeout;
504
505     gettimeofday( &now, NULL );
506     timeout.tv_sec = now.tv_sec + THREAD_COND_TIMEOUT;
507     timeout.tv_nsec = now.tv_usec * 1000;
508
509     i_result = pthread_cond_timedwait( &p_condvar->cond, &p_mutex->mutex,
510                                        &timeout );
511
512     if( i_result == ETIMEDOUT )
513     {
514         msg_Dbg( p_condvar->p_this,
515                   "thread %li: possible condition deadlock "
516                   "at %s:%d (%s)", CAST_PTHREAD_TO_INT(pthread_self()),
517                   psz_file, i_line, strerror(i_result) );
518
519         i_result = pthread_cond_wait( &p_condvar->cond, &p_mutex->mutex );
520     }
521
522 #   else
523     i_result = pthread_cond_wait( &p_condvar->cond, &p_mutex->mutex );
524 #   endif
525
526     if ( i_result )
527     {
528         i_thread = CAST_PTHREAD_TO_INT(pthread_self());
529         psz_error = strerror(i_result);
530     }
531
532 #elif defined( HAVE_CTHREADS_H )
533     condition_wait( (condition_t)p_condvar, (mutex_t)p_mutex );
534     i_result = 0;
535
536 #endif
537
538     if( i_result )
539     {
540         msg_Err( p_condvar->p_this,
541                  "thread %li: cond_wait failed at %s:%d (%d:%s)",
542                  i_thread, psz_file, i_line, i_result, psz_error );
543     }
544
545     return i_result;
546 }
547
548 /*****************************************************************************
549  * vlc_cond_destroy: destroy a condition
550  *****************************************************************************/
551 #define vlc_cond_destroy( P_COND )                                          \
552     __vlc_cond_destroy( __FILE__, __LINE__, P_COND )
553
554 /*****************************************************************************
555  * vlc_threadvar_create: create a thread-local variable
556  *****************************************************************************/
557 #define vlc_threadvar_create( PTHIS, P_TLS )                                 \
558    __vlc_threadvar_create( PTHIS, P_TLS )
559
560 /*****************************************************************************
561  * vlc_threadvar_set: create: set the value of a thread-local variable
562  *****************************************************************************/
563 static inline int vlc_threadvar_set( vlc_threadvar_t * p_tls, void *p_value )
564 {
565     int i_ret;
566
567 #if defined( PTH_INIT_IN_PTH_H )
568     return pth_key_setdata( p_tls->handle, p_value );
569 #elif  defined( ST_INIT_IN_ST_H )
570     return st_thread_setspecific( p_tls->handle, p_value );
571 #elif defined( HAVE_KERNEL_SCHEDULER_H )
572     return -1;
573
574 #elif defined( UNDER_CE ) || defined( WIN32 )
575     i_ret = ( TlsSetValue( p_tls->handle, p_value ) != 0 );
576
577 #elif defined( PTHREAD_COND_T_IN_PTHREAD_H )
578     i_ret = pthread_setspecific( p_tls->handle, p_value );
579
580 #elif defined( HAVE_CTHREADS_H )
581     i_ret = cthread_setspecific( p_tls->handle, p_value );
582 #endif
583
584     return i_ret;
585 }
586
587 /*****************************************************************************
588  * vlc_threadvar_get: create: get the value of a thread-local variable
589  *****************************************************************************/
590 static inline void* vlc_threadvar_get( vlc_threadvar_t * p_tls )
591 {
592     void* p_ret;
593
594 #if defined( PTH_INIT_IN_PTH_H )
595     p_ret = pth_key_getdata( p_handle->key );
596 #elif defined( ST_INIT_IN_ST_H )
597     p_ret = st_thread_getspecific( p_handle->key );
598 #elif defined( HAVE_KERNEL_SCHEDULER_H )
599     p_ret = NULL;
600 #elif defined( UNDER_CE ) || defined( WIN32 )
601     p_ret = TlsGetValue( p_tls->handle );
602
603 #elif defined( PTHREAD_COND_T_IN_PTHREAD_H )
604     p_ret = pthread_getspecific( p_tls->handle );
605
606 #elif defined( HAVE_CTHREADS_H )
607     if ( !cthread_getspecific( p_tls->handle, &p_ret ) )
608     {
609         p_ret = NULL;
610     }
611 #endif
612
613     return p_ret;
614 }
615
616 /*****************************************************************************
617  * vlc_thread_create: create a thread
618  *****************************************************************************/
619 #define vlc_thread_create( P_THIS, PSZ_NAME, FUNC, PRIORITY, WAIT )         \
620     __vlc_thread_create( VLC_OBJECT(P_THIS), __FILE__, __LINE__, PSZ_NAME, (void * ( * ) ( void * ))FUNC, PRIORITY, WAIT )
621
622 /*****************************************************************************
623  * vlc_thread_set_priority: set the priority of the calling thread
624  *****************************************************************************/
625 #define vlc_thread_set_priority( P_THIS, PRIORITY )                         \
626     __vlc_thread_set_priority( VLC_OBJECT(P_THIS), __FILE__, __LINE__, PRIORITY )
627
628 /*****************************************************************************
629  * vlc_thread_ready: tell the parent thread we were successfully spawned
630  *****************************************************************************/
631 #define vlc_thread_ready( P_THIS )                                          \
632     __vlc_thread_ready( VLC_OBJECT(P_THIS) )
633
634 /*****************************************************************************
635  * vlc_thread_join: wait until a thread exits
636  *****************************************************************************/
637 #define vlc_thread_join( P_THIS )                                           \
638     __vlc_thread_join( VLC_OBJECT(P_THIS), __FILE__, __LINE__ )
639
640 #endif