]> git.sesse.net Git - vlc/blob - src/misc/threads.c
* ALL: the first libvlc commit.
[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.1 2002/06/01 12:32:01 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_s
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  * vlc_threads_init: initialize threads system
73  *****************************************************************************/
74 int __vlc_threads_init( vlc_object_t *p_this )
75 {
76     /* FIXME: this is definitely _not_ threadsafe, but at least it works
77      * under all implementations. We should for instance use pthread_once
78      * for lazy initialization of the global lock. */
79     static int i_status = VLC_THREADS_UNINITIALIZED;
80     int i_ret;
81
82     if( i_status == VLC_THREADS_READY )
83     {
84         return 0;
85     }
86
87     if( i_status == VLC_THREADS_UNINITIALIZED )
88     {
89         i_status = VLC_THREADS_PENDING;
90
91 #if defined( PTH_INIT_IN_PTH_H )
92         i_ret = pth_init();
93
94 #elif defined( ST_INIT_IN_ST_H )
95         i_ret = st_init();
96
97 #elif defined( WIN32 )
98         i_ret = 0;
99
100 #elif defined( PTHREAD_COND_T_IN_PTHREAD_H )
101         i_ret = 0;
102
103 #elif defined( HAVE_CTHREADS_H )
104         i_ret = 0;
105
106 #elif defined( HAVE_KERNEL_SCHEDULER_H )
107         i_ret = 0;
108
109 #endif
110         if( i_ret )
111         {
112             i_status = VLC_THREADS_ERROR;
113             return i_ret;
114         }
115
116         vlc_mutex_init( p_this, p_this->p_vlc->p_global_lock );
117
118         i_status = VLC_THREADS_READY;
119
120         return i_ret;
121     }
122
123     /* Wait until the other thread has initialized the thread library */
124     while( i_status == VLC_THREADS_PENDING )
125     {
126         msleep( THREAD_SLEEP );
127     }
128
129     return( i_status == VLC_THREADS_READY );
130 }
131
132 /*****************************************************************************
133  * vlc_threads_end: stop threads system
134  *****************************************************************************/
135 int vlc_threads_end( void )
136 {
137 #if defined( PTH_INIT_IN_PTH_H )
138     return pth_kill();
139
140 #elif defined( ST_INIT_IN_ST_H )
141     return 0;
142
143 #elif defined( WIN32 )
144     return 0;
145
146 #elif defined( PTHREAD_COND_T_IN_PTHREAD_H )
147     return 0;
148
149 #elif defined( HAVE_CTHREADS_H )
150     return 0;
151
152 #elif defined( HAVE_KERNEL_SCHEDULER_H )
153     return 0;
154
155 #endif
156 }
157
158 static int mutexes = 0;
159 /*****************************************************************************
160  * vlc_mutex_init: initialize a mutex
161  *****************************************************************************/
162 int __vlc_mutex_init( vlc_object_t *p_this, vlc_mutex_t *p_mutex )
163 {
164 #if defined( PTH_INIT_IN_PTH_H )
165     return pth_mutex_init( p_mutex );
166
167 #elif defined( ST_INIT_IN_ST_H )
168     *p_mutex = st_mutex_new();
169     return ( *p_mutex == NULL ) ? errno : 0;
170
171 #elif defined( WIN32 )
172     /* We use mutexes on WinNT/2K/XP because we can use the SignalObjectAndWait
173      * function and have a 100% correct vlc_cond_wait() implementation.
174      * As this function is not available on Win9x, we can use the faster
175      * CriticalSections */
176     if( (GetVersion() < 0x80000000) && !p_this->p_vlc->p_sys->b_fast_pthread )
177     {
178         /* We are running on NT/2K/XP, we can use SignalObjectAndWait */
179         p_mutex->mutex = CreateMutex( 0, FALSE, 0 );
180         p_mutex->SignalObjectAndWait =
181                             p_this->p_vlc->p_sys->SignalObjectAndWait;
182         return ( p_mutex->mutex ? 0 : 1 );
183     }
184     else
185     {
186         InitializeCriticalSection( &p_mutex->csection );
187         p_mutex->mutex = NULL;
188         return 0;
189     }
190
191 #elif defined( PTHREAD_COND_T_IN_PTHREAD_H )
192 #   if defined(DEBUG) && defined(SYS_LINUX)
193     /* Create error-checking mutex to detect threads problems more easily. */
194     pthread_mutexattr_t attr;
195     int                 i_result;
196
197     pthread_mutexattr_init( &attr );
198     pthread_mutexattr_setkind_np( &attr, PTHREAD_MUTEX_ERRORCHECK_NP );
199     i_result = pthread_mutex_init( p_mutex, &attr );
200     pthread_mutexattr_destroy( &attr );
201     return( i_result );
202 #   endif
203     return pthread_mutex_init( p_mutex, NULL );
204
205 #elif defined( HAVE_CTHREADS_H )
206     mutex_init( p_mutex );
207     return 0;
208
209 #elif defined( HAVE_KERNEL_SCHEDULER_H )
210
211     /* check the arguments and whether it's already been initialized */
212     if( p_mutex == NULL )
213     {
214         return B_BAD_VALUE;
215     }
216
217     if( p_mutex->init == 9999 )
218     {
219         return EALREADY;
220     }
221
222     p_mutex->lock = create_sem( 1, "BeMutex" );
223     if( p_mutex->lock < B_NO_ERROR )
224     {
225         return( -1 );
226     }
227
228     p_mutex->init = 9999;
229     return B_OK;
230
231 #endif
232 }
233
234 /*****************************************************************************
235  * vlc_mutex_destroy: destroy a mutex, inner version
236  *****************************************************************************/
237 int __vlc_mutex_destroy( char * psz_file, int i_line, vlc_mutex_t *p_mutex )
238 {
239 #if defined( PTH_INIT_IN_PTH_H )
240     return 0;
241
242 #elif defined( ST_INIT_IN_ST_H )
243     return st_mutex_destroy( *p_mutex );
244
245 #elif defined( WIN32 )
246     if( p_mutex->mutex )
247     {
248         CloseHandle( p_mutex->mutex );
249     }
250     else
251     {
252         DeleteCriticalSection( &p_mutex->csection );
253     }
254     return 0;
255
256 #elif defined( PTHREAD_COND_T_IN_PTHREAD_H )    
257     int i_return = pthread_mutex_destroy( p_mutex );
258     if( i_return )
259     {
260 //X        intf_ErrMsg( "thread %d error: mutex_destroy failed at %s:%d (%s)",
261 //X                     pthread_self(), psz_file, i_line, strerror(i_return) );
262     }
263     return i_return;
264
265 #elif defined( HAVE_CTHREADS_H )
266     return 0;
267
268 #elif defined( HAVE_KERNEL_SCHEDULER_H )
269     if( p_mutex->init == 9999 )
270     {
271         delete_sem( p_mutex->lock );
272     }
273
274     p_mutex->init = 0;
275     return B_OK;
276
277 #endif    
278 }
279
280 /*****************************************************************************
281  * vlc_cond_init: initialize a condition
282  *****************************************************************************/
283 int vlc_cond_init( vlc_cond_t *p_condvar )
284 {
285 #if defined( PTH_INIT_IN_PTH_H )
286     return pth_cond_init( p_condvar );
287
288 #elif defined( ST_INIT_IN_ST_H )
289     *p_condvar = st_cond_new();
290     return ( *p_condvar == NULL ) ? errno : 0;
291
292 #elif defined( WIN32 )
293     /* initialise counter */
294     p_condvar->i_waiting_threads = 0;
295
296     /* Create an auto-reset event. */
297     p_condvar->signal = CreateEvent( NULL, /* no security */
298                                      FALSE,  /* auto-reset event */
299                                      FALSE,  /* non-signaled initially */
300                                      NULL ); /* unnamed */
301
302     return( !p_condvar->signal );
303
304 #elif defined( PTHREAD_COND_T_IN_PTHREAD_H )
305     return pthread_cond_init( p_condvar, NULL );
306
307 #elif defined( HAVE_CTHREADS_H )
308     /* condition_init() */
309     spin_lock_init( &p_condvar->lock );
310     cthread_queue_init( &p_condvar->queue );
311     p_condvar->name = 0;
312     p_condvar->implications = 0;
313
314     return 0;
315
316 #elif defined( HAVE_KERNEL_SCHEDULER_H )
317     if( !p_condvar )
318     {
319         return B_BAD_VALUE;
320     }
321
322     if( p_condvar->init == 9999 )
323     {
324         return EALREADY;
325     }
326
327     p_condvar->thread = -1;
328     p_condvar->init = 9999;
329     return 0;
330
331 #endif
332 }
333
334 /*****************************************************************************
335  * vlc_cond_destroy: destroy a condition, inner version
336  *****************************************************************************/
337 int __vlc_cond_destroy( char * psz_file, int i_line, vlc_cond_t *p_condvar )
338 {
339 #if defined( PTH_INIT_IN_PTH_H )
340     return 0;
341
342 #elif defined( ST_INIT_IN_ST_H )
343     return st_cond_destroy( *p_condvar );
344
345 #elif defined( WIN32 )
346     return( !CloseHandle( p_condvar->signal ) );
347
348 #elif defined( PTHREAD_COND_T_IN_PTHREAD_H )
349     int i_result = pthread_cond_destroy( p_condvar );
350     if( i_result )
351     {
352 //X        intf_ErrMsg( "thread %d error: cond_destroy failed at %s:%d (%s)",
353 //X                     pthread_self(), psz_file, i_line, strerror(i_result) );
354     }
355     return i_result;
356
357 #elif defined( HAVE_CTHREADS_H )
358     return 0;
359
360 #elif defined( HAVE_KERNEL_SCHEDULER_H )
361     p_condvar->init = 0;
362     return 0;
363
364 #endif    
365 }
366
367 /*****************************************************************************
368  * vlc_thread_create: create a thread, inner version
369  *****************************************************************************/
370 int __vlc_thread_create( vlc_object_t *p_this, char * psz_file, int i_line,
371                          char *psz_name, void * ( *func ) ( void * ),
372                          vlc_bool_t b_wait )
373 {
374     int i_ret;
375
376     vlc_mutex_init( p_this, &p_this->thread_lock );
377     vlc_cond_init( &p_this->thread_wait );
378     vlc_mutex_lock( &p_this->thread_lock );
379
380 #ifdef GPROF
381     wrapper_t wrapper;
382
383     /* Initialize the wrapper structure */
384     wrapper.func = func;
385     wrapper.p_data = (void *)p_this;
386     getitimer( ITIMER_PROF, &wrapper.itimer );
387     vlc_mutex_init( p_this, &wrapper.lock );
388     vlc_cond_init( &wrapper.wait );
389     vlc_mutex_lock( &wrapper.lock );
390
391     /* Alter user-passed data so that we call the wrapper instead
392      * of the real function */
393     p_data = &wrapper;
394     func = vlc_thread_wrapper;
395 #endif
396
397 #if defined( PTH_INIT_IN_PTH_H )
398     p_this->thread_id = pth_spawn( PTH_ATTR_DEFAULT, func, (void *)p_this );
399     i_ret = 0;
400
401 #elif defined( ST_INIT_IN_ST_H )
402     p_this->thread_id = st_thread_create( func, (void *)p_this, 1, 0 );
403     i_ret = 0;
404     
405 #elif defined( WIN32 )
406     unsigned threadID;
407     /* When using the MSVCRT C library you have to use the _beginthreadex
408      * function instead of CreateThread, otherwise you'll end up with memory
409      * leaks and the signal functions not working */
410     p_this->thread_id = (HANDLE)_beginthreadex( NULL, 0, (PTHREAD_START) func, 
411                                                 (void *)p_this, 0, &threadID );
412     
413     i_ret = ( p_this->thread_id ? 0 : 1 );
414
415 #elif defined( PTHREAD_COND_T_IN_PTHREAD_H )
416     i_ret = pthread_create( &p_this->thread_id, NULL, func, (void *)p_this );
417
418 #elif defined( HAVE_CTHREADS_H )
419     p_this->thread_id = cthread_fork( (cthread_fn_t)func, (any_t)p_this );
420     i_ret = 0;
421
422 #elif defined( HAVE_KERNEL_SCHEDULER_H )
423     p_this->thread_id = spawn_thread( (thread_func)func, psz_name,
424                                       B_NORMAL_PRIORITY, (void *)p_this );
425     i_ret = resume_thread( p_this->thread_id );
426
427 #endif
428
429 #ifdef GPROF
430     if( i_ret == 0 )
431     {
432         vlc_cond_wait( &wrapper.wait, &wrapper.lock );
433     }
434
435     vlc_mutex_unlock( &wrapper.lock );
436     vlc_mutex_destroy( &wrapper.lock );
437     vlc_cond_destroy( &wrapper.wait );
438 #endif
439
440     if( i_ret == 0 )
441     {
442         msg_Dbg( p_this, "thread %d (%s) created (%s:%d)",
443                          p_this->thread_id, psz_name, psz_file, i_line );
444
445         p_this->b_thread = 1;
446
447         if( b_wait )
448         {
449             msg_Dbg( p_this, "waiting for thread completion" );
450             vlc_cond_wait( &p_this->thread_wait, &p_this->thread_lock );
451         }
452
453         vlc_mutex_unlock( &p_this->thread_lock );
454     }
455     else
456     {
457         msg_Err( p_this, "%s thread could not be created at %s:%d (%s)",
458                          psz_name, psz_file, i_line, strerror(i_ret) );
459         vlc_mutex_unlock( &p_this->thread_lock );
460         vlc_mutex_destroy( &p_this->thread_lock );
461         vlc_cond_destroy( &p_this->thread_wait );
462     }
463
464     return i_ret;
465 }
466
467 /*****************************************************************************
468  * vlc_thread_ready: tell the parent thread we were successfully spawned
469  *****************************************************************************/
470 void __vlc_thread_ready( vlc_object_t *p_this )
471 {
472     vlc_mutex_lock( &p_this->thread_lock );
473     vlc_cond_signal( &p_this->thread_wait );
474     vlc_mutex_unlock( &p_this->thread_lock );
475 }
476
477 /*****************************************************************************
478  * vlc_thread_join: wait until a thread exits, inner version
479  *****************************************************************************/
480 void __vlc_thread_join( vlc_object_t *p_this, char * psz_file, int i_line )
481 {
482     int i_ret = 0;
483
484 #if defined( PTH_INIT_IN_PTH_H )
485     i_ret = pth_join( p_this->thread_id, NULL );
486
487 #elif defined( ST_INIT_IN_ST_H )
488     i_ret = st_thread_join( p_this->thread_id, NULL );
489     
490 #elif defined( WIN32 )
491     WaitForSingleObject( p_this->thread_id, INFINITE );
492
493 #elif defined( PTHREAD_COND_T_IN_PTHREAD_H )
494     i_ret = pthread_join( p_this->thread_id, NULL );
495
496 #elif defined( HAVE_CTHREADS_H )
497     cthread_join( p_this->thread_id );
498     i_ret = 1;
499
500 #elif defined( HAVE_KERNEL_SCHEDULER_H )
501     int32 exit_value;
502     wait_for_thread( p_this->thread_id, &exit_value );
503
504 #endif
505
506     vlc_mutex_destroy( &p_this->thread_lock );
507     vlc_cond_destroy( &p_this->thread_wait );
508
509     if( i_ret )
510     {
511         msg_Err( p_this, "thread_join(%d) failed at %s:%d (%s)",
512                          p_this->thread_id, psz_file, i_line, strerror(i_ret) );
513     }
514     else
515     {
516         msg_Dbg( p_this, "thread %d joined (%s:%d)",
517                          p_this->thread_id, psz_file, i_line );
518     }
519
520     p_this->b_thread = 0;
521 }
522
523 /*****************************************************************************
524  * vlc_thread_wrapper: wrapper around thread functions used when profiling.
525  *****************************************************************************/
526 #ifdef GPROF
527 static void *vlc_thread_wrapper( void *p_wrapper )
528 {
529     /* Put user data in thread-local variables */
530     void *            p_data = ((wrapper_t*)p_wrapper)->p_data;
531     vlc_thread_func_t func   = ((wrapper_t*)p_wrapper)->func;
532
533     /* Set the profile timer value */
534     setitimer( ITIMER_PROF, &((wrapper_t*)p_wrapper)->itimer, NULL );
535
536     /* Tell the calling thread that we don't need its data anymore */
537     vlc_mutex_lock( &((wrapper_t*)p_wrapper)->lock );
538     vlc_cond_signal( &((wrapper_t*)p_wrapper)->wait );
539     vlc_mutex_unlock( &((wrapper_t*)p_wrapper)->lock );
540
541     /* Call the real function */
542     return func( p_data );
543 }
544 #endif