threads.c 23.3 KB
Newer Older
1
2
3
/*****************************************************************************
 * threads.c : threads implementation for the VideoLAN client
 *****************************************************************************
4
 * Copyright (C) 1999-2007 the VideoLAN team
5
 * $Id$
6
7
8
9
 *
 * Authors: Jean-Marc Dressler <polux@via.ecp.fr>
 *          Samuel Hocevar <sam@zoy.org>
 *          Gildas Bazin <gbazin@netcourrier.com>
10
 *          Clément Sténac
11
12
13
14
15
 *
 * 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.
16
 *
17
18
19
20
21
22
23
 * 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
dionoea's avatar
dionoea committed
24
 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston MA 02110-1301, USA.
25
26
 *****************************************************************************/

27
28
29
30
#ifdef HAVE_CONFIG_H
# include "config.h"
#endif

31
#include <vlc_common.h>
32

33
#include "libvlc.h"
34
#include <assert.h>
35
36
37
#ifdef HAVE_UNISTD_H
# include <unistd.h>
#endif
38
#include <signal.h>
39

40
41
42
43
44
45
#define VLC_THREADS_UNINITIALIZED  0
#define VLC_THREADS_PENDING        1
#define VLC_THREADS_ERROR          2
#define VLC_THREADS_READY          3

/*****************************************************************************
46
 * Global mutex for lazy initialization of the threads system
47
 *****************************************************************************/
48
static volatile unsigned i_initializations = 0;
49

50
#if defined( LIBVLC_USE_PTHREAD )
51
52
# include <sched.h>

53
static pthread_mutex_t once_mutex = PTHREAD_MUTEX_INITIALIZER;
54
55
#endif

56
57
58
59
/**
 * Global process-wide VLC object.
 * Contains inter-instance data, such as the module cache and global mutexes.
 */
60
static libvlc_global_data_t *p_root;
61
62
63
64

libvlc_global_data_t *vlc_global( void )
{
    assert( i_initializations > 0 );
65
    return p_root;
66
67
}

68
#ifndef NDEBUG
69
70
71
72
73
74
75
76
77
/**
 * Object running the current thread
 */
static vlc_threadvar_t thread_object_key;

vlc_object_t *vlc_threadobj (void)
{
    return vlc_threadvar_get (&thread_object_key);
}
78
#endif
79

80
81
vlc_threadvar_t msg_context_global_key;

Rémi Denis-Courmont's avatar
Rémi Denis-Courmont committed
82
83
84
85
86
87
88
89
#if defined(LIBVLC_USE_PTHREAD)
static inline unsigned long vlc_threadid (void)
{
     union { pthread_t th; unsigned long int i; } v = { };
     v.th = pthread_self ();
     return v.i;
}

90
#if defined(HAVE_EXECINFO_H) && defined(HAVE_BACKTRACE)
91
92
93
# include <execinfo.h>
#endif

94
/*****************************************************************************
Rémi Denis-Courmont's avatar
Rémi Denis-Courmont committed
95
 * vlc_thread_fatal: Report an error from the threading layer
96
 *****************************************************************************
Rémi Denis-Courmont's avatar
Rémi Denis-Courmont committed
97
 * This is mostly meant for debugging.
98
 *****************************************************************************/
Rémi Denis-Courmont's avatar
Rémi Denis-Courmont committed
99
100
void vlc_pthread_fatal (const char *action, int error,
                        const char *file, unsigned line)
