]> git.sesse.net Git - vlc/blob - src/misc/threads.c
* ./modules/audio_output/oss.c: compilation fixes.
[vlc] / src / misc / threads.c
1 /*****************************************************************************
2  * threads.c : threads implementation for the VideoLAN client
3  *****************************************************************************
4  * Copyright (C) 1999, 2000, 2001, 2002 VideoLAN
5  * $Id: threads.c,v 1.12 2002/08/08 00:35:11 sam Exp $
6  *
7  * Authors: Jean-Marc Dressler <polux@via.ecp.fr>
8  *          Samuel Hocevar <sam@zoy.org>
9  *          Gildas Bazin <gbazin@netcourrier.com>
10  *
11  * This program is free software; you can redistribute it and/or modify
12  * it under the terms of the GNU General Public License as published by
13  * the Free Software Foundation; either version 2 of the License, or
14  * (at your option) any later version.
15  * 
16  * This program is distributed in the hope that it will be useful,
17  * but WITHOUT ANY WARRANTY; without even the implied warranty of
18  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
19  * GNU General Public License for more details.
20  *
21  * You should have received a copy of the GNU General Public License
22  * along with this program; if not, write to the Free Software
23  * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA  02111, USA.
24  *****************************************************************************/
25
26 #include <vlc/vlc.h>
27
28 #define VLC_THREADS_UNINITIALIZED  0
29 #define VLC_THREADS_PENDING        1
30 #define VLC_THREADS_ERROR          2
31 #define VLC_THREADS_READY          3
32
33 /*****************************************************************************
34  * Prototype for GPROF wrapper
35  *****************************************************************************/
36 #ifdef GPROF
37 /* Wrapper function for profiling */
38 static void *      vlc_thread_wrapper ( void *p_wrapper );
39
40 #   ifdef WIN32
41
42 #       define ITIMER_REAL 1
43 #       define ITIMER_PROF 2
44
45 struct itimerval
46 {
47     struct timeval it_value;
48     struct timeval it_interval;
49 };  
50
51 int setitimer(int kind, const struct itimerval* itnew, struct itimerval* itold);
52 #   endif /* WIN32 */
53
54 typedef struct wrapper_t
55 {
56     /* Data lock access */
57     vlc_mutex_t lock;
58     vlc_cond_t  wait;
59     
60     /* Data used to spawn the real thread */
61     vlc_thread_func_t func;
62     void *p_data;
63     
64     /* Profiling timer passed to the thread */
65     struct itimerval itimer;
66     
67 } wrapper_t;
68
69 #endif /* GPROF */
70
71 /*****************************************************************************
72  * Global mutexes for lazy initialization of the threads system
73  *****************************************************************************/
74 static volatile int i_initializations = 0;
75
76 #if defined( PTH_INIT_IN_PTH_H )
77     /* Unimplemented */
78 #elif defined( ST_INIT_IN_ST_H )
79     /* Unimplemented */
80 #elif defined( WIN32 )
81     /* Unimplemented */
82 #elif defined( PTHREAD_COND_T_IN_PTHREAD_H )
83     static pthread_mutex_t once_mutex = PTHREAD_MUTEX_INITIALIZER;
84 #elif defined( HAVE_CTHREADS_H )
85     /* Unimplemented */
86 #elif defined( HAVE_KERNEL_SCHEDULER_H )
87     /* Unimplemented */
88 #endif
89
90 /*****************************************************************************
91  * vlc_threads_init: initialize threads system
92  *****************************************************************************
93  * This function requires lazy initialization of a global lock in order to
94  * keep the library really thread-safe. Some architectures don't support this
95  * and thus do not guarantee the complete reentrancy.
96  *****************************************************************************/
97 int __vlc_threads_init( vlc_object_t *p_this )
98 {
99     static volatile int i_status = VLC_THREADS_UNINITIALIZED;
100     int i_ret = 0;
101
102 #if defined( PTH_INIT_IN_PTH_H )
103     /* Unimplemented */
104 #elif defined( ST_INIT_IN_ST_H )
105     /* Unimplemented */
106 #elif defined( WIN32 )
107     /* Unimplemented */
108 #elif defined( PTHREAD_COND_T_IN_PTHREAD_H )
109     pthread_mutex_lock( &once_mutex );
110 #elif defined( HAVE_CTHREADS_H )
111     /* Unimplemented */
112 #elif defined( HAVE_KERNEL_SCHEDULER_H )
113     /* Unimplemented */
114 #endif
115
116     if( i_status == VLC_THREADS_UNINITIALIZED )
117     {
118         i_status = VLC_THREADS_PENDING;
119
120 #if defined( PTH_INIT_IN_PTH_H )
121         i_ret = pth_init();
122 #elif defined( ST_INIT_IN_ST_H )
123         i_ret = st_init();
124 #elif defined( WIN32 )
125         /* Unimplemented */
126 #elif defined( PTHREAD_COND_T_IN_PTHREAD_H )
127         /* Unimplemented */
128 #elif defined( HAVE_CTHREADS_H )
129         /* Unimplemented */
130 #elif defined( HAVE_KERNEL_SCHEDULER_H )
131         /* Unimplemented */
132 #endif
133
134         if( i_ret )
135         {
136             i_status = VLC_THREADS_ERROR;
137         }
138         else
139         {
140             vlc_mutex_init( p_this, p_this->p_vlc->p_global_lock );
141             i_status = VLC_THREADS_READY;
142         }
143     }
144     else
145     {
146         i_ret = ( i_status == VLC_THREADS_READY );
147     }
148
149     i_initializations++;
150
151 #if defined( PTH_INIT_IN_PTH_H )
152     /* Unimplemented */
153 #elif defined( ST_INIT_IN_ST_H )
154     /* Unimplemented */
155 #elif defined( WIN32 )
156     /* Unimplemented */
157 #elif defined( PTHREAD_COND_T_IN_PTHREAD_H )
158     pthread_mutex_unlock( &once_mutex );
159     return i_ret;
160 #elif defined( HAVE_CTHREADS_H )
161     /* Unimplemented */
162 #elif defined( HAVE_KERNEL_SCHEDULER_H )
163     /* Unimplemented */
164 #endif
165
166     /* Wait until the other thread has initialized the thread library */
167     while( i_status == VLC_THREADS_PENDING )
168     {
169         msleep( THREAD_SLEEP );
170     }
171
172     return i_status == VLC_THREADS_READY;
173 }
174
175 /*****************************************************************************
176  * vlc_threads_end: stop threads system
177  *****************************************************************************
178  * FIXME: This function is far from being threadsafe. We should undo exactly
179  * what we did above in vlc_threads_init.
180  *****************************************************************************/
181 int __vlc_threads_end( vlc_object_t *p_this )
182 {
183 #if defined( PTH_INIT_IN_PTH_H )
184     i_initializations--;
185     if( i_initializations == 0 )
186     {
187         return pth_kill();
188     }
189     return 0;
190
191 #elif defined( ST_INIT_IN_ST_H )
192     i_initializations--;
193     return 0;
194
195 #elif defined( WIN32 )
196     i_initializations--;
197     return 0;
198
199 #elif defined( PTHREAD_COND_T_IN_PTHREAD_H )
200     pthread_mutex_lock( &once_mutex );
201     i_initializations--;
202     pthread_mutex_unlock( &once_mutex );
203     return 0;
204
205 #elif defined( HAVE_CTHREADS_H )
206     i_initializations--;
207     return 0;
208
209 #elif defined( HAVE_KERNEL_SCHEDULER_H )
210     i_initializations--;
211     return 0;
212
213 #endif
214 }
215
216 /*****************************************************************************
217  * vlc_mutex_init: initialize a mutex
218  *****************************************************************************/
219 int __vlc_mutex_init( vlc_object_t *p_this, vlc_mutex_t *p_mutex )
220 {
221 #if defined( PTH_INIT_IN_PTH_H )
222     return pth_mutex_init( p_mutex );
223
224 #elif defined( ST_INIT_IN_ST_H )
225     *p_mutex = st_mutex_new();
226     return ( *p_mutex == NULL ) ? errno : 0;
227
228 #elif defined( WIN32 )
229     /* We use mutexes on WinNT/2K/XP because we can use the SignalObjectAndWait
230      * function and have a 100% correct vlc_cond_wait() implementation.
231      * As this function is not available on Win9x, we can use the faster
232      * CriticalSections */
233     if( p_this->p_vlc->SignalObjectAndWait && !p_this->p_vlc->b_fast_mutex )
234     {
235         /* We are running on NT/2K/XP, we can use SignalObjectAndWait */
236         p_mutex->mutex = CreateMutex( 0, FALSE, 0 );
237         return ( p_mutex->mutex ? 0 : 1 );
238     }
239     else
240     {
241         p_mutex->mutex = NULL;
242         InitializeCriticalSection( &p_mutex->csection );
243         return 0;
244     }
245
246 #elif defined( PTHREAD_COND_T_IN_PTHREAD_H )
247 #   if defined(DEBUG) && defined(SYS_LINUX)
248     /* Create error-checking mutex to detect threads problems more easily. */
249     pthread_mutexattr_t attr;
250     int                 i_result;
251
252     pthread_mutexattr_init( &attr );
253     pthread_mutexattr_setkind_np( &attr, PTHREAD_MUTEX_ERRORCHECK_NP );
254     i_result = pthread_mutex_init( p_mutex, &attr );
255     pthread_mutexattr_destroy( &attr );
256     return( i_result );
257 #   endif
258     return pthread_mutex_init( p_mutex, NULL );
259
260 #elif defined( HAVE_CTHREADS_H )
261     mutex_init( p_mutex );
262     return 0;
263
264 #elif defined( HAVE_KERNEL_SCHEDULER_H )
265
266     /* check the arguments and whether it's already been initialized */
267     if( p_mutex == NULL )
268     {
269         return B_BAD_VALUE;
270     }
271
272     if( p_mutex->init == 9999 )
273     {
274         return EALREADY;
275     }
276
277     p_mutex->lock = create_sem( 1, "BeMutex" );
278     if( p_mutex->lock < B_NO_ERROR )
279     {
280         return( -1 );
281     }
282
283     p_mutex->init = 9999;
284     return B_OK;
285
286 #endif
287 }
288
289 /*****************************************************************************
290  * vlc_mutex_destroy: destroy a mutex, inner version
291  *****************************************************************************/
292 int __vlc_mutex_destroy( char * psz_file, int i_line, vlc_mutex_t *p_mutex )
293 {
294 #if defined( PTH_INIT_IN_PTH_H )
295     return 0;
296
297 #elif defined( ST_INIT_IN_ST_H )
298     return st_mutex_destroy( *p_mutex );
299
300 #elif defined( WIN32 )
301     if( p_mutex->mutex )
302     {
303         CloseHandle( p_mutex->mutex );
304     }
305     else
306     {
307         DeleteCriticalSection( &p_mutex->csection );
308     }
309     return 0;
310
311 #elif defined( PTHREAD_COND_T_IN_PTHREAD_H )    
312     int i_return = pthread_mutex_destroy( p_mutex );
313     if( i_return )
314     {
315 #if 0
316         intf_ErrMsg( "thread %d error: mutex_destroy failed at %s:%d (%s)",
317                      pthread_self(), psz_file, i_line, strerror(i_return) );
318 #endif
319     }
320     return i_return;
321
322 #elif defined( HAVE_CTHREADS_H )
323     return 0;
324
325 #elif defined( HAVE_KERNEL_SCHEDULER_H )
326     if( p_mutex->init == 9999 )
327     {
328         delete_sem( p_mutex->lock );
329     }
330
331     p_mutex->init = 0;
332     return B_OK;
333
334 #endif    
335 }
336
337 /*****************************************************************************
338  * vlc_cond_init: initialize a condition
339  *****************************************************************************/
340 int __vlc_cond_init( vlc_object_t *p_this, vlc_cond_t *p_condvar )
341 {
342 #if defined( PTH_INIT_IN_PTH_H )
343     return pth_cond_init( p_condvar );
344
345 #elif defined( ST_INIT_IN_ST_H )
346     *p_condvar = st_cond_new();
347     return ( *p_condvar == NULL ) ? errno : 0;
348
349 #elif defined( WIN32 )
350     /* Initialize counter */
351     p_condvar->i_waiting_threads = 0;
352
353     /* Misc init */
354     p_condvar->i_win9x_cv = p_this->p_vlc->i_win9x_cv;
355     p_condvar->SignalObjectAndWait = p_this->p_vlc->SignalObjectAndWait;
356
357     if( p_this->p_vlc->i_win9x_cv == 0 )
358     {
359         /* Create an auto-reset event. */
360         p_condvar->event = CreateEvent( NULL,   /* no security */
361                                         FALSE,  /* auto-reset event */
362                                         FALSE,  /* start non-signaled */
363                                         NULL ); /* unnamed */
364
365         p_condvar->semaphore = NULL;
366         return !p_condvar->event;
367     }
368     else
369     {
370         p_condvar->semaphore = CreateSemaphore( NULL,       /* no security */
371                                                 0,          /* initial count */
372                                                 0x7fffffff, /* max count */
373                                                 NULL );     /* unnamed */
374
375         if( p_this->p_vlc->i_win9x_cv == 1 )
376             /* Create a manual-reset event initially signaled. */
377             p_condvar->event = CreateEvent( NULL, TRUE, TRUE, NULL );
378         else
379             /* Create a auto-reset event. */
380             p_condvar->event = CreateEvent( NULL, FALSE, FALSE, NULL );
381
382         InitializeCriticalSection( &p_condvar->csection );
383
384         return !p_condvar->semaphore || !p_condvar->event;
385     }
386
387 #elif defined( PTHREAD_COND_T_IN_PTHREAD_H )
388     return pthread_cond_init( p_condvar, NULL );
389
390 #elif defined( HAVE_CTHREADS_H )
391     /* condition_init() */
392     spin_lock_init( &p_condvar->lock );
393     cthread_queue_init( &p_condvar->queue );
394     p_condvar->name = 0;
395     p_condvar->implications = 0;
396
397     return 0;
398
399 #elif defined( HAVE_KERNEL_SCHEDULER_H )
400     if( !p_condvar )
401     {
402         return B_BAD_VALUE;
403     }
404
405     if( p_condvar->init == 9999 )
406     {
407         return EALREADY;
408     }
409
410     p_condvar->thread = -1;
411     p_condvar->init = 9999;
412     return 0;
413
414 #endif
415 }
416
417 /*****************************************************************************
418  * vlc_cond_destroy: destroy a condition, inner version
419  *****************************************************************************/
420 int __vlc_cond_destroy( char * psz_file, int i_line, vlc_cond_t *p_condvar )
421 {
422 #if defined( PTH_INIT_IN_PTH_H )
423     return 0;
424
425 #elif defined( ST_INIT_IN_ST_H )
426     return st_cond_destroy( *p_condvar );
427
428 #elif defined( WIN32 )
429     if( !p_condvar->semaphore )
430         return !CloseHandle( p_condvar->event );
431     else
432         return !CloseHandle( p_condvar->event )
433           || !CloseHandle( p_condvar->semaphore );
434
435 #elif defined( PTHREAD_COND_T_IN_PTHREAD_H )
436     int i_result = pthread_cond_destroy( p_condvar );
437     if( i_result )
438     {
439 #if 0
440         intf_ErrMsg( "thread %d error: cond_destroy failed at %s:%d (%s)",
441                      pthread_self(), psz_file, i_line, strerror(i_result) );
442 #endif
443     }
444     return i_result;
445
446 #elif defined( HAVE_CTHREADS_H )
447     return 0;
448
449 #elif defined( HAVE_KERNEL_SCHEDULER_H )
450     p_condvar->init = 0;
451     return 0;
452
453 #endif    
454 }
455
456 /*****************************************************************************
457  * vlc_thread_create: create a thread, inner version
458  *****************************************************************************/
459 int __vlc_thread_create( vlc_object_t *p_this, char * psz_file, int i_line,
460                          char *psz_name, void * ( *func ) ( void * ),
461                          vlc_bool_t b_wait )
462 {
463     int i_ret;
464
465     vlc_mutex_lock( &p_this->object_lock );
466
467 #ifdef GPROF
468     wrapper_t wrapper;
469
470     /* Initialize the wrapper structure */
471     wrapper.func = func;
472     wrapper.p_data = (void *)p_this;
473     getitimer( ITIMER_PROF, &wrapper.itimer );
474     vlc_mutex_init( p_this, &wrapper.lock );
475     vlc_cond_init( p_this, &wrapper.wait );
476     vlc_mutex_lock( &wrapper.lock );
477
478     /* Alter user-passed data so that we call the wrapper instead
479      * of the real function */
480     p_data = &wrapper;
481     func = vlc_thread_wrapper;
482 #endif
483
484 #if defined( PTH_INIT_IN_PTH_H )
485     p_this->thread_id = pth_spawn( PTH_ATTR_DEFAULT, func, (void *)p_this );
486     i_ret = 0;
487
488 #elif defined( ST_INIT_IN_ST_H )
489     p_this->thread_id = st_thread_create( func, (void *)p_this, 1, 0 );
490     i_ret = 0;
491     
492 #elif defined( WIN32 )
493     {
494         unsigned threadID;
495         /* When using the MSVCRT C library you have to use the _beginthreadex
496          * function instead of CreateThread, otherwise you'll end up with
497          * memory leaks and the signal functions not working */
498         p_this->thread_id =
499                 (HANDLE)_beginthreadex( NULL, 0, (PTHREAD_START) func, 
500                                         (void *)p_this, 0, &threadID );
501     }
502
503     i_ret = ( p_this->thread_id ? 0 : 1 );
504
505 #elif defined( PTHREAD_COND_T_IN_PTHREAD_H )
506     i_ret = pthread_create( &p_this->thread_id, NULL, func, (void *)p_this );
507
508 #elif defined( HAVE_CTHREADS_H )
509     p_this->thread_id = cthread_fork( (cthread_fn_t)func, (any_t)p_this );
510     i_ret = 0;
511
512 #elif defined( HAVE_KERNEL_SCHEDULER_H )
513     p_this->thread_id = spawn_thread( (thread_func)func, psz_name,
514                                       B_NORMAL_PRIORITY, (void *)p_this );
515     i_ret = resume_thread( p_this->thread_id );
516
517 #endif
518
519 #ifdef GPROF
520     if( i_ret == 0 )
521     {
522         vlc_cond_wait( &wrapper.wait, &wrapper.lock );
523     }
524
525     vlc_mutex_unlock( &wrapper.lock );
526     vlc_mutex_destroy( &wrapper.lock );
527     vlc_cond_destroy( &wrapper.wait );
528 #endif
529
530     if( i_ret == 0 )
531     {
532         if( b_wait )
533         {
534             msg_Dbg( p_this, "waiting for thread completion" );
535             vlc_cond_wait( &p_this->object_wait, &p_this->object_lock );
536         }
537
538         p_this->b_thread = 1;
539
540         msg_Dbg( p_this, "thread %d (%s) created (%s:%d)",
541                          p_this->thread_id, psz_name, psz_file, i_line );
542
543         vlc_mutex_unlock( &p_this->object_lock );
544     }
545     else
546     {
547         msg_Err( p_this, "%s thread could not be created at %s:%d (%s)",
548                          psz_name, psz_file, i_line, strerror(i_ret) );
549         vlc_mutex_unlock( &p_this->object_lock );
550     }
551
552     return i_ret;
553 }
554
555 /*****************************************************************************
556  * vlc_thread_ready: tell the parent thread we were successfully spawned
557  *****************************************************************************/
558 void __vlc_thread_ready( vlc_object_t *p_this )
559 {
560     vlc_mutex_lock( &p_this->object_lock );
561     vlc_cond_signal( &p_this->object_wait );
562     vlc_mutex_unlock( &p_this->object_lock );
563 }
564
565 /*****************************************************************************
566  * vlc_thread_join: wait until a thread exits, inner version
567  *****************************************************************************/
568 void __vlc_thread_join( vlc_object_t *p_this, char * psz_file, int i_line )
569 {
570     int i_ret = 0;
571
572 #if defined( PTH_INIT_IN_PTH_H )
573     i_ret = pth_join( p_this->thread_id, NULL );
574
575 #elif defined( ST_INIT_IN_ST_H )
576     i_ret = st_thread_join( p_this->thread_id, NULL );
577     
578 #elif defined( WIN32 )
579     WaitForSingleObject( p_this->thread_id, INFINITE );
580
581 #elif defined( PTHREAD_COND_T_IN_PTHREAD_H )
582     i_ret = pthread_join( p_this->thread_id, NULL );
583
584 #elif defined( HAVE_CTHREADS_H )
585     cthread_join( p_this->thread_id );
586     i_ret = 1;
587
588 #elif defined( HAVE_KERNEL_SCHEDULER_H )
589     int32 exit_value;
590     wait_for_thread( p_this->thread_id, &exit_value );
591
592 #endif
593
594     if( i_ret )
595     {
596         msg_Err( p_this, "thread_join(%d) failed at %s:%d (%s)",
597                          p_this->thread_id, psz_file, i_line, strerror(i_ret) );
598     }
599     else
600     {
601         msg_Dbg( p_this, "thread %d joined (%s:%d)",
602                          p_this->thread_id, psz_file, i_line );
603     }
604
605     p_this->b_thread = 0;
606 }
607
608 /*****************************************************************************
609  * vlc_thread_wrapper: wrapper around thread functions used when profiling.
610  *****************************************************************************/
611 #ifdef GPROF
612 static void *vlc_thread_wrapper( void *p_wrapper )
613 {
614     /* Put user data in thread-local variables */
615     void *            p_data = ((wrapper_t*)p_wrapper)->p_data;
616     vlc_thread_func_t func   = ((wrapper_t*)p_wrapper)->func;
617
618     /* Set the profile timer value */
619     setitimer( ITIMER_PROF, &((wrapper_t*)p_wrapper)->itimer, NULL );
620
621     /* Tell the calling thread that we don't need its data anymore */
622     vlc_mutex_lock( &((wrapper_t*)p_wrapper)->lock );
623     vlc_cond_signal( &((wrapper_t*)p_wrapper)->wait );
624     vlc_mutex_unlock( &((wrapper_t*)p_wrapper)->lock );
625
626     /* Call the real function */
627     return func( p_data );
628 }
629 #endif