Class: PoolParty::ThreadPool::Worker

Inherits:
Object
  • Object
show all
Defined in:
lib/poolparty/thread_pool.rb

Instance Method Summary collapse

Constructor Details

#initializeWorker

Returns a new instance of Worker.



4
5
6
7
8
9
10
11
12
13
14
15
16
# File 'lib/poolparty/thread_pool.rb', line 4

def initialize
  @mutex = Mutex.new
  @thread = Thread.new do
    while true
      sleep 0.001
      block = get_block
      if block
        block.call
        reset_block
      end
    end
  end
end

Instance Method Details

#busy?Boolean

Returns:

  • (Boolean)


33
34
35
# File 'lib/poolparty/thread_pool.rb', line 33

def busy?
  @mutex.synchronize {!@block.nil?}
end

#get_blockObject



18
19
20
# File 'lib/poolparty/thread_pool.rb', line 18

def get_block
  @mutex.synchronize {@block}
end

#reset_blockObject



29
30
31
# File 'lib/poolparty/thread_pool.rb', line 29

def reset_block
  @mutex.synchronize {@block = nil}
end

#set_block(block) ⇒ Object



22
23
24
25
26
27
# File 'lib/poolparty/thread_pool.rb', line 22

def set_block(block)
  @mutex.synchronize do
    raise RuntimeError, "Thread already busy." if @block
    @block = block
  end
end