threads.c 23.5 KB
Newer Older
1 2 3 4
/*****************************************************************************
 * threads.c : threads implementation for the VideoLAN client
 *****************************************************************************
 * Copyright (C) 1999, 2000, 2001, 2002 VideoLAN
5
 * $Id: threads.c,v 1.21 2002/10/04 18:07:22 sam Exp $
6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27
 *
 * Authors: Jean-Marc Dressler <polux@via.ecp.fr>
 *          Samuel Hocevar <sam@zoy.org>
 *          Gildas Bazin <gbazin@netcourrier.com>
 *
 * This program is free software; you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation; either version 2 of the License, or
 * (at your option) any later version.
 * 
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program; if not, write to the Free Software
 * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA  02111, USA.
 *****************************************************************************/

#include <vlc/vlc.h>

28 29
#include <stdlib.h>

30 31 32 33 34 35
#define VLC_THREADS_UNINITIALIZED  0
#define VLC_THREADS_PENDING        1
#define VLC_THREADS_ERROR          2
#define VLC_THREADS_READY          3

/*****************************************************************************
36
 * Global mutex for lazy initialization of the threads system
37 38 39 40 41 42 43 44 45 46 47 48
 *****************************************************************************/
static volatile int i_initializations = 0;

#if defined( PTH_INIT_IN_PTH_H )
#elif defined( ST_INIT_IN_ST_H )
#elif defined( WIN32 )
#elif defined( PTHREAD_COND_T_IN_PTHREAD_H )
    static pthread_mutex_t once_mutex = PTHREAD_MUTEX_INITIALIZER;
#elif defined( HAVE_CTHREADS_H )
#elif defined( HAVE_KERNEL_SCHEDULER_H )
#endif

49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64
/*****************************************************************************
 * Global variable for named mutexes
 *****************************************************************************/
typedef struct vlc_namedmutex_t vlc_namedmutex_t;
struct vlc_namedmutex_t
{
    vlc_mutex_t lock;

    char *psz_name;
    int i_usage;
    vlc_namedmutex_t *p_next;
};

static vlc_namedmutex_t *p_named_list = NULL;
static vlc_mutex_t named_lock;

65 66
/*****************************************************************************
 * vlc_threads_init: initialize threads system
67 68 69 70
 *****************************************************************************
 * This function requires lazy initialization of a global lock in order to
 * keep the library really thread-safe. Some architectures don't support this
 * and thus do not guarantee the complete reentrancy.
71 72 73
 *****************************************************************************/
