Class: Af::ThreadPool::Worker

Inherits:
Object
  • Object
show all
Defined in:
lib/fiksu-af/thread_pool.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(thread_class = Thread) ⇒ Worker

Returns a new instance of Worker.



11
12
13
14
15
16
17
18
19
20
21
22
23
# File 'lib/fiksu-af/thread_pool.rb', line 11

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

Instance Attribute Details

#threadObject (readonly)

Returns the value of attribute thread.



9
10
11
# File 'lib/fiksu-af/thread_pool.rb', line 9

def thread
  @thread
end

Instance Method Details

#busy?Boolean

Returns:

  • (Boolean)


40
41
42
# File 'lib/fiksu-af/thread_pool.rb', line 40

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

#get_blockObject



25
26
27
# File 'lib/fiksu-af/thread_pool.rb', line 25

def get_block
  @mutex.synchronize {@block}
end

#reset_blockObject



36
37
38
# File 'lib/fiksu-af/thread_pool.rb', line 36

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

#set_block(block) ⇒ Object



29
30
31
32
33
34
# File 'lib/fiksu-af/thread_pool.rb', line 29

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