Removed all code

This commit is contained in:
Tristan B. Velloza Kildaire 2022-11-22 17:41:09 +02:00
parent b67f61b848
commit 9aea5aa12b
13 changed files with 0 additions and 958 deletions

16
.gitignore vendored
View File

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

165
LICENSE
View File

@ -1,165 +0,0 @@
GNU LESSER GENERAL PUBLIC LICENSE
Version 3, 29 June 2007
Copyright (C) 2007 Free Software Foundation, Inc. <https://fsf.org/>
Everyone is permitted to copy and distribute verbatim copies
of this license document, but changing it is not allowed.
This version of the GNU Lesser General Public License incorporates
the terms and conditions of version 3 of the GNU General Public
License, supplemented by the additional permissions listed below.
0. Additional Definitions.
As used herein, "this License" refers to version 3 of the GNU Lesser
General Public License, and the "GNU GPL" refers to version 3 of the GNU
General Public License.
"The Library" refers to a covered work governed by this License,
other than an Application or a Combined Work as defined below.
An "Application" is any work that makes use of an interface provided
by the Library, but which is not otherwise based on the Library.
Defining a subclass of a class defined by the Library is deemed a mode
of using an interface provided by the Library.
A "Combined Work" is a work produced by combining or linking an
Application with the Library. The particular version of the Library
with which the Combined Work was made is also called the "Linked
Version".
The "Minimal Corresponding Source" for a Combined Work means the
Corresponding Source for the Combined Work, excluding any source code
for portions of the Combined Work that, considered in isolation, are
based on the Application, and not on the Linked Version.
The "Corresponding Application Code" for a Combined Work means the
object code and/or source code for the Application, including any data
and utility programs needed for reproducing the Combined Work from the
Application, but excluding the System Libraries of the Combined Work.
1. Exception to Section 3 of the GNU GPL.
You may convey a covered work under sections 3 and 4 of this License
without being bound by section 3 of the GNU GPL.
2. Conveying Modified Versions.
If you modify a copy of the Library, and, in your modifications, a
facility refers to a function or data to be supplied by an Application
that uses the facility (other than as an argument passed when the
facility is invoked), then you may convey a copy of the modified
version:
a) under this License, provided that you make a good faith effort to
ensure that, in the event an Application does not supply the
function or data, the facility still operates, and performs
whatever part of its purpose remains meaningful, or
b) under the GNU GPL, with none of the additional permissions of
this License applicable to that copy.
3. Object Code Incorporating Material from Library Header Files.
The object code form of an Application may incorporate material from
a header file that is part of the Library. You may convey such object
code under terms of your choice, provided that, if the incorporated
material is not limited to numerical parameters, data structure
layouts and accessors, or small macros, inline functions and templates
(ten or fewer lines in length), you do both of the following:
a) Give prominent notice with each copy of the object code that the
Library is used in it and that the Library and its use are
covered by this License.
b) Accompany the object code with a copy of the GNU GPL and this license
document.
4. Combined Works.
You may convey a Combined Work under terms of your choice that,
taken together, effectively do not restrict modification of the
portions of the Library contained in the Combined Work and reverse
engineering for debugging such modifications, if you also do each of
the following:
a) Give prominent notice with each copy of the Combined Work that
the Library is used in it and that the Library and its use are
covered by this License.
b) Accompany the Combined Work with a copy of the GNU GPL and this license
document.
c) For a Combined Work that displays copyright notices during
execution, include the copyright notice for the Library among
these notices, as well as a reference directing the user to the
copies of the GNU GPL and this license document.
d) Do one of the following:
0) Convey the Minimal Corresponding Source under the terms of this
License, and the Corresponding Application Code in a form
suitable for, and under terms that permit, the user to
recombine or relink the Application with a modified version of
the Linked Version to produce a modified Combined Work, in the
manner specified by section 6 of the GNU GPL for conveying
Corresponding Source.
1) Use a suitable shared library mechanism for linking with the
Library. A suitable mechanism is one that (a) uses at run time
a copy of the Library already present on the user's computer
system, and (b) will operate properly with a modified version
of the Library that is interface-compatible with the Linked
Version.
e) Provide Installation Information, but only if you would otherwise
be required to provide such information under section 6 of the
GNU GPL, and only to the extent that such information is
necessary to install and execute a modified version of the
Combined Work produced by recombining or relinking the
Application with a modified version of the Linked Version. (If
you use option 4d0, the Installation Information must accompany
the Minimal Corresponding Source and Corresponding Application
Code. If you use option 4d1, you must provide the Installation
Information in the manner specified by section 6 of the GNU GPL
for conveying Corresponding Source.)
5. Combined Libraries.
You may place library facilities that are a work based on the
Library side by side in a single library together with other library
facilities that are not Applications and are not covered by this
License, and convey such a combined library under terms of your
choice, if you do both of the following:
a) Accompany the combined library with a copy of the same work based
on the Library, uncombined with any other library facilities,
conveyed under the terms of this License.
b) Give prominent notice with the combined library that part of it
is a work based on the Library, and explaining where to find the
accompanying uncombined form of the same work.
6. Revised Versions of the GNU Lesser General Public License.
The Free Software Foundation may publish revised and/or new versions
of the GNU Lesser General Public License from time to time. Such new
versions will be similar in spirit to the present version, but may
differ in detail to address new problems or concerns.
Each version is given a distinguishing version number. If the
Library as you received it specifies that a certain numbered version
of the GNU Lesser General Public License "or any later version"
applies to it, you have the option of following the terms and
conditions either of that published version or of any later version
published by the Free Software Foundation. If the Library as you
received it does not specify a version number of the GNU Lesser
General Public License, you may choose any version of the GNU Lesser
General Public License ever published by the Free Software Foundation.
If the Library as you received it specifies that a proxy can decide
whether future versions of the GNU Lesser General Public License shall
apply, that proxy's public statement of acceptance of any version is
permanent authorization for you to choose that version for the
Library.

