lib/bunny/channel_id_allocator.rb in bunny-0.9.0.pre7 vs lib/bunny/channel_id_allocator.rb in bunny-0.9.0.pre8

- old
+ new

@@ -8,39 +8,38 @@ # API # # @param [Integer] max_channel Max allowed channel id def initialize(max_channel = ((1 << 16) - 1)) - @int_allocator ||= AMQ::IntAllocator.new(1, max_channel) - - @channel_id_mutex ||= Mutex.new + @allocator = AMQ::IntAllocator.new(1, max_channel) + @mutex = Mutex.new end # Returns next available channel id. This method is thread safe. # # @return [Fixnum] # @api public # @see ChannelManager#release_channel_id # @see ChannelManager#reset_channel_id_allocator def next_channel_id - @channel_id_mutex.synchronize do - @int_allocator.allocate + @mutex.synchronize do + @allocator.allocate end end # Releases previously allocated channel id. This method is thread safe. # # @param [Fixnum] i Channel id to release # @api public # @see ChannelManager#next_channel_id # @see ChannelManager#reset_channel_id_allocator def release_channel_id(i) - @channel_id_mutex.synchronize do - @int_allocator.release(i) + @mutex.synchronize do + @allocator.release(i) end - end # self.release_channel_id(i) + end # Returns true if given channel id has been previously allocated and not yet released. # This method is thread safe. # @@ -48,21 +47,25 @@ # @return [Boolean] true if given channel id has been previously allocated and not yet released # @api public # @see ChannelManager#next_channel_id # @see ChannelManager#release_channel_id def allocated_channel_id?(i) - @channel_id_mutex.synchronize do - @int_allocator.allocated?(i) + @mutex.synchronize do + @allocator.allocated?(i) end end # Resets channel allocator. This method is thread safe. # @api public # @see Channel.next_channel_id # @see Channel.release_channel_id def reset_channel_id_allocator - @channel_id_mutex.synchronize do - @int_allocator.reset + @mutex.synchronize do + @allocator.reset end - end # reset_channel_id_allocator + end + + def synchronize(&block) + @mutex.synchronize(&block) + end end end