* threads.c : threads implementation for the VideoLAN client
*****************************************************************************
* Copyright (C) 1999, 2000, 2001, 2002 VideoLAN
- * $Id: threads.c,v 1.25 2002/11/10 23:41:53 sam Exp $
+ * $Id: threads.c,v 1.32 2003/01/06 22:57:47 massiot Exp $
*
* Authors: Jean-Marc Dressler <polux@via.ecp.fr>
* Samuel Hocevar <sam@zoy.org>
* 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
#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;
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 )
#elif defined( ST_INIT_IN_ST_H )
i_initializations--;
+#elif defined( UNDER_CE )
+ i_initializations--;
+
#elif defined( WIN32 )
i_initializations--;
/* 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
{
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 */
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 !defined( UNDER_CE )
if( p_this->p_libvlc->SignalObjectAndWait &&
!p_this->p_libvlc->b_fast_mutex )
{
return ( p_mutex->mutex != NULL ? 0 : 1 );
}
else
-# endif
{
p_mutex->mutex = NULL;
InitializeCriticalSection( &p_mutex->csection );
#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 )
{
}
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 )
{
p_mutex->init = 0;
return B_OK;
-#endif
+#endif
if( i_result )
{
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;
-# if !defined( UNDER_CE )
/* Misc init */
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_libvlc->b_fast_mutex)
|| p_condvar->i_win9x_cv == 0 )
{
-# endif
/* Create an auto-reset event. */
p_condvar->event = CreateEvent( NULL, /* no security */
FALSE, /* auto-reset event */
p_condvar->semaphore = NULL;
return !p_condvar->event;
-# if !defined( UNDER_CE )
}
else
{
return !p_condvar->semaphore || !p_condvar->event;
}
-# endif
#elif defined( PTHREAD_COND_T_IN_PTHREAD_H )
return pthread_cond_init( &p_condvar->cond, NULL );
#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 );
#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
* Knowledge Base, article 104641) */
p_this->thread_id =
#if defined( UNDER_CE )
- (HANDLE)CreateThread( NULL, 0, (PTHREAD_START) func,
+ (HANDLE)CreateThread( NULL, 0, (PTHREAD_START) func,
(void *)p_this, 0, &threadID );
#else
- (HANDLE)_beginthreadex( NULL, 0, (PTHREAD_START) func,
+ (HANDLE)_beginthreadex( NULL, 0, (PTHREAD_START) func,
(void *)p_this, 0, &threadID );
#endif
}
if ( i_priority )
{
+ int i_error;
struct sched_param param;
memset( ¶m, 0, sizeof(struct sched_param) );
param.sched_priority = i_priority;
- if ( pthread_setschedparam( p_this->thread_id, SCHED_RR, ¶m ) )
+ if ( (i_error = pthread_setschedparam( p_this->thread_id,
+ SCHED_RR, ¶m )) )
{
- 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;
}
}
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 );
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 )
+ {
+ struct sched_param param;
+ memset( ¶m, 0, sizeof(struct sched_param) );
+ param.sched_priority = i_priority;
+ if ( pthread_setschedparam( pthread_self(), SCHED_RR, ¶m ) )
+ {
+ msg_Warn( p_this, "couldn't go to real-time priority (%s:%d)",
+ psz_file, i_line );
+ return 1;
+ }
+ }
+
+#else
+ return 1;
+#endif
+
+ return 0;
+}
+
/*****************************************************************************
* vlc_thread_ready: tell the parent thread we were successfully spawned
*****************************************************************************/
#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 );
{
#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",
- p_this->thread_id, psz_file, i_line );
+ (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;