Module: RbbtSemaphore

Defined in:
lib/rbbt/util/semaphore.rb

Class Method Summary collapse

Class Method Details

.fork_each_on_semaphore(elems, size, file = nil) ⇒ Object



73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
# File 'lib/rbbt/util/semaphore.rb', line 73

def self.fork_each_on_semaphore(elems, size, file = nil)
  with_semaphore(size, file) do |file|
    begin

      pids = elems.collect do |elem| 
        Process.fork do 
          begin
            RbbtSemaphore.synchronize(file) do
              yield elem
            end
          rescue Interrupt
            Log.warn "Process #{Process.pid} was aborted"
          end
        end
      end

      while pids.any?
        pid = Process.wait -1
        pids.delete pid
      end
      #pids.each do |pid| Process.waitpid pid end
      
    rescue Exception
      Log.warn "Killing children: #{pids.sort * ", " }"
      pids.each do |pid| begin Process.kill("INT", pid); rescue; end; end
      pids.each do |pid| begin RbbtSemaphore.post_semaphore(file); rescue; end; end
      Log.warn "Ensuring children are dead: #{pids.sort * ", " }"
      pids.each do |pid| begin Process.waitpid pid; rescue; end; end
    end
  end
end

.synchronize(sem) ⇒ Object



53
54
55
56
57
58
59
60
# File 'lib/rbbt/util/semaphore.rb', line 53

def self.synchronize(sem)
  RbbtSemaphore.wait_semaphore(sem)
  begin
    yield
  ensure
    RbbtSemaphore.post_semaphore(sem)
  end
end

.thread_each_on_semaphore(elems, size) ⇒ Object



105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
# File 'lib/rbbt/util/semaphore.rb', line 105

def self.thread_each_on_semaphore(elems, size)
  mutex = Mutex.new
  count = 0
  cv = ConditionVariable.new
  wait_mutex = Mutex.new

  begin

    threads = []
    wait_mutex.synchronize do
      threads = elems.collect do |elem| 
        Thread.new(elem) do |elem|

          continue = false
          mutex.synchronize do
            while not continue do
              if count < size 
                continue = true
                count += 1
              end
              mutex.sleep 1 unless continue
            end
          end

          begin
            yield elem
          rescue Interrupt
            Log.error "Thread was aborted while processing: #{Misc.fingerprint elem}"
            raise $!
          ensure
            mutex.synchronize do
              count -= 1
              cv.signal if mutex.locked?
            end
          end
        end
      end
    end

    threads.each do |thread| 
      thread.join 
    end
  rescue Exception
    Log.exception $!
    Log.info "Ensuring threads are dead: #{threads.length}"
    threads.each do |thread| thread.kill end
  end
end

.with_semaphore(size, file = nil) ⇒ Object



62
63
64
65
66
67
68
69
70
71
# File 'lib/rbbt/util/semaphore.rb', line 62

def self.with_semaphore(size, file = nil)
  file = Misc.digest(rand.to_s) if file.nil?
  file.gsub!('/', '_')
  begin
    RbbtSemaphore.create_semaphore(file, size)
    yield file
  ensure
    RbbtSemaphore.delete_semaphore(file)
  end
end