Class: Dispatch::Queue

Inherits:
Object
  • Object
show all
Defined in:
lib/dispatch/queue.rb,
lib/dispatch/enumerable.rb

Class Method Summary collapse

Instance Method Summary collapse

Class Method Details

.for(obj) ⇒ Object

Returns a new serial queue with a unique label based on obj Typically used to serialize access to that object

a = Array.new
q = Dispatch::Queue.for(a)
q.async { a << 2 }


25
26
27
# File 'lib/dispatch/queue.rb', line 25

def self.for(obj)
  new(labelize(obj))
end

.labelize(obj) ⇒ Object

Returns a mostly unique reverse-DNS-style label based on the ancestor chain and ID of obj plus the current time

Dispatch::Queue.labelize(Array.new)
=> enumerable.array.0x2000cc2c0.1265915278.97557


12
13
14
15
16
# File 'lib/dispatch/queue.rb', line 12

def self.labelize(obj)
  names = obj.class.ancestors[0...-2].map {|a| a.to_s.downcase}
  label = names.uniq.reverse.join(".")
  "#{label}.0x%x.#{Time.now.to_f}" % obj.object_id
end

Instance Method Details

#fake_apply(n, &block) ⇒ Object



5
6
7
8
9
10
11
# File 'lib/dispatch/enumerable.rb', line 5

def fake_apply(n, &block)
  g = Dispatch::Group.new
  n.times do |i|
    async(g) { block.call(i) }
  end
  g.wait
end

#joinObject

Wait until pending blocks have completed



30
31
32
# File 'lib/dispatch/queue.rb', line 30

def join
  sync {}
end