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