Improve stdclass.h/cpp support for non-Linux non-Windows platforms

Simplifies having implementation on platform separated files, which is a
pain for platforms which are not Windows but not Linux either (and yet
support pthreads).
Some minor cleanup here and there while we are at it.
This commit is contained in:
David Guillen Fandos 2019-04-27 14:08:43 +02:00
parent ce90d43c34
commit 3692ea7ae6
7 changed files with 155 additions and 237 deletions

View File

@ -82,8 +82,7 @@ bool renderer_enabled = true; // Signals the renderer thread to exit
bool renderer_changed = false; // Signals the renderer thread to switch renderer
#if !defined(TARGET_NO_THREADS)
cResetEvent rs(false,true);
cResetEvent re(false,true);
cResetEvent rs, re;
#endif
int max_idx,max_mvo,max_op,max_pt,max_tr,max_vtx,max_modt, ovrn;

View File

@ -124,7 +124,7 @@ void VDecEnd()
cMutex mtx_rqueue;
TA_context* rqueue;
cResetEvent frame_finished(false, true);
cResetEvent frame_finished;
double last_frame = 0;
u64 last_cyces = 0;

View File

@ -130,109 +130,6 @@ void install_fault_handler (void)
#endif
}
#if !defined(TARGET_NO_THREADS)
//Thread class
cThread::cThread(ThreadEntryFP* function,void* prm)
{
Entry=function;
param=prm;
}
void cThread::Start()
{
verify(hThread == NULL);
if (pthread_create( (pthread_t*)&hThread, NULL, Entry, param))
{
die("Thread creation failed");
}
}
void cThread::WaitToEnd()
{
if (hThread != NULL)
{
pthread_join((pthread_t)hThread,0);
hThread = NULL;
}
}
//End thread class
#endif
//cResetEvent Calss
cResetEvent::cResetEvent(bool State,bool Auto)
{
//sem_init((sem_t*)hEvent, 0, State?1:0);
verify(State==false&&Auto==true);
pthread_mutex_init(&mutx, NULL);
pthread_cond_init(&cond, NULL);
}
cResetEvent::~cResetEvent()
{
//Destroy the event object ?
}
void cResetEvent::Set()//Signal
{
pthread_mutex_lock( &mutx );
state=true;
pthread_cond_signal( &cond);
pthread_mutex_unlock( &mutx );
}
void cResetEvent::Reset()//reset
{
pthread_mutex_lock( &mutx );
state=false;
pthread_mutex_unlock( &mutx );
}
bool cResetEvent::Wait(u32 msec)//Wait for signal , then reset
{
pthread_mutex_lock( &mutx );
if (!state)
{
struct timespec ts;
#if HOST_OS == OS_DARWIN
// OSX doesn't have clock_gettime.
clock_serv_t cclock;
mach_timespec_t mts;
host_get_clock_service(mach_host_self(), CALENDAR_CLOCK, &cclock);
clock_get_time(cclock, &mts);
mach_port_deallocate(mach_task_self(), cclock);
ts.tv_sec = mts.tv_sec;
ts.tv_nsec = mts.tv_nsec;
#else
clock_gettime(CLOCK_REALTIME, &ts);
#endif
ts.tv_sec += msec / 1000;
ts.tv_nsec += (msec % 1000) * 1000000;
while (ts.tv_nsec > 1000000000)
{
ts.tv_nsec -= 1000000000;
ts.tv_sec++;
}
pthread_cond_timedwait( &cond, &mutx, &ts );
}
bool rc = state;
state=false;
pthread_mutex_unlock( &mutx );
return rc;
}
void cResetEvent::Wait()//Wait for signal , then reset
{
pthread_mutex_lock( &mutx );
if (!state)
{
pthread_cond_wait( &cond, &mutx );
}
state=false;
pthread_mutex_unlock( &mutx );
}
//End AutoResetEvent
#include <errno.h>
void VArray2::LockRegion(u32 offset,u32 size)

View File

@ -29,9 +29,9 @@ public:
CustomTexture()
:
#ifndef TARGET_NO_THREADS
loader_thread(loader_thread_func, this),
loader_thread(loader_thread_func, this)
#endif
wakeup_thread(false, true) {}
{}
~CustomTexture() { Terminate(); }
u8* LoadCustomTexture(u32 hash, int& width, int& height);
void LoadCustomTextureAsync(TextureCacheData *texture_data);

View File

@ -4,6 +4,7 @@
#include <sys/stat.h>
#include "types.h"
#include "cfg/cfg.h"
#include "stdclass.h"
#if COMPILER_VC_OR_CLANG_WIN32
@ -153,55 +154,145 @@ bool make_directory(const string& path)
#endif
}
#if 0
//File Enumeration
void FindAllFiles(FileFoundCB* callback,wchar* dir,void* param)
{
WIN32_FIND_DATA FindFileData;
HANDLE hFind = INVALID_HANDLE_VALUE;
wchar DirSpec[MAX_PATH + 1]; // directory specification
DWORD dwError;
// Thread & related platform dependant code
#if !defined(HOST_NO_THREADS)
strncpy (DirSpec, dir, strlen(dir)+1);
//strncat (DirSpec, "\\*", 3);
hFind = FindFirstFile( DirSpec, &FindFileData);
if (hFind == INVALID_HANDLE_VALUE)
{
return;
}
else
{
if ((FindFileData.dwFileAttributes & FILE_ATTRIBUTE_DIRECTORY)==0)
{
callback(FindFileData.cFileName,param);
}
u32 rv;
while ( (rv=FindNextFile(hFind, &FindFileData)) != 0)
{
if ((FindFileData.dwFileAttributes & FILE_ATTRIBUTE_DIRECTORY)==0)
{
callback(FindFileData.cFileName,param);
}
}
dwError = GetLastError();
FindClose(hFind);
if (dwError != ERROR_NO_MORE_FILES)
{
return ;
}
#if HOST_OS==OS_WINDOWS
void cThread::Start() {
verify(hThread == NULL);
hThread = CreateThread(NULL, 0, (LPTHREAD_START_ROUTINE)entry, param, 0, NULL);
ResumeThread(hThread);
}
void cThread::WaitToEnd() {
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;
}
return ;
}
#endif
/*
#include "dc\sh4\rec_v1\compiledblock.h"
#include "dc\sh4\rec_v1\blockmanager.h"
#endif
#if HOST_OS==OS_WINDOWS
cResetEvent::cResetEvent() {
hEvent = CreateEvent(
NULL, // default security attributes
FALSE, // auto-reset event?
FALSE, // initial state is State
NULL // unnamed object
);
}
cResetEvent::~cResetEvent()
{
//Destroy the event object ?
CloseHandle(hEvent);
}
void cResetEvent::Set()//Signal
{
#if defined(DEBUG_THREADS)
Sleep(rand() % 10);
#endif
SetEvent(hEvent);
}
void cResetEvent::Reset()//reset
{
#if defined(DEBUG_THREADS)
Sleep(rand() % 10);
#endif
ResetEvent(hEvent);
}
bool cResetEvent::Wait(u32 msec)//Wait for signal , then reset
{
#if defined(DEBUG_THREADS)
Sleep(rand() % 10);
#endif
return WaitForSingleObject(hEvent,msec) == WAIT_OBJECT_0;
}
void cResetEvent::Wait()//Wait for signal , then reset
{
#if defined(DEBUG_THREADS)
Sleep(rand() % 10);
#endif
WaitForSingleObject(hEvent,(u32)-1);
}
#else
cResetEvent::cResetEvent() {
pthread_mutex_init(&mutx, NULL);
pthread_cond_init(&cond, NULL);
}
cResetEvent::~cResetEvent() {
}
void cResetEvent::Set()//Signal
{
pthread_mutex_lock( &mutx );
state=true;
pthread_cond_signal( &cond);
pthread_mutex_unlock( &mutx );
}
void cResetEvent::Reset()//reset
{
pthread_mutex_lock( &mutx );
state=false;
pthread_mutex_unlock( &mutx );
}
bool cResetEvent::Wait(u32 msec)//Wait for signal , then reset
{
pthread_mutex_lock( &mutx );
if (!state)
{
struct timespec ts;
#if HOST_OS == OS_DARWIN
// OSX doesn't have clock_gettime.
clock_serv_t cclock;
mach_timespec_t mts;
host_get_clock_service(mach_host_self(), CALENDAR_CLOCK, &cclock);
clock_get_time(cclock, &mts);
mach_port_deallocate(mach_task_self(), cclock);
ts.tv_sec = mts.tv_sec;
ts.tv_nsec = mts.tv_nsec;
#else
clock_gettime(CLOCK_REALTIME, &ts);
#endif
ts.tv_sec += msec / 1000;
ts.tv_nsec += (msec % 1000) * 1000000;
while (ts.tv_nsec > 1000000000)
{
ts.tv_nsec -= 1000000000;
ts.tv_sec++;
}
pthread_cond_timedwait( &cond, &mutx, &ts );
}
bool rc = state;
state=false;
pthread_mutex_unlock( &mutx );
return rc;
}
void cResetEvent::Wait()//Wait for signal , then reset
{
pthread_mutex_lock( &mutx );
if (!state)
{
pthread_cond_wait( &cond, &mutx );
}
state=false;
pthread_mutex_unlock( &mutx );
}
#endif
bool VramLockedWrite(u8* address);
bool RamLockedWrite(u8* address,u32* sp);
*/

View File

@ -166,38 +166,41 @@ public:
#if !defined(HOST_NO_THREADS)
typedef void* ThreadEntryFP(void* param);
typedef void* THREADHANDLE;
class cThread
{
class cThread {
private:
ThreadEntryFP* Entry;
ThreadEntryFP* entry;
void* param;
public :
THREADHANDLE hThread;
cThread(ThreadEntryFP* function,void* param);
#if HOST_OS==OS_WINDOWS
HANDLE hThread;
#else
pthread_t *hThread;
#endif
cThread(ThreadEntryFP* function, void* param)
:entry(function), param(param), hThread(NULL) {}
~cThread() { WaitToEnd(); }
void Start();
void WaitToEnd();
};
#endif
//Wait Events
typedef void* EVENTHANDLE;
class cResetEvent
{
private:
#if HOST_OS==OS_WINDOWS
EVENTHANDLE hEvent;
#else
pthread_mutex_t mutx;
pthread_cond_t cond;
bool state;
#endif
public :
bool state;
cResetEvent(bool State,bool Auto);
cResetEvent();
~cResetEvent();
void Set(); //Set state to signaled
void Reset(); //Set state to non signaled

View File

@ -750,78 +750,6 @@ void os_DoEvents()
}
//Windoze Code implementation of commong classes from here and after ..
//Thread class
cThread::cThread(ThreadEntryFP* function,void* prm)
{
Entry=function;
param=prm;
}
void cThread::Start()
{
verify(hThread == NULL);
hThread=CreateThread(NULL,0,(LPTHREAD_START_ROUTINE)Entry,param,0,NULL);
ResumeThread(hThread);
}
void cThread::WaitToEnd()
{
WaitForSingleObject(hThread,INFINITE);
CloseHandle(hThread);
hThread = NULL;
}
//End thread class
//cResetEvent Calss
cResetEvent::cResetEvent(bool State,bool Auto)
{
hEvent = CreateEvent(
NULL, // default security attributes
Auto?FALSE:TRUE, // auto-reset event?
State?TRUE:FALSE, // initial state is State
NULL // unnamed object
);
}
cResetEvent::~cResetEvent()
{
//Destroy the event object ?
CloseHandle(hEvent);
}
void cResetEvent::Set()//Signal
{
#if defined(DEBUG_THREADS)
Sleep(rand() % 10);
#endif
SetEvent(hEvent);
}
void cResetEvent::Reset()//reset
{
#if defined(DEBUG_THREADS)
Sleep(rand() % 10);
#endif
ResetEvent(hEvent);
}
bool cResetEvent::Wait(u32 msec)//Wait for signal , then reset
{
#if defined(DEBUG_THREADS)
Sleep(rand() % 10);
#endif
return WaitForSingleObject(hEvent,msec) == WAIT_OBJECT_0;
}
void cResetEvent::Wait()//Wait for signal , then reset
{
#if defined(DEBUG_THREADS)
Sleep(rand() % 10);
#endif
WaitForSingleObject(hEvent,(u32)-1);
}
//End AutoResetEvent
void VArray2::LockRegion(u32 offset,u32 size)
{
//verify(offset+size<this->size);