first commit

This commit is contained in:
Tristan B. Velloza Kildaire 2021-08-27 15:46:51 +02:00
commit 7e5e13a7de
5 changed files with 211 additions and 0 deletions

15
.gitignore vendored Normal file
View File

@ -0,0 +1,15 @@
.dub
docs.json
__dummy.html
docs/
/eventdisp
eventdisp.so
eventdisp.dylib
eventdisp.dll
eventdisp.a
eventdisp.lib
eventdisp-test-*
*.exe
*.o
*.obj
*.lst

10
dub.json Normal file
View File

@ -0,0 +1,10 @@
{
"authors": [
"Tristan B. Kildaire"
],
"copyright": "Copyright © 2020, Tristan B. Kildaire",
"description": "A minimal D application.",
"license": "proprietary",
"name": "eventdisp",
"targetType": "library"
}

5
dub.selections.json Normal file
View File

@ -0,0 +1,5 @@
{
"fileVersion": 1,
"versions": {
}
}

BIN
libeventdisp.a Normal file

Binary file not shown.

181
source/eventy.d Normal file
View File

@ -0,0 +1,181 @@
module eventy;
import core.thread : Thread;
import std.container.dlist : DList;
import core.sync.mutex : Mutex;
public class EventManager : Thread
{
/* List of signals than we can expect */
private Signal[] signals;
private Mutex signalMutex;
/* Recieved signals */
private DList!(ulong) signalInbox;
private Mutex signalInboxMutex;
this()
{
/* Initialize the signal queue */
}
/**
* Event loop
*/
private void eventLoop()
{
while(true)
{
/* Whether or not there is anything to process */
bool isEmpty;
/* The signal ID to be processed */
ulong currentID;
/* Lock the signal inbox */
signalInboxMutex.lock();
/* Check whether or not it is empty */
isEmpty = signalInbox.empty();
/* If it is not empty then dequeue a signal */
if(!isEmpty)
{
currentID = signalInbox.front();
signalInbox.removeFront(1);
}
/* Unlock the signal inbox */
signalInboxMutex.unlock();
/* Only process if we got something */
if(!isEmpty)
{
/* Find the matching Signal */
Signal matchingSignal = findSignal(currentID);
/* Get the Signal's Event(s) */
Event[] signalHandlers = matchingSignal.getEvents();
/* Dispatch all events */
foreach(Event signalHandler; signalHandlers)
{
/* Dispatch the current signal handler */
signalHandler.run();
}
}
}
}
/**
* Attach a new Signal to the event loop that
* can then be triggered with signal(ulong id)
*/
public void attachSignal(Signal signal)
{
}
private Signal findSignal(ulong id)
{
/* The matching Signal */
Signal matchingSignal;
/* Lock the signal registry */
signalMutex.lock();
/* Find the matching Signal */
foreach(Signal signal; signals)
{
if(signal.getID() == id)
{
matchingSignal = signal;
break;
}
}
/* Unllcok the signal registry */
signalMutex.unlock();
return matchingSignal;
}
/*
* Sends a signal by putting it in the signal queue
*
*/
public void signal(ulong id)
{
}
}
/**
* Signal
*
* A signal is associates a unique number (used to trigger
* the signal) with an event to be run on reception of the
* signal
*/
public final class Signal
{
/* The Event to trigger on reception of this signal */
private Event[] triggerEvents;
/* Unique signal ID */
private ulong ID;
/* Get the events */
public Event[] getEvents()
{
return triggerEvents;
}
/* Get this Signal's unique identifier */
public ulong getID()
{
return ID;
}
}
public final class Event
{
/* The function to call when this event is ran */
private void function() handler;
/* Whether or not a new thread should be spwaned for running this event */
private bool spawnThread;
/**
* Constructs a new Event with the given function to be
* run, `handler`, and whether or not a new thread should
* be spawned to run the handler, `spawnThread`, which
* is `false` by default
*/
this(void function() handler, bool spawnThread = false)
{
this.handler = handler;
/* TODO: Handle `spwanThread` = true */
this.spawnThread = spawnThread;
}
public void run()
{
/* Call on this thread if spawnThread = false */
if(!spawnThread)
{
handler();
}
/* Call on a new thread if spawnThread = true */
else
{
/* Create the new thread with the handler function */
Thread eventHandlerThread = new Thread(handler);
/* Start the thread */
eventHandlerThread.start();
}
}
}