Use CHECKPOINT and RageThreads.

This commit is contained in:
Glenn Maynard
2003-08-05 01:32:51 +00:00
parent 8ee7dc93a6
commit 32e81d2bbc
10 changed files with 53 additions and 65 deletions
@@ -116,28 +116,27 @@ InputHandler_DInput::InputHandler_DInput()
Devices[i].buffered? "buffered": "unbuffered" );
}
InputThreadPtr = NULL;
StartThread();
}
void InputHandler_DInput::StartThread()
{
ASSERT( InputThreadPtr == NULL );
ASSERT( !InputThread.IsCreated() );
if( PREFSMAN->m_bThreadedInput )
InputThreadPtr = SDL_CreateThread(InputThread_Start, this);
else
InputThreadPtr = NULL;
{
InputThread.SetName( "DirectInput thread" );
InputThread.Create( InputThread_Start, this );
}
}
void InputHandler_DInput::ShutdownThread()
{
if( InputThreadPtr == NULL )
if( !InputThread.IsCreated() )
return;
shutdown = true;
LOG->Trace("Shutting down DirectInput thread ...");
SDL_WaitThread(InputThreadPtr, NULL);
InputThreadPtr = NULL;
InputThread.Wait();
LOG->Trace("DirectInput thread shut down.");
shutdown = false;
}
@@ -427,7 +426,7 @@ void InputHandler_DInput::Update(float fDeltaTime)
{
if( !Devices[i].buffered )
UpdatePolled( Devices[i], zero );
else if( InputThreadPtr == NULL )
else if( !InputThread.IsCreated() )
{
/* If we have an input thread, it'll handle buffered devices. */
UpdateBuffered( Devices[i], zero );
@@ -438,10 +437,8 @@ void InputHandler_DInput::Update(float fDeltaTime)
}
void InputHandler_DInput::InputThread()
void InputHandler_DInput::InputThreadMain()
{
InitThreadData("DirectInput thread");
VDCHECKPOINT;
if(!SetThreadPriority(GetCurrentThread(), THREAD_PRIORITY_HIGHEST))
LOG->Warn(werr_ssprintf(GetLastError(), "Failed to set DirectInput thread priority"));
@@ -467,7 +464,7 @@ void InputHandler_DInput::InputThread()
while(!shutdown)
{
VDCHECKPOINT;
CHECKPOINT;
if( BufferedDevices.size() )
{
/* Update buffered devices. */
@@ -487,14 +484,14 @@ void InputHandler_DInput::InputThread()
UpdateBuffered( *BufferedDevices[i], now );
}
}
VDCHECKPOINT;
CHECKPOINT;
/* If we have no buffered devices, we didn't delay at WaitForMultipleObjectsEx. */
if( BufferedDevices.size() == 0 )
SDL_Delay( 50 );
VDCHECKPOINT;
CHECKPOINT;
}
VDCHECKPOINT;
CHECKPOINT;
for( i = 0; i < Devices.size(); ++i )
{
@@ -2,20 +2,20 @@
#define INPUTHANDLER_DIRECTINPUT_H
#include "InputHandler.h"
#include "SDL_Thread.h"
#include "RageThreads.h"
struct DIDevice;
class InputHandler_DInput: public InputHandler
{
SDL_Thread *InputThreadPtr;
RageThread InputThread;
bool shutdown;
void UpdatePolled(DIDevice &device, const RageTimer &tm);
void UpdateBuffered(DIDevice &device, const RageTimer &tm);
void PollAndAcquireDevices();
static int InputThread_Start( void *p ) { ((InputHandler_DInput *) p)->InputThread(); return 0; }
void InputThread();
static int InputThread_Start( void *p ) { ((InputHandler_DInput *) p)->InputThreadMain(); return 0; }
void InputThreadMain();
void StartThread();
void ShutdownThread();
@@ -26,18 +26,19 @@ InputHandler_Win32_Pump::InputHandler_Win32_Pump()
/* Don't start a thread if we have no pads. */
if( FoundOnePad && PREFSMAN->m_bThreadedInput )
InputThreadPtr = SDL_CreateThread(InputThread_Start, this);
else
InputThreadPtr = NULL;
{
InputThread.SetName("Pump thread");
InputThread.Create( InputThread_Start, this );
}
}
InputHandler_Win32_Pump::~InputHandler_Win32_Pump()
{
if( InputThreadPtr )
if( InputThread.IsCreated() )
{
shutdown = true;
LOG->Trace("Shutting down Pump thread ...");
SDL_WaitThread(InputThreadPtr, NULL);
InputThread.Wait();
LOG->Trace("Pump thread shut down.");
}
@@ -59,7 +60,7 @@ void InputHandler_Win32_Pump::HandleInput( int devno, int event )
DeviceInput dev(id, butno);
/* If we're in a thread, our timestamp is accurate. */
if( InputThreadPtr )
if( InputThread.IsCreated() )
dev.ts.Touch();
ButtonPressed(dev, !(event & bits[butno]));
@@ -80,15 +81,12 @@ void InputHandler_Win32_Pump::GetDevicesAndDescriptions(vector<InputDevice>& vDe
int InputHandler_Win32_Pump::InputThread_Start( void *p )
{
((InputHandler_Win32_Pump *) p)->InputThread();
((InputHandler_Win32_Pump *) p)->InputThreadMain();
return 0;
}
void InputHandler_Win32_Pump::InputThread()
void InputHandler_Win32_Pump::InputThreadMain()
{
InitThreadData("Pump thread");
VDCHECKPOINT;
vector<WindowsFileIO *> sources;
int i;
for(i = 0; i < NUM_PUMPS; ++i)
@@ -99,23 +97,23 @@ void InputHandler_Win32_Pump::InputThread()
while(!shutdown)
{
VDCHECKPOINT;
CHECKPOINT;
int actual = 0, val = 0;
int ret = WindowsFileIO::read_several(sources, &val, actual, 0.100f);
VDCHECKPOINT;
CHECKPOINT;
if(ret <= 0)
continue; /* no event */
HandleInput( actual, val );
InputHandler::UpdateTimer();
}
VDCHECKPOINT;
CHECKPOINT;
}
void InputHandler_Win32_Pump::Update(float fDeltaTime)
{
if( InputThreadPtr == NULL )
if( !InputThread.IsCreated() )
{
for(int i = 0; i < NUM_PUMPS; ++i)
{
@@ -2,17 +2,17 @@
#define INPUT_HANDLER_WIN32_PUMP_H 1
#include "InputHandler.h"
#include "SDL_Thread.h"
#include "RageThreads.h"
class USBDevice;
class InputHandler_Win32_Pump: public InputHandler
{
USBDevice *dev;
SDL_Thread *InputThreadPtr;
RageThread InputThread;
bool shutdown;
static int InputThread_Start( void *p );
void InputThread();
void InputThreadMain();
void HandleInput( int devno, int event );
@@ -40,11 +40,6 @@ int RageSound_DSound::MixerThread_start(void *p)
void RageSound_DSound::MixerThread()
{
#ifdef _WINDOWS
InitThreadData("Mixer thread");
#endif
VDCHECKPOINT;
/* SOUNDMAN will be set once RageSoundManager's ctor returns and
* assigns it; we might get here before that happens, though. */
while(!SOUNDMAN && !shutdown) Sleep(10);
@@ -53,14 +48,14 @@ void RageSound_DSound::MixerThread()
LOG->Warn(werr_ssprintf(GetLastError(), "Failed to set sound thread priority"));
while(!shutdown) {
VDCHECKPOINT;
CHECKPOINT;
/* Sleep for the size of one chunk. */
const int chunksize_frames = buffersize_frames / num_chunks;
float sleep_secs = (float(chunksize_frames) / samplerate);
Sleep(int(1000 * sleep_secs));
VDCHECKPOINT;
CHECKPOINT;
LockMutex L(SOUNDMAN->lock);
for(unsigned i = 0; i < stream_pool.size(); ++i)
{
@@ -104,7 +99,7 @@ void RageSound_DSound::Update(float delta)
* it in the opposite buffer. */
bool RageSound_DSound::stream::GetData(bool init)
{
VDCHECKPOINT;
CHECKPOINT;
char *locked_buf;
unsigned len;
@@ -196,7 +191,8 @@ RageSound_DSound::RageSound_DSound()
/* Set channel volumes. */
VolumeChanged();
MixerThreadPtr = SDL_CreateThread(MixerThread_start, this);
MixingThread.SetName("Mixer thread");
MixingThread.Create( MixerThread_start, this );
}
RageSound_DSound::~RageSound_DSound()
@@ -205,7 +201,7 @@ RageSound_DSound::~RageSound_DSound()
shutdown = true;
LOG->Trace("Shutting down mixer thread ...");
LOG->Flush();
SDL_WaitThread(MixerThreadPtr, NULL);
MixingThread.Wait();
LOG->Trace("Mixer thread shut down.");
LOG->Flush();
@@ -2,8 +2,8 @@
#define RAGE_SOUND_DSOUND
#include "RageSoundDriver.h"
#include "SDL_Thread.h"
#include "DSoundHelpers.h"
#include "RageThreads.h"
struct IDirectSound;
struct IDirectSoundBuffer;
@@ -41,7 +41,7 @@ class RageSound_DSound: public RageSoundDriver
bool shutdown; /* tells the MixerThread to shut down */
static int MixerThread_start(void *p);
void MixerThread();
SDL_Thread *MixerThreadPtr;
RageThread MixingThread;
/* virtuals: */
void StartMixing(RageSound *snd); /* used by RageSound */
@@ -32,9 +32,6 @@ int RageSound_DSound_Software::MixerThread_start(void *p)
void RageSound_DSound_Software::MixerThread()
{
InitThreadData("Mixer thread");
VDCHECKPOINT;
/* SOUNDMAN will be set once RageSoundManager's ctor returns and
* assigns it; we might get here before that happens, though. */
while(!SOUNDMAN && !shutdown) Sleep(10);
@@ -178,16 +175,18 @@ RageSound_DSound_Software::RageSound_DSound_Software()
str_ds = new DSoundBuf(ds,
DSoundBuf::HW_DONT_CARE,
channels, samplerate, 16, buffersize);
MixerThreadPtr = SDL_CreateThread(MixerThread_start, this);
MixingThread.SetName("Mixer thread");
MixingThread.Create( MixerThread_start, this );
}
RageSound_DSound_Software::~RageSound_DSound_Software()
{
/* Signal the mixing thread to quit. */
shutdown = true;
LOG->Trace("Shutting down mixer thread %p ...", MixerThreadPtr);
LOG->Trace("Shutting down mixer thread ...");
LOG->Flush();
SDL_WaitThread(MixerThreadPtr, NULL);
MixingThread.Wait();
LOG->Trace("Mixer thread shut down.");
LOG->Flush();
@@ -2,8 +2,8 @@
#define RAGE_SOUND_DSOUND_SOFTWARE
#include "RageSoundDriver.h"
#include "SDL_Thread.h"
#include "DSoundHelpers.h"
#include "RageThreads.h"
struct IDirectSound;
struct IDirectSoundBuffer;
@@ -33,7 +33,7 @@ class RageSound_DSound_Software: public RageSoundDriver
static int MixerThread_start(void *p);
void MixerThread();
SDL_Thread *MixerThreadPtr;
RageThread MixingThread;
/* virtuals: */
void StartMixing(RageSound *snd); /* used by RageSound */
@@ -44,9 +44,6 @@ int RageSound_WaveOut::MixerThread_start(void *p)
void RageSound_WaveOut::MixerThread()
{
InitThreadData("Mixer thread");
VDCHECKPOINT;
/* SOUNDMAN will be set once RageSoundManager's ctor returns and
* assigns it; we might get here before that happens, though. */
while(!SOUNDMAN && !shutdown) Sleep(10);
@@ -204,7 +201,8 @@ RageSound_WaveOut::RageSound_WaveOut()
buffers[b].dwFlags |= WHDR_DONE;
}
MixerThreadPtr = SDL_CreateThread(MixerThread_start, this);
MixingThread.SetName("Mixer thread");
MixingThread.Create( MixerThread_start, this );
}
RageSound_WaveOut::~RageSound_WaveOut()
@@ -212,7 +210,7 @@ RageSound_WaveOut::~RageSound_WaveOut()
/* Signal the mixing thread to quit. */
shutdown = true;
LOG->Trace("Shutting down mixer thread ...");
SDL_WaitThread(MixerThreadPtr, NULL);
MixingThread.Wait();
LOG->Trace("Mixer thread shut down.");
CloseHandle(sound_event);
@@ -2,7 +2,7 @@
#define RAGE_SOUND_WAVEOUT
#include "RageSoundDriver.h"
#include "SDL_Thread.h"
#include "RageThreads.h"
#include "Mmsystem.h"
class RageSound_WaveOut: public RageSoundDriver
@@ -28,7 +28,7 @@ class RageSound_WaveOut: public RageSoundDriver
static int MixerThread_start(void *p);
void MixerThread();
SDL_Thread *MixerThreadPtr;
RageThread MixingThread;
bool GetData();
void Update(float delta);