-
Notifications
You must be signed in to change notification settings - Fork 78
/
Copy pathQueueConfined.swift
69 lines (59 loc) · 1.85 KB
/
QueueConfined.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
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
//
// QueueConfined.swift
// PMHTTP
//
// Created by Lily Ballard on 12/23/15.
// Copyright © 2015 Postmates.
//
// Licensed under the Apache License, Version 2.0 <LICENSE-APACHE or
// http://www.apache.org/licenses/LICENSE-2.0> or the MIT license
// <LICENSE-MIT or http://opensource.org/licenses/MIT>, at your
// option. This file may not be copied, modified, or distributed
// except according to those terms.
//
import Foundation
/// Manages access to a contained value using a concurrent dispatch queue.
// NB: This is a class because struct copies would break the safety
internal class QueueConfined<Value: AnyObject> {
private let queue: DispatchQueue
private var value: Value
init(label: String, value: Value) {
queue = DispatchQueue(label: label, attributes: .concurrent)
self.value = value
}
func sync(_ f: (Value) -> Void) {
queue.sync {
f(self.value)
}
}
func sync<T>(_ f: (Value) -> T) -> T {
return queue.sync {
return f(self.value)
}
}
func syncBarrier(_ f: (Value) -> Void) {
queue.sync(flags: .barrier, execute: {
f(self.value)
})
}
func syncBarrier<T>(_ f: (Value) -> T) -> T {
return queue.sync(flags: .barrier, execute: {
return f(self.value)
})
}
func async(_ f: @escaping (Value) -> Void) {
queue.async {
f(self.value)
}
}
func asyncBarrier(_ f: @escaping (Value) -> Void) {
queue.async(flags: .barrier, execute: {
f(self.value)
})
}
/// Provides direct access to the value without going through the queue.
/// Use this only when you're guaranteed that there's no concurrency.
func unsafeDirectAccess<T>(_ f: (Value) -> T) -> T {
return f(value)
}
}