d3d12: Do not detach garbage collection thread

Thanks Neko for the tips.
This commit is contained in:
Vincent Lejeune 2015-08-11 22:04:15 +02:00
commit 9cb7339067
2 changed files with 31 additions and 34 deletions

View file

@ -35,53 +35,51 @@ void SetGetD3DGSFrameCallback(GetGSFrameCb2 value)
GarbageCollectionThread::GarbageCollectionThread() GarbageCollectionThread::GarbageCollectionThread()
{ {
m_isThreadAlive = true;
m_askForTermination = false; m_askForTermination = false;
m_worker = std::thread([this]() { m_worker = std::thread([this]() {
while (m_isThreadAlive) std::unique_lock<std::mutex> lock(m_mutex);
while (!m_askForTermination)
{ {
std::unique_lock<std::mutex> lock(m_mutex); if (!lock)
while (!m_askForTermination)
{ {
CHECK_EMU_STATUS; lock.lock();
continue;
if (!lock)
{
lock.lock();
continue;
}
if (!m_queue.empty())
{
auto func = std::move(m_queue.front());
m_queue.pop();
if (lock) lock.unlock();
func();
continue;
}
cv.wait(lock);
} }
if (!m_queue.empty())
{
auto func = std::move(m_queue.front());
m_queue.pop();
if (lock) lock.unlock();
func();
continue;
}
cv.wait(lock);
} }
m_isThreadAlive = false;
}); });
m_worker.detach();
} }
GarbageCollectionThread::~GarbageCollectionThread() GarbageCollectionThread::~GarbageCollectionThread()
{ {
m_askForTermination = true; {
while (m_isThreadAlive); std::unique_lock<std::mutex> lock(m_mutex);
m_askForTermination = true;
cv.notify_one();
}
m_worker.join();
} }
void GarbageCollectionThread::pushWork(std::function<void()>&& f) void GarbageCollectionThread::pushWork(std::function<void()>&& f)
{ {
std::unique_lock<std::mutex> lock(m_mutex); {
m_queue.push(f); std::unique_lock<std::mutex> lock(m_mutex);
cv.notify_all(); m_queue.push(f);
}
cv.notify_one();
} }
void GarbageCollectionThread::waitForCompletion() void GarbageCollectionThread::waitForCompletion()

View file

@ -215,8 +215,7 @@ struct DataHeap
*/ */
struct GarbageCollectionThread struct GarbageCollectionThread
{ {
bool m_isThreadAlive; std::atomic<bool> m_askForTermination;
bool m_askForTermination;
std::mutex m_mutex; std::mutex m_mutex;
std::condition_variable cv; std::condition_variable cv;
std::queue<std::function<void()> > m_queue; std::queue<std::function<void()> > m_queue;