]> git.sesse.net Git - vlc/blob - src/misc/threads.c
Win32: fix warning
[vlc] / src / misc / threads.c
1 /*****************************************************************************
2  * threads.c : threads implementation for the VideoLAN client
3  *****************************************************************************
4  * Copyright (C) 1999-2008 the VideoLAN team
5  * $Id$
6  *
7  * Authors: Jean-Marc Dressler <polux@via.ecp.fr>
8  *          Samuel Hocevar <sam@zoy.org>
9  *          Gildas Bazin <gbazin@netcourrier.com>
10  *          Clément Sténac
11  *
12  * This program is free software; you can redistribute it and/or modify
13  * it under the terms of the GNU General Public License as published by
14  * the Free Software Foundation; either version 2 of the License, or
15  * (at your option) any later version.
16  *
17  * This program is distributed in the hope that it will be useful,
18  * but WITHOUT ANY WARRANTY; without even the implied warranty of
19  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
20  * GNU General Public License for more details.
21  *
22  * You should have received a copy of the GNU General Public License
23  * along with this program; if not, write to the Free Software
24  * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston MA 02110-1301, USA.
25  *****************************************************************************/
26
27 #ifdef HAVE_CONFIG_H
28 # include "config.h"
29 #endif
30
31 #include <vlc_common.h>
32
33 #include "libvlc.h"
34 #include <stdarg.h>
35 #include <assert.h>
36 #ifdef HAVE_UNISTD_H
37 # include <unistd.h>
38 #endif
39 #include <signal.h>
40
41 #define VLC_THREADS_UNINITIALIZED  0
42 #define VLC_THREADS_PENDING        1
43 #define VLC_THREADS_ERROR          2
44 #define VLC_THREADS_READY          3
45
46 /*****************************************************************************
47  * Global mutex for lazy initialization of the threads system
48  *****************************************************************************/
49 static volatile unsigned i_initializations = 0;
50
51 #if defined( LIBVLC_USE_PTHREAD )
52 # include <sched.h>
53
54 static pthread_mutex_t once_mutex = PTHREAD_MUTEX_INITIALIZER;
55 #endif
56
57 /**
58  * Global process-wide VLC object.
59  * Contains inter-instance data, such as the module cache and global mutexes.
60  */
61 static libvlc_global_data_t *p_root;
62
63 libvlc_global_data_t *vlc_global( void )
64 {
65     assert( i_initializations > 0 );
66     return p_root;
67 }
68
69 #ifdef HAVE_EXECINFO_H
70 # include <execinfo.h>
71 #endif
72
73 /**
74  * Print a backtrace to the standard error for debugging purpose.
75  */
76 void vlc_trace (const char *fn, const char *file, unsigned line)
77 {
78      fprintf (stderr, "at %s:%u in %s\n", file, line, fn);
79      fflush (stderr); /* needed before switch to low-level I/O */
80 #ifdef HAVE_BACKTRACE
81      void *stack[20];
82      int len = backtrace (stack, sizeof (stack) / sizeof (stack[0]));
83      backtrace_symbols_fd (stack, len, 2);
84 #endif
85 #ifndef WIN32
86      fsync (2);
87 #endif
88 }
89
90 static inline unsigned long vlc_threadid (void)
91 {
92 #if defined(LIBVLC_USE_PTHREAD)
93      union { pthread_t th; unsigned long int i; } v = { };
94      v.th = pthread_self ();
95      return v.i;
96
97 #elif defined (WIN32)
98      return GetCurrentThreadId ();
99
100 #else
101      return 0;
102
103 #endif
104 }
105
106 /*****************************************************************************
107  * vlc_thread_fatal: Report an error from the threading layer
108  *****************************************************************************
109  * This is mostly meant for debugging.
110  *****************************************************************************/
111 void vlc_thread_fatal (const char *action, int error, const char *function,
112                         const char *file, unsigned line)
113 {
114     fprintf (stderr, "LibVLC fatal error %s (%d) in thread %lu ",
115              action, error, vlc_threadid ());
116     vlc_trace (function, file, line);
117
118     /* Sometimes strerror_r() crashes too, so make sure we print an error
119      * message before we invoke it */
120 #ifdef __GLIBC__
121     /* Avoid the strerror_r() prototype brain damage in glibc */
122     errno = error;
123     fprintf (stderr, " Error message: %m at:\n");
124 #else
125     char buf[1000];
126     const char *msg;
127
128     switch (strerror_r (error, buf, sizeof (buf)))
129     {
130         case 0:
131             msg = buf;
132             break;
133         case ERANGE: /* should never happen */
134             msg = "unknwon (too big to display)";
135             break;
136         default:
137             msg = "unknown (invalid error number)";
138             break;
139     }
140     fprintf (stderr, " Error message: %s\n", msg);
141 #endif
142     fflush (stderr);
143
144     abort ();
145 }
146
147 /**
148  * Per-thread cancellation data
149  */
150 #ifndef LIBVLC_USE_PTHREAD_CANCEL
151 typedef struct vlc_cancel_t
152 {
153     vlc_cleanup_t *cleaners;
154     bool           killable;
155     bool           killed;
156 } vlc_cancel_t;
157
158 # define VLC_CANCEL_INIT { NULL, false, true }
159 #endif
160
161 /*****************************************************************************
162  * vlc_threads_init: initialize threads system
163  *****************************************************************************
164  * This function requires lazy initialization of a global lock in order to
165  * keep the library really thread-safe. Some architectures don't support this
166  * and thus do not guarantee the complete reentrancy.
167  *****************************************************************************/
168 int vlc_threads_init( void )
169 {
170     int i_ret = VLC_SUCCESS;
171
172     /* If we have lazy mutex initialization, use it. Otherwise, we just
173      * hope nothing wrong happens. */
174 #if defined( LIBVLC_USE_PTHREAD )
175     pthread_mutex_lock( &once_mutex );
176 #endif
177
178     if( i_initializations == 0 )
179     {
180         p_root = vlc_custom_create( (vlc_object_t *)NULL, sizeof( *p_root ),
181                                     VLC_OBJECT_GENERIC, "root" );
182         if( p_root == NULL )
183         {
184             i_ret = VLC_ENOMEM;
185             goto out;
186         }
187
188         /* We should be safe now. Do all the initialization stuff we want. */
189 #ifndef LIBVLC_USE_PTHREAD_CANCEL
190         vlc_threadvar_create( &cancel_key, free );
191 #endif
192     }
193     i_initializations++;
194
195 out:
196     /* If we have lazy mutex initialization support, unlock the mutex.
197      * Otherwize, we are screwed. */
198 #if defined( LIBVLC_USE_PTHREAD )
199     pthread_mutex_unlock( &once_mutex );
200 #endif
201
202     return i_ret;
203 }
204
205 /*****************************************************************************
206  * vlc_threads_end: stop threads system
207  *****************************************************************************
208  * FIXME: This function is far from being threadsafe.
209  *****************************************************************************/
210 void vlc_threads_end( void )
211 {
212 #if defined( LIBVLC_USE_PTHREAD )
213     pthread_mutex_lock( &once_mutex );
214 #endif
215
216     assert( i_initializations > 0 );
217
218     if( i_initializations == 1 )
219     {
220         vlc_object_release( p_root );
221 #ifndef LIBVLC_USE_PTHREAD
222         vlc_threadvar_delete( &cancel_key );
223 #endif
224     }
225     i_initializations--;
226
227 #if defined( LIBVLC_USE_PTHREAD )
228     pthread_mutex_unlock( &once_mutex );
229 #endif
230 }
231
232 #if defined (__GLIBC__) && (__GLIBC_MINOR__ < 6)
233 /* This is not prototyped under glibc, though it exists. */
234 int pthread_mutexattr_setkind_np( pthread_mutexattr_t *attr, int kind );
235 #endif
236
237 /*****************************************************************************
238  * vlc_mutex_init: initialize a mutex
239  *****************************************************************************/
240 int vlc_mutex_init( vlc_mutex_t *p_mutex )
241 {
242 #if defined( LIBVLC_USE_PTHREAD )
243     pthread_mutexattr_t attr;
244     int                 i_result;
245
246     pthread_mutexattr_init( &attr );
247
248 # ifndef NDEBUG
249     /* Create error-checking mutex to detect problems more easily. */
250 #  if defined (__GLIBC__) && (__GLIBC_MINOR__ < 6)
251     pthread_mutexattr_setkind_np( &attr, PTHREAD_MUTEX_ERRORCHECK_NP );
252 #  else
253     pthread_mutexattr_settype( &attr, PTHREAD_MUTEX_ERRORCHECK );
254 #  endif
255 # endif
256     i_result = pthread_mutex_init( p_mutex, &attr );
257     pthread_mutexattr_destroy( &attr );
258     return i_result;
259 #elif defined( UNDER_CE )
260     InitializeCriticalSection( &p_mutex->csection );
261     return 0;
262
263 #elif defined( WIN32 )
264     *p_mutex = CreateMutex( 0, FALSE, 0 );
265     return (*p_mutex != NULL) ? 0 : ENOMEM;
266
267 #elif defined( HAVE_KERNEL_SCHEDULER_H )
268     /* check the arguments and whether it's already been initialized */
269     if( p_mutex == NULL )
270     {
271         return B_BAD_VALUE;
272     }
273
274     if( p_mutex->init == 9999 )
275     {
276         return EALREADY;
277     }
278
279     p_mutex->lock = create_sem( 1, "BeMutex" );
280     if( p_mutex->lock < B_NO_ERROR )
281     {
282         return( -1 );
283     }
284
285     p_mutex->init = 9999;
286     return B_OK;
287
288 #endif
289 }
290
291 /*****************************************************************************
292  * vlc_mutex_init: initialize a recursive mutex (Do not use)
293  *****************************************************************************/
294 int vlc_mutex_init_recursive( vlc_mutex_t *p_mutex )
295 {
296 #if defined( LIBVLC_USE_PTHREAD )
297     pthread_mutexattr_t attr;
298     int                 i_result;
299
300     pthread_mutexattr_init( &attr );
301 #  if defined (__GLIBC__) && (__GLIBC_MINOR__ < 6)
302     pthread_mutexattr_setkind_np( &attr, PTHREAD_MUTEX_RECURSIVE_NP );
303 #  else
304     pthread_mutexattr_settype( &attr, PTHREAD_MUTEX_RECURSIVE );
305 #  endif
306     i_result = pthread_mutex_init( p_mutex, &attr );
307     pthread_mutexattr_destroy( &attr );
308     return( i_result );
309 #elif defined( WIN32 )
310     /* Create mutex returns a recursive mutex */
311     *p_mutex = CreateMutex( 0, FALSE, 0 );
312     return (*p_mutex != NULL) ? 0 : ENOMEM;
313 #else
314 # error Unimplemented!
315 #endif
316 }
317
318
319 /*****************************************************************************
320  * vlc_mutex_destroy: destroy a mutex, inner version
321  *****************************************************************************/
322 void __vlc_mutex_destroy( const char * psz_file, int i_line, vlc_mutex_t *p_mutex )
323 {
324 #if defined( LIBVLC_USE_PTHREAD )
325     int val = pthread_mutex_destroy( p_mutex );
326     VLC_THREAD_ASSERT ("destroying mutex");
327
328 #elif defined( UNDER_CE )
329     VLC_UNUSED( psz_file); VLC_UNUSED( i_line );
330
331     DeleteCriticalSection( &p_mutex->csection );
332
333 #elif defined( WIN32 )
334     VLC_UNUSED( psz_file); VLC_UNUSED( i_line );
335
336     CloseHandle( *p_mutex );
337
338 #elif defined( HAVE_KERNEL_SCHEDULER_H )
339     if( p_mutex->init == 9999 )
340         delete_sem( p_mutex->lock );
341
342     p_mutex->init = 0;
343
344 #endif
345 }
346
347 /*****************************************************************************
348  * vlc_cond_init: initialize a condition
349  *****************************************************************************/
350 int __vlc_cond_init( vlc_cond_t *p_condvar )
351 {
352 #if defined( LIBVLC_USE_PTHREAD )
353     pthread_condattr_t attr;
354     int ret;
355
356     ret = pthread_condattr_init (&attr);
357     if (ret)
358         return ret;
359
360 # if !defined (_POSIX_CLOCK_SELECTION)
361    /* Fairly outdated POSIX support (that was defined in 2001) */
362 #  define _POSIX_CLOCK_SELECTION (-1)
363 # endif
364 # if (_POSIX_CLOCK_SELECTION >= 0)
365     /* NOTE: This must be the same clock as the one in mtime.c */
366     pthread_condattr_setclock (&attr, CLOCK_MONOTONIC);
367 # endif
368
369     ret = pthread_cond_init (p_condvar, &attr);
370     pthread_condattr_destroy (&attr);
371     return ret;
372
373 #elif defined( UNDER_CE ) || defined( WIN32 )
374     /* Create an auto-reset event. */
375     *p_condvar = CreateEvent( NULL,   /* no security */
376                               FALSE,  /* auto-reset event */
377                               FALSE,  /* start non-signaled */
378                               NULL ); /* unnamed */
379     return *p_condvar ? 0 : ENOMEM;
380
381 #elif defined( HAVE_KERNEL_SCHEDULER_H )
382     if( !p_condvar )
383     {
384         return B_BAD_VALUE;
385     }
386
387     if( p_condvar->init == 9999 )
388     {
389         return EALREADY;
390     }
391
392     p_condvar->thread = -1;
393     p_condvar->init = 9999;
394     return 0;
395
396 #endif
397 }
398
399 /*****************************************************************************
400  * vlc_cond_destroy: destroy a condition, inner version
401  *****************************************************************************/
402 void __vlc_cond_destroy( const char * psz_file, int i_line, vlc_cond_t *p_condvar )
403 {
404 #if defined( LIBVLC_USE_PTHREAD )
405     int val = pthread_cond_destroy( p_condvar );
406     VLC_THREAD_ASSERT ("destroying condition");
407
408 #elif defined( UNDER_CE ) || defined( WIN32 )
409     VLC_UNUSED( psz_file); VLC_UNUSED( i_line );
410
411     CloseHandle( *p_condvar );
412
413 #elif defined( HAVE_KERNEL_SCHEDULER_H )
414     p_condvar->init = 0;
415
416 #endif
417 }
418
419 /*****************************************************************************
420  * vlc_tls_create: create a thread-local variable
421  *****************************************************************************/
422 int vlc_threadvar_create( vlc_threadvar_t *p_tls, void (*destr) (void *) )
423 {
424     int i_ret;
425
426 #if defined( LIBVLC_USE_PTHREAD )
427     i_ret =  pthread_key_create( p_tls, destr );
428 #elif defined( UNDER_CE )
429     i_ret = ENOSYS;
430 #elif defined( WIN32 )
431     /* FIXME: remember/use the destr() callback and stop leaking whatever */
432     *p_tls = TlsAlloc();
433     i_ret = (*p_tls == TLS_OUT_OF_INDEXES) ? EAGAIN : 0;
434 #else
435 # error Unimplemented!
436 #endif
437     return i_ret;
438 }
439
440 void vlc_threadvar_delete (vlc_threadvar_t *p_tls)
441 {
442 #if defined( LIBVLC_USE_PTHREAD )
443     pthread_key_delete (*p_tls);
444 #elif defined( UNDER_CE )
445 #elif defined( WIN32 )
446     TlsFree (*p_tls);
447 #else
448 # error Unimplemented!
449 #endif
450 }
451
452 #if defined (LIBVLC_USE_PTHREAD)
453 #elif defined (WIN32)
454 static unsigned __stdcall vlc_entry (void *data)
455 {
456     vlc_cancel_t cancel_data = VLC_CANCEL_INIT;
457     vlc_thread_t self = data;
458
459     vlc_threadvar_set (&cancel_key, &cancel_data);
460     self->data = self->entry (self->data);
461     return 0;
462 }
463 #endif
464
465 /**
466  * Creates and starts new thread.
467  *
468  * @param p_handle [OUT] pointer to write the handle of the created thread to
469  * @param entry entry point for the thread
470  * @param data data parameter given to the entry point
471  * @param priority thread priority value
472  * @return 0 on success, a standard error code on error.
473  */
474 int vlc_clone (vlc_thread_t *p_handle, void * (*entry) (void *), void *data,
475                int priority)
476 {
477     int ret;
478
479 #if defined( LIBVLC_USE_PTHREAD )
480     pthread_attr_t attr;
481     pthread_attr_init (&attr);
482
483     /* Block the signals that signals interface plugin handles.
484      * If the LibVLC caller wants to handle some signals by itself, it should
485      * block these before whenever invoking LibVLC. And it must obviously not
486      * start the VLC signals interface plugin.
487      *
488      * LibVLC will normally ignore any interruption caused by an asynchronous
489      * signal during a system call. But there may well be some buggy cases
490      * where it fails to handle EINTR (bug reports welcome). Some underlying
491      * libraries might also not handle EINTR properly.
492      */
493     sigset_t oldset;
494     {
495         sigset_t set;
496         sigemptyset (&set);
497         sigdelset (&set, SIGHUP);
498         sigaddset (&set, SIGINT);
499         sigaddset (&set, SIGQUIT);
500         sigaddset (&set, SIGTERM);
501
502         sigaddset (&set, SIGPIPE); /* We don't want this one, really! */
503         pthread_sigmask (SIG_BLOCK, &set, &oldset);
504     }
505     {
506         struct sched_param sp = { .sched_priority = priority, };
507         int policy;
508
509         if (sp.sched_priority <= 0)
510             sp.sched_priority += sched_get_priority_max (policy = SCHED_OTHER);
511         else
512             sp.sched_priority += sched_get_priority_min (policy = SCHED_RR);
513
514         pthread_attr_setschedpolicy (&attr, policy);
515         pthread_attr_setschedparam (&attr, &sp);
516     }
517
518     ret = pthread_create (p_handle, &attr, entry, data);
519     pthread_sigmask (SIG_SETMASK, &oldset, NULL);
520     pthread_attr_destroy (&attr);
521
522 #elif defined( WIN32 ) || defined( UNDER_CE )
523     /* When using the MSVCRT C library you have to use the _beginthreadex
524      * function instead of CreateThread, otherwise you'll end up with
525      * memory leaks and the signal functions not working (see Microsoft
526      * Knowledge Base, article 104641) */
527     HANDLE hThread;
528     vlc_thread_t th = malloc (sizeof (*th));
529
530     if (th == NULL)
531         return ENOMEM;
532
533     th->data = data;
534     th->entry = entry;
535 #if defined( UNDER_CE )
536     hThread = CreateThread (NULL, 0, vlc_entry, th, CREATE_SUSPENDED, NULL);
537 #else
538     hThread = (HANDLE)(uintptr_t)
539         _beginthreadex (NULL, 0, vlc_entry, th, CREATE_SUSPENDED, NULL);
540 #endif
541
542     if (hThread)
543     {
544         /* Thread closes the handle when exiting, duplicate it here
545          * to be on the safe side when joining. */
546         if (!DuplicateHandle (GetCurrentProcess (), hThread,
547                               GetCurrentProcess (), &th->handle, 0, FALSE,
548                               DUPLICATE_SAME_ACCESS))
549         {
550             CloseHandle (hThread);
551             free (th);
552             return ENOMEM;
553         }
554
555         ResumeThread (hThread);
556         if (priority)
557             SetThreadPriority (hThread, priority);
558
559         ret = 0;
560         *p_handle = th;
561     }
562     else
563     {
564         ret = errno;
565         free (th);
566     }
567
568 #elif defined( HAVE_KERNEL_SCHEDULER_H )
569     *p_handle = spawn_thread( entry, psz_name, priority, data );
570     ret = resume_thread( *p_handle );
571
572 #endif
573     return ret;
574 }
575
576 #if defined (WIN32)
577 /* APC procedure for thread cancellation */
578 static void CALLBACK vlc_cancel_self (ULONG_PTR dummy)
579 {
580     (void)dummy;
581     vlc_control_cancel (VLC_DO_CANCEL);
582 }
583 #endif
584
585 /**
586  * Marks a thread as cancelled. Next time the target thread reaches a
587  * cancellation point (while not having disabled cancellation), it will
588  * run its cancellation cleanup handler, the thread variable destructors, and
589  * terminate. vlc_join() must be used afterward regardless of a thread being
590  * cancelled or not.
591  */
592 void vlc_cancel (vlc_thread_t thread_id)
593 {
594 #if defined (LIBVLC_USE_PTHREAD_CANCEL)
595     pthread_cancel (thread_id);
596 #elif defined (WIN32)
597     QueueUserAPC (vlc_cancel_self, thread_id->handle, 0);
598 #else
599 #   warning vlc_cancel is not implemented!
600 #endif
601 }
602
603 /**
604  * Waits for a thread to complete (if needed), and destroys it.
605  * This is a cancellation point; in case of cancellation, the join does _not_
606  * occur.
607  *
608  * @param handle thread handle
609  * @param p_result [OUT] pointer to write the thread return value or NULL
610  * @return 0 on success, a standard error code otherwise.
611  */
612 void vlc_join (vlc_thread_t handle, void **result)
613 {
614 #if defined( LIBVLC_USE_PTHREAD )
615     int val = pthread_join (handle, result);
616     if (val)
617         vlc_thread_fatal ("joining thread", val, __func__, __FILE__, __LINE__);
618
619 #elif defined( UNDER_CE ) || defined( WIN32 )
620     do
621         vlc_testcancel ();
622     while (WaitForSingleObjectEx (handle->handle, INFINITE, TRUE)
623                                                         == WAIT_IO_COMPLETION);
624
625     CloseHandle (handle->handle);
626     if (result)
627         *result = handle->data;
628     free (handle);
629
630 #elif defined( HAVE_KERNEL_SCHEDULER_H )
631     int32_t exit_value;
632     int val = (B_OK == wait_for_thread( p_priv->thread_id, &exit_value ));
633     if( !val && result )
634         *result = (void *)exit_value;
635
636 #endif
637 }
638
639
640 struct vlc_thread_boot
641 {
642     void * (*entry) (vlc_object_t *);
643     vlc_object_t *object;
644 };
645
646 static void *thread_entry (void *data)
647 {
648     vlc_object_t *obj = ((struct vlc_thread_boot *)data)->object;
649     void *(*func) (vlc_object_t *) = ((struct vlc_thread_boot *)data)->entry;
650
651     free (data);
652     msg_Dbg (obj, "thread started");
653     func (obj);
654     msg_Dbg (obj, "thread ended");
655
656     return NULL;
657 }
658
659 /*****************************************************************************
660  * vlc_thread_create: create a thread, inner version
661  *****************************************************************************
662  * Note that i_priority is only taken into account on platforms supporting
663  * userland real-time priority threads.
664  *****************************************************************************/
665 int __vlc_thread_create( vlc_object_t *p_this, const char * psz_file, int i_line,
666                          const char *psz_name, void * ( *func ) ( vlc_object_t * ),
667                          int i_priority, bool b_wait )
668 {
669     int i_ret;
670     vlc_object_internals_t *p_priv = vlc_internals( p_this );
671
672     struct vlc_thread_boot *boot = malloc (sizeof (*boot));
673     if (boot == NULL)
674         return errno;
675     boot->entry = func;
676     boot->object = p_this;
677
678     vlc_object_lock( p_this );
679
680     /* Make sure we don't re-create a thread if the object has already one */
681     assert( !p_priv->b_thread );
682
683 #if defined( LIBVLC_USE_PTHREAD )
684 #ifndef __APPLE__
685     if( config_GetInt( p_this, "rt-priority" ) > 0 )
686 #endif
687     {
688         /* Hack to avoid error msg */
689         if( config_GetType( p_this, "rt-offset" ) )
690             i_priority += config_GetInt( p_this, "rt-offset" );
691     }
692 #endif
693
694     i_ret = vlc_clone( &p_priv->thread_id, thread_entry, boot, i_priority );
695     if( i_ret == 0 )
696     {
697         if( b_wait )
698         {
699             msg_Dbg( p_this, "waiting for thread initialization" );
700             vlc_object_wait( p_this );
701         }
702
703         p_priv->b_thread = true;
704         msg_Dbg( p_this, "thread %lu (%s) created at priority %d (%s:%d)",
705                  (unsigned long)p_priv->thread_id, psz_name, i_priority,
706                  psz_file, i_line );
707     }
708     else
709     {
710         errno = i_ret;
711         msg_Err( p_this, "%s thread could not be created at %s:%d (%m)",
712                          psz_name, psz_file, i_line );
713     }
714
715     vlc_object_unlock( p_this );
716     return i_ret;
717 }
718
719 /*****************************************************************************
720  * vlc_thread_set_priority: set the priority of the current thread when we
721  * couldn't set it in vlc_thread_create (for instance for the main thread)
722  *****************************************************************************/
723 int __vlc_thread_set_priority( vlc_object_t *p_this, const char * psz_file,
724                                int i_line, int i_priority )
725 {
726     vlc_object_internals_t *p_priv = vlc_internals( p_this );
727
728     if( !p_priv->b_thread )
729     {
730         msg_Err( p_this, "couldn't set priority of non-existent thread" );
731         return ESRCH;
732     }
733
734 #if defined( LIBVLC_USE_PTHREAD )
735 # ifndef __APPLE__
736     if( config_GetInt( p_this, "rt-priority" ) > 0 )
737 # endif
738     {
739         int i_error, i_policy;
740         struct sched_param param;
741
742         memset( &param, 0, sizeof(struct sched_param) );
743         if( config_GetType( p_this, "rt-offset" ) )
744             i_priority += config_GetInt( p_this, "rt-offset" );
745         if( i_priority <= 0 )
746         {
747             param.sched_priority = (-1) * i_priority;
748             i_policy = SCHED_OTHER;
749         }
750         else
751         {
752             param.sched_priority = i_priority;
753             i_policy = SCHED_RR;
754         }
755         if( (i_error = pthread_setschedparam( p_priv->thread_id,
756                                               i_policy, &param )) )
757         {
758             errno = i_error;
759             msg_Warn( p_this, "couldn't set thread priority (%s:%d): %m",
760                       psz_file, i_line );
761             i_priority = 0;
762         }
763     }
764
765 #elif defined( WIN32 ) || defined( UNDER_CE )
766     VLC_UNUSED( psz_file); VLC_UNUSED( i_line );
767
768     if( !SetThreadPriority(p_priv->thread_id->handle, i_priority) )
769     {
770         msg_Warn( p_this, "couldn't set a faster priority" );
771         return 1;
772     }
773
774 #endif
775
776     return 0;
777 }
778
779 /*****************************************************************************
780  * vlc_thread_join: wait until a thread exits, inner version
781  *****************************************************************************/
782 void __vlc_thread_join( vlc_object_t *p_this )
783 {
784     vlc_object_internals_t *p_priv = vlc_internals( p_this );
785
786 #if defined( LIBVLC_USE_PTHREAD )
787     vlc_join (p_priv->thread_id, NULL);
788
789 #elif defined( UNDER_CE ) || defined( WIN32 )
790     HANDLE hThread;
791     FILETIME create_ft, exit_ft, kernel_ft, user_ft;
792     int64_t real_time, kernel_time, user_time;
793
794     if( ! DuplicateHandle(GetCurrentProcess(),
795             p_priv->thread_id->handle,
796             GetCurrentProcess(),
797             &hThread,
798             0,
799             FALSE,
800             DUPLICATE_SAME_ACCESS) )
801     {
802         p_priv->b_thread = false;
803         return; /* We have a problem! */
804     }
805
806     vlc_join( p_priv->thread_id, NULL );
807
808     if( GetThreadTimes( hThread, &create_ft, &exit_ft, &kernel_ft, &user_ft ) )
809     {
810         real_time =
811           ((((int64_t)exit_ft.dwHighDateTime)<<32)| exit_ft.dwLowDateTime) -
812           ((((int64_t)create_ft.dwHighDateTime)<<32)| create_ft.dwLowDateTime);
813         real_time /= 10;
814
815         kernel_time =
816           ((((int64_t)kernel_ft.dwHighDateTime)<<32)|
817            kernel_ft.dwLowDateTime) / 10;
818
819         user_time =
820           ((((int64_t)user_ft.dwHighDateTime)<<32)|
821            user_ft.dwLowDateTime) / 10;
822
823         msg_Dbg( p_this, "thread times: "
824                  "real %"PRId64"m%fs, kernel %"PRId64"m%fs, user %"PRId64"m%fs",
825                  real_time/60/1000000,
826                  (double)((real_time%(60*1000000))/1000000.0),
827                  kernel_time/60/1000000,
828                  (double)((kernel_time%(60*1000000))/1000000.0),
829                  user_time/60/1000000,
830                  (double)((user_time%(60*1000000))/1000000.0) );
831     }
832     CloseHandle( hThread );
833
834 #else
835     vlc_join( p_priv->thread_id, NULL );
836
837 #endif
838
839     p_priv->b_thread = false;
840 }
841
842 void vlc_thread_cancel (vlc_object_t *obj)
843 {
844     vlc_object_internals_t *priv = vlc_internals (obj);
845
846     if (priv->b_thread)
847         vlc_cancel (priv->thread_id);
848 }
849
850 void vlc_control_cancel (int cmd, ...)
851 {
852     /* NOTE: This function only modifies thread-specific data, so there is no
853      * need to lock anything. */
854 #ifdef LIBVLC_USE_PTHREAD_CANCEL
855     (void) cmd;
856     assert (0);
857 #else
858     va_list ap;
859
860     va_start (ap, cmd);
861
862     vlc_cancel_t *nfo = vlc_threadvar_get (&cancel_key);
863     if (nfo == NULL)
864     {
865 #ifdef WIN32
866         /* Main thread - cannot be cancelled anyway */
867         return;
868 #else
869         nfo = malloc (sizeof (*nfo));
870         if (nfo == NULL)
871             return; /* Uho! Expect problems! */
872         *nfo = VLC_CANCEL_INIT;
873         vlc_threadvar_set (&cancel_key, nfo);
874 #endif
875     }
876
877     switch (cmd)
878     {
879         case VLC_SAVE_CANCEL:
880         {
881             int *p_state = va_arg (ap, int *);
882             *p_state = nfo->killable;
883             nfo->killable = false;
884             break;
885         }
886
887         case VLC_RESTORE_CANCEL:
888         {
889             int state = va_arg (ap, int);
890             nfo->killable = state != 0;
891             break;
892         }
893
894         case VLC_TEST_CANCEL:
895             if (nfo->killable && nfo->killed)
896             {
897                 for (vlc_cleanup_t *p = nfo->cleaners; p != NULL; p = p->next)
898                      p->proc (p->data);
899                 free (nfo);
900 #if defined (LIBVLC_USE_PTHREAD)
901                 pthread_exit (PTHREAD_CANCELLED);
902 #elif defined (WIN32)
903                 _endthread ();
904 #else
905 # error Not implemented!
906 #endif
907             }
908             break;
909
910         case VLC_DO_CANCEL:
911             nfo->killed = true;
912             break;
913
914         case VLC_CLEANUP_PUSH:
915         {
916             /* cleaner is a pointer to the caller stack, no need to allocate
917              * and copy anything. As a nice side effect, this cannot fail. */
918             vlc_cleanup_t *cleaner = va_arg (ap, vlc_cleanup_t *);
919             cleaner->next = nfo->cleaners;
920             nfo->cleaners = cleaner;
921             break;
922         }
923
924         case VLC_CLEANUP_POP:
925         {
926             nfo->cleaners = nfo->cleaners->next;
927             break;
928         }
929     }
930     va_end (ap);
931 #endif
932 }