remove ThreadYieldRWGate
This commit is contained in:
1 parent
2c8c6ac9b8
commit
e05c38dcf6
1 file changed
-35
@@ -1,35 +0,0 @@
|
||||
using System;
|
||||
using System.Threading;
|
||||
|
||||
namespace mbts.utils {
|
||||
/// <summary>
|
||||
/// Provides mechanisms to ensure that only one write op may execute at a time, and calls to read or write while a write op is underway will Thread.yield until the write-op is completed.
|
||||
/// If no write-op is underway, read-ops may run concurrently.
|
||||
/// </summary>
|
||||
class ThreadYieldRWGate {
|
||||
int _n_write_ops, _n_read_ops;
|
||||
|
||||
public IDisposable read() {
|
||||
while (_n_write_ops > 0)
|
||||
Thread.Yield();
|
||||
Interlocked.Increment(ref _n_read_ops);
|
||||
if (_n_write_ops > 0) {
|
||||
// A write-op was yielding at the same time and beat us to the punch! Try again
|
||||
Interlocked.Decrement(ref _n_read_ops);
|
||||
return read();
|
||||
}
|
||||
return Disposable.Create(() => Interlocked.Decrement(ref _n_read_ops));
|
||||
}
|
||||
|
||||
public IDisposable write() {
|
||||
while (_n_read_ops > 0 || _n_write_ops > 0)
|
||||
Thread.Yield();
|
||||
if (Interlocked.Increment(ref _n_write_ops) > 1 || _n_read_ops > 0) {
|
||||
// Another write/read-op was yielding at the same time and beat us to the punch! Try again
|
||||
Interlocked.Decrement(ref _n_write_ops);
|
||||
return write();
|
||||
}
|
||||
return Disposable.Create(() => Interlocked.Decrement(ref _n_write_ops));
|
||||
}
|
||||
}
|
||||
}
|
||||
Reference in new issue
Block a user