Skip to content

Commit

Permalink
[sync] fix TEvent
Browse files Browse the repository at this point in the history
  • Loading branch information
yellowsink committed Apr 21, 2024
1 parent 78661f5 commit 0a11e8d
Show file tree
Hide file tree
Showing 2 changed files with 53 additions and 12 deletions.
32 changes: 26 additions & 6 deletions source/rockhopper/sync.d
Original file line number Diff line number Diff line change
Expand Up @@ -50,9 +50,11 @@ shared class TEvent
import std.typecons : Tuple, tuple;
import core.thread.osthread : Thread;

alias ThreadEventsTy = Tuple!(shared(EventDriver), EventID)[typeof(Thread.getThis.id)];

shared bool triggered;
// you may only await an event from the thread that created it, so we need one event per thread
shared Tuple!(shared(EventDriver), EventID)[typeof(Thread.getThis.id)] threadEvents;
shared ThreadEventsTy threadEvents;

import std.stdio;

Expand All @@ -63,15 +65,15 @@ shared class TEvent
foreach (_, tup; threadEvents)
tup[0].events.trigger(tup[1], true);

threadEvents = typeof(threadEvents).init;
threadEvents = ThreadEventsTy.init;
}

synchronized void reset()
{
if (triggered)
{
triggered = false;
threadEvents = typeof(threadEvents).init;
threadEvents = ThreadEventsTy.init;
}
else
{
Expand All @@ -86,9 +88,27 @@ shared class TEvent

auto tid = Thread.getThis.id;

auto ev = eventDriver.events.create();
// TODO: this does not compile?
threadEvents[tid] = tuple(cast(shared) eventDriver, ev);
EventID ev = void; // always assigned

synchronized(this)
{
// while in a synchronized, you can cast away a `shared`
// -- technically you can cast shared away any time, but its safe here :)
// use a pointer else this causes a copy and that screws stuff up.
// we still have to make sure the contents of the AA are shared due to transititivy,
// but this fixes it not liking us trying to do the assignment *at all*
auto tEvs = cast(ThreadEventsTy*) &threadEvents;

if (tid in *tEvs)
{
ev = (*tEvs)[tid][1];
}
else
{
ev = eventDriver.events.create();
(*tEvs)[tid] = tuple(cast(shared) eventDriver, ev);
}
}

waitThreadEvent(ev);

Expand Down
33 changes: 27 additions & 6 deletions testscript.d
Original file line number Diff line number Diff line change
Expand Up @@ -22,11 +22,32 @@ import core.thread.osthread : Thread;
void main()
{
entrypoint({
auto eepyTask = taskify!sleep(dur!"seconds"(5));
auto tBefore = MonoTime.currTime;

eepyTask.waitRes(); // void

writeln("eeped for ", MonoTime.currTime - tBefore);
auto ev = new TEvent;

auto thread2 = new Thread({
entrypoint({
ev.wait();
writeln("yay 2!");
});
}).start();

auto thread3 = new Thread({
entrypoint({
ev.wait();
writeln("yay 3!");
});
}).start();

auto thread4 = new Thread({
entrypoint({
writeln("helo");
Thread.sleep(dur!"seconds"(2));
ev.notify();
});
}).start();

thread2.join();
thread3.join();
thread4.join();
});
}

0 comments on commit 0a11e8d

Please sign in to comment.