Skip to content
Browse files

Just use an @closed boolean instead of @state == :closed

  • Loading branch information...
1 parent c58e5ff commit fbf6a3f1be1bc6a334a2f01bd564d4a0b95f1a76 @nate nate committed
Showing with 19 additions and 19 deletions.
  1. +8 −8 lib/agent/channel.rb
  2. +1 −1 lib/agent/pop.rb
  3. +2 −2 lib/agent/push.rb
  4. +8 −8 lib/agent/queue.rb
View
16 lib/agent/channel.rb
@@ -22,7 +22,7 @@ def initialize(opts = {})
# Module includes both classes and modules
raise InvalidType unless opts[:type].is_a?(Module)
- @state = :open
+ @closed = false
@name = opts[:name] || Agent::UUID.generate
@max = opts[:size] || 0
@type = opts[:type]
@@ -43,14 +43,14 @@ def queue
# Serialization methods
def marshal_load(ary)
- @state, @name, @max, @type, @direction = *ary
+ @closed, @name, @max, @type, @direction = *ary
@queue = Queues[@name]
- @state = :closed unless @queue
+ @closed = @queue.nil?
self
end
def marshal_dump
- [@state, @name, @max, @type, @direction]
+ [@closed, @name, @max, @type, @direction]
end
@@ -97,15 +97,15 @@ def pop?; queue.pop?; end
def close
@close_mutex.synchronize do
- raise ChannelClosed if @state == :closed
- @state = :closed
+ raise ChannelClosed if @closed
+ @closed = true
@queue.close
@queue = nil
Queues.delete(@name)
end
end
- def closed?; @state == :closed; end
- def open?; @state == :open; end
+ def closed?; @closed; end
+ def open?; !@closed; end
def remove_operations(operations)
# ugly, but it overcomes the race condition without synchronization
View
2 lib/agent/pop.rb
@@ -25,7 +25,7 @@ def closed?
def wait
@mutex.synchronize do
- until received? || closed?
+ until @received || @closed
@cvar.wait(@mutex)
end
return received?
View
4 lib/agent/push.rb
@@ -25,10 +25,10 @@ def closed?
def wait
@mutex.synchronize do
- until sent? || closed?
+ until @sent || @closed
@cvar.wait(@mutex)
end
- raise ChannelClosed if closed?
+ raise ChannelClosed if @closed
end
end
View
16 lib/agent/queue.rb
@@ -8,7 +8,7 @@ class NotImplementedError < Exception; end
attr_reader :queue, :operations, :pushes, :pops, :mutex
def initialize
- @state = :open
+ @closed = false
@queue = []
@@ -42,8 +42,8 @@ def push?
def close
mutex.synchronize do
- raise ChannelClosed if closed?
- @state = :closed
+ raise ChannelClosed if @closed
+ @closed = true
@operations.each{|o| o.close }
@operations.clear
@queue.clear
@@ -54,12 +54,12 @@ def close
end
end
- def closed?; @state == :closed; end
- def open?; @state == :open; end
+ def closed?; @closed; end
+ def open?; !@closed; end
def push(p)
mutex.synchronize do
- raise ChannelClosed if closed?
+ raise ChannelClosed if @closed
operations << p
pushes << p
process
@@ -68,7 +68,7 @@ def push(p)
def pop(p)
mutex.synchronize do
- raise ChannelClosed if closed?
+ raise ChannelClosed if @closed
operations << p
pops << p
process
@@ -77,7 +77,7 @@ def pop(p)
def remove_operations(ops)
mutex.synchronize do
- return if closed?
+ return if @closed
ops.each do |operation|
operations.delete(operation)

0 comments on commit fbf6a3f

Please sign in to comment.
Something went wrong with that request. Please try again.