101
{
Rémi Denis-Courmont's avatar
Rémi Denis-Courmont committed
102
103
104
105
    fprintf (stderr, "LibVLC fatal error %s in thread %lu at %s:%u: %d\n",
             action, vlc_threadid (), file, line, error);
    fflush (stderr);

106
    /* Sometimes strerror_r() crashes too, so make sure we print an error
Rémi Denis-Courmont's avatar
Rémi Denis-Courmont committed
107
     * message before we invoke it */
108
109
110
#ifdef __GLIBC__
    /* Avoid the strerror_r() prototype brain damage in glibc */
    errno = error;
111
    dprintf (2, " Error message: %m at:\n");
112
#else
113
114
115
    char buf[1000];
    const char *msg;

Rémi Denis-Courmont's avatar
Typo    
Rémi Denis-Courmont committed
116
    switch (strerror_r (error, buf, sizeof (buf)))
117
118
119
120
121
122
123
124
125
126
127
128
    {
        case 0:
            msg = buf;
            break;
        case ERANGE: /* should never happen */
            msg = "unknwon (too big to display)";
            break;
        default:
            msg = "unknown (invalid error number)";
            break;
    }
    fprintf (stderr, " Error message: %s\n", msg);
129
    fflush (stderr);
130
#endif
131

132
#ifdef HAVE_BACKTRACE
133
134
135
136
137
    void *stack[20];
    int len = backtrace (stack, sizeof (stack) / sizeof (stack[0]));
    backtrace_symbols_fd (stack, len, 2);
#endif

Rémi Denis-Courmont's avatar
Rémi Denis-Courmont committed
138
    abort ();
139
}
140
141
142
143
144
145
146
#else
void vlc_pthread_fatal (const char *action, int error,
                        const char *file, unsigned line)
{
    (void)action; (void)error; (void)file; (void)line;
    abort();
}
Rémi Denis-Courmont's avatar
Rémi Denis-Courmont committed
147
#endif
148

149
150
/*****************************************************************************
 * vlc_threads_init: initialize threads system
151
152
153
154
 *****************************************************************************
 * 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.
155
 *****************************************************************************/
156
int vlc_threads_init( void )
157
{
158
159
160
161
    int i_ret = VLC_SUCCESS;

    /* If we have lazy mutex initialization, use it. Otherwise, we just
     * hope nothing wrong happens. */
162
#if defined( LIBVLC_USE_PTHREAD )
163
164
    pthread_mutex_lock( &once_mutex );
#endif
165

Rémi Denis-Courmont's avatar
Rémi Denis-Courmont committed
166
    if( i_initializations == 0 )
167
    {
168
        p_root = vlc_custom_create( NULL, sizeof( *p_root ),
169
                                    VLC_OBJECT_GENERIC, "root" );
170
        if( p_root == NULL )
171
        {
Rémi Denis-Courmont's avatar
Rémi Denis-Courmont committed
172
173
            i_ret = VLC_ENOMEM;
            goto out;
174
        }
175
176

        /* We should be safe now. Do all the initialization stuff we want. */
177
#ifndef NDEBUG
178
        vlc_threadvar_create( &thread_object_key, NULL );
179
#endif
180
        vlc_threadvar_create( &msg_context_global_key, msg_StackDestroy );
181
    }
Rémi Denis-Courmont's avatar
Rémi Denis-Courmont committed
182
    i_initializations++;
183

Rémi Denis-Courmont's avatar
Rémi Denis-Courmont committed
184
185
186
out:
    /* If we have lazy mutex initialization support, unlock the mutex.
     * Otherwize, we are screwed. */
187
#if defined( LIBVLC_USE_PTHREAD )
188
189
    pthread_mutex_unlock( &once_mutex );
#endif
190

191
    return i_ret;
192
193
194
195
}

/*****************************************************************************
 * vlc_threads_end: stop threads system
196
 *****************************************************************************
197
 * FIXME: This function is far from being threadsafe.
198
 *****************************************************************************/
199
void vlc_threads_end( void )
200
{
201
#if defined( LIBVLC_USE_PTHREAD )
202
203
204
    pthread_mutex_lock( &once_mutex );
#endif

Rémi Denis-Courmont's avatar
Rémi Denis-Courmont committed
205
    assert( i_initializations > 0 );
206
207

    if( i_initializations == 1 )
208
    {
209
        vlc_object_release( p_root );
210
        vlc_threadvar_delete( &msg_context_global_key );
211
#ifndef NDEBUG
212
        vlc_threadvar_delete( &thread_object_key );
213
#endif
214
    }
215
    i_initializations--;
216

217
#if defined( LIBVLC_USE_PTHREAD )
218
    pthread_mutex_unlock( &once_mutex );
219
220
221
#endif
}

222
223
#if defined (__GLIBC__) && (__GLIBC_MINOR__ < 6)
/* This is not prototyped under glibc, though it exists. */
224
225
226
int pthread_mutexattr_setkind_np( pthread_mutexattr_t *attr, int kind );
#endif

227
228
229
/*****************************************************************************
 * vlc_mutex_init: initialize a mutex
 *****************************************************************************/
