Mutex.swift 4.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150
  1. //===----------------------------------------------------------------------===//
  2. //
  3. // This source file is part of the Swift Argument Parser open source project
  4. //
  5. // Copyright (c) 2020 Apple Inc. and the Swift project authors
  6. // Licensed under Apache License v2.0 with Runtime Library Exception
  7. //
  8. // See https://swift.org/LICENSE.txt for license information
  9. //
  10. //===----------------------------------------------------------------------===//
  11. #if canImport(os)
  12. #if compiler(>=6.0)
  13. import os
  14. #if canImport(C.os.lock)
  15. import C.os.lock
  16. #endif
  17. #else
  18. import os
  19. #if canImport(C.os.lock)
  20. import C.os.lock
  21. #endif
  22. #endif
  23. #elseif canImport(Bionic)
  24. @preconcurrency import Bionic
  25. #elseif canImport(Glibc)
  26. @preconcurrency import Glibc
  27. #elseif canImport(Musl)
  28. @preconcurrency import Musl
  29. #elseif canImport(WinSDK)
  30. import WinSDK
  31. #endif
  32. struct ArgParserMutex<State> {
  33. // Internal implementation for a cheap lock to aid sharing code across platforms
  34. private struct _Lock {
  35. #if canImport(os)
  36. typealias Primitive = os_unfair_lock
  37. #elseif os(FreeBSD) || os(OpenBSD)
  38. typealias Primitive = pthread_mutex_t?
  39. #elseif canImport(Bionic) || canImport(Glibc) || canImport(Musl)
  40. typealias Primitive = pthread_mutex_t
  41. #elseif canImport(WinSDK)
  42. typealias Primitive = SRWLOCK
  43. #elseif os(WASI)
  44. // WASI is single-threaded, so we don't need a lock.
  45. typealias Primitive = Void
  46. #endif
  47. typealias PlatformLock = UnsafeMutablePointer<Primitive>
  48. var _platformLock: PlatformLock
  49. fileprivate static func initialize(_ platformLock: PlatformLock) {
  50. #if canImport(os)
  51. platformLock.initialize(to: os_unfair_lock())
  52. #elseif canImport(Bionic) || canImport(Glibc) || canImport(Musl)
  53. pthread_mutex_init(platformLock, nil)
  54. #elseif canImport(WinSDK)
  55. InitializeSRWLock(platformLock)
  56. #elseif os(WASI)
  57. // no-op
  58. #else
  59. #error("Lock._Lock.initialize is unimplemented on this platform")
  60. #endif
  61. }
  62. fileprivate static func deinitialize(_ platformLock: PlatformLock) {
  63. #if canImport(Bionic) || canImport(Glibc) || canImport(Musl)
  64. pthread_mutex_destroy(platformLock)
  65. #endif
  66. platformLock.deinitialize(count: 1)
  67. }
  68. static fileprivate func lock(_ platformLock: PlatformLock) {
  69. #if canImport(os)
  70. os_unfair_lock_lock(platformLock)
  71. #elseif canImport(Bionic) || canImport(Glibc) || canImport(Musl)
  72. pthread_mutex_lock(platformLock)
  73. #elseif canImport(WinSDK)
  74. AcquireSRWLockExclusive(platformLock)
  75. #elseif os(WASI)
  76. // no-op
  77. #else
  78. #error("Lock._Lock.lock is unimplemented on this platform")
  79. #endif
  80. }
  81. static fileprivate func unlock(_ platformLock: PlatformLock) {
  82. #if canImport(os)
  83. os_unfair_lock_unlock(platformLock)
  84. #elseif canImport(Bionic) || canImport(Glibc) || canImport(Musl)
  85. pthread_mutex_unlock(platformLock)
  86. #elseif canImport(WinSDK)
  87. ReleaseSRWLockExclusive(platformLock)
  88. #elseif os(WASI)
  89. // no-op
  90. #else
  91. #error("Lock._Lock.unlock is unimplemented on this platform")
  92. #endif
  93. }
  94. }
  95. private class _Buffer: ManagedBuffer<State, _Lock.Primitive> {
  96. deinit {
  97. withUnsafeMutablePointerToElements {
  98. _Lock.deinitialize($0)
  99. }
  100. }
  101. }
  102. private let _buffer: ManagedBuffer<State, _Lock.Primitive>
  103. init(_ initialState: State) {
  104. _buffer = _Buffer.create(
  105. minimumCapacity: 1,
  106. makingHeaderWith: { buf in
  107. buf.withUnsafeMutablePointerToElements {
  108. _Lock.initialize($0)
  109. }
  110. return initialState
  111. })
  112. }
  113. func withLock<T>(_ body: @Sendable (inout State) throws -> T) rethrows -> T {
  114. try withLockUnchecked(body)
  115. }
  116. func withLockUnchecked<T>(_ body: (inout State) throws -> T) rethrows -> T {
  117. try _buffer.withUnsafeMutablePointers { state, lock in
  118. _Lock.lock(lock)
  119. defer { _Lock.unlock(lock) }
  120. return try body(&state.pointee)
  121. }
  122. }
  123. // Ensures the managed state outlives the locked scope.
  124. func withLockExtendingLifetimeOfState<T>(
  125. _ body: @Sendable (inout State) throws -> T
  126. ) rethrows -> T {
  127. try _buffer.withUnsafeMutablePointers { state, lock in
  128. _Lock.lock(lock)
  129. return try withExtendedLifetime(state.pointee) {
  130. defer { _Lock.unlock(lock) }
  131. return try body(&state.pointee)
  132. }
  133. }
  134. }
  135. }
  136. extension ArgParserMutex: @unchecked Sendable where State: Sendable {}