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

View File

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

View File

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