Rémi Denis-Courmont's avatar
Rémi Denis-Courmont committed
230
int vlc_mutex_init( vlc_mutex_t *p_mutex )
231
{
232
233
234
235
236
237
238
239
#if defined( LIBVLC_USE_PTHREAD )
    pthread_mutexattr_t attr;
    int                 i_result;

    pthread_mutexattr_init( &attr );

# ifndef NDEBUG
    /* Create error-checking mutex to detect problems more easily. */
240
#  if defined (__GLIBC__) && (__GLIBC_MINOR__ < 6)
241
242
243
244
245
246
247
248
249
    pthread_mutexattr_setkind_np( &attr, PTHREAD_MUTEX_ERRORCHECK_NP );
#  else
    pthread_mutexattr_settype( &attr, PTHREAD_MUTEX_ERRORCHECK );
#  endif
# endif
    i_result = pthread_mutex_init( p_mutex, &attr );
    pthread_mutexattr_destroy( &attr );
    return i_result;
#elif defined( UNDER_CE )
250
251
252
    InitializeCriticalSection( &p_mutex->csection );
    return 0;

253
#elif defined( WIN32 )
Rémi Denis-Courmont's avatar
Rémi Denis-Courmont committed
254
255
    *p_mutex = CreateMutex( 0, FALSE, 0 );
    return (*p_mutex != NULL) ? 0 : ENOMEM;
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280

#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
}

281
282
283
/*****************************************************************************
 * vlc_mutex_init: initialize a recursive mutex (Do not use)
 *****************************************************************************/
Rémi Denis-Courmont's avatar
Rémi Denis-Courmont committed
284
int vlc_mutex_init_recursive( vlc_mutex_t *p_mutex )
285
{
286
#if defined( LIBVLC_USE_PTHREAD )
287
288
289
290
    pthread_mutexattr_t attr;
    int                 i_result;

    pthread_mutexattr_init( &attr );
291
292
293
#  if defined (__GLIBC__) && (__GLIBC_MINOR__ < 6)
    pthread_mutexattr_setkind_np( &attr, PTHREAD_MUTEX_RECURSIVE_NP );
#  else
294
    pthread_mutexattr_settype( &attr, PTHREAD_MUTEX_RECURSIVE );
295
#  endif
296
    i_result = pthread_mutex_init( p_mutex, &attr );
297
298
    pthread_mutexattr_destroy( &attr );
    return( i_result );
299
300
301
302
#elif defined( WIN32 )
    /* Create mutex returns a recursive mutex */
    *p_mutex = CreateMutex( 0, FALSE, 0 );
    return (*p_mutex != NULL) ? 0 : ENOMEM;
303
#else
304
# error Unimplemented!
305
306
307
308
#endif
}


309
310
311
/*****************************************************************************
 * vlc_mutex_destroy: destroy a mutex, inner version
 *****************************************************************************/
Rémi Denis-Courmont's avatar
Rémi Denis-Courmont committed
312
void __vlc_mutex_destroy( const char * psz_file, int i_line, vlc_mutex_t *p_mutex )
313
{
314
315
316
317
318
#if defined( LIBVLC_USE_PTHREAD )
    int val = pthread_mutex_destroy( p_mutex );
    VLC_THREAD_ASSERT ("destroying mutex");

#elif defined( UNDER_CE )
319
320
    VLC_UNUSED( psz_file); VLC_UNUSED( i_line );

321
322
    DeleteCriticalSection( &p_mutex->csection );

323
#elif defined( WIN32 )
324
325
    VLC_UNUSED( psz_file); VLC_UNUSED( i_line );

Rémi Denis-Courmont's avatar
Rémi Denis-Courmont committed
326
    CloseHandle( *p_mutex );
327

328
329
330
331
332
333
#elif defined( HAVE_KERNEL_SCHEDULER_H )
    if( p_mutex->init == 9999 )
        delete_sem( p_mutex->lock );

    p_mutex->init = 0;

334
#endif
335
336
337
338
339
}

/*****************************************************************************
 * vlc_cond_init: initialize a condition
 *****************************************************************************/