int __vlc_threads_init( vlc_object_t *p_this )
{
74
    static volatile int i_status = VLC_THREADS_UNINITIALIZED;
75

76 77 78 79 80
    libvlc_t *p_libvlc = (libvlc_t *)p_this;
    int i_ret = VLC_SUCCESS;

    /* If we have lazy mutex initialization, use it. Otherwise, we just
     * hope nothing wrong happens. */
81 82 83
#if defined( PTH_INIT_IN_PTH_H )
#elif defined( ST_INIT_IN_ST_H )
#elif defined( WIN32 )
84
    HINSTANCE hInstLib;
85 86 87 88 89
#elif defined( PTHREAD_COND_T_IN_PTHREAD_H )
    pthread_mutex_lock( &once_mutex );
#elif defined( HAVE_CTHREADS_H )
#elif defined( HAVE_KERNEL_SCHEDULER_H )
#endif
90 91 92 93 94

    if( i_status == VLC_THREADS_UNINITIALIZED )
    {
        i_status = VLC_THREADS_PENDING;

95 96 97 98
        /* We should be safe now. Do all the initialization stuff we want. */
        vlc_object_create( p_libvlc, VLC_OBJECT_ROOT );
        p_libvlc->b_ready = VLC_FALSE;

99 100
#if defined( PTH_INIT_IN_PTH_H )
        i_ret = pth_init();
101

102 103
#elif defined( ST_INIT_IN_ST_H )
        i_ret = st_init();
104

105
#elif defined( WIN32 )
106
        /* Dynamically get the address of SignalObjectAndWait */
107 108 109 110 111 112
        if( GetVersion() < 0x80000000 )
        {
            /* We are running on NT/2K/XP, we can use SignalObjectAndWait */
            hInstLib = LoadLibrary( "kernel32" );
            if( hInstLib )
            {
113
                p_libvlc->SignalObjectAndWait =
114 115 116 117 118 119
                    (SIGNALOBJECTANDWAIT)GetProcAddress( hInstLib,
                                                     "SignalObjectAndWait" );
            }
        }
        else
        {
120
            p_libvlc->SignalObjectAndWait = NULL;
121 122
        }

123 124
        p_libvlc->b_fast_mutex = 0;
        p_libvlc->i_win9x_cv = 0;
125

126 127 128 129
#elif defined( PTHREAD_COND_T_IN_PTHREAD_H )
#elif defined( HAVE_CTHREADS_H )
#elif defined( HAVE_KERNEL_SCHEDULER_H )
#endif
130

131
        vlc_mutex_init( p_libvlc, &named_lock );
132

133 134 135 136
        if( i_ret )
        {
            i_status = VLC_THREADS_ERROR;
        }
137 138
        else
        {
139
            i_initializations++;
140 141 142 143 144
            i_status = VLC_THREADS_READY;
        }
    }
    else
    {
145 146
        /* Just increment the initialization count */
        i_initializations++;
147
    }
148

149 150
    /* If we have lazy mutex initialization support, unlock the mutex;
     * otherwize, do a naive wait loop. */
151
#if defined( PTH_INIT_IN_PTH_H )
152
    while( i_status == VLC_THREADS_PENDING ) msleep( THREAD_SLEEP );
153
#elif defined( ST_INIT_IN_ST_H )
154
    while( i_status == VLC_THREADS_PENDING ) msleep( THREAD_SLEEP );
155
#elif defined( WIN32 )
156
    while( i_status == VLC_THREADS_PENDING ) msleep( THREAD_SLEEP );
157 158 159
#elif defined( PTHREAD_COND_T_IN_PTHREAD_H )
    pthread_mutex_unlock( &once_mutex );
#elif defined( HAVE_CTHREADS_H )
160
    while( i_status == VLC_THREADS_PENDING ) msleep( THREAD_SLEEP );
161
#elif defined( HAVE_KERNEL_SCHEDULER_H )
162
    while( i_status == VLC_THREADS_PENDING ) msleep( THREAD_SLEEP );
163
#endif
164

165
    if( i_status != VLC_THREADS_READY )
166
    {
167
        return VLC_ETHREAD;
168 169
    }

170
    return i_ret;
171 172 173 174
}

/*****************************************************************************
 * vlc_threads_end: stop threads system
175 176 177
 *****************************************************************************
 * FIXME: This function is far from being threadsafe. We should undo exactly
 * what we did above in vlc_threads_init.
178
 *****************************************************************************/
179
int __vlc_threads_end( vlc_object_t *p_this )
180 181
{
#if defined( PTH_INIT_IN_PTH_H )
182 183 184 185 186
    i_initializations--;
    if( i_initializations == 0 )
    {
        return pth_kill();
    }
187 188

#elif defined( ST_INIT_IN_ST_H )
189
    i_initializations--;
190 191

#elif defined( WIN32 )
192
    i_initializations--;
193 194

#elif defined( PTHREAD_COND_T_IN_PTHREAD_H )
195 196 197
    pthread_mutex_lock( &once_mutex );
    i_initializations--;
    pthread_mutex_unlock( &once_mutex );
198 199

#elif defined( HAVE_CTHREADS_H )
200
    i_initializations--;
201 202

#elif defined( HAVE_KERNEL_SCHEDULER_H )
203
    i_initializations--;
204 205

#endif
206
    return VLC_SUCCESS;
207 208
}

209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246
/*****************************************************************************
 * Prototype for GPROF wrapper
 *****************************************************************************/
#ifdef GPROF
/* Wrapper function for profiling */
static void *      vlc_thread_wrapper ( void *p_wrapper );

#   ifdef WIN32

#       define ITIMER_REAL 1
#       define ITIMER_PROF 2

struct itimerval
{
    struct timeval it_value;
    struct timeval it_interval;
};  

int setitimer(int kind, const struct itimerval* itnew, struct itimerval* itold);
#   endif /* WIN32 */

