Skip to content

Instantly share code, notes, and snippets.

@avdi
Created September 4, 2013 21:00
Show Gist options
  • Star 8 You must be signed in to star a gist
  • Fork 2 You must be signed in to fork a gist
  • Save avdi/6442810 to your computer and use it in GitHub Desktop.
Save avdi/6442810 to your computer and use it in GitHub Desktop.
require "thread"
class BoundedQueue
def initialize(max_size = :infinite)
@lock = Mutex.new
@items = []
@item_available = ConditionVariable.new
@max_size = max_size
@space_available = ConditionVariable.new
end
def push(obj, timeout=:never, &timeout_policy)
timeout_policy ||= -> do
raise "Push timed out"
end
wait_for_condition(
@space_available,
->{!full?},
timeout,
timeout_policy) do
@items.push(obj)
@item_available.signal
end
end
def pop(timeout = :never, &timeout_policy)
timeout_policy ||= ->{nil}
wait_for_condition(
@item_available,
->{@items.any?},
timeout,
timeout_policy) do
@items.shift
end
end
private
def full?
return false if @max_size == :infinite
@max_size <= @items.size
end
def wait_for_condition(
cv, condition_predicate, timeout=:never, timeout_policy=->{nil})
deadline = timeout == :never ? :never : Time.now + timeout
@lock.synchronize do
loop do
cv_timeout = timeout == :never ? nil : deadline - Time.now
if !condition_predicate.call && cv_timeout.to_f >= 0
cv.wait(@lock, cv_timeout)
end
if condition_predicate.call
return yield
elsif deadline == :never || deadline > Time.now
next
else
return timeout_policy.call
end
end
end
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment