Skip to content

Commit

Permalink
Merge pull request #42 from deavmi/hotfix/migrate_libsnooze
Browse files Browse the repository at this point in the history
Migrate from libsnooze
  • Loading branch information
deavmi authored Oct 24, 2023
2 parents aca76f0 + 78ba1d8 commit be5043c
Show file tree
Hide file tree
Showing 4 changed files with 41 additions and 105 deletions.
3 changes: 1 addition & 2 deletions dub.json
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,7 @@
"copyright": "Copyright © 2023, Tristan B. Kildaire",
"dependencies": {
"dlog": ">=0.3.19",
"eventy": ">=0.4.0",
"libsnooze": ">=1.3.0-beta"
"eventy": ">=0.4.0"
},
"description": "A sane IRC framework for the D language",
"license": "LGPL-3.0",
Expand Down
7 changes: 0 additions & 7 deletions source/birchwood/client/client.d
Original file line number Diff line number Diff line change
Expand Up @@ -20,8 +20,6 @@ import birchwood.client.receiver : ReceiverThread;
import birchwood.client.sender : SenderThread;
import birchwood.client.events;

import libsnooze.exceptions : SnoozeError;

import dlog;

package __gshared Logger logger;
Expand Down Expand Up @@ -908,11 +906,6 @@ public class Client : Thread
// TODO: Could deallocate here
throw new BirchwoodException(ErrorType.INTERNAL_FAILURE, e.toString());
}
catch(SnoozeError e)
{
// TODO: Coudl deallocate here
throw new BirchwoodException(ErrorType.INTERNAL_FAILURE, e.toString());
}
}
// TODO: Do actual liveliness check here
else
Expand Down
68 changes: 20 additions & 48 deletions source/birchwood/client/receiver.d
Original file line number Diff line number Diff line change
Expand Up @@ -7,13 +7,10 @@ import core.thread : Thread, dur;

import std.container.slist : SList;
import core.sync.mutex : Mutex;
import core.sync.condition : Condition;

import eventy : EventyEvent = Event;

// TODO: Examine the below import which seemingly fixes stuff for libsnooze
import libsnooze.clib;
import libsnooze;

import birchwood.client;
import birchwood.protocol.messages : Message, decodeMessage;
import std.string : indexOf;
Expand Down Expand Up @@ -43,11 +40,10 @@ public final class ReceiverThread : Thread
private Mutex recvQueueLock;

/**
* The libsnooze event to await on which
* when we wake up signals a new message
* to be processed and received
* Condition variable for waking
* up receive queue reader
*/
private Event receiveEvent;
private Condition recvQueueCond;

/**
* The associated IRC client
Expand All @@ -68,9 +64,8 @@ public final class ReceiverThread : Thread
{
super(&recvHandlerFunc);
this.client = client;
this.receiveEvent = new Event();
this.recvQueueLock = new Mutex();
this.receiveEvent.ensure(this);
this.recvQueueCond = new Condition(this.recvQueueLock);
}

/**
Expand All @@ -88,14 +83,11 @@ public final class ReceiverThread : Thread
/* Add to queue */
recvQueue.insertAfter(recvQueue[], encodedMessage);

/* Wake the sleeping message handler */
recvQueueCond.notify();

/* Unlock queue */
recvQueueLock.unlock();

/**
* Wake up all threads waiting on this event
* (if any, and if so it would only be the receiver)
*/
receiveEvent.notifyAll();
}

/**
Expand All @@ -112,34 +104,12 @@ public final class ReceiverThread : Thread
{
while(client.isRunning())
{
// TODO: We could look at libsnooze wait starvation or mutex racing (future thought)

try
{
receiveEvent.wait();
}
catch(InterruptedException e)
{
version(unittest)
{
writeln("wait() interrupted");
}
continue;
}
catch(FatalException e)
{
// TODO: This should crash and end
version(unittest)
{
writeln("wait() had a FATAL error!!!!!!!!!!!");
}
continue;
}


/* Lock the receieve queue */
/* Lock the queue */
recvQueueLock.lock();

