summaryrefslogtreecommitdiffhomepage
path: root/ios/Operations/AsyncOperationQueue.swift
blob: e659c96f45168c9e37251ebf62e929ee40e60b28 (plain)
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
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
//
//  AsyncOperationQueue.swift
//  Operations
//
//  Created by pronebird on 30/05/2022.
//  Copyright © 2025 Mullvad VPN AB. All rights reserved.
//

import Foundation

public final class AsyncOperationQueue: OperationQueue, @unchecked Sendable {
    override public func addOperation(_ operation: Operation) {
        if let operation = operation as? AsyncOperation {
            let categories = operation.conditions
                .filter { condition in
                    condition.isMutuallyExclusive
                }
                .map { condition in
                    condition.name
                }

            if !categories.isEmpty {
                ExclusivityManager.shared.addOperation(operation, categories: Set(categories))
            }

            super.addOperation(operation)

            operation.didEnqueue()
        } else {
            super.addOperation(operation)
        }
    }

    override public func addOperations(_ operations: [Operation], waitUntilFinished wait: Bool) {
        for operation in operations {
            addOperation(operation)
        }

        if wait {
            for operation in operations {
                operation.waitUntilFinished()
            }
        }
    }

    public static func makeSerial() -> AsyncOperationQueue {
        let queue = AsyncOperationQueue()
        queue.maxConcurrentOperationCount = 1
        return queue
    }
}

private final class ExclusivityManager: @unchecked Sendable {
    static let shared = ExclusivityManager()

    private var operationsByCategory = [String: [Operation]]()
    private let nslock = NSLock()

    private init() {}

    func addOperation(_ operation: AsyncOperation, categories: Set<String>) {
        nslock.lock()
        defer { nslock.unlock() }

        for category in categories {
            var operations = operationsByCategory[category] ?? []

            if let lastOperation = operations.last {
                operation.addDependency(lastOperation)
            }

            operations.append(operation)

            operationsByCategory[category] = operations

            operation.onFinish { [weak self] op, _ in
                self?.removeOperation(op, categories: categories)
            }
        }
    }

    private func removeOperation(_ operation: Operation, categories: Set<String>) {
        nslock.lock()
        defer { nslock.unlock() }

        for category in categories {
            guard var operations = operationsByCategory[category] else {
                continue
            }

            if let index = operations.firstIndex(of: operation) {
                operations.remove(at: index)
            }

            if operations.isEmpty {
                operationsByCategory.removeValue(forKey: category)
            } else {
                operationsByCategory[category] = operations
            }
        }
    }
}