Class: PatchFinder::ThreadPool

Inherits:
Object
  • Object
show all
Defined in:
lib/patch_finder/core/thread_pool.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(size) ⇒ void

Initializes the pool.

Parameters:

  • size (Fixnum)

    Max number of threads to be running at the same time.



10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
# File 'lib/patch_finder/core/thread_pool.rb', line 10

def initialize(size)
  @size = size
  @mutex = Mutex.new
  @jobs = Queue.new
  @pool = Array.new(@size) do |i|
    Thread.new do
      Thread.current[:id] = i
      catch(:exit) do
        loop do
          job, args = @jobs.pop
          job.call(*args)
        end
      end
    end
  end
end

Instance Attribute Details

#mutexObject

Returns the value of attribute mutex.



4
5
6
# File 'lib/patch_finder/core/thread_pool.rb', line 4

def mutex
  @mutex
end

Instance Method Details

#cleanupvoid

This method returns an undefined value.

Terminates all threads



56
57
58
59
# File 'lib/patch_finder/core/thread_pool.rb', line 56

def cleanup
  @jobs.clear
  @pool.map(&:kill)
end

#eop?boolean

Returns whether there’s anything in the queue left.

Returns:

  • (boolean)


49
50
51
# File 'lib/patch_finder/core/thread_pool.rb', line 49

def eop?
  @jobs.empty?
end

#schedule(*args, &block) ⇒ Object

Adds a job to the queue.

Parameters:

  • args (Array)

    Arguments.

  • block (Proc)

    Code.



31
32
33
# File 'lib/patch_finder/core/thread_pool.rb', line 31

def schedule(*args, &block)
  @jobs << [block, args]
end

#shutdownvoid

This method returns an undefined value.

Shuts down all the jobs.



38
39
40
41
42
43
44
# File 'lib/patch_finder/core/thread_pool.rb', line 38

def shutdown
  @size.times do
    schedule { throw :exit }
  end

  @pool.map(&:join)
end