Created
November 6, 2015 22:24
-
-
Save JAChapmanII/4b9e8bec2e37540194f0 to your computer and use it in GitHub Desktop.
future event log
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
class EventLog { | |
class Event { | |
DateTime when; | |
// can track when event started as well, lets you loop back through and find failed tasks | |
Key key; | |
} | |
Event[] events; | |
int nextPush = 0; | |
int nextPop = 0; | |
private Object _popLock = new Object(); | |
private Object _punhLock = new Object(); | |
EventLog(int totalEvents) { | |
// easy to statically size this ahead of time | |
// if static size, know there's no contention for size | |
// otherwise, you can do circular buffer of count(distinct(key)) | |
events = new Event[totalEvents]; | |
} | |
public Event pop() { | |
Event e; | |
while(!tryPop(ref e)) { | |
Thread.Sleep(100); // can do sempahore or something better than spinwait | |
} | |
if (e.when > now) | |
sleepUntil(e.when); | |
return e; | |
} | |
private bool tryPop(ref Event e) { | |
lock (_popLock) { | |
if(nextPop >= nextPush) | |
return false; | |
e = this.events[nextPop++]; | |
return true; | |
} | |
} | |
public void push(Key key) { | |
Event e = new Event { now() + delay, key }; | |
lock (_pushLock) { | |
this.events[nextPush++] = e; | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Yeah my bang code semaphored its way out of that sleep too.
I thought spinwait wasn't that though. It's when the processor is constantly checking, not sleeping?