typedef struct wrapper_t
{
    /* Data lock access */
    vlc_mutex_t lock;
    vlc_cond_t  wait;
    
    /* Data used to spawn the real thread */
    vlc_thread_func_t func;
    void *p_data;
    
    /* Profiling timer passed to the thread */
    struct itimerval itimer;
    
} wrapper_t;

#endif /* GPROF */

247 248 249 250 251
/*****************************************************************************
 * vlc_mutex_init: initialize a mutex
 *****************************************************************************/
int __vlc_mutex_init( vlc_object_t *p_this, vlc_mutex_t *p_mutex )
{
252 253
    p_mutex->p_this = p_this;

254
#if defined( PTH_INIT_IN_PTH_H )
255
    return pth_mutex_init( &p_mutex->mutex );
256 257

#elif defined( ST_INIT_IN_ST_H )
258
    p_mutex->mutex = st_mutex_new();
259
    return ( p_mutex->mutex == NULL ) ? errno : 0;
260 261 262 263 264 265

#elif defined( WIN32 )
    /* We use mutexes on WinNT/2K/XP because we can use the SignalObjectAndWait
     * function and have a 100% correct vlc_cond_wait() implementation.
     * As this function is not available on Win9x, we can use the faster
     * CriticalSections */
gbazin's avatar
 
gbazin committed
266 267
    if( p_this->p_libvlc->SignalObjectAndWait &&
        !p_this->p_libvlc->b_fast_mutex )
268 269 270
    {
        /* We are running on NT/2K/XP, we can use SignalObjectAndWait */
        p_mutex->mutex = CreateMutex( 0, FALSE, 0 );
271
        return ( p_mutex->mutex != NULL ? 0 : 1 );
272 273 274 275
    }
    else
    {
        p_mutex->mutex = NULL;
276
        InitializeCriticalSection( &p_mutex->csection );
277 278 279 280 281
        return 0;
    }

#elif defined( PTHREAD_COND_T_IN_PTHREAD_H )
#   if defined(DEBUG) && defined(SYS_LINUX)
282 283 284 285 286 287 288
    {
        /* Create error-checking mutex to detect problems more easily. */
        pthread_mutexattr_t attr;
        int                 i_result;

        pthread_mutexattr_init( &attr );
        pthread_mutexattr_setkind_np( &attr, PTHREAD_MUTEX_ERRORCHECK_NP );
289
        i_result = pthread_mutex_init( &p_mutex->mutex, &attr );
290 291 292
        pthread_mutexattr_destroy( &attr );
        return( i_result );
    }
293
#   endif
294
    return pthread_mutex_init( &p_mutex->mutex, NULL );
295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323

#elif defined( HAVE_CTHREADS_H )
    mutex_init( p_mutex );
    return 0;

#elif defined( HAVE_KERNEL_SCHEDULER_H )
    /* check the arguments and whether it's already been initialized */
    if( p_mutex == NULL )
    {
        return B_BAD_VALUE;
    }

    if( p_mutex->init == 9999 )
    {
        return EALREADY;
    }

    p_mutex->lock = create_sem( 1, "BeMutex" );
    if( p_mutex->lock < B_NO_ERROR )
    {
        return( -1 );
    }

    p_mutex->init = 9999;
    return B_OK;

#endif
}

324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416
/*****************************************************************************
 * vlc_mutex_need: create a global mutex from its name
 *****************************************************************************/
vlc_mutex_t * __vlc_mutex_need( vlc_object_t *p_this, char *psz_name )
{
    vlc_namedmutex_t *p_named;

    vlc_mutex_lock( &named_lock );

    p_named = p_named_list;
    while( p_named )
    {
        if( !strcmp( psz_name, p_named->psz_name ) )
        {
            break;
        }
        p_named = p_named->p_next;
    }

    if( p_named )
    {
        p_named->i_usage++;
    }
    else
    {
        p_named = malloc( sizeof( vlc_namedmutex_t ) );
        vlc_mutex_init( p_this, &p_named->lock );
        p_named->psz_name = strdup( psz_name );
        p_named->i_usage = 1;
        p_named->p_next = p_named_list;
        p_named_list = p_named;
    }

    vlc_mutex_unlock( &named_lock );

    return &p_named->lock;
}

