-
Notifications
You must be signed in to change notification settings - Fork 13
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
WIP: Remove warnings #102
base: master
Are you sure you want to change the base?
WIP: Remove warnings #102
Changes from all commits
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 |
---|---|---|
|
@@ -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() | ||
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 saw this comment: /// 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 { I'm trying to remember what the reason we the 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. Hmm, ok this solution might not be correct at all. I thought that because 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. @niil-ohlin do you remember why this didn't work? I opened #104 now and saw your PR |
||
|
||
public init() { | ||
mutex.initialize() | ||
|
@@ -30,13 +29,27 @@ public final class Mutex { | |
public func unlock() { | ||
mutex.unlock() | ||
} | ||
} | ||
|
||
/// Will lock `self`, call `block`, then unlock `self` | ||
@discardableResult | ||
public func protect<T>(_ block: () throws -> T) rethrows -> T { | ||
mutex.lock() | ||
defer { mutex.unlock() } | ||
return try block() | ||
extension pthread_mutex_t { | ||
mutating func withPointer<T>(_ body: (PThreadMutex) throws -> T) rethrows -> T { | ||
return try withUnsafeMutablePointer(to: &self, body) | ||
} | ||
|
||
mutating func initialize() { | ||
withPointer { $0.initialize() } | ||
} | ||
|
||
mutating func deinitialize() { | ||
withPointer { $0.deinitialize() } | ||
} | ||
|
||
mutating func lock() { | ||
withPointer { $0.lock() } | ||
} | ||
|
||
mutating func unlock() { | ||
withPointer { $0.unlock() } | ||
} | ||
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. Why not add 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. It caused crashes when I used 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. How did your code look like and did the log specify why it crashed (e.g. exclusivity violation)? |
||
} | ||
|
||
|
@@ -86,8 +99,7 @@ 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) } | ||
private var mutex = pthread_mutex_t() | ||
|
||
init(state: State, callback: @escaping (Value) -> ()) { | ||
val = state | ||
|
@@ -192,12 +204,12 @@ extension StateAndCallback where Value == () { | |
|
||
func +=<Value, State>(bag: StateAndCallback<Value, State>, disposable: Disposable?) { | ||
guard let disposable = disposable else { return } | ||
bag.mutex.lock() | ||
bag.lock() | ||
let hasBeenDisposed = bag.callback == nil | ||
if !hasBeenDisposed { | ||
bag.disposables.append(disposable) | ||
} | ||
bag.mutex.unlock() | ||
bag.unlock() | ||
if hasBeenDisposed { | ||
disposable.dispose() | ||
} | ||
|
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.
Are you allowed to add extension to PThreadMutex (UnsafeMutablePointer<pthread_mutex_t>)? Otherwise perhaps create a thin wrapper around it and use that instead?
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.
good idea! I'll try that!