cdvdgigaherz: Use C++11 threads instead of Win32 threads

This commit is contained in:
Jonathan Li 2016-10-25 01:51:36 +01:00
parent d872c0560a
commit cccf216dbb
3 changed files with 30 additions and 36 deletions

View File

@ -17,6 +17,7 @@
#include <cstdio> #include <cstdio>
#include <atomic> #include <atomic>
#include <condition_variable> #include <condition_variable>
#include <thread>
#include "svnrev.h" #include "svnrev.h"
Settings g_settings; Settings g_settings;
@ -26,8 +27,7 @@ void (*newDiscCB)();
static std::mutex s_keepalive_lock; static std::mutex s_keepalive_lock;
static std::condition_variable s_keepalive_cv; static std::condition_variable s_keepalive_cv;
HANDLE hThread_keepAlive = nullptr; static std::thread s_keepalive_thread;
DWORD pidThreadKeepAlive = 0;
#define STRFY(x) #x #define STRFY(x) #x
#define TOSTR(x) STRFY(x) #define TOSTR(x) STRFY(x)
@ -132,7 +132,7 @@ extern s32 prefetch_last_mode;
/////////////////////////////////////////////////////////////////////////////// ///////////////////////////////////////////////////////////////////////////////
// keepAliveThread throws a read event regularly to prevent drive spin down // // keepAliveThread throws a read event regularly to prevent drive spin down //
DWORD CALLBACK keepAliveThread(PVOID param) void keepAliveThread()
{ {
printf(" * CDVD: KeepAlive thread started...\n"); printf(" * CDVD: KeepAlive thread started...\n");
std::unique_lock<std::mutex> guard(s_keepalive_lock); std::unique_lock<std::mutex> guard(s_keepalive_lock);
@ -148,35 +148,31 @@ DWORD CALLBACK keepAliveThread(PVOID param)
} }
printf(" * CDVD: KeepAlive thread finished.\n"); printf(" * CDVD: KeepAlive thread finished.\n");
return 0;
} }
s32 StartKeepAliveThread() bool StartKeepAliveThread()
{ {
s_keepalive_is_open = true; s_keepalive_is_open = true;
hThread_keepAlive = CreateThread(NULL, 0, keepAliveThread, NULL, 0, &pidThreadKeepAlive); try {
if (hThread_keepAlive == nullptr) { s_keepalive_thread = std::thread(keepAliveThread);
} catch (std::system_error &) {
s_keepalive_is_open = false; s_keepalive_is_open = false;
return -1;
} }
SetThreadPriority(hThread_keepAlive, THREAD_PRIORITY_NORMAL); return s_keepalive_is_open;
return 0;
} }
void StopKeepAliveThread() void StopKeepAliveThread()
{ {
if (!s_keepalive_thread.joinable())
return;
{ {
std::lock_guard<std::mutex> guard(s_keepalive_lock); std::lock_guard<std::mutex> guard(s_keepalive_lock);
s_keepalive_is_open = false; s_keepalive_is_open = false;
} }
s_keepalive_cv.notify_one(); s_keepalive_cv.notify_one();
if (WaitForSingleObject(hThread_keepAlive, 5000) == WAIT_TIMEOUT) { s_keepalive_thread.join();
TerminateThread(hThread_keepAlive, 0);
}
CloseHandle(hThread_keepAlive);
} }
/////////////////////////////////////////////////////////////////////////////// ///////////////////////////////////////////////////////////////////////////////
@ -210,7 +206,10 @@ s32 CALLBACK CDVDopen(const char *pTitleFilename)
} }
//setup threading manager //setup threading manager
cdvdStartThread(); if (!cdvdStartThread()) {
src.reset();
return -1;
}
StartKeepAliveThread(); StartKeepAliveThread();
return cdvdRefreshData(); return cdvdRefreshData();

View File

@ -99,7 +99,7 @@ extern bool weAreInNewDiskCB;
extern void (*newDiscCB)(); extern void (*newDiscCB)();
s32 cdvdStartThread(); bool cdvdStartThread();
void cdvdStopThread(); void cdvdStopThread();
s32 cdvdRequestSector(u32 sector, s32 mode); s32 cdvdRequestSector(u32 sector, s32 mode);
s32 cdvdRequestComplete(); s32 cdvdRequestComplete();

View File

@ -16,6 +16,7 @@
#include "CDVD.h" #include "CDVD.h"
#include <atomic> #include <atomic>
#include <condition_variable> #include <condition_variable>
#include <thread>
const s32 prefetch_max_blocks = 16; const s32 prefetch_max_blocks = 16;
s32 prefetch_mode = 0; s32 prefetch_mode = 0;
@ -23,7 +24,7 @@ s32 prefetch_last_lba = 0;
s32 prefetch_last_mode = 0; s32 prefetch_last_mode = 0;
s32 prefetch_left = 0; s32 prefetch_left = 0;
HANDLE hThread = nullptr; static std::thread s_thread;
static std::mutex s_notify_lock; static std::mutex s_notify_lock;
static std::condition_variable s_notify_cv; static std::condition_variable s_notify_cv;
@ -33,8 +34,6 @@ static std::mutex s_cache_lock;
static std::atomic<bool> cdvd_is_open; static std::atomic<bool> cdvd_is_open;
DWORD pidThread = 0;
typedef struct typedef struct
{ {
int lsn; int lsn;
@ -131,7 +130,7 @@ bool cdvdUpdateDiscStatus()
return !ready; return !ready;
} }
DWORD CALLBACK cdvdThread(PVOID param) void cdvdThread()
{ {
printf(" * CDVD: IO thread started...\n"); printf(" * CDVD: IO thread started...\n");
std::unique_lock<std::mutex> guard(s_notify_lock); std::unique_lock<std::mutex> guard(s_notify_lock);
@ -139,7 +138,7 @@ DWORD CALLBACK cdvdThread(PVOID param)
while (cdvd_is_open) { while (cdvd_is_open) {
if (cdvdUpdateDiscStatus()) { if (cdvdUpdateDiscStatus()) {
// Need to sleep some to avoid an aggressive spin that sucks the cpu dry. // Need to sleep some to avoid an aggressive spin that sucks the cpu dry.
Sleep(10); std::this_thread::sleep_for(std::chrono::milliseconds(10));
continue; continue;
} }
@ -198,32 +197,28 @@ DWORD CALLBACK cdvdThread(PVOID param)
} }
} }
printf(" * CDVD: IO thread finished.\n"); printf(" * CDVD: IO thread finished.\n");
return 0;
} }
s32 cdvdStartThread() bool cdvdStartThread()
{ {
cdvd_is_open = true; cdvd_is_open = true;
hThread = CreateThread(NULL, 0, cdvdThread, NULL, 0, &pidThread); try {
s_thread = std::thread(cdvdThread);
if (hThread == nullptr) } catch (std::system_error &) {
return -1; cdvd_is_open = false;
return false;
SetThreadPriority(hThread, THREAD_PRIORITY_NORMAL); }
cdvdCacheReset(); cdvdCacheReset();
return 0; return true;
} }
void cdvdStopThread() void cdvdStopThread()
{ {
cdvd_is_open = false; cdvd_is_open = false;
s_notify_cv.notify_one(); s_notify_cv.notify_one();
if (WaitForSingleObject(hThread, 4000) == WAIT_TIMEOUT) { s_thread.join();
TerminateThread(hThread, 0);
}
CloseHandle(hThread);
} }
s32 cdvdRequestSector(u32 sector, s32 mode) s32 cdvdRequestSector(u32 sector, s32 mode)