/*****************************************************************************
 * vlc_mutex_unneed: destroy a global mutex from its name
 *****************************************************************************/
void __vlc_mutex_unneed( vlc_object_t *p_this, char *psz_name )
{
    vlc_namedmutex_t *p_named, *p_prev;

    vlc_mutex_lock( &named_lock );

    p_named = p_named_list;
    p_prev = NULL;
    while( p_named )
    {
        if( !strcmp( psz_name, p_named->psz_name ) )
        {
            break;
        }
        p_prev = p_named;
        p_named = p_named->p_next;
    }

    if( p_named )
    {
        p_named->i_usage--;

        if( p_named->i_usage <= 0 )
        {
            /* Unlink named mutex */
            if( p_prev )
            {
                p_prev->p_next = p_named->p_next;
            }
            else
            {
                p_named_list = p_named->p_next;
            }

            /* Release this lock as soon as possible */
            vlc_mutex_unlock( &named_lock );

            vlc_mutex_destroy( &p_named->lock );
            free( p_named->psz_name );
            free( p_named );

            return;
        }
    }
    else
    {
        msg_Err( p_this, "no named mutex called %s", psz_name );
    }

    vlc_mutex_unlock( &named_lock );
}

417 418 419 420 421
/*****************************************************************************
 * vlc_mutex_destroy: destroy a mutex, inner version
 *****************************************************************************/
int __vlc_mutex_destroy( char * psz_file, int i_line, vlc_mutex_t *p_mutex )
{
422 423 424 425 426
    int i_result;
    /* In case of error : */
    int i_thread = -1;
    const char * psz_error = "";

427 428 429 430
#if defined( PTH_INIT_IN_PTH_H )
    return 0;

#elif defined( ST_INIT_IN_ST_H )
431
    i_result = st_mutex_destroy( p_mutex->mutex );
432 433 434 435 436 437 438 439 440 441 442 443 444

#elif defined( WIN32 )
    if( p_mutex->mutex )
    {
        CloseHandle( p_mutex->mutex );
    }
    else
    {
        DeleteCriticalSection( &p_mutex->csection );
    }
    return 0;

#elif defined( PTHREAD_COND_T_IN_PTHREAD_H )    
445 446
    i_result = pthread_mutex_destroy( &p_mutex->mutex );
    if ( i_result )
447
    {
Christophe Massiot's avatar
Christophe Massiot committed
448
        i_thread = (int)pthread_self();
449
        psz_error = strerror(i_result);
450 451 452 453 454 455 456 457 458 459 460 461 462 463
    }

#elif defined( HAVE_CTHREADS_H )
    return 0;

#elif defined( HAVE_KERNEL_SCHEDULER_H )
    if( p_mutex->init == 9999 )
    {
        delete_sem( p_mutex->lock );
    }

    p_mutex->init = 0;
    return B_OK;
#endif    
464 465 466 467 468 469 470 471

    if( i_result )
    {
        msg_Err( p_mutex->p_this,
                 "thread %d: mutex_destroy failed at %s:%d (%d:%s)",
                 i_thread, psz_file, i_line, i_result, psz_error );
    }
    return i_result;
472 473 474 475 476
}

/*****************************************************************************
 * vlc_cond_init: initialize a condition
 *****************************************************************************/
