-
-
Save NDiiong/15bb0c7788a83f479138cd825ebc1cca to your computer and use it in GitHub Desktop.
some messy code of a synchronization gateway that allows only a single thread of execution for all passed callbacks. It allows new callbacks to be enqueued while they are being executed by another thread.
This implementation is almost certainly broken because I didn't care if it compiles at all and if all closure stuff is done correctly, but hop…
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
public class ConcurrentGateway | |
{ | |
private ConcurrentQueue<Action> _workQueue = new ConcurrentQueue<Action>(); | |
private int _writeLock = 0; | |
[ThreadStatic] | |
private static AutoResetEvent _waitEvent = new AutoResetEvent(false); | |
protected static AutoResetEvent GetThreadWaitEvent() | |
{ | |
var ev = _waitEvent; | |
if (ev == null) | |
{ | |
ev = new AutoResetEvent(false); | |
_waitEvent = ev; | |
} | |
return ev; | |
} | |
public void DoSynchronized(Action act) | |
{ | |
bool done = false; | |
var ev = GetThreadWaitEvent(); | |
_workQueue.Enqueue(delegate() //enqueue the work TBD | |
{ | |
act(); | |
done = true; | |
ev.Set(); | |
}); | |
int lk = Interlocked.CompareExchange(ref _writeLock, 1, 0); | |
if (lk == 0) | |
{ //I'm a writer thread, do all the work now | |
Action a2; | |
while(_workQueue.TryDequeue(out a2)) { | |
a2(); | |
} | |
var ov = Interlocked.Exchange(ref _writeLock, 0); | |
System.Diagnostics.Debug.Assert(ov == 1); //release | |
} | |
else | |
{ //i'm not a writer so I have to wait | |
if (lk != 1) throw new Exception("LK: " +lk); | |
while (!done) | |
{ | |
ev.WaitOne(); | |
} | |
} | |
} | |
} | |
//with a dedicated handler thread | |
public class ThreadedConcurrentGateway | |
{ | |
private ConcurrentQueue<Action> _workQueue = new ConcurrentQueue<Action>(); | |
private Thread _worker; | |
public ThreadedConcurrentGateway() | |
{ | |
_worker = new Thread(new ThreadStart(() => { | |
Action act; | |
Console.WriteLine("GW thread started"); | |
while (true) | |
{ | |
if (_workQueue.TryDequeue(out act)) | |
{ | |
act(); | |
} | |
else | |
{ | |
_wakeUp.WaitOne(); | |
} | |
} | |
})); | |
_worker.Start(); | |
} | |
private AutoResetEvent _wakeUp = new AutoResetEvent(false); | |
[ThreadStatic] | |
private static AutoResetEvent _waitEvent = new AutoResetEvent(false); | |
protected static AutoResetEvent GetThreadWaitEvent() | |
{ | |
var ev = _waitEvent; | |
if (ev == null) | |
{ | |
ev = new AutoResetEvent(false); | |
_waitEvent = ev; | |
} | |
return ev; | |
} | |
public void DoSynchronized(Action act) | |
{ | |
bool done = false; | |
var ev = GetThreadWaitEvent(); | |
_workQueue.Enqueue(delegate() //enqueue the work TBD | |
{ | |
act(); | |
done = true; | |
ev.Set(); | |
}); | |
_wakeUp.Set(); | |
while (!done) | |
{ | |
ev.WaitOne(); | |
} | |
} | |
} | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment