Class: Pwrake::LocalityConditionVariable

Inherits:
ConditionVariable
  • Object
show all
Defined in:
lib/pwrake/locality_aware_queue.rb

Instance Method Summary collapse

Instance Method Details

#broadcast(hints = nil) ⇒ Object



41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
# File 'lib/pwrake/locality_aware_queue.rb', line 41

def broadcast(hints=nil)
  if hints.nil?
    super()
  elsif Array===hints
      threads = []
      @waiters_mutex.synchronize do
        hints.each do |h|
          @waiters.each do |t,v|
            if t[:hint] == h
              threads << t
              break
            end
          end
        end
        threads.each do |t|
          @waiters.delete(t)
        end
      end
      Log.debug "--- LCV#broadcast: hints=#{hints.inspect} threads_to_run=#{threads.inspect} @waiters.size=#{@waiters.size}"
      threads.each do |t|
        begin
          t.run
        rescue ThreadError
        end
      end
  else
    raise ArgumentError,"argument must be an Array"
  end
  self
end

#signal(hints = nil) ⇒ Object



11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
# File 'lib/pwrake/locality_aware_queue.rb', line 11

def signal(hints=nil)
  if hints.nil?
    super()
  elsif Array===hints
      thread = nil
      @waiters_mutex.synchronize do
        @waiters.each do |t,v|
          if hints.include?(t[:hint])
            thread = t
            break
          end
        end
        if thread
          @waiters.delete(thread)
        else
          thread,_ = @waiters.shift
        end
      end
      Log.debug "--- LCV#signal: hints=#{hints.inspect} thread_to_run=#{thread.inspect} @waiters.size=#{@waiters.size}"
      begin
        thread.run if thread
      rescue ThreadError
        retry # t was already dead?
      end
  else
    raise ArgumentError,"argument must be an Array"
  end
  self
end