Class: DispatchQueue::ThreadPoolQueue

Inherits:
Object
  • Object
show all
Includes:
DispatchAfterImpl, DispatchSyncImpl
Defined in:
lib/dispatch_queue_rb/internal/thread_pool_queue.rb

Instance Method Summary collapse

Methods included from DispatchAfterImpl

#dispatch_after

Methods included from DispatchSyncImpl

#dispatch_barrier_sync, #dispatch_sync

Constructor Details

#initialize(max_threads: nil, debug_trace: nil, thread_termination_delay: 0.01) ⇒ ThreadPoolQueue

Returns a new instance of ThreadPoolQueue.



13
14
15
16
17
18
19
20
21
22
23
# File 'lib/dispatch_queue_rb/internal/thread_pool_queue.rb', line 13

def initialize( max_threads:nil, debug_trace:nil, thread_termination_delay:0.01 )
  @max_threads = max_threads || Dispatch.ncpu()
  @debug_trace = debug_trace
  @thread_termination_delay = thread_termination_delay

  @mutex = Mutex.new
  @condition = ConditionVariable.new
  @tasks = []
  @worker_threads = Set.new
  @idle_count = 0
end

Instance Method Details

#dispatch_async(group: nil, &task) ⇒ Object Also known as: dispatch_barrier_async



25
26
27
28
29
30
31
32
33
# File 'lib/dispatch_queue_rb/internal/thread_pool_queue.rb', line 25

def dispatch_async( group:nil, &task )
  group.enter() if group
  @mutex.synchronize do
    @tasks << Continuation.new( group:group, &task )
    @condition.signal()
    _sync_try_spwan_more_threads()
  end
  self
end

#max_threadsObject



42
43
44
45
46
# File 'lib/dispatch_queue_rb/internal/thread_pool_queue.rb', line 42

def max_threads
  @mutex.synchronize do
    @max_threads
  end
end

#max_threads=(new_value) ⇒ Object



48
49
50
51
52
53
# File 'lib/dispatch_queue_rb/internal/thread_pool_queue.rb', line 48

def max_threads=( new_value )
  @mutex.synchronize do
    @max_threads = new_value
    _sync_try_spwan_more_threads()
  end
end

#wait_for_all_threads_terminationObject



55
56
57
58
59
60
# File 'lib/dispatch_queue_rb/internal/thread_pool_queue.rb', line 55

def wait_for_all_threads_termination
  loop do
    sleep 0.001
    break if @tasks.empty? && @worker_threads.empty?
  end
end