| Class | PoolingExecutor |
| In: |
vendor/plugins/acts_as_searchable/lib/pooling_executor.rb
|
| Parent: | Object |
| pool | [R] |
# File vendor/plugins/acts_as_searchable/lib/pooling_executor.rb, line 9
9: def initialize
10: @mutex = Mutex.new
11: @pool = []
12: @handleravail = ConditionVariable.new
13: @threads = {}
14: yield self if block_given?
15: end
# File vendor/plugins/acts_as_searchable/lib/pooling_executor.rb, line 17
17: def add_handler(handler)
18: @pool << handler
19: end
# File vendor/plugins/acts_as_searchable/lib/pooling_executor.rb, line 60
60: def num_tasks
61: @threads.size
62: end
# File vendor/plugins/acts_as_searchable/lib/pooling_executor.rb, line 23
23: def run(logger = nil)
24: handler = nil
25: ref = []
26: @mutex.synchronize do
27: if @pool.size == 0
28: @handleravail.wait @mutex until @pool.size > 0
29: end
30: handler = @pool.shift
31: @threads[handler] = ref # so it can be removed from @threads in due time
32: logger and logger.info("Got handler #{handler.inspect}.")
33: end
34: ref << Thread.new do
35: begin
36: logger and logger.info("Yielding handler #{handler.inspect} for execution.")
37: yield handler
38: logger and logger.info("Finished execution with #{handler.inspect}.")
39: ensure
40: @mutex.synchronize do
41: @pool << handler
42: @threads.delete handler
43: @handleravail.signal
44: end
45: end
46: end
47: ref[0]
48: end