Last active
August 29, 2015 14:02
-
-
Save seki/5b2163bec0c42005a6a0 to your computer and use it in GitHub Desktop.
YA SizedQueue, uses pattern [key, Range, Array].
This file contains 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 ChannelSpace | |
class ChannelHandle | |
def initialize(ts, size) | |
@ts = ts | |
@size = size | |
@name = Object.new | |
@ts.write([@name, 0, []]) | |
end | |
def write(value) | |
_, _, ary = @ts.take([@name, 0...@size, nil]) | |
ary.push(value) | |
value | |
ensure | |
@ts.write([@name, ary.size, ary]) | |
end | |
def read | |
_, _, ary = @ts.take([@name, 1..@size, nil]) | |
ary.shift | |
ensure | |
@ts.write([@name, ary.size, ary]) | |
end | |
end | |
def initialize | |
@ts = Rinda::TupleSpace.new | |
end | |
def open(size) | |
ChannelHandle.new(@ts, size) | |
end | |
end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment