mirror of https://github.com/deavmi/eventy.git
Compare commits
25 Commits
|
@ -0,0 +1,32 @@
|
||||||
|
# This workflow uses actions that are not certified by GitHub.
|
||||||
|
# They are provided by a third-party and are governed by
|
||||||
|
# separate terms of service, privacy policy, and support
|
||||||
|
# documentation.
|
||||||
|
name: D
|
||||||
|
|
||||||
|
on:
|
||||||
|
push:
|
||||||
|
branches: [ "master" ]
|
||||||
|
pull_request:
|
||||||
|
branches: [ "master" ]
|
||||||
|
|
||||||
|
permissions:
|
||||||
|
contents: read
|
||||||
|
|
||||||
|
jobs:
|
||||||
|
build:
|
||||||
|
|
||||||
|
runs-on: ubuntu-latest
|
||||||
|
|
||||||
|
steps:
|
||||||
|
- uses: actions/checkout@v3
|
||||||
|
- uses: dlang-community/setup-dlang@4c99aa991ce7d19dd3064de0a4f2f6b2f152e2d7
|
||||||
|
|
||||||
|
- name: 'Build & Test'
|
||||||
|
run: |
|
||||||
|
# Build the project, with its main file included, without unittests
|
||||||
|
dub build --compiler=$DC
|
||||||
|
# Build and run tests, as defined by `unittest` configuration
|
||||||
|
# In this mode, `mainSourceFile` is excluded and `version (unittest)` are included
|
||||||
|
# See https://dub.pm/package-format-json.html#configurations
|
||||||
|
dub test --compiler=$DC
|
|
@ -14,3 +14,5 @@ eventdisp-test-*
|
||||||
*.obj
|
*.obj
|
||||||
*.lst
|
*.lst
|
||||||
libeventdisp.a
|
libeventdisp.a
|
||||||
|
eventy-test-library
|
||||||
|
libeventy.a
|
||||||
|
|
75
README.md
75
README.md
|
@ -7,50 +7,51 @@ Eventy
|
||||||
|
|
||||||
---
|
---
|
||||||
|
|
||||||
|
[![D](https://github.com/deavmi/eventy/actions/workflows/d.yml/badge.svg)](https://github.com/deavmi/eventy/actions/workflows/d.yml)
|
||||||
|
|
||||||
## Getting started
|
## Getting started
|
||||||
|
|
||||||
### The _engine_
|
### The _engine_
|
||||||
|
|
||||||
The first thing every Eventy-based application will need is an instance of the `Engine`.
|
The first thing every Eventy-based application will need is an instance of the `Engine`.
|
||||||
This provides the user with the basic event-loop functionality that eventy provides. It's
|
This provides the user with a single object instance of the [`Engine` class](https://eventy.dpldocs.info/v0.4.3/eventy.engine.Engine.html) by which
|
||||||
the core of the whole framework that exists to have event-triggers ingested into its
|
the user can register _event types_, _signal handlers_ for said events and the ability
|
||||||
_queues_, checking those _queues_ and one by one dispatching each _signal handler_ that
|
to trigger or _push_ events into the engine.
|
||||||
is associated with each queue on each item in the queue.
|
|
||||||
|
|
||||||
The simplest way to get a new _engine_ up and running is as follow:
|
The simplest way to get a new _engine_ up and running is as follow:
|
||||||
|
|
||||||
```d
|
```d
|
||||||
Engine engine = new Engine();
|
Engine engine = new Engine();
|
||||||
engine.start();
|
|
||||||
```
|
```
|
||||||
|
|
||||||
This will create a new engine initializing all of its internals and then start it as well.
|
This will create a new engine initializing all of its internals such that it is ready for
|
||||||
|
use.
|
||||||
|
|
||||||
### Queues
|
### Event types
|
||||||
|
|
||||||
_Queues_ are as they sound, a list containing items. Each queue has a unique ID which we
|
_Event types_ are effectively just numbers. The use of these is to be able to connect events
|
||||||
can choose. The items of each queue will be the _events_ that are pushed into the _engine_.
|
pushed into the engine with their respective signal handlers (which are registered to handle
|
||||||
An _event_ has an ID associated with it which tells the _engine_ which queue it must be
|
one or more event types).
|
||||||
added to!
|
|
||||||
|
|
||||||
Let's create two queues, with IDs `1` and `2`:
|
Let's create two event types, with IDs `1` and `2`:
|
||||||
|
|
||||||
```d
|
```d
|
||||||
engine.addQueue(1);
|
engine.addEventType(new EventType(1));
|
||||||
engine.addQueue(2);
|
engine.addEventType(new EventType(2));
|
||||||
```
|
```
|
||||||
|
|
||||||
This will tell the engine to create two new queues with tags `1` and `2` respectively.
|
This will tell the engine to create two new event types with tags `1` and `2` respectively.
|
||||||
|
|
||||||
### Event handlers
|
### Signal handlers
|
||||||
|
|
||||||
We're almost done. So far we have created a new _engine_ for handling our queues and
|
We're almost done. So far we have created a new _engine_ for handling our event tyoes and
|
||||||
the triggering of events. What is missing is something to _handle those queues_ when
|
the triggering of events. What is missing is something to _handle those event types_ when
|
||||||
they have something added to them, we call this an _"event handler"_ in computer science
|
an event of one of those types is pushed into the engine. Such handlers are referred to as
|
||||||
but this is Eventy, and in Eventy this is known as a `Signal`.
|
_signal handlers_ and in Eventy these are instances of the [`Signal` class](https://eventy.dpldocs.info/v0.4.3/eventy.signal.Signal.html).
|
||||||
|
|
||||||
We're going to create a signal that can handle both the queues and perform the same task
|
We're going to create a signal that can handle both of the event types `1` and `2` that we
|
||||||
for both of them. We do this by creating a class that inherits from the `Signal` base type:
|
registered earlier on. We can do this by creating a class that inherits from the `Signal`
|
||||||
|
base class:
|
||||||
|
|
||||||
```d
|
```d
|
||||||
class SignalHandler1 : Signal
|
class SignalHandler1 : Signal
|
||||||
|
@ -63,15 +64,15 @@ class SignalHandler1 : Signal
|
||||||
public override void handler(Event e)
|
public override void handler(Event e)
|
||||||
{
|
{
|
||||||
import std.stdio;
|
import std.stdio;
|
||||||
writeln("Running event", e.id);
|
writeln("Running event", e.getID());
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
```
|
```
|
||||||
|
|
||||||
We need to tell the `Signal` class two things:
|
We need to tell the `Signal` class two things:
|
||||||
|
|
||||||
1. What _queue IDs_ it will handle
|
1. What _event typess_ it will handle
|
||||||
2. What to _run_ for said queues
|
2. What to _run_ for said event types
|
||||||
|
|
||||||
---
|
---
|
||||||
|
|
||||||
|
@ -92,7 +93,7 @@ two IDs, namely `1` and `2`.
|
||||||
As for _what to run_, that is specified by overriding the `void handler(Event)` method
|
As for _what to run_, that is specified by overriding the `void handler(Event)` method
|
||||||
in the `Signal` class. In our case we make it write to the console the ID of the event
|
in the `Signal` class. In our case we make it write to the console the ID of the event
|
||||||
(which would end up either being `1` or `2` seeing as this handler is only registered
|
(which would end up either being `1` or `2` seeing as this handler is only registered
|
||||||
for those queue IDs).
|
for those event types).
|
||||||
|
|
||||||
```d
|
```d
|
||||||
import std.stdio;
|
import std.stdio;
|
||||||
|
@ -120,8 +121,6 @@ engine.push(eTest);
|
||||||
|
|
||||||
eTest = new Event(2);
|
eTest = new Event(2);
|
||||||
engine.push(eTest);
|
engine.push(eTest);
|
||||||
|
|
||||||
|
|
||||||
```
|
```
|
||||||
|
|
||||||
You will then see something like this:
|
You will then see something like this:
|
||||||
|
@ -138,6 +137,20 @@ Running event1
|
||||||
Running event2
|
Running event2
|
||||||
```
|
```
|
||||||
|
|
||||||
The reason is it depends on which process gets shceduled by the Linux kernel first, this
|
Despite us pushing the events into the engine in the order of `1` and _then_ `2`, the
|
||||||
is because new threads (special types of processes) are spanwed on the dispatch of each
|
scheduling of such threads is up to the Linux kernel and hence one could be run before
|
||||||
event.
|
the other.
|
||||||
|
|
||||||
|
---
|
||||||
|
|
||||||
|
## Release notes
|
||||||
|
|
||||||
|
### `v0.4.3`
|
||||||
|
|
||||||
|
```
|
||||||
|
Completely overhauled Eventy system for the v0.4.3 release
|
||||||
|
|
||||||
|
Removed the event-loop for a better system (for now) whereby we just dispatch signal handlers on the call to `push(Event)`.
|
||||||
|
|
||||||
|
In a future release I hope to bring the event loop back but in a signal-based manner, such that we can support deferred events and priorities and such
|
||||||
|
```
|
||||||
|
|
|
@ -0,0 +1,35 @@
|
||||||
|
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;
|
||||||
|
|
||||||
|
/* Calling `shutdown()` will wait for any pending events to be dispatched before shutting down */
|
||||||
|
bool gracefulShutdown;
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Hold off mode
|
||||||
|
*
|
||||||
|
* Method to use for "sleeping" the event loop
|
||||||
|
*/
|
||||||
|
public enum HoldOffMode
|
||||||
|
{
|
||||||
|
YIELD,
|
||||||
|
SLEEP
|
||||||
|
}
|
|
@ -1,36 +1,28 @@
|
||||||
module eventy.engine;
|
module eventy.engine;
|
||||||
|
|
||||||
import eventy.queues : Queue;
|
import eventy.types : EventType;
|
||||||
import eventy.signal : Signal;
|
import eventy.signal : Signal;
|
||||||
import eventy.event : Event;
|
import eventy.event : Event;
|
||||||
|
import eventy.config;
|
||||||
|
import eventy.exceptions;
|
||||||
|
|
||||||
import std.container.dlist;
|
import std.container.dlist;
|
||||||
import core.sync.mutex : Mutex;
|
import core.sync.mutex : Mutex;
|
||||||
import core.thread : Thread, dur, Duration;
|
import core.thread : Thread, dur, Duration;
|
||||||
|
import std.conv : to;
|
||||||
import eventy.exceptions;
|
|
||||||
|
|
||||||
import std.stdio;
|
|
||||||
|
|
||||||
/* TODO: Move elsewhere, this thing thinks it's a delegate in the unit test, idk why */
|
|
||||||
void runner(Event e)
|
|
||||||
{
|
|
||||||
import std.stdio;
|
|
||||||
|
|
||||||
writeln("Running event", e.id);
|
|
||||||
}
|
|
||||||
|
|
||||||
unittest
|
unittest
|
||||||
{
|
{
|
||||||
|
import std.stdio;
|
||||||
|
|
||||||
Engine engine = new Engine();
|
Engine engine = new Engine();
|
||||||
engine.start();
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Let the event engine know what typeIDs are
|
* Let the event engine know what typeIDs are
|
||||||
* allowed to be queued
|
* allowed to be queued
|
||||||
*/
|
*/
|
||||||
engine.addQueue(1);
|
engine.addEventType(new EventType(1));
|
||||||
engine.addQueue(2);
|
engine.addEventType(new EventType(2));
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Create a new Signal Handler that will handles
|
* Create a new Signal Handler that will handles
|
||||||
|
@ -46,9 +38,7 @@ unittest
|
||||||
|
|
||||||
public override void handler(Event e)
|
public override void handler(Event e)
|
||||||
{
|
{
|
||||||
import std.stdio;
|
writeln("Running event", e.getID());
|
||||||
|
|
||||||
writeln("Running event", e.id);
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -68,7 +58,65 @@ unittest
|
||||||
Thread.sleep(dur!("seconds")(2));
|
Thread.sleep(dur!("seconds")(2));
|
||||||
engine.push(eTest);
|
engine.push(eTest);
|
||||||
|
|
||||||
writeln("naai");
|
writeln("done with main thread code");
|
||||||
|
|
||||||
|
while(engine.hasEventsRunning()) {}
|
||||||
|
|
||||||
|
/* TODO: Before shutting down, actually test it out (i.e. all events ran) */
|
||||||
|
engine.shutdown();
|
||||||
|
}
|
||||||
|
|
||||||
|
unittest
|
||||||
|
{
|
||||||
|
import std.stdio;
|
||||||
|
|
||||||
|
EngineSettings customSettings = {holdOffMode: HoldOffMode.YIELD};
|
||||||
|
Engine engine = new Engine(customSettings);
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Let the event engine know what typeIDs are
|
||||||
|
* allowed to be queued
|
||||||
|
*/
|
||||||
|
engine.addEventType(new EventType(1));
|
||||||
|
engine.addEventType(new EventType(2));
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Create a new Signal Handler that will handles
|
||||||
|
* event types `1` and `2` with the given `handler()`
|
||||||
|
* function
|
||||||
|
*/
|
||||||
|
class SignalHandler1 : Signal
|
||||||
|
{
|
||||||
|
this()
|
||||||
|
{
|
||||||
|
super([1, 2]);
|
||||||
|
}
|
||||||
|
|
||||||
|
public override void handler(Event e)
|
||||||
|
{
|
||||||
|
writeln("Running event", e.getID());
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Tell the event engine that I want to register
|
||||||
|
* the following handler for its queues `1` and `2`
|
||||||
|
*/
|
||||||
|
Signal j = new SignalHandler1();
|
||||||
|
engine.addSignalHandler(j);
|
||||||
|
|
||||||
|
Event eTest = new Event(1);
|
||||||
|
engine.push(eTest);
|
||||||
|
|
||||||
|
eTest = new Event(2);
|
||||||
|
engine.push(eTest);
|
||||||
|
|
||||||
|
Thread.sleep(dur!("seconds")(2));
|
||||||
|
engine.push(eTest);
|
||||||
|
|
||||||
|
writeln("done with main thread code");
|
||||||
|
|
||||||
|
while(engine.hasEventsRunning()) {}
|
||||||
|
|
||||||
/* TODO: Before shutting down, actually test it out (i.e. all events ran) */
|
/* TODO: Before shutting down, actually test it out (i.e. all events ran) */
|
||||||
engine.shutdown();
|
engine.shutdown();
|
||||||
|
@ -84,56 +132,95 @@ unittest
|
||||||
* handlers, add signal handlers, among many
|
* handlers, add signal handlers, among many
|
||||||
* other things
|
* other things
|
||||||
*/
|
*/
|
||||||
public final class Engine : Thread
|
public final class Engine
|
||||||
{
|
{
|
||||||
/* TODO: Or use a queue data structure */
|
/* Registered queues */
|
||||||
private DList!(Queue) queues;
|
private DList!(EventType) eventTypes;
|
||||||
private Mutex queueLock;
|
private Mutex eventTypesLock;
|
||||||
|
|
||||||
/* TODO: Or use a queue data structure */
|
/* Registered signal handlers */
|
||||||
private DList!(Signal) handlers;
|
private DList!(Signal) handlers;
|
||||||
private Mutex handlerLock;
|
private Mutex handlerLock;
|
||||||
|
|
||||||
private Duration sleepTime;
|
/* Engine configuration */
|
||||||
|
private EngineSettings settings;
|
||||||
|
|
||||||
|
/* Whether engine is running or not */
|
||||||
private bool running;
|
private bool running;
|
||||||
|
|
||||||
|
/* Dispatched threads */
|
||||||
private DList!(DispatchWrapper) threadStore;
|
private DList!(DispatchWrapper) threadStore;
|
||||||
private Mutex threadStoreLock;
|
private Mutex threadStoreLock;
|
||||||
|
|
||||||
this()
|
/**
|
||||||
|
* Instantiates a new Eventy engine with the provided
|
||||||
|
* configuration
|
||||||
|
*
|
||||||
|
* Params:
|
||||||
|
* settings = The EngineSettings to use
|
||||||
|
*/
|
||||||
|
this(EngineSettings settings)
|
||||||
{
|
{
|
||||||
super(&run);
|
eventTypesLock = new Mutex();
|
||||||
queueLock = new Mutex();
|
|
||||||
handlerLock = new Mutex();
|
handlerLock = new Mutex();
|
||||||
threadStoreLock = new Mutex();
|
threadStoreLock = new Mutex();
|
||||||
|
|
||||||
|
this.settings = settings;
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Set the event loop sleep time
|
* Instantiates a new Eventy engine with the default
|
||||||
*
|
* settings
|
||||||
* The load average will sky rocket if it is 0,
|
*/
|
||||||
* which is just because it is calculated on how
|
this()
|
||||||
* 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;
|
EngineSettings defaultSettings;
|
||||||
|
|
||||||
|
/* Yield if a lock fails (prevent potential thread starvation) */
|
||||||
|
defaultSettings.agressiveTryLock = false;
|
||||||
|
|
||||||
|
// FIXME: Investigate ways to lower load average
|
||||||
|
// /* Make the event engine loop sleep (1) and for 50ms (2) (TODO: Adjust this) */
|
||||||
|
// defaultSettings.holdOffMode = HoldOffMode.SLEEP;
|
||||||
|
// defaultSettings.sleepTime = dur!("msecs")(50);
|
||||||
|
|
||||||
|
/* Use yeilding for most responsiveness */
|
||||||
|
defaultSettings.holdOffMode = HoldOffMode.YIELD;
|
||||||
|
|
||||||
|
/* Do not gracefully shutdown */
|
||||||
|
defaultSettings.gracefulShutdown = false;
|
||||||
|
|
||||||
|
this(defaultSettings);
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Adds the given Signal handler
|
* Returns the current configuration paremeters being
|
||||||
*
|
* used by the engine
|
||||||
* @param e the Signal handler to add
|
*
|
||||||
*/
|
* Returns: The EngineSettings struct
|
||||||
|
*/
|
||||||
|
public EngineSettings getConfig()
|
||||||
|
{
|
||||||
|
return settings;
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Updates the current configuration of the engine
|
||||||
|
*
|
||||||
|
* Params:
|
||||||
|
* newSettings = The new EngineSettings struct to use
|
||||||
|
*/
|
||||||
|
public void setConfig(EngineSettings newSettings)
|
||||||
|
{
|
||||||
|
this.settings = newSettings;
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Attaches a new signal handler to the engine
|
||||||
|
*
|
||||||
|
* Params:
|
||||||
|
* e = the signal handler to add
|
||||||
|
*/
|
||||||
public void addSignalHandler(Signal e)
|
public void addSignalHandler(Signal e)
|
||||||
{
|
{
|
||||||
/* Lock the signal-set */
|
/* Lock the signal-set */
|
||||||
|
@ -146,94 +233,35 @@ public final class Engine : Thread
|
||||||
handlerLock.unlock();
|
handlerLock.unlock();
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Event loop
|
* Shuts down the event engine
|
||||||
*/
|
*/
|
||||||
public void run()
|
|
||||||
{
|
|
||||||
running = true;
|
|
||||||
|
|
||||||
while (running)
|
|
||||||
{
|
|
||||||
/* TODO: Implement me */
|
|
||||||
|
|
||||||
/**
|
|
||||||
* TODO: If lock fails, then yield
|
|
||||||
*/
|
|
||||||
|
|
||||||
/**
|
|
||||||
* Lock the queue-set
|
|
||||||
*
|
|
||||||
* Additionally:
|
|
||||||
* Don't waste time spinning on mutex,
|
|
||||||
* if it is not lockable then yield
|
|
||||||
*/
|
|
||||||
while (!queueLock.tryLock_nothrow())
|
|
||||||
{
|
|
||||||
yield();
|
|
||||||
}
|
|
||||||
|
|
||||||
foreach (Queue queue; queues)
|
|
||||||
{
|
|
||||||
/* If the queue has evenets queued */
|
|
||||||
if (queue.hasEvents())
|
|
||||||
{
|
|
||||||
/* TODO: Add different dequeuing techniques */
|
|
||||||
|
|
||||||
/* Pop the first Event */
|
|
||||||
Event headEvent = queue.popEvent();
|
|
||||||
|
|
||||||
/* Get all signal-handlers for this event type */
|
|
||||||
Signal[] handlersMatched = getSignalsForEvent(headEvent);
|
|
||||||
|
|
||||||
/* Dispatch the signal handlers */
|
|
||||||
dispatch(handlersMatched, headEvent);
|
|
||||||
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
/* 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);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
|
||||||
* Stops the event engine
|
|
||||||
*
|
|
||||||
* TODO: Examine edge cases where this might not work
|
|
||||||
*/
|
|
||||||
public void shutdown()
|
public void shutdown()
|
||||||
{
|
{
|
||||||
/* TODO: Insert a lock here, that dispatch should adhere too as well */
|
/* TODO: Insert a lock here, that dispatch should adhere too as well */
|
||||||
|
|
||||||
/* Stop the loop */
|
/* FIXME: We should prevent adding of queues during shutdown */
|
||||||
running = false;
|
/* FIXME: We should prevent pushing of events during shutdown */
|
||||||
|
|
||||||
|
/* Wait for any pendings events (if configured) */
|
||||||
|
if(settings.gracefulShutdown)
|
||||||
|
{
|
||||||
|
while(hasEventsRunning()) {}
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Dispatch(Signal[] set, Event e)
|
* Creates a new thread per signal and dispatches the event to them
|
||||||
*
|
*
|
||||||
* Creates a new thread per signal and dispatches the event to them
|
* Params:
|
||||||
*
|
* signalSet = The signal handlers to use for dispatching
|
||||||
* TODO: Add ability to dispatch on this thread
|
* e = the Event to be dispatched to each handler
|
||||||
*/
|
*/
|
||||||
private void dispatch(Signal[] signalSet, Event e)
|
private void dispatch(Signal[] signalSet, Event e)
|
||||||
{
|
{
|
||||||
foreach (Signal signal; signalSet)
|
foreach (Signal signal; signalSet)
|
||||||
{
|
{
|
||||||
/* Create a new Thread */
|
/* Create a new Thread */
|
||||||
// Thread handlerThread = getThread(signal, e);
|
|
||||||
DispatchWrapper handlerThread = new DispatchWrapper(signal, e);
|
DispatchWrapper handlerThread = new DispatchWrapper(signal, e);
|
||||||
|
|
||||||
/**
|
/**
|
||||||
|
@ -258,15 +286,20 @@ public final class Engine : Thread
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Store the thread
|
* Adds a thread to the thread store
|
||||||
*
|
*
|
||||||
* TODO: This can only be implemented if we use
|
* Params:
|
||||||
* wrapper threads that exit, and we can signal
|
* t = the thread to add
|
||||||
* removal from thread store then
|
*/
|
||||||
*/
|
|
||||||
private void storeThread(DispatchWrapper t)
|
private void storeThread(DispatchWrapper t)
|
||||||
{
|
{
|
||||||
|
/**
|
||||||
|
* TODO: This can only be implemented if we use
|
||||||
|
* wrapper threads that exit, and we can signal
|
||||||
|
* removal from thread store then
|
||||||
|
*/
|
||||||
|
|
||||||
/* Lock the thread store from editing */
|
/* Lock the thread store from editing */
|
||||||
threadStoreLock.lock();
|
threadStoreLock.lock();
|
||||||
|
|
||||||
|
@ -277,9 +310,12 @@ public final class Engine : Thread
|
||||||
threadStoreLock.unlock();
|
threadStoreLock.unlock();
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Removes a thread from the thread store
|
* Removes a thread from the thread store
|
||||||
*/
|
*
|
||||||
|
* Params:
|
||||||
|
* t = the thread to remove
|
||||||
|
*/
|
||||||
private void removeThread(DispatchWrapper t)
|
private void removeThread(DispatchWrapper t)
|
||||||
{
|
{
|
||||||
/* Lock the thread store from editing */
|
/* Lock the thread store from editing */
|
||||||
|
@ -292,12 +328,35 @@ public final class Engine : Thread
|
||||||
threadStoreLock.unlock();
|
threadStoreLock.unlock();
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* DispatchWrapper
|
* Checks whether or not there are still events
|
||||||
*
|
* running at the time of calling
|
||||||
* Effectively a thread but with the Signal,
|
*
|
||||||
* Event included with clean-up routines
|
* Returns: <code>true</code> if there are events
|
||||||
*/
|
* still running, <code>false</code> otherwise
|
||||||
|
*/
|
||||||
|
public bool hasEventsRunning()
|
||||||
|
{
|
||||||
|
/* Whether there are events running or not */
|
||||||
|
bool has = false;
|
||||||
|
|
||||||
|
/* Lock the thread store */
|
||||||
|
threadStoreLock.lock();
|
||||||
|
|
||||||
|
has = !threadStore.empty();
|
||||||
|
|
||||||
|
/* Unlock the thread store */
|
||||||
|
threadStoreLock.unlock();
|
||||||
|
|
||||||
|
return has;
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* DispatchWrapper
|
||||||
|
*
|
||||||
|
* Effectively a thread but with the Signal,
|
||||||
|
* Event included with clean-up routines
|
||||||
|
*/
|
||||||
private class DispatchWrapper : Thread
|
private class DispatchWrapper : Thread
|
||||||
{
|
{
|
||||||
private Signal signal;
|
private Signal signal;
|
||||||
|
@ -320,14 +379,15 @@ public final class Engine : Thread
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* returns all signal(s) responsible for
|
* Returns all the signal handlers responsible
|
||||||
* handling the type of Event provided
|
* for handling the type of Event provided
|
||||||
*
|
*
|
||||||
* @param e the Event type to match to
|
* Params:
|
||||||
* @returns Signal[] the list of signal
|
* e = the Event type to match to
|
||||||
* handlers that handle event e
|
* Returns: A Signal[] containing each handler
|
||||||
*/
|
* registered to handle type <code>e</code>
|
||||||
|
*/
|
||||||
public Signal[] getSignalsForEvent(Event e)
|
public Signal[] getSignalsForEvent(Event e)
|
||||||
{
|
{
|
||||||
/* Matched handlers */
|
/* Matched handlers */
|
||||||
|
@ -339,7 +399,7 @@ public final class Engine : Thread
|
||||||
/* Find all handlers matching */
|
/* Find all handlers matching */
|
||||||
foreach (Signal signal; handlers)
|
foreach (Signal signal; handlers)
|
||||||
{
|
{
|
||||||
if (signal.handles(e.id))
|
if (signal.handles(e.getID()))
|
||||||
{
|
{
|
||||||
matchedHandlers ~= signal;
|
matchedHandlers ~= signal;
|
||||||
}
|
}
|
||||||
|
@ -351,87 +411,127 @@ public final class Engine : Thread
|
||||||
return matchedHandlers;
|
return matchedHandlers;
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* push(Event e)
|
* Checks if there is a signal handler that handles
|
||||||
*
|
* the given event id
|
||||||
* Provided an Event, `e`, this will enqueue the event
|
*
|
||||||
* to
|
* Params:
|
||||||
*/
|
* id = the event ID to check
|
||||||
|
* Returns: <code>true</code> if a signal handler does
|
||||||
|
* exist, <code>false</code> otherwise
|
||||||
|
*/
|
||||||
|
public bool isSignalExists(ulong id)
|
||||||
|
{
|
||||||
|
return getSignalsForEvent(new Event(id)).length != 0;
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Pushes the given Event into the engine
|
||||||
|
* for eventual dispatch
|
||||||
|
*
|
||||||
|
* Params:
|
||||||
|
* e = the event to push
|
||||||
|
*/
|
||||||
public void push(Event e)
|
public void push(Event e)
|
||||||
{
|
{
|
||||||
Queue matchedQueue = findQueue(e.id);
|
//TODO: New code goes below here
|
||||||
|
/**
|
||||||
|
* What we want to do here is to effectively
|
||||||
|
* wake up a checker thread and also (before that)
|
||||||
|
* perhaps we say what queue was modified
|
||||||
|
*
|
||||||
|
* THEN the checker thread goes to said queue and
|
||||||
|
* executes said event (dispatches it) and then sleep
|
||||||
|
* again till it is interrupted. We need Pids and kill etc for this
|
||||||
|
*
|
||||||
|
* Idea (2)
|
||||||
|
*
|
||||||
|
* If we cannot do a checker thread then we can spwan a thread here
|
||||||
|
* but then we get no control for priorities etc, although actually we could
|
||||||
|
* maybe? It depends, we don't want multiple dispathers at same time then
|
||||||
|
* (A checker thread would ensure we don't get this)
|
||||||
|
*/
|
||||||
|
|
||||||
if (matchedQueue)
|
/* Obtain all signal handlers for the given event */
|
||||||
|
Signal[] handlersMatched = getSignalsForEvent(e);
|
||||||
|
|
||||||
|
/* If we get signal handlers then dispatch them */
|
||||||
|
if(handlersMatched.length)
|
||||||
{
|
{
|
||||||
/* Append to the queue */
|
dispatch(handlersMatched, e);
|
||||||
matchedQueue.add(e);
|
}
|
||||||
|
/* If there are no matching events */
|
||||||
|
else
|
||||||
|
{
|
||||||
|
//TODO: Add default handler support
|
||||||
|
//TODO: Add error throwing in case where not true
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Creates a new queue with the given id
|
* Registers a new EventType with the engine
|
||||||
* and then adds it
|
* and then adds it.
|
||||||
*
|
*
|
||||||
* @param id the id of the new queue to add
|
* Throws EventyException if the id of the given
|
||||||
* @throws EventyException if a queue with
|
* EventType is is already in use by another
|
||||||
* the given id already exists
|
*
|
||||||
*/
|
* Params:
|
||||||
public void addQueue(ulong id)
|
* id = the id of the new event type to add
|
||||||
|
* Throws: EventyException
|
||||||
|
*/
|
||||||
|
public void addEventType(EventType evType)
|
||||||
{
|
{
|
||||||
/* Create a new queue with the given id */
|
/* Lock the event types list */
|
||||||
Queue newQueue = new Queue(id);
|
eventTypesLock.lock();
|
||||||
|
|
||||||
/* Lock the queue collection */
|
|
||||||
queueLock.lock();
|
|
||||||
|
|
||||||
/* If no such queue exists then add it (recursive mutex used) */
|
/* If no such queue exists then add it (recursive mutex used) */
|
||||||
if (!findQueue(id))
|
if (!findEventType(evType.getID()))
|
||||||
{
|
{
|
||||||
/* Add the queue */
|
/* Add the event types list */
|
||||||
queues ~= newQueue;
|
eventTypes ~= evType;
|
||||||
}
|
}
|
||||||
else
|
else
|
||||||
{
|
{
|
||||||
throw new EventyException("Failure to add queue with ID already in use");
|
throw new EventyException("Failure to add EventType with id '"~to!(string)(evType.getID())~"\' as it is already in use");
|
||||||
}
|
}
|
||||||
|
|
||||||
/* Unlock the queue collection */
|
/* Unlock the event types list */
|
||||||
queueLock.unlock();
|
eventTypesLock.unlock();
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Given an id, this will return
|
* Given an if, this will return the EventType
|
||||||
* the Queue associated with said
|
* associated with said id
|
||||||
* id
|
*
|
||||||
*
|
* Params:
|
||||||
* @param id the id of the Queue
|
* id = the id of the EventType
|
||||||
* @returns The Queue if found but
|
* Returns: The EventType if found, otherwise
|
||||||
* null otherwise
|
* <code>null</code>
|
||||||
*/
|
*/
|
||||||
public Queue findQueue(ulong id)
|
public EventType findEventType(ulong id)
|
||||||
{
|
{
|
||||||
/* Lock the queue collection */
|
/* Lock the EventType list */
|
||||||
queueLock.lock();
|
eventTypesLock.lock();
|
||||||
|
|
||||||
/* Find the matching queue */
|
/* Find the matching EventType */
|
||||||
Queue matchedQueue;
|
EventType matchedEventType;
|
||||||
foreach (Queue queue; queues)
|
foreach (EventType eventType; eventTypes)
|
||||||
{
|
{
|
||||||
if (queue.id == id)
|
if (eventType.getID() == id)
|
||||||
{
|
{
|
||||||
matchedQueue = queue;
|
matchedEventType = eventType;
|
||||||
break;
|
break;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
/* Unlock the queue collection */
|
/* Unlock the EventType list */
|
||||||
queueLock.unlock();
|
eventTypesLock.unlock();
|
||||||
|
|
||||||
return matchedQueue;
|
return matchedEventType;
|
||||||
}
|
}
|
||||||
|
|
||||||
/* TODO: Add coumentation */
|
/* TODO: Add coumentation */
|
||||||
public ulong[] getTypes()
|
private ulong[] getTypes()
|
||||||
{
|
{
|
||||||
/* TODO: Implement me */
|
/* TODO: Implement me */
|
||||||
return null;
|
return null;
|
||||||
|
|
|
@ -1,27 +1,34 @@
|
||||||
module eventy.event;
|
module eventy.event;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Event
|
* Event
|
||||||
*
|
*
|
||||||
* FIXME: Rename this to `Trigger`
|
* An Event represents a trigger for a given signal(s)
|
||||||
*
|
* handlers which associate with the given typeID
|
||||||
* An Event represents a trigger for a given signal(s)
|
*/
|
||||||
* handlers which associate with the given typeID
|
|
||||||
*
|
|
||||||
* It can optionally take a payload with it as well
|
|
||||||
*/
|
|
||||||
public class Event
|
public class Event
|
||||||
{
|
{
|
||||||
/**
|
/* The event's type id */
|
||||||
* Creates a new Event, optionally taking with is a
|
private ulong id;
|
||||||
* payload
|
|
||||||
*/
|
/**
|
||||||
this(ulong typeID, ubyte[] payload = null)
|
* Creates a new Event with the given typeID
|
||||||
|
*
|
||||||
|
* Params:
|
||||||
|
* typeID = the new Event's type ID
|
||||||
|
*/
|
||||||
|
this(ulong typeID)
|
||||||
{
|
{
|
||||||
this.id = typeID;
|
this.id = typeID;
|
||||||
this.payload = payload;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
ulong id;
|
/**
|
||||||
ubyte[] payload;
|
* Returns the type ID of this Event
|
||||||
|
*
|
||||||
|
* Returns: The Event's type ID
|
||||||
|
*/
|
||||||
|
public final ulong getID()
|
||||||
|
{
|
||||||
|
return id;
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
|
@ -1,5 +1,10 @@
|
||||||
module eventy.exceptions;
|
module eventy.exceptions;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* EventyException
|
||||||
|
*
|
||||||
|
* An Eventy runtime error
|
||||||
|
*/
|
||||||
public final class EventyException : Exception
|
public final class EventyException : Exception
|
||||||
{
|
{
|
||||||
this(string message)
|
this(string message)
|
||||||
|
|
|
@ -3,5 +3,6 @@ module eventy;
|
||||||
public import eventy.event;
|
public import eventy.event;
|
||||||
public import eventy.exceptions;
|
public import eventy.exceptions;
|
||||||
public import eventy.engine;
|
public import eventy.engine;
|
||||||
public import eventy.queues;
|
public import eventy.types;
|
||||||
public import eventy.signal;
|
public import eventy.signal;
|
||||||
|
public import eventy.config;
|
|
@ -1,75 +0,0 @@
|
||||||
module eventy.queues;
|
|
||||||
|
|
||||||
import eventy.event : Event;
|
|
||||||
import core.sync.mutex : Mutex;
|
|
||||||
import std.container.dlist;
|
|
||||||
import std.range;
|
|
||||||
|
|
||||||
/**
|
|
||||||
* Queue
|
|
||||||
*
|
|
||||||
* Represents a queue with a given ID that can
|
|
||||||
* have Event-s enqueued to it
|
|
||||||
*/
|
|
||||||
public final class Queue
|
|
||||||
{
|
|
||||||
public ulong id;
|
|
||||||
/* TODO: Add queue of Event's here */
|
|
||||||
|
|
||||||
private DList!(Event) queue;
|
|
||||||
private Mutex queueLock;
|
|
||||||
|
|
||||||
|
|
||||||
this(ulong id)
|
|
||||||
{
|
|
||||||
this.id = id;
|
|
||||||
queueLock = new Mutex();
|
|
||||||
}
|
|
||||||
|
|
||||||
public DList!(Event).Range getKak()
|
|
||||||
{
|
|
||||||
return queue[];
|
|
||||||
}
|
|
||||||
|
|
||||||
public void add(Event e)
|
|
||||||
{
|
|
||||||
/* Lock the queue */
|
|
||||||
queueLock.lock();
|
|
||||||
|
|
||||||
queue.insert(e);
|
|
||||||
|
|
||||||
/* Unlock the queue */
|
|
||||||
queueLock.unlock();
|
|
||||||
}
|
|
||||||
|
|
||||||
public bool hasEvents()
|
|
||||||
{
|
|
||||||
bool has;
|
|
||||||
|
|
||||||
/* Lock the queue */
|
|
||||||
queueLock.lock();
|
|
||||||
|
|
||||||
has = !(queue[]).empty();
|
|
||||||
|
|
||||||
/* Unlock the queue */
|
|
||||||
queueLock.unlock();
|
|
||||||
|
|
||||||
return has;
|
|
||||||
}
|
|
||||||
|
|
||||||
public Event popEvent()
|
|
||||||
{
|
|
||||||
Event poppedEvent;
|
|
||||||
|
|
||||||
/* Lock the queue */
|
|
||||||
queueLock.lock();
|
|
||||||
|
|
||||||
poppedEvent = (queue[]).front();
|
|
||||||
queue.removeFront();
|
|
||||||
|
|
||||||
/* Unlock the queue */
|
|
||||||
queueLock.unlock();
|
|
||||||
|
|
||||||
return poppedEvent;
|
|
||||||
}
|
|
||||||
}
|
|
|
@ -0,0 +1,40 @@
|
||||||
|
module eventy.types;
|
||||||
|
|
||||||
|
import eventy.event : Event;
|
||||||
|
import core.sync.mutex : Mutex;
|
||||||
|
import std.container.dlist;
|
||||||
|
import std.range;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* EventType
|
||||||
|
*
|
||||||
|
* Represents a type of event. Every Event has an EventType
|
||||||
|
* and Signal(s)-handlers register to one or more of these
|
||||||
|
* types to handle
|
||||||
|
*/
|
||||||
|
public final class EventType
|
||||||
|
{
|
||||||
|
/* The EventType's ID */
|
||||||
|
private ulong id;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Instantiates a new EventType with the given id
|
||||||
|
*
|
||||||
|
* Params:
|
||||||
|
* id = The EventType's id
|
||||||
|
*/
|
||||||
|
this(ulong id)
|
||||||
|
{
|
||||||
|
this.id = id;
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Returns the id of this EventType
|
||||||
|
*
|
||||||
|
* Returns: The id of this EventType
|
||||||
|
*/
|
||||||
|
public ulong getID()
|
||||||
|
{
|
||||||
|
return id;
|
||||||
|
}
|
||||||
|
}
|
Loading…
Reference in New Issue