diff --git a/demos/external/sources/mmutils/thread_pool.d b/demos/external/sources/mmutils/thread_pool.d index bd05fe5..ee02974 100644 --- a/demos/external/sources/mmutils/thread_pool.d +++ b/demos/external/sources/mmutils/thread_pool.d @@ -12,11 +12,10 @@ import std.algorithm : map; version = MM_NO_LOGS; // Disable log creation //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 = MM_NO_LOGS; + version = MM_USE_POSIX_THREADS; extern(C) struct FILE { @@ -33,6 +32,7 @@ else version (D_BetterC) { + version (Posix) version = MM_USE_POSIX_THREADS; import bubel.ecs.std; extern (C) __gshared int _d_eh_personality(int, int, size_t, void*, void*) { @@ -55,9 +55,9 @@ else import core.stdc.string; } -////////////////////////////////////////////// -//////////////////// Alloc /////////////////// -////////////////////////////////////////////// +////////////////////////////////////////////////// +//////////////////// Allocator /////////////////// +////////////////////////////////////////////////// T* makeVar(T)(T init) { T* el = cast(T*) malloc(T.sizeof); @@ -120,21 +120,7 @@ long useconds() { 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); - - //time = spec.tv_sec; - //return spec.tv_sec * 1000_000 + spec.tv_nsec / 1000; } else version (Posix) { @@ -146,6 +132,7 @@ long useconds() } else version (Windows) { + //TODO: implement timer on windows /*import core.sys.windows.windows : QueryPerformanceFrequency; __gshared double mul = -1; @@ -243,7 +230,7 @@ version (MM_USE_POSIX_THREADS) version (WebAssembly) { extern(C): - //alias uint time_t; + struct pthread_attr_t { union @@ -259,19 +246,11 @@ version (MM_USE_POSIX_THREADS) uint x; } - /*struct timespec - { - time_t tv_sec; - int tv_nsec; - }*/ - // pthread int pthread_create(pthread_t*, in pthread_attr_t*, void* function(void*), void*); int pthread_join(pthread_t, void**); void pthread_exit(void *retval); - // semaphore.h - //alias sem_t = void*; struct sem_t { shared int[4] __val; @@ -282,8 +261,6 @@ version (MM_USE_POSIX_THREADS) int sem_post(sem_t*); int sem_destroy(sem_t*); int sem_timedwait(sem_t* sem, const timespec* abstime); - //import core.sys.posix.pthread; - //import core.sys.posix.semaphore; } else version (Posix) { @@ -347,7 +324,6 @@ version (MM_USE_POSIX_THREADS) bool tryWait() { - //return true; int ret = sem_trywait(&mutex); return (ret == 0); } @@ -411,91 +387,7 @@ version (MM_USE_POSIX_THREADS) } else version(D_BetterC) { - version(Posix) - { - 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) + version(Windows) { import core.stdc.stdint : uintptr_t; import core.sys.windows.windows; @@ -551,15 +443,13 @@ else version(D_BetterC) case WAIT_TIMEOUT: return false; default: - assert(0);//throw new SyncError( "Unable to wait for semaphore" ); + assert(0, "Unable to wait for semaphore" ); } } void post() { assert(ReleaseSemaphore( handle, 1, null )); - //if ( !ReleaseSemaphore( m_hndl, 1, null ) ) - //throw new SyncError( "Unable to notify semaphore" ); } void destroy() @@ -575,7 +465,6 @@ else version(D_BetterC) th.threadStart(); - //(null); ExitThread(0); return 0; } @@ -591,21 +480,22 @@ else version(D_BetterC) { threadStart = dg; 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() { if ( WaitForSingleObject( handle, INFINITE ) == WAIT_OBJECT_0 )assert(0); CloseHandle( handle ); - //pthread_join(handle, null); + handle = handle.init; threadStart = null; } } } - + else + { + static assert(0, "Platform is unsupported in betterC mode!"); + } } else { @@ -674,7 +564,7 @@ else ///////////////// ThreadPool ///////////////// ////////////////////////////////////////////// -private enum gMaxThreadsNum = 32; +private enum gMaxThreadsNum = 64; alias JobDelegate = void delegate(ThreadData*, JobData*);