From 03dfe3ccfd42972e5316c058d5356e394ebcf828 Mon Sep 17 00:00:00 2001 From: "Tristan B. Velloza Kildaire" Date: Sat, 26 Nov 2022 16:52:21 +0200 Subject: [PATCH] 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 --- source/eventy/config.d | 30 +++++++++++++++++++++++ source/eventy/engine.d | 54 +++++++++++++++++++++++++++++++++--------- 2 files changed, 73 insertions(+), 11 deletions(-) create mode 100644 source/eventy/config.d diff --git a/source/eventy/config.d b/source/eventy/config.d new file mode 100644 index 0000000..05aa5c4 --- /dev/null +++ b/source/eventy/config.d @@ -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 +} \ No newline at end of file diff --git a/source/eventy/engine.d b/source/eventy/engine.d index 4396ce9..e7dfbec 100644 --- a/source/eventy/engine.d +++ b/source/eventy/engine.d @@ -3,6 +3,7 @@ module eventy.engine; import eventy.queues : Queue; import eventy.signal : Signal; import eventy.event : Event; +import eventy.config; import std.container.dlist; import core.sync.mutex : Mutex; @@ -94,19 +95,42 @@ public final class Engine : Thread private DList!(Signal) handlers; private Mutex handlerLock; - private Duration sleepTime; + + /* Engine configuration */ + private EngineSettings settings; private bool running; private DList!(DispatchWrapper) threadStore; private Mutex threadStoreLock; - this() + this(EngineSettings settings) { super(&run); queueLock = new Mutex(); handlerLock = 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) { - sleepTime = time; + // sleepTime = time; } /** @@ -192,14 +216,22 @@ public final class Engine : Thread /* Unlock the queue set */ queueLock.unlock(); - /* Yield to stop mutex starvation */ - yield(); - - /* TODO: Add yield to stop mutex starvation on a single thread */ - - /* Sleep the thread */ - // sleepTime = dur!("seconds")(0); - // sleep(sleepTime); + /* Activate hold off (dependening on the type) */ + if(settings.holdOffMode == HoldOffMode.YIELD) + { + /* Yield to stop mutex starvation */ + yield(); + } + else if(settings.holdOffMode == HoldOffMode.SLEEP) + { + /* Sleep the thread (for given time) to stop mutex starvation */ + sleep(settings.sleepTime); + } + else + { + /* This should never happen */ + assert(false); + } } }