]> git.sesse.net Git - vlc/blob - src/misc/threads.c
* ./src/misc/darwin_specific.c, ./src/misc/extras.c: moved our custom
[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.8 2002/07/05 11:18:56 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 /*****************************************************************************
159  * vlc_mutex_init: initialize a mutex
160  *****************************************************************************/
161 int __vlc_mutex_init( vlc_object_t *p_this, vlc_mutex_t *p_mutex )
162 {
163 #if defined( PTH_INIT_IN_PTH_H )
164     return pth_mutex_init( p_mutex );
165
166 #elif defined( ST_INIT_IN_ST_H )
167     *p_mutex = st_mutex_new();
168     return ( *p_mutex == NULL ) ? errno : 0;
169
170 #elif defined( WIN32 )
171     /* We use mutexes on WinNT/2K/XP because we can use the SignalObjectAndWait
172      * function and have a 100% correct vlc_cond_wait() implementation.
173      * As this function is not available on Win9x, we can use the faster
174      * CriticalSections */
175     if( (GetVersion() < 0x80000000) && !p_this->p_vlc->b_fast_pthread )
176     {
177         /* We are running on NT/2K/XP, we can use SignalObjectAndWait */
178         p_mutex->mutex = CreateMutex( 0, FALSE, 0 );
179         return ( p_mutex->mutex ? 0 : 1 );
180     }
181     else
182     {
183         p_mutex->mutex = NULL;
184         InitializeCriticalSection( &p_mutex->csection );
185         return 0;
186     }
187
188 #elif defined( PTHREAD_COND_T_IN_PTHREAD_H )
189 #   if defined(DEBUG) && defined(SYS_LINUX)
190     /* Create error-checking mutex to detect threads problems more easily. */
191     pthread_mutexattr_t attr;
192     int                 i_result;
193
194     pthread_mutexattr_init( &attr );
195     pthread_mutexattr_setkind_np( &attr, PTHREAD_MUTEX_ERRORCHECK_NP );
196     i_result = pthread_mutex_init( p_mutex, &attr );
197     pthread_mutexattr_destroy( &attr );
198     return( i_result );
199 #   endif
200     return pthread_mutex_init( p_mutex, NULL );
201
202 #elif defined( HAVE_CTHREADS_H )
203     mutex_init( p_mutex );
204     return 0;
205
206 #elif defined( HAVE_KERNEL_SCHEDULER_H )
207
208     /* check the arguments and whether it's already been initialized */
209     if( p_mutex == NULL )
210     {
211         return B_BAD_VALUE;
212     }
213
214     if( p_mutex->init == 9999 )
215     {
216         return EALREADY;
217     }
218
219     p_mutex->lock = create_sem( 1, "BeMutex" );
220     if( p_mutex->lock < B_NO_ERROR )
221     {
222         return( -1 );
223     }
224
225     p_mutex->init = 9999;
226     return B_OK;
227
228 #endif
229 }
230
231 /*****************************************************************************
232  * vlc_mutex_destroy: destroy a mutex, inner version
233  *****************************************************************************/
234 int __vlc_mutex_destroy( char * psz_file, int i_line, vlc_mutex_t *p_mutex )
235 {
236 #if defined( PTH_INIT_IN_PTH_H )
237     return 0;
238
239 #elif defined( ST_INIT_IN_ST_H )
240     return st_mutex_destroy( *p_mutex );
241
242 #elif defined( WIN32 )
243     if( p_mutex->mutex )
244     {
245         CloseHandle( p_mutex->mutex );
246     }
247     else
248     {
249         DeleteCriticalSection( &p_mutex->csection );
250     }
251     return 0;
252
253 #elif defined( PTHREAD_COND_T_IN_PTHREAD_H )    
254     int i_return = pthread_mutex_destroy( p_mutex );
255     if( i_return )
256     {
257 //X        intf_ErrMsg( "thread %d error: mutex_destroy failed at %s:%d (%s)",
258 //X                     pthread_self(), psz_file, i_line, strerror(i_return) );
259     }
260     return i_return;
261
262 #elif defined( HAVE_CTHREADS_H )
263     return 0;
264
265 #elif defined( HAVE_KERNEL_SCHEDULER_H )
266     if( p_mutex->init == 9999 )
267     {
268         delete_sem( p_mutex->lock );
269     }
270
271     p_mutex->init = 0;
272     return B_OK;
273
274 #endif    
275 }
276
277 /*****************************************************************************
278  * vlc_cond_init: initialize a condition
279  *****************************************************************************/
280 int __vlc_cond_init( vlc_object_t *p_this, vlc_cond_t *p_condvar )
281 {
282 #if defined( PTH_INIT_IN_PTH_H )
283     return pth_cond_init( p_condvar );
284
285 #elif defined( ST_INIT_IN_ST_H )
286     *p_condvar = st_cond_new();
287     return ( *p_condvar == NULL ) ? errno : 0;
288
289 #elif defined( WIN32 )
290     /* Initialize counter */
291     p_condvar->i_waiting_threads = 0;
292
293     if( (GetVersion() < 0x80000000) && !p_this->p_vlc->b_fast_pthread )
294     {
295         /* Create an auto-reset event and a semaphore. */
296         p_condvar->signal = CreateEvent( NULL, FALSE, FALSE, NULL );
297         p_condvar->semaphore = CreateSemaphore( NULL, 0, 0x7fffffff, NULL );
298
299         p_condvar->b_broadcast = 0;
300
301         /* We are running on NT/2K/XP, we can use SignalObjectAndWait */
302         p_condvar->SignalObjectAndWait = p_this->p_vlc->SignalObjectAndWait;
303
304         return !p_condvar->signal || !p_condvar->semaphore;
305     }
306     else
307     {
308         p_condvar->signal = NULL;
309
310         /* Create an auto-reset event and a manual-reset event. */
311         p_condvar->p_events[0] = CreateEvent( NULL, FALSE, FALSE, NULL );
312         p_condvar->p_events[1] = CreateEvent( NULL, TRUE, FALSE, NULL );
313
314         return !p_condvar->p_events[0] || !p_condvar->p_events[1];
315     }
316
317 #elif defined( PTHREAD_COND_T_IN_PTHREAD_H )
318     return pthread_cond_init( p_condvar, NULL );
319
320 #elif defined( HAVE_CTHREADS_H )
321     /* condition_init() */
322     spin_lock_init( &p_condvar->lock );
323     cthread_queue_init( &p_condvar->queue );
324     p_condvar->name = 0;
325     p_condvar->implications = 0;
326
327     return 0;
328
329 #elif defined( HAVE_KERNEL_SCHEDULER_H )
330     if( !p_condvar )
331     {
332         return B_BAD_VALUE;
333     }
334
335     if( p_condvar->init == 9999 )
336     {
337         return EALREADY;
338     }
339
340     p_condvar->thread = -1;
341     p_condvar->init = 9999;
342     return 0;
343
344 #endif
345 }
346
347 /*****************************************************************************
348  * vlc_cond_destroy: destroy a condition, inner version
349  *****************************************************************************/
350 int __vlc_cond_destroy( char * psz_file, int i_line, vlc_cond_t *p_condvar )
351 {
352 #if defined( PTH_INIT_IN_PTH_H )
353     return 0;
354
355 #elif defined( ST_INIT_IN_ST_H )
356     return st_cond_destroy( *p_condvar );
357
358 #elif defined( WIN32 )
359     if( p_condvar->signal )
360     {
361         return !CloseHandle( p_condvar->signal )
362                  || !CloseHandle( p_condvar->semaphore );
363     }
364     else
365     {
366         return !CloseHandle( p_condvar->p_events[0] )
367                  || !CloseHandle( p_condvar->p_events[1] );
368     }
369
370 #elif defined( PTHREAD_COND_T_IN_PTHREAD_H )
371     int i_result = pthread_cond_destroy( p_condvar );
372     if( i_result )
373     {
374 //X        intf_ErrMsg( "thread %d error: cond_destroy failed at %s:%d (%s)",
375 //X                     pthread_self(), psz_file, i_line, strerror(i_result) );
376     }
377     return i_result;
378
379 #elif defined( HAVE_CTHREADS_H )
380     return 0;
381
382 #elif defined( HAVE_KERNEL_SCHEDULER_H )
383     p_condvar->init = 0;
384     return 0;
385
386 #endif    
387 }
388
389 /*****************************************************************************
390  * vlc_thread_create: create a thread, inner version
391  *****************************************************************************/
392 int __vlc_thread_create( vlc_object_t *p_this, char * psz_file, int i_line,
393                          char *psz_name, void * ( *func ) ( void * ),
394                          vlc_bool_t b_wait )
395 {
396     int i_ret;
397
398     vlc_mutex_lock( &p_this->object_lock );
399
400 #ifdef GPROF
401     wrapper_t wrapper;
402
403     /* Initialize the wrapper structure */
404     wrapper.func = func;
405     wrapper.p_data = (void *)p_this;
406     getitimer( ITIMER_PROF, &wrapper.itimer );
407     vlc_mutex_init( p_this, &wrapper.lock );
408     vlc_cond_init( p_this, &wrapper.wait );
409     vlc_mutex_lock( &wrapper.lock );
410
411     /* Alter user-passed data so that we call the wrapper instead
412      * of the real function */
413     p_data = &wrapper;
414     func = vlc_thread_wrapper;
415 #endif
416
417 #if defined( PTH_INIT_IN_PTH_H )
418     p_this->thread_id = pth_spawn( PTH_ATTR_DEFAULT, func, (void *)p_this );
419     i_ret = 0;
420
421 #elif defined( ST_INIT_IN_ST_H )
422     p_this->thread_id = st_thread_create( func, (void *)p_this, 1, 0 );
423     i_ret = 0;
424     
425 #elif defined( WIN32 )
426     {
427         unsigned threadID;
428         /* When using the MSVCRT C library you have to use the _beginthreadex
429          * function instead of CreateThread, otherwise you'll end up with
430          * memory leaks and the signal functions not working */
431         p_this->thread_id =
432                 (HANDLE)_beginthreadex( NULL, 0, (PTHREAD_START) func, 
433                                         (void *)p_this, 0, &threadID );
434     }
435
436     i_ret = ( p_this->thread_id ? 0 : 1 );
437
438 #elif defined( PTHREAD_COND_T_IN_PTHREAD_H )
439     i_ret = pthread_create( &p_this->thread_id, NULL, func, (void *)p_this );
440
441 #elif defined( HAVE_CTHREADS_H )
442     p_this->thread_id = cthread_fork( (cthread_fn_t)func, (any_t)p_this );
443     i_ret = 0;
444
445 #elif defined( HAVE_KERNEL_SCHEDULER_H )
446     p_this->thread_id = spawn_thread( (thread_func)func, psz_name,
447                                       B_NORMAL_PRIORITY, (void *)p_this );
448     i_ret = resume_thread( p_this->thread_id );
449
450 #endif
451
452 #ifdef GPROF
453     if( i_ret == 0 )
454     {
455         vlc_cond_wait( &wrapper.wait, &wrapper.lock );
456     }
457
458     vlc_mutex_unlock( &wrapper.lock );
459     vlc_mutex_destroy( &wrapper.lock );
460     vlc_cond_destroy( &wrapper.wait );
461 #endif
462
463     if( i_ret == 0 )
464     {
465         if( b_wait )
466         {
467             msg_Dbg( p_this, "waiting for thread completion" );
468             vlc_cond_wait( &p_this->object_wait, &p_this->object_lock );
469         }
470
471         p_this->b_thread = 1;
472
473         msg_Dbg( p_this, "thread %d (%s) created (%s:%d)",
474                          p_this->thread_id, psz_name, psz_file, i_line );
475
476         vlc_mutex_unlock( &p_this->object_lock );
477     }
478     else
479     {
480         msg_Err( p_this, "%s thread could not be created at %s:%d (%s)",
481                          psz_name, psz_file, i_line, strerror(i_ret) );
482         vlc_mutex_unlock( &p_this->object_lock );
483     }
484
485     return i_ret;
486 }
487
488 /*****************************************************************************
489  * vlc_thread_ready: tell the parent thread we were successfully spawned
490  *****************************************************************************/
491 void __vlc_thread_ready( vlc_object_t *p_this )
492 {
493     vlc_mutex_lock( &p_this->object_lock );
494     vlc_cond_signal( &p_this->object_wait );
495     vlc_mutex_unlock( &p_this->object_lock );
496 }
497
498 /*****************************************************************************
499  * vlc_thread_join: wait until a thread exits, inner version
500  *****************************************************************************/
501 void __vlc_thread_join( vlc_object_t *p_this, char * psz_file, int i_line )
502 {
503     int i_ret = 0;
504
505 #if defined( PTH_INIT_IN_PTH_H )
506     i_ret = pth_join( p_this->thread_id, NULL );
507
508 #elif defined( ST_INIT_IN_ST_H )
509     i_ret = st_thread_join( p_this->thread_id, NULL );
510     
511 #elif defined( WIN32 )
512     WaitForSingleObject( p_this->thread_id, INFINITE );
513
514 #elif defined( PTHREAD_COND_T_IN_PTHREAD_H )
515     i_ret = pthread_join( p_this->thread_id, NULL );
516
517 #elif defined( HAVE_CTHREADS_H )
518     cthread_join( p_this->thread_id );
519     i_ret = 1;
520
521 #elif defined( HAVE_KERNEL_SCHEDULER_H )
522     int32 exit_value;
523     wait_for_thread( p_this->thread_id, &exit_value );
524
525 #endif
526
527     if( i_ret )
528     {
529         msg_Err( p_this, "thread_join(%d) failed at %s:%d (%s)",
530                          p_this->thread_id, psz_file, i_line, strerror(i_ret) );
531     }
532     else
533     {
534         msg_Dbg( p_this, "thread %d joined (%s:%d)",
535                          p_this->thread_id, psz_file, i_line );
536     }
537
538     p_this->b_thread = 0;
539 }
540
541 /*****************************************************************************
542  * vlc_thread_wrapper: wrapper around thread functions used when profiling.
543  *****************************************************************************/
544 #ifdef GPROF
545 static void *vlc_thread_wrapper( void *p_wrapper )
546 {
547     /* Put user data in thread-local variables */
548     void *            p_data = ((wrapper_t*)p_wrapper)->p_data;
549     vlc_thread_func_t func   = ((wrapper_t*)p_wrapper)->func;
550
551     /* Set the profile timer value */
552     setitimer( ITIMER_PROF, &((wrapper_t*)p_wrapper)->itimer, NULL );
553
554     /* Tell the calling thread that we don't need its data anymore */
555     vlc_mutex_lock( &((wrapper_t*)p_wrapper)->lock );
556     vlc_cond_signal( &((wrapper_t*)p_wrapper)->wait );
557     vlc_mutex_unlock( &((wrapper_t*)p_wrapper)->lock );
558
559     /* Call the real function */
560     return func( p_data );
561 }
562 #endif