-
Notifications
You must be signed in to change notification settings - Fork 14
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Fix pthread_mutex_t dangling pointers #111
Changes from all commits
2ae2301
5cf11e2
a1c92c3
796f046
1709d3c
ca1a7a2
f32c157
8e12c7f
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,52 @@ | ||
<?xml version="1.0" encoding="UTF-8"?> | ||
<Scheme | ||
LastUpgradeVersion = "1220" | ||
version = "1.3"> | ||
<BuildAction | ||
parallelizeBuildables = "YES" | ||
buildImplicitDependencies = "YES"> | ||
</BuildAction> | ||
<TestAction | ||
buildConfiguration = "Debug" | ||
selectedDebuggerIdentifier = "Xcode.DebuggerFoundation.Debugger.LLDB" | ||
selectedLauncherIdentifier = "Xcode.DebuggerFoundation.Launcher.LLDB" | ||
shouldUseLaunchSchemeArgsEnv = "YES"> | ||
<Testables> | ||
<TestableReference | ||
skipped = "NO"> | ||
<BuildableReference | ||
BuildableIdentifier = "primary" | ||
BlueprintIdentifier = "F6D80B621BBBB2ED008F8574" | ||
BuildableName = "FlowTests.xctest" | ||
BlueprintName = "FlowTests" | ||
ReferencedContainer = "container:Flow.xcodeproj"> | ||
</BuildableReference> | ||
</TestableReference> | ||
</Testables> | ||
</TestAction> | ||
<LaunchAction | ||
buildConfiguration = "Debug" | ||
selectedDebuggerIdentifier = "Xcode.DebuggerFoundation.Debugger.LLDB" | ||
selectedLauncherIdentifier = "Xcode.DebuggerFoundation.Launcher.LLDB" | ||
launchStyle = "0" | ||
useCustomWorkingDirectory = "NO" | ||
ignoresPersistentStateOnLaunch = "NO" | ||
debugDocumentVersioning = "YES" | ||
debugServiceExtension = "internal" | ||
allowLocationSimulation = "YES"> | ||
</LaunchAction> | ||
<ProfileAction | ||
buildConfiguration = "Release" | ||
shouldUseLaunchSchemeArgsEnv = "YES" | ||
savedToolIdentifier = "" | ||
useCustomWorkingDirectory = "NO" | ||
debugDocumentVersioning = "YES"> | ||
</ProfileAction> | ||
<AnalyzeAction | ||
buildConfiguration = "Debug"> | ||
</AnalyzeAction> | ||
<ArchiveAction | ||
buildConfiguration = "Release" | ||
revealArchiveInOrganizer = "YES"> | ||
</ArchiveAction> | ||
</Scheme> |
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -10,8 +10,7 @@ import Foundation | |
|
||
/// A reference wrapper around a POSIX thread mutex | ||
public final class Mutex { | ||
private var _mutex = pthread_mutex_t() | ||
private var mutex: PThreadMutex { return PThreadMutex(&_mutex) } | ||
private var mutex = pthread_mutex_t() | ||
|
||
public init() { | ||
mutex.initialize() | ||
|
@@ -40,18 +39,60 @@ public final class Mutex { | |
} | ||
} | ||
|
||
enum MutexType { | ||
case normal | ||
case recursive | ||
|
||
var attrType: Int32 { | ||
switch self { | ||
case .normal: return PTHREAD_MUTEX_NORMAL | ||
case .recursive: return PTHREAD_MUTEX_RECURSIVE | ||
} | ||
} | ||
} | ||
|
||
internal extension pthread_mutex_t { | ||
|
||
mutating func initialize(as type: MutexType = .normal) { | ||
withUnsafeMutablePointer(to: &self) { | ||
$0.initialize(as: type) | ||
} | ||
} | ||
|
||
mutating func deinitialize() { | ||
withUnsafeMutablePointer(to: &self) { | ||
$0.deinitialize() | ||
} | ||
} | ||
|
||
mutating func lock() { | ||
withUnsafeMutablePointer(to: &self) { | ||
$0.lock() | ||
} | ||
} | ||
|
||
mutating func unlock() { | ||
withUnsafeMutablePointer(to: &self) { | ||
$0.unlock() | ||
} | ||
} | ||
|
||
} | ||
|
||
typealias PThreadMutex = UnsafeMutablePointer<pthread_mutex_t> | ||
|
||
/// Helper methods to work directly with a Pthread mutex pointer to avoid overhead of alloction and reference counting of using the Mutex reference type. | ||
/// - Note: You have to explicity call `initialize()` before use (typically in a class init) and `deinitialize()` when done (typically in a class deinit) | ||
extension UnsafeMutablePointer where Pointee == pthread_mutex_t { | ||
func initialize() { | ||
|
||
func initialize(as type: MutexType = .recursive) { | ||
var attr = pthread_mutexattr_t() | ||
defer { pthread_mutexattr_destroy(&attr) } | ||
guard pthread_mutexattr_init(&attr) == 0 else { | ||
preconditionFailure() | ||
} | ||
|
||
pthread_mutexattr_settype(&attr, PTHREAD_MUTEX_NORMAL) | ||
pthread_mutexattr_settype(&attr, type.attrType) | ||
|
||
guard pthread_mutex_init(self, &attr) == 0 else { | ||
preconditionFailure() | ||
|
@@ -71,23 +112,14 @@ extension UnsafeMutablePointer where Pointee == pthread_mutex_t { | |
func unlock() { | ||
pthread_mutex_unlock(self) | ||
} | ||
|
||
/// Will lock `self`, call `block`, then unlock `self` | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I removed |
||
@discardableResult | ||
func protect<T>(_ block: () throws -> T) rethrows -> T { | ||
pthread_mutex_lock(self) | ||
defer { pthread_mutex_unlock(self) } | ||
return try block() | ||
} | ||
} | ||
|
||
/// Internal helper to help manage state in stateful transforms. | ||
final class StateAndCallback<Value, State>: Disposable { | ||
var callback: ((Value) -> ())? | ||
var val: State | ||
fileprivate var disposables = [Disposable]() | ||
private var _mutex = pthread_mutex_t() | ||
fileprivate var mutex: PThreadMutex { return PThreadMutex(&_mutex) } | ||
fileprivate var mutex = pthread_mutex_t() | ||
|
||
init(state: State, callback: @escaping (Value) -> ()) { | ||
val = state | ||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Before adding the ability to initialize this as a recursive mutex, it deadlocked in
FlowTests.FutureQueueTests testBatchQueue
.