143
README.md
View File

@ -1,143 +0,0 @@
![](logos/logo.png)
Eventy
======
### _Easy-to-use_ event-loop dispatcher framework for D-based applications
---
## Getting started
### 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
the core of the whole framework that exists to have event-triggers ingested into its
_queues_, checking those _queues_ and one by one dispatching each _signal handler_ that
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:
```d
Engine engine = new Engine();
engine.start();
```
This will create a new engine initializing all of its internals and then start it as well.
### Queues
_Queues_ are as they sound, a list containing items. Each queue has a unique ID which we
can choose. The items of each queue will be the _events_ that are pushed into the _engine_.
An _event_ has an ID associated with it which tells the _engine_ which queue it must be
added to!
Let's create two queues, with IDs `1` and `2`:
```d
engine.addQueue(1);
engine.addQueue(2);
```
This will tell the engine to create two new queues with tags `1` and `2` respectively.
### Event handlers
We're almost done. So far we have created a new _engine_ for handling our queues and
the triggering of events. What is missing is something to _handle those queues_ when
they have something added to them, we call this an _"event handler"_ in computer science
but this is Eventy, and in Eventy this is known as a `Signal`.
We're going to create a signal that can handle both the queues and perform the same task
for both of them. We do this by creating a class that inherits from the `Signal` base type:
```d
class SignalHandler1 : Signal
{
this()
{
super([1,2]);
}
public override void handler(Event e)
{
import std.stdio;
writeln("Running event", e.id);
}
}
```
We need to tell the `Signal` class two things:
1. What _queue IDs_ it will handle
2. What to _run_ for said queues
---
The first of these two is very easy, this is what you see in the constructor `this()`:
```d
this()
{
super([1,2]);
}
```
The `super([1,2])` call tells the Signal class that this signal handler handles those
two IDs, namely `1` and `2`.
---
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
(which would end up either being `1` or `2` seeing as this handler is only registered
for those queue IDs).
```d
import std.stdio;
writeln("Running event", e.id);
```
---
We're almost there, trust me. The last thing to do is to register this signal handler
with the engine, we do so as follows:
```d
Signal j = new SignalHandler1();
engine.addSignalHandler(j);
```
### Triggering events
Now comes the fun part, you can add events into the system by _pushing them to the core_
as follows:
```d
Event eTest = new Event(1);
engine.push(eTest);
eTest = new Event(2);
engine.push(eTest);
```
You will then see something like this:
```
Running event1
Running event2
```
or:
```
Running event1
Running event2
```
The reason is it depends on which process gets shceduled by the Linux kernel first, this
is because new threads (special types of processes) are spanwed on the dispatch of each
event.

