Changeset View
Changeset View
Standalone View
Standalone View
source/soundmanager/SoundManager.cpp
Show All 26 Lines | |||||
#include "lib/external_libraries/libsdl.h" | #include "lib/external_libraries/libsdl.h" | ||||
#include "ps/CLogger.h" | #include "ps/CLogger.h" | ||||
#include "ps/CStr.h" | #include "ps/CStr.h" | ||||
#include "ps/ConfigDB.h" | #include "ps/ConfigDB.h" | ||||
#include "ps/Filesystem.h" | #include "ps/Filesystem.h" | ||||
#include "ps/Profiler2.h" | #include "ps/Profiler2.h" | ||||
#include "ps/XML/Xeromyces.h" | #include "ps/XML/Xeromyces.h" | ||||
#include <thread> | |||||
ISoundManager* g_SoundManager = NULL; | ISoundManager* g_SoundManager = NULL; | ||||
#define SOURCE_NUM 64 | #define SOURCE_NUM 64 | ||||
#if CONFIG2_AUDIO | #if CONFIG2_AUDIO | ||||
class CSoundManagerWorker | class CSoundManagerWorker | ||||
{ | { | ||||
NONCOPYABLE(CSoundManagerWorker); | NONCOPYABLE(CSoundManagerWorker); | ||||
public: | public: | ||||
CSoundManagerWorker() | CSoundManagerWorker() | ||||
{ | { | ||||
m_Items = new ItemsList; | m_Items = new ItemsList; | ||||
m_DeadItems = new ItemsList; | m_DeadItems = new ItemsList; | ||||
m_Shutdown = false; | m_Shutdown = false; | ||||
int ret = pthread_create(&m_WorkerThread, NULL, &RunThread, this); | m_WorkerThread = std::thread(RunThread, this); | ||||
ENSURE(ret == 0); | |||||
} | } | ||||
~CSoundManagerWorker() | ~CSoundManagerWorker() | ||||
{ | { | ||||
delete m_Items; | delete m_Items; | ||||
CleanupItems(); | CleanupItems(); | ||||
delete m_DeadItems; | delete m_DeadItems; | ||||
} | } | ||||
Show All 9 Lines | bool Shutdown() | ||||
while (lstr != m_Items->end()) | while (lstr != m_Items->end()) | ||||
{ | { | ||||
delete *lstr; | delete *lstr; | ||||
++lstr; | ++lstr; | ||||
} | } | ||||
} | } | ||||
pthread_join(m_WorkerThread, NULL); | m_WorkerThread.join(); | ||||
return true; | return true; | ||||
} | } | ||||
void addItem(ISoundItem* anItem) | void addItem(ISoundItem* anItem) | ||||
{ | { | ||||
std::lock_guard<std::mutex> lock(m_WorkerMutex); | std::lock_guard<std::mutex> lock(m_WorkerMutex); | ||||
m_Items->push_back(anItem); | m_Items->push_back(anItem); | ||||
Show All 10 Lines | while (deadItems != m_DeadItems->end()) | ||||
++deadItems; | ++deadItems; | ||||
AL_CHECK; | AL_CHECK; | ||||
} | } | ||||
m_DeadItems->clear(); | m_DeadItems->clear(); | ||||
} | } | ||||
private: | private: | ||||
static void* RunThread(void* data) | static void RunThread(CSoundManagerWorker* data) | ||||
{ | { | ||||
debug_SetThreadName("CSoundManagerWorker"); | debug_SetThreadName("CSoundManagerWorker"); | ||||
g_Profiler2.RegisterCurrentThread("soundmanager"); | g_Profiler2.RegisterCurrentThread("soundmanager"); | ||||
static_cast<CSoundManagerWorker*>(data)->Run(); | data->Run(); | ||||
return NULL; | |||||
} | } | ||||
void Run() | void Run() | ||||
{ | { | ||||
while (true) | while (true) | ||||
{ | { | ||||
// Handle shutdown requests as soon as possible | // Handle shutdown requests as soon as possible | ||||
if (GetShutdown()) | if (GetShutdown()) | ||||
▲ Show 20 Lines • Show All 41 Lines • ▼ Show 20 Lines | private: | ||||
bool GetShutdown() | bool GetShutdown() | ||||
{ | { | ||||
std::lock_guard<std::mutex> lock(m_WorkerMutex); | std::lock_guard<std::mutex> lock(m_WorkerMutex); | ||||
return m_Shutdown; | return m_Shutdown; | ||||
} | } | ||||
private: | private: | ||||
// Thread-related members: | // Thread-related members: | ||||
pthread_t m_WorkerThread; | std::thread m_WorkerThread; | ||||
std::mutex m_WorkerMutex; | std::mutex m_WorkerMutex; | ||||
std::mutex m_DeadItemsMutex; | std::mutex m_DeadItemsMutex; | ||||
// Shared by main thread and worker thread: | // Shared by main thread and worker thread: | ||||
// These variables are all protected by a mutexes | // These variables are all protected by a mutexes | ||||
ItemsList* m_Items; | ItemsList* m_Items; | ||||
ItemsList* m_DeadItems; | ItemsList* m_DeadItems; | ||||
▲ Show 20 Lines • Show All 640 Lines • Show Last 20 Lines |
Wildfire Games · Phabricator