Class: Rbgo::Channel::BufferChan

Inherits:
SizedQueue
  • Object
show all
Includes:
Enumerable, Chan
Defined in:
lib/rbgo/select_chan.rb

Overview

BufferChan

Instance Method Summary collapse

Methods included from Chan

new

Constructor Details

#initialize(max) ⇒ BufferChan

Returns a new instance of BufferChan.



202
203
204
205
206
207
208
# File 'lib/rbgo/select_chan.rb', line 202

def initialize(max)
  super(max)
  @mutex = Mutex.new

  self.ios            = []
  self.register_mutex = Mutex.new
end

Instance Method Details

#clearObject



235
236
237
238
239
# File 'lib/rbgo/select_chan.rb', line 235

def clear
  super
  notify
  self
end

#closeObject



241
242
243
244
245
246
247
# File 'lib/rbgo/select_chan.rb', line 241

def close
  @mutex.synchronize do
    super
    notify
    self
  end
end

#eachObject



188
189
190
191
192
193
194
195
196
197
198
199
200
# File 'lib/rbgo/select_chan.rb', line 188

def each
  if block_given?
    loop do
      begin
        yield pop(true)
      rescue ThreadError
        return
      end
    end
  else
    enum_for(:each)
  end
end

#pop(nonblock = false) ⇒ Object Also known as: deq, shift



219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
# File 'lib/rbgo/select_chan.rb', line 219

def pop(nonblock = false)
  @mutex.synchronize do
    res = nil
    ok  = true
    ok  = false if empty? && closed?
    begin
      res = super(nonblock)
      notify
    rescue ThreadError
      raise unless closed?
      ok = false
    end
    [res, ok]
  end
end

#push(obj, nonblock = false) ⇒ Object Also known as: <<, enq



210
211
212
213
214
215
216
217
# File 'lib/rbgo/select_chan.rb', line 210

def push(obj, nonblock = false)
  super(obj, nonblock)
  notify
  self
rescue ThreadError
  raise ClosedQueueError.new if closed?
  raise
end