2021-08-27 14:53:19 +01:00
|
|
|
module eventy.engine;
|
|
|
|
|
2021-08-27 15:12:48 +01:00
|
|
|
import eventy.queues : Queue;
|
|
|
|
import eventy.signal : Signal;
|
|
|
|
import eventy.event : Event;
|
|
|
|
|
2021-08-31 10:34:41 +01:00
|
|
|
import std.container.dlist;
|
|
|
|
import core.sync.mutex : Mutex;
|
2021-08-27 14:53:19 +01:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Engine
|
|
|
|
*
|
|
|
|
* An instance of this represents an engine that
|
|
|
|
* can, at any time, handle the delivery of new
|
|
|
|
* events, trigger the correct signal handlers
|
|
|
|
* for the respective events, remove signal
|
|
|
|
* handlers, add signal handlers, among many
|
|
|
|
* other things
|
|
|
|
*/
|
|
|
|
public final class Engine
|
|
|
|
{
|
2021-08-27 15:12:48 +01:00
|
|
|
/* TODO: Or use a queue data structure */
|
2021-08-31 10:34:41 +01:00
|
|
|
private DList!(Queue) queues;
|
|
|
|
private Mutex queueLock;
|
2021-08-27 15:12:48 +01:00
|
|
|
|
|
|
|
/* TODO: Or use a queue data structure */
|
2021-08-31 10:34:41 +01:00
|
|
|
private DList!(Signal) handlers;
|
|
|
|
private Mutex handlerLock;
|
2021-08-27 15:12:48 +01:00
|
|
|
|
|
|
|
this()
|
|
|
|
{
|
2021-08-31 10:34:41 +01:00
|
|
|
queueLock = new Mutex();
|
|
|
|
handlerLock = new Mutex();
|
2021-08-27 15:12:48 +01:00
|
|
|
}
|
|
|
|
|
2021-08-28 10:41:21 +01:00
|
|
|
/**
|
|
|
|
* Event loop
|
|
|
|
*/
|
|
|
|
public void run()
|
|
|
|
{
|
|
|
|
while(true)
|
|
|
|
{
|
|
|
|
/* TODO: Implement me */
|
|
|
|
|
|
|
|
/* TODO: Add yield to stop mutex starvation on a single thread */
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-08-27 15:12:48 +01:00
|
|
|
/**
|
|
|
|
* push(Event e)
|
|
|
|
*
|
|
|
|
* Provided an Event, `e`, this will enqueue the event
|
|
|
|
* to
|
|
|
|
*/
|
|
|
|
public void push(Event e)
|
|
|
|
{
|
2021-08-31 10:34:41 +01:00
|
|
|
Queue matchedQueue = findQueue(e.id);
|
|
|
|
|
|
|
|
if(matchedQueue)
|
|
|
|
{
|
|
|
|
/* Append to the queue */
|
|
|
|
matchedQueue.add(e);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
public Queue findQueue(ulong id)
|
|
|
|
{
|
|
|
|
/* Lock the queue collection */
|
|
|
|
queueLock.lock();
|
|
|
|
|
|
|
|
/* Find the matching queue */
|
|
|
|
Queue matchedQueue;
|
|
|
|
foreach(Queue queue; queues)
|
|
|
|
{
|
|
|
|
if(queue.id == id)
|
|
|
|
{
|
|
|
|
matchedQueue = queue;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Unlock the queue collection */
|
|
|
|
queueLock.unlock();
|
2021-08-27 15:12:48 +01:00
|
|
|
|
2021-08-31 10:34:41 +01:00
|
|
|
return matchedQueue;
|
2021-08-27 15:12:48 +01:00
|
|
|
}
|
2021-08-27 14:53:19 +01:00
|
|
|
|
2021-08-27 15:12:48 +01:00
|
|
|
public ulong[] getTypes()
|
|
|
|
{
|
|
|
|
/* TODO: Implement me */
|
|
|
|
return null;
|
|
|
|
}
|
2021-08-27 14:53:19 +01:00
|
|
|
}
|