Class: LittleGhost::Support::PooledThreadRunner
- Inherits:
-
Object
- Object
- LittleGhost::Support::PooledThreadRunner
- Defined in:
- lib/little_ghost/support/pooled_thread_runner.rb
Overview
PooledThreadRunner starts Tasks on a fixed set of reusable threads. It is used for calls that would otherwise pause every fiber on the caller's thread.
Constant Summary collapse
- DEFAULT_CAPACITY =
:nodoc:
2
Instance Attribute Summary collapse
-
#capacity ⇒ Object
Returns the value of attribute capacity.
Instance Method Summary collapse
-
#initialize(capacity: DEFAULT_CAPACITY) ⇒ PooledThreadRunner
constructor
A new instance of PooledThreadRunner.
- #spawn(&work) ⇒ Object
- #try_spawn(&work) ⇒ Object
Constructor Details
#initialize(capacity: DEFAULT_CAPACITY) ⇒ PooledThreadRunner
Returns a new instance of PooledThreadRunner.
15 16 17 18 19 20 21 |
# File 'lib/little_ghost/support/pooled_thread_runner.rb', line 15 def initialize(capacity: DEFAULT_CAPACITY) validate_capacity!(capacity) @capacity = capacity @mutex = Mutex.new @condition = ConditionVariable.new reset_pool end |
Instance Attribute Details
#capacity ⇒ Object
Returns the value of attribute capacity.
13 14 15 |
# File 'lib/little_ghost/support/pooled_thread_runner.rb', line 13 def capacity @capacity end |
Instance Method Details
#spawn(&work) ⇒ Object
35 36 37 38 39 40 41 42 43 |
# File 'lib/little_ghost/support/pooled_thread_runner.rb', line 35 def spawn(&work) raise ArgumentError, "a task block is required" unless work task = build_pool_task(&work).start return task.tap(&:call) if current? || !current_scheduler submit(task, wait: true) task end |
#try_spawn(&work) ⇒ Object
45 46 47 48 49 50 51 52 |
# File 'lib/little_ghost/support/pooled_thread_runner.rb', line 45 def try_spawn(&work) raise ArgumentError, "a task block is required" unless work task = build_pool_task(&work).start return task.tap(&:call) if current? || !current_scheduler submit(task, wait: false) ? task : nil end |