Class: Noda::RQueue

Inherits:
Object
  • Object
show all
Includes:
DRb::DRbUndumped
Defined in:
lib/noda/rqueue.rb

Overview

q.push Noda::MyTask.new(“hogehgoe”)

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(max = nil, name = nil) ⇒ RQueue

Returns a new instance of RQueue.



19
20
21
22
23
24
25
26
27
# File 'lib/noda/rqueue.rb', line 19

def initialize( max=nil, name=nil )
  @name = name
  @list = []
  @max = nil
  @max = max if max
  self.extend(MonitorMixin)
  @m_empty = self.new_cond
  @m_full = self.new_cond
end

Instance Attribute Details

#nameObject (readonly)

Returns the value of attribute name.



17
18
19
# File 'lib/noda/rqueue.rb', line 17

def name
  @name
end

Instance Method Details

#_at(pos) ⇒ Object

キューの先頭N番目の値を調べる.チェック用。

  • pos 1からNまでの値を取る。先頭は1 で指定する.0始まりでないことに注意



73
74
75
76
# File 'lib/noda/rqueue.rb', line 73

def _at(pos) 
  i = pos - 1 
  return @list.at(i) if (i) < self.size 
end

#allObject

キューの値全てを取り出す.



70
# File 'lib/noda/rqueue.rb', line 70

def all() self.firsts(self.size) end

#close_to_full?Boolean

キュー満杯が近いときにTrueを返す.

Returns:

  • (Boolean)


63
# File 'lib/noda/rqueue.rb', line 63

def close_to_full?()    @list.size >= @max-5    end

#empty?Boolean

キューに値があるか

Returns:

  • (Boolean)


61
# File 'lib/noda/rqueue.rb', line 61

def empty?()    @list.empty?            end

#firstObject

キューの先頭1個を取り出す. pop の別名



55
# File 'lib/noda/rqueue.rb', line 55

def first()     self.pop                end

#firsts(n = 1) ⇒ Object

キューの先頭N個を取り出す.



53
# File 'lib/noda/rqueue.rb', line 53

def firsts(n=1) (0...n).map{self.pop}   end

#full?Boolean

キューが満杯かどうか

Returns:

  • (Boolean)


65
66
67
68
# File 'lib/noda/rqueue.rb', line 65

def full?()
  # max=nil ならLimitless。つまり無限大
  @list.size >= @max if @max
end

#include?(v) ⇒ Boolean Also known as: exists?

実験用メソッド・使わない.

Returns:

  • (Boolean)


50
# File 'lib/noda/rqueue.rb', line 50

def include?(v) @list.include? v        end

#max_sizeObject

キュー格納可能数



59
# File 'lib/noda/rqueue.rb', line 59

def max_size()  @max                    end

#popObject

キュー先頭からオブジェクトを取り出す.

キュー空なら実行スレッドをWaitさせる.



41
42
43
44
45
46
47
48
# File 'lib/noda/rqueue.rb', line 41

def pop
  self.synchronize{
    @m_empty.wait_while{ self.empty?  }
    obj = @list.shift
    @m_full.broadcast if @max
    obj
  }
end

#push(obj) ⇒ Object

キュー末尾にオブジェクトを追加。

キュー満杯時は実行スレッドをWaitさせます。



31
32
33
34
35
36
37
# File 'lib/noda/rqueue.rb', line 31

def push obj
  self.synchronize{
    @m_full.wait_while{ self.full?  } if @max
    @list.push obj
    @m_empty.broadcast
  }
end

#sizeObject

キューのサイズを取得



57
# File 'lib/noda/rqueue.rb', line 57

def size()      @list.size              end