340
int __vlc_cond_init( vlc_cond_t *p_condvar )
341
{
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
#if defined( LIBVLC_USE_PTHREAD )
    pthread_condattr_t attr;
    int ret;

    ret = pthread_condattr_init (&attr);
    if (ret)
        return ret;

# if !defined (_POSIX_CLOCK_SELECTION)
   /* Fairly outdated POSIX support (that was defined in 2001) */
#  define _POSIX_CLOCK_SELECTION (-1)
# endif
# if (_POSIX_CLOCK_SELECTION >= 0)
    /* NOTE: This must be the same clock as the one in mtime.c */
    pthread_condattr_setclock (&attr, CLOCK_MONOTONIC);
# endif

    ret = pthread_cond_init (p_condvar, &attr);
    pthread_condattr_destroy (&attr);
    return ret;

#elif defined( UNDER_CE ) || defined( WIN32 )
364
    /* Initialize counter */
365
366
    p_condvar->i_waiting_threads = 0;

367
368
369
370
371
372
    /* Create an auto-reset event. */
    p_condvar->event = CreateEvent( NULL,   /* no security */
                                    FALSE,  /* auto-reset event */
                                    FALSE,  /* start non-signaled */
                                    NULL ); /* unnamed */
    return !p_condvar->event;
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387

#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;
388

389
390
391
392
393
394
#endif
}

/*****************************************************************************
 * vlc_cond_destroy: destroy a condition, inner version
 *****************************************************************************/
Rémi Denis-Courmont's avatar
Rémi Denis-Courmont committed
395
void __vlc_cond_destroy( const char * psz_file, int i_line, vlc_cond_t *p_condvar )
396
{
397
398
399
400
401
#if defined( LIBVLC_USE_PTHREAD )
    int val = pthread_cond_destroy( p_condvar );
    VLC_THREAD_ASSERT ("destroying condition");

#elif defined( UNDER_CE ) || defined( WIN32 )
402
403
    VLC_UNUSED( psz_file); VLC_UNUSED( i_line );

Rémi Denis-Courmont's avatar
Rémi Denis-Courmont committed
404
    CloseHandle( p_condvar->event );
405

406
407
408
#elif defined( HAVE_KERNEL_SCHEDULER_H )
    p_condvar->init = 0;

409
#endif
410
411
}

412
413
414
/*****************************************************************************
 * vlc_tls_create: create a thread-local variable
 *****************************************************************************/
415
int vlc_threadvar_create( vlc_threadvar_t *p_tls, void (*destr) (void *) )
416
{
417
    int i_ret;
418

419
#if defined( LIBVLC_USE_PTHREAD )
420
    i_ret =  pthread_key_create( p_tls, destr );
Rémi Denis-Courmont's avatar
Rémi Denis-Courmont committed
421
#elif defined( UNDER_CE )
422
    i_ret = ENOSYS;
423
#elif defined( WIN32 )
424
    *p_tls = TlsAlloc();
Rémi Denis-Courmont's avatar
Rémi Denis-Courmont committed
425
    i_ret = (*p_tls == TLS_OUT_OF_INDEXES) ? EAGAIN : 0;
426
427
#else
# error Unimplemented!
428
#endif
429
    return i_ret;
430
431
}

432
433
434
void vlc_threadvar_delete (vlc_threadvar_t *p_tls)
{
#if defined( LIBVLC_USE_PTHREAD )
435
    pthread_key_delete (*p_tls);
436
437
438
439
440
441
442
443
#elif defined( UNDER_CE )
#elif defined( WIN32 )
    TlsFree (*p_tls);
#else
# error Unimplemented!
#endif
}

444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
struct vlc_thread_boot
{
    void * (*entry) (void *);
    vlc_object_t *object;
};

#if defined (LIBVLC_USE_PTHREAD)
# define THREAD_RTYPE void *
# define THREAD_RVAL  NULL
#elif defined (WIN32)
# define THREAD_RTYPE __stdcall unsigned
# define THREAD_RVAL 0
#endif

static THREAD_RTYPE thread_entry (void *data)
{
    vlc_object_t *obj = ((struct vlc_thread_boot *)data)->object;
    void *(*func) (void *) = ((struct vlc_thread_boot *)data)->entry;

    free (data);
464
#ifndef NDEBUG
465
    vlc_threadvar_set (&thread_object_key, obj);
466
#endif
467
468
469
470
471
472
    msg_Dbg (obj, "thread started");
    func (obj);
    msg_Dbg (obj, "thread ended");
    return THREAD_RVAL;
}

473
474
/*****************************************************************************
 * vlc_thread_create: create a thread, inner version
475
476
477
 *****************************************************************************
 * Note that i_priority is only taken into account on platforms supporting
 * userland real-time priority threads.
478
 *****************************************************************************/
479
480
int __vlc_thread_create( vlc_object_t *p_this, const char * psz_file, int i_line,
                         const char *psz_name, void * ( *func ) ( void * ),
481
                         int i_priority, bool b_wait )
482
483
{
    int i_ret;
484
    vlc_object_internals_t *p_priv = vlc_internals( p_this );
485

486
487
488
489
490
491
    struct vlc_thread_boot *boot = malloc (sizeof (*boot));
    if (boot == NULL)
        return errno;
    boot->entry = func;
    boot->object = p_this;

Sam Hocevar's avatar
Sam Hocevar committed
492
    vlc_mutex_lock( &p_this->object_lock );
493

494
#if defined( LIBVLC_USE_PTHREAD )
495
496
    pthread_attr_t attr;
    pthread_attr_init (&attr);
497

498
499
500
501
502
503
504
505
506
507
    /* Block the signals that signals interface plugin handles.
     * If the LibVLC caller wants to handle some signals by itself, it should
     * block these before whenever invoking LibVLC. And it must obviously not
     * start the VLC signals interface plugin.
     *
     * LibVLC will normally ignore any interruption caused by an asynchronous
     * signal during a system call. But there may well be some buggy cases
     * where it fails to handle EINTR (bug reports welcome). Some underlying
     * libraries might also not handle EINTR properly.
     */
508
    sigset_t set, oldset;
509
510
511
512
513
514
515
    sigemptyset (&set);
    sigdelset (&set, SIGHUP);
    sigaddset (&set, SIGINT);
    sigaddset (&set, SIGQUIT);
    sigaddset (&set, SIGTERM);

    sigaddset (&set, SIGPIPE); /* We don't want this one, really! */
516
517
    pthread_sigmask (SIG_BLOCK, &set, &oldset);

518
519
520
521
#ifndef __APPLE__
    if( config_GetInt( p_this, "rt-priority" ) > 0 )
#endif
    {
522
523
524
        struct sched_param p = { .sched_priority = i_priority, };
        int policy;

525
        /* Hack to avoid error msg */
526
        if( config_GetType( p_this, "rt-offset" ) )
527
528
529
            p.sched_priority += config_GetInt( p_this, "rt-offset" );
        if( p.sched_priority <= 0 )
            p.sched_priority += sched_get_priority_max (policy = SCHED_OTHER);
530
        else
531
            p.sched_priority += sched_get_priority_min (policy = SCHED_RR);
532

533
534
        pthread_attr_setschedpolicy (&attr, policy);
        pthread_attr_setschedparam (&attr, &p);
535
    }
536
537
538
539

    i_ret = pthread_create( &p_priv->thread_id, &attr, thread_entry, boot );
    pthread_sigmask (SIG_SETMASK, &oldset, NULL);
    pthread_attr_destroy (&attr);
540
541

#elif defined( WIN32 ) || defined( UNDER_CE )
542
543
    {
        /* When using the MSVCRT C library you have to use the _beginthreadex
gbazin's avatar
   
gbazin committed
544
         * function instead of CreateThread, otherwise you'll end up with
545
546
547
         * memory leaks and the signal functions not working (see Microsoft
         * Knowledge Base, article 104641) */
#if defined( UNDER_CE )
548
549
        HANDLE hThread = CreateThread( NULL, 0, thread_entry,
                                       (LPVOID)boot, CREATE_SUSPENDED,
Rémi Denis-Courmont's avatar
Rémi Denis-Courmont committed
550
                                        NULL );
551
#else
Rémi Denis-Courmont's avatar
Rémi Denis-Courmont committed
552
        HANDLE hThread = (HANDLE)(uintptr_t)
553
554
            _beginthreadex( NULL, 0, thread_entry, boot,
                            CREATE_SUSPENDED, NULL );
555
#endif
Rémi Denis-Courmont's avatar
Rémi Denis-Courmont committed
556
557
        p_priv->thread_id = hThread;
        ResumeThread(hThread);
558
    }
gbazin's avatar
   
gbazin committed
559

Rémi Denis-Courmont's avatar
Rémi Denis-Courmont committed
560
    i_ret = ( p_priv->thread_id ? 0 : errno );
561

562
    if( !i_ret && i_priority )
563
    {
Rémi Denis-Courmont's avatar
Rémi Denis-Courmont committed
564
        if( !SetThreadPriority(p_priv->thread_id, i_priority) )
565
566
567
568
569
570
        {
            msg_Warn( p_this, "couldn't set a faster priority" );
            i_priority = 0;
        }
    }

571
#elif defined( HAVE_KERNEL_SCHEDULER_H )
572
    p_priv->thread_id = spawn_thread( (thread_func)thread_entry, psz_name,
573
                                      i_priority, p_data );
574
    i_ret = resume_thread( p_priv->thread_id );
575

576
577
578
579
580
581
582
#endif

    if( i_ret == 0 )
    {
        if( b_wait )
        {
            msg_Dbg( p_this, "waiting for thread completion" );
Rémi Denis-Courmont's avatar
Rémi Denis-Courmont committed
583
            vlc_object_wait( p_this );
584
585
        }

586
        p_priv->b_thread = true;
Rémi Denis-Courmont's avatar
Rémi Denis-Courmont committed
587
588
        msg_Dbg( p_this, "thread %lu (%s) created at priority %d (%s:%d)",
                 (unsigned long)p_priv->thread_id, psz_name, i_priority,
589
                 psz_file, i_line );
590
591
592
    }
    else
    {
593
594
595
        errno = i_ret;
        msg_Err( p_this, "%s thread could not be created at %s:%d (%m)",
                         psz_name, psz_file, i_line );
596
597
    }

598
    vlc_mutex_unlock( &p_this->object_lock );
599
600
601
    return i_ret;
}

