Added yield to stop starving the mutex on our side. They (API users) could of course still starve it on their side, so they should use functions not to rapidly else event-loop gets no time to dispatch events they add.

This commit is contained in:
Tristan B. Velloza Kildaire 2021-09-07 14:05:31 +02:00
parent a6aa3486ad
commit 217688767a
1 changed files with 33 additions and 1 deletions

View File

@ -6,7 +6,7 @@ import eventy.event : Event;
import std.container.dlist;
import core.sync.mutex : Mutex;
import core.thread : Thread;
import core.thread : Thread, dur, Duration;
import std.stdio;
@ -33,6 +33,9 @@ unittest
Event eTest = new Event(1);
engine.push(eTest);
Thread.sleep(dur!("seconds")(2));
engine.push(eTest);
writeln("naai");
}
@ -56,6 +59,8 @@ public final class Engine : Thread
private DList!(Signal) handlers;
private Mutex handlerLock;
private Duration sleepTime;
this()
{
super(&run);
@ -63,6 +68,26 @@ public final class Engine : Thread
handlerLock = new Mutex();
}
/**
* Set the event loop sleep time
*
* The load average will sky rocket if it is 0,
* which is just because it is calculated on how
* full the run queue is, length but also over time
* and even just one task continousy in it will
* make the average high
*
* Reason why it's always runnable is the process
* (the "thread") is a tight loop with no sleeps
* that would dequeue it from the run queue and/or
* no I/O system calls that would put it into the
* waiting queue
*/
public void setSleep(Duration time)
{
sleepTime = time;
}
public void addSignalHandler(Signal e)
{
/* Lock the signal-set */
@ -109,7 +134,14 @@ 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);
}
}