Code cleanup and fixes #19

Merged
Mergul merged 5 commits from Demos into master 2021-03-03 18:07:22 +01:00
Showing only changes of commit 1387011b04 - Show all commits

View file

@ -12,11 +12,10 @@ import std.algorithm : map;
version = MM_NO_LOGS; // Disable log creation version = MM_NO_LOGS; // Disable log creation
//version = MM_USE_POSIX_THREADS; // Use posix threads insted of standard library, required for betterC //version = MM_USE_POSIX_THREADS; // Use posix threads insted of standard library, required for betterC
version (Posix)version = MM_USE_POSIX_THREADS;
version (WebAssembly) version (WebAssembly)
{ {
version = MM_NO_LOGS; version = MM_NO_LOGS;
version = MM_USE_POSIX_THREADS;
extern(C) struct FILE extern(C) struct FILE
{ {
@ -33,6 +32,7 @@ else
version (D_BetterC) version (D_BetterC)
{ {
version (Posix) version = MM_USE_POSIX_THREADS;
import bubel.ecs.std; import bubel.ecs.std;
extern (C) __gshared int _d_eh_personality(int, int, size_t, void*, void*) extern (C) __gshared int _d_eh_personality(int, int, size_t, void*, void*)
{ {
@ -55,9 +55,9 @@ else
import core.stdc.string; import core.stdc.string;
} }
////////////////////////////////////////////// //////////////////////////////////////////////////
//////////////////// Alloc /////////////////// //////////////////// Allocator ///////////////////
////////////////////////////////////////////// //////////////////////////////////////////////////
T* makeVar(T)(T init) T* makeVar(T)(T init)
{ {
T* el = cast(T*) malloc(T.sizeof); T* el = cast(T*) malloc(T.sizeof);
@ -120,21 +120,7 @@ long useconds()
{ {
version (WebAssembly) version (WebAssembly)
{ {
//import core.sys.posix.sys.time : gettimeofday, timeval;
/*timeval t;
gettimeofday(&t, null);
return t.tv_sec * 1_000_000 + t.tv_usec;*/
//time_t time;
//timespec spec;
//lock_gettime(CLOCK_REALTIME, &spec);
return cast(long)(emscripten_get_now() * 1000.0); return cast(long)(emscripten_get_now() * 1000.0);
//time = spec.tv_sec;
//return spec.tv_sec * 1000_000 + spec.tv_nsec / 1000;
} }
else version (Posix) else version (Posix)
{ {
@ -146,6 +132,7 @@ long useconds()
} }
else version (Windows) else version (Windows)
{ {
//TODO: implement timer on windows
/*import core.sys.windows.windows : QueryPerformanceFrequency; /*import core.sys.windows.windows : QueryPerformanceFrequency;
__gshared double mul = -1; __gshared double mul = -1;
@ -243,7 +230,7 @@ version (MM_USE_POSIX_THREADS)
version (WebAssembly) version (WebAssembly)
{ {
extern(C): extern(C):
//alias uint time_t;
struct pthread_attr_t struct pthread_attr_t
{ {
union union
@ -259,19 +246,11 @@ version (MM_USE_POSIX_THREADS)
uint x; uint x;
} }
/*struct timespec
{
time_t tv_sec;
int tv_nsec;
}*/
// pthread // pthread
int pthread_create(pthread_t*, in pthread_attr_t*, void* function(void*), void*); int pthread_create(pthread_t*, in pthread_attr_t*, void* function(void*), void*);
int pthread_join(pthread_t, void**); int pthread_join(pthread_t, void**);
void pthread_exit(void *retval); void pthread_exit(void *retval);
// semaphore.h
//alias sem_t = void*;
struct sem_t struct sem_t
{ {
shared int[4] __val; shared int[4] __val;
@ -282,8 +261,6 @@ version (MM_USE_POSIX_THREADS)
int sem_post(sem_t*); int sem_post(sem_t*);
int sem_destroy(sem_t*); int sem_destroy(sem_t*);
int sem_timedwait(sem_t* sem, const timespec* abstime); int sem_timedwait(sem_t* sem, const timespec* abstime);
//import core.sys.posix.pthread;
//import core.sys.posix.semaphore;
} }
else version (Posix) else version (Posix)
{ {
@ -347,7 +324,6 @@ version (MM_USE_POSIX_THREADS)
bool tryWait() bool tryWait()
{ {
//return true;
int ret = sem_trywait(&mutex); int ret = sem_trywait(&mutex);
return (ret == 0); return (ret == 0);
} }
@ -411,91 +387,7 @@ version (MM_USE_POSIX_THREADS)
} }
else version(D_BetterC) else version(D_BetterC)
{ {
version(Posix) version(Windows)
{
import core.sys.posix.pthread;
import core.sys.posix.semaphore;
struct Semaphore
{
sem_t mutex;
void initialize()
{
sem_init(&mutex, 0, 0);
}
void wait()
{
int ret = sem_wait(&mutex);
assert(ret == 0);
}
bool tryWait()
{
//return true;
int ret = sem_trywait(&mutex);
return (ret == 0);
}
bool timedWait(int usecs)
{
timespec tv;
// if there is no such a function look at it: https://stackoverflow.com/questions/5404277/porting-clock-gettime-to-windows
clock_gettime(CLOCK_REALTIME, &tv);
tv.tv_sec += usecs / 1_000_000;
tv.tv_nsec += (usecs % 1_000_000) * 1_000;
int ret = sem_timedwait(&mutex, &tv);
return (ret == 0);
}
void post()
{
int ret = sem_post(&mutex);
assert(ret == 0);
}
void destroy()
{
sem_destroy(&mutex);
}
}
private extern (C) void* threadRunFunc(void* threadVoid)
{
Thread* th = cast(Thread*) threadVoid;
th.threadStart();
pthread_exit(null);
return null;
}
struct Thread
{
alias DG = void delegate();
DG threadStart;
pthread_t handle;
void start(DG dg)
{
threadStart = dg;
int ok = pthread_create(&handle, null, &threadRunFunc, cast(void*)&this);
if(!ok)handle = pthread_t();
//assert(ok == 0);
}
void join()
{
pthread_join(handle, null);
handle = handle.init;
threadStart = null;
}
}
}
else version(Windows)
{ {
import core.stdc.stdint : uintptr_t; import core.stdc.stdint : uintptr_t;
import core.sys.windows.windows; import core.sys.windows.windows;
@ -551,15 +443,13 @@ else version(D_BetterC)
case WAIT_TIMEOUT: case WAIT_TIMEOUT:
return false; return false;
default: default:
assert(0);//throw new SyncError( "Unable to wait for semaphore" ); assert(0, "Unable to wait for semaphore" );
} }
} }
void post() void post()
{ {
assert(ReleaseSemaphore( handle, 1, null )); assert(ReleaseSemaphore( handle, 1, null ));
//if ( !ReleaseSemaphore( m_hndl, 1, null ) )
//throw new SyncError( "Unable to notify semaphore" );
} }
void destroy() void destroy()
@ -575,7 +465,6 @@ else version(D_BetterC)
th.threadStart(); th.threadStart();
//(null);
ExitThread(0); ExitThread(0);
return 0; return 0;
} }
@ -591,21 +480,22 @@ else version(D_BetterC)
{ {
threadStart = dg; threadStart = dg;
handle = cast(HANDLE) _beginthreadex( null, 0, &threadRunFunc, cast(void*)&this, 0, null ); handle = cast(HANDLE) _beginthreadex( null, 0, &threadRunFunc, cast(void*)&this, 0, null );
//int ok = pthread_create(&handle, null, &threadRunFunc, cast(void*)&this);
//assert(handle != null);
} }
void join() void join()
{ {
if ( WaitForSingleObject( handle, INFINITE ) == WAIT_OBJECT_0 )assert(0); if ( WaitForSingleObject( handle, INFINITE ) == WAIT_OBJECT_0 )assert(0);
CloseHandle( handle ); CloseHandle( handle );
//pthread_join(handle, null);
handle = handle.init; handle = handle.init;
threadStart = null; threadStart = null;
} }
} }
} }
else
{
static assert(0, "Platform is unsupported in betterC mode!");
}
} }
else else
{ {
@ -674,7 +564,7 @@ else
///////////////// ThreadPool ///////////////// ///////////////// ThreadPool /////////////////
////////////////////////////////////////////// //////////////////////////////////////////////
private enum gMaxThreadsNum = 32; private enum gMaxThreadsNum = 64;
alias JobDelegate = void delegate(ThreadData*, JobData*); alias JobDelegate = void delegate(ThreadData*, JobData*);