- Notifications
You must be signed in to change notification settings - Fork 162
/
Copy pathLocking.swift
156 lines (137 loc) · 4.43 KB
/
Locking.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
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
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
//===----------------------------------------------------------------------===//
//
// This source file is part of the Swift Async Algorithms open source project
//
// Copyright (c) 2022 Apple Inc. and the Swift project authors
// Licensed under Apache License v2.0 with Runtime Library Exception
//
// See https://swift.org/LICENSE.txt for license information
//
//===----------------------------------------------------------------------===//
#if canImport(Darwin)
import Darwin
#elseif canImport(Glibc)
import Glibc
#elseif canImport(Musl)
import Musl
#elseif canImport(WinSDK)
import WinSDK
#elseif canImport(Bionic)
import Bionic
#else
#error("Unsupported platform")
#endif
internalstructLock{
#if canImport(Darwin)
typealiasPrimitive=os_unfair_lock
#elseif canImport(Glibc) || canImport(Musl) || canImport(Bionic)
typealiasPrimitive=pthread_mutex_t
#elseif canImport(WinSDK)
typealiasPrimitive=SRWLOCK
#else
#error("Unsupported platform")
#endif
typealiasPlatformLock=UnsafeMutablePointer<Primitive>
letplatformLock:PlatformLock
privateinit(_ platformLock:PlatformLock){
self.platformLock = platformLock
}
fileprivatestaticfunc initialize(_ platformLock:PlatformLock){
#if canImport(Darwin)
platformLock.initialize(to:os_unfair_lock())
#elseif canImport(Glibc) || canImport(Musl) || canImport(Bionic)
letresult=pthread_mutex_init(platformLock,nil)
precondition(result ==0,"pthread_mutex_init failed")
#elseif canImport(WinSDK)
InitializeSRWLock(platformLock)
#else
#error("Unsupported platform")
#endif
}
fileprivatestaticfunc deinitialize(_ platformLock:PlatformLock){
#if canImport(Glibc) || canImport(Musl) || canImport(Bionic)
letresult=pthread_mutex_destroy(platformLock)
precondition(result ==0,"pthread_mutex_destroy failed")
#endif
platformLock.deinitialize(count:1)
}
fileprivatestaticfunc lock(_ platformLock:PlatformLock){
#if canImport(Darwin)
os_unfair_lock_lock(platformLock)
#elseif canImport(Glibc) || canImport(Musl) || canImport(Bionic)
pthread_mutex_lock(platformLock)
#elseif canImport(WinSDK)
AcquireSRWLockExclusive(platformLock)
#else
#error("Unsupported platform")
#endif
}
fileprivatestaticfunc unlock(_ platformLock:PlatformLock){
#if canImport(Darwin)
os_unfair_lock_unlock(platformLock)
#elseif canImport(Glibc) || canImport(Musl) || canImport(Bionic)
letresult=pthread_mutex_unlock(platformLock)
precondition(result ==0,"pthread_mutex_unlock failed")
#elseif canImport(WinSDK)
ReleaseSRWLockExclusive(platformLock)
#else
#error("Unsupported platform")
#endif
}
staticfunc allocate()->Lock{
letplatformLock=PlatformLock.allocate(capacity:1)
initialize(platformLock)
returnLock(platformLock)
}
func deinitialize(){
Lock.deinitialize(platformLock)
platformLock.deallocate()
}
func lock(){
Lock.lock(platformLock)
}
func unlock(){
Lock.unlock(platformLock)
}
/// Acquire the lock for the duration of the given block.
///
/// This convenience method should be preferred to `lock` and `unlock` in
/// most situations, as it ensures that the lock will be released regardless
/// of how `body` exits.
///
/// - Parameter body: The block to execute while holding the lock.
/// - Returns: The value returned by the block.
func withLock<T>(_ body:()throws->T)rethrows->T{
self.lock()
defer{
self.unlock()
}
returntrybody()
}
// specialise Void return (for performance)
func withLockVoid(_ body:()throws->Void)rethrows{
tryself.withLock(body)
}
}
structManagedCriticalState<State>{
privatefinalclassLockedBuffer:ManagedBuffer<State,Lock.Primitive>{
deinit{
withUnsafeMutablePointerToElements{Lock.deinitialize($0)}
}
}
privateletbuffer:ManagedBuffer<State,Lock.Primitive>
init(_ initial:State){
buffer =LockedBuffer.create(minimumCapacity:1){ buffer in
buffer.withUnsafeMutablePointerToElements{Lock.initialize($0)}
return initial
}
}
func withCriticalRegion<R>(_ critical:(inoutState)throws->R)rethrows->R{
try buffer.withUnsafeMutablePointers{ header, lock in
Lock.lock(lock)
defer{Lock.unlock(lock)}
returntrycritical(&header.pointee)
}
}
}
extensionManagedCriticalState:@uncheckedSendablewhere State:Sendable{}