Skip to content

Commit

Permalink
Add case for SDL threads to sound related threads
Browse files Browse the repository at this point in the history
  • Loading branch information
uyjulian committed Aug 7, 2023
1 parent 0344cf1 commit 4c778a3
Show file tree
Hide file tree
Showing 3 changed files with 35 additions and 0 deletions.
4 changes: 4 additions & 0 deletions sound/SoundDecodeThread.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -61,8 +61,12 @@ void tTVPSoundDecodeThread::Execute(void) {
// buffer is not full; sleep shorter
// ダブルバッファリングなのでここに来る可能性は低いが、デコードが極度に送れている場合は来る
// 一応スレッド切り替えして占有は避け、次のバッファをデコードする
#ifdef KRKRZ_USE_SDL_THREADS
SDL_Delay(1);
#else
#if !defined(__EMSCRIPTEN__) || (defined(__EMSCRIPTEN__) && defined(__EMSCRIPTEN_PTHREADS__))
std::this_thread::yield();
#endif
#endif
}
}
Expand Down
13 changes: 13 additions & 0 deletions sound/SoundEventThread.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,9 @@ tTVPSoundEventThread::tTVPSoundEventThread( tTVPSoundBuffers* parent )
SuspendThread( false ), PendingLabelEventExists( false ),
NextLabelEventTick( 0 ), LastFilledTick( 0 ), WndProcToBeCalled( false )
{
#ifdef KRKRZ_USE_SDL_THREADS
SuspendMutex = SDL_CreateMutex();
#endif
EventQueue.Allocate();
SetPriority(ttpHighest);
StartTread();
Expand All @@ -28,6 +31,9 @@ tTVPSoundEventThread::~tTVPSoundEventThread()
Event.Set();
WaitFor();
EventQueue.Deallocate();
#ifdef KRKRZ_USE_SDL_THREADS
SDL_DestroyMutex(SuspendMutex);
#endif
}
//---------------------------------------------------------------------------
void tTVPSoundEventThread::UtilWndProc( NativeEvent& ev )
Expand Down Expand Up @@ -146,10 +152,17 @@ void tTVPSoundEventThread::Execute(void)
if( !GetTerminated() ) {
bool suspendrequest = false;
{
#ifdef KRKRZ_USE_SDL_THREADS
SDL_LockMutex(SuspendMutex);
#else
#if !defined(__EMSCRIPTEN__) || (defined(__EMSCRIPTEN__) && defined(__EMSCRIPTEN_PTHREADS__))
std::lock_guard<std::mutex> lock( SuspendMutex );
#endif
#endif
suspendrequest = SuspendThread;
#ifdef KRKRZ_USE_SDL_THREADS
SDL_UnlockMutex(SuspendMutex);
#endif
}
if( suspendrequest ) {
Event.WaitFor( 0 ); // infinity
Expand Down
18 changes: 18 additions & 0 deletions sound/SoundEventThread.h
Original file line number Diff line number Diff line change
Expand Up @@ -18,8 +18,12 @@
class tTVPSoundEventThread : public tTVPThread
{
tTVPThreadEvent Event;
#ifdef KRKRZ_USE_SDL_THREADS
SDL_mutex *SuspendMutex;
#else
#if !defined(__EMSCRIPTEN__) || (defined(__EMSCRIPTEN__) && defined(__EMSCRIPTEN_PTHREADS__))
std::mutex SuspendMutex;
#endif
#endif
bool SuspendThread;

Expand All @@ -39,17 +43,31 @@ class tTVPSoundEventThread : public tTVPThread

void SetSuspend()
{
#ifdef KRKRZ_USE_SDL_THREADS
SDL_LockMutex(SuspendMutex);
#else
#if !defined(__EMSCRIPTEN__) || (defined(__EMSCRIPTEN__) && defined(__EMSCRIPTEN_PTHREADS__))
std::lock_guard<std::mutex> lock( SuspendMutex );
#endif
#endif
SuspendThread = true;
#ifdef KRKRZ_USE_SDL_THREADS
SDL_UnlockMutex(SuspendMutex);
#endif
}
void ResetSuspend()
{
#ifdef KRKRZ_USE_SDL_THREADS
SDL_LockMutex(SuspendMutex);
#else
#if !defined(__EMSCRIPTEN__) || (defined(__EMSCRIPTEN__) && defined(__EMSCRIPTEN_PTHREADS__))
std::lock_guard<std::mutex> lock( SuspendMutex );
#endif
#endif
SuspendThread = false;
#ifdef KRKRZ_USE_SDL_THREADS
SDL_UnlockMutex(SuspendMutex);
#endif
}

public:
Expand Down

0 comments on commit 4c778a3

Please sign in to comment.