477
int __vlc_cond_init( vlc_object_t *p_this, vlc_cond_t *p_condvar )
478
{
479 480
    p_condvar->p_this = p_this;

481
#if defined( PTH_INIT_IN_PTH_H )
482
    return pth_cond_init( &p_condvar->cond );
483 484

#elif defined( ST_INIT_IN_ST_H )
485 486
    p_condvar->cond = st_cond_new();
    return ( p_condvar->cond == NULL ) ? errno : 0;
487 488

#elif defined( WIN32 )
489
    /* Initialize counter */
490 491
    p_condvar->i_waiting_threads = 0;

gbazin's avatar
 
gbazin committed
492
    /* Misc init */
gbazin's avatar
 
gbazin committed
493 494
    p_condvar->i_win9x_cv = p_this->p_libvlc->i_win9x_cv;
    p_condvar->SignalObjectAndWait = p_this->p_libvlc->SignalObjectAndWait;
495

gbazin's avatar
 
gbazin committed
496
    if( (p_condvar->SignalObjectAndWait && !p_this->p_libvlc->b_fast_mutex)
gbazin's avatar
 
gbazin committed
497
        || p_condvar->i_win9x_cv == 0 )
gbazin's avatar
 
gbazin committed
498 499 500 501 502 503 504 505 506
    {
        /* Create an auto-reset event. */
        p_condvar->event = CreateEvent( NULL,   /* no security */
                                        FALSE,  /* auto-reset event */
                                        FALSE,  /* start non-signaled */
                                        NULL ); /* unnamed */

        p_condvar->semaphore = NULL;
        return !p_condvar->event;
507 508 509
    }
    else
    {
gbazin's avatar
 
gbazin committed
510 511 512 513 514
        p_condvar->semaphore = CreateSemaphore( NULL,       /* no security */
                                                0,          /* initial count */
                                                0x7fffffff, /* max count */
                                                NULL );     /* unnamed */

gbazin's avatar
 
gbazin committed
515
        if( p_condvar->i_win9x_cv == 1 )
gbazin's avatar
 
gbazin committed
516 517 518 519 520
            /* Create a manual-reset event initially signaled. */
            p_condvar->event = CreateEvent( NULL, TRUE, TRUE, NULL );
        else
            /* Create a auto-reset event. */
            p_condvar->event = CreateEvent( NULL, FALSE, FALSE, NULL );
521

gbazin's avatar
 
gbazin committed
522
        InitializeCriticalSection( &p_condvar->csection );
523

gbazin's avatar
 
gbazin committed
524
        return !p_condvar->semaphore || !p_condvar->event;
525
    }
526 527

#elif defined( PTHREAD_COND_T_IN_PTHREAD_H )
528
    return pthread_cond_init( &p_condvar->cond, NULL );
529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560

#elif defined( HAVE_CTHREADS_H )
    /* condition_init() */
    spin_lock_init( &p_condvar->lock );
    cthread_queue_init( &p_condvar->queue );
    p_condvar->name = 0;
    p_condvar->implications = 0;

    return 0;

#elif defined( HAVE_KERNEL_SCHEDULER_H )
    if( !p_condvar )
    {
        return B_BAD_VALUE;
    }

    if( p_condvar->init == 9999 )
    {
        return EALREADY;
    }

    p_condvar->thread = -1;
    p_condvar->init = 9999;
    return 0;
#endif
}

/*****************************************************************************
 * vlc_cond_destroy: destroy a condition, inner version
 *****************************************************************************/
int __vlc_cond_destroy( char * psz_file, int i_line, vlc_cond_t *p_condvar )
{
561 562 563 564 565
    int i_result;
    /* In case of error : */
    int i_thread = -1;
    const char * psz_error = "";

566 567 568 569
#if defined( PTH_INIT_IN_PTH_H )
    return 0;

#elif defined( ST_INIT_IN_ST_H )
570
    i_result = st_cond_destroy( p_condvar->cond );
571 572

#elif defined( WIN32 )
gbazin's avatar
 
gbazin committed
573
    if( !p_condvar->semaphore )
574
        i_result = !CloseHandle( p_condvar->event );
575
    else
576
        i_result = !CloseHandle( p_condvar->event )
gbazin's avatar
 
gbazin committed
577
          || !CloseHandle( p_condvar->semaphore );
578 579

#elif defined( PTHREAD_COND_T_IN_PTHREAD_H )
580 581
    i_result = pthread_cond_destroy( &p_condvar->cond );
    if ( i_result )
582
    {
Christophe Massiot's avatar
Christophe Massiot committed
583
        i_thread = (int)pthread_self();
584
        psz_error = strerror(i_result);
585 586 587 588 589 590 591 592
    }

#elif defined( HAVE_CTHREADS_H )
    return 0;

#elif defined( HAVE_KERNEL_SCHEDULER_H )
    p_condvar->init = 0;
    return 0;
593
#endif
594

595 596 597 598 599 600 601
    if( i_result )
    {
        msg_Err( p_condvar->p_this,
                 "thread %d: cond_destroy failed at %s:%d (%d:%s)",
                 i_thread, psz_file, i_line, i_result, psz_error );
    }
    return i_result;
602 603 604 605
}