View File

@ -1,10 +0,0 @@
{
"authors": [
"Tristan B. Kildaire"
],
"copyright": "Copyright © 2020, Tristan B. Kildaire",
"description": "Easy to use event-loop dispatcher mechanism",
"license": "LGPL v3",
"name": "eventy",
"targetType": "library"
}

View File

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

Binary file not shown.

Before

Width:  |  Height:  |  Size: 3.1 KiB

Binary file not shown.

View File

@ -1,445 +0,0 @@
module eventy.engine;
import eventy.queues : Queue;
import eventy.signal : Signal;
import eventy.event : Event;
import std.container.dlist;
import core.sync.mutex : Mutex;
import core.thread : Thread, dur, Duration;
import eventy.exceptions;
import std.stdio;
/* TODO: Move elsewhere, this thing thinks it's a delegate in the unit test, idk why */
private void runner(Event e)
{
import std.stdio;
writeln("Running event", e.id);
}
unittest
{
Engine engine = new Engine();
engine.start();
/**
* Let the event engine know what typeIDs are
* allowed to be queued
*/
engine.addQueue(1);
engine.addQueue(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)
{
import std.stdio;
writeln("Running event", e.id);
}
}
/**
* 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");
/* TODO: Before shutting down, actually test it out (i.e. all events ran) */
engine.shutdown();
}
/**
* 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 : Thread
{
/* TODO: Or use a queue data structure */
private DList!(Queue) queues;
private Mutex queueLock;
/* TODO: Or use a queue data structure */
private DList!(Signal) handlers;
private Mutex handlerLock;
private Duration sleepTime;
private bool running;
private DList!(DispatchWrapper) threadStore;
private Mutex threadStoreLock;
this()
{
super(&run);
queueLock = new Mutex();
handlerLock = new Mutex();
threadStoreLock = 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;
}
/**
* Adds the given Signal handler
*
* @param e the Signal handler to add
*/
public void addSignalHandler(Signal e)
{
/* Lock the signal-set */
handlerLock.lock();
/* Add the new handler */
handlers ~= e;
/* Unlock the signal-set */
handlerLock.unlock();
}
/**
* Event loop
*/
public void run()
{
running = true;
while (running)
{
/* TODO: Implement me */
/**
* 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()
{
/* TODO: Insert a lock here, that dispatch should adhere too as well */
/* Stop the loop */
running = false;
}
/**
* Dispatch(Signal[] set, Event e)
*
* Creates a new thread per signal and dispatches the event to them
*
* TODO: Add ability to dispatch on this thread
*/
private void dispatch(Signal[] signalSet, Event e)
{
foreach (Signal signal; signalSet)
{
/* Create a new Thread */
// Thread handlerThread = getThread(signal, e);
DispatchWrapper handlerThread = new DispatchWrapper(signal, e);
/**
* TODO
*
* When we call `shutdown()` there may very well be a case of
* where the threadStoreLock unlocks after the clean up
* loop, but storeThread hangs here during that time,
* then proceeds to start the thread, we should therefore,
* either block on running changed (solution 1, not as granular)
*
* Solution 2: Block on dispatch being called <- use this method rather
* But still needs a running check, it must not go ahead if running is now
* false
*/
/* Store the thread */
storeThread(handlerThread);
/* Start the thread */
handlerThread.start();
}
}
/**
* Store the thread
*
* TODO: This can only be implemented if we use
* wrapper threads that exit, and we can signal
* removal from thread store then
*/
private void storeThread(DispatchWrapper t)
{
/* Lock the thread store from editing */
threadStoreLock.lock();
/* Add the thread */
threadStore ~= t;
/* Unlock the thread store for editing */
threadStoreLock.unlock();
}
/**
* Removes a thread from the thread store
*/
private void removeThread(DispatchWrapper t)
{
/* Lock the thread store from editing */
threadStoreLock.lock();
/* Remove the thread */
threadStore.linearRemoveElement(t);
/* Unlock the thread store for editing */
threadStoreLock.unlock();
}
/**
* DispatchWrapper
*
* Effectively a thread but with the Signal,
* Event included with clean-up routines
*/
private class DispatchWrapper : Thread
{
private Signal signal;
private Event e;
this(Signal signal, Event e)
{
super(&run);
this.signal = signal;
this.e = e;
}
private void run()
{
/* Run the signal handler */
signal.handler(e);
/* Remove myself from the thread store */
removeThread(this);
}
}
/**
* returns all signal(s) responsible for
* handling the type of Event provided
*
* @param e the Event type to match to
* @returns Signal[] the list of signal
* handlers that handle event e
*/
public Signal[] getSignalsForEvent(Event e)
{
/* Matched handlers */
Signal[] matchedHandlers;
/* Lock the signal-set */
handlerLock.lock();
/* Find all handlers matching */
foreach (Signal signal; handlers)
{
if (signal.handles(e.id))
{
matchedHandlers ~= signal;
}
}
/* Unlock the signal-set */
handlerLock.unlock();
return matchedHandlers;
}
/**
* Checks if there is a Signal that handles the given
* event ID
*/
public bool isSignalExists(ulong id)
{
return getSignalsForEvent(new Event(id)).length != 0;
}
/**
* push(Event e)
*
* Provided an Event, `e`, this will enqueue the event
* to
*/
public void push(Event e)
{
Queue matchedQueue = findQueue(e.id);
if (matchedQueue)
{
/* Append to the queue */
matchedQueue.add(e);
}
}
/**
* Creates a new queue with the given id
* and then adds it
*
* @param id the id of the new queue to add
* @throws EventyException if a queue with
* the given id already exists
*/
public void addQueue(ulong id)
{
/* Create a new queue with the given id */
Queue newQueue = new Queue(id);
/* Lock the queue collection */
queueLock.lock();
/* If no such queue exists then add it (recursive mutex used) */
if (!findQueue(id))
{
/* Add the queue */
queues ~= newQueue;
}
else
{
throw new EventyException("Failure to add queue with ID already in use");
}
/* Unlock the queue collection */
queueLock.unlock();
}
/**
* Given an id, this will return
* the Queue associated with said
* id
*
* @param id the id of the Queue
* @returns The Queue if found but
* null otherwise
*/
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();
return matchedQueue;
}
/* TODO: Add coumentation */
public ulong[] getTypes()
{
/* TODO: Implement me */
return null;
}
}

