|
| 1 | +import SwiftShims |
| 2 | + |
| 3 | +fileprivate let ValueBufferSize = unsafe 3 * MemoryLayout<UnsafeRawPointer>.stride |
| 4 | + |
| 5 | +fileprivate let TrackingFlag: UInt = 0x20 |
| 6 | +fileprivate let ActionMask: UInt = 0x1 |
| 7 | + |
| 8 | +fileprivate typealias AccessPointer = UnsafeMutablePointer<Access> |
| 9 | +@unsafe fileprivate struct Access { |
| 10 | + |
| 11 | + enum Action: UInt { |
| 12 | + case read |
| 13 | + case modify |
| 14 | + } |
| 15 | + |
| 16 | + struct NextAndAction { |
| 17 | + private var rawValue: UInt |
| 18 | + |
| 19 | + var action: Action { |
| 20 | + get { |
| 21 | + Action(rawValue: rawValue & ActionMask)! |
| 22 | + } |
| 23 | + set { |
| 24 | + rawValue = (rawValue & ~ActionMask) | newValue.rawValue |
| 25 | + } |
| 26 | + } |
| 27 | + |
| 28 | + var next: AccessPointer? { |
| 29 | + get { unsafe UnsafeMutablePointer(bitPattern: rawValue & ~ActionMask) } |
| 30 | + set { rawValue = UInt(bitPattern: newValue) | (rawValue & ActionMask) } |
| 31 | + } |
| 32 | + } |
| 33 | + |
| 34 | + var location: UnsafeRawPointer? |
| 35 | + var pc: UnsafeRawPointer? |
| 36 | + var nextAndAction: NextAndAction |
| 37 | + |
| 38 | + var action: Action { |
| 39 | + get { unsafe nextAndAction.action } |
| 40 | + set { unsafe nextAndAction.action = newValue } |
| 41 | + } |
| 42 | + |
| 43 | + var next: AccessPointer? { |
| 44 | + get { unsafe nextAndAction.next } |
| 45 | + set { unsafe nextAndAction.next = newValue } |
| 46 | + } |
| 47 | + |
| 48 | + static func from(rawPointer: UnsafeMutableRawPointer?) -> AccessPointer? { |
| 49 | + guard let rawPointer = unsafe rawPointer else { return nil } |
| 50 | + return unsafe rawPointer.assumingMemoryBound(to: Access.self) |
| 51 | + } |
| 52 | + |
| 53 | + @inline(__always) |
| 54 | + static func search( |
| 55 | + buffer: UnsafeMutableRawPointer, |
| 56 | + location: UnsafeRawPointer, |
| 57 | + pc: UnsafeRawPointer?, |
| 58 | + action: Action, |
| 59 | + inserting: Bool, |
| 60 | + head: inout AccessPointer? |
| 61 | + ) { |
| 62 | + var cursor = unsafe head |
| 63 | + while let nextPtr = unsafe cursor { |
| 64 | + if unsafe nextPtr.pointee.location == location { |
| 65 | + if unsafe nextPtr.pointee.action == Action.modify |
| 66 | + || action == Action.modify { |
| 67 | + unsafe _swift_reportExclusivityConflict( |
| 68 | + nextPtr.pointee.action.rawValue, |
| 69 | + nextPtr.pointee.pc, |
| 70 | + action.rawValue, |
| 71 | + pc, |
| 72 | + location) |
| 73 | + } |
| 74 | + } |
| 75 | + unsafe cursor = nextPtr.pointee.next |
| 76 | + } |
| 77 | + |
| 78 | + if inserting { |
| 79 | + guard let access = unsafe Access.from(rawPointer: buffer) else { |
| 80 | + nullAccessBuffer() |
| 81 | + } |
| 82 | + |
| 83 | + unsafe access.pointee.location = location |
| 84 | + unsafe access.pointee.pc = pc |
| 85 | + unsafe access.pointee.action = action |
| 86 | + |
| 87 | + unsafe access.pointee.next = head |
| 88 | + unsafe head = access |
| 89 | + } |
| 90 | + } |
| 91 | + |
| 92 | + @inline(__always) |
| 93 | + static func remove(access: AccessPointer, head: inout AccessPointer?) { |
| 94 | + var cursor = unsafe head |
| 95 | + var previous: AccessPointer? = nil |
| 96 | + while let nextPtr = unsafe cursor { |
| 97 | + if unsafe nextPtr == access { |
| 98 | + if let previous = unsafe previous { |
| 99 | + unsafe previous.pointee.next = access.pointee.next |
| 100 | + } else { |
| 101 | + unsafe head = access.pointee.next |
| 102 | + } |
| 103 | + return |
| 104 | + } |
| 105 | + unsafe previous = nextPtr |
| 106 | + unsafe cursor = nextPtr.pointee.next |
| 107 | + } |
| 108 | + |
| 109 | + unsafe accessNotFound(access) |
| 110 | + } |
| 111 | + |
| 112 | + @inline(__always) |
| 113 | + static func findParent( |
| 114 | + access: AccessPointer, |
| 115 | + child: AccessPointer? |
| 116 | + ) -> AccessPointer? { |
| 117 | + var cursor = unsafe access |
| 118 | + while let next = unsafe cursor.pointee.next { |
| 119 | + if unsafe next == child { |
| 120 | + return unsafe cursor |
| 121 | + } |
| 122 | + unsafe cursor = next |
| 123 | + } |
| 124 | + |
| 125 | + // If we were searching for nil, then we found it. |
| 126 | + if unsafe child == nil { |
| 127 | + return unsafe cursor |
| 128 | + } |
| 129 | + |
| 130 | + // If we were searching for a non-nil node, we didn't find it. |
| 131 | + return nil |
| 132 | + } |
| 133 | + |
| 134 | + static func forEach(_ head: AccessPointer?, _ action: (Access) -> Void) { |
| 135 | + var cursor = unsafe head |
| 136 | + while let nextPtr = unsafe cursor { |
| 137 | + unsafe action(nextPtr.pointee) |
| 138 | + unsafe cursor = nextPtr.pointee.next |
| 139 | + } |
| 140 | + } |
| 141 | +} |
| 142 | + |
| 143 | +fileprivate var accessHead: AccessPointer? { |
| 144 | + get { unsafe Access.from(rawPointer: _swift_getExclusivityTLS()) } |
| 145 | + set { unsafe _swift_setExclusivityTLS(newValue) } |
| 146 | +} |
| 147 | + |
| 148 | +@_cdecl("swift_beginAccess") |
| 149 | +@usableFromInline |
| 150 | +@unsafe |
| 151 | +internal func swift_beginAccess( |
| 152 | + pointer: UnsafeRawPointer, |
| 153 | + buffer: UnsafeMutableRawPointer, |
| 154 | + flags: UInt, |
| 155 | + pc: UnsafeRawPointer? |
| 156 | +) { |
| 157 | + // Make sure that Access fits in the value buffer. This should be a static |
| 158 | + // assert, but we'll settle for a precondition for now. The optimizer should |
| 159 | + // eliminate the check when it's true. |
| 160 | + precondition(unsafe MemoryLayout<Access>.size <= ValueBufferSize) |
| 161 | + |
| 162 | + guard let action = Access.Action(rawValue: flags & ActionMask) else { |
| 163 | + invalidFlags(flags) |
| 164 | + } |
| 165 | + |
| 166 | + let isTracking = (flags & TrackingFlag) != 0 |
| 167 | + |
| 168 | + unsafe Access.search( |
| 169 | + buffer: buffer, |
| 170 | + location: pointer, |
| 171 | + pc: pc ?? _swift_stdlib_get_return_address(), |
| 172 | + action: action, |
| 173 | + inserting: isTracking, |
| 174 | + head: &accessHead) |
| 175 | +} |
| 176 | + |
| 177 | +@_cdecl("swift_endAccess") |
| 178 | +@usableFromInline |
| 179 | +@unsafe |
| 180 | +internal func swift_endAccess(buffer: UnsafeMutableRawPointer) { |
| 181 | + guard let access = unsafe Access.from(rawPointer: buffer) else { |
| 182 | + nullAccessBuffer() |
| 183 | + } |
| 184 | + unsafe Access.remove(access: access, head: &accessHead) |
| 185 | +} |
| 186 | + |
| 187 | +@_cdecl("_swift_exclusivityAccessSetNext") |
| 188 | +@usableFromInline |
| 189 | +@unsafe |
| 190 | +internal func _swift_exclusivityAccessSetNext( |
| 191 | + access: UnsafeMutableRawPointer, |
| 192 | + next: UnsafeMutableRawPointer |
| 193 | +) { |
| 194 | + let access = unsafe Access.from(rawPointer: access) |
| 195 | + let next = unsafe Access.from(rawPointer: next) |
| 196 | + unsafe access?.pointee.next = next |
| 197 | +} |
| 198 | + |
| 199 | +@_cdecl("swift_dumpTrackedAccesses") |
| 200 | +@usableFromInline |
| 201 | +@unsafe |
| 202 | +internal func swift_dumpTrackedAccesses() { |
| 203 | + if let head = unsafe accessHead { |
| 204 | + unsafe Access.forEach(head) { |
| 205 | + unsafe _swift_stdlib_fputs_stderr(" Access. " + |
| 206 | + "Pointer: \($0.location, default: "<null>")." + |
| 207 | + "PC: \($0.pc, default: "<null>"). " + |
| 208 | + "AccessAction: \($0.action)\n") |
| 209 | + } |
| 210 | + } else { |
| 211 | + unsafe _swift_stdlib_fputs_stderr(" No Accesses.\n") |
| 212 | + } |
| 213 | +} |
| 214 | + |
| 215 | +/// Starting from `access`, find the access that is the parent node of `child`. |
| 216 | +/// If `child` is `nil`, find the last access in the list. |
| 217 | +@_cdecl("_swift_exclusivityAccessGetParent") |
| 218 | +@usableFromInline |
| 219 | +@unsafe |
| 220 | +internal func _swift_exclusivityAccessGetParent( |
| 221 | + access: UnsafeMutableRawPointer?, |
| 222 | + child: UnsafeMutableRawPointer? |
| 223 | +) -> UnsafeMutableRawPointer? { |
| 224 | + if let access = unsafe Access.from(rawPointer: access) { |
| 225 | + let result = unsafe Access.findParent( |
| 226 | + access: access, child: Access.from(rawPointer: child)) |
| 227 | + return UnsafeMutableRawPointer(result) |
| 228 | + } |
| 229 | + return nil |
| 230 | +} |
| 231 | + |
| 232 | +@inline(never) |
| 233 | +fileprivate func invalidFlags(_ flags: UInt) -> Never { |
| 234 | + reportExclusivityError("Internal exclusivity error", |
| 235 | + "unable to construct action from flags \(flags)") |
| 236 | +} |
| 237 | + |
| 238 | +@inline(never) |
| 239 | +fileprivate func accessNotFound(_ access: AccessPointer) -> Never { |
| 240 | + unsafe reportExclusivityError("Internal exclusivity error", |
| 241 | + "didn't find exclusive access buffer \(access)") |
| 242 | +} |
| 243 | + |
| 244 | +@inline(never) |
| 245 | +fileprivate func nullAccessBuffer() -> Never { |
| 246 | + reportExclusivityError("Internal exclusivity error", "NULL access buffer") |
| 247 | +} |
| 248 | + |
| 249 | +fileprivate func reportExclusivityError( |
| 250 | + _ prefix: StaticString, _ message: String |
| 251 | +) -> Never { |
| 252 | + prefix.withUTF8Buffer { prefixBuffer in |
| 253 | + var message = message |
| 254 | + message.withUTF8 { messageBuffer in |
| 255 | + unsafe _swift_stdlib_reportFatalError( |
| 256 | + prefixBuffer.baseAddress!, CInt(prefixBuffer.count), |
| 257 | + messageBuffer.baseAddress!, CInt(messageBuffer.count), |
| 258 | + 0) |
| 259 | + } |
| 260 | + } |
| 261 | + Builtin.int_trap() |
| 262 | +} |
0 commit comments