Merge pull request #73 from scribam/thread-rework

Rework cThread to use std::thread
This commit is contained in:
flyinghead 2020-04-17 10:05:29 +02:00 committed by GitHub
commit ed1a9fabd0
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
6 changed files with 24 additions and 51 deletions

View File

@ -38,6 +38,7 @@ Cartridge *CurrentCartridge;
bool bios_loaded = false;
#ifdef _WIN32
#include <windows.h>
typedef HANDLE fd_t;
#define INVALID_FD INVALID_HANDLE_VALUE
#else

View File

@ -158,38 +158,18 @@ bool make_directory(const std::string& path)
#endif
}
// Thread & related platform dependant code
#if !defined(HOST_NO_THREADS)
void cThread::Start()
{
verify(!thread.joinable());
thread = std::thread(entry, param);
}
#ifdef _WIN32
void cThread::Start() {
verify(hThread == NULL);
hThread = CreateThread(NULL, 0, (LPTHREAD_START_ROUTINE)entry, param, 0, NULL);
ResumeThread(hThread);
}
void cThread::WaitToEnd() {
if (GetCurrentThreadId() != GetThreadId(hThread))
WaitForSingleObject(hThread, INFINITE);
CloseHandle(hThread);
hThread = NULL;
}
#else
void cThread::Start() {
verify(hThread == NULL);
hThread = new pthread_t;
if (pthread_create( hThread, NULL, entry, param))
die("Thread creation failed");
}
void cThread::WaitToEnd() {
if (hThread) {
pthread_join(*hThread,0);
delete hThread;
hThread = NULL;
void cThread::WaitToEnd()
{
if (thread.joinable()) {
thread.join();
}
}
#endif
#endif
cResetEvent::cResetEvent() : state(false)
{

View File

@ -5,12 +5,7 @@
#include <mutex>
#include <algorithm>
#include <cctype>
#ifndef _WIN32
#include <pthread.h>
#else
#include <windows.h>
#endif
#include <thread>
#ifdef __ANDROID__
#include <sys/mman.h>
@ -21,29 +16,22 @@
#define PAGE_MASK (PAGE_SIZE-1)
#endif
//Threads
#if !defined(HOST_NO_THREADS)
typedef void* ThreadEntryFP(void* param);
class cThread {
class cThread
{
private:
typedef void* ThreadEntryFP(void* param);
ThreadEntryFP* entry;
void* param;
public :
#ifdef _WIN32
HANDLE hThread;
#else
pthread_t *hThread;
#endif
public:
std::thread thread;
cThread(ThreadEntryFP* function, void* param)
:entry(function), param(param), hThread(NULL) {}
:entry(function), param(param) {}
~cThread() { WaitToEnd(); }
void Start();
void WaitToEnd();
};
#endif
class cResetEvent
{

View File

@ -1,6 +1,8 @@
#pragma once
#include "input/keyboard_device.h"
#include <windows.h>
// Used to differentiate between main enter key and num keypad one
#define VK_NUMPAD_RETURN 0x0E

View File

@ -1,7 +1,9 @@
#include <xinput.h>
#include "input/gamepad_device.h"
#include "rend/gui.h"
#include <windows.h>
#include <xinput.h>
class XInputMapping : public InputMapping
{
public:

View File

@ -373,7 +373,7 @@ static cThread render_thread(render_thread_func, NULL);
JNIEXPORT void JNICALL Java_com_reicast_emulator_emu_JNIdc_rendinitNative(JNIEnv * env, jobject obj, jobject surface, jint width, jint height)
{
if (render_thread.hThread != NULL)
if (render_thread.thread.joinable())
{
if (surface == NULL)
{