View File

@ -1,27 +0,0 @@
module eventy.event;
/**
* Event
*
* FIXME: Rename this to `Trigger`
*
* 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
{
/**
* Creates a new Event, optionally taking with is a
* payload
*/
this(ulong typeID, ubyte[] payload = null)
{
this.id = typeID;
this.payload = payload;
}
ulong id;
ubyte[] payload;
}

View File

@ -1,9 +0,0 @@
module eventy.exceptions;
public final class EventyException : Exception
{
this(string message)
{
super(message);
}
}

View File

@ -1,7 +0,0 @@
module eventy;
public import eventy.event;
public import eventy.exceptions;
public import eventy.engine;
public import eventy.queues;
public import eventy.signal;

View File

@ -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;
}
}

View File

@ -1,56 +0,0 @@
module eventy.signal;
import eventy.event : Event;
/**
* Signal
*
* Represents a signal handler that handles a given set of typeIDs
* which means that it contains an associated function to be run
* on handling of a given Event
*/
//alias EventHandler = void function(Event);
public abstract class Signal
{
/* TypeIDs this signal handler associates with */
private ulong[] typeIDs;
/* Signal handler */
//private EventHandler handler;
this(ulong[] typeIDs)
{
this.typeIDs = typeIDs;
}
/**
* Returns true if this signal handles the given typeID
* false otherwise
*/
public bool handles(ulong typeID)
{
/* FIXME: Implement */
foreach(ulong id; typeIDs)
{
if(id == typeID)
{
return true;
}
}
return false;
}
public void registerTypeID(ulong typeID)
{
}
public void deregisterTypeID(ulong typeID)
{
}
public abstract void handler(Event);
}