How to create UnsafeMutablePointer<UnsafeMutablePointer> in swift

jack sexton

The function:

public func OSAtomicCompareAndSwapPtr(_ __oldValue: UnsafeMutableRawPointer!, _ __newValue: UnsafeMutableRawPointer!, ___theValue: UnsafeMutablePointer<UnsafeMutableRawPointer?>!) -> Bool

requires a type of UnsafeMutablePointer<UnsafeMutableRawPointer?> for the a parameter theValue but I'm having trouble figuring out how to make one from a type UnsafeMutablePointer<T?>

Any thoughts on how I could do this?

EDIT:

for the curious about why I am trying to do this, I am trying to create a generic wrapper around this to create a bounded MPMC queue in swift as outlined in this blog post. Here is my wrapper so far

func compareAndSwap<T: AnyObject>(current: T?, future: T?, toPtr: UnsafeMutablePointer<T?>) -> Bool {
    let currentPtr = current.map(Unmanaged.passUnretained)?.toOpaque()
    let futurePtr = future.map(Unmanaged.passRetained)?.toOpaque()

    if OSAtomicCompareAndSwapPtr(currentPtr, futurePtr, ????) {
        return true
    }
    return false
kennytm

In Swift 3, use .withMemoryRebound to reinterpret-cast that "T**" pointer to a "void**" pointer:

return toPtr.withMemoryRebound(to: (UnsafeMutableRawPointer?).self, capacity: 1) {
    OSAtomicCompareAndSwapPtr(currentPtr, futurePtr, $0)
}

Note that

  • OSAtomicCompareAndSwapPtr has been deprecated in favor of the C11 functions like atomic_compare_and_exchange_strong, which unfortunately you cannot use in Swift. This is a known issue.
  • The blog post's atomic operations uses "acquire", "release" and also "relaxed" memory order. The OSAtomic libraries provides only the "seq_cst" and "relaxed" order (the Barrier vs non-Barrier variant of each function). So your Swift translation cannot be a 1:1 functional copy of the C++ code.

There is also a thin wrapper library timehop/Atomics which hides all the dangerous casts for you (it is not generic, however).

var toPtr = HOPAtomicReference(object: ...)
...
return toPtr.compareTo(current, andSetObject: future)

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

How to create UnsafeMutablePointer<CGPoint> Object in swift

How to create a UnsafeMutablePointer<UnsafeMutablePointer<UnsafeMutablePointer<Int8>>>

How to use UnsafeMutablePointer in Swift 3?

How to use UnsafeMutablePointer<OpaquePointer> in Swift?

Swift UnsafeMutablePointer & UnsafeMutablePointer<UnsafePointer<SomeType>>

How to cast self to UnsafeMutablePointer<Void> type in swift

How to dealloc UnsafeMutablePointer referenced from Swift struct

how to loop through elements of array of <UnsafeMutablePointer> in Swift

How to use UnsafeMutablePointer<T?> in swift 3

UnsafeMutablePointer to expected argument type UnsafeMutablePointer<_> in Swift 3

UnsafeMutablePointer<CFTypeRef> in Swift 3

Error in UnsafeMutablePointer in swift3

Swift 2 - UnsafeMutablePointer<Void> to object

Using UnsafeMutablePointer and CFRunLoopObserverContext in swift 2

Cast a Swift struct to UnsafeMutablePointer<Void>

What is UnsafeMutablePointer<Unmanaged<CMSampleBuffer>?> in Swift?

UnsafeMutablePointer<Bytef> swift3

How to get length of a UnsafeMutablePointer<Object> written to a OpenSSL BIO in Swift?

How to cast UnsafeMutableRawPointer! to UnsafeMutablePointer<Float> in Swift 3.0 or newer?

How to do pointer arithmetic on UnsafeMutablePointer and get String in Swift 3

How to convert String to UnsafeMutablePointer<UInt8> ? Swift 3

Swift: Cannot convert value of type 'UnsafeMutablePointer' to expected argument type 'UnsafeMutablePointer'

In Swift 3.1, UnsafeMutablePointer.initialize(from:) is deprecated

Swift convert Data to UnsafeMutablePointer<Int8>

Cast a swift dictionary ( [String:AnyObject] ) to a UnsafeMutablePointer

Convert UnsafeMutableRawPointer to UnsafeMutablePointer<T> in swift 3

Swift convert string to UnsafeMutablePointer<Int8>

Cannot assign value of type UnsafeMutablePointer ObjCBool in Swift

Memset to UnsafeMutablePointer<UInt8> in swift