Class: LittleGhost::Support::PooledThreadRunner

Inherits:
Object
  • Object
show all
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

Instance Method Summary collapse

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

#capacityObject

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

Raises:

  • (ArgumentError)


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

Raises:

  • (ArgumentError)


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