Last active
February 4, 2019 21:55
-
-
Save dminuoso/8be273ff2472bea943dba28714218195 to your computer and use it in GitHub Desktop.
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 TBQueue | |
def initialize size | |
tvar = Concurrent::TVar | |
@read = tvar.new [] | |
@write = tvar.new [] | |
@rsize = tvar.new 0 | |
@wsize = tvar.new size | |
end | |
def write(a) | |
w = t.read(@wsize) | |
if w != 0 | |
t.write(@wsize, w - 1) | |
else | |
r = t.read(@rsize) | |
if r != 0 | |
t.write(@rsize, 0) | |
t.write(@wsize, r - 1) | |
else | |
retry_transaction | |
end | |
end | |
as = t.read(@write) | |
t.write(@write, [*as, a]) | |
return nil | |
end | |
def read | |
xs = t.read(@read) | |
r = t.read(@rsize) | |
t.write(@rsize, r + 1) | |
if !xs.empty? | |
x, *xs_ = xs | |
t.write(@read, xs_) | |
return x | |
else | |
ys = t.read(@write) | |
if ys.empty? | |
retry_transaction | |
else | |
z, *zs = ys | |
t.write(@write, []) | |
t.write(@read, zs) | |
return z | |
end | |
end | |
end | |
def peek | |
x = read | |
unshift x | |
return x | |
end | |
def unget a | |
r = t.read @rsize | |
if r > 0 | |
t.write @rsize, r - 1 | |
else | |
w = t.read @wsize | |
if w > 0 | |
t.write @wsize, w - 1 | |
else | |
retry_transaction | |
end | |
end | |
xs = t.read @read | |
t.write @read, [a, *xs] | |
return nil | |
end | |
def empty? | |
xs = t.read @read | |
if xs.empty? | |
ys = t.read @write | |
return ys.empty? | |
else | |
return false | |
end | |
end | |
private | |
def t | |
Concurrent::Transaction.current | |
end | |
def retry_transaction | |
Concurrent.abort_transaction | |
end | |
end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment