]> git.sesse.net Git - vlc/blobdiff - src/misc/threads.c
* vlc_threads.h: lower a priority that made sound choppy on not-so-fast
[vlc] / src / misc / threads.c
index 3b4b5f5b1575e2f23adf5b5d8e38f8a5cc9a940c..e3fefd993ecaf7f9874924e314b90b7cf0a275db 100644 (file)
@@ -2,7 +2,7 @@
  * threads.c : threads implementation for the VideoLAN client
  *****************************************************************************
  * Copyright (C) 1999, 2000, 2001, 2002 VideoLAN
- * $Id: threads.c,v 1.19 2002/10/03 17:01:58 gbazin Exp $
+ * $Id: threads.c,v 1.35 2003/01/24 06:31:56 titer Exp $
  *
  * Authors: Jean-Marc Dressler <polux@via.ecp.fr>
  *          Samuel Hocevar <sam@zoy.org>
@@ -12,7 +12,7 @@
  * 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
@@ -25,6 +25,8 @@
 
 #include <vlc/vlc.h>
 
+#include <stdlib.h>
+
 #define VLC_THREADS_UNINITIALIZED  0
 #define VLC_THREADS_PENDING        1
 #define VLC_THREADS_ERROR          2
@@ -37,6 +39,7 @@ static volatile int i_initializations = 0;
 
 #if defined( PTH_INIT_IN_PTH_H )
 #elif defined( ST_INIT_IN_ST_H )
+#elif defined( UNDER_CE )
 #elif defined( WIN32 )
 #elif defined( PTHREAD_COND_T_IN_PTHREAD_H )
     static pthread_mutex_t once_mutex = PTHREAD_MUTEX_INITIALIZER;
@@ -44,6 +47,19 @@ static volatile int i_initializations = 0;
 #elif defined( HAVE_KERNEL_SCHEDULER_H )
 #endif
 
+/*****************************************************************************
+ * 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;
+};
+
 /*****************************************************************************
  * vlc_threads_init: initialize threads system
  *****************************************************************************
@@ -62,6 +78,7 @@ int __vlc_threads_init( vlc_object_t *p_this )
      * hope nothing wrong happens. */
 #if defined( PTH_INIT_IN_PTH_H )
 #elif defined( ST_INIT_IN_ST_H )
+#elif defined( UNDER_CE )
 #elif defined( WIN32 )
     HINSTANCE hInstLib;
 #elif defined( PTHREAD_COND_T_IN_PTHREAD_H )
@@ -84,6 +101,9 @@ int __vlc_threads_init( vlc_object_t *p_this )
 #elif defined( ST_INIT_IN_ST_H )
         i_ret = st_init();
 
+#elif defined( UNDER_CE )
+        /* Nothing to initialize */
+
 #elif defined( WIN32 )
         /* Dynamically get the address of SignalObjectAndWait */
         if( GetVersion() < 0x80000000 )
@@ -110,8 +130,6 @@ int __vlc_threads_init( vlc_object_t *p_this )
 #elif defined( HAVE_KERNEL_SCHEDULER_H )
 #endif
 
-        vlc_mutex_init( p_libvlc, &p_libvlc->global_lock );
-
         if( i_ret )
         {
             i_status = VLC_THREADS_ERROR;
@@ -134,6 +152,8 @@ int __vlc_threads_init( vlc_object_t *p_this )
     while( i_status == VLC_THREADS_PENDING ) msleep( THREAD_SLEEP );
 #elif defined( ST_INIT_IN_ST_H )
     while( i_status == VLC_THREADS_PENDING ) msleep( THREAD_SLEEP );
+#elif defined( UNDER_CE )
+    while( i_status == VLC_THREADS_PENDING ) msleep( THREAD_SLEEP );
 #elif defined( WIN32 )
     while( i_status == VLC_THREADS_PENDING ) msleep( THREAD_SLEEP );
 #elif defined( PTHREAD_COND_T_IN_PTHREAD_H )
@@ -170,6 +190,9 @@ int __vlc_threads_end( vlc_object_t *p_this )
 #elif defined( ST_INIT_IN_ST_H )
     i_initializations--;
 
+#elif defined( UNDER_CE )
+    i_initializations--;
+
 #elif defined( WIN32 )
     i_initializations--;
 
@@ -195,7 +218,7 @@ int __vlc_threads_end( vlc_object_t *p_this )
 /* Wrapper function for profiling */
 static void *      vlc_thread_wrapper ( void *p_wrapper );
 
-#   ifdef WIN32
+#   if defined( WIN32 ) && !defined( UNDER_CE )
 
 #       define ITIMER_REAL 1
 #       define ITIMER_PROF 2
@@ -204,24 +227,24 @@ struct itimerval
 {
     struct timeval it_value;
     struct timeval it_interval;
-};  
+};
 
 int setitimer(int kind, const struct itimerval* itnew, struct itimerval* itold);
-#   endif /* WIN32 */
+#   endif /* WIN32 && !UNDER_CE */
 
 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 */
@@ -240,12 +263,17 @@ int __vlc_mutex_init( vlc_object_t *p_this, vlc_mutex_t *p_mutex )
     p_mutex->mutex = st_mutex_new();
     return ( p_mutex->mutex == NULL ) ? errno : 0;
 
+#elif defined( UNDER_CE )
+    InitializeCriticalSection( &p_mutex->csection );
+    return 0;
+
 #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 */
-    if( p_this->p_vlc->SignalObjectAndWait && !p_this->p_vlc->b_fast_mutex )
+    if( p_this->p_libvlc->SignalObjectAndWait &&
+        !p_this->p_libvlc->b_fast_mutex )
     {
         /* We are running on NT/2K/XP, we can use SignalObjectAndWait */
         p_mutex->mutex = CreateMutex( 0, FALSE, 0 );
@@ -318,6 +346,10 @@ int __vlc_mutex_destroy( char * psz_file, int i_line, vlc_mutex_t *p_mutex )
 #elif defined( ST_INIT_IN_ST_H )
     i_result = st_mutex_destroy( p_mutex->mutex );
 
+#elif defined( UNDER_CE )
+    DeleteCriticalSection( &p_mutex->csection );
+    return 0;
+
 #elif defined( WIN32 )
     if( p_mutex->mutex )
     {
@@ -329,7 +361,7 @@ int __vlc_mutex_destroy( char * psz_file, int i_line, vlc_mutex_t *p_mutex )
     }
     return 0;
 
-#elif defined( PTHREAD_COND_T_IN_PTHREAD_H )    
+#elif defined( PTHREAD_COND_T_IN_PTHREAD_H )
     i_result = pthread_mutex_destroy( &p_mutex->mutex );
     if ( i_result )
     {
@@ -348,7 +380,7 @@ int __vlc_mutex_destroy( char * psz_file, int i_line, vlc_mutex_t *p_mutex )
 
     p_mutex->init = 0;
     return B_OK;
-#endif    
+#endif
 
     if( i_result )
     {
@@ -373,15 +405,26 @@ int __vlc_cond_init( vlc_object_t *p_this, vlc_cond_t *p_condvar )
     p_condvar->cond = st_cond_new();
     return ( p_condvar->cond == NULL ) ? errno : 0;
 
+#elif defined( UNDER_CE )
+    /* Initialize counter */
+    p_condvar->i_waiting_threads = 0;
+
+    /* 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;
+
 #elif defined( WIN32 )
     /* Initialize counter */
     p_condvar->i_waiting_threads = 0;
 
     /* Misc init */
-    p_condvar->i_win9x_cv = p_this->p_vlc->i_win9x_cv;
-    p_condvar->SignalObjectAndWait = p_this->p_vlc->SignalObjectAndWait;
+    p_condvar->i_win9x_cv = p_this->p_libvlc->i_win9x_cv;
+    p_condvar->SignalObjectAndWait = p_this->p_libvlc->SignalObjectAndWait;
 
-    if( (p_condvar->SignalObjectAndWait && !p_this->p_vlc->b_fast_mutex)
+    if( (p_condvar->SignalObjectAndWait && !p_this->p_libvlc->b_fast_mutex)
         || p_condvar->i_win9x_cv == 0 )
     {
         /* Create an auto-reset event. */
@@ -400,7 +443,7 @@ int __vlc_cond_init( vlc_object_t *p_this, vlc_cond_t *p_condvar )
                                                 0x7fffffff, /* max count */
                                                 NULL );     /* unnamed */
 
-        if( p_this->p_vlc->i_win9x_cv == 1 )
+        if( p_condvar->i_win9x_cv == 1 )
             /* Create a manual-reset event initially signaled. */
             p_condvar->event = CreateEvent( NULL, TRUE, TRUE, NULL );
         else
@@ -457,6 +500,9 @@ int __vlc_cond_destroy( char * psz_file, int i_line, vlc_cond_t *p_condvar )
 #elif defined( ST_INIT_IN_ST_H )
     i_result = st_cond_destroy( p_condvar->cond );
 
+#elif defined( UNDER_CE )
+    i_result = !CloseHandle( p_condvar->event );
+
 #elif defined( WIN32 )
     if( !p_condvar->semaphore )
         i_result = !CloseHandle( p_condvar->event );
@@ -527,16 +573,22 @@ int __vlc_thread_create( vlc_object_t *p_this, char * psz_file, int i_line,
 #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 )
+
+#elif defined( WIN32 ) || defined( UNDER_CE )
     {
         unsigned threadID;
         /* When using the MSVCRT C library you have to use the _beginthreadex
          * function instead of CreateThread, otherwise you'll end up with
-        * memory leaks and the signal functions not working */
+         * memory leaks and the signal functions not working (see Microsoft
+         * Knowledge Base, article 104641) */
         p_this->thread_id =
-                (HANDLE)_beginthreadex( NULL, 0, (PTHREAD_START) func, 
+#if defined( UNDER_CE )
+                (HANDLE)CreateThread( NULL, 0, (PTHREAD_START) func,
+                                      (void *)p_this, 0, &threadID );
+#else
+                (HANDLE)_beginthreadex( NULL, 0, (PTHREAD_START) func,
                                         (void *)p_this, 0, &threadID );
+#endif
     }
 
     if ( p_this->thread_id && i_priority )
@@ -555,14 +607,18 @@ int __vlc_thread_create( vlc_object_t *p_this, char * psz_file, int i_line,
 
     if ( i_priority )
     {
+        int i_error;
         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 ) )
+        if ( (i_error = pthread_setschedparam( p_this->thread_id,
+                                               SCHED_RR, &param )) )
         {
-            msg_Warn( p_this, "couldn't go to real-time priority" );
+            msg_Warn( p_this, "couldn't go to real-time priority (%s:%d): %s",
+                      psz_file, i_line, strerror(i_error) );
             i_priority = 0;
         }
+
     }
 
 #elif defined( HAVE_CTHREADS_H )
@@ -571,7 +627,7 @@ int __vlc_thread_create( vlc_object_t *p_this, char * psz_file, int i_line,
 
 #elif defined( HAVE_KERNEL_SCHEDULER_H )
     p_this->thread_id = spawn_thread( (thread_func)func, psz_name,
-                                      B_NORMAL_PRIORITY, (void *)p_this );
+                                      i_priority, (void *)p_this );
     i_ret = resume_thread( p_this->thread_id );
 
 #endif
@@ -598,21 +654,61 @@ int __vlc_thread_create( vlc_object_t *p_this, char * psz_file, int i_line,
         p_this->b_thread = 1;
 
         msg_Dbg( p_this, "thread %d (%s) created at priority %d (%s:%d)",
-                 p_this->thread_id, psz_name, i_priority,
+                 (int)p_this->thread_id, psz_name, i_priority,
                  psz_file, i_line );
 
         vlc_mutex_unlock( &p_this->object_lock );
     }
     else
     {
+#ifdef HAVE_STRERROR
         msg_Err( p_this, "%s thread could not be created at %s:%d (%s)",
                          psz_name, psz_file, i_line, strerror(i_ret) );
+#else
+        msg_Err( p_this, "%s thread could not be created at %s:%d",
+                         psz_name, psz_file, i_line );
+#endif
         vlc_mutex_unlock( &p_this->object_lock );
     }
 
     return i_ret;
 }
 
+/*****************************************************************************
+ * 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)
+ *****************************************************************************/
+int __vlc_thread_set_priority( vlc_object_t *p_this, char * psz_file,
+                               int i_line, int i_priority )
+{
+#if defined( WIN32 ) || defined( UNDER_CE )
+    if ( !SetThreadPriority(GetCurrentThread(), i_priority) )
+    {
+        msg_Warn( p_this, "couldn't set a faster priority" );
+        return 1;
+    }
+
+#elif defined( PTHREAD_COND_T_IN_PTHREAD_H )
+    if ( i_priority )
+    {
+        int i_error;
+        struct sched_param param;
+        memset( &param, 0, sizeof(struct sched_param) );
+        param.sched_priority = i_priority;
+        if ( (i_error = pthread_setschedparam( pthread_self(),
+                                               SCHED_RR, &param )) )
+        {
+            msg_Warn( p_this, "couldn't go to real-time priority (%s:%d): %s",
+                      psz_file, i_line, strerror(i_error) );
+            i_priority = 0;
+        }
+    }
+
+#endif
+
+    return 0;
+}
+
 /*****************************************************************************
  * vlc_thread_ready: tell the parent thread we were successfully spawned
  *****************************************************************************/
@@ -635,7 +731,10 @@ void __vlc_thread_join( vlc_object_t *p_this, char * psz_file, int i_line )
 
 #elif defined( ST_INIT_IN_ST_H )
     i_ret = st_thread_join( p_this->thread_id, NULL );
-    
+
+#elif defined( UNDER_CE )
+    WaitForSingleObject( p_this->thread_id, INFINITE );
+
 #elif defined( WIN32 )
     WaitForSingleObject( p_this->thread_id, INFINITE );
 
@@ -654,13 +753,19 @@ void __vlc_thread_join( vlc_object_t *p_this, char * psz_file, int i_line )
 
     if( i_ret )
     {
+#ifdef HAVE_STRERROR
         msg_Err( p_this, "thread_join(%d) failed at %s:%d (%s)",
-                         p_this->thread_id, psz_file, i_line, strerror(i_ret) );
+                         (int)p_this->thread_id, psz_file, i_line,
+                         strerror(i_ret) );
+#else
+        msg_Err( p_this, "thread_join(%d) failed at %s:%d",
+                         (int)p_this->thread_id, psz_file, i_line );
+#endif
     }
     else
     {
         msg_Dbg( p_this, "thread %d joined (%s:%d)",
-                         p_this->thread_id, psz_file, i_line );
+                         (int)p_this->thread_id, psz_file, i_line );
     }
 
     p_this->b_thread = 0;