Commit 886080d9 authored by Gildas Bazin's avatar Gildas Bazin

* include/threads.h,include/threads_funcs.h: reverted Win32 pthread implementation to
the old code. Fixed vlc_cond_broadcast() for WinNT/2K/XP. Additional vlc_cond_*
implementations for Win9x.
* src/interface/main.c: renamed --fast_pthread option into --fast-mutex. Added a
--win9x-cv-method option to choose which vlc_cond_* implementation we want on Win9x.
parent 1d050f96
...@@ -3,7 +3,7 @@ ...@@ -3,7 +3,7 @@
* This header provides a portable threads implementation. * This header provides a portable threads implementation.
***************************************************************************** *****************************************************************************
* Copyright (C) 1999, 2000 VideoLAN * Copyright (C) 1999, 2000 VideoLAN
* $Id: threads.h,v 1.42.2.1 2002/06/17 08:37:56 sam Exp $ * $Id: threads.h,v 1.42.2.2 2002/07/29 16:12:24 gbazin Exp $
* *
* Authors: Jean-Marc Dressler <polux@via.ecp.fr> * Authors: Jean-Marc Dressler <polux@via.ecp.fr>
* Samuel Hocevar <sam@via.ecp.fr> * Samuel Hocevar <sam@via.ecp.fr>
...@@ -114,14 +114,12 @@ typedef struct ...@@ -114,14 +114,12 @@ typedef struct
typedef struct typedef struct
{ {
int i_waiting_threads; volatile int i_waiting_threads;
/* WinNT/2K/XP implementation */ /* WinNT/2K/XP implementation */
HANDLE semaphore; HANDLE event;
HANDLE signal;
boolean_t b_broadcast;
/* Win95/98/ME implementation */ /* Win95/98/ME implementation */
enum { SIGNAL = 0, BROADCAST = 1 }; HANDLE semaphore;
HANDLE p_events[2]; CRITICAL_SECTION csection;
} vlc_cond_t; } vlc_cond_t;
typedef unsigned (__stdcall *PTHREAD_START) (void *); typedef unsigned (__stdcall *PTHREAD_START) (void *);
......
...@@ -3,7 +3,7 @@ ...@@ -3,7 +3,7 @@
* This header provides a portable threads implementation. * This header provides a portable threads implementation.
***************************************************************************** *****************************************************************************
* Copyright (C) 1999, 2000 VideoLAN * Copyright (C) 1999, 2000 VideoLAN
* $Id: threads_funcs.h,v 1.4.2.2 2002/07/25 22:23:52 sam Exp $ * $Id: threads_funcs.h,v 1.4.2.3 2002/07/29 16:12:24 gbazin Exp $
* *
* Authors: Jean-Marc Dressler <polux@via.ecp.fr> * Authors: Jean-Marc Dressler <polux@via.ecp.fr>
* Samuel Hocevar <sam@via.ecp.fr> * Samuel Hocevar <sam@via.ecp.fr>
...@@ -97,7 +97,7 @@ static inline int vlc_mutex_init( vlc_mutex_t *p_mutex ) ...@@ -97,7 +97,7 @@ static inline int vlc_mutex_init( vlc_mutex_t *p_mutex )
* function and have a 100% correct vlc_cond_wait() implementation. * function and have a 100% correct vlc_cond_wait() implementation.
* As this function is not available on Win9x, we can use the faster * As this function is not available on Win9x, we can use the faster
* CriticalSections */ * CriticalSections */
if( (GetVersion() < 0x80000000) && !p_main->p_sys->b_fast_pthread ) if( p_main->p_sys->SignalObjectAndWait && !p_main->p_sys->b_fast_mutex )
{ {
/* We are running on NT/2K/XP, we can use SignalObjectAndWait */ /* We are running on NT/2K/XP, we can use SignalObjectAndWait */
p_mutex->mutex = CreateMutex( 0, FALSE, 0 ); p_mutex->mutex = CreateMutex( 0, FALSE, 0 );
...@@ -349,24 +349,34 @@ static inline int vlc_cond_init( vlc_cond_t *p_condvar ) ...@@ -349,24 +349,34 @@ static inline int vlc_cond_init( vlc_cond_t *p_condvar )
/* Initialize counter */ /* Initialize counter */
p_condvar->i_waiting_threads = 0; p_condvar->i_waiting_threads = 0;
if( (GetVersion() < 0x80000000) && !p_main->p_sys->b_fast_pthread ) if( p_main->p_sys->i_win9x_cv == 0 )
{ {
/* Create an auto-reset event and a semaphore. */ /* Create an auto-reset event. */
p_condvar->signal = CreateEvent( NULL, FALSE, FALSE, NULL ); p_condvar->event = CreateEvent( NULL, /* no security */
p_condvar->semaphore = CreateSemaphore( NULL, 0, 0x7fffffff, NULL ); FALSE, /* auto-reset event */
FALSE, /* start non-signaled */
p_condvar->b_broadcast = 0; NULL ); /* unnamed */
return !p_condvar->signal || !p_condvar->semaphore; p_condvar->semaphore = NULL;
return !p_condvar->event;
} }
else else
{ {
p_condvar->signal = NULL; p_condvar->semaphore = CreateSemaphore( NULL, /* no security */
0, /* initial count */
0x7fffffff, /* max count */
NULL ); /* unnamed */
if( p_main->p_sys->i_win9x_cv == 1 )
/* 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 );
InitializeCriticalSection( &p_condvar->csection );
/* Create an auto-reset event and a manual-reset event. */ return !p_condvar->semaphore || !p_condvar->event;
p_condvar->p_events[0] = CreateEvent( NULL, FALSE, FALSE, NULL );
p_condvar->p_events[1] = CreateEvent( NULL, TRUE, FALSE, NULL );
return !p_condvar->p_events[0] || !p_condvar->p_events[1];
} }
#elif defined( PTHREAD_COND_T_IN_PTHREAD_H ) #elif defined( PTHREAD_COND_T_IN_PTHREAD_H )
...@@ -414,15 +424,40 @@ static inline int vlc_cond_signal( vlc_cond_t *p_condvar ) ...@@ -414,15 +424,40 @@ static inline int vlc_cond_signal( vlc_cond_t *p_condvar )
/* Release one waiting thread if one is available. */ /* Release one waiting thread if one is available. */
/* For this trick to work properly, the vlc_cond_signal must be surrounded /* For this trick to work properly, the vlc_cond_signal must be surrounded
* by a mutex. This will prevent another thread from stealing the signal */ * by a mutex. This will prevent another thread from stealing the signal */
if( p_condvar->i_waiting_threads )
if( !p_condvar->semaphore )
{
PulseEvent( p_condvar->event );
}
else if( p_main->p_sys->i_win9x_cv == 1 )
{ {
if( p_condvar->signal ) /* Wait for the gate to be open */
WaitForSingleObject( p_condvar->event, INFINITE );
if( p_condvar->i_waiting_threads )
{ {
/* Using a semaphore exposes us to a race condition. It is
* possible for another thread to start waiting on the semaphore
* just after we signaled it and thus steal the signal.
* We have to prevent new threads from entering the cond_wait(). */
ResetEvent( p_condvar->event );
/* A semaphore is used here because Win9x doesn't have
* SignalObjectAndWait() and thus a race condition exists
* during the time we release the mutex and the time we start
* waiting on the event (more precisely, the signal can sometimes
* be missed by the waiting thread if we use PulseEvent()). */
ReleaseSemaphore( p_condvar->semaphore, 1, 0 ); ReleaseSemaphore( p_condvar->semaphore, 1, 0 );
} }
else }
else
{
if( p_condvar->i_waiting_threads )
{ {
SetEvent( p_condvar->p_events[0] ); ReleaseSemaphore( p_condvar->semaphore, 1, 0 );
/* Wait for the last thread to be awakened */
WaitForSingleObject( p_condvar->event, INFINITE );
} }
} }
return 0; return 0;
...@@ -495,23 +530,36 @@ static inline int vlc_cond_broadcast( vlc_cond_t *p_condvar ) ...@@ -495,23 +530,36 @@ static inline int vlc_cond_broadcast( vlc_cond_t *p_condvar )
#elif defined( WIN32 ) #elif defined( WIN32 )
/* Release all waiting threads. */ /* Release all waiting threads. */
if( p_condvar->i_waiting_threads ) int i;
if( !p_condvar->semaphore )
for( i = p_condvar->i_waiting_threads; i > 0; i-- )
PulseEvent( p_condvar->event );
else if( p_main->p_sys->i_win9x_cv == 1 )
{ {
if( p_condvar->signal ) /* Wait for the gate to be open */
WaitForSingleObject( p_condvar->event, INFINITE );
if( p_condvar->i_waiting_threads )
{ {
p_condvar->b_broadcast = 1; /* close gate */
/* This call is atomic */ ResetEvent( p_condvar->event );
ReleaseSemaphore( p_condvar->semaphore, ReleaseSemaphore( p_condvar->semaphore,
p_condvar->i_waiting_threads, 0 ); p_condvar->i_waiting_threads, 0 );
/* Wait for all threads to get the semaphore */
WaitForSingleObject( p_condvar->signal, INFINITE );
p_condvar->b_broadcast = 0;
} }
else }
else
{
if( p_condvar->i_waiting_threads )
{ {
SetEvent( p_condvar->p_events[1] ); ReleaseSemaphore( p_condvar->semaphore,
p_condvar->i_waiting_threads, 0 );
/* Wait for the last thread to be awakened */
WaitForSingleObject( p_condvar->event, INFINITE );
} }
} }
return 0; return 0;
#elif defined( PTHREAD_COND_T_IN_PTHREAD_H ) #elif defined( PTHREAD_COND_T_IN_PTHREAD_H )
...@@ -592,58 +640,76 @@ static inline int _vlc_cond_wait( char * psz_file, int i_line, ...@@ -592,58 +640,76 @@ static inline int _vlc_cond_wait( char * psz_file, int i_line,
return i_ret; return i_ret;
#elif defined( WIN32 ) #elif defined( WIN32 )
/* Increase our wait count */ if( !p_condvar->semaphore )
p_condvar->i_waiting_threads++;
if( p_condvar->signal )
{ {
/* Increase our wait count */
p_condvar->i_waiting_threads++;
if( p_main->p_sys->SignalObjectAndWait )
/* It is only possible to atomically release the mutex and initiate the /* It is only possible to atomically release the mutex and initiate the
* waiting on WinNT/2K/XP. Win9x doesn't have SignalObjectAndWait(). */ * waiting on WinNT/2K/XP. Win9x doesn't have SignalObjectAndWait(). */
p_main->p_sys->SignalObjectAndWait( p_mutex->mutex, p_main->p_sys->SignalObjectAndWait( p_mutex->mutex,
p_condvar->semaphore, p_condvar->event,
INFINITE, FALSE );
/* XXX: we should protect i_waiting_threads with a mutex, but
* is it really worth it ? */
p_condvar->i_waiting_threads--;
if( p_condvar->b_broadcast
&& p_condvar->i_waiting_threads == 0 )
{
p_main->p_sys->SignalObjectAndWait( p_condvar->signal,
p_mutex->mutex,
INFINITE, FALSE ); INFINITE, FALSE );
}
else else
{ {
/* Just take back the lock */ LeaveCriticalSection( &p_mutex->csection );
WaitForSingleObject( p_mutex->mutex, INFINITE ); WaitForSingleObject( p_condvar->event, INFINITE );
} }
return 0; }
else if( p_main->p_sys->i_win9x_cv == 1 )
{
int i_waiting_threads;
/* Wait for the gate to be open */
WaitForSingleObject( p_condvar->event, INFINITE );
/* Increase our wait count */
p_condvar->i_waiting_threads++;
LeaveCriticalSection( &p_mutex->csection );
WaitForSingleObject( p_condvar->semaphore, INFINITE );
/* Decrement and test must be atomic */
EnterCriticalSection( &p_condvar->csection );
/* Decrease our wait count */
i_waiting_threads = --p_condvar->i_waiting_threads;
LeaveCriticalSection( &p_condvar->csection );
/* Reopen the gate if we were the last waiting thread */
if( !i_waiting_threads )
SetEvent( p_condvar->event );
} }
else else
{ {
int i_ret; int i_waiting_threads;
/* Increase our wait count */
p_condvar->i_waiting_threads++;
/* Release the mutex, wait, and reacquire. */
LeaveCriticalSection( &p_mutex->csection ); LeaveCriticalSection( &p_mutex->csection );
i_ret = WaitForMultipleObjects( 2, p_condvar->p_events, WaitForSingleObject( p_condvar->semaphore, INFINITE );
FALSE, INFINITE );
EnterCriticalSection( &p_mutex->csection ); /* Decrement and test must be atomic */
EnterCriticalSection( &p_condvar->csection );
/* Decrease our wait count */ /* Decrease our wait count */
p_condvar->i_waiting_threads--; i_waiting_threads = --p_condvar->i_waiting_threads;
/* If we are the last waiter and it was a broadcast signal, reset LeaveCriticalSection( &p_condvar->csection );
* the broadcast event. */
if( i_ret == WAIT_OBJECT_0 + 1 /* Signal that the last waiting thread just went through */
&& p_condvar->i_waiting_threads == 0 ) if( !i_waiting_threads )
{ SetEvent( p_condvar->event );
ResetEvent( p_condvar->p_events[1] );
}
return( i_ret == WAIT_FAILED );
} }
/* Reacquire the mutex before returning. */
vlc_mutex_lock( p_mutex );
return 0;
#elif defined( PTHREAD_COND_T_IN_PTHREAD_H ) #elif defined( PTHREAD_COND_T_IN_PTHREAD_H )
#ifndef DEBUG #ifndef DEBUG
...@@ -733,16 +799,11 @@ static inline int _vlc_cond_destroy( char * psz_file, int i_line, ...@@ -733,16 +799,11 @@ static inline int _vlc_cond_destroy( char * psz_file, int i_line,
return st_cond_destroy( *p_condvar ); return st_cond_destroy( *p_condvar );
#elif defined( WIN32 ) #elif defined( WIN32 )
if( p_condvar->signal ) if( !p_condvar->semaphore )
{ return !CloseHandle( p_condvar->event );
return !CloseHandle( p_condvar->signal )
|| !CloseHandle( p_condvar->semaphore );
}
else else
{ return !CloseHandle( p_condvar->event )
return !CloseHandle( p_condvar->p_events[0] ) || !CloseHandle( p_condvar->semaphore );
|| !CloseHandle( p_condvar->p_events[1] );
}
#elif defined( PTHREAD_COND_T_IN_PTHREAD_H ) #elif defined( PTHREAD_COND_T_IN_PTHREAD_H )
int i_result = pthread_cond_destroy( p_condvar ); int i_result = pthread_cond_destroy( p_condvar );
......
...@@ -2,7 +2,7 @@ ...@@ -2,7 +2,7 @@
* win32_specific.h: Win32 specific features * win32_specific.h: Win32 specific features
***************************************************************************** *****************************************************************************
* Copyright (C) 2001 VideoLAN * Copyright (C) 2001 VideoLAN
* $Id: win32_specific.h,v 1.2 2002/04/02 23:43:57 gbazin Exp $ * $Id: win32_specific.h,v 1.2.2.1 2002/07/29 16:12:24 gbazin Exp $
* *
* Authors: Samuel Hocevar <sam@zoy.org> * Authors: Samuel Hocevar <sam@zoy.org>
* Gildas Bazin <gbazin@netcourrier.com> * Gildas Bazin <gbazin@netcourrier.com>
...@@ -36,6 +36,7 @@ typedef BOOL (WINAPI *SIGNALOBJECTANDWAIT)( HANDLE, HANDLE, DWORD, BOOL ); ...@@ -36,6 +36,7 @@ typedef BOOL (WINAPI *SIGNALOBJECTANDWAIT)( HANDLE, HANDLE, DWORD, BOOL );
typedef struct main_sys_s typedef struct main_sys_s
{ {
SIGNALOBJECTANDWAIT SignalObjectAndWait; SIGNALOBJECTANDWAIT SignalObjectAndWait;
boolean_t b_fast_pthread; boolean_t b_fast_mutex;
int i_win9x_cv;
} main_sys_t; } main_sys_t;
...@@ -4,7 +4,7 @@ ...@@ -4,7 +4,7 @@
* and spawn threads. * and spawn threads.
***************************************************************************** *****************************************************************************
* Copyright (C) 1998-2001 VideoLAN * Copyright (C) 1998-2001 VideoLAN
* $Id: main.c,v 1.195.2.4 2002/07/19 21:14:40 massiot Exp $ * $Id: main.c,v 1.195.2.5 2002/07/29 16:12:24 gbazin Exp $
* *
* Authors: Vincent Seguin <seguin@via.ecp.fr> * Authors: Vincent Seguin <seguin@via.ecp.fr>
* Samuel Hocevar <sam@zoy.org> * Samuel Hocevar <sam@zoy.org>
...@@ -337,11 +337,23 @@ ...@@ -337,11 +337,23 @@
#define DEMUX_LONGTEXT N_( \ #define DEMUX_LONGTEXT N_( \
"This is a legacy entry to let you configure demux modules") "This is a legacy entry to let you configure demux modules")
#define FAST_PTHREAD_TEXT N_("fast pthread on NT/2K/XP (developpers only)") #define FAST_MUTEX_TEXT N_("fast mutex on NT/2K/XP (developpers only)")
#define FAST_PTHREAD_LONGTEXT N_( \ #define FAST_MUTEX_LONGTEXT N_( \
"On Windows NT/2K/XP we use a slow but correct pthread implementation, " \ "On Windows NT/2K/XP we use a slow mutex implementation but which " \
"you can also use this faster implementation but you might experience " \ "allows us to correctely implement condition variables. " \
"problems with it.") "You can also use the faster Win9x implementation but you might " \
"experience problems with it.")
#define WIN9X_CV_TEXT N_("Condition variables implementation for Win9x " \
"(developpers only)")
#define WIN9X_CV_LONGTEXT N_( \
"On Windows 9x/Me we use a fast but not correct condition variables " \
"implementation (more precisely there is a possibility for a race " \
"condition to happen). " \
"However it is possible to use slower alternatives which should be more " \
"robust. " \
"Currently you can choose between implementation 0 (which is the " \
"default and the fastest), 1 and 2.")
/* /*
* Quick usage guide for the configuration options: * Quick usage guide for the configuration options:
...@@ -436,7 +448,8 @@ ADD_MODULE ( "access", MODULE_CAPABILITY_ACCESS, NULL, NULL, ACCESS_TEXT, ACCES ...@@ -436,7 +448,8 @@ ADD_MODULE ( "access", MODULE_CAPABILITY_ACCESS, NULL, NULL, ACCESS_TEXT, ACCES
ADD_MODULE ( "demux", MODULE_CAPABILITY_DEMUX, NULL, NULL, DEMUX_TEXT, DEMUX_LONGTEXT ) ADD_MODULE ( "demux", MODULE_CAPABILITY_DEMUX, NULL, NULL, DEMUX_TEXT, DEMUX_LONGTEXT )
#if defined(WIN32) #if defined(WIN32)
ADD_BOOL ( "fast_pthread", 0, NULL, FAST_PTHREAD_TEXT, FAST_PTHREAD_LONGTEXT ) ADD_BOOL ( "fast-mutex", 0, NULL, FAST_MUTEX_TEXT, FAST_MUTEX_LONGTEXT )
ADD_INTEGER ( "win9x-cv-method", 0, NULL, WIN9X_CV_TEXT, WIN9X_CV_LONGTEXT )
#endif #endif
MODULE_CONFIG_STOP MODULE_CONFIG_STOP
......
...@@ -2,7 +2,7 @@ ...@@ -2,7 +2,7 @@
* win32_specific.c: Win32 specific features * win32_specific.c: Win32 specific features
***************************************************************************** *****************************************************************************
* Copyright (C) 2001 VideoLAN * Copyright (C) 2001 VideoLAN
* $Id: win32_specific.c,v 1.7 2002/04/27 22:11:22 gbazin Exp $ * $Id: win32_specific.c,v 1.7.2.1 2002/07/29 16:12:24 gbazin Exp $
* *
* Authors: Samuel Hocevar <sam@zoy.org> * Authors: Samuel Hocevar <sam@zoy.org>
* Gildas Bazin <gbazin@netcourrier.com> * Gildas Bazin <gbazin@netcourrier.com>
...@@ -49,9 +49,16 @@ void system_Init( int *pi_argc, char *ppsz_argv[], char *ppsz_env[] ) ...@@ -49,9 +49,16 @@ void system_Init( int *pi_argc, char *ppsz_argv[], char *ppsz_env[] )
} }
/* dynamically get the address of SignalObjectAndWait */ /* dynamically get the address of SignalObjectAndWait */
hInstLib = LoadLibrary( "kernel32" ); if( (GetVersion() < 0x80000000) )
p_main->p_sys->SignalObjectAndWait = {
(SIGNALOBJECTANDWAIT)GetProcAddress( hInstLib, "SignalObjectAndWait" ); /* We are running on NT/2K/XP, we can use SignalObjectAndWait */
hInstLib = LoadLibrary( "kernel32" );
if( hInstLib)
p_main->p_sys->SignalObjectAndWait =
(SIGNALOBJECTANDWAIT)GetProcAddress( hInstLib,
"SignalObjectAndWait" );
}
else p_main->p_sys->SignalObjectAndWait = NULL;
/* WinSock Library Init. */ /* WinSock Library Init. */
i_err = WSAStartup( MAKEWORD( 1, 1 ), &Data ); i_err = WSAStartup( MAKEWORD( 1, 1 ), &Data );
...@@ -69,7 +76,8 @@ void system_Init( int *pi_argc, char *ppsz_argv[], char *ppsz_env[] ) ...@@ -69,7 +76,8 @@ void system_Init( int *pi_argc, char *ppsz_argv[], char *ppsz_env[] )
*****************************************************************************/ *****************************************************************************/
void system_Configure( void ) void system_Configure( void )
{ {
p_main->p_sys->b_fast_pthread = config_GetIntVariable( "fast_pthread" ); p_main->p_sys->b_fast_mutex = config_GetIntVariable( "fast-mutex" );
p_main->p_sys->i_win9x_cv = config_GetIntVariable( "win9x-cv-method" );
} }
/***************************************************************************** /*****************************************************************************
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment