Class: DispatchQueue::DispatchGroup

Inherits:
Object
  • Object
show all
Defined in:
lib/dispatch_queue_rb/dispatch_group.rb

Instance Method Summary collapse

Constructor Details

#initializeDispatchGroup

Returns a new instance of DispatchGroup.



12
13
14
15
16
17
# File 'lib/dispatch_queue_rb/dispatch_group.rb', line 12

def initialize()
  @mutex = Mutex.new
  @condition = ConditionVariable.new
  @count = 0
  @notify_list = []
end

Instance Method Details

#enterObject



19
20
21
22
# File 'lib/dispatch_queue_rb/dispatch_group.rb', line 19

def enter()
  @mutex.synchronize { @count += 1 }
  self
end

#leaveObject



24
25
26
27
28
29
30
31
32
33
34
35
36
# File 'lib/dispatch_queue_rb/dispatch_group.rb', line 24

def leave()
  notify_list = @mutex.synchronize do
    @count -= 1
    raise "Unbalanced calls to DispatchGroup.enter() / .leave()" if @count < 0
    if @count == 0
      @condition.broadcast()
      _sync_swap_notify_list()
    end
  end

  _schedule_notify_list( notify_list ) if notify_list
  self
end

#notify(target_queue: nil, barrier: false, group: nil, &task) ⇒ Object



38
39
40
41
42
43
44
45
46
47
48
49
# File 'lib/dispatch_queue_rb/dispatch_group.rb', line 38

def notify( target_queue:nil, barrier:false, group:nil, &task )
  continuation = Continuation.new( target_queue:target_queue,
                                   barrier:barrier, group:group, &task )
  @mutex.synchronize do
    if @count == 0
      continuation.run( default_target_queue:Dispatch.default_queue )
    else
      @notify_list << continuation
    end
  end
  self
end

#wait(timeout: nil) ⇒ Object



51
52
53
54
55
56
57
# File 'lib/dispatch_queue_rb/dispatch_group.rb', line 51

def wait( timeout:nil )
  @mutex.synchronize do
    return true if @count == 0
    @condition.wait( @mutex, timeout )
    return @count == 0
  end
end