602
603
604
605
/*****************************************************************************
 * vlc_thread_set_priority: set the priority of the current thread when we
 * couldn't set it in vlc_thread_create (for instance for the main thread)
 *****************************************************************************/
606
int __vlc_thread_set_priority( vlc_object_t *p_this, const char * psz_file,
607
608
                               int i_line, int i_priority )
{
609
    vlc_object_internals_t *p_priv = vlc_internals( p_this );
610

611
#if defined( LIBVLC_USE_PTHREAD )
612
613
614
# ifndef __APPLE__
    if( config_GetInt( p_this, "rt-priority" ) > 0 )
# endif
615
    {
616
        int i_error, i_policy;
617
        struct sched_param param;
618

619
        memset( &param, 0, sizeof(struct sched_param) );
620
621
        if( config_GetType( p_this, "rt-offset" ) )
            i_priority += config_GetInt( p_this, "rt-offset" );
622
        if( i_priority <= 0 )
623
624
625
626
627
628
629
630
631
        {
            param.sched_priority = (-1) * i_priority;
            i_policy = SCHED_OTHER;
        }
        else
        {
            param.sched_priority = i_priority;
            i_policy = SCHED_RR;
        }
632
633
634
        if( !p_priv->thread_id )
            p_priv->thread_id = pthread_self();
        if( (i_error = pthread_setschedparam( p_priv->thread_id,
635
                                               i_policy, &param )) )
636
        {
637
638
639
            errno = i_error;
            msg_Warn( p_this, "couldn't set thread priority (%s:%d): %m",
                      psz_file, i_line );
640
            i_priority = 0;
641
642
        }
    }
643
644
645
646

#elif defined( WIN32 ) || defined( UNDER_CE )
    VLC_UNUSED( psz_file); VLC_UNUSED( i_line );

Rémi Denis-Courmont's avatar
Rémi Denis-Courmont committed
647
648
649
    if( !p_priv->thread_id )
        p_priv->thread_id = GetCurrentThread();
    if( !SetThreadPriority(p_priv->thread_id, i_priority) )
650
651
652
653
654
    {
        msg_Warn( p_this, "couldn't set a faster priority" );
        return 1;
    }

655
#endif
gbazin's avatar
   
gbazin committed
656
657

    return 0;
658
659
}

660
661
662
663
664
/*****************************************************************************
 * vlc_thread_ready: tell the parent thread we were successfully spawned
 *****************************************************************************/
void __vlc_thread_ready( vlc_object_t *p_this )
{
Rémi Denis-Courmont's avatar
Rémi Denis-Courmont committed
665
    vlc_object_signal( p_this );
666
667
668
669
670
}

/*****************************************************************************
 * vlc_thread_join: wait until a thread exits, inner version
 *****************************************************************************/