/*****************************************************************************
 * vlc_thread_create: create a thread, inner version
606 607 608
 *****************************************************************************
 * Note that i_priority is only taken into account on platforms supporting
 * userland real-time priority threads.
609 610 611
 *****************************************************************************/
int __vlc_thread_create( vlc_object_t *p_this, char * psz_file, int i_line,
                         char *psz_name, void * ( *func ) ( void * ),
612
                         int i_priority, vlc_bool_t b_wait )
613 614 615
{
    int i_ret;

Sam Hocevar's avatar
Sam Hocevar committed
616
    vlc_mutex_lock( &p_this->object_lock );
617 618 619 620 621 622 623 624 625

#ifdef GPROF
    wrapper_t wrapper;

    /* Initialize the wrapper structure */
    wrapper.func = func;
    wrapper.p_data = (void *)p_this;
    getitimer( ITIMER_PROF, &wrapper.itimer );
    vlc_mutex_init( p_this, &wrapper.lock );
626
    vlc_cond_init( p_this, &wrapper.wait );
627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643
    vlc_mutex_lock( &wrapper.lock );

    /* Alter user-passed data so that we call the wrapper instead
     * of the real function */
    p_data = &wrapper;
    func = vlc_thread_wrapper;
#endif

#if defined( PTH_INIT_IN_PTH_H )
    p_this->thread_id = pth_spawn( PTH_ATTR_DEFAULT, func, (void *)p_this );
    i_ret = 0;

#elif defined( ST_INIT_IN_ST_H )
    p_this->thread_id = st_thread_create( func, (void *)p_this, 1, 0 );
    i_ret = 0;
    
#elif defined( WIN32 )
644 645 646
    {
        unsigned threadID;
        /* When using the MSVCRT C library you have to use the _beginthreadex
gbazin's avatar
 
gbazin committed
647 648
         * function instead of CreateThread, otherwise you'll end up with
	 * memory leaks and the signal functions not working */
649 650 651 652
        p_this->thread_id =
                (HANDLE)_beginthreadex( NULL, 0, (PTHREAD_START) func, 
                                        (void *)p_this, 0, &threadID );
    }
gbazin's avatar
 
gbazin committed
653

654 655 656 657 658 659 660 661 662
    if ( p_this->thread_id && i_priority )
    {
        if ( !SetThreadPriority(p_this->thread_id, i_priority) )
        {
            msg_Warn( p_this, "couldn't set a faster priority" );
            i_priority = 0;
        }
    }

663 664 665 666 667
    i_ret = ( p_this->thread_id ? 0 : 1 );

#elif defined( PTHREAD_COND_T_IN_PTHREAD_H )
    i_ret = pthread_create( &p_this->thread_id, NULL, func, (void *)p_this );

668 669 670 671 672 673 674 675 676 677 678 679
    if ( i_priority )
    {
        struct sched_param param;
        memset( &param, 0, sizeof(struct sched_param) );
        param.sched_priority = i_priority;
        if ( pthread_setschedparam( p_this->thread_id, SCHED_RR, &param ) )
        {
            msg_Warn( p_this, "couldn't go to real-time priority" );
            i_priority = 0;
        }
    }

680 681 682 683 684 685 686 687 688 689 690 691 692 693 694 695 696 697 698 699 700 701 702 703 704 705 706
#elif defined( HAVE_CTHREADS_H )
    p_this->thread_id = cthread_fork( (cthread_fn_t)func, (any_t)p_this );
    i_ret = 0;

#elif defined( HAVE_KERNEL_SCHEDULER_H )
    p_this->thread_id = spawn_thread( (thread_func)func, psz_name,
                                      B_NORMAL_PRIORITY, (void *)p_this );
    i_ret = resume_thread( p_this->thread_id );

#endif

#ifdef GPROF
    if( i_ret == 0 )
    {
        vlc_cond_wait( &wrapper.wait, &wrapper.lock );
    }

    vlc_mutex_unlock( &wrapper.lock );
    vlc_mutex_destroy( &wrapper.lock );
    vlc_cond_destroy( &wrapper.wait );
#endif

    if( i_ret == 0 )
    {
        if( b_wait )
        {
            msg_Dbg( p_this, "waiting for thread completion" );
Sam Hocevar's avatar
Sam Hocevar committed
707
            vlc_cond_wait( &p_this->object_wait, &p_this->object_lock );
708 709
        }

710 711
        p_this->b_thread = 1;

712 713 714
        msg_Dbg( p_this, "thread %d (%s) created at priority %d (%s:%d)",
                 p_this->thread_id, psz_name, i_priority,
                 psz_file, i_line );
715

Sam Hocevar's avatar
Sam Hocevar committed
716
        vlc_mutex_unlock( &p_this->object_lock );
717 718 719 720 721
    }
    else
    {
        msg_Err( p_this, "%s thread could not be created at %s:%d (%s)",
                         psz_name, psz_file, i_line, strerror(i_ret) );
Sam Hocevar's avatar
Sam Hocevar committed
722
        vlc_mutex_unlock( &p_this->object_lock );
723 724 725 726 727 728 729 730 731 732
    }

    return i_ret;
}

