Skip to content

Re-add the optional overload for atomic ptr swaps #3121

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

Merged
merged 1 commit into from
Jun 22, 2016
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 5 additions & 3 deletions stdlib/public/core/Runtime.swift.gyb
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,7 @@ func _stdlib_atomicCompareExchangeStrongPtrImpl(
return Bool(won)
}

% for optional in ['', '?']:
/// Atomic compare and exchange of `UnsafeMutablePointer<T>` with sequentially
/// consistent memory ordering. Precise semantics are defined in C++11 or C11.
///
Expand Down Expand Up @@ -66,14 +67,15 @@ func _stdlib_atomicCompareExchangeStrongPtrImpl(
@_transparent
public // @testable
func _stdlib_atomicCompareExchangeStrongPtr<T>(
object target: UnsafeMutablePointer<UnsafeMutablePointer<T>>,
expected: UnsafeMutablePointer<UnsafeMutablePointer<T>>,
desired: UnsafeMutablePointer<T>) -> Bool {
object target: UnsafeMutablePointer<UnsafeMutablePointer<T>${optional}>,
expected: UnsafeMutablePointer<UnsafeMutablePointer<T>${optional}>,
desired: UnsafeMutablePointer<T>${optional}) -> Bool {
return _stdlib_atomicCompareExchangeStrongPtrImpl(
object: UnsafeMutablePointer(target),
expected: UnsafeMutablePointer(expected),
desired: UnsafeMutablePointer(desired))
}
% end

@_transparent
@discardableResult
Expand Down
21 changes: 12 additions & 9 deletions test/1_stdlib/Runtime.swift → test/1_stdlib/Runtime.swift.gyb
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
// RUN: rm -rf %t && mkdir %t
//
// RUN: %target-build-swift -parse-stdlib -module-name a %s -o %t.out
// RUN: %gyb %s -o %t/Runtime.swift
// RUN: %target-build-swift -parse-stdlib -module-name a %t/Runtime.swift -o %t.out
// RUN: %target-run %t.out
// REQUIRES: executable_test

Expand Down Expand Up @@ -356,11 +357,12 @@ Runtime.test("demangleName") {
expectEqual("Foobar", _stdlib_demangleName("_TtC13__lldb_expr_46Foobar"))
}

% for optionality in ['', '?']:
Runtime.test("_stdlib_atomicCompareExchangeStrongPtr") {
typealias IntPtr = UnsafeMutablePointer<Int>
var origP1 = IntPtr(bitPattern: 0x10101010)!
var origP2 = IntPtr(bitPattern: 0x20202020)!
var origP3 = IntPtr(bitPattern: 0x30303030)!
var origP1: IntPtr${optionality} = IntPtr(bitPattern: 0x10101010)!
var origP2: IntPtr${optionality} = IntPtr(bitPattern: 0x20202020)!
var origP3: IntPtr${optionality} = IntPtr(bitPattern: 0x30303030)!

do {
var object = origP1
Expand All @@ -383,32 +385,33 @@ Runtime.test("_stdlib_atomicCompareExchangeStrongPtr") {

struct FooStruct {
var i: Int
var object: IntPtr
var expected: IntPtr
var object: IntPtr${optionality}
var expected: IntPtr${optionality}

init(_ object: IntPtr, _ expected: IntPtr) {
init(object: IntPtr${optionality}, expected: IntPtr${optionality}) {
self.i = 0
self.object = object
self.expected = expected
}
}
do {
var foo = FooStruct(origP1, origP1)
var foo = FooStruct(object: origP1, expected: origP1)
let r = _stdlib_atomicCompareExchangeStrongPtr(
object: &foo.object, expected: &foo.expected, desired: origP2)
expectTrue(r)
expectEqual(origP2, foo.object)
expectEqual(origP1, foo.expected)
}
do {
var foo = FooStruct(origP1, origP2)
var foo = FooStruct(object: origP1, expected: origP2)
let r = _stdlib_atomicCompareExchangeStrongPtr(
object: &foo.object, expected: &foo.expected, desired: origP3)
expectFalse(r)
expectEqual(origP1, foo.object)
expectEqual(origP1, foo.expected)
}
}
% end

Runtime.test("casting AnyObject to class metatypes") {
do {
Expand Down