671
void __vlc_thread_join( vlc_object_t *p_this, const char * psz_file, int i_line )
672
{
673
    vlc_object_internals_t *p_priv = vlc_internals( p_this );
674
675
676
677
678
679
680
681
682
    int i_ret = 0;

#if defined( LIBVLC_USE_PTHREAD )
    /* Make sure we do return if we are calling vlc_thread_join()
     * from the joined thread */
    if (pthread_equal (pthread_self (), p_priv->thread_id))
        i_ret = pthread_detach (p_priv->thread_id);
    else
        i_ret = pthread_join (p_priv->thread_id, NULL);
683

684
#elif defined( UNDER_CE ) || defined( WIN32 )
685
686
687
688
689
    HMODULE hmodule;
    BOOL (WINAPI *OurGetThreadTimes)( HANDLE, FILETIME*, FILETIME*,
                                      FILETIME*, FILETIME* );
    FILETIME create_ft, exit_ft, kernel_ft, user_ft;
    int64_t real_time, kernel_time, user_time;
690
    HANDLE hThread;
691

692
    /*
693
    ** object will close its thread handle when destroyed, duplicate it here
694
695
    ** to be on the safe side
    */
696
    if( ! DuplicateHandle(GetCurrentProcess(),
Rémi Denis-Courmont's avatar
Rémi Denis-Courmont committed
697
            p_priv->thread_id,
698
699
700
701
702
703
            GetCurrentProcess(),
            &hThread,
            0,
            FALSE,
            DUPLICATE_SAME_ACCESS) )
    {
704
        p_priv->b_thread = false;
Rémi Denis-Courmont's avatar
Rémi Denis-Courmont committed
705
706
        i_ret = GetLastError();
        goto error;
707
    }
708

709
    WaitForSingleObject( hThread, INFINITE );
710
711
712
713
714
715
716
717
718
719
720

#if defined( UNDER_CE )
    hmodule = GetModuleHandle( _T("COREDLL") );
#else
    hmodule = GetModuleHandle( _T("KERNEL32") );
#endif
    OurGetThreadTimes = (BOOL (WINAPI*)( HANDLE, FILETIME*, FILETIME*,
                                         FILETIME*, FILETIME* ))
        GetProcAddress( hmodule, _T("GetThreadTimes") );

    if( OurGetThreadTimes &&
721
        OurGetThreadTimes( hThread,
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
                           &create_ft, &exit_ft, &kernel_ft, &user_ft ) )
    {
        real_time =
          ((((int64_t)exit_ft.dwHighDateTime)<<32)| exit_ft.dwLowDateTime) -
          ((((int64_t)create_ft.dwHighDateTime)<<32)| create_ft.dwLowDateTime);
        real_time /= 10;

        kernel_time =
          ((((int64_t)kernel_ft.dwHighDateTime)<<32)|
           kernel_ft.dwLowDateTime) / 10;

        user_time =
          ((((int64_t)user_ft.dwHighDateTime)<<32)|
           user_ft.dwLowDateTime) / 10;

        msg_Dbg( p_this, "thread times: "
Rémi Denis-Courmont's avatar
Rémi Denis-Courmont committed
738
                 "real %"PRId64"m%fs, kernel %"PRId64"m%fs, user %"PRId64"m%fs",
739
740
741
742
743
744
745
                 real_time/60/1000000,
                 (double)((real_time%(60*1000000))/1000000.0),
                 kernel_time/60/1000000,
                 (double)((kernel_time%(60*1000000))/1000000.0),
                 user_time/60/1000000,
                 (double)((user_time%(60*1000000))/1000000.0) );
    }
746
    CloseHandle( hThread );
Rémi Denis-Courmont's avatar
Rémi Denis-Courmont committed
747
error:
748

749
#elif defined( HAVE_KERNEL_SCHEDULER_H )
750
    int32_t exit_value;
751
    i_ret = (B_OK == wait_for_thread( p_priv->thread_id, &exit_value ));
752

753
754
755
756
#endif

    if( i_ret )
    {
757
        errno = i_ret;
Rémi Denis-Courmont's avatar
Rémi Denis-Courmont committed
758
759
        msg_Err( p_this, "thread_join(%lu) failed at %s:%d (%m)",
                         (unsigned long)p_priv->thread_id, psz_file, i_line );
760
761
    }
    else
Rémi Denis-Courmont's avatar
Rémi Denis-Courmont committed
762
763
        msg_Dbg( p_this, "thread %lu joined (%s:%d)",
                         (unsigned long)p_priv->thread_id, psz_file, i_line );
764

765
    p_priv->b_thread = false;
766
}