Class: Bake::Multithread::Jobs

Inherits:
Object
  • Object
show all
Defined in:
lib/multithread/job.rb

Instance Method Summary collapse

Constructor Details

#initialize(jobs, &block) ⇒ Jobs

Returns a new instance of Jobs.



9
10
11
12
13
14
15
16
17
18
# File 'lib/multithread/job.rb', line 9

def initialize(jobs, &block)
  nr_of_threads = [Bake.options.threads, jobs.length].min
  @jobs = jobs
  @threads = []
  nr_of_threads.times do
    @threads << ::Thread.new do
      block.call(self)
    end
  end
end

Instance Method Details

#failedObject



20
21
22
# File 'lib/multithread/job.rb', line 20

def failed
  @failed ||= false
end

#get_next_or_nilObject



27
28
29
30
31
32
33
# File 'lib/multithread/job.rb', line 27

def get_next_or_nil
  the_next = nil
  mutex.synchronize {
    the_next = @jobs.shift
  }
  the_next
end

#joinObject



34
35
36
# File 'lib/multithread/job.rb', line 34

def join
  @threads.each{|t| while not t.join(2) do end}
end

#mutexObject



37
38
39
# File 'lib/multithread/job.rb', line 37

def mutex
  @mutex ||= Mutex.new
end

#set_failedObject



23
24
25
# File 'lib/multithread/job.rb', line 23

def set_failed
  @failed = true
end