]> git.sesse.net Git - vlc/blob - include/vlc_threads_funcs.h
311b7451649c56f3238dff69c43abba2a762d3c2
[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 /*****************************************************************************
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 ) );
41
42
43 /*****************************************************************************
44  * vlc_threads_init: initialize threads system
45  *****************************************************************************/
46 #define vlc_threads_init( P_THIS )                                          \
47     __vlc_threads_init( VLC_OBJECT(P_THIS) )
48
49 /*****************************************************************************
50  * vlc_threads_end: deinitialize threads system
51  *****************************************************************************/
52 #define vlc_threads_end( P_THIS )                                           \
53     __vlc_threads_end( VLC_OBJECT(P_THIS) )
54
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 )
60
61 /*****************************************************************************
62  * vlc_mutex_lock: lock a mutex
63  *****************************************************************************/
64 #define vlc_mutex_lock( P_MUTEX )                                           \
65     __vlc_mutex_lock( __FILE__, __LINE__, P_MUTEX )
66
67 #ifdef __APPLE__
68 #   define CAST_PTHREAD_TO_INT(t) (unsigned long int)(uintptr_t)(void *)t
69 #else
70 #   define CAST_PTHREAD_TO_INT(t) (unsigned long int)t
71 #endif
72
73 static inline int __vlc_mutex_lock( const char * psz_file, int i_line,
74                                     vlc_mutex_t * p_mutex )
75 {
76     int i_result;
77     /* In case of error : */
78     unsigned long int i_thread = 0;
79     const char * psz_error = "";
80
81 #if defined( PTH_INIT_IN_PTH_H )
82     i_result = ( pth_mutex_acquire( &p_mutex->mutex, FALSE, NULL ) == FALSE );
83
84 #elif defined( ST_INIT_IN_ST_H )
85     i_result = st_mutex_lock( p_mutex->mutex );
86
87 #elif defined( UNDER_CE )
88     EnterCriticalSection( &p_mutex->csection );
89     i_result = 0;
90
91 #elif defined( WIN32 )
92     if( p_mutex->mutex )
93     {
94         WaitForSingleObject( p_mutex->mutex, INFINITE );
95     }
96     else
97     {
98         EnterCriticalSection( &p_mutex->csection );
99     }
100     i_result = 0;
101
102 #elif defined( HAVE_KERNEL_SCHEDULER_H )
103     if( p_mutex == NULL )
104     {
105         i_result = B_BAD_VALUE;
106     }
107     else if( p_mutex->init < 2000 )
108     {
109         i_result = B_NO_INIT;
110     }
111     else
112     {
113         i_result = acquire_sem( p_mutex->lock );
114     }
115
116 #elif defined( PTHREAD_COND_T_IN_PTHREAD_H )
117     i_result = pthread_mutex_lock( &p_mutex->mutex );
118     if ( i_result )
119     {
120         i_thread = CAST_PTHREAD_TO_INT(pthread_self());
121         psz_error = strerror(i_result);
122     }
123
124 #elif defined( HAVE_CTHREADS_H )
125     mutex_lock( p_mutex->mutex );
126     i_result = 0;
127
128 #endif
129
130     if( i_result )
131     {
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 );
135     }
136     return i_result;
137 }
138
139 /*****************************************************************************
140  * vlc_mutex_unlock: unlock a mutex
141  *****************************************************************************/
142 #define vlc_mutex_unlock( P_MUTEX )                                         \
143     __vlc_mutex_unlock( __FILE__, __LINE__, P_MUTEX )
144
145 static inline int __vlc_mutex_unlock( const char * psz_file, int i_line,
146                                       vlc_mutex_t *p_mutex )
147 {
148     int i_result;
149     /* In case of error : */
150     unsigned long int i_thread = 0;
151     const char * psz_error = "";
152
153 #if defined( PTH_INIT_IN_PTH_H )
154     i_result = ( pth_mutex_release( &p_mutex->mutex ) == FALSE );
155
156 #elif defined( ST_INIT_IN_ST_H )
157     i_result = st_mutex_unlock( p_mutex->mutex );
158
159 #elif defined( UNDER_CE )
160     LeaveCriticalSection( &p_mutex->csection );
161     i_result = 0;
162
163 #elif defined( WIN32 )
164     if( p_mutex->mutex )
165     {
166         ReleaseMutex( p_mutex->mutex );
167     }
168     else
169     {
170         LeaveCriticalSection( &p_mutex->csection );
171     }
172     i_result = 0;
173
174 #elif defined( HAVE_KERNEL_SCHEDULER_H )
175     if( p_mutex == NULL )
176     {
177         i_result = B_BAD_VALUE;
178     }
179     else if( p_mutex->init < 2000 )
180     {
181         i_result = B_NO_INIT;
182     }
183     else
184     {
185         release_sem( p_mutex->lock );
186         return B_OK;
187     }
188
189 #elif defined( PTHREAD_COND_T_IN_PTHREAD_H )
190     i_result = pthread_mutex_unlock( &p_mutex->mutex );
191     if ( i_result )
192     {
193         i_thread = CAST_PTHREAD_TO_INT(pthread_self());
194         psz_error = strerror(i_result);
195     }
196
197 #elif defined( HAVE_CTHREADS_H )
198     mutex_unlock( p_mutex );
199     i_result = 0;
200
201 #endif
202
203     if( i_result )
204     {
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 );
208     }
209
210     return i_result;
211 }
212
213 /*****************************************************************************
214  * vlc_mutex_destroy: destroy a mutex
215  *****************************************************************************/
216 #define vlc_mutex_destroy( P_MUTEX )                                        \
217     __vlc_mutex_destroy( __FILE__, __LINE__, P_MUTEX )
218
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 )
224
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 )
230
231 static inline int __vlc_cond_signal( const char * psz_file, int i_line,
232                                      vlc_cond_t *p_condvar )
233 {
234     int i_result;
235     /* In case of error : */
236     unsigned long int i_thread = 0;
237     const char * psz_error = "";
238
239 #if defined( PTH_INIT_IN_PTH_H )
240     i_result = ( pth_cond_notify( &p_condvar->cond, FALSE ) == FALSE );
241
242 #elif defined( ST_INIT_IN_ST_H )
243     i_result = st_cond_signal( p_condvar->cond );
244
245 #elif defined( UNDER_CE )
246     PulseEvent( p_condvar->event );
247     i_result = 0;
248
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 )
254     {
255         PulseEvent( p_condvar->event );
256     }
257     else if( p_condvar->i_win9x_cv == 1 )
258     {
259         /* Wait for the gate to be open */
260         WaitForSingleObject( p_condvar->event, INFINITE );
261
262         if( p_condvar->i_waiting_threads )
263         {
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 );
269
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 );
276         }
277     }
278     else
279     {
280         if( p_condvar->i_waiting_threads )
281         {
282             ReleaseSemaphore( p_condvar->semaphore, 1, 0 );
283
284             /* Wait for the last thread to be awakened */
285             WaitForSingleObject( p_condvar->event, INFINITE );
286         }
287     }
288     i_result = 0;
289
290 #elif defined( HAVE_KERNEL_SCHEDULER_H )
291     if( p_condvar == NULL )
292     {
293         i_result = B_BAD_VALUE;
294     }
295     else if( p_condvar->init < 2000 )
296     {
297         i_result = B_NO_INIT;
298     }
299     else
300     {
301         while( p_condvar->thread != -1 )
302         {
303             thread_info info;
304             if( get_thread_info(p_condvar->thread, &info) == B_BAD_VALUE )
305             {
306                 return 0;
307             }
308
309             if( info.state != B_THREAD_SUSPENDED )
310             {
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. */
315                 snooze( 10000 );
316             }
317             else
318             {
319                 /* Ok, we have to wake up that thread */
320                 resume_thread( p_condvar->thread );
321                 return 0;
322             }
323         }
324         i_result = 0;
325     }
326
327 #elif defined( PTHREAD_COND_T_IN_PTHREAD_H )
328     i_result = pthread_cond_signal( &p_condvar->cond );
329     if ( i_result )
330     {
331         i_thread = CAST_PTHREAD_TO_INT(pthread_self());
332         psz_error = strerror(i_result);
333     }
334
335 #elif defined( HAVE_CTHREADS_H )
336     /* condition_signal() */
337     if ( p_condvar->queue.head || p_condvar->implications )
338     {
339         cond_signal( (condition_t)p_condvar );
340     }
341     i_result = 0;
342
343 #endif
344
345     if( i_result )
346     {
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 );
350     }
351
352     return i_result;
353 }
354
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  )
360
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 )
363 {
364     int i_result;
365     /* In case of error : */
366     unsigned long int i_thread = 0;
367     const char * psz_error = "";
368
369 #if defined( PTH_INIT_IN_PTH_H )
370     i_result = ( pth_cond_await( &p_condvar->cond, &p_mutex->mutex, NULL )
371                  == FALSE );
372
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 );
377
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--;
383
384     /* Reacquire the mutex before returning. */
385     vlc_mutex_lock( p_mutex );
386
387     i_result = 0;
388
389 #elif defined( WIN32 )
390     if( !p_condvar->semaphore )
391     {
392         /* Increase our wait count */
393         p_condvar->i_waiting_threads++;
394
395         if( p_mutex->mutex )
396         {
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,
401                                             p_condvar->event,
402                                             INFINITE, FALSE );
403         }
404         else
405         {
406             LeaveCriticalSection( &p_mutex->csection );
407             WaitForSingleObject( p_condvar->event, INFINITE );
408         }
409
410         p_condvar->i_waiting_threads--;
411     }
412     else if( p_condvar->i_win9x_cv == 1 )
413     {
414         int i_waiting_threads;
415
416         /* Wait for the gate to be open */
417         WaitForSingleObject( p_condvar->event, INFINITE );
418
419         /* Increase our wait count */
420         p_condvar->i_waiting_threads++;
421
422         LeaveCriticalSection( &p_mutex->csection );
423         WaitForSingleObject( p_condvar->semaphore, INFINITE );
424
425         /* Decrement and test must be atomic */
426         EnterCriticalSection( &p_condvar->csection );
427
428         /* Decrease our wait count */
429         i_waiting_threads = --p_condvar->i_waiting_threads;
430
431         LeaveCriticalSection( &p_condvar->csection );
432
433         /* Reopen the gate if we were the last waiting thread */
434         if( !i_waiting_threads )
435             SetEvent( p_condvar->event );
436     }
437     else
438     {
439         int i_waiting_threads;
440
441         /* Increase our wait count */
442         p_condvar->i_waiting_threads++;
443
444         LeaveCriticalSection( &p_mutex->csection );
445         WaitForSingleObject( p_condvar->semaphore, INFINITE );
446
447         /* Decrement and test must be atomic */
448         EnterCriticalSection( &p_condvar->csection );
449
450         /* Decrease our wait count */
451         i_waiting_threads = --p_condvar->i_waiting_threads;
452
453         LeaveCriticalSection( &p_condvar->csection );
454
455         /* Signal that the last waiting thread just went through */
456         if( !i_waiting_threads )
457             SetEvent( p_condvar->event );
458     }
459
460     /* Reacquire the mutex before returning. */
461     vlc_mutex_lock( p_mutex );
462
463     i_result = 0;
464
465 #elif defined( HAVE_KERNEL_SCHEDULER_H )
466     if( p_condvar == NULL )
467     {
468         i_result = B_BAD_VALUE;
469     }
470     else if( p_mutex == NULL )
471     {
472         i_result = B_BAD_VALUE;
473     }
474     else if( p_condvar->init < 2000 )
475     {
476         i_result = B_NO_INIT;
477     }
478
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;
486
487     vlc_mutex_lock( p_mutex );
488     i_result = 0;
489
490 #elif defined( PTHREAD_COND_T_IN_PTHREAD_H )
491
492 #   ifdef DEBUG
493     /* In debug mode, timeout */
494     struct timeval now;
495     struct timespec timeout;
496
497     gettimeofday( &now, NULL );
498     timeout.tv_sec = now.tv_sec + THREAD_COND_TIMEOUT;
499     timeout.tv_nsec = now.tv_usec * 1000;
500
501     i_result = pthread_cond_timedwait( &p_condvar->cond, &p_mutex->mutex,
502                                        &timeout );
503
504     if( i_result == ETIMEDOUT )
505     {
506         msg_Dbg( p_condvar->p_this,
507                   "thread %li: probable condition deadlock "
508                   "at %s:%d (%s)", CAST_PTHREAD_TO_INT(pthread_self()),
509                   psz_file, i_line, strerror(i_result) );
510
511         i_result = pthread_cond_wait( &p_condvar->cond, &p_mutex->mutex );
512     }
513
514 #   else
515     i_result = pthread_cond_wait( &p_condvar->cond, &p_mutex->mutex );
516 #   endif
517
518     if ( i_result )
519     {
520         i_thread = CAST_PTHREAD_TO_INT(pthread_self());
521         psz_error = strerror(i_result);
522     }
523
524 #elif defined( HAVE_CTHREADS_H )
525     condition_wait( (condition_t)p_condvar, (mutex_t)p_mutex );
526     i_result = 0;
527
528 #endif
529
530     if( i_result )
531     {
532         msg_Err( p_condvar->p_this,
533                  "thread %li: cond_wait failed at %s:%d (%d:%s)",
534                  i_thread, psz_file, i_line, i_result, psz_error );
535     }
536
537     return i_result;
538 }
539
540 /*****************************************************************************
541  * vlc_cond_destroy: destroy a condition
542  *****************************************************************************/
543 #define vlc_cond_destroy( P_COND )                                          \
544     __vlc_cond_destroy( __FILE__, __LINE__, P_COND )
545
546 /*****************************************************************************
547  * vlc_thread_create: create a thread
548  *****************************************************************************/
549 #define vlc_thread_create( P_THIS, PSZ_NAME, FUNC, PRIORITY, WAIT )         \
550     __vlc_thread_create( VLC_OBJECT(P_THIS), __FILE__, __LINE__, PSZ_NAME, (void * ( * ) ( void * ))FUNC, PRIORITY, WAIT )
551
552 /*****************************************************************************
553  * vlc_thread_set_priority: set the priority of the calling thread
554  *****************************************************************************/
555 #define vlc_thread_set_priority( P_THIS, PRIORITY )                         \
556     __vlc_thread_set_priority( VLC_OBJECT(P_THIS), __FILE__, __LINE__, PRIORITY )
557
558 /*****************************************************************************
559  * vlc_thread_ready: tell the parent thread we were successfully spawned
560  *****************************************************************************/
561 #define vlc_thread_ready( P_THIS )                                          \
562     __vlc_thread_ready( VLC_OBJECT(P_THIS) )
563
564 /*****************************************************************************
565  * vlc_thread_join: wait until a thread exits
566  *****************************************************************************/
567 #define vlc_thread_join( P_THIS )                                           \
568     __vlc_thread_join( VLC_OBJECT(P_THIS), __FILE__, __LINE__ )