-
Notifications
You must be signed in to change notification settings - Fork 20
/
DispatchGroup.swift
48 lines (35 loc) · 1.12 KB
/
DispatchGroup.swift
1
2
3
4
5
6
7
8
9
10
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
40
41
42
43
44
45
46
47
48
import CoreGraphics
import Dispatch
public typealias Group = DispatchGroup
public class DispatchGroup {
public init (_ tasks: Int = 0) {
for _ in 0..<tasks { ++self }
}
public private(set) var tasks = 0
public let dispatch_group = dispatch_group_create()
public func done (callback: Void -> Void) {
dispatch_group_notify(dispatch_group, gcd.current.dispatch_queue, callback)
}
public func wait (delay: CGFloat, _ callback: Void -> Void) {
dispatch_group_wait(dispatch_group, dispatch_time(DISPATCH_TIME_NOW, Int64(delay * CGFloat(NSEC_PER_SEC))))
}
deinit { assert(tasks == 0, "A DispatchGroup cannot be deallocated when tasks is greater than zero!") }
}
public prefix func ++ (group: DispatchGroup) {
objc_sync_enter(group)
group.tasks += 1
dispatch_group_enter(group.dispatch_group)
objc_sync_exit(group)
}
public prefix func -- (group: DispatchGroup) {
objc_sync_enter(group)
group.tasks -= 1
dispatch_group_leave(group.dispatch_group)
objc_sync_exit(group)
}
public postfix func ++ (group: DispatchGroup) {
++group
}
public postfix func -- (group: DispatchGroup) {
--group
}