/*****************************************************************************
 * vlc_thread_ready: tell the parent thread we were successfully spawned
 *****************************************************************************/
void __vlc_thread_ready( vlc_object_t *p_this )
{
Sam Hocevar's avatar
Sam Hocevar committed
733 734 735
    vlc_mutex_lock( &p_this->object_lock );
    vlc_cond_signal( &p_this->object_wait );
    vlc_mutex_unlock( &p_this->object_lock );
736 737 738 739 740 741 742 743 744 745 746 747 748 749 750 751 752 753 754 755 756 757 758 759 760 761 762 763 764 765 766 767 768 769 770 771 772 773 774 775 776 777 778 779 780 781 782 783 784 785 786 787 788 789 790 791 792 793 794 795 796 797 798 799 800 801 802
}

/*****************************************************************************
 * vlc_thread_join: wait until a thread exits, inner version
 *****************************************************************************/
void __vlc_thread_join( vlc_object_t *p_this, char * psz_file, int i_line )
{
    int i_ret = 0;

#if defined( PTH_INIT_IN_PTH_H )
    i_ret = pth_join( p_this->thread_id, NULL );

#elif defined( ST_INIT_IN_ST_H )
    i_ret = st_thread_join( p_this->thread_id, NULL );
    
#elif defined( WIN32 )
    WaitForSingleObject( p_this->thread_id, INFINITE );

#elif defined( PTHREAD_COND_T_IN_PTHREAD_H )
    i_ret = pthread_join( p_this->thread_id, NULL );

#elif defined( HAVE_CTHREADS_H )
    cthread_join( p_this->thread_id );
    i_ret = 1;

#elif defined( HAVE_KERNEL_SCHEDULER_H )
    int32 exit_value;
    wait_for_thread( p_this->thread_id, &exit_value );

#endif

    if( i_ret )
    {
        msg_Err( p_this, "thread_join(%d) failed at %s:%d (%s)",
                         p_this->thread_id, psz_file, i_line, strerror(i_ret) );
    }
    else
    {
        msg_Dbg( p_this, "thread %d joined (%s:%d)",
                         p_this->thread_id, psz_file, i_line );
    }

    p_this->b_thread = 0;
}

/*****************************************************************************
 * vlc_thread_wrapper: wrapper around thread functions used when profiling.
 *****************************************************************************/
#ifdef GPROF
static void *vlc_thread_wrapper( void *p_wrapper )
{
    /* Put user data in thread-local variables */
    void *            p_data = ((wrapper_t*)p_wrapper)->p_data;
    vlc_thread_func_t func   = ((wrapper_t*)p_wrapper)->func;

    /* Set the profile timer value */
    setitimer( ITIMER_PROF, &((wrapper_t*)p_wrapper)->itimer, NULL );

    /* Tell the calling thread that we don't need its data anymore */
    vlc_mutex_lock( &((wrapper_t*)p_wrapper)->lock );
    vlc_cond_signal( &((wrapper_t*)p_wrapper)->wait );
    vlc_mutex_unlock( &((wrapper_t*)p_wrapper)->lock );

    /* Call the real function */
    return func( p_data );
}
#endif