/* Sleep till woken (new message) */
recvQueueCond.wait(); // TODO: Check SyncError?

/* Parsed messages */
SList!(Message) currentMessageQueue;

Expand Down Expand Up @@ -241,14 +211,16 @@ public final class ReceiverThread : Thread
*/
public void end()
{
// TODO: See above notes about libsnooze behaviour due
// ... to usage in our context
receiveEvent.notifyAll();
/* Lock the queue */
recvQueueLock.lock();

/* Wake up sleeping thread (so it can exit) */
recvQueueCond.notify();

/* Unlock the queue */
recvQueueLock.unlock();

// Wait on the manager thread to end
join();

// Dispose the eventy event (TODO: We could do this then join for same effect)
receiveEvent.dispose();
}
}
68 changes: 20 additions & 48 deletions source/birchwood/client/sender.d
Original file line number Diff line number Diff line change
Expand Up @@ -7,10 +7,7 @@ import core.thread : Thread, dur;

import std.container.slist : SList;
import core.sync.mutex : Mutex;

// TODO: Examine the below import which seemingly fixes stuff for libsnooze
import libsnooze.clib;
import libsnooze;
import core.sync.condition : Condition;

import birchwood.client;

Expand All @@ -35,11 +32,10 @@ public final class SenderThread : Thread
private Mutex sendQueueLock;

/**
* The libsnooze event to await on which
* when we wake up signals a new message
* to be processed and sent
* Condition variable for waking
* up send queue reader
*/
private Event sendEvent;
private Condition sendQueueCond;

/**
* The associated IRC client
Expand All @@ -60,9 +56,8 @@ public final class SenderThread : Thread
{
super(&sendHandlerFunc);
this.client = client;
this.sendEvent = new Event();
this.sendQueueLock = new Mutex();
this.sendEvent.ensure(this);
this.sendQueueCond = new Condition(this.sendQueueLock);
}

/**
Expand All @@ -80,14 +75,11 @@ public final class SenderThread : Thread
/* Add to queue */
sendQueue.insertAfter(sendQueue[], encodedMessage);

/* Wake the sleeping message handler */
sendQueueCond.notify();

/* Unlock queue */
sendQueueLock.unlock();

/**
* Wake up all threads waiting on this event
* (if any, and if so it would only be the sender)
*/
sendEvent.notifyAll();
}

/**
Expand All @@ -97,36 +89,14 @@ public final class SenderThread : Thread
{
while(client.isRunning())
{
// TODO: We could look at libsnooze wait starvation or mutex racing (future thought)

/* TODO: handle normal messages (xCount with fakeLagInBetween) */

try
{
sendEvent.wait();
}
catch(InterruptedException e)
{
version(unittest)
{
writeln("wait() interrupted");
}
continue;
}
catch(FatalException e)
{
// TODO: This should crash and end
version(unittest)
{
writeln("wait() had a FATAL error!!!!!!!!!!!");
}
continue;
}


/* Lock queue */
/* Lock the queue */
sendQueueLock.lock();

/* Sleep till woken (new message) */
sendQueueCond.wait(); // TODO: Check SyncError?

foreach(ubyte[] message; sendQueue[])
{
client.socket.send(message);
Expand All @@ -146,14 +116,16 @@ public final class SenderThread : Thread
*/
public void end()
{
// TODO: See above notes about libsnooze behaviour due
// ... to usage in our context
sendEvent.notifyAll();
/* Lock the queue */
sendQueueLock.lock();

/* Wake up sleeping thread (so it can exit) */
sendQueueCond.notify();

/* Unlock the queue */
sendQueueLock.unlock();

// Wait on the manager thread to end
join();

// Dispose the eventy event (TODO: We could do this then join for same effect)
sendEvent.dispose();
}
}

0 comments on commit be5043c

Please sign in to comment.