Common update:

-added multiple new function to allocate template and add entity
-updated README.md (complete initial version)
-empty components now don't take memory
-fixedd small bug with TestRunner
-added many new tests (HashMap, Vector, EntityMeta, ...)
-added default hashing function to HashMap
-fixed critical bug with adding entities
-fixed small bug with adding entity with remplacement components
-added asserts into code to better bug detection
-small performance improvement for events
-added ComponentRef structure which contain data pointer and componentID
-remove EntityID from Event structure
-now events are handled before removing entiteis
-fixed GDC compilation
-fixed rendering of rotated sprites
-added weapons as separate entities to space ship and others
-added Tower enemy to SpaceInvaders demo
-added Boss to SpaceInvaders demo (boss has four tower attached to it)
-Boss towers shoot multiple bullets upon death
-fixed critical bug with demos switching
-fixed critical bug related to adding/removing entities form inside onAdd/onRemove entity callback
-added animation support
-added particles sypport and particles for firing and explostions, and more
-multithreaded rendering now has same rendering order as singlethreaded
-application automaticallu detect host CPU threads count
-added upgrades to SPaceInvaders demo
-fixed texture memory freeing
-improved documentation
-improved multithreaded performance
-improve shader code
-fixed registration issue
-some additional performance improvements
-added depth and colors to rendering parameters
-jobs now has names corresponding to their systems
-change execute() -> willExecute()
-added EntityMeta structure to speedup getting fetching components form entity
-improved multithreading rendering
-added possibility tio change number of threads runtime
-added bullets collision detection in SpaceInvaders demo
-some CI changes
-added VBO batch rendering (current default, no render mode switch yet)
-fixed camera positioning calculation
-fixed buffer issue with WebGL
-added viewport scalling (at least 300 pixels height). Pixels are scalled if screen is bigger.
-center demos gameplay area
-added fullpage html template for Emscripten build
-added many new sprites to atlas
-fixed critical bug with CPU usage in multithreaded mode
-snake render tile coresponding to body part
-snake is destroyed after collision and emit some particles
-added some functionality to vectors
-fixed documentation issue in Manager.d
-more minor code changes and cleanup
This commit is contained in:
Dawid Masiukiewicz 2020-05-28 16:48:42 +00:00
parent 2ddb97e9ce
commit 024356df9b
62 changed files with 5918 additions and 1673 deletions

View file

@ -1,6 +1,6 @@
module mmutils.thread_pool;
import ecs.atomic;
import bubel.ecs.atomic;
//import core.stdc.stdio;
//import core.stdc.stdlib : free, malloc, realloc;
@ -16,6 +16,7 @@ version (Posix)version = MM_USE_POSIX_THREADS;
version (WebAssembly)
{
version = MM_NO_LOGS;
extern(C) struct FILE
{
@ -32,7 +33,7 @@ else
version (D_BetterC)
{
import ecs.std;
import bubel.ecs.std;
extern (C) __gshared int _d_eh_personality(int, int, size_t, void*, void*)
{
return 0;
@ -181,6 +182,12 @@ void instructionPause()
__builtin_ia32_pause();
}
else version(GNU)
{
import gcc.builtins;
__builtin_ia32_pause();
}
else version (DigitalMars)
{
asm
@ -188,7 +195,6 @@ void instructionPause()
rep;
nop;
}
}
else
{
@ -799,6 +805,7 @@ struct ThreadPool
alias FlushLogsDelegaste = void delegate(ThreadData* threadData, JobLog[] logs); /// Type of delegate to flush logs
FlushLogsDelegaste onFlushLogs; /// User custom delegate to flush logs, if overriden defaultFlushLogs will be used. Can be sset after initialize() call
int logsCacheNum; /// Number of log cache entries. Should be set before setThreadsNum is called
int tryWaitCount = 2000; ///Number of times which tryWait are called before timedWait call. Higher value sets better response but takes CPU time even if there are no jobs.
private:
ThreadData*[gMaxThreadsNum] threadsData; /// Data for threads
align(64) shared int threadsNum; /// Number of threads currentlu accepting jobs
@ -808,6 +815,46 @@ private:
JobData[4] resumeJobs; /// Dummu jobs to resume some thread
public:
static int getCPUCoresCount()
{
version(Windows)
{
import core.sys.windows.winbase : SYSTEM_INFO, GetSystemInfo;
SYSTEM_INFO sysinfo;
GetSystemInfo(&sysinfo);
return sysinfo.dwNumberOfProcessors;
}
else version (linux)
{
version(D_BetterC)
{
import core.sys.posix.unistd : _SC_NPROCESSORS_ONLN, sysconf;
return cast(int)sysconf(_SC_NPROCESSORS_ONLN);
}
else
{
import core.sys.linux.sched : CPU_COUNT, cpu_set_t, sched_getaffinity;
import core.sys.posix.unistd : _SC_NPROCESSORS_ONLN, sysconf;
cpu_set_t set = void;
if (sched_getaffinity(0, cpu_set_t.sizeof, &set) == 0)
{
int count = CPU_COUNT(&set);
if (count > 0)
return cast(uint) count;
}
return cast(int)sysconf(_SC_NPROCESSORS_ONLN);
}
}
else version(Posix)
{
import core.sys.posix.unistd;
return cast(int)sysconf(_SC_NPROCESSORS_ONLN);
}
else return -1;
}
int jobsDoneCount()
{
int sum;
@ -1141,17 +1188,19 @@ public:
foreach (ref log; logs)
{
size += log.name.length; // size of name
size += log.name.length + 1; // size of name
}
char* buffer = cast(char*) malloc(size);
foreach (ref log; logs)
{
char[100] name_buffer;
name_buffer[0 .. log.name.length] = log.name;
name_buffer[log.name.length] = 0;
size_t charWritten = snprintf(buffer + used, size - used,
`{"name":"%s", "pid":1, "tid":%lld, "ph":"X", "ts":%lld, "dur":%lld }, %s`,
log.name.ptr, threadData.threadId + 1, log.time, log.duration, "\n".ptr);
name_buffer.ptr, threadData.threadId + 1, log.time, log.duration, "\n".ptr);
used += charWritten;
}
@ -1447,7 +1496,21 @@ private void threadFunc(ThreadData* threadData)
if (data is null)
{
// Thread does not have own job and can not steal it, so wait for a job
bool ok = threadData.semaphore.timedWait(1_000 + !acceptJobs * 10_000);
int tryWait = 0;
//bool ok = threadData.semaphore.timedWait(1_000 + !acceptJobs * 10_000);
bool ok = true;
while(!threadData.semaphore.tryWait())
{
tryWait++;
if(tryWait>threadPool.tryWaitCount)
{
ok = false;
break;
}
static foreach(i;0..10)instructionPause();
}
if(!ok)ok = threadData.semaphore.timedWait(1_000 + !acceptJobs * 10_000);
if (ok)
{