Module: Hsdq::Threadpool

Included in:
Hsdq
Defined in:
lib/hsdq/threadpool.rb

Overview

This module is used to manage the threads

Instance Method Summary collapse

Instance Method Details

#allow_new_threads?Boolean

Returns true if below the max number of allowed thread.

Returns:

  • (Boolean)

    true if below the max number of allowed thread



26
27
28
# File 'lib/hsdq/threadpool.rb', line 26

def allow_new_threads?
  hsdq_threads_count < max_thread_count && !paused?
end

#hsdq_start_thread(ignition) ⇒ TheadGroup

Start a new thread and add it to the thread group

Parameters:

  • the (Proc)

    thread staring block

Returns:

  • (TheadGroup)

    the hdsq thread goup



51
52
53
54
55
# File 'lib/hsdq/threadpool.rb', line 51

def hsdq_start_thread(ignition)
  t = Thread.new(&ignition)
  p "New thread: #{t}"
  hsdq_threads_add t
end

#hsdq_threadsThreadGroup

Cached ThreadGroup instance holding the threads for a given hsdq class

Returns:

  • (ThreadGroup)


32
33
34
# File 'lib/hsdq/threadpool.rb', line 32

def hsdq_threads
  @hsdq_threads ||= ThreadGroup.new
end

#hsdq_threads_add(thread) ⇒ Threadgroup

Add a thread to the thread group

Parameters:

  • (Thread)

Returns:

  • (Threadgroup)

    the hdsq thread goup



39
40
41
# File 'lib/hsdq/threadpool.rb', line 39

def hsdq_threads_add(thread)
  hsdq_threads.add thread
end

#hsdq_threads_countObject

Returns the number of thread in the thread group.

Returns:

  • the number of thread in the thread group



44
45
46
# File 'lib/hsdq/threadpool.rb', line 44

def hsdq_threads_count
  hsdq_threads.list.size
end

#max_thread_count(max_count = nil) ⇒ Integer

The maximum number of threads allowed to run at the same time. This is setter and a cached getter.

Parameters:

  • max_count (Integer or nil) (defaults to: nil)

    The max count to be set if passed if nil or no param, do not change the the value.

Returns:

  • (Integer)

    the max allowed number of threads



8
9
10
11
# File 'lib/hsdq/threadpool.rb', line 8

def max_thread_count(max_count=nil)
  @max_thread_count = max_count if max_count
  @max_thread_count ||= hsdq_opts[:max_thread_count] || 10
end

#paused(paused) ⇒ Object

Set paused flag

Parameters:

  • paused (Boolean)


15
16
17
# File 'lib/hsdq/threadpool.rb', line 15

def paused(paused)
  @paused = paused
end

#paused?Boolean

Returns true is paused.

Returns:

  • (Boolean)

    true is paused



20
21
22
23
# File 'lib/hsdq/threadpool.rb', line 20

def paused?
  @paused = false if @paused.nil?
  @paused
end