mirror of https://github.com/deavmi/eventy.git
Removed TODO comment
Added configuration system Introduced two modes of "sleeping" the event loop, sleep-based and yield-based (with 200ms sleep-mode as the default) Disabled `setSleep(Duration)` for now
This commit is contained in:
parent
b67f61b848
commit
03dfe3ccfd
|
@ -0,0 +1,30 @@
|
||||||
|
module eventy.config;
|
||||||
|
|
||||||
|
import core.thread : Duration, dur;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Configuration system for eventy
|
||||||
|
*
|
||||||
|
* Allows the user to specify certain
|
||||||
|
* tweaks to the engine
|
||||||
|
*/
|
||||||
|
public struct EngineSettings
|
||||||
|
{
|
||||||
|
/* Agressive lock trying (can starve the loop-check) */
|
||||||
|
bool agressiveTryLock;
|
||||||
|
|
||||||
|
/* Hold-off mode */
|
||||||
|
HoldOffMode holdOffMode;
|
||||||
|
|
||||||
|
/* If `holdOffMode` is `SLEEP` then set the duration for the sleep */
|
||||||
|
Duration sleepTime;
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
*
|
||||||
|
*/
|
||||||
|
public enum HoldOffMode
|
||||||
|
{
|
||||||
|
YIELD,
|
||||||
|
SLEEP
|
||||||
|
}
|
|
@ -3,6 +3,7 @@ module eventy.engine;
|
||||||
import eventy.queues : Queue;
|
import eventy.queues : Queue;
|
||||||
import eventy.signal : Signal;
|
import eventy.signal : Signal;
|
||||||
import eventy.event : Event;
|
import eventy.event : Event;
|
||||||
|
import eventy.config;
|
||||||
|
|
||||||
import std.container.dlist;
|
import std.container.dlist;
|
||||||
import core.sync.mutex : Mutex;
|
import core.sync.mutex : Mutex;
|
||||||
|
@ -94,19 +95,42 @@ public final class Engine : Thread
|
||||||
private DList!(Signal) handlers;
|
private DList!(Signal) handlers;
|
||||||
private Mutex handlerLock;
|
private Mutex handlerLock;
|
||||||
|
|
||||||
private Duration sleepTime;
|
|
||||||
|
/* Engine configuration */
|
||||||
|
private EngineSettings settings;
|
||||||
|
|
||||||
private bool running;
|
private bool running;
|
||||||
|
|
||||||
private DList!(DispatchWrapper) threadStore;
|
private DList!(DispatchWrapper) threadStore;
|
||||||
private Mutex threadStoreLock;
|
private Mutex threadStoreLock;
|
||||||
|
|
||||||
this()
|
this(EngineSettings settings)
|
||||||
{
|
{
|
||||||
super(&run);
|
super(&run);
|
||||||
queueLock = new Mutex();
|
queueLock = new Mutex();
|
||||||
handlerLock = new Mutex();
|
handlerLock = new Mutex();
|
||||||
threadStoreLock = new Mutex();
|
threadStoreLock = new Mutex();
|
||||||
|
|
||||||
|
this.settings = settings;
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Instantiates a new Eventy engine with the default
|
||||||
|
* settings
|
||||||
|
*/
|
||||||
|
this()
|
||||||
|
{
|
||||||
|
EngineSettings defaultSettings;
|
||||||
|
|
||||||
|
/* Yield if a lock fails (prevent potential thread starvation) */
|
||||||
|
defaultSettings.agressiveTryLock = false;
|
||||||
|
|
||||||
|
/* Make the event engine loop sleep (1) and for 200ms (2) (TODO: Adjust this) */
|
||||||
|
defaultSettings.holdOffMode = HoldOffMode.SLEEP;
|
||||||
|
defaultSettings.sleepTime = dur!("msecs")(200);
|
||||||
|
|
||||||
|
this(defaultSettings);
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
|
@ -126,7 +150,7 @@ public final class Engine : Thread
|
||||||
*/
|
*/
|
||||||
public void setSleep(Duration time)
|
public void setSleep(Duration time)
|
||||||
{
|
{
|
||||||
sleepTime = time;
|
// sleepTime = time;
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
|
@ -192,14 +216,22 @@ public final class Engine : Thread
|
||||||
/* Unlock the queue set */
|
/* Unlock the queue set */
|
||||||
queueLock.unlock();
|
queueLock.unlock();
|
||||||
|
|
||||||
|
/* Activate hold off (dependening on the type) */
|
||||||
|
if(settings.holdOffMode == HoldOffMode.YIELD)
|
||||||
|
{
|
||||||
/* Yield to stop mutex starvation */
|
/* Yield to stop mutex starvation */
|
||||||
yield();
|
yield();
|
||||||
|
}
|
||||||
/* TODO: Add yield to stop mutex starvation on a single thread */
|
else if(settings.holdOffMode == HoldOffMode.SLEEP)
|
||||||
|
{
|
||||||
/* Sleep the thread */
|
/* Sleep the thread (for given time) to stop mutex starvation */
|
||||||
// sleepTime = dur!("seconds")(0);
|
sleep(settings.sleepTime);
|
||||||
// sleep(sleepTime);
|
}
|
||||||
|
else
|
||||||
|
{
|
||||||
|
/* This should never happen */
|
||||||
|
assert(false);
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
Loading…
Reference in New Issue