Class: ThreadPool::Worker
- Inherits:
-
Object
- Object
- ThreadPool::Worker
- Includes:
- Awakenable
- Defined in:
- lib/threadpool.rb
Instance Method Summary collapse
- #available? ⇒ Boolean
- #dead? ⇒ Boolean
- #die? ⇒ Boolean
-
#initialize(pool) ⇒ Worker
constructor
A new instance of Worker.
- #join ⇒ Object
- #kill ⇒ Object
- #process(*args, &block) ⇒ Object
Methods included from Awakenable
Constructor Details
#initialize(pool) ⇒ Worker
Returns a new instance of Worker.
36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 |
# File 'lib/threadpool.rb', line 36 def initialize (pool) @pool = pool @mutex = Mutex.new @thread = Thread.new { loop do if @block begin @block.call(*@args) rescue Exception => e @pool.raise(e) end @block = nil @args = nil @pool.wake_up else sleep unless die? break if die? end end } end |
Instance Method Details
#available? ⇒ Boolean
61 62 63 64 65 |
# File 'lib/threadpool.rb', line 61 def available? @mutex.synchronize { !@block } end |
#dead? ⇒ Boolean
93 94 95 |
# File 'lib/threadpool.rb', line 93 def dead? @thread.stop? end |
#die? ⇒ Boolean
89 90 91 |
# File 'lib/threadpool.rb', line 89 def die? @die end |
#join ⇒ Object
78 79 80 |
# File 'lib/threadpool.rb', line 78 def join @thread.join end |
#kill ⇒ Object
82 83 84 85 86 87 |
# File 'lib/threadpool.rb', line 82 def kill return if die? @die = true wake_up end |
#process(*args, &block) ⇒ Object
67 68 69 70 71 72 73 74 75 76 |
# File 'lib/threadpool.rb', line 67 def process (*args, &block) return unless available? @mutex.synchronize { @block = block @args = args wake_up } end |