Class: GoChanel::Chanel
- Inherits:
-
Object
- Object
- GoChanel::Chanel
- Defined in:
- lib/go_chanel/chanel.rb
Instance Method Summary collapse
- #cap ⇒ Object
- #close ⇒ Object
-
#initialize(cap_size = 1, objs = []) ⇒ Chanel
constructor
A new instance of Chanel.
- #pop ⇒ Object
- #push(obj) ⇒ Object
- #size ⇒ Object
Constructor Details
#initialize(cap_size = 1, objs = []) ⇒ Chanel
Returns a new instance of Chanel.
10 11 12 13 14 15 16 17 18 |
# File 'lib/go_chanel/chanel.rb', line 10 def initialize(cap_size = 1, objs = []) raise ChanelException.new("can not create a 0 size channel") if cap_size < 1 @obj_mutex = Mutex.new @close_mutex = Mutex.new @cap_size = cap_size @objs = objs @closed = false end |
Instance Method Details
#cap ⇒ Object
50 51 52 |
# File 'lib/go_chanel/chanel.rb', line 50 def cap @cap_size end |
#close ⇒ Object
54 55 56 57 58 |
# File 'lib/go_chanel/chanel.rb', line 54 def close @close_mutex.synchronize do @closed = true end end |
#pop ⇒ Object
33 34 35 36 37 38 39 40 41 42 43 44 |
# File 'lib/go_chanel/chanel.rb', line 33 def pop while @objs.count == 0 do return nil, false if @closed sleep(0.1) end obj = @obj_mutex.synchronize do @objs.shift end return obj, true end |
#push(obj) ⇒ Object
20 21 22 23 24 25 26 27 28 29 30 31 |
# File 'lib/go_chanel/chanel.rb', line 20 def push(obj) raise ChanelException.new("can not push to closed channel") if @closed while @objs.count >= @cap_size do raise ChanelException.new("can not push to closed channel") if @closed sleep(0.1) end @obj_mutex.synchronize do @objs.push(obj) end end |
#size ⇒ Object
46 47 48 |
# File 'lib/go_chanel/chanel.rb', line 46 def size @objs.count end |