Class: Bake::Multithread::Jobs

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

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(jobs, &block) ⇒ Jobs

Returns a new instance of Jobs.



22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
# File 'lib/multithread/job.rb', line 22

def initialize(jobs, &block)
  nr_of_threads = [Bake.options.threads, jobs.length].min
  @jobs = jobs
  @threads = []
  nr_of_threads.times do
    @threads << ::Thread.new(Thread.current[:stdout]) do |outStr|
      Thread.current[:stdout] = outStr
      begin
        Jobs.incThread()
        block.call(self)
      ensure
        Jobs.decThread()
      end
    end
  end
end

Class Method Details

.decThreadObject



15
16
17
# File 'lib/multithread/job.rb', line 15

def self.decThread
  @@semaphore.release
end

.incThreadObject



12
13
14
# File 'lib/multithread/job.rb', line 12

def self.incThread
  @@semaphore.acquire
end

.init_semaphoreObject



18
19
20
# File 'lib/multithread/job.rb', line 18

def self.init_semaphore
  @@semaphore = ::Concurrent::MutexSemaphore.new(Bake.options.threads)
end

Instance Method Details

#failedObject



39
40
41
# File 'lib/multithread/job.rb', line 39

def failed
  @failed ||= false
end

#get_next_or_nilObject



46
47
48
49
50
51
52
# File 'lib/multithread/job.rb', line 46

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

#joinObject



53
54
55
# File 'lib/multithread/job.rb', line 53

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

#mutexObject



56
57
58
# File 'lib/multithread/job.rb', line 56

def mutex
  @mutex ||= Mutex.new
end

#set_failedObject



42
43
44
# File 'lib/multithread/job.rb', line 42

def set_failed
  @failed = true
end