diff --git a/docs/rh-log.txt b/docs/rh-log.txt index dedf8b7f7..5bbd1cbc3 100644 --- a/docs/rh-log.txt +++ b/docs/rh-log.txt @@ -1,4 +1,6 @@ April 8, 2008 +- Added the FCriticalSection class in critsec.h to make it easier to + use critical sections in other places besides the OPL player. - Reimplemented snd_midiprecache, now for MIDI as well as MUS, and defaulting to false. diff --git a/src/oplsynth/music_opl_mididevice.cpp b/src/oplsynth/music_opl_mididevice.cpp index 10b8ff00b..bfab365c8 100644 --- a/src/oplsynth/music_opl_mididevice.cpp +++ b/src/oplsynth/music_opl_mididevice.cpp @@ -261,9 +261,9 @@ void OPLMIDIDevice::Stop() int OPLMIDIDevice::StreamOutSync(MIDIHDR *header) { - Serialize(); + ChipAccess.Enter(); StreamOut(header); - Unserialize(); + ChipAccess.Leave(); return 0; } diff --git a/src/oplsynth/opl_mus_player.cpp b/src/oplsynth/opl_mus_player.cpp index f8201e035..2e0c5e90f 100644 --- a/src/oplsynth/opl_mus_player.cpp +++ b/src/oplsynth/opl_mus_player.cpp @@ -28,59 +28,22 @@ OPLmusicBlock::OPLmusicBlock() TwoChips = !opl_onechip; Looping = false; io = NULL; -#ifdef _WIN32 - InitializeCriticalSection (&ChipAccess); -#else - ChipAccess = SDL_CreateMutex (); - if (ChipAccess == NULL) - { - return; - } -#endif io = new OPLio; } OPLmusicBlock::~OPLmusicBlock() { BlockForStats = NULL; -#ifdef _WIN32 - DeleteCriticalSection (&ChipAccess); -#else - if (ChipAccess != NULL) - { - SDL_DestroyMutex (ChipAccess); - ChipAccess = NULL; - } -#endif delete io; } -void OPLmusicBlock::Serialize() -{ -#ifdef _WIN32 - EnterCriticalSection (&ChipAccess); -#else - if (SDL_mutexP (ChipAccess) != 0) - return; -#endif -} - -void OPLmusicBlock::Unserialize() -{ -#ifdef _WIN32 - LeaveCriticalSection (&ChipAccess); -#else - SDL_mutexV (ChipAccess); -#endif -} - void OPLmusicBlock::ResetChips () { TwoChips = !opl_onechip; - Serialize(); + ChipAccess.Enter(); io->OPLdeinit (); io->OPLinit (TwoChips + 1); - Unserialize(); + ChipAccess.Leave(); } void OPLmusicBlock::Restart() @@ -237,7 +200,7 @@ bool OPLmusicBlock::ServiceStream (void *buff, int numbytes) samples1 = samples; memset(buff, 0, numbytes); - Serialize(); + ChipAccess.Enter(); while (numsamples > 0) { double ticky = NextTickIn; @@ -294,7 +257,7 @@ bool OPLmusicBlock::ServiceStream (void *buff, int numbytes) } } } - Unserialize(); + ChipAccess.Leave(); return res; } diff --git a/src/oplsynth/opl_mus_player.h b/src/oplsynth/opl_mus_player.h index d20e0a960..0540ecb13 100644 --- a/src/oplsynth/opl_mus_player.h +++ b/src/oplsynth/opl_mus_player.h @@ -1,11 +1,4 @@ -#ifdef _WIN32 -#define WIN32_LEAN_AND_MEAN -#include -#define USE_WINDOWS_DWORD -#else -#include -#endif - +#include "critsec.h" #include "muslib.h" #include "files.h" @@ -23,19 +16,12 @@ public: protected: virtual int PlayTick() = 0; - void Serialize(); - void Unserialize(); - double NextTickIn; double SamplesPerTick; bool TwoChips; bool Looping; -#ifdef _WIN32 - CRITICAL_SECTION ChipAccess; -#else - SDL_mutex *ChipAccess; -#endif + FCriticalSection ChipAccess; }; class OPLmusicFile : public OPLmusicBlock diff --git a/src/sdl/critsec.h b/src/sdl/critsec.h new file mode 100644 index 000000000..daaf30f7d --- /dev/null +++ b/src/sdl/critsec.h @@ -0,0 +1,48 @@ +// Wraps an SDL mutex object. (A critical section is a Windows synchronization +// object similar to a mutex but optimized for access by threads belonging to +// only one process, hence the class name.) + +#ifndef CRITSEC_H +#define CRITSEC_H + +#include "SDL.h" +#include "SDL_thread.h" +#include "i_system.h" + +class FCriticalSection +{ +public: + FCriticalSection() + { + CritSec = SDL_CreateMutex(); + if (CritSec == NULL) + { + I_FatalError("Failed to create a critical section mutex."); + } + } + ~FCriticalSection() + { + if (CritSec != NULL) + { + SDL_DestroyMutex(CritSec); + } + } + void Enter() + { + if (SDL_mutexP(CritSec) != 0) + { + I_FatalError("Failed entering a critical section."); + } + } + void Leave() + { + if (SDL_mutexV(CritSec) != 0) + { + I_FatalError("Failed to leave a critical section."); + } + } +private: + SDL_mutex *CritSec; +}; + +#endif diff --git a/src/sound/music_mus_midiout.cpp b/src/sound/music_mus_midiout.cpp index e048d2796..651f154b4 100644 --- a/src/sound/music_mus_midiout.cpp +++ b/src/sound/music_mus_midiout.cpp @@ -197,7 +197,7 @@ void MUSSong2::Precache() { if (used[i] < 128) { - work[j++] = used[i]; + work[j++] = (BYTE)used[i]; } else if (used[i] >= 135 && used[i] <= 181) { // Percussions are 100-based, not 128-based, eh? diff --git a/src/win32/critsec.h b/src/win32/critsec.h new file mode 100644 index 000000000..05d54f15f --- /dev/null +++ b/src/win32/critsec.h @@ -0,0 +1,42 @@ +// Wraps a Windows critical section object. + +#ifndef CRITSEC_H +#define CRITSEC_H + +#ifndef _WINNT_ +#define WIN32_LEAN_AND_MEAN +#include +#define USE_WINDOWS_DWORD +#endif + +class FCriticalSection +{ +public: + FCriticalSection() + { + InitializeCriticalSection(&CritSec); + } + ~FCriticalSection() + { + DeleteCriticalSection(&CritSec); + } + void Enter() + { + EnterCriticalSection(&CritSec); + } + void Leave() + { + LeaveCriticalSection(&CritSec); + } +#if 0 + // SDL has no equivalent functionality, so better not use it on Windows. + bool TryEnter() + { + return TryEnterCriticalSection(&CritSec) != 0; + } +#endif +private: + CRITICAL_SECTION CritSec; +}; + +#endif diff --git a/zdoom.vcproj b/zdoom.vcproj index 63aa07d9b..a55ae97b5 100644 --- a/zdoom.vcproj +++ b/zdoom.vcproj @@ -1,7 +1,7 @@ - - - - - - - - - - - - - - - - - - - - + + + + + + + + + + + + + + + + + + + + - - - @@ -944,6 +934,16 @@ Outputs=""src/$(InputName).h"" /> + + + @@ -1538,16 +1538,6 @@ Outputs="$(IntDir)\$(InputName).obj" /> - - - @@ -1558,6 +1548,16 @@ Outputs="$(IntDir)/$(InputName).obj" /> + + + @@ -1582,16 +1582,6 @@ Outputs="$(IntDir)\$(InputName).obj" /> - - - @@ -1602,6 +1592,16 @@ Outputs="$(IntDir)/$(InputName).obj" /> + + + @@ -1626,16 +1626,6 @@ Outputs="$(IntDir)\$(InputName).obj" /> - - - @@ -1646,6 +1636,16 @@ Outputs="$(IntDir)\$(InputName).obj" /> + + + @@ -1670,16 +1670,6 @@ Outputs="$(IntDir)\$(InputName).obj" /> - - - @@ -1690,6 +1680,16 @@ Outputs="$(IntDir)\$(InputName).obj" /> + + + @@ -1714,16 +1714,6 @@ Outputs="$(IntDir)\$(InputName).obj" /> - - - @@ -1734,6 +1724,16 @@ Outputs="$(IntDir)\$(InputName).obj" /> + + + @@ -1781,6 +1781,10 @@ RelativePath=".\src\win32\boing8.ico" > + + @@ -1894,6 +1898,14 @@ Outputs="$(IntDir)\$(InputName).obj" /> + + + @@ -1904,14 +1916,6 @@ Outputs="$(IntDir)\$(InputName).obj" /> - - - + + + @@ -2770,14 +2782,6 @@ AdditionalIncludeDirectories="src\win32;$(NoInherit)" /> - - - @@ -2923,86 +2927,6 @@ - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - + + @@ -3056,7 +2984,7 @@ />