Initial commit.

This commit is contained in:
Karoy Lorentey committed 2020-10-01 16:57:36 -07:00
commit b97750eb18
44 files changed
+79658

No files matched your search

+9
View File
@@ -0,0 +1,9 @@
# Byte compiled Python modules.
*.pyc
# Finder metadata
.DS_Store
# Generated files from SPM and Xcode.
/.build
/.swiftpm
+55
View File
@@ -0,0 +1,55 @@
# Code of Conduct
To be a truly great community, Swift.org needs to welcome developers from all walks of life,
with different backgrounds, and with a wide range of experience. A diverse and friendly
community will have more great ideas, more unique perspectives, and produce more great
code. We will work diligently to make the Swift community welcoming to everyone.
To give clarity of what is expected of our members, Swift.org has adopted the code of conduct
defined by [contributor-covenant.org](https://www.contributor-covenant.org). This document is used across many open source
communities, and we think it articulates our values well. The full text is copied below:
### Contributor Code of Conduct v1.3
As contributors and maintainers of this project, and in the interest of fostering an open and
welcoming community, we pledge to respect all people who contribute through reporting
issues, posting feature requests, updating documentation, submitting pull requests or patches,
and other activities.
We are committed to making participation in this project a harassment-free experience for
everyone, regardless of level of experience, gender, gender identity and expression, sexual
orientation, disability, personal appearance, body size, race, ethnicity, age, religion, or
nationality.
Examples of unacceptable behavior by participants include:
- The use of sexualized language or imagery
- Personal attacks
- Trolling or insulting/derogatory comments
- Public or private harassment
- Publishing other’s private information, such as physical or electronic addresses, without explicit permission
- Other unethical or unprofessional conduct
Project maintainers have the right and responsibility to remove, edit, or reject comments,
commits, code, wiki edits, issues, and other contributions that are not aligned to this Code of
Conduct, or to ban temporarily or permanently any contributor for other behaviors that they
deem inappropriate, threatening, offensive, or harmful.
By adopting this Code of Conduct, project maintainers commit themselves to fairly and
consistently applying these principles to every aspect of managing this project. Project
maintainers who do not follow or enforce the Code of Conduct may be permanently removed
from the project team.
This code of conduct applies both within project spaces and in public spaces when an
individual is representing the project or its community.
Instances of abusive, harassing, or otherwise unacceptable behavior may be reported by
contacting a project maintainer at [conduct@swift.org](mailto:conduct@swift.org). All complaints will be reviewed and
investigated and will result in a response that is deemed necessary and appropriate to the
circumstances. Maintainers are obligated to maintain confidentiality with regard to the reporter
of an incident.
*This policy is adapted from the Contributor Code of Conduct [version 1.3.0](http://contributor-covenant.org/version/1/3/0/).*
### Reporting
A working group of community members is committed to promptly addressing any [reported
issues](mailto:conduct@swift.org). Working group members are volunteers appointed by the project lead, with a
preference for individuals with varied backgrounds and perspectives. Membership is expected
to change regularly, and may grow or shrink.
+10
View File
@@ -0,0 +1,10 @@
By submitting a pull request, you represent that you have the right to license
your contribution to Apple and the community, and agree by submitting the patch
that your contributions are licensed under the [Swift
license](https://swift.org/LICENSE.txt).
---
Before submitting the pull request, please make sure you have tested your
changes and that they follow the Swift project [guidelines for contributing
code](https://swift.org/contributing/#contributing-code).
+211
View File
@@ -0,0 +1,211 @@
Apache License
Version 2.0, January 2004
http://www.apache.org/licenses/
TERMS AND CONDITIONS FOR USE, REPRODUCTION, AND DISTRIBUTION
1. Definitions.
"License" shall mean the terms and conditions for use, reproduction,
and distribution as defined by Sections 1 through 9 of this document.
"Licensor" shall mean the copyright owner or entity authorized by
the copyright owner that is granting the License.
"Legal Entity" shall mean the union of the acting entity and all
other entities that control, are controlled by, or are under common
control with that entity. For the purposes of this definition,
"control" means (i) the power, direct or indirect, to cause the
direction or management of such entity, whether by contract or
otherwise, or (ii) ownership of fifty percent (50%) or more of the
outstanding shares, or (iii) beneficial ownership of such entity.
"You" (or "Your") shall mean an individual or Legal Entity
exercising permissions granted by this License.
"Source" form shall mean the preferred form for making modifications,
including but not limited to software source code, documentation
source, and configuration files.
"Object" form shall mean any form resulting from mechanical
transformation or translation of a Source form, including but
not limited to compiled object code, generated documentation,
and conversions to other media types.
"Work" shall mean the work of authorship, whether in Source or
Object form, made available under the License, as indicated by a
copyright notice that is included in or attached to the work
(an example is provided in the Appendix below).
"Derivative Works" shall mean any work, whether in Source or Object
form, that is based on (or derived from) the Work and for which the
editorial revisions, annotations, elaborations, or other modifications
represent, as a whole, an original work of authorship. For the purposes
of this License, Derivative Works shall not include works that remain
separable from, or merely link (or bind by name) to the interfaces of,
the Work and Derivative Works thereof.
"Contribution" shall mean any work of authorship, including
the original version of the Work and any modifications or additions
to that Work or Derivative Works thereof, that is intentionally
submitted to Licensor for inclusion in the Work by the copyright owner
or by an individual or Legal Entity authorized to submit on behalf of
the copyright owner. For the purposes of this definition, "submitted"
means any form of electronic, verbal, or written communication sent
to the Licensor or its representatives, including but not limited to
communication on electronic mailing lists, source code control systems,
and issue tracking systems that are managed by, or on behalf of, the
Licensor for the purpose of discussing and improving the Work, but
excluding communication that is conspicuously marked or otherwise
designated in writing by the copyright owner as "Not a Contribution."
"Contributor" shall mean Licensor and any individual or Legal Entity
on behalf of whom a Contribution has been received by Licensor and
subsequently incorporated within the Work.
2. Grant of Copyright License. Subject to the terms and conditions of
this License, each Contributor hereby grants to You a perpetual,
worldwide, non-exclusive, no-charge, royalty-free, irrevocable
copyright license to reproduce, prepare Derivative Works of,
publicly display, publicly perform, sublicense, and distribute the
Work and such Derivative Works in Source or Object form.
3. Grant of Patent License. Subject to the terms and conditions of
this License, each Contributor hereby grants to You a perpetual,
worldwide, non-exclusive, no-charge, royalty-free, irrevocable
(except as stated in this section) patent license to make, have made,
use, offer to sell, sell, import, and otherwise transfer the Work,
where such license applies only to those patent claims licensable
by such Contributor that are necessarily infringed by their
Contribution(s) alone or by combination of their Contribution(s)
with the Work to which such Contribution(s) was submitted. If You
institute patent litigation against any entity (including a
cross-claim or counterclaim in a lawsuit) alleging that the Work
or a Contribution incorporated within the Work constitutes direct
or contributory patent infringement, then any patent licenses
granted to You under this License for that Work shall terminate
as of the date such litigation is filed.
4. Redistribution. You may reproduce and distribute copies of the
Work or Derivative Works thereof in any medium, with or without
modifications, and in Source or Object form, provided that You
meet the following conditions:
(a) You must give any other recipients of the Work or
Derivative Works a copy of this License; and
(b) You must cause any modified files to carry prominent notices
stating that You changed the files; and
(c) You must retain, in the Source form of any Derivative Works
that You distribute, all copyright, patent, trademark, and
attribution notices from the Source form of the Work,
excluding those notices that do not pertain to any part of
the Derivative Works; and
(d) If the Work includes a "NOTICE" text file as part of its
distribution, then any Derivative Works that You distribute must
include a readable copy of the attribution notices contained
within such NOTICE file, excluding those notices that do not
pertain to any part of the Derivative Works, in at least one
of the following places: within a NOTICE text file distributed
as part of the Derivative Works; within the Source form or
documentation, if provided along with the Derivative Works; or,
within a display generated by the Derivative Works, if and
wherever such third-party notices normally appear. The contents
of the NOTICE file are for informational purposes only and
do not modify the License. You may add Your own attribution
notices within Derivative Works that You distribute, alongside
or as an addendum to the NOTICE text from the Work, provided
that such additional attribution notices cannot be construed
as modifying the License.
You may add Your own copyright statement to Your modifications and
may provide additional or different license terms and conditions
for use, reproduction, or distribution of Your modifications, or
for any such Derivative Works as a whole, provided Your use,
reproduction, and distribution of the Work otherwise complies with
the conditions stated in this License.
5. Submission of Contributions. Unless You explicitly state otherwise,
any Contribution intentionally submitted for inclusion in the Work
by You to the Licensor shall be under the terms and conditions of
this License, without any additional terms or conditions.
Notwithstanding the above, nothing herein shall supersede or modify
the terms of any separate license agreement you may have executed
with Licensor regarding such Contributions.
6. Trademarks. This License does not grant permission to use the trade
names, trademarks, service marks, or product names of the Licensor,
except as required for reasonable and customary use in describing the
origin of the Work and reproducing the content of the NOTICE file.
7. Disclaimer of Warranty. Unless required by applicable law or
agreed to in writing, Licensor provides the Work (and each
Contributor provides its Contributions) on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or
implied, including, without limitation, any warranties or conditions
of TITLE, NON-INFRINGEMENT, MERCHANTABILITY, or FITNESS FOR A
PARTICULAR PURPOSE. You are solely responsible for determining the
appropriateness of using or redistributing the Work and assume any
risks associated with Your exercise of permissions under this License.
8. Limitation of Liability. In no event and under no legal theory,
whether in tort (including negligence), contract, or otherwise,
unless required by applicable law (such as deliberate and grossly
negligent acts) or agreed to in writing, shall any Contributor be
liable to You for damages, including any direct, indirect, special,
incidental, or consequential damages of any character arising as a
result of this License or out of the use or inability to use the
Work (including but not limited to damages for loss of goodwill,
work stoppage, computer failure or malfunction, or any and all
other commercial damages or losses), even if such Contributor
has been advised of the possibility of such damages.
9. Accepting Warranty or Additional Liability. While redistributing
the Work or Derivative Works thereof, You may choose to offer,
and charge a fee for, acceptance of support, warranty, indemnity,
or other liability obligations and/or rights consistent with this
License. However, in accepting such obligations, You may act only
on Your own behalf and on Your sole responsibility, not on behalf
of any other Contributor, and only if You agree to indemnify,
defend, and hold each Contributor harmless for any liability
incurred by, or claims asserted against, such Contributor by reason
of your accepting any such warranty or additional liability.
END OF TERMS AND CONDITIONS
APPENDIX: How to apply the Apache License to your work.
To apply the Apache License to your work, attach the following
boilerplate notice, with the fields enclosed by brackets "[]"
replaced with your own identifying information. (Don't include
the brackets!) The text should be enclosed in the appropriate
comment syntax for the file format. We also recommend that a
file or class name and description of purpose be included on the
same "printed page" as the copyright notice for easier
identification within third-party archives.
Copyright [yyyy] [name of copyright owner]
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
## Runtime Library Exception to the Apache 2.0 License: ##
As an exception, if you use this Software to compile your source code and
portions of this Software are embedded into the binary product as a result,
you may redistribute such product without providing attribution as would
otherwise be required by Sections 4(a), 4(b) and 4(d) of the License.
+35
View File
@@ -0,0 +1,35 @@
// swift-tools-version:5.1
//===----------------------------------------------------------------------===//
//
// This source file is part of the Swift.org open source project
//
// Copyright (c) 2020 Apple Inc. and the Swift project authors
// Licensed under Apache License v2.0 with Runtime Library Exception
//
// See https://swift.org/LICENSE.txt for license information
// See https://swift.org/CONTRIBUTORS.txt for the list of Swift project authors
//
//===----------------------------------------------------------------------===//
import PackageDescription
let package = Package(
name: "swift-atomics",
products: [
.library(
name: "Atomics",
targets: ["Atomics"]),
],
targets: [
.target(name: "_AtomicsShims"),
.target(
name: "Atomics",
dependencies: ["_AtomicsShims"],
path: "Sources/Atomics"
),
.testTarget(
name: "AtomicsTests",
dependencies: ["Atomics"]
),
]
)
+166
View File
@@ -0,0 +1,166 @@
# Swift Atomics ⚛︎︎
[SE-0282]: https://github.com/apple/swift-evolution/blob/master/proposals/0282-atomics.md
[SE-0282r0]: https://github.com/apple/swift-evolution/blob/3a358a07e878a58bec256639d2beb48461fc3177/proposals/0282-atomics.md
This package implements an atomics library for Swift, providing atomic operations for a variety of Swift types, including integers and pointer values. The goal is to enable intrepid developers to start building synchronization constructs directly in Swift.
Atomic operations aren't subject to the usual exclusivity rules. The same memory location may be safely read and updated from multiple concurrent threads of execution, as long as all such access is done through atomic operations. For example, here is a trivial atomic counter:
``` swift
import Atomics
import Dispatch
let counter = ManagedAtomic<Int>(0)
DispatchQueue.concurrentPerform(iterations: 10) { _ in
for _ in 0 ..< 1_000_000 {
counter.wrappingIncrement(ordering: .relaxed)
}
}
counter.load(ordering: .relaxed) // ⟹ 10_000_000
```
The only way to access the counter value is to use one of the methods provided by `ManagedAtomic`, each of which implement a particular atomic operation, and each of which require an explicit ordering value. (Swift supports a subset of the C/C++ memory orderings.)
## Getting Started
To use `Atomics` in your own project, you need to set it up as a package dependency:
```swift
// swift-tools-version:5.3
import PackageDescription
let package = Package(
name: "MyPackage",
dependencies: [
.package(
url: "https://github.com/apple/swift-atomics.git",
from: "0.0.1"
)
],
targets: [
.target(
name: "MyTarget",
dependencies: [
.product(name: "Atomics", package: "swift-atomics")
]
)
]
)
```
Because `Atomics` has not reached version 1.0 yet, source stability is only guaranteed within minor versions (e.g. between 0.0.1 and 0.0.2). If you don't want potentially source-breaking package updates, use this dependency specification instead:
```swift
.package(
url: "https://github.com/apple/swift-atomics.git",
.upToNextMinor(from: "0.0.1")
)
```
## Supported Types
The package implements atomic operations for the following Swift constructs, all of which conform to the public `AtomicValue` protocol:
- Standard signed integer types (`Int`, `Int64`, `Int32`, `Int16`, `Int8`)
- Standard unsigned integer types (`UInt`, `UInt64`, `UInt32`, `UInt16`, `UInt8`)
- Booleans (`Bool`)
- Standard pointer types (`UnsafeRawPointer`, `UnsafeMutableRawPointer`, `UnsafePointer<T>`, `UnsafeMutablePointer<T>`), along with their optional-wrapped forms (such as `Optional<UnsafePointer<T>>`)
- Unmanaged references (`Unmanaged<T>`, `Optional<Unmanaged<T>>`)
- A special `DoubleWord` type that consists of two `UInt` values, `low` and `high`, providing double-wide atomic primitives
- Any `RawRepresentable` type whose `RawValue` is in turn an atomic type (such as simple custom enum types)
- Strong references to class instances that opted into atomic use (by conforming to the `AtomicReference` protocol)
Of particular note is full support for atomic strong references. This provides a convenient memory reclamation solution for concurrent data structures that fits perfectly with Swift's reference counting memory management model. (Atomic strong references are implemented in terms of `DoubleWord` operations.) However, accessing an atomic strong reference is (relatively) expensive, so we also provide a separate set of efficient constructs (`ManagedAtomicLazyReference` and `UnsafeAtomicLazyReference`) for the common case of a lazily initialized (but otherwise constant) atomic strong reference.
## Lock-Free vs Wait-Free Operations
All atomic operations exposed by this package are guaranteed to have lock-free implementations. However, we do not guarantee wait-free operation -- depending on the capabilities of the target platform, some of the exposed operations may be implemented by compare-and-exchange loops. That said, all atomic operations map directly to dedicated CPU instructions where available -- to the extent supported by llvm & Clang.
## Memory Management
Atomic access is implemented in terms of dedicated atomic storage representations that are kept distinct from the corresponding regular (non-atomic) type. (E.g., the actual integer value underlying the counter above isn't directly accessible.) This has several advantages:
- it helps prevent accidental non-atomic access to atomic variables,
- it enables custom storage representations (such as the one used by atomic strong references), and
- it is a better fit with the standard C atomics library that we use to implement the actual operations (as enabled by [SE-0282]).
[SE-0282]: https://github.com/apple/swift-evolution/blob/master/proposals/0282-atomics.md
While the underlying pointer-based atomic operations are exposed as static methods on the corresponding `AtomicStorage` types, we strongly recommend the use of higher-level atomic wrappers to manage the details of preparing/disposing atomic storage. This version of the library provides two wrapper types:
- an easy to use, memory-safe `ManagedAtomic<T>` generic class and
- a less convenient, but more flexible `UnsafeAtomic<T>` generic struct.
Both constructs provide the following operations on all `AtomicValue` types:
```swift
func load(ordering: AtomicLoadOrdering) -> Value
func store(_ desired: Value, ordering: AtomicStoreOrdering)
func exchange(_ desired: Value, ordering: AtomicUpdateOrdering) -> Value
func compareExchange(
expected: Value,
desired: Value,
ordering: AtomicUpdateOrdering
) -> (exchanged: Bool, original: Value)
func compareExchange(
expected: Value,
desired: Value,
successOrdering: AtomicUpdateOrdering,
failureOrdering: AtomicLoadOrdering
) -> (exchanged: Bool, original: Value)
func weakCompareExchange(
expected: Value,
desired: Value,
successOrdering: AtomicUpdateOrdering,
failureOrdering: AtomicLoadOrdering
) -> (exchanged: Bool, original: Value)
```
Integer types come with additional atomic operations for incrementing or decrementing values and bitwise logical operations. `Bool` provides select additional boolean operations along the same vein.
For an introduction to the APIs provided by this package, for now please see the [first version of SE-0282][SE-0282r0].
Note that when/if Swift gains support for non-copiable types, we expect to replace both `ManagedAtomic` and `UnsafeAtomic` with a single move-only atomic struct that combines the performance and versatility of `UnsafeAtomic` with the ease-of-use and memory safety of `ManagedAtomic`.
The current version of the `Atomics` module does not implement APIs for tagged atomics (see issue #5), although it does expose a `DoubleWord` type that can be used to implement them. (Atomic strong references are already implemented in terms of `DoubleWord`, although in their current form they do not expose any user-customizable bits.)
## Contributing
Swift Atomics is a standalone library separate from the core Swift project. We expect some of the atomics APIs may eventually get incorporated into the Swift Standard Library. If and when that happens such changes will be proposed to the Swift Standard Library using the established evolution process of the Swift project.
This library is licensed under the [Swift License]. For more information, see the Swift.org [Community Guidelines], [Contribution Guidelines], as well as the files [LICENSE.txt](./LICENSE.txt), [CONTRIBUTING.md](./CONTRIBUTING.md) and [CODE_OF_CONDUCT.md](./CODE_OF_CONDUCT.md) at the root of this repository.
Swift Atomics uses GitHub issues to track bugs and enhancement requests. We use pull requests for development.
[Swift License]: https://swift.org/LICENSE.txt
[Community Guidelines]: https://swift.org/community/
[Contribution Guidelines]: https://swift.org/contributing/
## Development
This package defines a large number of similar-but-not-quite-the-same operations. To make it easier to maintain these, we use code generation to produce these.
A number of [source files](./Sources/Atomics) have a `.swift.gyb` extension. These are using a Python-based code generation utility called [gyb](./Utilities/gyb.py) which we also use within the Swift Standard Library (the name is short for Generate Your Boilerplate). To make sure the package remains buildable by SPM, the autogenerated output files are committed into this repository. You must never edit the contents of `autogenerated` subdirectories, or your changes will get overwritten the next time the code is regenerated.
To regenerate sources (and to update the inventory of XCTest tests), you need to manually run the script [`generate-sources`](./generate-sources) in the root of this repository. This needs to be done every time you modify one of the template files.
The same script also runs `swift test --generate-linuxmain` to register any newly added unit tests.
In addition to gyb, the [`_AtomicsShims.h`](./Sources/_AtomicsShims/include/_AtomicsShims.h) header file uses the C preprocessor to define trivial wrapper functions for every supported atomic operation -- memory ordering pairing.
⚛︎︎
<!-- Local Variables: -->
<!-- mode: markdown -->
<!-- fill-column: 10000 -->
<!-- eval: (setq-local whitespace-style '(face tabs newline empty)) -->
<!-- eval: (whitespace-mode 1) -->
<!-- eval: (visual-line-mode 1) -->
<!-- End: -->
+269
View File
@@ -0,0 +1,269 @@
//===----------------------------------------------------------------------===//
//
// This source file is part of the Swift Atomics open source project
//
// Copyright (c) 2020 Apple Inc. and the Swift project authors
// Licensed under Apache License v2.0 with Runtime Library Exception
//
// See https://swift.org/LICENSE.txt for license information
// See https://swift.org/CONTRIBUTORS.txt for the list of Swift project authors
//
//===----------------------------------------------------------------------===//
%{
from gyb_utils import (
autogenerated_warning, loadOrderings, storeOrderings, updateOrderings,
boolOperations, lowerFirst, argLabel, shimOrders)
}%
${autogenerated_warning()}
import _AtomicsShims
extension Bool: AtomicValue {
public struct AtomicRepresentation {
public typealias Value = Bool
@usableFromInline
internal var _storage: _AtomicBoolStorage
@inline(__always) @_alwaysEmitIntoClient
public init(_ value: Bool) {
_storage = _sa_prepare_Bool(value)
}
@inline(__always) @_alwaysEmitIntoClient
public func dispose() -> Value {
_sa_dispose_Bool(_storage)
}
}
}
extension Bool.AtomicRepresentation {
@_transparent @_alwaysEmitIntoClient
@usableFromInline
static func _extract(
_ ptr: UnsafeMutablePointer<Self>
) -> UnsafeMutablePointer<_AtomicBoolStorage> {
// `Self` is layout-compatible with its only stored property.
return UnsafeMutableRawPointer(ptr)
.assumingMemoryBound(to: _AtomicBoolStorage.self)
}
}
extension Bool.AtomicRepresentation: AtomicStorage {
@_semantics("atomics.requires_constant_orderings")
@_transparent @_alwaysEmitIntoClient
public static func atomicLoad(
at pointer: UnsafeMutablePointer<Bool.AtomicRepresentation>,
ordering: AtomicLoadOrdering
) -> Bool {
switch ordering {
% for (swiftOrder, shimOrder) in loadOrderings:
case .${swiftOrder}:
return _sa_load_${shimOrder}_Bool(_extract(pointer))
% end
default:
fatalError("Unsupported ordering")
}
}
@_semantics("atomics.requires_constant_orderings")
@_transparent @_alwaysEmitIntoClient
public static func atomicStore(
_ desired: __owned Bool,
at pointer: UnsafeMutablePointer<Bool.AtomicRepresentation>,
ordering: AtomicStoreOrdering
) {
switch ordering {
% for (swiftOrder, shimOrder) in storeOrderings:
case .${swiftOrder}:
_sa_store_${shimOrder}_Bool(_extract(pointer), desired)
% end
default:
fatalError("Unsupported ordering")
}
}
@_semantics("atomics.requires_constant_orderings")
@_transparent @_alwaysEmitIntoClient
public static func atomicExchange(
_ desired: __owned Bool,
at pointer: UnsafeMutablePointer<Bool.AtomicRepresentation>,
ordering: AtomicUpdateOrdering
) -> Bool {
switch ordering {
% for (swiftOrder, shimOrder, _) in updateOrderings:
case .${swiftOrder}:
return _sa_exchange_${shimOrder}_Bool(_extract(pointer), desired)
% end
default:
fatalError("Unsupported ordering")
}
}
@_semantics("atomics.requires_constant_orderings")
@_transparent @_alwaysEmitIntoClient
public static func atomicCompareExchange(
expected: Bool,
desired: __owned Bool,
at pointer: UnsafeMutablePointer<Bool.AtomicRepresentation>,
ordering: AtomicUpdateOrdering
) -> (exchanged: Bool, original: Bool) {
var expected = expected
let exchanged: Bool
switch ordering {
% for (swiftOrder, shimOrder, failOrder) in updateOrderings:
case .${swiftOrder}:
exchanged = _sa_cmpxchg_strong_${shimOrder}_${failOrder}_Bool(
_extract(pointer),
&expected, desired)
% end
default:
fatalError("Unsupported ordering")
}
return (exchanged, expected)
}
@_semantics("atomics.requires_constant_orderings")
@_transparent @_alwaysEmitIntoClient
public static func atomicCompareExchange(
expected: Bool,
desired: __owned Bool,
at pointer: UnsafeMutablePointer<Bool.AtomicRepresentation>,
successOrdering: AtomicUpdateOrdering,
failureOrdering: AtomicLoadOrdering
) -> (exchanged: Bool, original: Bool) {
var expected = expected
let exchanged: Bool
// FIXME: stdatomic.h (and LLVM underneath) doesn't support
// arbitrary ordering combinations yet, so upgrade the success
// ordering when necessary so that it is at least as "strong" as
// the failure case.
switch (successOrdering, failureOrdering) {
% for (swiftSuccess, shimSuccess, _) in updateOrderings:
% for (swiftFailure, shimFailure) in loadOrderings:
case (.${swiftSuccess}, .${swiftFailure}):
exchanged = _sa_cmpxchg_strong_${shimOrders(shimSuccess, shimFailure)}_Bool(
_extract(pointer),
&expected,
desired)
% end
% end
default:
fatalError("Unsupported ordering")
}
return (exchanged, expected)
}
@_semantics("atomics.requires_constant_orderings")
@_transparent @_alwaysEmitIntoClient
public static func atomicWeakCompareExchange(
expected: Bool,
desired: __owned Bool,
at pointer: UnsafeMutablePointer<Bool.AtomicRepresentation>,
successOrdering: AtomicUpdateOrdering,
failureOrdering: AtomicLoadOrdering
) -> (exchanged: Bool, original: Bool) {
var expected = expected
let exchanged: Bool
// FIXME: stdatomic.h (and LLVM underneath) doesn't support
// arbitrary ordering combinations yet, so upgrade the success
// ordering when necessary so that it is at least as "strong" as
// the failure case.
switch (successOrdering, failureOrdering) {
% for (swiftSuccess, shimSuccess, _) in updateOrderings:
% for (swiftFailure, shimFailure) in loadOrderings:
case (.${swiftSuccess}, .${swiftFailure}):
exchanged = _sa_cmpxchg_weak_${shimOrders(shimSuccess, shimFailure)}_Bool(
_extract(pointer),
&expected,
desired)
% end
% end
default:
fatalError("Unsupported ordering")
}
return (exchanged, expected)
}
}
// MARK: - Additional operations
extension Bool.AtomicRepresentation {
% for (name, cname, op, label, doc) in boolOperations:
/// Perform an atomic ${doc} operation on the value referenced by
/// `pointer` and return the original value, applying the specified memory
/// ordering.
///
/// - Parameter operand: A boolean value.
/// - Parameter pointer: A memory location previously initialized with a value
/// returned by `prepareAtomicRepresentation(for:)`.
/// - Parameter ordering: The memory ordering to apply on this operation.
/// - Returns: The original value before the operation.
@_semantics("atomics.requires_constant_orderings")
@_transparent @_alwaysEmitIntoClient
static func atomicLoadThen${name}(
${label} operand: Value,
at pointer: UnsafeMutablePointer<Self>,
ordering: AtomicUpdateOrdering
) -> Value {
switch ordering {
% for (swiftOrder, shimOrder, _) in updateOrderings:
case .${swiftOrder}:
return _sa_fetch_${cname}_${shimOrder}_Bool(
_extract(pointer),
operand)
% end
default:
fatalError("Unsupported ordering")
}
}
% end
}
% for construct in ["UnsafeAtomic", "ManagedAtomic"]:
extension ${construct} where Value == Bool {
% for (name, cname, op, label, doc) in boolOperations:
/// Perform an atomic ${doc} operation and return the original value, applying
/// the specified memory ordering.
///
/// - Parameter operand: A boolean value.
/// - Parameter ordering: The memory ordering to apply on this operation.
/// - Returns: The original value before the operation.
@_semantics("atomics.requires_constant_orderings")
@_transparent @_alwaysEmitIntoClient
public func loadThen${name}(
${label} operand: Value,
ordering: AtomicUpdateOrdering
) -> Value {
Value.AtomicRepresentation.atomicLoadThen${name}(
${argLabel(label)}operand,
at: _ptr,
ordering: ordering)
}
% end
}
extension ${construct} where Value == Bool {
% for (name, cname, op, label, doc) in boolOperations:
/// Perform an atomic ${doc} operation and return the original value, applying
/// the specified memory ordering.
///
/// - Parameter operand: A boolean value.
/// - Parameter ordering: The memory ordering to apply on this operation.
/// - Returns: The original value before the operation.
@_semantics("atomics.requires_constant_orderings")
@_transparent @_alwaysEmitIntoClient
public func ${lowerFirst(name)}ThenLoad(
${label} operand: Value,
ordering: AtomicUpdateOrdering
) -> Value {
let original = Value.AtomicRepresentation.atomicLoadThen${name}(
${argLabel(label)}operand,
at: _ptr,
ordering: ordering)
return original ${op} operand
}
% end
}
% end
+120
View File
@@ -0,0 +1,120 @@
//===----------------------------------------------------------------------===//
//
// This source file is part of the Swift Atomics open source project
//
// Copyright (c) 2020 Apple Inc. and the Swift project authors
// Licensed under Apache License v2.0 with Runtime Library Exception
//
// See https://swift.org/LICENSE.txt for license information
// See https://swift.org/CONTRIBUTORS.txt for the list of Swift project authors
//
//===----------------------------------------------------------------------===//
/// A type that supports atomic integer operations through a separate
/// atomic storage representation.
///
/// Atomic integer types provide a number of additional atomic
/// operations beyond the ones supported by `AtomicValue`, such as
/// atomic increments/decrements as well as atomic bitwise operations.
/// These may be mapped to dedicated instructions that can be more
/// efficient than implementations based on the general compare and
/// exchange operation; however, this depends on the capabilities of
/// the compiler and the underlying hardware.
public protocol AtomicInteger: AtomicValue, FixedWidthInteger
where AtomicRepresentation: AtomicIntegerStorage {}
/// The storage representation for an atomic integer value, providing
/// pointer-based atomic operations.
///
/// This is a low-level implementation detail of atomic types; instead
/// of directly handling conforming types, it is usually better to use
/// the `UnsafeAtomic` or `ManagedAtomic` generics -- these provide a
/// more reliable interface while ensuring that the storage is
/// correctly constructed and destroyed.
public protocol AtomicIntegerStorage: AtomicStorage {
/// Perform an atomic wrapping increment operation on the value referenced by
/// `pointer` and return the original value, applying the specified memory
/// ordering.
///
/// Note: This operation silently wraps around on overflow, like the
/// `&+=` operator does on integer values.
///
/// - Parameter operand: The value to add to the current value.
/// - Parameter pointer: A memory location previously initialized with a value
/// returned by `prepareAtomicRepresentation(for:)`.
/// - Parameter ordering: The memory ordering to apply on this operation.
/// - Returns: The original value before the operation.
@_semantics("atomics.requires_constant_orderings")
static func atomicLoadThenWrappingIncrement(
by operand: Value,
at pointer: UnsafeMutablePointer<Self>,
ordering: AtomicUpdateOrdering
) -> Value
/// Perform an atomic wrapping decrement operation on the value referenced by
/// `pointer` and return the original value, applying the specified memory
/// ordering.
///
/// Note: This operation silently wraps around on overflow, like the
/// `&-=` operator does on integer values.
///
/// - Parameter operand: The value to subtract from the current value.
/// - Parameter pointer: A memory location previously initialized with a value
/// returned by `prepareAtomicRepresentation(for:)`.
/// - Parameter ordering: The memory ordering to apply on this operation.
/// - Returns: The original value before the operation.
@_semantics("atomics.requires_constant_orderings")
static func atomicLoadThenWrappingDecrement(
by operand: Value,
at pointer: UnsafeMutablePointer<Self>,
ordering: AtomicUpdateOrdering
) -> Value
/// Perform an atomic bitwise AND operation on the value referenced by
/// `pointer` and return the original value, applying the specified memory
/// ordering.
///
/// - Parameter operand: An integer value.
/// - Parameter pointer: A memory location previously initialized with a value
/// returned by `prepareAtomicRepresentation(for:)`.
/// - Parameter ordering: The memory ordering to apply on this operation.
/// - Returns: The original value before the operation.
@_semantics("atomics.requires_constant_orderings")
static func atomicLoadThenBitwiseAnd(
with operand: Value,
at pointer: UnsafeMutablePointer<Self>,
ordering: AtomicUpdateOrdering
) -> Value
/// Perform an atomic bitwise OR operation on the value referenced by
/// `pointer` and return the original value, applying the specified memory
/// ordering.
///
/// - Parameter operand: An integer value.
/// - Parameter pointer: A memory location previously initialized with a value
/// returned by `prepareAtomicRepresentation(for:)`.
/// - Parameter ordering: The memory ordering to apply on this operation.
/// - Returns: The original value before the operation.
@_semantics("atomics.requires_constant_orderings")
static func atomicLoadThenBitwiseOr(
with operand: Value,
at pointer: UnsafeMutablePointer<Self>,
ordering: AtomicUpdateOrdering
) -> Value
/// Perform an atomic bitwise XOR operation on the value referenced by
/// `pointer` and return the original value, applying the specified memory
/// ordering.
///
/// - Parameter operand: An integer value.
/// - Parameter pointer: A memory location previously initialized with a value
/// returned by `prepareAtomicRepresentation(for:)`.
/// - Parameter ordering: The memory ordering to apply on this operation.
/// - Returns: The original value before the operation.
@_semantics("atomics.requires_constant_orderings")
static func atomicLoadThenBitwiseXor(
with operand: Value,
at pointer: UnsafeMutablePointer<Self>,
ordering: AtomicUpdateOrdering
) -> Value
}
@@ -0,0 +1,199 @@
//===----------------------------------------------------------------------===//
//
// This source file is part of the Swift Atomics open source project
//
// Copyright (c) 2020 Apple Inc. and the Swift project authors
// Licensed under Apache License v2.0 with Runtime Library Exception
//
// See https://swift.org/LICENSE.txt for license information
// See https://swift.org/CONTRIBUTORS.txt for the list of Swift project authors
//
//===----------------------------------------------------------------------===//
%{
from gyb_utils import autogenerated_warning
}%
${autogenerated_warning()}
/// An unsafe reference type holding a lazily initializable atomic
/// strong reference, requiring manual memory management of the
/// underlying storage representation.
///
/// These values can be set (initialized) exactly once, but read many
/// times.
@frozen
public struct UnsafeAtomicLazyReference<Instance: AnyObject> {
/// The value logically stored in an atomic lazy reference value.
public typealias Value = Instance?
@usableFromInline
internal typealias _Rep = Optional<Unmanaged<Instance>>.AtomicRepresentation
@usableFromInline
internal let _ptr: UnsafeMutablePointer<_Rep>
/// Initialize an unsafe atomic lazy reference that uses the supplied memory
/// location for storage. The storage location must already be initialized to
/// represent a valid atomic value.
///
/// At the end of the lifetime of the atomic value, you must manually ensure
/// that the storage location is correctly `dispose()`d, deinitalized and
/// deallocated.
///
/// Note: This is not an atomic operation.
@_transparent // Debug performance
public init(@_nonEphemeral at pointer: UnsafeMutablePointer<Storage>) {
// `Storage` is layout-compatible with its only stored property.
_ptr = UnsafeMutableRawPointer(pointer).assumingMemoryBound(to: _Rep.self)
}
}
extension UnsafeAtomicLazyReference {
/// The storage representation for an atomic lazy reference value.
@frozen
public struct Storage {
@usableFromInline
internal var _storage: _Rep
/// Initialize a new atomic lazy reference storage value holding `nil`.
///
/// Note: This is not an atomic operation. This call may have side effects
/// (such as unpaired retains of strong references) that will need to be
/// undone by calling `dispose()` before the storage value is
/// deinitialized.
@inlinable @inline(__always)
public init() {
_storage = _Rep(nil)
}
/// Prepare this atomic storage value for deinitialization, extracting the
/// logical value it represents. This invalidates this atomic storage; you
/// must not perform any operations on it after this call (except for
/// deinitialization).
///
/// This call prevents resource leaks when destroying the storage
/// representation of certain `AtomicValue` types. (In particular, ones
/// that model strong references.)
///
/// Note: This is not an atomic operation. Logically, it implements a
/// custom destructor for the underlying non-copiable value.
@inlinable @inline(__always)
@discardableResult
public mutating func dispose() -> Value {
defer { _storage = _Rep(nil) }
return _storage.dispose()?.takeRetainedValue()
}
}
}
extension UnsafeAtomicLazyReference {
/// Create a new `UnsafeAtomicLazyReference` value by dynamically allocating
/// storage for it.
///
/// This call is usually paired with `destroy` to get rid of the allocated
/// storage at the end of its lifetime.
///
/// Note: This is not an atomic operation.
@inlinable
public static func create() -> Self {
let ptr = UnsafeMutablePointer<Storage>.allocate(capacity: 1)
ptr.initialize(to: Storage())
return Self(at: ptr)
}
/// Disposes of the current value of the storage location corresponding to
/// this unsafe atomic lazy reference, then deinitializes and deallocates the
/// storage.
///
/// Note: This is not an atomic operation.
///
/// - Returns: The last value stored in the storage representation before it
/// was destroyed.
@discardableResult
@inlinable
public func destroy() -> Value {
// `Storage` is layout-compatible with its only stored property.
let address = UnsafeMutableRawPointer(_ptr)
.assumingMemoryBound(to: Storage.self)
defer { address.deallocate() }
return address.pointee.dispose()
}
}
/// A reference type holding a lazily initializable atomic
/// strong reference, with automatic memory management.
///
/// These values can be set (initialized) exactly once, but read many
/// times.
public class ManagedAtomicLazyReference<Instance: AnyObject> {
/// The value logically stored in an atomic lazy reference value.
public typealias Value = Instance?
@usableFromInline
internal typealias _Rep = Optional<Unmanaged<Instance>>.AtomicRepresentation
@usableFromInline
internal let _storage: _Rep
/// Initializes a new managed atomic lazy reference with a nil value.
@inlinable
public init() {
_storage = _Rep(nil)
}
@_alwaysEmitIntoClient @inline(__always)
internal var _ptr: UnsafeMutablePointer<_Rep> {
_getUnsafePointerToStoredProperties(self).assumingMemoryBound(to: _Rep.self)
}
}
% for type in ["UnsafeAtomicLazyReference", "ManagedAtomicLazyReference"]:
extension ${type} {
/// Atomically initializes this reference if its current value is nil, then
/// returns the initialized value. If this reference is already initialized,
/// then `storeIfNilThenLoad(_:)` discards its supplied argument and returns
/// the current value without updating it.
///
/// The following example demonstrates how this can be used to implement a
/// thread-safe lazily initialized reference:
///
/// ```
/// class Image {
/// var _histogram: UnsafeAtomicLazyReference<Histogram> = ...
///
/// // This is safe to call concurrently from multiple threads.
/// var atomicLazyHistogram: Histogram {
/// if let histogram = _histogram.load() { return foo }
/// // Note that code here may run concurrently on
/// // multiple threads, but only one of them will get to
/// // succeed setting the reference.
/// let histogram = ...
/// return _histogram.storeIfNilThenLoad(foo)
/// }
/// ```
///
/// This operation uses acquiring-and-releasing memory ordering.
public func storeIfNilThenLoad(_ desired: __owned Instance) -> Instance {
let desiredUnmanaged = Unmanaged.passRetained(desired)
let (exchanged, current) = _Rep.atomicCompareExchange(
expected: nil,
desired: desiredUnmanaged,
at: _ptr,
ordering: .acquiringAndReleasing)
if !exchanged {
// The reference has already been initialized. Balance the retain that
// we performed on `desired`.
desiredUnmanaged.release()
return current!.takeUnretainedValue()
}
return desiredUnmanaged.takeUnretainedValue()
}
/// Atomically loads and returns the current value of this reference.
///
/// The load operation is performed with the memory ordering
/// `AtomicLoadOrdering.acquiring`.
public func load() -> Instance? {
let value = _Rep.atomicLoad(at: _ptr, ordering: .acquiring)
return value?.takeUnretainedValue()
}
}
+321
View File
@@ -0,0 +1,321 @@
//===----------------------------------------------------------------------===//
//
// This source file is part of the Swift Atomics open source project
//
// Copyright (c) 2020 Apple Inc. and the Swift project authors
// Licensed under Apache License v2.0 with Runtime Library Exception
//
// See https://swift.org/LICENSE.txt for license information
// See https://swift.org/CONTRIBUTORS.txt for the list of Swift project authors
//
//===----------------------------------------------------------------------===//
import _AtomicsShims
/// Specifies the memory ordering semantics of an atomic load operation.
@frozen
public struct AtomicLoadOrdering {
// This struct works like a non-frozen enum whose cases aren't reorderable.
@usableFromInline
internal var _rawValue: Int
@_semantics("constant_evaluable")
@inlinable @_transparent // Debug performance
internal init(_rawValue: Int) {
self._rawValue = _rawValue
}
}
extension AtomicLoadOrdering {
// FIXME: Explain these ordering levels in more detail.
/// Guarantees the atomicity of the specific operation on which it is applied,
/// but imposes no ordering constraints on any other variable accesses.
///
/// This value corresponds to `std::memory_order_relaxed` in C++.
@_semantics("constant_evaluable")
@_alwaysEmitIntoClient
@_transparent // Debug performance
public static var relaxed: Self {
Self(_rawValue: 0)
}
/// An acquiring load synchronizes with a releasing operation whose
/// value its reads. It ensures that the releasing and acquiring
/// threads agree that all subsequent variable accesses on the the
/// acquring thread happen after the atomic operation itself.
///
/// This value corresponds to `std::memory_order_acquire` in C++.
@_semantics("constant_evaluable")
@_alwaysEmitIntoClient
@_transparent // Debug performance
public static var acquiring: Self {
Self(_rawValue: 2)
}
/// A sequentially consistent load performs an acquiring load and
/// also guarantees that it and all other sequentially consistent
/// atomic operations (loads, stores, updates) appear to be executed
/// in a single, total sequential ordering.
///
/// This value corresponds to `std::memory_order_seq_cst` in C++.
@_semantics("constant_evaluable")
@_alwaysEmitIntoClient
@_transparent // Debug performance
public static var sequentiallyConsistent: Self {
Self(_rawValue: 5)
}
}
extension AtomicLoadOrdering: Equatable {
@_transparent // Debug performance
public static func ==(left: Self, right: Self) -> Bool {
return left._rawValue == right._rawValue
}
}
extension AtomicLoadOrdering: Hashable {
@inlinable
public func hash(into hasher: inout Hasher) {
hasher.combine(_rawValue)
}
}
extension AtomicLoadOrdering: CustomStringConvertible {
public var description: String {
switch self {
case .relaxed: return "relaxed"
case .acquiring: return "acquiring"
case .sequentiallyConsistent: return "sequentiallyConsistent"
default: return "AtomicLoadOrdering(\(_rawValue))"
}
}
}
//------------------------------------------------------------------------------
/// Specifies the memory ordering semantics of an atomic store operation.
@frozen
public struct AtomicStoreOrdering {
// This struct works like a non-frozen enum whose cases aren't reorderable.
@usableFromInline
internal var _rawValue: Int
@_semantics("constant_evaluable")
@inlinable @_transparent // Debug performance
internal init(_rawValue: Int) {
self._rawValue = _rawValue
}
}
extension AtomicStoreOrdering {
// FIXME: Explain these ordering levels in more detail.
/// Guarantees the atomicity of the specific operation on which it is applied,
/// but imposes no ordering constraints on any other variable accesses.
///
/// This value corresponds to `std::memory_order_relaxed` in C++.
@_semantics("constant_evaluable")
@_alwaysEmitIntoClient
@_transparent // Debug performance
public static var relaxed: Self {
Self(_rawValue: 0)
}
/// A releasing store synchronizes with acquiring operations that
/// read the value it stores. It ensures that the releasing and
/// acquiring threads agree that all preceding variable accesses on
/// the releasing thread happen before the atomic operation itself.
///
/// This value corresponds to `std::memory_order_release` in C++.
@_semantics("constant_evaluable")
@_alwaysEmitIntoClient
@_transparent // Debug performance
public static var releasing: Self {
Self(_rawValue: 3)
}
/// A sequentially consistent store performs a releasing store and
/// also guarantees that it and all other sequentially consistent
/// atomic operations (loads, stores, updates) appear to be executed
/// in a single, total sequential ordering.
///
/// This value corresponds to `std::memory_order_seq_cst` in C++.
@_semantics("constant_evaluable")
@_alwaysEmitIntoClient
@_transparent // Debug performance
public static var sequentiallyConsistent: Self {
Self(_rawValue: 5)
}
}
extension AtomicStoreOrdering: Equatable {
@_transparent // Debug performance
public static func ==(left: Self, right: Self) -> Bool {
return left._rawValue == right._rawValue
}
}
extension AtomicStoreOrdering: Hashable {
@inlinable
public func hash(into hasher: inout Hasher) {
hasher.combine(_rawValue)
}
}
extension AtomicStoreOrdering: CustomStringConvertible {
public var description: String {
switch self {
case .relaxed: return "relaxed"
case .releasing: return "releasing"
case .sequentiallyConsistent: return "sequentiallyConsistent"
default: return "AtomicStoreOrdering(\(_rawValue))"
}
}
}
//------------------------------------------------------------------------------
/// Specifies the memory ordering semantics of an atomic read-modify-write
/// operation.
@frozen
public struct AtomicUpdateOrdering {
// This struct works like a non-frozen enum whose cases aren't reorderable.
@usableFromInline
internal var _rawValue: Int
@_semantics("constant_evaluable")
@inlinable @_transparent // Debug performance
internal init(_rawValue: Int) {
self._rawValue = _rawValue
}
}
extension AtomicUpdateOrdering {
// FIXME: Explain these ordering levels in more detail.
/// Guarantees the atomicity of the specific operation on which it is applied,
/// but imposes no ordering constraints on any other variable accesses.
///
/// This value corresponds to `std::memory_order_relaxed` in C++.
@_semantics("constant_evaluable")
@_alwaysEmitIntoClient
@_transparent // Debug performance
public static var relaxed: Self {
Self(_rawValue: 0)
}
/// An acquiring update synchronizes with a releasing operation
/// whose value its reads. It ensures that the releasing and
/// acquiring threads agree that all subsequent variable accesses on
/// the acquring thread happen after the atomic operation itself.
///
/// This value corresponds to `std::memory_order_acquire` in C++.
@_semantics("constant_evaluable")
@_alwaysEmitIntoClient
@_transparent // Debug performance
public static var acquiring: Self {
Self(_rawValue: 2)
}
/// A releasing update synchronizes with acquiring operations that
/// read the value it stores. It ensures that the releasing and
/// acquiring threads agree that all preceding variable accesses on
/// the releasing thread happen before the atomic operation itself.
///
/// This value corresponds to `std::memory_order_release` in C++.
@_semantics("constant_evaluable")
@_alwaysEmitIntoClient
@_transparent // Debug performance
public static var releasing: Self {
Self(_rawValue: 3)
}
/// An acquiring-and-releasing operation is a combination of
/// `.acquiring` and `.releasing` operation on the same variable.
///
/// This value corresponds to `std::memory_order_acq_rel` in C++.
@_semantics("constant_evaluable")
@_alwaysEmitIntoClient
@_transparent // Debug performance
public static var acquiringAndReleasing: Self {
Self(_rawValue: 4)
}
/// A sequentially consistent update performs an
/// acquiring-and-releasing update and also guarantees that it and
/// all other sequentially consistent atomic operations (loads, stores,
/// updates) appear to be executed in a single, total sequential
/// ordering.
///
/// This value corresponds to `std::memory_order_seq_cst` in C++.
@_semantics("constant_evaluable")
@_alwaysEmitIntoClient
@_transparent // Debug performance
public static var sequentiallyConsistent: Self {
Self(_rawValue: 5)
}
}
extension AtomicUpdateOrdering: Equatable {
@_transparent // Debug performance
public static func ==(left: Self, right: Self) -> Bool {
return left._rawValue == right._rawValue
}
}
extension AtomicUpdateOrdering: Hashable {
@inlinable
public func hash(into hasher: inout Hasher) {
hasher.combine(_rawValue)
}
}
extension AtomicUpdateOrdering: CustomStringConvertible {
public var description: String {
switch self {
case .relaxed: return "relaxed"
case .acquiring: return "acquiring"
case .releasing: return "releasing"
case .acquiringAndReleasing: return "acquiringAndReleasing"
case .sequentiallyConsistent: return "sequentiallyConsistent"
default: return "AtomicUpdateOrdering(\(_rawValue))"
}
}
}
/// Establishes a memory ordering without associating it with a
/// particular atomic operation.
///
/// - A relaxed fence has no effect.
/// - An acquiring fence ties to any preceding atomic operation that
/// reads a value, and synchronizes with any releasing operation whose
/// value was read.
/// - A releasing fence ties to any subsequent atomic operation that
/// modifies a value, and synchronizes with any acquiring operation
/// that reads the result.
/// - An acquiring and releasing fence is a combination of an
/// acquiring and a releasing fence.
/// - A sequentially consistent fence behaves like an acquiring and
/// releasing fence, and ensures that the fence itself is part of
/// the single, total ordering for all sequentially consistent
/// operations.
///
/// This operation corresponds to `std::atomic_thread_fence` in C++.
@_semantics("atomics.requires_constant_orderings")
@_transparent // Debug performance
public func atomicMemoryFence(
ordering: AtomicUpdateOrdering
) {
switch ordering {
case .relaxed: break
case .acquiring: _sa_thread_fence_acquire()
case .releasing: _sa_thread_fence_release()
case .acquiringAndReleasing: _sa_thread_fence_acq_rel()
case .sequentiallyConsistent: _sa_thread_fence_seq_cst()
default: fatalError()
}
}
+25
View File
@@ -0,0 +1,25 @@
//===----------------------------------------------------------------------===//
//
// This source file is part of the Swift Atomics open source project
//
// Copyright (c) 2020 Apple Inc. and the Swift project authors
// Licensed under Apache License v2.0 with Runtime Library Exception
//
// See https://swift.org/LICENSE.txt for license information
// See https://swift.org/CONTRIBUTORS.txt for the list of Swift project authors
//
//===----------------------------------------------------------------------===//
/// An atomic value that also supports atomic operations when wrapped
/// in an `Optional`. Atomic optional wrappable types come with a
/// standalone atomic representation for their optional-wrapped
/// variants.
public protocol AtomicOptionalWrappable: AtomicValue {
/// The atomic storage representation for `Optional<Self>`.
associatedtype AtomicOptionalRepresentation: AtomicStorage
where AtomicOptionalRepresentation.Value == Self?
}
extension Optional: AtomicValue where Wrapped: AtomicOptionalWrappable {
public typealias AtomicRepresentation = Wrapped.AtomicOptionalRepresentation
}
@@ -0,0 +1,126 @@
//===----------------------------------------------------------------------===//
//
// This source file is part of the Swift Atomics open source project
//
// Copyright (c) 2020 Apple Inc. and the Swift project authors
// Licensed under Apache License v2.0 with Runtime Library Exception
//
// See https://swift.org/LICENSE.txt for license information
// See https://swift.org/CONTRIBUTORS.txt for the list of Swift project authors
//
//===----------------------------------------------------------------------===//
extension RawRepresentable where Self: AtomicValue, RawValue: AtomicValue {
public typealias AtomicRepresentation = AtomicRawRepresentableStorage<Self>
}
/// The default atomic storage representation for an atomic `RawRepresentable`
/// type whose `RawValue` conforms to `AtomicValue`.
@frozen
public struct AtomicRawRepresentableStorage<Value>: AtomicStorage
where Value: RawRepresentable, Value.RawValue: AtomicValue {
@usableFromInline internal typealias Storage = Value.RawValue.AtomicRepresentation
@usableFromInline
internal var _storage: Storage
@_transparent @_alwaysEmitIntoClient
public init(_ value: __owned Value) {
_storage = Storage(value.rawValue)
}
@_transparent @_alwaysEmitIntoClient
public func dispose() -> Value {
Value(rawValue: _storage.dispose())!
}
@_transparent @_alwaysEmitIntoClient
internal static func _extract(
_ ptr: UnsafeMutablePointer<Self>
) -> UnsafeMutablePointer<Storage> {
// `Self` is layout-compatible with its only stored property.
UnsafeMutableRawPointer(ptr).assumingMemoryBound(to: Storage.self)
}
@_semantics("atomics.requires_constant_orderings")
@_transparent @_alwaysEmitIntoClient
public static func atomicLoad(
at pointer: UnsafeMutablePointer<Self>,
ordering: AtomicLoadOrdering
) -> Value {
let raw = Storage.atomicLoad(at: _extract(pointer), ordering: ordering)
return Value(rawValue: raw)!
}
@_semantics("atomics.requires_constant_orderings")
@_transparent @_alwaysEmitIntoClient
public static func atomicStore(
_ desired: Value,
at pointer: UnsafeMutablePointer<Self>,
ordering: AtomicStoreOrdering
) {
Storage.atomicStore(desired.rawValue, at: _extract(pointer), ordering: ordering)
}
@_semantics("atomics.requires_constant_orderings")
@_transparent @_alwaysEmitIntoClient
public static func atomicExchange(
_ desired: Value,
at pointer: UnsafeMutablePointer<Self>,
ordering: AtomicUpdateOrdering
) -> Value {
let raw = Storage.atomicExchange(desired.rawValue, at: _extract(pointer), ordering: ordering)
return Value(rawValue: raw)!
}
@_semantics("atomics.requires_constant_orderings")
@_transparent @_alwaysEmitIntoClient
public static func atomicCompareExchange(
expected: Value,
desired: Value,
at pointer: UnsafeMutablePointer<Self>,
ordering: AtomicUpdateOrdering
) -> (exchanged: Bool, original: Value) {
let raw = Storage.atomicCompareExchange(
expected: expected.rawValue,
desired: desired.rawValue,
at: _extract(pointer),
ordering: ordering)
return (raw.exchanged, Value(rawValue: raw.original)!)
}
@_semantics("atomics.requires_constant_orderings")
@_transparent @_alwaysEmitIntoClient
public static func atomicCompareExchange(
expected: Value,
desired: Value,
at pointer: UnsafeMutablePointer<Self>,
successOrdering: AtomicUpdateOrdering,
failureOrdering: AtomicLoadOrdering
) -> (exchanged: Bool, original: Value) {
let raw = Storage.atomicCompareExchange(
expected: expected.rawValue,
desired: desired.rawValue,
at: _extract(pointer),
successOrdering: successOrdering,
failureOrdering: failureOrdering)
return (raw.exchanged, Value(rawValue: raw.original)!)
}
@_semantics("atomics.requires_constant_orderings")
@_transparent @_alwaysEmitIntoClient
public static func atomicWeakCompareExchange(
expected: Value,
desired: Value,
at pointer: UnsafeMutablePointer<Self>,
successOrdering: AtomicUpdateOrdering,
failureOrdering: AtomicLoadOrdering
) -> (exchanged: Bool, original: Value) {
let raw = Storage.atomicWeakCompareExchange(
expected: expected.rawValue,
desired: desired.rawValue,
at: _extract(pointer),
successOrdering: successOrdering,
failureOrdering: failureOrdering)
return (raw.exchanged, Value(rawValue: raw.original)!)
}
}
+571
View File
@@ -0,0 +1,571 @@
//===----------------------------------------------------------------------===//
//
// This source file is part of the Swift Atomics open source project
//
// Copyright (c) 2020 Apple Inc. and the Swift project authors
// Licensed under Apache License v2.0 with Runtime Library Exception
//
// See https://swift.org/LICENSE.txt for license information
// See https://swift.org/CONTRIBUTORS.txt for the list of Swift project authors
//
//===----------------------------------------------------------------------===//
import _AtomicsShims
/// A class type that supports atomic strong references.
public protocol AtomicReference: AnyObject, AtomicOptionalWrappable
where
AtomicRepresentation == AtomicReferenceStorage<Self>,
AtomicOptionalRepresentation == AtomicOptionalReferenceStorage<Self>
{
// These were added as a workaround for https://bugs.swift.org/browse/SR-10251
// FIXME: We should remove these once the package requires a
// compiler version that contains that fix.
override associatedtype AtomicRepresentation = AtomicReferenceStorage<Self>
override associatedtype AtomicOptionalRepresentation =
AtomicOptionalReferenceStorage<Self>
}
/// The maximum number of other threads that can start accessing a
/// strong reference before an in-flight update needs to cancel and
/// retry.
@inlinable @inline(__always)
internal var _concurrencyWindow: Int { 20 }
extension Unmanaged {
@inlinable
internal func retain(by delta: Int) {
_sa_retain_n(toOpaque(), UInt32(delta))
}
@inlinable
internal func release(by delta: Int) {
_sa_release_n(toOpaque(), UInt32(delta))
}
}
extension Unmanaged {
fileprivate static func passRetained(_ instance: __owned Instance?) -> Self? {
guard let instance = instance else { return nil }
return .passRetained(instance)
}
}
extension DoubleWord {
fileprivate init(_raw: UnsafeRawPointer?, readers: Int, version: Int) {
let r = UInt(bitPattern: readers) & Self._readersMask
assert(r == readers)
self.init(
high: r | (UInt(bitPattern: version) &<< Self._readersBitWidth),
low: UInt(bitPattern: _raw))
}
@inline(__always)
fileprivate var _raw: UnsafeMutableRawPointer? {
get { UnsafeMutableRawPointer(bitPattern: low) }
set { low = UInt(bitPattern: newValue) }
}
@inline(__always)
fileprivate var _unmanaged: Unmanaged<AnyObject>? {
guard let raw = _raw else { return nil }
return .fromOpaque(raw)
}
@inline(__always)
fileprivate static var _readersBitWidth: Int {
#if arch(i386) || arch(arm) || arch(arm64_32)
return 8
#else
return 16
#endif
}
@inline(__always)
fileprivate static var _readersMask: UInt { (1 &<< _readersBitWidth) - 1 }
@inline(__always)
fileprivate var _readers: Int {
get { Int(bitPattern: high & Self._readersMask) }
set {
let n = UInt(bitPattern: newValue) & Self._readersMask
assert(n == newValue)
high = (high & ~Self._readersMask) | n
}
}
@inline(__always)
fileprivate var _version: Int {
get { Int(bitPattern: high &>> Self._readersBitWidth) }
set {
// Silently truncate any high bits we cannot store.
high = (
(UInt(bitPattern: newValue) &<< Self._readersBitWidth) |
high & Self._readersMask)
}
}
}
extension Optional where Wrapped == AnyObject {
fileprivate var _raw: UnsafeMutableRawPointer? {
guard let value = self else { return nil }
return Unmanaged.passUnretained(value).toOpaque()
}
}
extension UnsafeMutablePointer where Pointee == _AtomicReferenceStorage {
@inlinable @inline(__always)
internal var _extract: UnsafeMutablePointer<DoubleWord.AtomicRepresentation> {
UnsafeMutableRawPointer(self)
.assumingMemoryBound(to: DoubleWord.AtomicRepresentation.self)
}
}
@usableFromInline
internal struct _AtomicReferenceStorage {
internal typealias Storage = DoubleWord.AtomicRepresentation
internal var _storage: Storage
@usableFromInline
internal init(_ value: __owned AnyObject?) {
let dword = DoubleWord(
_raw: Unmanaged.passRetained(value)?.toOpaque(),
readers: 0,
version: 0)
_storage = Storage(dword)
}
@usableFromInline
internal func dispose() -> AnyObject? {
let value = _storage.dispose()
precondition(value._readers == 0,
"Attempt to dispose of a busy atomic strong reference \(value)")
return value._unmanaged?.takeRetainedValue()
}
private static func _startLoading(
from pointer: UnsafeMutablePointer<Self>,
hint: DoubleWord? = nil
) -> DoubleWord {
var old = hint ?? Storage.atomicLoad(at: pointer._extract, ordering: .relaxed)
if old._raw == nil {
atomicMemoryFence(ordering: .acquiring)
return old
}
// Increment reader count
while true {
let new = DoubleWord(
_raw: old._raw,
readers: old._readers &+ 1,
version: old._version)
var done: Bool
(done, old) = Storage.atomicWeakCompareExchange(
expected: old,
desired: new,
at: pointer._extract,
successOrdering: .acquiring,
failureOrdering: .acquiring)
if done { return new }
if old._raw == nil { return old }
}
}
private static func _finishLoading(
_ value: DoubleWord,
from pointer: UnsafeMutablePointer<Self>
) -> AnyObject? {
if value._raw == nil { return nil }
// Retain result before we exit the access.
let result = value._unmanaged!.takeUnretainedValue()
// Decrement reader count, using the version number to prevent any
// ABA issues.
var current = value
var done = false
repeat {
assert(current._readers >= 1)
(done, current) = Storage.atomicWeakCompareExchange(
expected: current,
desired: DoubleWord(
_raw: current._raw,
readers: current._readers &- 1,
version: current._version),
at: pointer._extract,
successOrdering: .acquiringAndReleasing,
failureOrdering: .acquiring)
} while !done && current._raw == value._raw && current._version == value._version
if !done {
// The reference changed while we were loading it. Cancel out
// our part of the refcount bias for the loaded instance.
value._unmanaged!.release()
}
return result
}
@usableFromInline
internal static func atomicLoad(
at pointer: UnsafeMutablePointer<Self>
) -> AnyObject? {
let new = _startLoading(from: pointer)
return _finishLoading(new, from: pointer)
}
/// Try updating the current value from `old` to `new`. Don't do anything if the
/// current value differs from `old`.
///
/// Returns a tuple `(exchanged, original)` where `exchanged` indicates whether the
/// update was successful. If `exchange` true, then `original` contains the original
/// reference value; otherwise `original` is nil.
///
/// On an unsuccessful exchange, this function updates `old` to the latest known value,
/// and reenters the current thread as a reader of it.
private static func _tryExchange(
old: inout DoubleWord,
new: Unmanaged<AnyObject>?,
at pointer: UnsafeMutablePointer<Self>
) -> (exchanged: Bool, original: AnyObject?) {
let new = DoubleWord(_raw: new?.toOpaque(), readers: 0, version: old._version &+ 1)
guard let ref = old._unmanaged else {
// Try replacing the current nil value with the desired new value.
let (done, current) = Storage.atomicCompareExchange(
expected: old,
desired: new,
at: pointer._extract,
ordering: .acquiringAndReleasing)
if done {
return (true, nil)
}
// Someone else changed the reference. Give up for now.
old = _startLoading(from: pointer, hint: current)
return (false, nil)
}
assert(old._readers >= 1)
var delta = old._readers + _concurrencyWindow
ref.retain(by: delta)
while true {
// Try replacing the current value with the desired new value.
let (done, current) = Storage.atomicCompareExchange(
expected: old,
desired: new,
at: pointer._extract,
ordering: .acquiringAndReleasing)
if done {
// Successfully replaced the object. Clean up extra retains.
assert(current._readers == old._readers)
assert(current._raw == old._raw)
assert(current._readers <= delta)
ref.release(by: delta - current._readers + 1) // +1 is for our own role as a reader.
return (true, ref.takeRetainedValue())
}
if current._version != old._version {
// Someone else changed the reference. Give up for now.
ref.release(by: delta + 1) // +1 covers our reader bias
old = _startLoading(from: pointer, hint: current)
return (false, nil)
}
// Some readers entered or left while we were processing things. Try again.
assert(current._raw == old._raw)
assert(current._raw != nil)
assert(current._readers >= 1)
if current._readers > delta {
// We need to do more retains to cover readers.
let d = current._readers + _concurrencyWindow
ref.retain(by: d - delta)
delta = d
}
old = current
}
}
@usableFromInline
internal static func atomicExchange(
_ desired: __owned AnyObject?,
at pointer: UnsafeMutablePointer<Self>
) -> AnyObject? {
let new = Unmanaged.passRetained(desired)
var old = _startLoading(from: pointer)
var (exchanged, original) = _tryExchange(old: &old, new: new, at: pointer)
while !exchanged {
(exchanged, original) = _tryExchange(old: &old, new: new, at: pointer)
}
return original
}
@usableFromInline
internal static func atomicCompareExchange(
expected: AnyObject?,
desired: __owned AnyObject?,
at pointer: UnsafeMutablePointer<Self>
) -> (exchanged: Bool, original: AnyObject?) {
let expectedRaw = expected._raw
let new = Unmanaged.passRetained(desired)
var old = _startLoading(from: pointer)
while old._raw == expectedRaw {
let (exchanged, original) = _tryExchange(old: &old, new: new, at: pointer)
if exchanged {
assert(original === expected)
return (true, original)
}
}
// We did not find the expected value. Cancel the retain of the new value
// and return the old value.
new?.release()
return (false, _finishLoading(old, from: pointer))
}
}
public struct AtomicReferenceStorage<Value: AnyObject> {
@usableFromInline
internal var _storage: _AtomicReferenceStorage
@inlinable
public init(_ value: __owned Value) {
_storage = .init(value)
}
@inlinable
public func dispose() -> Value {
return unsafeDowncast(_storage.dispose()!, to: Value.self)
}
}
extension AtomicReferenceStorage {
@inlinable @inline(__always)
@_alwaysEmitIntoClient
static func _extract(
_ ptr: UnsafeMutablePointer<Self>
) -> UnsafeMutablePointer<_AtomicReferenceStorage> {
// `Self` is layout-compatible with its only stored property.
return UnsafeMutableRawPointer(ptr)
.assumingMemoryBound(to: _AtomicReferenceStorage.self)
}
}
extension AtomicReferenceStorage: AtomicStorage {
@inlinable @inline(__always)
@_alwaysEmitIntoClient
@_semantics("atomics.requires_constant_orderings")
public static func atomicLoad(
at pointer: UnsafeMutablePointer<Self>,
ordering: AtomicLoadOrdering
) -> Value {
// FIXME: All orderings are treated as acquiring-and-releasing.
let result = _AtomicReferenceStorage.atomicLoad(at: Self._extract(pointer))
return unsafeDowncast(result!, to: Value.self)
}
@inlinable @inline(__always)
@_alwaysEmitIntoClient
@_semantics("atomics.requires_constant_orderings")
public static func atomicStore(
_ desired: __owned Value,
at pointer: UnsafeMutablePointer<Self>,
ordering: AtomicStoreOrdering
) {
// FIXME: All orderings are treated as acquiring-and-releasing.
_ = _AtomicReferenceStorage.atomicExchange(
desired,
at: _extract(pointer))
}
@inlinable @inline(__always)
@_alwaysEmitIntoClient
@_semantics("atomics.requires_constant_orderings")
public static func atomicExchange(
_ desired: __owned Value,
at pointer: UnsafeMutablePointer<Self>,
ordering: AtomicUpdateOrdering
) -> Value {
// FIXME: All orderings are treated as acquiring-and-releasing.
let result = _AtomicReferenceStorage.atomicExchange(
desired,
at: _extract(pointer))
return unsafeDowncast(result!, to: Value.self)
}
@inlinable @inline(__always)
@_alwaysEmitIntoClient
@_semantics("atomics.requires_constant_orderings")
public static func atomicCompareExchange(
expected: Value,
desired: __owned Value,
at pointer: UnsafeMutablePointer<Self>,
ordering: AtomicUpdateOrdering
) -> (exchanged: Bool, original: Value) {
// FIXME: All orderings are treated as acquiring-and-releasing.
let result = _AtomicReferenceStorage.atomicCompareExchange(
expected: expected,
desired: desired,
at: _extract(pointer))
return (result.exchanged, unsafeDowncast(result.original!, to: Value.self))
}
@inlinable @inline(__always)
@_alwaysEmitIntoClient
@_semantics("atomics.requires_constant_orderings")
public static func atomicCompareExchange(
expected: Value,
desired: __owned Value,
at pointer: UnsafeMutablePointer<Self>,
successOrdering: AtomicUpdateOrdering,
failureOrdering: AtomicLoadOrdering
) -> (exchanged: Bool, original: Value) {
// FIXME: All orderings are treated as acquiring-and-releasing.
let result = _AtomicReferenceStorage.atomicCompareExchange(
expected: expected,
desired: desired,
at: _extract(pointer))
return (result.exchanged, unsafeDowncast(result.original!, to: Value.self))
}
@inlinable @inline(__always)
@_alwaysEmitIntoClient
@_semantics("atomics.requires_constant_orderings")
public static func atomicWeakCompareExchange(
expected: Value,
desired: __owned Value,
at pointer: UnsafeMutablePointer<Self>,
successOrdering: AtomicUpdateOrdering,
failureOrdering: AtomicLoadOrdering
) -> (exchanged: Bool, original: Value) {
// FIXME: All orderings are treated as acquiring-and-releasing.
let result = _AtomicReferenceStorage.atomicCompareExchange(
expected: expected,
desired: desired,
at: _extract(pointer))
return (result.exchanged, unsafeDowncast(result.original!, to: Value.self))
}
}
public struct AtomicOptionalReferenceStorage<Instance: AnyObject> {
@usableFromInline
internal var _storage: _AtomicReferenceStorage
@inlinable
public init(_ value: __owned Instance?) {
_storage = .init(value)
}
@inlinable
public func dispose() -> Instance? {
guard let value = _storage.dispose() else { return nil }
return unsafeDowncast(value, to: Instance.self)
}
}
extension AtomicOptionalReferenceStorage {
@inlinable @inline(__always)
@_alwaysEmitIntoClient
static func _extract(
_ ptr: UnsafeMutablePointer<Self>
) -> UnsafeMutablePointer<_AtomicReferenceStorage> {
// `Self` is layout-compatible with its only stored property.
return UnsafeMutableRawPointer(ptr)
.assumingMemoryBound(to: _AtomicReferenceStorage.self)
}
}
extension AtomicOptionalReferenceStorage: AtomicStorage {
public typealias Value = Instance?
@inlinable @inline(__always)
@_alwaysEmitIntoClient
@_semantics("atomics.requires_constant_orderings")
public static func atomicLoad(
at pointer: UnsafeMutablePointer<Self>,
ordering: AtomicLoadOrdering
) -> Instance? {
// FIXME: All orderings are treated as acquiring-and-releasing.
let result = _AtomicReferenceStorage.atomicLoad(at: Self._extract(pointer))
guard let r = result else { return nil }
return unsafeDowncast(r, to: Instance.self)
}
@inlinable @inline(__always)
@_alwaysEmitIntoClient
@_semantics("atomics.requires_constant_orderings")
public static func atomicStore(
_ desired: __owned Instance?,
at pointer: UnsafeMutablePointer<Self>,
ordering: AtomicStoreOrdering
) {
// FIXME: All orderings are treated as acquiring-and-releasing.
_ = _AtomicReferenceStorage.atomicExchange(
desired,
at: _extract(pointer))
}
@inlinable @inline(__always)
@_alwaysEmitIntoClient
@_semantics("atomics.requires_constant_orderings")
public static func atomicExchange(
_ desired: __owned Instance?,
at pointer: UnsafeMutablePointer<Self>,
ordering: AtomicUpdateOrdering
) -> Instance? {
// FIXME: All orderings are treated as acquiring-and-releasing.
let result = _AtomicReferenceStorage.atomicExchange(
desired,
at: _extract(pointer))
guard let r = result else { return nil }
return unsafeDowncast(r, to: Instance.self)
}
@inlinable @inline(__always)
@_alwaysEmitIntoClient
@_semantics("atomics.requires_constant_orderings")
public static func atomicCompareExchange(
expected: Instance?,
desired: __owned Instance?,
at pointer: UnsafeMutablePointer<Self>,
ordering: AtomicUpdateOrdering
) -> (exchanged: Bool, original: Instance?) {
// FIXME: All orderings are treated as acquiring-and-releasing.
let result = _AtomicReferenceStorage.atomicCompareExchange(
expected: expected,
desired: desired,
at: _extract(pointer))
guard let original = result.original else { return (result.exchanged, nil) }
return (result.exchanged, unsafeDowncast(original, to: Instance.self))
}
@inlinable @inline(__always)
@_alwaysEmitIntoClient
@_semantics("atomics.requires_constant_orderings")
public static func atomicCompareExchange(
expected: Instance?,
desired: __owned Instance?,
at pointer: UnsafeMutablePointer<Self>,
successOrdering: AtomicUpdateOrdering,
failureOrdering: AtomicLoadOrdering
) -> (exchanged: Bool, original: Instance?) {
// FIXME: All orderings are treated as acquiring-and-releasing.
let result = _AtomicReferenceStorage.atomicCompareExchange(
expected: expected,
desired: desired,
at: _extract(pointer))
guard let original = result.original else { return (result.exchanged, nil) }
return (result.exchanged, unsafeDowncast(original, to: Instance.self))
}
@inlinable @inline(__always)
@_alwaysEmitIntoClient
@_semantics("atomics.requires_constant_orderings")
public static func atomicWeakCompareExchange(
expected: Instance?,
desired: __owned Instance?,
at pointer: UnsafeMutablePointer<Self>,
successOrdering: AtomicUpdateOrdering,
failureOrdering: AtomicLoadOrdering
) -> (exchanged: Bool, original: Instance?) {
// FIXME: All orderings are treated as acquiring-and-releasing.
let result = _AtomicReferenceStorage.atomicCompareExchange(
expected: expected,
desired: desired,
at: _extract(pointer))
guard let original = result.original else { return (result.exchanged, nil) }
return (result.exchanged, unsafeDowncast(original, to: Instance.self))
}
}
+218
View File
@@ -0,0 +1,218 @@
//===----------------------------------------------------------------------===//
//
// This source file is part of the Swift Atomics open source project
//
// Copyright (c) 2020 Apple Inc. and the Swift project authors
// Licensed under Apache License v2.0 with Runtime Library Exception
//
// See https://swift.org/LICENSE.txt for license information
// See https://swift.org/CONTRIBUTORS.txt for the list of Swift project authors
//
//===----------------------------------------------------------------------===//
/// A type that supports atomic operations through a separate atomic storage
/// representation.
public protocol AtomicValue {
associatedtype AtomicRepresentation: AtomicStorage
where AtomicRepresentation.Value == Self
}
/// The storage representation for an atomic value, providing pointer-based
/// atomic operations. This is a low-level implementation detail of atomic
/// types; instead of directly handling conforming types, it is usually better
/// to use the `UnsafeAtomic` or `ManagedAtomic` generics -- these provide more
/// convenient and safer interfaces while also ensuring that the storage is
/// correctly constructed and destroyed.
///
/// Logically, atomic storage representations are neither value- nor reference
/// types: they should be treated as non-copiable values with a custom
/// destructor. Such constructs cannot currently be modeled directly in Swift,
/// so types conforming to this protocol must be handled carefully to prevent
/// accidental copying. For example, it usually isn't safe to pass around atomic
/// storage representations as function arguments or return values. Instead,
/// they are usually addressed through unsafe pointers.
public protocol AtomicStorage {
/// The type whose values this storage representation is representing.
associatedtype Value
/// Encode the supplied value into its atomic storage representation.
///
/// Note: This is not an atomic operation. This call may have side effects
/// (such as unpaired retains of strong references) that will need to be
/// undone by calling `dispose()` before the storage value is deinitialized.
init(_ value: __owned Value)
/// Prepare this atomic storage value for deinitialization, extracting the
/// logical value it represents. This invalidates this atomic storage; you
/// must not perform any operations on it after this call (except for
/// deinitialization).
///
/// This call prevents resource leaks when destroying the storage
/// representation of certain `AtomicValue` types. (In particular, ones that
/// model strong references.)
///
/// Note: This is not an atomic operation. Logically, it implements a custom
/// destructor for the underlying non-copiable value.
__consuming func dispose() -> Value
/// Atomically loads and returns the value referenced by the given pointer,
/// applying the specified memory ordering.
///
/// - Parameter pointer: A memory location previously initialized with a value
/// returned by `prepareAtomicRepresentation(for:)`.
/// - Parameter ordering: The memory ordering to apply on this operation.
/// - Returns: The current value referenced by `pointer`.
@_semantics("atomics.requires_constant_orderings")
static func atomicLoad(
at pointer: UnsafeMutablePointer<Self>,
ordering: AtomicLoadOrdering
) -> Value
/// Atomically sets the value referenced by `pointer` to `desired`,
/// applying the specified memory ordering.
///
/// - Parameter desired: The desired new value.
/// - Parameter pointer: A memory location previously initialized with a value
/// returned by `prepareAtomicRepresentation(for:)`.
/// - Parameter ordering: The memory ordering to apply on this operation.
@_semantics("atomics.requires_constant_orderings")
static func atomicStore(
_ desired: __owned Value,
at pointer: UnsafeMutablePointer<Self>,
ordering: AtomicStoreOrdering
)
/// Atomically sets the value referenced by `pointer` to `desired` and returns
/// the original value, applying the specified memory ordering.
///
/// - Parameter desired: The desired new value.
/// - Parameter pointer: A memory location previously initialized with a value
/// returned by `prepareAtomicRepresentation(for:)`.
/// - Parameter ordering: The memory ordering to apply on this operation.
/// - Returns: The original value.
@_semantics("atomics.requires_constant_orderings")
static func atomicExchange(
_ desired: __owned Value,
at pointer: UnsafeMutablePointer<Self>,
ordering: AtomicUpdateOrdering
) -> Value
/// Perform an atomic compare and exchange operation on the value referenced
/// by `pointer`, applying the specified memory ordering.
///
/// This operation performs the following algorithm as a single atomic
/// transaction:
///
/// ```
/// atomic(self) { currentValue in
/// let original = currentValue
/// guard original == expected else { return (false, original) }
/// currentValue = desired
/// return (true, original)
/// }
/// ```
///
/// This method implements a "strong" compare and exchange operation
/// that does not permit spurious failures.
///
/// - Parameter expected: The expected current value.
/// - Parameter desired: The desired new value.
/// - Parameter pointer: A memory location previously initialized with a value
/// returned by `prepareAtomicRepresentation(for:)`.
/// - Parameter ordering: The memory ordering to apply on this operation.
/// - Returns: A tuple `(exchanged, original)`, where `exchanged` is true if
/// the exchange was successful, and `original` is the original value.
@_semantics("atomics.requires_constant_orderings")
static func atomicCompareExchange(
expected: Value,
desired: __owned Value,
at pointer: UnsafeMutablePointer<Self>,
ordering: AtomicUpdateOrdering
) -> (exchanged: Bool, original: Value)
/// Perform an atomic compare and exchange operation on the value referenced
/// by `pointer`, applying the specified success/failure memory orderings.
///
/// This operation performs the following algorithm as a single atomic
/// transaction:
///
/// ```
/// atomic(self) { currentValue in
/// let original = currentValue
/// guard original == expected else { return (false, original) }
/// currentValue = desired
/// return (true, original)
/// }
/// ```
///
/// The `successOrdering` argument specifies the memory ordering to use when
/// the operation manages to update the current value, while `failureOrdering`
/// will be used when the operation leaves the value intact.
///
/// This method implements a "strong" compare and exchange operation
/// that does not permit spurious failures.
///
/// - Parameter expected: The expected current value.
/// - Parameter desired: The desired new value.
/// - Parameter pointer: A memory location previously initialized with a value
/// returned by `prepareSelf(for:)`.
/// - Parameter successOrdering: The memory ordering to apply if this
/// operation performs the exchange.
/// - Parameter failureOrdering: The memory ordering to apply on this
/// operation does not perform the exchange.
/// - Returns: A tuple `(exchanged, original)`, where `exchanged` is true if
/// the exchange was successful, and `original` is the original value.
@_semantics("atomics.requires_constant_orderings")
static func atomicCompareExchange(
expected: Value,
desired: __owned Value,
at pointer: UnsafeMutablePointer<Self>,
successOrdering: AtomicUpdateOrdering,
failureOrdering: AtomicLoadOrdering
) -> (exchanged: Bool, original: Value)
/// Perform an atomic weak compare and exchange operation on the value
/// referenced by `pointer`, applying the specified success/failure memory
/// orderings. This compare-exchange variant is allowed to spuriously fail; it
/// is designed to be called in a loop until it indicates a successful
/// exchange has happened.
///
/// This operation performs the following algorithm as a single atomic
/// transaction:
///
/// ```
/// atomic(self) { currentValue in
/// let original = currentValue
/// guard original == expected else { return (false, original) }
/// currentValue = desired
/// return (true, original)
/// }
/// ```
///
/// (In this weak form, transient conditions may cause the `original ==
/// expected` check to sometimes return false when the two values are in fact
/// the same.)
///
/// The `successOrdering` argument specifies the memory ordering to use when the
/// operation manages to update the current value, while `failureOrdering`
/// will be used when the operation leaves the value intact.
///
/// - Parameter expected: The expected current value.
/// - Parameter desired: The desired new value.
/// - Parameter pointer: A memory location previously initialized with a value
/// returned by `prepareAtomicRepresentation(for:)`.
/// - Parameter successOrdering: The memory ordering to apply if this
/// operation performs the exchange.
/// - Parameter failureOrdering: The memory ordering to apply on this
/// operation does not perform the exchange.
/// - Returns: A tuple `(exchanged, original)`, where `exchanged` is true if
/// the exchange was successful, and `original` is the original value.
@_semantics("atomics.requires_constant_orderings")
static func atomicWeakCompareExchange(
expected: Value,
desired: __owned Value,
at pointer: UnsafeMutablePointer<Self>,
successOrdering: AtomicUpdateOrdering,
failureOrdering: AtomicLoadOrdering
) -> (exchanged: Bool, original: Value)
}
+117
View File
@@ -0,0 +1,117 @@
//===----------------------------------------------------------------------===//
//
// This source file is part of the Swift Atomics open source project
//
// Copyright (c) 2020 Apple Inc. and the Swift project authors
// Licensed under Apache License v2.0 with Runtime Library Exception
//
// See https://swift.org/LICENSE.txt for license information
// See https://swift.org/CONTRIBUTORS.txt for the list of Swift project authors
//
//===----------------------------------------------------------------------===//
import _AtomicsShims
public typealias DoubleWord = _AtomicsShims.DoubleWord
extension DoubleWord {
/// Initialize a new `DoubleWord` value given its high- and
/// low-order words.
@inlinable @inline(__always)
public init(high: UInt, low: UInt) {
self = _sa_dword_create(high, low)
}
/// Initialize a new `DoubleWord` value given its component words,
/// in the order in which they are laid out in memory.
@inlinable @inline(__always)
public init(first: UInt, second: UInt) {
#if _endian(little)
self = _sa_dword_create(second, first)
#else
self = _sa_dword_create(first, second)
#endif
}
/// The most significant word in `self`, considering it as a single,
/// wide integer value. This may correspond to either `first` or
/// `second`, depending on the endianness of the underlying
/// architecture.
@inlinable @inline(__always)
public var high: UInt {
get { _sa_dword_extract_high(self) }
set { self = _sa_dword_create(newValue, low) }
}
/// The least significant word in `self`, considering it as a
/// single, wide integer value. This may correspond to either
/// `first` or `second`, depending on the endianness of the
/// underlying architecture.
@inlinable @inline(__always)
public var low: UInt {
get { _sa_dword_extract_low(self) }
set { self = _sa_dword_create(high, newValue) }
}
/// The first word in `self` in its underlying binary
/// representation.
@inlinable @inline(__always)
public var first: UInt {
get {
#if _endian(little)
return _sa_dword_extract_low(self)
#else
return _sa_dword_extract_high(self)
#endif
}
set {
#if _endian(little)
self = _sa_dword_create(high, newValue)
#else
self = _sa_dword_create(newValue, low)
#endif
}
}
/// The second word in `self` in its underlying binary
/// representation.
@inlinable @inline(__always)
public var second: UInt {
get {
#if _endian(little)
return _sa_dword_extract_high(self)
#else
return _sa_dword_extract_low(self)
#endif
}
set {
#if _endian(little)
self = _sa_dword_create(newValue, low)
#else
self = _sa_dword_create(high, newValue)
#endif
}
}
}
extension DoubleWord: Equatable {
@inlinable
public static func ==(left: Self, right: Self) -> Bool {
left.high == right.high && left.low == right.low
}
}
extension DoubleWord: Hashable {
@inlinable
public func hash(into hasher: inout Hasher) {
hasher.combine(self.high)
hasher.combine(self.low)
}
}
extension DoubleWord: CustomStringConvertible {
public var description: String {
"DoubleWord(high: \(high), low: \(low))"
}
}
+366
View File
@@ -0,0 +1,366 @@
//===----------------------------------------------------------------------===//
//
// This source file is part of the Swift Atomics open source project
//
// Copyright (c) 2020 Apple Inc. and the Swift project authors
// Licensed under Apache License v2.0 with Runtime Library Exception
//
// See https://swift.org/LICENSE.txt for license information
// See https://swift.org/CONTRIBUTORS.txt for the list of Swift project authors
//
//===----------------------------------------------------------------------===//
%{
from gyb_utils import (
autogenerated_warning, integerOperations, lowerFirst, argLabel)
}%
${autogenerated_warning()}
/// An unsafe reference type holding an atomic value, requiring manual memory
/// management of the underlying storage representation.
@frozen
public struct UnsafeAtomic<Value: AtomicValue> {
public typealias Storage = Value.AtomicRepresentation
@usableFromInline
internal let _ptr: UnsafeMutablePointer<Storage>
/// Initialize an unsafe atomic value that uses the supplied memory location
/// for storage. The storage location must already be initialized to
/// represent a valid atomic value.
///
/// At the end of the lifetime of the atomic value, you must manually ensure
/// that the storage location is correctly `dispose()`d, deinitalized and
/// deallocated.
///
/// Note: This is not an atomic operation.
@_transparent // Debug performance
public init(
@_nonEphemeral at pointer: UnsafeMutablePointer<Storage>
) {
self._ptr = pointer
}
/// Create a new `UnsafeAtomic` value with the supplied initial value by
/// dynamically allocating storage for it.
///
/// This call is usually paired with `destroy` to get rid of the allocated
/// storage at the end of its lifetime.
///
/// Note: This is not an atomic operation.
@inlinable
public static func create(_ initialValue: __owned Value) -> Self {
let ptr = UnsafeMutablePointer<Storage>.allocate(capacity: 1)
ptr.initialize(to: Storage(initialValue))
return Self(at: ptr)
}
/// Disposes of the current value of the storage location corresponding to
/// this unsafe atomic value, then deinitializes and deallocates the storage.
///
/// Note: This is not an atomic operation.
///
/// - Returns: The last value stored in the storage representation before it
/// was destroyed.
@discardableResult
@inlinable
public func destroy() -> Value {
let result = _ptr.pointee.dispose()
_ptr.deinitialize(count: 1)
_ptr.deallocate()
return result
}
}
/// A reference type holding an atomic value, with automatic memory management.
public class ManagedAtomic<Value: AtomicValue> {
@usableFromInline
internal var _storage: Value.AtomicRepresentation
/// Initialize a new managed atomic instance holding the specified initial
/// value.
@inline(__always) @_alwaysEmitIntoClient
public init(_ value: Value) {
_storage = Value.AtomicRepresentation(value)
}
deinit {
_ = _storage.dispose()
}
@_alwaysEmitIntoClient @inline(__always)
internal var _ptr: UnsafeMutablePointer<Value.AtomicRepresentation> {
_getUnsafePointerToStoredProperties(self)
.assumingMemoryBound(to: Value.AtomicRepresentation.self)
}
}
% for type in ["UnsafeAtomic", "ManagedAtomic"]:
extension ${type} {
/// Atomically loads and returns the current value, applying the specified
/// memory ordering.
///
/// - Parameter ordering: The memory ordering to apply on this operation.
/// - Returns: The current value.
@_semantics("atomics.requires_constant_orderings")
@_transparent @_alwaysEmitIntoClient
public func load(
ordering: AtomicLoadOrdering
) -> Value {
Value.AtomicRepresentation.atomicLoad(at: _ptr, ordering: ordering)
}
/// Atomically sets the current value to `desired`, applying the specified
/// memory ordering.
///
/// - Parameter desired: The desired new value.
/// - Parameter ordering: The memory ordering to apply on this operation.
@_semantics("atomics.requires_constant_orderings")
@_transparent @_alwaysEmitIntoClient
public func store(
_ desired: __owned Value,
ordering: AtomicStoreOrdering
) {
Value.AtomicRepresentation.atomicStore(desired, at: _ptr, ordering: ordering)
}
/// Atomically sets the current value to `desired` and returns the original
/// value, applying the specified memory ordering.
///
/// - Parameter desired: The desired new value.
/// - Parameter ordering: The memory ordering to apply on this operation.
/// - Returns: The original value.
@_semantics("atomics.requires_constant_orderings")
@_transparent @_alwaysEmitIntoClient
public func exchange(
_ desired: __owned Value,
ordering: AtomicUpdateOrdering
) -> Value {
Value.AtomicRepresentation.atomicExchange(desired, at: _ptr, ordering: ordering)
}
/// Perform an atomic compare and exchange operation on the current value,
/// applying the specified memory ordering.
///
/// This operation performs the following algorithm as a single atomic
/// transaction:
///
/// ```
/// atomic(self) { currentValue in
/// let original = currentValue
/// guard original == expected else { return (false, original) }
/// currentValue = desired
/// return (true, original)
/// }
/// ```
///
/// This method implements a "strong" compare and exchange operation
/// that does not permit spurious failures.
///
/// - Parameter expected: The expected current value.
/// - Parameter desired: The desired new value.
/// - Parameter ordering: The memory ordering to apply on this operation.
/// - Returns: A tuple `(exchanged, original)`, where `exchanged` is true if
/// the exchange was successful, and `original` is the original value.
@_semantics("atomics.requires_constant_orderings")
@_transparent @_alwaysEmitIntoClient
public func compareExchange(
expected: Value,
desired: __owned Value,
ordering: AtomicUpdateOrdering
) -> (exchanged: Bool, original: Value) {
Value.AtomicRepresentation.atomicCompareExchange(
expected: expected,
desired: desired,
at: _ptr,
ordering: ordering)
}
/// Perform an atomic compare and exchange operation on the current value,
/// applying the specified success/failure memory orderings.
///
/// This operation performs the following algorithm as a single atomic
/// transaction:
///
/// ```
/// atomic(self) { currentValue in
/// let original = currentValue
/// guard original == expected else { return (false, original) }
/// currentValue = desired
/// return (true, original)
/// }
/// ```
///
/// The `succesOrdering` argument specifies the memory ordering to use when
/// the operation manages to update the current value, while `failureOrdering`
/// will be used when the operation leaves the value intact.
///
/// This method implements a "strong" compare and exchange operation
/// that does not permit spurious failures.
///
/// - Parameter expected: The expected current value.
/// - Parameter desired: The desired new value.
/// - Parameter successOrdering: The memory ordering to apply if this
/// operation performs the exchange.
/// - Parameter failureOrdering: The memory ordering to apply on this
/// operation does not perform the exchange.
/// - Returns: A tuple `(exchanged, original)`, where `exchanged` is true if
/// the exchange was successful, and `original` is the original value.
@_semantics("atomics.requires_constant_orderings")
@_transparent @_alwaysEmitIntoClient
public func compareExchange(
expected: Value,
desired: __owned Value,
successOrdering: AtomicUpdateOrdering,
failureOrdering: AtomicLoadOrdering
) -> (exchanged: Bool, original: Value) {
Value.AtomicRepresentation.atomicCompareExchange(
expected: expected,
desired: desired,
at: _ptr,
successOrdering: successOrdering,
failureOrdering: failureOrdering)
}
/// Perform an atomic weak compare and exchange operation on the current
/// value, applying the specified success/failure memory orderings. This
/// compare-exchange variant is allowed to spuriously fail; it is designed to
/// be called in a loop until it indicates a successful exchange has happened.
///
/// This operation performs the following algorithm as a single atomic
/// transaction:
///
/// ```
/// atomic(self) { currentValue in
/// let original = currentValue
/// guard original == expected else { return (false, original) }
/// currentValue = desired
/// return (true, original)
/// }
/// ```
///
/// (In this weak form, transient conditions may cause the `original ==
/// expected` check to sometimes return false when the two values are in fact
/// the same.)
///
/// The `ordering` argument specifies the memory ordering to use when the
/// operation manages to update the current value, while `failureOrdering`
/// will be used when the operation leaves the value intact.
///
/// - Parameter expected: The expected current value.
/// - Parameter desired: The desired new value.
/// - Parameter successOrdering: The memory ordering to apply if this
/// operation performs the exchange.
/// - Parameter failureOrdering: The memory ordering to apply on this
/// operation does not perform the exchange.
/// - Returns: A tuple `(exchanged, original)`, where `exchanged` is true if
/// the exchange was successful, and `original` is the original value.
@_semantics("atomics.requires_constant_orderings")
@_transparent @_alwaysEmitIntoClient
public func weakCompareExchange(
expected: Value,
desired: __owned Value,
successOrdering: AtomicUpdateOrdering,
failureOrdering: AtomicLoadOrdering
) -> (exchanged: Bool, original: Value) {
Value.AtomicRepresentation.atomicWeakCompareExchange(
expected: expected,
desired: desired,
at: _ptr,
successOrdering: successOrdering,
failureOrdering: failureOrdering)
}
}
extension ${type} where Value: AtomicInteger {
% for (name, _, op, label, doc) in integerOperations:
/// Perform an atomic ${doc} operation and return the original value, applying
/// the specified memory ordering.
///
% if "Wrapping" in name:
/// Note: This operation silently wraps around on overflow, like the
/// `${op}` operator does on `Int` values.
///
% end
/// - Parameter operand: An integer value.
/// - Parameter ordering: The memory ordering to apply on this operation.
/// - Returns: The original value before the operation.
@_semantics("atomics.requires_constant_orderings")
@_transparent @_alwaysEmitIntoClient
public func loadThen${name}(
${label} operand: Value${" = 1" if "crement" in name else ""},
ordering: AtomicUpdateOrdering
) -> Value {
Value.AtomicRepresentation.atomicLoadThen${name}(
${argLabel(label)}operand,
at: _ptr,
ordering: ordering)
}
% end
% for (name, _, op, label, doc) in integerOperations:
/// Perform an atomic ${doc} operation and return the new value, applying
/// the specified memory ordering.
///
% if "Wrapping" in name:
/// Note: This operation silently wraps around on overflow, like the
/// `${op}` operator does on `Int` values.
///
% end
/// - Parameter operand: An integer value.
/// - Parameter ordering: The memory ordering to apply on this operation.
/// - Returns: The new value after the operation.
@_semantics("atomics.requires_constant_orderings")
@_transparent @_alwaysEmitIntoClient
public func ${lowerFirst(name)}ThenLoad(
${label} operand: Value${" = 1" if "crement" in name else ""},
ordering: AtomicUpdateOrdering
) -> Value {
let original = Value.AtomicRepresentation.atomicLoadThen${name}(
${argLabel(label)}operand,
at: _ptr,
ordering: ordering)
return original ${op} operand
}
% end
/// Perform an atomic wrapping increment operation applying the
/// specified memory ordering.
///
/// Note: This operation silently wraps around on overflow, like the
/// `&+=` operator does on `Int` values.
///
/// - Parameter operand: The value to add to the current value.
/// - Parameter ordering: The memory ordering to apply on this operation.
@_semantics("atomics.requires_constant_orderings")
@_transparent @_alwaysEmitIntoClient
public func wrappingIncrement(
by operand: Value = 1,
ordering: AtomicUpdateOrdering
) {
_ = Value.AtomicRepresentation.atomicLoadThenWrappingIncrement(
by: operand,
at: _ptr,
ordering: ordering)
}
/// Perform an atomic wrapping decrement operation applying the
/// specified memory ordering.
///
/// Note: This operation silently wraps around on overflow, like the
/// `&-=` operator does on `Int` values.
///
/// - Parameter operand: The value to subtract from the current value.
/// - Parameter ordering: The memory ordering to apply on this operation.
@_semantics("atomics.requires_constant_orderings")
@_transparent @_alwaysEmitIntoClient
public func wrappingDecrement(
by operand: Value = 1,
ordering: AtomicUpdateOrdering
) {
_ = Value.AtomicRepresentation.atomicLoadThenWrappingDecrement(
by: operand,
at: _ptr,
ordering: ordering)
}
}
@@ -0,0 +1,222 @@
//===----------------------------------------------------------------------===//
//
// This source file is part of the Swift Atomics open source project
//
// Copyright (c) 2020 Apple Inc. and the Swift project authors
// Licensed under Apache License v2.0 with Runtime Library Exception
//
// See https://swift.org/LICENSE.txt for license information
// See https://swift.org/CONTRIBUTORS.txt for the list of Swift project authors
//
//===----------------------------------------------------------------------===//
%{
from gyb_utils import (
autogenerated_warning, loadOrderings, storeOrderings, updateOrderings,
integerOperations, lowerFirst, argLabel, shimOrders)
def atomicTypes():
return ["Int", "Int64", "Int32", "Int16", "Int8",
"UInt", "UInt64", "UInt32", "UInt16", "UInt8",
"DoubleWord"]
}%
${autogenerated_warning()}
import _AtomicsShims
% for swiftType in atomicTypes():
extension ${swiftType}: AtomicValue {
public struct AtomicRepresentation {
public typealias Value = ${swiftType}
@usableFromInline
var _storage: _Atomic${swiftType}Storage
@inline(__always) @_alwaysEmitIntoClient
public init(_ value: Value) {
self._storage = _sa_prepare_${swiftType}(value)
}
@inline(__always) @_alwaysEmitIntoClient
public func dispose() -> Value {
_sa_dispose_${swiftType}(_storage)
}
}
}
extension UnsafeMutablePointer
where Pointee == ${swiftType}.AtomicRepresentation {
@inlinable @inline(__always)
internal var _extract: UnsafeMutablePointer<_Atomic${swiftType}Storage> {
// `${swiftType}` is layout-compatible with its only stored property.
return UnsafeMutableRawPointer(self)
.assumingMemoryBound(to: _Atomic${swiftType}Storage.self)
}
}
extension ${swiftType}.AtomicRepresentation: AtomicStorage {
@_semantics("atomics.requires_constant_orderings")
@_transparent @_alwaysEmitIntoClient
public static func atomicLoad(
at pointer: UnsafeMutablePointer<Self>,
ordering: AtomicLoadOrdering
) -> Value {
switch ordering {
% for (swiftOrder, shimOrder) in loadOrderings:
case .${swiftOrder}:
return _sa_load_${shimOrder}_${swiftType}(pointer._extract)
% end
default:
fatalError("Unsupported ordering")
}
}
@_semantics("atomics.requires_constant_orderings")
@_transparent @_alwaysEmitIntoClient
public static func atomicStore(
_ desired: Value,
at pointer: UnsafeMutablePointer<Self>,
ordering: AtomicStoreOrdering
) {
switch ordering {
% for (swiftOrder, shimOrder) in storeOrderings:
case .${swiftOrder}:
_sa_store_${shimOrder}_${swiftType}(pointer._extract, desired)
% end
default:
fatalError("Unsupported ordering")
}
}
@_semantics("atomics.requires_constant_orderings")
@_transparent @_alwaysEmitIntoClient
public static func atomicExchange(
_ desired: Value,
at pointer: UnsafeMutablePointer<Self>,
ordering: AtomicUpdateOrdering
) -> Value {
switch ordering {
% for (swiftOrder, shimOrder, _) in updateOrderings:
case .${swiftOrder}:
return _sa_exchange_${shimOrder}_${swiftType}(pointer._extract, desired)
% end
default:
fatalError("Unsupported ordering")
}
}
@_semantics("atomics.requires_constant_orderings")
@_transparent @_alwaysEmitIntoClient
public static func atomicCompareExchange(
expected: Value,
desired: Value,
at pointer: UnsafeMutablePointer<Self>,
ordering: AtomicUpdateOrdering
) -> (exchanged: Bool, original: Value) {
var expected = expected
let exchanged: Bool
switch ordering {
% for (swiftOrder, shimOrder, failOrder) in updateOrderings:
case .${swiftOrder}:
exchanged = _sa_cmpxchg_strong_${shimOrder}_${failOrder}_${swiftType}(
pointer._extract,
&expected, desired)
% end
default:
fatalError("Unsupported ordering")
}
return (exchanged, expected)
}
@_semantics("atomics.requires_constant_orderings")
@_transparent @_alwaysEmitIntoClient
public static func atomicCompareExchange(
expected: Value,
desired: Value,
at pointer: UnsafeMutablePointer<Self>,
successOrdering: AtomicUpdateOrdering,
failureOrdering: AtomicLoadOrdering
) -> (exchanged: Bool, original: Value) {
var expected = expected
let exchanged: Bool
// FIXME: stdatomic.h (and LLVM underneath) doesn't support
// arbitrary ordering combinations yet, so upgrade the success
// ordering when necessary so that it is at least as "strong" as
// the failure case.
switch (successOrdering, failureOrdering) {
% for (swiftSuccess, shimSuccess, _) in updateOrderings:
% for (swiftFailure, shimFailure) in loadOrderings:
case (.${swiftSuccess}, .${swiftFailure}):
exchanged = _sa_cmpxchg_strong_${shimOrders(shimSuccess, shimFailure)}_${swiftType}(
pointer._extract,
&expected,
desired)
% end
% end
default:
fatalError("Unsupported ordering")
}
return (exchanged, expected)
}
@_semantics("atomics.requires_constant_orderings")
@_transparent @_alwaysEmitIntoClient
public static func atomicWeakCompareExchange(
expected: Value,
desired: Value,
at pointer: UnsafeMutablePointer<Self>,
successOrdering: AtomicUpdateOrdering,
failureOrdering: AtomicLoadOrdering
) -> (exchanged: Bool, original: Value) {
var expected = expected
let exchanged: Bool
// FIXME: stdatomic.h (and LLVM underneath) doesn't support
// arbitrary ordering combinations yet, so upgrade the success
// ordering when necessary so that it is at least as "strong" as
// the failure case.
switch (successOrdering, failureOrdering) {
% for (swiftSuccess, shimSuccess, _) in updateOrderings:
% for (swiftFailure, shimFailure) in loadOrderings:
case (.${swiftSuccess}, .${swiftFailure}):
exchanged = _sa_cmpxchg_weak_${shimOrders(shimSuccess, shimFailure)}_${swiftType}(
pointer._extract,
&expected,
desired)
% end
% end
default:
fatalError("Unsupported ordering")
}
return (exchanged, expected)
}
}
% if swiftType <> "DoubleWord":
extension ${swiftType}: AtomicInteger {}
extension ${swiftType}.AtomicRepresentation: AtomicIntegerStorage {
% for (name, cname, op, label, doc) in integerOperations:
% defaultValue = " = 1" if label <> "" else ""
@_semantics("atomics.requires_constant_orderings")
@_transparent @_alwaysEmitIntoClient
@discardableResult
public static func atomicLoadThen${name}(
${label} operand: Value${defaultValue},
at pointer: UnsafeMutablePointer<Self>,
ordering: AtomicUpdateOrdering
) -> Value {
switch ordering {
% for (swiftOrder, shimOrder, _) in updateOrderings:
case .${swiftOrder}:
return _sa_fetch_${cname}_${shimOrder}_${swiftType}(
pointer._extract,
operand)
% end
default:
fatalError("Unsupported ordering")
}
}
% end
}
% end
% end
@@ -0,0 +1,303 @@
//===----------------------------------------------------------------------===//
//
// This source file is part of the Swift Atomics open source project
//
// Copyright (c) 2020 Apple Inc. and the Swift project authors
// Licensed under Apache License v2.0 with Runtime Library Exception
//
// See https://swift.org/LICENSE.txt for license information
// See https://swift.org/CONTRIBUTORS.txt for the list of Swift project authors
//
//===----------------------------------------------------------------------===//
%{
from gyb_utils import autogenerated_warning
atomicTypes = [
"UnsafeRawPointer",
"UnsafeMutableRawPointer",
"UnsafePointer",
"UnsafeMutablePointer",
"Unmanaged",
]
}%
${autogenerated_warning()}
% for swiftType in atomicTypes:
extension ${swiftType}: AtomicValue {
@frozen
public struct AtomicRepresentation {
public typealias Value = ${swiftType}
@usableFromInline internal typealias Storage = Int.AtomicRepresentation
@usableFromInline
internal let _storage: Storage
@inline(__always) @_alwaysEmitIntoClient
public init(_ value: Value) {
self._storage = .init(Self._encode(value))
}
@inline(__always) @_alwaysEmitIntoClient
public func dispose() -> Value {
Self._decode(_storage.dispose())
}
}
}
extension ${swiftType}.AtomicRepresentation {
@_transparent @_alwaysEmitIntoClient
@usableFromInline
internal static func _extract(
_ ptr: UnsafeMutablePointer<Self>
) -> UnsafeMutablePointer<Storage> {
// `Self` is layout-compatible with its only stored property.
return UnsafeMutableRawPointer(ptr)
.assumingMemoryBound(to: Storage.self)
}
@_transparent @_alwaysEmitIntoClient
internal static func _decode(_ bitPattern: Int) -> Value {
% if swiftType == "Unmanaged":
return Unmanaged.fromOpaque(UnsafeRawPointer(bitPattern: bitPattern)!)
% else:
return ${swiftType}(bitPattern: bitPattern)!
% end
}
@_transparent @_alwaysEmitIntoClient
internal static func _encode(_ value: Value) -> Int {
% if swiftType == "Unmanaged":
return Int(bitPattern: value.toOpaque())
% else:
return Int(bitPattern: value)
% end
}
}
extension ${swiftType}.AtomicRepresentation: AtomicStorage {
@_semantics("atomics.requires_constant_orderings")
@_transparent @_alwaysEmitIntoClient
public static func atomicLoad(
at pointer: UnsafeMutablePointer<Self>,
ordering: AtomicLoadOrdering
) -> Value {
let encoded = Storage.atomicLoad(at: _extract(pointer), ordering: ordering)
return _decode(encoded)
}
@_semantics("atomics.requires_constant_orderings")
@_transparent @_alwaysEmitIntoClient
public static func atomicStore(
_ desired: Value,
at pointer: UnsafeMutablePointer<Self>,
ordering: AtomicStoreOrdering
) {
Storage.atomicStore(_encode(desired), at: _extract(pointer), ordering: ordering)
}
@_semantics("atomics.requires_constant_orderings")
@_transparent @_alwaysEmitIntoClient
public static func atomicExchange(
_ desired: Value,
at pointer: UnsafeMutablePointer<Self>,
ordering: AtomicUpdateOrdering
) -> Value {
_decode(Storage.atomicExchange(_encode(desired), at: _extract(pointer), ordering: ordering))
}
@_semantics("atomics.requires_constant_orderings")
@_transparent @_alwaysEmitIntoClient
public static func atomicCompareExchange(
expected: Value,
desired: Value,
at pointer: UnsafeMutablePointer<Self>,
ordering: AtomicUpdateOrdering
) -> (exchanged: Bool, original: Value) {
let (exchanged, original) = Storage.atomicCompareExchange(
expected: _encode(expected),
desired: _encode(desired),
at: _extract(pointer),
ordering: ordering)
return (exchanged, _decode(original))
}
@_semantics("atomics.requires_constant_orderings")
@_transparent @_alwaysEmitIntoClient
public static func atomicCompareExchange(
expected: Value,
desired: Value,
at pointer: UnsafeMutablePointer<Self>,
successOrdering: AtomicUpdateOrdering,
failureOrdering: AtomicLoadOrdering
) -> (exchanged: Bool, original: Value) {
let (exchanged, original) = Storage.atomicCompareExchange(
expected: _encode(expected),
desired: _encode(desired),
at: _extract(pointer),
successOrdering: successOrdering,
failureOrdering: failureOrdering)
return (exchanged, _decode(original))
}
@_semantics("atomics.requires_constant_orderings")
@_transparent @_alwaysEmitIntoClient
public static func atomicWeakCompareExchange(
expected: Value,
desired: Value,
at pointer: UnsafeMutablePointer<Self>,
successOrdering: AtomicUpdateOrdering,
failureOrdering: AtomicLoadOrdering
) -> (exchanged: Bool, original: Value) {
let (exchanged, original) = Storage.atomicWeakCompareExchange(
expected: _encode(expected),
desired: _encode(desired),
at: _extract(pointer),
successOrdering: successOrdering,
failureOrdering: failureOrdering)
return (exchanged, _decode(original))
}
}
% end
% for swiftType in atomicTypes:
extension ${swiftType}: AtomicOptionalWrappable {
@frozen
public struct AtomicOptionalRepresentation {
public typealias Value = ${swiftType}?
@usableFromInline internal typealias Storage = Int.AtomicRepresentation
@usableFromInline
internal let _storage: Storage
@inline(__always) @_alwaysEmitIntoClient
public init(_ value: Value) {
self._storage = .init(Self._encode(value))
}
@inline(__always) @_alwaysEmitIntoClient
public func dispose() -> Value {
Self._decode(_storage.dispose())
}
}
}
extension ${swiftType}.AtomicOptionalRepresentation {
@_transparent @_alwaysEmitIntoClient
@usableFromInline
internal static func _extract(
_ ptr: UnsafeMutablePointer<Self>
) -> UnsafeMutablePointer<Storage> {
// `Self` is layout-compatible with its only stored property.
return UnsafeMutableRawPointer(ptr)
.assumingMemoryBound(to: Storage.self)
}
@_transparent @_alwaysEmitIntoClient
internal static func _decode(_ bitPattern: Int) -> Value {
% if swiftType == "Unmanaged":
guard let opaque = UnsafeRawPointer(bitPattern: bitPattern) else {
return nil
}
return Unmanaged.fromOpaque(opaque)
% else:
return ${swiftType}(bitPattern: bitPattern)
% end
}
@_transparent @_alwaysEmitIntoClient
internal static func _encode(_ value: Value) -> Int {
guard let value = value else { return 0 }
% if swiftType == "Unmanaged":
return Int(bitPattern: value.toOpaque())
% else:
return Int(bitPattern: value)
% end
}
}
extension ${swiftType}.AtomicOptionalRepresentation: AtomicStorage {
@_semantics("atomics.requires_constant_orderings")
@_transparent @_alwaysEmitIntoClient
public static func atomicLoad(
at pointer: UnsafeMutablePointer<Self>,
ordering: AtomicLoadOrdering
) -> Value {
let encoded = Storage.atomicLoad(at: _extract(pointer), ordering: ordering)
return _decode(encoded)
}
@_semantics("atomics.requires_constant_orderings")
@_transparent @_alwaysEmitIntoClient
public static func atomicStore(
_ desired: Value,
at pointer: UnsafeMutablePointer<Self>,
ordering: AtomicStoreOrdering
) {
Storage.atomicStore(_encode(desired), at: _extract(pointer), ordering: ordering)
}
@_semantics("atomics.requires_constant_orderings")
@_transparent @_alwaysEmitIntoClient
public static func atomicExchange(
_ desired: Value,
at pointer: UnsafeMutablePointer<Self>,
ordering: AtomicUpdateOrdering
) -> Value {
_decode(Storage.atomicExchange(_encode(desired), at: _extract(pointer), ordering: ordering))
}
@_semantics("atomics.requires_constant_orderings")
@_transparent @_alwaysEmitIntoClient
public static func atomicCompareExchange(
expected: Value,
desired: Value,
at pointer: UnsafeMutablePointer<Self>,
ordering: AtomicUpdateOrdering
) -> (exchanged: Bool, original: Value) {
let (exchanged, original) = Storage.atomicCompareExchange(
expected: _encode(expected),
desired: _encode(desired),
at: _extract(pointer),
ordering: ordering)
return (exchanged, _decode(original))
}
@_semantics("atomics.requires_constant_orderings")
@_transparent @_alwaysEmitIntoClient
public static func atomicCompareExchange(
expected: Value,
desired: Value,
at pointer: UnsafeMutablePointer<Self>,
successOrdering: AtomicUpdateOrdering,
failureOrdering: AtomicLoadOrdering
) -> (exchanged: Bool, original: Value) {
let (exchanged, original) = Storage.atomicCompareExchange(
expected: _encode(expected),
desired: _encode(desired),
at: _extract(pointer),
successOrdering: successOrdering,
failureOrdering: failureOrdering)
return (exchanged, _decode(original))
}
@_semantics("atomics.requires_constant_orderings")
@_transparent @_alwaysEmitIntoClient
public static func atomicWeakCompareExchange(
expected: Value,
desired: Value,
at pointer: UnsafeMutablePointer<Self>,
successOrdering: AtomicUpdateOrdering,
failureOrdering: AtomicLoadOrdering
) -> (exchanged: Bool, original: Value) {
let (exchanged, original) = Storage.atomicWeakCompareExchange(
expected: _encode(expected),
desired: _encode(desired),
at: _extract(pointer),
successOrdering: successOrdering,
failureOrdering: failureOrdering)
return (exchanged, _decode(original))
}
}
% end
@@ -0,0 +1,694 @@
//===----------------------------------------------------------------------===//
//
// This source file is part of the Swift Atomics open source project
//
// Copyright (c) 2020 Apple Inc. and the Swift project authors
// Licensed under Apache License v2.0 with Runtime Library Exception
//
// See https://swift.org/LICENSE.txt for license information
// See https://swift.org/CONTRIBUTORS.txt for the list of Swift project authors
//
//===----------------------------------------------------------------------===//
// #############################################################################
// # #
// # DO NOT EDIT THIS FILE; IT IS AUTOGENERATED. #
// # #
// #############################################################################
import _AtomicsShims
extension Bool: AtomicValue {
public struct AtomicRepresentation {
public typealias Value = Bool
@usableFromInline
internal var _storage: _AtomicBoolStorage
@inline(__always) @_alwaysEmitIntoClient
public init(_ value: Bool) {
_storage = _sa_prepare_Bool(value)
}
@inline(__always) @_alwaysEmitIntoClient
public func dispose() -> Value {
_sa_dispose_Bool(_storage)
}
}
}
extension Bool.AtomicRepresentation {
@_transparent @_alwaysEmitIntoClient
@usableFromInline
static func _extract(
_ ptr: UnsafeMutablePointer<Self>
) -> UnsafeMutablePointer<_AtomicBoolStorage> {
// `Self` is layout-compatible with its only stored property.
return UnsafeMutableRawPointer(ptr)
.assumingMemoryBound(to: _AtomicBoolStorage.self)
}
}
extension Bool.AtomicRepresentation: AtomicStorage {
@_semantics("atomics.requires_constant_orderings")
@_transparent @_alwaysEmitIntoClient
public static func atomicLoad(
at pointer: UnsafeMutablePointer<Bool.AtomicRepresentation>,
ordering: AtomicLoadOrdering
) -> Bool {
switch ordering {
case .relaxed:
return _sa_load_relaxed_Bool(_extract(pointer))
case .acquiring:
return _sa_load_acquire_Bool(_extract(pointer))
case .sequentiallyConsistent:
return _sa_load_seq_cst_Bool(_extract(pointer))
default:
fatalError("Unsupported ordering")
}
}
@_semantics("atomics.requires_constant_orderings")
@_transparent @_alwaysEmitIntoClient
public static func atomicStore(
_ desired: __owned Bool,
at pointer: UnsafeMutablePointer<Bool.AtomicRepresentation>,
ordering: AtomicStoreOrdering
) {
switch ordering {
case .relaxed:
_sa_store_relaxed_Bool(_extract(pointer), desired)
case .releasing:
_sa_store_release_Bool(_extract(pointer), desired)
case .sequentiallyConsistent:
_sa_store_seq_cst_Bool(_extract(pointer), desired)
default:
fatalError("Unsupported ordering")
}
}
@_semantics("atomics.requires_constant_orderings")
@_transparent @_alwaysEmitIntoClient
public static func atomicExchange(
_ desired: __owned Bool,
at pointer: UnsafeMutablePointer<Bool.AtomicRepresentation>,
ordering: AtomicUpdateOrdering
) -> Bool {
switch ordering {
case .relaxed:
return _sa_exchange_relaxed_Bool(_extract(pointer), desired)
case .acquiring:
return _sa_exchange_acquire_Bool(_extract(pointer), desired)
case .releasing:
return _sa_exchange_release_Bool(_extract(pointer), desired)
case .acquiringAndReleasing:
return _sa_exchange_acq_rel_Bool(_extract(pointer), desired)
case .sequentiallyConsistent:
return _sa_exchange_seq_cst_Bool(_extract(pointer), desired)
default:
fatalError("Unsupported ordering")
}
}
@_semantics("atomics.requires_constant_orderings")
@_transparent @_alwaysEmitIntoClient
public static func atomicCompareExchange(
expected: Bool,
desired: __owned Bool,
at pointer: UnsafeMutablePointer<Bool.AtomicRepresentation>,
ordering: AtomicUpdateOrdering
) -> (exchanged: Bool, original: Bool) {
var expected = expected
let exchanged: Bool
switch ordering {
case .relaxed:
exchanged = _sa_cmpxchg_strong_relaxed_relaxed_Bool(
_extract(pointer),
&expected, desired)
case .acquiring:
exchanged = _sa_cmpxchg_strong_acquire_acquire_Bool(
_extract(pointer),
&expected, desired)
case .releasing:
exchanged = _sa_cmpxchg_strong_release_relaxed_Bool(
_extract(pointer),
&expected, desired)
case .acquiringAndReleasing:
exchanged = _sa_cmpxchg_strong_acq_rel_acquire_Bool(
_extract(pointer),
&expected, desired)
case .sequentiallyConsistent:
exchanged = _sa_cmpxchg_strong_seq_cst_seq_cst_Bool(
_extract(pointer),
&expected, desired)
default:
fatalError("Unsupported ordering")
}
return (exchanged, expected)
}
@_semantics("atomics.requires_constant_orderings")
@_transparent @_alwaysEmitIntoClient
public static func atomicCompareExchange(
expected: Bool,
desired: __owned Bool,
at pointer: UnsafeMutablePointer<Bool.AtomicRepresentation>,
successOrdering: AtomicUpdateOrdering,
failureOrdering: AtomicLoadOrdering
) -> (exchanged: Bool, original: Bool) {
var expected = expected
let exchanged: Bool
// FIXME: stdatomic.h (and LLVM underneath) doesn't support
// arbitrary ordering combinations yet, so upgrade the success
// ordering when necessary so that it is at least as "strong" as
// the failure case.
switch (successOrdering, failureOrdering) {
case (.relaxed, .relaxed):
exchanged = _sa_cmpxchg_strong_relaxed_relaxed_Bool(
_extract(pointer),
&expected,
desired)
case (.relaxed, .acquiring):
exchanged = _sa_cmpxchg_strong_acquire_acquire_Bool(
_extract(pointer),
&expected,
desired)
case (.relaxed, .sequentiallyConsistent):
exchanged = _sa_cmpxchg_strong_seq_cst_seq_cst_Bool(
_extract(pointer),
&expected,
desired)
case (.acquiring, .relaxed):
exchanged = _sa_cmpxchg_strong_acquire_relaxed_Bool(
_extract(pointer),
&expected,
desired)
case (.acquiring, .acquiring):
exchanged = _sa_cmpxchg_strong_acquire_acquire_Bool(
_extract(pointer),
&expected,
desired)
case (.acquiring, .sequentiallyConsistent):
exchanged = _sa_cmpxchg_strong_seq_cst_seq_cst_Bool(
_extract(pointer),
&expected,
desired)
case (.releasing, .relaxed):
exchanged = _sa_cmpxchg_strong_release_relaxed_Bool(
_extract(pointer),
&expected,
desired)
case (.releasing, .acquiring):
exchanged = _sa_cmpxchg_strong_acq_rel_acquire_Bool(
_extract(pointer),
&expected,
desired)
case (.releasing, .sequentiallyConsistent):
exchanged = _sa_cmpxchg_strong_seq_cst_seq_cst_Bool(
_extract(pointer),
&expected,
desired)
case (.acquiringAndReleasing, .relaxed):
exchanged = _sa_cmpxchg_strong_acq_rel_relaxed_Bool(
_extract(pointer),
&expected,
desired)
case (.acquiringAndReleasing, .acquiring):
exchanged = _sa_cmpxchg_strong_acq_rel_acquire_Bool(
_extract(pointer),
&expected,
desired)
case (.acquiringAndReleasing, .sequentiallyConsistent):
exchanged = _sa_cmpxchg_strong_seq_cst_seq_cst_Bool(
_extract(pointer),
&expected,
desired)
case (.sequentiallyConsistent, .relaxed):
exchanged = _sa_cmpxchg_strong_seq_cst_relaxed_Bool(
_extract(pointer),
&expected,
desired)
case (.sequentiallyConsistent, .acquiring):
exchanged = _sa_cmpxchg_strong_seq_cst_acquire_Bool(
_extract(pointer),
&expected,
desired)
case (.sequentiallyConsistent, .sequentiallyConsistent):
exchanged = _sa_cmpxchg_strong_seq_cst_seq_cst_Bool(
_extract(pointer),
&expected,
desired)
default:
fatalError("Unsupported ordering")
}
return (exchanged, expected)
}
@_semantics("atomics.requires_constant_orderings")
@_transparent @_alwaysEmitIntoClient
public static func atomicWeakCompareExchange(
expected: Bool,
desired: __owned Bool,
at pointer: UnsafeMutablePointer<Bool.AtomicRepresentation>,
successOrdering: AtomicUpdateOrdering,
failureOrdering: AtomicLoadOrdering
) -> (exchanged: Bool, original: Bool) {
var expected = expected
let exchanged: Bool
// FIXME: stdatomic.h (and LLVM underneath) doesn't support
// arbitrary ordering combinations yet, so upgrade the success
// ordering when necessary so that it is at least as "strong" as
// the failure case.
switch (successOrdering, failureOrdering) {
case (.relaxed, .relaxed):
exchanged = _sa_cmpxchg_weak_relaxed_relaxed_Bool(
_extract(pointer),
&expected,
desired)
case (.relaxed, .acquiring):
exchanged = _sa_cmpxchg_weak_acquire_acquire_Bool(
_extract(pointer),
&expected,
desired)
case (.relaxed, .sequentiallyConsistent):
exchanged = _sa_cmpxchg_weak_seq_cst_seq_cst_Bool(
_extract(pointer),
&expected,
desired)
case (.acquiring, .relaxed):
exchanged = _sa_cmpxchg_weak_acquire_relaxed_Bool(
_extract(pointer),
&expected,
desired)
case (.acquiring, .acquiring):
exchanged = _sa_cmpxchg_weak_acquire_acquire_Bool(
_extract(pointer),
&expected,
desired)
case (.acquiring, .sequentiallyConsistent):
exchanged = _sa_cmpxchg_weak_seq_cst_seq_cst_Bool(
_extract(pointer),
&expected,
desired)
case (.releasing, .relaxed):
exchanged = _sa_cmpxchg_weak_release_relaxed_Bool(
_extract(pointer),
&expected,
desired)
case (.releasing, .acquiring):
exchanged = _sa_cmpxchg_weak_acq_rel_acquire_Bool(
_extract(pointer),
&expected,
desired)
case (.releasing, .sequentiallyConsistent):
exchanged = _sa_cmpxchg_weak_seq_cst_seq_cst_Bool(
_extract(pointer),
&expected,
desired)
case (.acquiringAndReleasing, .relaxed):
exchanged = _sa_cmpxchg_weak_acq_rel_relaxed_Bool(
_extract(pointer),
&expected,
desired)
case (.acquiringAndReleasing, .acquiring):
exchanged = _sa_cmpxchg_weak_acq_rel_acquire_Bool(
_extract(pointer),
&expected,
desired)
case (.acquiringAndReleasing, .sequentiallyConsistent):
exchanged = _sa_cmpxchg_weak_seq_cst_seq_cst_Bool(
_extract(pointer),
&expected,
desired)
case (.sequentiallyConsistent, .relaxed):
exchanged = _sa_cmpxchg_weak_seq_cst_relaxed_Bool(
_extract(pointer),
&expected,
desired)
case (.sequentiallyConsistent, .acquiring):
exchanged = _sa_cmpxchg_weak_seq_cst_acquire_Bool(
_extract(pointer),
&expected,
desired)
case (.sequentiallyConsistent, .sequentiallyConsistent):
exchanged = _sa_cmpxchg_weak_seq_cst_seq_cst_Bool(
_extract(pointer),
&expected,
desired)
default:
fatalError("Unsupported ordering")
}
return (exchanged, expected)
}
}
// MARK: - Additional operations
extension Bool.AtomicRepresentation {
/// Perform an atomic logical AND operation on the value referenced by
/// `pointer` and return the original value, applying the specified memory
/// ordering.
///
/// - Parameter operand: A boolean value.
/// - Parameter pointer: A memory location previously initialized with a value
/// returned by `prepareAtomicRepresentation(for:)`.
/// - Parameter ordering: The memory ordering to apply on this operation.
/// - Returns: The original value before the operation.
@_semantics("atomics.requires_constant_orderings")
@_transparent @_alwaysEmitIntoClient
static func atomicLoadThenLogicalAnd(
with operand: Value,
at pointer: UnsafeMutablePointer<Self>,
ordering: AtomicUpdateOrdering
) -> Value {
switch ordering {
case .relaxed:
return _sa_fetch_and_relaxed_Bool(
_extract(pointer),
operand)
case .acquiring:
return _sa_fetch_and_acquire_Bool(
_extract(pointer),
operand)
case .releasing:
return _sa_fetch_and_release_Bool(
_extract(pointer),
operand)
case .acquiringAndReleasing:
return _sa_fetch_and_acq_rel_Bool(
_extract(pointer),
operand)
case .sequentiallyConsistent:
return _sa_fetch_and_seq_cst_Bool(
_extract(pointer),
operand)
default:
fatalError("Unsupported ordering")
}
}
/// Perform an atomic logical OR operation on the value referenced by
/// `pointer` and return the original value, applying the specified memory
/// ordering.
///
/// - Parameter operand: A boolean value.
/// - Parameter pointer: A memory location previously initialized with a value
/// returned by `prepareAtomicRepresentation(for:)`.
/// - Parameter ordering: The memory ordering to apply on this operation.
/// - Returns: The original value before the operation.
@_semantics("atomics.requires_constant_orderings")
@_transparent @_alwaysEmitIntoClient
static func atomicLoadThenLogicalOr(
with operand: Value,
at pointer: UnsafeMutablePointer<Self>,
ordering: AtomicUpdateOrdering
) -> Value {
switch ordering {
case .relaxed:
return _sa_fetch_or_relaxed_Bool(
_extract(pointer),
operand)
case .acquiring:
return _sa_fetch_or_acquire_Bool(
_extract(pointer),
operand)
case .releasing:
return _sa_fetch_or_release_Bool(
_extract(pointer),
operand)
case .acquiringAndReleasing:
return _sa_fetch_or_acq_rel_Bool(
_extract(pointer),
operand)
case .sequentiallyConsistent:
return _sa_fetch_or_seq_cst_Bool(
_extract(pointer),
operand)
default:
fatalError("Unsupported ordering")
}
}
/// Perform an atomic logical XOR operation on the value referenced by
/// `pointer` and return the original value, applying the specified memory
/// ordering.
///
/// - Parameter operand: A boolean value.
/// - Parameter pointer: A memory location previously initialized with a value
/// returned by `prepareAtomicRepresentation(for:)`.
/// - Parameter ordering: The memory ordering to apply on this operation.
/// - Returns: The original value before the operation.
@_semantics("atomics.requires_constant_orderings")
@_transparent @_alwaysEmitIntoClient
static func atomicLoadThenLogicalXor(
with operand: Value,
at pointer: UnsafeMutablePointer<Self>,
ordering: AtomicUpdateOrdering
) -> Value {
switch ordering {
case .relaxed:
return _sa_fetch_xor_relaxed_Bool(
_extract(pointer),
operand)
case .acquiring:
return _sa_fetch_xor_acquire_Bool(
_extract(pointer),
operand)
case .releasing:
return _sa_fetch_xor_release_Bool(
_extract(pointer),
operand)
case .acquiringAndReleasing:
return _sa_fetch_xor_acq_rel_Bool(
_extract(pointer),
operand)
case .sequentiallyConsistent:
return _sa_fetch_xor_seq_cst_Bool(
_extract(pointer),
operand)
default:
fatalError("Unsupported ordering")
}
}
}
extension UnsafeAtomic where Value == Bool {
/// Perform an atomic logical AND operation and return the original value, applying
/// the specified memory ordering.
///
/// - Parameter operand: A boolean value.
/// - Parameter ordering: The memory ordering to apply on this operation.
/// - Returns: The original value before the operation.
@_semantics("atomics.requires_constant_orderings")
@_transparent @_alwaysEmitIntoClient
public func loadThenLogicalAnd(
with operand: Value,
ordering: AtomicUpdateOrdering
) -> Value {
Value.AtomicRepresentation.atomicLoadThenLogicalAnd(
with: operand,
at: _ptr,
ordering: ordering)
}
/// Perform an atomic logical OR operation and return the original value, applying
/// the specified memory ordering.
///
/// - Parameter operand: A boolean value.
/// - Parameter ordering: The memory ordering to apply on this operation.
/// - Returns: The original value before the operation.
@_semantics("atomics.requires_constant_orderings")
@_transparent @_alwaysEmitIntoClient
public func loadThenLogicalOr(
with operand: Value,
ordering: AtomicUpdateOrdering
) -> Value {
Value.AtomicRepresentation.atomicLoadThenLogicalOr(
with: operand,
at: _ptr,
ordering: ordering)
}
/// Perform an atomic logical XOR operation and return the original value, applying
/// the specified memory ordering.
///
/// - Parameter operand: A boolean value.
/// - Parameter ordering: The memory ordering to apply on this operation.
/// - Returns: The original value before the operation.
@_semantics("atomics.requires_constant_orderings")
@_transparent @_alwaysEmitIntoClient
public func loadThenLogicalXor(
with operand: Value,
ordering: AtomicUpdateOrdering
) -> Value {
Value.AtomicRepresentation.atomicLoadThenLogicalXor(
with: operand,
at: _ptr,
ordering: ordering)
}
}
extension UnsafeAtomic where Value == Bool {
/// Perform an atomic logical AND operation and return the original value, applying
/// the specified memory ordering.
///
/// - Parameter operand: A boolean value.
/// - Parameter ordering: The memory ordering to apply on this operation.
/// - Returns: The original value before the operation.
@_semantics("atomics.requires_constant_orderings")
@_transparent @_alwaysEmitIntoClient
public func logicalAndThenLoad(
with operand: Value,
ordering: AtomicUpdateOrdering
) -> Value {
let original = Value.AtomicRepresentation.atomicLoadThenLogicalAnd(
with: operand,
at: _ptr,
ordering: ordering)
return original && operand
}
/// Perform an atomic logical OR operation and return the original value, applying
/// the specified memory ordering.
///
/// - Parameter operand: A boolean value.
/// - Parameter ordering: The memory ordering to apply on this operation.
/// - Returns: The original value before the operation.
@_semantics("atomics.requires_constant_orderings")
@_transparent @_alwaysEmitIntoClient
public func logicalOrThenLoad(
with operand: Value,
ordering: AtomicUpdateOrdering
) -> Value {
let original = Value.AtomicRepresentation.atomicLoadThenLogicalOr(
with: operand,
at: _ptr,
ordering: ordering)
return original || operand
}
/// Perform an atomic logical XOR operation and return the original value, applying
/// the specified memory ordering.
///
/// - Parameter operand: A boolean value.
/// - Parameter ordering: The memory ordering to apply on this operation.
/// - Returns: The original value before the operation.
@_semantics("atomics.requires_constant_orderings")
@_transparent @_alwaysEmitIntoClient
public func logicalXorThenLoad(
with operand: Value,
ordering: AtomicUpdateOrdering
) -> Value {
let original = Value.AtomicRepresentation.atomicLoadThenLogicalXor(
with: operand,
at: _ptr,
ordering: ordering)
return original != operand
}
}
extension ManagedAtomic where Value == Bool {
/// Perform an atomic logical AND operation and return the original value, applying
/// the specified memory ordering.
///
/// - Parameter operand: A boolean value.
/// - Parameter ordering: The memory ordering to apply on this operation.
/// - Returns: The original value before the operation.
@_semantics("atomics.requires_constant_orderings")
@_transparent @_alwaysEmitIntoClient
public func loadThenLogicalAnd(
with operand: Value,
ordering: AtomicUpdateOrdering
) -> Value {
Value.AtomicRepresentation.atomicLoadThenLogicalAnd(
with: operand,
at: _ptr,
ordering: ordering)
}
/// Perform an atomic logical OR operation and return the original value, applying
/// the specified memory ordering.
///
/// - Parameter operand: A boolean value.
/// - Parameter ordering: The memory ordering to apply on this operation.
/// - Returns: The original value before the operation.
@_semantics("atomics.requires_constant_orderings")
@_transparent @_alwaysEmitIntoClient
public func loadThenLogicalOr(
with operand: Value,
ordering: AtomicUpdateOrdering
) -> Value {
Value.AtomicRepresentation.atomicLoadThenLogicalOr(
with: operand,
at: _ptr,
ordering: ordering)
}
/// Perform an atomic logical XOR operation and return the original value, applying
/// the specified memory ordering.
///
/// - Parameter operand: A boolean value.
/// - Parameter ordering: The memory ordering to apply on this operation.
/// - Returns: The original value before the operation.
@_semantics("atomics.requires_constant_orderings")
@_transparent @_alwaysEmitIntoClient
public func loadThenLogicalXor(
with operand: Value,
ordering: AtomicUpdateOrdering
) -> Value {
Value.AtomicRepresentation.atomicLoadThenLogicalXor(
with: operand,
at: _ptr,
ordering: ordering)
}
}
extension ManagedAtomic where Value == Bool {
/// Perform an atomic logical AND operation and return the original value, applying
/// the specified memory ordering.
///
/// - Parameter operand: A boolean value.
/// - Parameter ordering: The memory ordering to apply on this operation.
/// - Returns: The original value before the operation.
@_semantics("atomics.requires_constant_orderings")
@_transparent @_alwaysEmitIntoClient
public func logicalAndThenLoad(
with operand: Value,
ordering: AtomicUpdateOrdering
) -> Value {
let original = Value.AtomicRepresentation.atomicLoadThenLogicalAnd(
with: operand,
at: _ptr,
ordering: ordering)
return original && operand
}
/// Perform an atomic logical OR operation and return the original value, applying
/// the specified memory ordering.
///
/// - Parameter operand: A boolean value.
/// - Parameter ordering: The memory ordering to apply on this operation.
/// - Returns: The original value before the operation.
@_semantics("atomics.requires_constant_orderings")
@_transparent @_alwaysEmitIntoClient
public func logicalOrThenLoad(
with operand: Value,
ordering: AtomicUpdateOrdering
) -> Value {
let original = Value.AtomicRepresentation.atomicLoadThenLogicalOr(
with: operand,
at: _ptr,
ordering: ordering)
return original || operand
}
/// Perform an atomic logical XOR operation and return the original value, applying
/// the specified memory ordering.
///
/// - Parameter operand: A boolean value.
/// - Parameter ordering: The memory ordering to apply on this operation.
/// - Returns: The original value before the operation.
@_semantics("atomics.requires_constant_orderings")
@_transparent @_alwaysEmitIntoClient
public func logicalXorThenLoad(
with operand: Value,
ordering: AtomicUpdateOrdering
) -> Value {
let original = Value.AtomicRepresentation.atomicLoadThenLogicalXor(
with: operand,
at: _ptr,
ordering: ordering)
return original != operand
}
}
@@ -0,0 +1,251 @@
//===----------------------------------------------------------------------===//
//
// This source file is part of the Swift Atomics open source project
//
// Copyright (c) 2020 Apple Inc. and the Swift project authors
// Licensed under Apache License v2.0 with Runtime Library Exception
//
// See https://swift.org/LICENSE.txt for license information
// See https://swift.org/CONTRIBUTORS.txt for the list of Swift project authors
//
//===----------------------------------------------------------------------===//
// #############################################################################
// # #
// # DO NOT EDIT THIS FILE; IT IS AUTOGENERATED. #
// # #
// #############################################################################
/// An unsafe reference type holding a lazily initializable atomic
/// strong reference, requiring manual memory management of the
/// underlying storage representation.
///
/// These values can be set (initialized) exactly once, but read many
/// times.
@frozen
public struct UnsafeAtomicLazyReference<Instance: AnyObject> {
/// The value logically stored in an atomic lazy reference value.
public typealias Value = Instance?
@usableFromInline
internal typealias _Rep = Optional<Unmanaged<Instance>>.AtomicRepresentation
@usableFromInline
internal let _ptr: UnsafeMutablePointer<_Rep>
/// Initialize an unsafe atomic lazy reference that uses the supplied memory
/// location for storage. The storage location must already be initialized to
/// represent a valid atomic value.
///
/// At the end of the lifetime of the atomic value, you must manually ensure
/// that the storage location is correctly `dispose()`d, deinitalized and
/// deallocated.
///
/// Note: This is not an atomic operation.
@_transparent // Debug performance
public init(@_nonEphemeral at pointer: UnsafeMutablePointer<Storage>) {
// `Storage` is layout-compatible with its only stored property.
_ptr = UnsafeMutableRawPointer(pointer).assumingMemoryBound(to: _Rep.self)
}
}
extension UnsafeAtomicLazyReference {
/// The storage representation for an atomic lazy reference value.
@frozen
public struct Storage {
@usableFromInline
internal var _storage: _Rep
/// Initialize a new atomic lazy reference storage value holding `nil`.
///
/// Note: This is not an atomic operation. This call may have side effects
/// (such as unpaired retains of strong references) that will need to be
/// undone by calling `dispose()` before the storage value is
/// deinitialized.
@inlinable @inline(__always)
public init() {
_storage = _Rep(nil)
}
/// Prepare this atomic storage value for deinitialization, extracting the
/// logical value it represents. This invalidates this atomic storage; you
/// must not perform any operations on it after this call (except for
/// deinitialization).
///
/// This call prevents resource leaks when destroying the storage
/// representation of certain `AtomicValue` types. (In particular, ones
/// that model strong references.)
///
/// Note: This is not an atomic operation. Logically, it implements a
/// custom destructor for the underlying non-copiable value.
@inlinable @inline(__always)
@discardableResult
public mutating func dispose() -> Value {
defer { _storage = _Rep(nil) }
return _storage.dispose()?.takeRetainedValue()
}
}
}
extension UnsafeAtomicLazyReference {
/// Create a new `UnsafeAtomicLazyReference` value by dynamically allocating
/// storage for it.
///
/// This call is usually paired with `destroy` to get rid of the allocated
/// storage at the end of its lifetime.
///
/// Note: This is not an atomic operation.
@inlinable
public static func create() -> Self {
let ptr = UnsafeMutablePointer<Storage>.allocate(capacity: 1)
ptr.initialize(to: Storage())
return Self(at: ptr)
}
/// Disposes of the current value of the storage location corresponding to
/// this unsafe atomic lazy reference, then deinitializes and deallocates the
/// storage.
///
/// Note: This is not an atomic operation.
///
/// - Returns: The last value stored in the storage representation before it
/// was destroyed.
@discardableResult
@inlinable
public func destroy() -> Value {
// `Storage` is layout-compatible with its only stored property.
let address = UnsafeMutableRawPointer(_ptr)
.assumingMemoryBound(to: Storage.self)
defer { address.deallocate() }
return address.pointee.dispose()
}
}
/// A reference type holding a lazily initializable atomic
/// strong reference, with automatic memory management.
///
/// These values can be set (initialized) exactly once, but read many
/// times.
public class ManagedAtomicLazyReference<Instance: AnyObject> {
/// The value logically stored in an atomic lazy reference value.
public typealias Value = Instance?
@usableFromInline
internal typealias _Rep = Optional<Unmanaged<Instance>>.AtomicRepresentation
@usableFromInline
internal let _storage: _Rep
/// Initializes a new managed atomic lazy reference with a nil value.
@inlinable
public init() {
_storage = _Rep(nil)
}
@_alwaysEmitIntoClient @inline(__always)
internal var _ptr: UnsafeMutablePointer<_Rep> {
_getUnsafePointerToStoredProperties(self).assumingMemoryBound(to: _Rep.self)
}
}
extension UnsafeAtomicLazyReference {
/// Atomically initializes this reference if its current value is nil, then
/// returns the initialized value. If this reference is already initialized,
/// then `storeIfNilThenLoad(_:)` discards its supplied argument and returns
/// the current value without updating it.
///
/// The following example demonstrates how this can be used to implement a
/// thread-safe lazily initialized reference:
///
/// ```
/// class Image {
/// var _histogram: UnsafeAtomicLazyReference<Histogram> = ...
///
/// // This is safe to call concurrently from multiple threads.
/// var atomicLazyHistogram: Histogram {
/// if let histogram = _histogram.load() { return foo }
/// // Note that code here may run concurrently on
/// // multiple threads, but only one of them will get to
/// // succeed setting the reference.
/// let histogram = ...
/// return _histogram.storeIfNilThenLoad(foo)
/// }
/// ```
///
/// This operation uses acquiring-and-releasing memory ordering.
public func storeIfNilThenLoad(_ desired: __owned Instance) -> Instance {
let desiredUnmanaged = Unmanaged.passRetained(desired)
let (exchanged, current) = _Rep.atomicCompareExchange(
expected: nil,
desired: desiredUnmanaged,
at: _ptr,
ordering: .acquiringAndReleasing)
if !exchanged {
// The reference has already been initialized. Balance the retain that
// we performed on `desired`.
desiredUnmanaged.release()
return current!.takeUnretainedValue()
}
return desiredUnmanaged.takeUnretainedValue()
}
/// Atomically loads and returns the current value of this reference.
///
/// The load operation is performed with the memory ordering
/// `AtomicLoadOrdering.acquiring`.
public func load() -> Instance? {
let value = _Rep.atomicLoad(at: _ptr, ordering: .acquiring)
return value?.takeUnretainedValue()
}
}
extension ManagedAtomicLazyReference {
/// Atomically initializes this reference if its current value is nil, then
/// returns the initialized value. If this reference is already initialized,
/// then `storeIfNilThenLoad(_:)` discards its supplied argument and returns
/// the current value without updating it.
///
/// The following example demonstrates how this can be used to implement a
/// thread-safe lazily initialized reference:
///
/// ```
/// class Image {
/// var _histogram: UnsafeAtomicLazyReference<Histogram> = ...
///
/// // This is safe to call concurrently from multiple threads.
/// var atomicLazyHistogram: Histogram {
/// if let histogram = _histogram.load() { return foo }
/// // Note that code here may run concurrently on
/// // multiple threads, but only one of them will get to
/// // succeed setting the reference.
/// let histogram = ...
/// return _histogram.storeIfNilThenLoad(foo)
/// }
/// ```
///
/// This operation uses acquiring-and-releasing memory ordering.
public func storeIfNilThenLoad(_ desired: __owned Instance) -> Instance {
let desiredUnmanaged = Unmanaged.passRetained(desired)
let (exchanged, current) = _Rep.atomicCompareExchange(
expected: nil,
desired: desiredUnmanaged,
at: _ptr,
ordering: .acquiringAndReleasing)
if !exchanged {
// The reference has already been initialized. Balance the retain that
// we performed on `desired`.
desiredUnmanaged.release()
return current!.takeUnretainedValue()
}
return desiredUnmanaged.takeUnretainedValue()
}
/// Atomically loads and returns the current value of this reference.
///
/// The load operation is performed with the memory ordering
/// `AtomicLoadOrdering.acquiring`.
public func load() -> Instance? {
let value = _Rep.atomicLoad(at: _ptr, ordering: .acquiring)
return value?.takeUnretainedValue()
}
}
@@ -0,0 +1,911 @@
//===----------------------------------------------------------------------===//
//
// This source file is part of the Swift Atomics open source project
//
// Copyright (c) 2020 Apple Inc. and the Swift project authors
// Licensed under Apache License v2.0 with Runtime Library Exception
//
// See https://swift.org/LICENSE.txt for license information
// See https://swift.org/CONTRIBUTORS.txt for the list of Swift project authors
//
//===----------------------------------------------------------------------===//
// #############################################################################
// # #
// # DO NOT EDIT THIS FILE; IT IS AUTOGENERATED. #
// # #
// #############################################################################
/// An unsafe reference type holding an atomic value, requiring manual memory
/// management of the underlying storage representation.
@frozen
public struct UnsafeAtomic<Value: AtomicValue> {
public typealias Storage = Value.AtomicRepresentation
@usableFromInline
internal let _ptr: UnsafeMutablePointer<Storage>
/// Initialize an unsafe atomic value that uses the supplied memory location
/// for storage. The storage location must already be initialized to
/// represent a valid atomic value.
///
/// At the end of the lifetime of the atomic value, you must manually ensure
/// that the storage location is correctly `dispose()`d, deinitalized and
/// deallocated.
///
/// Note: This is not an atomic operation.
@_transparent // Debug performance
public init(
@_nonEphemeral at pointer: UnsafeMutablePointer<Storage>
) {
self._ptr = pointer
}
/// Create a new `UnsafeAtomic` value with the supplied initial value by
/// dynamically allocating storage for it.
///
/// This call is usually paired with `destroy` to get rid of the allocated
/// storage at the end of its lifetime.
///
/// Note: This is not an atomic operation.
@inlinable
public static func create(_ initialValue: __owned Value) -> Self {
let ptr = UnsafeMutablePointer<Storage>.allocate(capacity: 1)
ptr.initialize(to: Storage(initialValue))
return Self(at: ptr)
}
/// Disposes of the current value of the storage location corresponding to
/// this unsafe atomic value, then deinitializes and deallocates the storage.
///
/// Note: This is not an atomic operation.
///
/// - Returns: The last value stored in the storage representation before it
/// was destroyed.
@discardableResult
@inlinable
public func destroy() -> Value {
let result = _ptr.pointee.dispose()
_ptr.deinitialize(count: 1)
_ptr.deallocate()
return result
}
}
/// A reference type holding an atomic value, with automatic memory management.
public class ManagedAtomic<Value: AtomicValue> {
@usableFromInline
internal var _storage: Value.AtomicRepresentation
/// Initialize a new managed atomic instance holding the specified initial
/// value.
@inline(__always) @_alwaysEmitIntoClient
public init(_ value: Value) {
_storage = Value.AtomicRepresentation(value)
}
deinit {
_ = _storage.dispose()
}
@_alwaysEmitIntoClient @inline(__always)
internal var _ptr: UnsafeMutablePointer<Value.AtomicRepresentation> {
_getUnsafePointerToStoredProperties(self)
.assumingMemoryBound(to: Value.AtomicRepresentation.self)
}
}
extension UnsafeAtomic {
/// Atomically loads and returns the current value, applying the specified
/// memory ordering.
///
/// - Parameter ordering: The memory ordering to apply on this operation.
/// - Returns: The current value.
@_semantics("atomics.requires_constant_orderings")
@_transparent @_alwaysEmitIntoClient
public func load(
ordering: AtomicLoadOrdering
) -> Value {
Value.AtomicRepresentation.atomicLoad(at: _ptr, ordering: ordering)
}
/// Atomically sets the current value to `desired`, applying the specified
/// memory ordering.
///
/// - Parameter desired: The desired new value.
/// - Parameter ordering: The memory ordering to apply on this operation.
@_semantics("atomics.requires_constant_orderings")
@_transparent @_alwaysEmitIntoClient
public func store(
_ desired: __owned Value,
ordering: AtomicStoreOrdering
) {
Value.AtomicRepresentation.atomicStore(desired, at: _ptr, ordering: ordering)
}
/// Atomically sets the current value to `desired` and returns the original
/// value, applying the specified memory ordering.
///
/// - Parameter desired: The desired new value.
/// - Parameter ordering: The memory ordering to apply on this operation.
/// - Returns: The original value.
@_semantics("atomics.requires_constant_orderings")
@_transparent @_alwaysEmitIntoClient
public func exchange(
_ desired: __owned Value,
ordering: AtomicUpdateOrdering
) -> Value {
Value.AtomicRepresentation.atomicExchange(desired, at: _ptr, ordering: ordering)
}
/// Perform an atomic compare and exchange operation on the current value,
/// applying the specified memory ordering.
///
/// This operation performs the following algorithm as a single atomic
/// transaction:
///
/// ```
/// atomic(self) { currentValue in
/// let original = currentValue
/// guard original == expected else { return (false, original) }
/// currentValue = desired
/// return (true, original)
/// }
/// ```
///
/// This method implements a "strong" compare and exchange operation
/// that does not permit spurious failures.
///
/// - Parameter expected: The expected current value.
/// - Parameter desired: The desired new value.
/// - Parameter ordering: The memory ordering to apply on this operation.
/// - Returns: A tuple `(exchanged, original)`, where `exchanged` is true if
/// the exchange was successful, and `original` is the original value.
@_semantics("atomics.requires_constant_orderings")
@_transparent @_alwaysEmitIntoClient
public func compareExchange(
expected: Value,
desired: __owned Value,
ordering: AtomicUpdateOrdering
) -> (exchanged: Bool, original: Value) {
Value.AtomicRepresentation.atomicCompareExchange(
expected: expected,
desired: desired,
at: _ptr,
ordering: ordering)
}
/// Perform an atomic compare and exchange operation on the current value,
/// applying the specified success/failure memory orderings.
///
/// This operation performs the following algorithm as a single atomic
/// transaction:
///
/// ```
/// atomic(self) { currentValue in
/// let original = currentValue
/// guard original == expected else { return (false, original) }
/// currentValue = desired
/// return (true, original)
/// }
/// ```
///
/// The `succesOrdering` argument specifies the memory ordering to use when
/// the operation manages to update the current value, while `failureOrdering`
/// will be used when the operation leaves the value intact.
///
/// This method implements a "strong" compare and exchange operation
/// that does not permit spurious failures.
///
/// - Parameter expected: The expected current value.
/// - Parameter desired: The desired new value.
/// - Parameter successOrdering: The memory ordering to apply if this
/// operation performs the exchange.
/// - Parameter failureOrdering: The memory ordering to apply on this
/// operation does not perform the exchange.
/// - Returns: A tuple `(exchanged, original)`, where `exchanged` is true if
/// the exchange was successful, and `original` is the original value.
@_semantics("atomics.requires_constant_orderings")
@_transparent @_alwaysEmitIntoClient
public func compareExchange(
expected: Value,
desired: __owned Value,
successOrdering: AtomicUpdateOrdering,
failureOrdering: AtomicLoadOrdering
) -> (exchanged: Bool, original: Value) {
Value.AtomicRepresentation.atomicCompareExchange(
expected: expected,
desired: desired,
at: _ptr,
successOrdering: successOrdering,
failureOrdering: failureOrdering)
}
/// Perform an atomic weak compare and exchange operation on the current
/// value, applying the specified success/failure memory orderings. This
/// compare-exchange variant is allowed to spuriously fail; it is designed to
/// be called in a loop until it indicates a successful exchange has happened.
///
/// This operation performs the following algorithm as a single atomic
/// transaction:
///
/// ```
/// atomic(self) { currentValue in
/// let original = currentValue
/// guard original == expected else { return (false, original) }
/// currentValue = desired
/// return (true, original)
/// }
/// ```
///
/// (In this weak form, transient conditions may cause the `original ==
/// expected` check to sometimes return false when the two values are in fact
/// the same.)
///
/// The `ordering` argument specifies the memory ordering to use when the
/// operation manages to update the current value, while `failureOrdering`
/// will be used when the operation leaves the value intact.
///
/// - Parameter expected: The expected current value.
/// - Parameter desired: The desired new value.
/// - Parameter successOrdering: The memory ordering to apply if this
/// operation performs the exchange.
/// - Parameter failureOrdering: The memory ordering to apply on this
/// operation does not perform the exchange.
/// - Returns: A tuple `(exchanged, original)`, where `exchanged` is true if
/// the exchange was successful, and `original` is the original value.
@_semantics("atomics.requires_constant_orderings")
@_transparent @_alwaysEmitIntoClient
public func weakCompareExchange(
expected: Value,
desired: __owned Value,
successOrdering: AtomicUpdateOrdering,
failureOrdering: AtomicLoadOrdering
) -> (exchanged: Bool, original: Value) {
Value.AtomicRepresentation.atomicWeakCompareExchange(
expected: expected,
desired: desired,
at: _ptr,
successOrdering: successOrdering,
failureOrdering: failureOrdering)
}
}
extension UnsafeAtomic where Value: AtomicInteger {
/// Perform an atomic wrapping add operation and return the original value, applying
/// the specified memory ordering.
///
/// Note: This operation silently wraps around on overflow, like the
/// `&+` operator does on `Int` values.
///
/// - Parameter operand: An integer value.
/// - Parameter ordering: The memory ordering to apply on this operation.
/// - Returns: The original value before the operation.
@_semantics("atomics.requires_constant_orderings")
@_transparent @_alwaysEmitIntoClient
public func loadThenWrappingIncrement(
by operand: Value = 1,
ordering: AtomicUpdateOrdering
) -> Value {
Value.AtomicRepresentation.atomicLoadThenWrappingIncrement(
by: operand,
at: _ptr,
ordering: ordering)
}
/// Perform an atomic wrapping subtract operation and return the original value, applying
/// the specified memory ordering.
///
/// Note: This operation silently wraps around on overflow, like the
/// `&-` operator does on `Int` values.
///
/// - Parameter operand: An integer value.
/// - Parameter ordering: The memory ordering to apply on this operation.
/// - Returns: The original value before the operation.
@_semantics("atomics.requires_constant_orderings")
@_transparent @_alwaysEmitIntoClient
public func loadThenWrappingDecrement(
by operand: Value = 1,
ordering: AtomicUpdateOrdering
) -> Value {
Value.AtomicRepresentation.atomicLoadThenWrappingDecrement(
by: operand,
at: _ptr,
ordering: ordering)
}
/// Perform an atomic bitwise AND operation and return the original value, applying
/// the specified memory ordering.
///
/// - Parameter operand: An integer value.
/// - Parameter ordering: The memory ordering to apply on this operation.
/// - Returns: The original value before the operation.
@_semantics("atomics.requires_constant_orderings")
@_transparent @_alwaysEmitIntoClient
public func loadThenBitwiseAnd(
with operand: Value,
ordering: AtomicUpdateOrdering
) -> Value {
Value.AtomicRepresentation.atomicLoadThenBitwiseAnd(
with: operand,
at: _ptr,
ordering: ordering)
}
/// Perform an atomic bitwise OR operation and return the original value, applying
/// the specified memory ordering.
///
/// - Parameter operand: An integer value.
/// - Parameter ordering: The memory ordering to apply on this operation.
/// - Returns: The original value before the operation.
@_semantics("atomics.requires_constant_orderings")
@_transparent @_alwaysEmitIntoClient
public func loadThenBitwiseOr(
with operand: Value,
ordering: AtomicUpdateOrdering
) -> Value {
Value.AtomicRepresentation.atomicLoadThenBitwiseOr(
with: operand,
at: _ptr,
ordering: ordering)
}
/// Perform an atomic bitwise XOR operation and return the original value, applying
/// the specified memory ordering.
///
/// - Parameter operand: An integer value.
/// - Parameter ordering: The memory ordering to apply on this operation.
/// - Returns: The original value before the operation.
@_semantics("atomics.requires_constant_orderings")
@_transparent @_alwaysEmitIntoClient
public func loadThenBitwiseXor(
with operand: Value,
ordering: AtomicUpdateOrdering
) -> Value {
Value.AtomicRepresentation.atomicLoadThenBitwiseXor(
with: operand,
at: _ptr,
ordering: ordering)
}
/// Perform an atomic wrapping add operation and return the new value, applying
/// the specified memory ordering.
///
/// Note: This operation silently wraps around on overflow, like the
/// `&+` operator does on `Int` values.
///
/// - Parameter operand: An integer value.
/// - Parameter ordering: The memory ordering to apply on this operation.
/// - Returns: The new value after the operation.
@_semantics("atomics.requires_constant_orderings")
@_transparent @_alwaysEmitIntoClient
public func wrappingIncrementThenLoad(
by operand: Value = 1,
ordering: AtomicUpdateOrdering
) -> Value {
let original = Value.AtomicRepresentation.atomicLoadThenWrappingIncrement(
by: operand,
at: _ptr,
ordering: ordering)
return original &+ operand
}
/// Perform an atomic wrapping subtract operation and return the new value, applying
/// the specified memory ordering.
///
/// Note: This operation silently wraps around on overflow, like the
/// `&-` operator does on `Int` values.
///
/// - Parameter operand: An integer value.
/// - Parameter ordering: The memory ordering to apply on this operation.
/// - Returns: The new value after the operation.
@_semantics("atomics.requires_constant_orderings")
@_transparent @_alwaysEmitIntoClient
public func wrappingDecrementThenLoad(
by operand: Value = 1,
ordering: AtomicUpdateOrdering
) -> Value {
let original = Value.AtomicRepresentation.atomicLoadThenWrappingDecrement(
by: operand,
at: _ptr,
ordering: ordering)
return original &- operand
}
/// Perform an atomic bitwise AND operation and return the new value, applying
/// the specified memory ordering.
///
/// - Parameter operand: An integer value.
/// - Parameter ordering: The memory ordering to apply on this operation.
/// - Returns: The new value after the operation.
@_semantics("atomics.requires_constant_orderings")
@_transparent @_alwaysEmitIntoClient
public func bitwiseAndThenLoad(
with operand: Value,
ordering: AtomicUpdateOrdering
) -> Value {
let original = Value.AtomicRepresentation.atomicLoadThenBitwiseAnd(
with: operand,
at: _ptr,
ordering: ordering)
return original & operand
}
/// Perform an atomic bitwise OR operation and return the new value, applying
/// the specified memory ordering.
///
/// - Parameter operand: An integer value.
/// - Parameter ordering: The memory ordering to apply on this operation.
/// - Returns: The new value after the operation.
@_semantics("atomics.requires_constant_orderings")
@_transparent @_alwaysEmitIntoClient
public func bitwiseOrThenLoad(
with operand: Value,
ordering: AtomicUpdateOrdering
) -> Value {
let original = Value.AtomicRepresentation.atomicLoadThenBitwiseOr(
with: operand,
at: _ptr,
ordering: ordering)
return original | operand
}
/// Perform an atomic bitwise XOR operation and return the new value, applying
/// the specified memory ordering.
///
/// - Parameter operand: An integer value.
/// - Parameter ordering: The memory ordering to apply on this operation.
/// - Returns: The new value after the operation.
@_semantics("atomics.requires_constant_orderings")
@_transparent @_alwaysEmitIntoClient
public func bitwiseXorThenLoad(
with operand: Value,
ordering: AtomicUpdateOrdering
) -> Value {
let original = Value.AtomicRepresentation.atomicLoadThenBitwiseXor(
with: operand,
at: _ptr,
ordering: ordering)
return original ^ operand
}
/// Perform an atomic wrapping increment operation applying the
/// specified memory ordering.
///
/// Note: This operation silently wraps around on overflow, like the
/// `&+=` operator does on `Int` values.
///
/// - Parameter operand: The value to add to the current value.
/// - Parameter ordering: The memory ordering to apply on this operation.
@_semantics("atomics.requires_constant_orderings")
@_transparent @_alwaysEmitIntoClient
public func wrappingIncrement(
by operand: Value = 1,
ordering: AtomicUpdateOrdering
) {
_ = Value.AtomicRepresentation.atomicLoadThenWrappingIncrement(
by: operand,
at: _ptr,
ordering: ordering)
}
/// Perform an atomic wrapping decrement operation applying the
/// specified memory ordering.
///
/// Note: This operation silently wraps around on overflow, like the
/// `&-=` operator does on `Int` values.
///
/// - Parameter operand: The value to subtract from the current value.
/// - Parameter ordering: The memory ordering to apply on this operation.
@_semantics("atomics.requires_constant_orderings")
@_transparent @_alwaysEmitIntoClient
public func wrappingDecrement(
by operand: Value = 1,
ordering: AtomicUpdateOrdering
) {
_ = Value.AtomicRepresentation.atomicLoadThenWrappingDecrement(
by: operand,
at: _ptr,
ordering: ordering)
}
}
extension ManagedAtomic {
/// Atomically loads and returns the current value, applying the specified
/// memory ordering.
///
/// - Parameter ordering: The memory ordering to apply on this operation.
/// - Returns: The current value.
@_semantics("atomics.requires_constant_orderings")
@_transparent @_alwaysEmitIntoClient
public func load(
ordering: AtomicLoadOrdering
) -> Value {
Value.AtomicRepresentation.atomicLoad(at: _ptr, ordering: ordering)
}
/// Atomically sets the current value to `desired`, applying the specified
/// memory ordering.
///
/// - Parameter desired: The desired new value.
/// - Parameter ordering: The memory ordering to apply on this operation.
@_semantics("atomics.requires_constant_orderings")
@_transparent @_alwaysEmitIntoClient
public func store(
_ desired: __owned Value,
ordering: AtomicStoreOrdering
) {
Value.AtomicRepresentation.atomicStore(desired, at: _ptr, ordering: ordering)
}
/// Atomically sets the current value to `desired` and returns the original
/// value, applying the specified memory ordering.
///
/// - Parameter desired: The desired new value.
/// - Parameter ordering: The memory ordering to apply on this operation.
/// - Returns: The original value.
@_semantics("atomics.requires_constant_orderings")
@_transparent @_alwaysEmitIntoClient
public func exchange(
_ desired: __owned Value,
ordering: AtomicUpdateOrdering
) -> Value {
Value.AtomicRepresentation.atomicExchange(desired, at: _ptr, ordering: ordering)
}
/// Perform an atomic compare and exchange operation on the current value,
/// applying the specified memory ordering.
///
/// This operation performs the following algorithm as a single atomic
/// transaction:
///
/// ```
/// atomic(self) { currentValue in
/// let original = currentValue
/// guard original == expected else { return (false, original) }
/// currentValue = desired
/// return (true, original)
/// }
/// ```
///
/// This method implements a "strong" compare and exchange operation
/// that does not permit spurious failures.
///
/// - Parameter expected: The expected current value.
/// - Parameter desired: The desired new value.
/// - Parameter ordering: The memory ordering to apply on this operation.
/// - Returns: A tuple `(exchanged, original)`, where `exchanged` is true if
/// the exchange was successful, and `original` is the original value.
@_semantics("atomics.requires_constant_orderings")
@_transparent @_alwaysEmitIntoClient
public func compareExchange(
expected: Value,
desired: __owned Value,
ordering: AtomicUpdateOrdering
) -> (exchanged: Bool, original: Value) {
Value.AtomicRepresentation.atomicCompareExchange(
expected: expected,
desired: desired,
at: _ptr,
ordering: ordering)
}
/// Perform an atomic compare and exchange operation on the current value,
/// applying the specified success/failure memory orderings.
///
/// This operation performs the following algorithm as a single atomic
/// transaction:
///
/// ```
/// atomic(self) { currentValue in
/// let original = currentValue
/// guard original == expected else { return (false, original) }
/// currentValue = desired
/// return (true, original)
/// }
/// ```
///
/// The `succesOrdering` argument specifies the memory ordering to use when
/// the operation manages to update the current value, while `failureOrdering`
/// will be used when the operation leaves the value intact.
///
/// This method implements a "strong" compare and exchange operation
/// that does not permit spurious failures.
///
/// - Parameter expected: The expected current value.
/// - Parameter desired: The desired new value.
/// - Parameter successOrdering: The memory ordering to apply if this
/// operation performs the exchange.
/// - Parameter failureOrdering: The memory ordering to apply on this
/// operation does not perform the exchange.
/// - Returns: A tuple `(exchanged, original)`, where `exchanged` is true if
/// the exchange was successful, and `original` is the original value.
@_semantics("atomics.requires_constant_orderings")
@_transparent @_alwaysEmitIntoClient
public func compareExchange(
expected: Value,
desired: __owned Value,
successOrdering: AtomicUpdateOrdering,
failureOrdering: AtomicLoadOrdering
) -> (exchanged: Bool, original: Value) {
Value.AtomicRepresentation.atomicCompareExchange(
expected: expected,
desired: desired,
at: _ptr,
successOrdering: successOrdering,
failureOrdering: failureOrdering)
}
/// Perform an atomic weak compare and exchange operation on the current
/// value, applying the specified success/failure memory orderings. This
/// compare-exchange variant is allowed to spuriously fail; it is designed to
/// be called in a loop until it indicates a successful exchange has happened.
///
/// This operation performs the following algorithm as a single atomic
/// transaction:
///
/// ```
/// atomic(self) { currentValue in
/// let original = currentValue
/// guard original == expected else { return (false, original) }
/// currentValue = desired
/// return (true, original)
/// }
/// ```
///
/// (In this weak form, transient conditions may cause the `original ==
/// expected` check to sometimes return false when the two values are in fact
/// the same.)
///
/// The `ordering` argument specifies the memory ordering to use when the
/// operation manages to update the current value, while `failureOrdering`
/// will be used when the operation leaves the value intact.
///
/// - Parameter expected: The expected current value.
/// - Parameter desired: The desired new value.
/// - Parameter successOrdering: The memory ordering to apply if this
/// operation performs the exchange.
/// - Parameter failureOrdering: The memory ordering to apply on this
/// operation does not perform the exchange.
/// - Returns: A tuple `(exchanged, original)`, where `exchanged` is true if
/// the exchange was successful, and `original` is the original value.
@_semantics("atomics.requires_constant_orderings")
@_transparent @_alwaysEmitIntoClient
public func weakCompareExchange(
expected: Value,
desired: __owned Value,
successOrdering: AtomicUpdateOrdering,
failureOrdering: AtomicLoadOrdering
) -> (exchanged: Bool, original: Value) {
Value.AtomicRepresentation.atomicWeakCompareExchange(
expected: expected,
desired: desired,
at: _ptr,
successOrdering: successOrdering,
failureOrdering: failureOrdering)
}
}
extension ManagedAtomic where Value: AtomicInteger {
/// Perform an atomic wrapping add operation and return the original value, applying
/// the specified memory ordering.
///
/// Note: This operation silently wraps around on overflow, like the
/// `&+` operator does on `Int` values.
///
/// - Parameter operand: An integer value.
/// - Parameter ordering: The memory ordering to apply on this operation.
/// - Returns: The original value before the operation.
@_semantics("atomics.requires_constant_orderings")
@_transparent @_alwaysEmitIntoClient
public func loadThenWrappingIncrement(
by operand: Value = 1,
ordering: AtomicUpdateOrdering
) -> Value {
Value.AtomicRepresentation.atomicLoadThenWrappingIncrement(
by: operand,
at: _ptr,
ordering: ordering)
}
/// Perform an atomic wrapping subtract operation and return the original value, applying
/// the specified memory ordering.
///
/// Note: This operation silently wraps around on overflow, like the
/// `&-` operator does on `Int` values.
///
/// - Parameter operand: An integer value.
/// - Parameter ordering: The memory ordering to apply on this operation.
/// - Returns: The original value before the operation.
@_semantics("atomics.requires_constant_orderings")
@_transparent @_alwaysEmitIntoClient
public func loadThenWrappingDecrement(
by operand: Value = 1,
ordering: AtomicUpdateOrdering
) -> Value {
Value.AtomicRepresentation.atomicLoadThenWrappingDecrement(
by: operand,
at: _ptr,
ordering: ordering)
}
/// Perform an atomic bitwise AND operation and return the original value, applying
/// the specified memory ordering.
///
/// - Parameter operand: An integer value.
/// - Parameter ordering: The memory ordering to apply on this operation.
/// - Returns: The original value before the operation.
@_semantics("atomics.requires_constant_orderings")
@_transparent @_alwaysEmitIntoClient
public func loadThenBitwiseAnd(
with operand: Value,
ordering: AtomicUpdateOrdering
) -> Value {
Value.AtomicRepresentation.atomicLoadThenBitwiseAnd(
with: operand,
at: _ptr,
ordering: ordering)
}
/// Perform an atomic bitwise OR operation and return the original value, applying
/// the specified memory ordering.
///
/// - Parameter operand: An integer value.
/// - Parameter ordering: The memory ordering to apply on this operation.
/// - Returns: The original value before the operation.
@_semantics("atomics.requires_constant_orderings")
@_transparent @_alwaysEmitIntoClient
public func loadThenBitwiseOr(
with operand: Value,
ordering: AtomicUpdateOrdering
) -> Value {
Value.AtomicRepresentation.atomicLoadThenBitwiseOr(
with: operand,
at: _ptr,
ordering: ordering)
}
/// Perform an atomic bitwise XOR operation and return the original value, applying
/// the specified memory ordering.
///
/// - Parameter operand: An integer value.
/// - Parameter ordering: The memory ordering to apply on this operation.
/// - Returns: The original value before the operation.
@_semantics("atomics.requires_constant_orderings")
@_transparent @_alwaysEmitIntoClient
public func loadThenBitwiseXor(
with operand: Value,
ordering: AtomicUpdateOrdering
) -> Value {
Value.AtomicRepresentation.atomicLoadThenBitwiseXor(
with: operand,
at: _ptr,
ordering: ordering)
}
/// Perform an atomic wrapping add operation and return the new value, applying
/// the specified memory ordering.
///
/// Note: This operation silently wraps around on overflow, like the
/// `&+` operator does on `Int` values.
///
/// - Parameter operand: An integer value.
/// - Parameter ordering: The memory ordering to apply on this operation.
/// - Returns: The new value after the operation.
@_semantics("atomics.requires_constant_orderings")
@_transparent @_alwaysEmitIntoClient
public func wrappingIncrementThenLoad(
by operand: Value = 1,
ordering: AtomicUpdateOrdering
) -> Value {
let original = Value.AtomicRepresentation.atomicLoadThenWrappingIncrement(
by: operand,
at: _ptr,
ordering: ordering)
return original &+ operand
}
/// Perform an atomic wrapping subtract operation and return the new value, applying
/// the specified memory ordering.
///
/// Note: This operation silently wraps around on overflow, like the
/// `&-` operator does on `Int` values.
///
/// - Parameter operand: An integer value.
/// - Parameter ordering: The memory ordering to apply on this operation.
/// - Returns: The new value after the operation.
@_semantics("atomics.requires_constant_orderings")
@_transparent @_alwaysEmitIntoClient
public func wrappingDecrementThenLoad(
by operand: Value = 1,
ordering: AtomicUpdateOrdering
) -> Value {
let original = Value.AtomicRepresentation.atomicLoadThenWrappingDecrement(
by: operand,
at: _ptr,
ordering: ordering)
return original &- operand
}
/// Perform an atomic bitwise AND operation and return the new value, applying
/// the specified memory ordering.
///
/// - Parameter operand: An integer value.
/// - Parameter ordering: The memory ordering to apply on this operation.
/// - Returns: The new value after the operation.
@_semantics("atomics.requires_constant_orderings")
@_transparent @_alwaysEmitIntoClient
public func bitwiseAndThenLoad(
with operand: Value,
ordering: AtomicUpdateOrdering
) -> Value {
let original = Value.AtomicRepresentation.atomicLoadThenBitwiseAnd(
with: operand,
at: _ptr,
ordering: ordering)
return original & operand
}
/// Perform an atomic bitwise OR operation and return the new value, applying
/// the specified memory ordering.
///
/// - Parameter operand: An integer value.
/// - Parameter ordering: The memory ordering to apply on this operation.
/// - Returns: The new value after the operation.
@_semantics("atomics.requires_constant_orderings")
@_transparent @_alwaysEmitIntoClient
public func bitwiseOrThenLoad(
with operand: Value,
ordering: AtomicUpdateOrdering
) -> Value {
let original = Value.AtomicRepresentation.atomicLoadThenBitwiseOr(
with: operand,
at: _ptr,
ordering: ordering)
return original | operand
}
/// Perform an atomic bitwise XOR operation and return the new value, applying
/// the specified memory ordering.
///
/// - Parameter operand: An integer value.
/// - Parameter ordering: The memory ordering to apply on this operation.
/// - Returns: The new value after the operation.
@_semantics("atomics.requires_constant_orderings")
@_transparent @_alwaysEmitIntoClient
public func bitwiseXorThenLoad(
with operand: Value,
ordering: AtomicUpdateOrdering
) -> Value {
let original = Value.AtomicRepresentation.atomicLoadThenBitwiseXor(
with: operand,
at: _ptr,
ordering: ordering)
return original ^ operand
}
/// Perform an atomic wrapping increment operation applying the
/// specified memory ordering.
///
/// Note: This operation silently wraps around on overflow, like the
/// `&+=` operator does on `Int` values.
///
/// - Parameter operand: The value to add to the current value.
/// - Parameter ordering: The memory ordering to apply on this operation.
@_semantics("atomics.requires_constant_orderings")
@_transparent @_alwaysEmitIntoClient
public func wrappingIncrement(
by operand: Value = 1,
ordering: AtomicUpdateOrdering
) {
_ = Value.AtomicRepresentation.atomicLoadThenWrappingIncrement(
by: operand,
at: _ptr,
ordering: ordering)
}
/// Perform an atomic wrapping decrement operation applying the
/// specified memory ordering.
///
/// Note: This operation silently wraps around on overflow, like the
/// `&-=` operator does on `Int` values.
///
/// - Parameter operand: The value to subtract from the current value.
/// - Parameter ordering: The memory ordering to apply on this operation.
@_semantics("atomics.requires_constant_orderings")
@_transparent @_alwaysEmitIntoClient
public func wrappingDecrement(
by operand: Value = 1,
ordering: AtomicUpdateOrdering
) {
_ = Value.AtomicRepresentation.atomicLoadThenWrappingDecrement(
by: operand,
at: _ptr,
ordering: ordering)
}
}
File diff suppressed because it is too large. Load diff
File diff suppressed because it is too large. Load diff
@@ -0,0 +1,366 @@
//===----------------------------------------------------------------------===//
//
// This source file is part of the Swift Atomics open source project
//
// Copyright (c) 2020 Apple Inc. and the Swift project authors
// Licensed under Apache License v2.0 with Runtime Library Exception
//
// See https://swift.org/LICENSE.txt for license information
// See https://swift.org/CONTRIBUTORS.txt for the list of Swift project authors
//
//===----------------------------------------------------------------------===//
#ifndef SWIFTATOMIC_HEADER_INCLUDED
#define SWIFTATOMIC_HEADER_INCLUDED 1
// Swift-importable shims for C atomics.
//
// Swift cannot import C's atomic types or any operations over them, so we need
// to meticulously wrap all of them in tiny importable types and functions.
//
// This file defines an atomic storage representation and 56 atomic operations
// for each of the 10 standard integer types in the Standard Library, as well as
// Bool and a double-word integer type. To prevent us from having to manually
// write/maintain a thousand or so functions, we use the C preprocessor to stamp
// these out.
//
// Supporting double-wide integers is tricky, because neither Swift nor C has
// standard integer representations for these on platforms where `Int` has a bit
// width of 64. Standard C can model 128-bit atomics through `_Atomic(struct
// pair)` where `pair` is a struct of two `intptr_t`s, but current Swift
// compilers (as of version 5.3) get confused by atomic structs
// (https://reviews.llvm.org/D86218). To work around that, we need to use the
// nonstandard `__uint128_t` type. The Swift compiler does seem to be able to
// deal with `_Atomic(__uint128_t)`, but it refuses to directly import
// `__uint128_t`, so that too needs to be wrapped in a dummy struct (which we
// call `_sa_dword`). We want to stamp out 128-bit atomics from the same code as
// regular atomics, so this requires all atomic operations to distinguish
// between the "exported" C type and the corresponding "internal"
// representation, and to explicitly convert between them as needed. This is
// done using the `SWIFTATOMIC_ENCODE_<swiftType>` and
// `SWIFTATOMIC_DECODE_<swiftType>` family of macros.
//
// In the case of compare-and-exchange, the conversion of the `expected`
// argument needs to go through a temporary stack variable that would result in
// slightly worse codegen for the regular single-word case, so we distinguish
// between `SIMPLE` and `COMPLEX` cmpxchg variants. The single variant assumes
// that the internal representation is identical to the exported type, and does
// away with the comparisons.
//
// FIXME: Upgrading from the preprocessor to gyb would perhaps make things more
// readable here.
//
// FIXME: Eliminate the encoding/decoding mechanism once the package requires a
// compiler that includes https://reviews.llvm.org/D86218.
#include <stdbool.h>
#include <stdint.h>
#include <stdatomic.h>
#include <assert.h>
#define SWIFTATOMIC_INLINE static inline __attribute__((__always_inline__))
#define SWIFTATOMIC_SWIFT_NAME(name) __attribute__((swift_name(#name)))
// Atomic fences
#define SWIFTATOMIC_THREAD_FENCE_FN(order) \
SWIFTATOMIC_INLINE void _sa_thread_fence_##order(void) \
{ \
atomic_thread_fence(memory_order_##order); \
}
SWIFTATOMIC_THREAD_FENCE_FN(acquire)
SWIFTATOMIC_THREAD_FENCE_FN(release)
SWIFTATOMIC_THREAD_FENCE_FN(acq_rel)
SWIFTATOMIC_THREAD_FENCE_FN(seq_cst)
// Definition of an atomic storage type.
#define SWIFTATOMIC_STORAGE_TYPE(swiftType, cType, storageType) \
typedef struct { \
_Atomic(storageType) value; \
} _sa_##swiftType \
SWIFTATOMIC_SWIFT_NAME(_Atomic##swiftType##Storage);
// Storage value initializer
#define SWIFTATOMIC_PREPARE_FN(swiftType, cType, storageType) \
SWIFTATOMIC_INLINE \
_sa_##swiftType _sa_prepare_##swiftType(cType value) \
{ \
_sa_##swiftType storage = { SWIFTATOMIC_ENCODE_##swiftType(value) }; \
assert(atomic_is_lock_free(&storage.value)); \
return storage; \
}
// Storage value disposal function
#define SWIFTATOMIC_DISPOSE_FN(swiftType, cType, storageType) \
SWIFTATOMIC_INLINE \
cType _sa_dispose_##swiftType(_sa_##swiftType storage) \
{ \
return SWIFTATOMIC_DECODE_##swiftType(storage.value); \
}
// Atomic load
#define SWIFTATOMIC_LOAD_FN(swiftType, cType, storageType, order) \
SWIFTATOMIC_INLINE \
cType _sa_load_##order##_##swiftType( \
_sa_##swiftType *ptr) \
{ \
return SWIFTATOMIC_DECODE_##swiftType( \
atomic_load_explicit(&ptr->value, \
memory_order_##order)); \
}
// Atomic store
#define SWIFTATOMIC_STORE_FN(swiftType, cType, storageType, order) \
SWIFTATOMIC_INLINE \
void _sa_store_##order##_##swiftType( \
_sa_##swiftType *ptr, \
cType desired) \
{ \
atomic_store_explicit(&ptr->value, \
SWIFTATOMIC_ENCODE_##swiftType(desired), \
memory_order_##order); \
}
// Atomic exchange
#define SWIFTATOMIC_EXCHANGE_FN(swiftType, cType, storageType, order) \
SWIFTATOMIC_INLINE \
cType _sa_exchange_##order##_##swiftType( \
_sa_##swiftType *ptr, \
cType desired) \
{ \
return SWIFTATOMIC_DECODE_##swiftType( \
atomic_exchange_explicit( \
&ptr->value, \
SWIFTATOMIC_ENCODE_##swiftType(desired), \
memory_order_##order)); \
}
// Atomic compare/exchange
#define SWIFTATOMIC_CMPXCHG_FN_SIMPLE(_kind, swiftType, cType, storageType, succ, fail) \
SWIFTATOMIC_INLINE \
bool \
_sa_cmpxchg_##_kind##_##succ##_##fail##_##swiftType( \
_sa_##swiftType *ptr, \
cType *expected, \
cType desired) \
{ \
return atomic_compare_exchange_##_kind##_explicit( \
&ptr->value, \
expected, \
desired, \
memory_order_##succ, \
memory_order_##fail); \
}
#define SWIFTATOMIC_CMPXCHG_FN_COMPLEX(_kind, swiftType, cType, storageType, succ, fail) \
SWIFTATOMIC_INLINE \
bool \
_sa_cmpxchg_##_kind##_##succ##_##fail##_##swiftType( \
_sa_##swiftType *ptr, \
cType *expected, \
cType desired) \
{ \
storageType _expected = SWIFTATOMIC_ENCODE_##swiftType(*expected); \
bool result = atomic_compare_exchange_##_kind##_explicit( \
&ptr->value, \
&_expected, \
SWIFTATOMIC_ENCODE_##swiftType(desired), \
memory_order_##succ, \
memory_order_##fail); \
*expected = SWIFTATOMIC_DECODE_##swiftType(_expected); \
return result; \
}
// Atomic integer operations
#define SWIFTATOMIC_INTEGER_FN(op, swiftType, cType, storageType, order) \
SWIFTATOMIC_INLINE \
cType _sa_fetch_##op##_##order##_##swiftType( \
_sa_##swiftType *ptr, \
cType operand) \
{ \
return SWIFTATOMIC_DECODE_##swiftType( \
atomic_fetch_##op##_explicit( \
&ptr->value, \
SWIFTATOMIC_ENCODE_##swiftType(operand), \
memory_order_##order)); \
}
// Functions for each supported operation + memory ordering combination
#define SWIFTATOMIC_STORE_FNS(swiftType, cType, storageType) \
SWIFTATOMIC_STORE_FN(swiftType, cType, storageType, relaxed) \
SWIFTATOMIC_STORE_FN(swiftType, cType, storageType, release) \
SWIFTATOMIC_STORE_FN(swiftType, cType, storageType, seq_cst)
#define SWIFTATOMIC_LOAD_FNS(swiftType, cType, storageType) \
SWIFTATOMIC_LOAD_FN(swiftType, cType, storageType, relaxed) \
SWIFTATOMIC_LOAD_FN(swiftType, cType, storageType, acquire) \
SWIFTATOMIC_LOAD_FN(swiftType, cType, storageType, seq_cst)
#define SWIFTATOMIC_EXCHANGE_FNS(swiftType, cType, storageType) \
SWIFTATOMIC_EXCHANGE_FN(swiftType, cType, storageType, relaxed) \
SWIFTATOMIC_EXCHANGE_FN(swiftType, cType, storageType, acquire) \
SWIFTATOMIC_EXCHANGE_FN(swiftType, cType, storageType, release) \
SWIFTATOMIC_EXCHANGE_FN(swiftType, cType, storageType, acq_rel) \
SWIFTATOMIC_EXCHANGE_FN(swiftType, cType, storageType, seq_cst)
#define SWIFTATOMIC_CMPXCHG_FNS(variant, kind, swiftType, cType, storageType) \
SWIFTATOMIC_CMPXCHG_FN_##variant(kind, swiftType, cType, storageType, relaxed, relaxed) \
SWIFTATOMIC_CMPXCHG_FN_##variant(kind, swiftType, cType, storageType, acquire, relaxed) \
SWIFTATOMIC_CMPXCHG_FN_##variant(kind, swiftType, cType, storageType, release, relaxed) \
SWIFTATOMIC_CMPXCHG_FN_##variant(kind, swiftType, cType, storageType, acq_rel, relaxed) \
SWIFTATOMIC_CMPXCHG_FN_##variant(kind, swiftType, cType, storageType, seq_cst, relaxed) \
SWIFTATOMIC_CMPXCHG_FN_##variant(kind, swiftType, cType, storageType, acquire, acquire) \
SWIFTATOMIC_CMPXCHG_FN_##variant(kind, swiftType, cType, storageType, acq_rel, acquire) \
SWIFTATOMIC_CMPXCHG_FN_##variant(kind, swiftType, cType, storageType, seq_cst, acquire) \
SWIFTATOMIC_CMPXCHG_FN_##variant(kind, swiftType, cType, storageType, seq_cst, seq_cst)
#define SWIFTATOMIC_INTEGER_FNS(op, swiftType, cType, storageType) \
SWIFTATOMIC_INTEGER_FN(op, swiftType, cType, storageType, relaxed) \
SWIFTATOMIC_INTEGER_FN(op, swiftType, cType, storageType, acquire) \
SWIFTATOMIC_INTEGER_FN(op, swiftType, cType, storageType, release) \
SWIFTATOMIC_INTEGER_FN(op, swiftType, cType, storageType, acq_rel) \
SWIFTATOMIC_INTEGER_FN(op, swiftType, cType, storageType, seq_cst)
#define SWIFTATOMIC_DEFINE_TYPE(variant, swiftType, cType, storageType) \
SWIFTATOMIC_STORAGE_TYPE(swiftType, cType, storageType) \
SWIFTATOMIC_PREPARE_FN(swiftType, cType, storageType) \
SWIFTATOMIC_DISPOSE_FN(swiftType, cType, storageType) \
SWIFTATOMIC_LOAD_FNS(swiftType, cType, storageType) \
SWIFTATOMIC_STORE_FNS(swiftType, cType, storageType) \
SWIFTATOMIC_EXCHANGE_FNS(swiftType, cType, storageType) \
SWIFTATOMIC_CMPXCHG_FNS(variant, strong, swiftType, cType, storageType) \
SWIFTATOMIC_CMPXCHG_FNS(variant, weak, swiftType, cType, storageType)
#define SWIFTATOMIC_DEFINE_INTEGER_TYPE(variant, swiftType, cType, storageType) \
SWIFTATOMIC_DEFINE_TYPE(variant, swiftType, cType, storageType) \
SWIFTATOMIC_INTEGER_FNS(add, swiftType, cType, storageType) \
SWIFTATOMIC_INTEGER_FNS(sub, swiftType, cType, storageType) \
SWIFTATOMIC_INTEGER_FNS(or, swiftType, cType, storageType) \
SWIFTATOMIC_INTEGER_FNS(xor, swiftType, cType, storageType) \
SWIFTATOMIC_INTEGER_FNS(and, swiftType, cType, storageType)
// All known integer types
#define SWIFTATOMIC_ENCODE_Int(value) (value)
#define SWIFTATOMIC_DECODE_Int(value) (value)
SWIFTATOMIC_DEFINE_INTEGER_TYPE(SIMPLE, Int, intptr_t, intptr_t)
#define SWIFTATOMIC_ENCODE_Int8(value) (value)
#define SWIFTATOMIC_DECODE_Int8(value) (value)
SWIFTATOMIC_DEFINE_INTEGER_TYPE(SIMPLE, Int8, int8_t, int8_t)
#define SWIFTATOMIC_ENCODE_Int16(value) (value)
#define SWIFTATOMIC_DECODE_Int16(value) (value)
SWIFTATOMIC_DEFINE_INTEGER_TYPE(SIMPLE, Int16, int16_t, int16_t)
#define SWIFTATOMIC_ENCODE_Int32(value) (value)
#define SWIFTATOMIC_DECODE_Int32(value) (value)
SWIFTATOMIC_DEFINE_INTEGER_TYPE(SIMPLE, Int32, int32_t, int32_t)
#define SWIFTATOMIC_ENCODE_Int64(value) (value)
#define SWIFTATOMIC_DECODE_Int64(value) (value)
SWIFTATOMIC_DEFINE_INTEGER_TYPE(SIMPLE, Int64, int64_t, int64_t)
#define SWIFTATOMIC_ENCODE_UInt(value) (value)
#define SWIFTATOMIC_DECODE_UInt(value) (value)
SWIFTATOMIC_DEFINE_INTEGER_TYPE(SIMPLE, UInt, uintptr_t, uintptr_t)
#define SWIFTATOMIC_ENCODE_UInt8(value) (value)
#define SWIFTATOMIC_DECODE_UInt8(value) (value)
SWIFTATOMIC_DEFINE_INTEGER_TYPE(SIMPLE, UInt8, uint8_t, uint8_t)
#define SWIFTATOMIC_ENCODE_UInt16(value) (value)
#define SWIFTATOMIC_DECODE_UInt16(value) (value)
SWIFTATOMIC_DEFINE_INTEGER_TYPE(SIMPLE, UInt16, uint16_t, uint16_t)
#define SWIFTATOMIC_ENCODE_UInt32(value) (value)
#define SWIFTATOMIC_DECODE_UInt32(value) (value)
SWIFTATOMIC_DEFINE_INTEGER_TYPE(SIMPLE, UInt32, uint32_t, uint32_t)
#define SWIFTATOMIC_ENCODE_UInt64(value) (value)
#define SWIFTATOMIC_DECODE_UInt64(value) (value)
SWIFTATOMIC_DEFINE_INTEGER_TYPE(SIMPLE, UInt64, uint64_t, uint64_t)
// Atomic boolean
#define SWIFTATOMIC_ENCODE_Bool(value) (value)
#define SWIFTATOMIC_DECODE_Bool(value) (value)
SWIFTATOMIC_DEFINE_TYPE(SIMPLE, Bool, bool, bool)
SWIFTATOMIC_INTEGER_FNS(or, Bool, bool, bool)
SWIFTATOMIC_INTEGER_FNS(xor, Bool, bool, bool)
SWIFTATOMIC_INTEGER_FNS(and, Bool, bool, bool)
// Double wide atomics (__uint128_t on 64-bit platforms, uint64_t elsewhere)
#if __INTPTR_WIDTH__ == 64
#if !defined(__SIZEOF_INT128__)
#error "Double wide atomics need __uint128_t"
#endif
typedef struct {
__uint128_t value;
} _sa_dword SWIFTATOMIC_SWIFT_NAME(DoubleWord);
SWIFTATOMIC_INLINE
_sa_dword _sa_dword_create(uintptr_t high, uintptr_t low) {
_sa_dword result = { ((__uint128_t)high << 64) | (__uint128_t)low };
return result;
}
SWIFTATOMIC_INLINE
uintptr_t _sa_dword_extract_high(_sa_dword value) {
return (uintptr_t)(value.value >> 64);
}
SWIFTATOMIC_INLINE
uintptr_t _sa_dword_extract_low(_sa_dword value) {
return (uintptr_t)value.value;
}
SWIFTATOMIC_INLINE
_sa_dword _sa_decode_dword(__uint128_t value) {
_sa_dword result = { value };
return result;
}
#define SWIFTATOMIC_ENCODE_DoubleWord(_value) (_value).value
#define SWIFTATOMIC_DECODE_DoubleWord(value) _sa_decode_dword(value)
SWIFTATOMIC_DEFINE_TYPE(COMPLEX, DoubleWord, _sa_dword, __uint128_t)
#elif __INTPTR_WIDTH__ == 32
typedef struct {
uint64_t value;
} _sa_dword SWIFTATOMIC_SWIFT_NAME(DoubleWord);
SWIFTATOMIC_INLINE
_sa_dword _sa_dword_create(uintptr_t high, uintptr_t low) {
_sa_dword result = { ((uint64_t)high << 32) | (uint64_t)low };
return result;
}
SWIFTATOMIC_INLINE
uintptr_t _sa_dword_extract_high(_sa_dword value) {
return (uintptr_t)(value.value >> 32);
}
SWIFTATOMIC_INLINE
uintptr_t _sa_dword_extract_low(_sa_dword value) {
return (uintptr_t)value.value;
}
SWIFTATOMIC_INLINE
_sa_dword _sa_decode_dword(uint64_t value) {
_sa_dword result = { value };
return result;
}
#define SWIFTATOMIC_ENCODE_DoubleWord(_value) (_value).value
#define SWIFTATOMIC_DECODE_DoubleWord(value) _sa_decode_dword(value)
SWIFTATOMIC_DEFINE_TYPE(COMPLEX, DoubleWord, _sa_dword, uint64_t)
#else
#error "Unsupported intptr_t bit width"
#endif // __INTPTR_WIDTH
extern void _sa_retain_n(void *object, uint32_t n);
extern void _sa_release_n(void *object, uint32_t n);
#endif //SWIFTATOMIC_HEADER_INCLUDED
+23
View File
@@ -0,0 +1,23 @@
//===----------------------------------------------------------------------===//
//
// This source file is part of the Swift Atomics open source project
//
// Copyright (c) 2020 Apple Inc. and the Swift project authors
// Licensed under Apache License v2.0 with Runtime Library Exception
//
// See https://swift.org/LICENSE.txt for license information
// See https://swift.org/CONTRIBUTORS.txt for the list of Swift project authors
//
//===----------------------------------------------------------------------===//
#include "_AtomicsShims.h"
void _sa_retain_n(void *object, uint32_t n) {
extern void *swift_retain_n(void *object, uint32_t n);
swift_retain_n(object, n);
}
void _sa_release_n(void *object, uint32_t n) {
extern void swift_release_n(void *object, uint32_t n);
swift_release_n(object, n);
}
+369
View File
@@ -0,0 +1,369 @@
//===----------------------------------------------------------------------===//
//
// This source file is part of the Swift Atomics open source project
//
// Copyright (c) 2020 Apple Inc. and the Swift project authors
// Licensed under Apache License v2.0 with Runtime Library Exception
//
// See https://swift.org/LICENSE.txt for license information
// See https://swift.org/CONTRIBUTORS.txt for the list of Swift project authors
//
//===----------------------------------------------------------------------===//
%{
from gyb_utils import (
autogenerated_warning, loadOrderings, storeOrderings, updateOrderings,
boolOperations, integerOperations, lowerFirst, argLabel)
types = [
("Int", "Int", "12", "23"),
("Int8", "Int8", "12", "23"),
("Int16", "Int16", "12", "23"),
("Int32", "Int32", "12", "23"),
("Int64", "Int64", "12", "23"),
("UInt", "UInt", "12", "23"),
("UInt8", "UInt8", "12", "23"),
("UInt16", "UInt16", "12", "23"),
("UInt32", "UInt32", "12", "23"),
("UInt64", "UInt64", "12", "23"),
("Bool", "Bool", "true", "false"),
("DoubleWord", "DoubleWord", "DoubleWord(high: 100, low: 64)", "DoubleWord(high: 50, low: 32)"),
("Pointer", "UnsafePointer<Foo>", "_foo1", "_foo2"),
("OptionalPointer", "UnsafePointer<Foo>?", "nil", "_foo2"),
("MutablePointer", "UnsafeMutablePointer<Foo>", "_mfoo1", "_mfoo2"),
("OptionalMutablePointer", "UnsafeMutablePointer<Foo>?", "nil", "_mfoo2"),
("RawPointer", "UnsafeMutableRawPointer", "_raw1", "_raw2"),
("OptionalRawPointer", "UnsafeMutableRawPointer?", "nil", "_raw2"),
("Unmanaged", "Unmanaged<Bar>", "_bar1", "_bar2"),
("OptionalUnmanaged", "Unmanaged<Bar>?", "nil", "_bar2"),
("Reference", "Baz", "_baz1", "_baz2"),
("OptionalReference", "Baz?", "nil", "_baz2"),
("RawRepresentable", "Fred", "Fred.one", "Fred.two"),
]
}%
${autogenerated_warning()}
import XCTest
import Atomics
extension Unmanaged: Equatable { // FIXME: This is terrible
public static func ==(left: Self, right: Self) -> Bool {
left.toOpaque() == right.toOpaque()
}
}
private struct Foo: Equatable, CustomStringConvertible {
var value: Int
init(_ value: Int) { self.value = value }
var description: String { "Foo(\(value))" }
}
private class Bar: Equatable, CustomStringConvertible {
var value: Int
init(_ value: Int) { self.value = value }
var description: String { "Bar(\(value))" }
static func ==(left: Bar, right: Bar) -> Bool {
left === right
}
}
private class Baz: Equatable, CustomStringConvertible, AtomicReference {
var value: Int
init(_ value: Int) { self.value = value }
var description: String { "Bar(\(value))" }
static func ==(left: Baz, right: Baz) -> Bool {
left === right
}
}
private enum Fred: Int, AtomicValue {
case one
case two
}
/// Exercises all operations in a single-threaded context, verifying
/// they provide the expected results.
class BasicAtomicTests: XCTestCase {
private let _mfoo1: UnsafeMutablePointer<Foo> = {
let p = UnsafeMutablePointer<Foo>.allocate(capacity: 1)
p.initialize(to: Foo(1))
return p
}()
private let _mfoo2: UnsafeMutablePointer<Foo> = {
let p = UnsafeMutablePointer<Foo>.allocate(capacity: 1)
p.initialize(to: Foo(2))
return p
}()
private var _foo1: UnsafePointer<Foo> { UnsafePointer(_mfoo1) }
private var _foo2: UnsafePointer<Foo> { UnsafePointer(_mfoo2) }
private let _raw1 = UnsafeMutableRawPointer.allocate(byteCount: 16, alignment: 1)
private let _raw2 = UnsafeMutableRawPointer.allocate(byteCount: 16, alignment: 1)
private let _bar1 = Unmanaged<Bar>.passRetained(Bar(1))
private let _bar2 = Unmanaged<Bar>.passRetained(Bar(2))
private let _baz1 = Baz(1)
private let _baz2 = Baz(2)
deinit {
_mfoo1.deinitialize(count: 1)
_mfoo1.deallocate()
_mfoo2.deinitialize(count: 1)
_mfoo2.deallocate()
_raw1.deallocate()
_raw2.deallocate()
_bar1.release()
_bar2.release()
}
% for label, type, a, b in types:
func test_${label}_create_destroy() {
let v = UnsafeAtomic<${type}>.create(${a})
defer { v.destroy() }
XCTAssertEqual(v.load(ordering: .relaxed), ${a})
let w = UnsafeAtomic<${type}>.create(${b})
defer { w.destroy() }
XCTAssertEqual(w.load(ordering: .relaxed), ${b})
}
% for (order, _) in loadOrderings:
func test_${label}_load_${order}() {
let v = UnsafeAtomic<${type}>.create(${a})
defer { v.destroy() }
XCTAssertEqual(v.load(ordering: .${order}), ${a})
let w = UnsafeAtomic<${type}>.create(${b})
defer { w.destroy() }
XCTAssertEqual(w.load(ordering: .${order}), ${b})
}
% end
% for (order, _) in storeOrderings:
func test_${label}_store_${order}() {
let v = UnsafeAtomic<${type}>.create(${a})
defer { v.destroy() }
v.store(${b}, ordering: .${order})
XCTAssertEqual(v.load(ordering: .relaxed), ${b})
let w = UnsafeAtomic<${type}>.create(${b})
defer { w.destroy() }
w.store(${a}, ordering: .${order})
XCTAssertEqual(w.load(ordering: .relaxed), ${a})
}
% end
% for (order, _, _) in updateOrderings:
func test_${label}_exchange_${order}() {
let v = UnsafeAtomic<${type}>.create(${a})
defer { v.destroy() }
XCTAssertEqual(v.exchange(${a}, ordering: .${order}), ${a})
XCTAssertEqual(v.load(ordering: .relaxed), ${a})
XCTAssertEqual(v.exchange(${b}, ordering: .${order}), ${a})
XCTAssertEqual(v.load(ordering: .relaxed), ${b})
XCTAssertEqual(v.exchange(${b}, ordering: .${order}), ${b})
XCTAssertEqual(v.load(ordering: .relaxed), ${b})
}
% end
% for (order, _, _) in updateOrderings:
func test_${label}_compareExchange_${order}() {
let v = UnsafeAtomic<${type}>.create(${a})
defer { v.destroy() }
var (exchanged, original) = v.compareExchange(
expected: ${a},
desired: ${b},
ordering: .${order})
XCTAssertTrue(exchanged)
XCTAssertEqual(original, ${a})
XCTAssertEqual(v.load(ordering: .relaxed), ${b})
(exchanged, original) = v.compareExchange(
expected: ${a},
desired: ${b},
ordering: .${order})
XCTAssertFalse(exchanged)
XCTAssertEqual(original, ${b})
XCTAssertEqual(v.load(ordering: .relaxed), ${b})
(exchanged, original) = v.compareExchange(
expected: ${b},
desired: ${a},
ordering: .${order})
XCTAssertTrue(exchanged)
XCTAssertEqual(original, ${b})
XCTAssertEqual(v.load(ordering: .relaxed), ${a})
(exchanged, original) = v.compareExchange(
expected: ${b},
desired: ${a},
ordering: .${order})
XCTAssertFalse(exchanged)
XCTAssertEqual(original, ${a})
XCTAssertEqual(v.load(ordering: .relaxed), ${a})
}
% end
% for operation in ["compareExchange", "weakCompareExchange"]:
% for (successorder, _, _) in updateOrderings:
% for (failorder, _) in loadOrderings:
func test_${label}_${operation}_${successorder}_${failorder}() {
let v = UnsafeAtomic<${type}>.create(${a})
defer { v.destroy() }
var (exchanged, original) = v.compareExchange(
expected: ${a},
desired: ${b},
successOrdering: .${successorder},
failureOrdering: .${failorder})
XCTAssertTrue(exchanged)
XCTAssertEqual(original, ${a})
XCTAssertEqual(v.load(ordering: .relaxed), ${b})
(exchanged, original) = v.compareExchange(
expected: ${a},
desired: ${b},
successOrdering: .${successorder},
failureOrdering: .${failorder})
XCTAssertFalse(exchanged)
XCTAssertEqual(original, ${b})
XCTAssertEqual(v.load(ordering: .relaxed), ${b})
(exchanged, original) = v.compareExchange(
expected: ${b},
desired: ${a},
successOrdering: .${successorder},
failureOrdering: .${failorder})
XCTAssertTrue(exchanged)
XCTAssertEqual(original, ${b})
XCTAssertEqual(v.load(ordering: .relaxed), ${a})
(exchanged, original) = v.compareExchange(
expected: ${b},
desired: ${a},
successOrdering: .${successorder},
failureOrdering: .${failorder})
XCTAssertFalse(exchanged)
XCTAssertEqual(original, ${a})
XCTAssertEqual(v.load(ordering: .relaxed), ${a})
}
% end
% end
% end
// Bool operations
% if type == "Bool":
% for (name, _, operator, arglabel, _) in boolOperations:
% for (order, _, _) in updateOrderings:
func test_${label}_loadThen${name}_${order}() {
let v = UnsafeAtomic<Bool>.create(false)
defer { v.destroy() }
// The truth tables are super tiny, so just check every value
for a in [false, true] {
for b in [false, true] {
v.store(a, ordering: .relaxed)
let r = v.loadThen${name}(${argLabel(arglabel)}b, ordering: .${order})
XCTAssertEqual(r, a, "a = \(a), b = \(b)")
XCTAssertEqual(
v.load(ordering: .relaxed),
a ${operator} b,
"a = \(a), b =\(b)")
}
}
}
% end
% end
% for (name, _, operator, arglabel, _) in boolOperations:
% for (order, _, _) in updateOrderings:
func test_${label}_${lowerFirst(name)}ThenLoad_${order}() {
let v = UnsafeAtomic<Bool>.create(false)
defer { v.destroy() }
// The truth tables are super tiny, so just check every value
for a in [false, true] {
for b in [false, true] {
v.store(a, ordering: .relaxed)
let r = v.${lowerFirst(name)}ThenLoad(
${argLabel(arglabel)}b,
ordering: .${order})
XCTAssertEqual(r, a ${operator} b, "a = \(a), b = \(b)")
XCTAssertEqual(
v.load(ordering: .relaxed),
a ${operator} b,
"a = \(a), b =\(b)")
}
}
}
% end
% end
% end
// Integer operations
% if type.startswith("Int") or type.startswith("UInt"):
% for (name, _, operator, arglabel, _) in integerOperations:
% for (order, _, _) in updateOrderings:
func test_${label}_loadThen${name}_${order}() {
let a: ${type} = 3
let b: ${type} = 8
let c: ${type} = 12
let result1: ${type} = a ${operator} b
let result2: ${type} = result1 ${operator} c
let v = UnsafeAtomic<${type}>.create(a)
defer { v.destroy() }
let old1 = v.loadThen${name}(${argLabel(arglabel)}b, ordering: .${order})
XCTAssertEqual(old1, a)
XCTAssertEqual(v.load(ordering: .relaxed), result1)
let old2 = v.loadThen${name}(${argLabel(arglabel)}c, ordering: .${order})
XCTAssertEqual(old2, result1)
XCTAssertEqual(v.load(ordering: .relaxed), result2)
}
% end
% end
% for (name, _, operator, arglabel, _) in integerOperations:
% for (order, _, _) in updateOrderings:
func test_${label}_${lowerFirst(name)}ThenLoad_${order}() {
let a: ${type} = 3
let b: ${type} = 8
let c: ${type} = 12
let result1: ${type} = a ${operator} b
let result2: ${type} = result1 ${operator} c
let v = UnsafeAtomic<${type}>.create(a)
defer { v.destroy() }
let new1 = v.${lowerFirst(name)}ThenLoad(${argLabel(arglabel)}b, ordering: .${order})
XCTAssertEqual(new1, result1)
XCTAssertEqual(v.load(ordering: .relaxed), result1)
let new2 = v.${lowerFirst(name)}ThenLoad(${argLabel(arglabel)}c, ordering: .${order})
XCTAssertEqual(new2, result2)
XCTAssertEqual(v.load(ordering: .relaxed), result2)
}
% end
% end
% end
% end
}
+89
View File
@@ -0,0 +1,89 @@
//===----------------------------------------------------------------------===//
//
// This source file is part of the Swift Atomics open source project
//
// Copyright (c) 2020 Apple Inc. and the Swift project authors
// Licensed under Apache License v2.0 with Runtime Library Exception
//
// See https://swift.org/LICENSE.txt for license information
// See https://swift.org/CONTRIBUTORS.txt for the list of Swift project authors
//
//===----------------------------------------------------------------------===//
import XCTest
import Atomics
class DoubleWordTests: XCTestCase {
func testMemoryLayout() {
XCTAssertEqual(MemoryLayout<DoubleWord>.size, 2 * MemoryLayout<UInt>.size)
XCTAssertEqual(MemoryLayout<DoubleWord>.stride, MemoryLayout<DoubleWord>.size)
XCTAssertEqual(MemoryLayout<DoubleWord>.alignment, 2 * MemoryLayout<UInt>.alignment)
}
struct UIntPair: Equatable {
var first: UInt
var second: UInt
init(_ first: UInt, _ second: UInt) {
self.first = first
self.second = second
}
}
func componentsInMemoryOrder(of dword: DoubleWord) -> UIntPair {
let p = UnsafeMutableRawPointer.allocate(
byteCount: MemoryLayout<DoubleWord>.size,
alignment: MemoryLayout<DoubleWord>.alignment)
p.storeBytes(of: dword, as: DoubleWord.self)
let first = p.load(as: UInt.self)
let second = p.load(fromByteOffset: MemoryLayout<UInt>.stride, as: UInt.self)
return UIntPair(first, second)
}
func testFirstSecondInitializer() {
let value = DoubleWord(first: 1, second: 2)
XCTAssertEqual(componentsInMemoryOrder(of: value), UIntPair(1, 2))
}
func testHighLowInitializer() {
let value = DoubleWord(high: 1, low: 2)
#if _endian(little)
XCTAssertEqual(componentsInMemoryOrder(of: value), UIntPair(2, 1))
#else
XCTAssertEqual(componentsInMemoryOrder(of: value), UIntPair(1, 2))
#endif
}
func testPropertyGetters() {
let value = DoubleWord(first: UInt.max, second: 0)
XCTAssertEqual(value.first, UInt.max)
XCTAssertEqual(value.second, 0)
#if _endian(little)
XCTAssertEqual(value.high, 0)
XCTAssertEqual(value.low, UInt.max)
#else
XCTAssertEqual(value.high, UInt.max)
XCTAssertEqual(value.low, 0)
#endif
}
func testPropertySetters() {
var value = DoubleWord(first: 1, second: 2)
value.first = 3
XCTAssertEqual(componentsInMemoryOrder(of: value), UIntPair(3, 2))
value.second = 4
XCTAssertEqual(componentsInMemoryOrder(of: value), UIntPair(3, 4))
value.low = 5
#if _endian(little)
XCTAssertEqual(componentsInMemoryOrder(of: value), UIntPair(5, 4))
#else
XCTAssertEqual(componentsInMemoryOrder(of: value), UIntPair(3, 5))
#endif
value.high = 6
#if _endian(little)
XCTAssertEqual(componentsInMemoryOrder(of: value), UIntPair(5, 6))
#else
XCTAssertEqual(componentsInMemoryOrder(of: value), UIntPair(6, 5))
#endif
}
}
+78
View File
@@ -0,0 +1,78 @@
//===----------------------------------------------------------------------===//
//
// This source file is part of the Swift Atomics open source project
//
// Copyright (c) 2014 - 2020 Apple Inc. and the Swift project authors
// Licensed under Apache License v2.0 with Runtime Library Exception
//
// See https://swift.org/LICENSE.txt for license information
// See https://swift.org/CONTRIBUTORS.txt for the list of Swift project authors
//
//===----------------------------------------------------------------------===//
/// A type that tracks the number of live instances.
///
/// To be useful in more contexts, `LifetimeTracked` conforms to various
/// protocols in trivial ways.
///
/// `LifetimeTracked` is useful to check for leaks in algorithms and data
/// structures. `StdlibUnittest` harness automatically checks that after each
/// test has done executing, no `LifetimeTracked` instances exist.
public final class LifetimeTracked {
public init(_ value: Int, identity: Int = 0) {
LifetimeTracked.instances += 1
LifetimeTracked._nextSerialNumber += 1
serialNumber = LifetimeTracked._nextSerialNumber
self.value = value
self.identity = identity
}
deinit {
assert(serialNumber > 0, "double destruction!")
LifetimeTracked.instances -= 1
serialNumber = -serialNumber
}
public static var instances: Int = 0
internal static var _nextSerialNumber = 0
public let value: Int
public var identity: Int
public var serialNumber: Int = 0
}
extension LifetimeTracked : Equatable {
public static func == (x: LifetimeTracked, y: LifetimeTracked) -> Bool {
return x.value == y.value
}
}
extension LifetimeTracked : Hashable {
public var hashValue: Int {
return value
}
public func hash(into hasher: inout Hasher) {
hasher.combine(value)
}
}
extension LifetimeTracked : Strideable {
public func distance(to other: LifetimeTracked) -> Int {
return self.value.distance(to: other.value)
}
public func advanced(by n: Int) -> LifetimeTracked {
return LifetimeTracked(self.value.advanced(by: n))
}
}
extension LifetimeTracked : CustomStringConvertible {
public var description: String {
assert(serialNumber > 0, "dead Tracked!")
return value.description
}
}
public func < (x: LifetimeTracked, y: LifetimeTracked) -> Bool {
return x.value < y.value
}
+171
View File
@@ -0,0 +1,171 @@
//===----------------------------------------------------------------------===//
//
// This source file is part of the Swift Atomics open source project
//
// Copyright (c) 2020 Apple Inc. and the Swift project authors
// Licensed under Apache License v2.0 with Runtime Library Exception
//
// See https://swift.org/LICENSE.txt for license information
// See https://swift.org/CONTRIBUTORS.txt for the list of Swift project authors
//
//===----------------------------------------------------------------------===//
// A lock-free concurrent queue implementation adapted from
// M. Michael and M. Scott's 1996 paper [Michael 1996].
//
// [Michael 1996]: https://doi.org/10.1145/248052.248106
//
// While this is a nice illustration of the use of atomic strong references,
// this is a somewhat sloppy implementation of an old algorithm. If you need a
// lock-free queue for actual production use, it would probably be a good idea
// to look at some more recent algorithms before deciding on this one.
//
// Note: because this implementation uses reference counting, we don't need
// to implement a free list to resolve the original algorithm's use-after-free
// problem.
import XCTest
import Dispatch
import Atomics
class LockFreeQueue<Element> {
class Node: AtomicReference {
let next: ManagedAtomic<Node?>
var value: Element?
init(value: Element?, next: Node?) {
self.value = value
self.next = ManagedAtomic(next)
}
deinit {
var values = 0
// Prevent stack overflow when reclaiming a long queue
var node = self.next.exchange(nil, ordering: .relaxed)
while node != nil && isKnownUniquelyReferenced(&node) {
let next = node!.next.exchange(nil, ordering: .relaxed)
withExtendedLifetime(node) {
values += 1
}
node = next
}
if values > 0 {
print(values)
}
}
}
let head: ManagedAtomic<Node>
let tail: ManagedAtomic<Node>
// Used to distinguish removed nodes from active nodes with a nil `next`.
let marker = Node(value: nil, next: nil)
init() {
let dummy = Node(value: nil, next: nil)
self.head = ManagedAtomic(dummy)
self.tail = ManagedAtomic(dummy)
}
func enqueue(_ newValue: Element) {
let new = Node(value: newValue, next: nil)
var tail = self.tail.load(ordering: .acquiring)
while true {
let next = tail.next.load(ordering: .acquiring)
if tail === marker || next === marker {
// The node we loaded has been unlinked by a dequeue on another thread.
// Try again.
tail = self.tail.load(ordering: .acquiring)
continue
}
if let next = next {
// Assist competing threads by nudging `self.tail` forward a step.
let (exchanged, original) = self.tail.compareExchange(
expected: tail,
desired: next,
ordering: .acquiringAndReleasing)
tail = (exchanged ? next : original)
continue
}
let (exchanged, current) = tail.next.compareExchange(
expected: nil,
desired: new,
ordering: .acquiringAndReleasing
)
if exchanged {
_ = self.tail.compareExchange(expected: tail, desired: new, ordering: .releasing)
return
}
tail = current!
}
}
func dequeue() -> Element? {
while true {
let head = self.head.load(ordering: .acquiring)
let next = head.next.load(ordering: .acquiring)
if next === marker { continue }
guard let n = next else { return nil }
let tail = self.tail.load(ordering: .acquiring)
if head === tail {
// Nudge `tail` forward a step to make sure it doesn't fall off the
// list when we unlink this node.
_ = self.tail.compareExchange(expected: tail, desired: n, ordering: .acquiringAndReleasing)
}
if self.head.compareExchange(expected: head, desired: n, ordering: .releasing).exchanged {
let result = n.value!
n.value = nil
// To prevent threads that are suspended in `enqueue`/`dequeue` from
// holding onto arbitrarily long chains of removed nodes, we unlink
// removed nodes by replacing their `next` value with the special
// `marker`.
head.next.store(marker, ordering: .releasing)
return result
}
}
}
}
class QueueTests: XCTestCase {
func check(readers: Int, writers: Int, count: Int) {
let queue = LockFreeQueue<(writer: Int, value: Int)>()
let num = ManagedAtomic(0)
DispatchQueue.concurrentPerform(iterations: writers + readers) { id in
if id < writers {
// Writer
for i in 0 ..< count {
queue.enqueue((id, i))
}
} else {
// Reader
var values = (0 ..< writers).map { _ in -1 }
while num.load(ordering: .relaxed) < writers * count {
// Spin until we get a value
guard let (writer, value) = queue.dequeue() else { continue }
precondition(writer >= 0 && writer < writers)
precondition(readers == 1 ? value == values[writer] + 1 : value > values[writer])
values[writer] = value
num.wrappingIncrement(ordering: .relaxed)
}
}
}
}
func test01_10() {
check(readers: 1, writers: 10, count: 1_000_000)
}
func test02_10() {
check(readers: 2, writers: 10, count: 1_000_000)
}
func test04_10() {
check(readers: 2, writers: 10, count: 1_000_000)
}
func test16_16() {
check(readers: 16, writers: 16, count: 1_000_000)
}
}
@@ -0,0 +1,145 @@
//===----------------------------------------------------------------------===//
//
// This source file is part of the Swift Atomics open source project
//
// Copyright (c) 2020 Apple Inc. and the Swift project authors
// Licensed under Apache License v2.0 with Runtime Library Exception
//
// See https://swift.org/LICENSE.txt for license information
// See https://swift.org/CONTRIBUTORS.txt for the list of Swift project authors
//
//===----------------------------------------------------------------------===//
import XCTest
import Atomics
import Dispatch
class LockFreeSingleConsumerStack<Element> {
struct Node {
let value: Element
var next: UnsafeMutablePointer<Node>?
}
typealias NodePtr = UnsafeMutablePointer<Node>
private var _last = UnsafeAtomic<NodePtr?>.create(nil)
private var _consumerCount = UnsafeAtomic<Int>.create(0)
private var foo = 0
deinit {
// Discard remaining nodes
while let _ = pop() {}
_last.destroy()
_consumerCount.destroy()
}
// Push the given element to the top of the stack.
// It is okay to concurrently call this in an arbitrary number of threads.
func push(_ value: Element) {
let new = NodePtr.allocate(capacity: 1)
new.initialize(to: Node(value: value, next: nil))
var done = false
var current = _last.load(ordering: .relaxed)
while !done {
new.pointee.next = current
(done, current) = _last.compareExchange(
expected: current,
desired: new,
ordering: .releasing)
}
}
// Pop and return the topmost element from the stack.
// This method does not support multiple overlapping concurrent calls.
func pop() -> Element? {
precondition(
_consumerCount.loadThenWrappingIncrement(ordering: .acquiring) == 0,
"Multiple consumers detected")
defer { _consumerCount.wrappingDecrement(ordering: .releasing) }
var done = false
var current = _last.load(ordering: .acquiring)
while let c = current {
(done, current) = _last.compareExchange(
expected: c,
desired: c.pointee.next,
ordering: .acquiring)
if done {
let result = c.move()
c.deallocate()
return result.value
}
}
return nil
}
}
class LockFreeSingleConsumerStackTests: XCTestCase {
func test_Basics() {
let stack = LockFreeSingleConsumerStack<Int>()
XCTAssertNil(stack.pop())
stack.push(0)
XCTAssertEqual(0, stack.pop())
stack.push(1)
stack.push(2)
stack.push(3)
stack.push(4)
XCTAssertEqual(4, stack.pop())
XCTAssertEqual(3, stack.pop())
XCTAssertEqual(2, stack.pop())
XCTAssertEqual(1, stack.pop())
XCTAssertNil(stack.pop())
}
func test_ConcurrentPushes() {
let stack = LockFreeSingleConsumerStack<(thread: Int, value: Int)>()
let numThreads = 100
let numValues = 10_000
DispatchQueue.concurrentPerform(iterations: numThreads) { thread in
for value in 1 ... numValues {
stack.push((thread: thread, value: value))
}
}
var expected: [Int] = Array(repeating: numValues, count: numThreads)
while let (thread, value) = stack.pop() {
XCTAssertEqual(expected[thread], value)
expected[thread] -= 1
}
XCTAssertEqual(Array(repeating: 0, count: numThreads), expected)
}
func test_ConcurrentPushesAndPops() {
let stack = LockFreeSingleConsumerStack<(thread: Int, value: Int)>()
let numThreads = 100
let numValues = 10_000
var perThreadSums: [Int] = Array(repeating: 0, count: numThreads)
let consumerQueue = DispatchQueue(label: "org.swift.background")
consumerQueue.async {
var count = 0
while count < numThreads * numValues {
// Note: busy wait
if let (thread, value) = stack.pop() {
perThreadSums[thread] += value
count += 1
}
}
}
DispatchQueue.concurrentPerform(iterations: numThreads + 1) { thread in
if thread < numThreads {
// Producers
for value in 0 ..< numValues {
stack.push((thread: thread, value: value))
}
}
}
consumerQueue.sync {
XCTAssertEqual(Array(repeating: numValues * (numValues - 1) / 2, count: numThreads), perThreadSums)
}
}
}
@@ -0,0 +1,229 @@
//===----------------------------------------------------------------------===//
//
// This source file is part of the Swift Atomics open source project
//
// Copyright (c) 2020 Apple Inc. and the Swift project authors
// Licensed under Apache License v2.0 with Runtime Library Exception
//
// See https://swift.org/LICENSE.txt for license information
// See https://swift.org/CONTRIBUTORS.txt for the list of Swift project authors
//
//===----------------------------------------------------------------------===//
import XCTest
import Atomics
import Dispatch
let iterations = 1_000_000
private class Node: AtomicReference {
let value: Int
init(_ value: Int = 0) {
self.value = value
Node.instances.wrappingIncrement(ordering: .relaxed)
}
deinit {
precondition(Node.instances.load(ordering: .relaxed) > 0)
Node.instances.wrappingDecrement(ordering: .relaxed)
}
static let instances = ManagedAtomic<Int>(0)
}
extension Node: CustomStringConvertible {
var description: String {
let unmanaged = Unmanaged.passRetained(self)
defer { unmanaged.release() }
return String(UInt(bitPattern: unmanaged.toOpaque()), radix: 16)
}
}
@inline(never)
public func blackHole(_ value: AnyObject) {}
@inline(never)
public func blackHole(_ value: AnyObject?) {}
class StrongReferenceRace: XCTestCase {
override func tearDown() {
super.tearDown()
let leftover = Node.instances.load(ordering: .relaxed)
XCTAssertEqual(
leftover, 0,
"Leak - \(leftover) leftover instances remaining")
}
func checkLoad(count: Int, iterations: Int, file: StaticString = #file, line: UInt = #line) {
let ref = UnsafeAtomic<Node>.create(Node())
defer { ref.destroy() }
DispatchQueue.concurrentPerform(iterations: count) { id in
for _ in 0 ..< iterations {
blackHole(ref.load(ordering: .relaxed))
}
}
}
func testLoad1() { checkLoad(count: 1, iterations: iterations) }
func testLoad2() { checkLoad(count: 2, iterations: iterations) }
func testLoad4() { checkLoad(count: 4, iterations: iterations) }
func testLoad8() { checkLoad(count: 8, iterations: iterations) }
func testLoad16() { checkLoad(count: 16, iterations: iterations) }
func checkCompareExchange(count: Int, iterations: Int, file: StaticString = #file, line: UInt = #line) {
let a = Node()
let b = Node()
let ref = UnsafeAtomic<Node>.create(a)
defer { ref.destroy() }
DispatchQueue.concurrentPerform(iterations: count) { id in
var expected = a
for _ in 0 ..< iterations {
var done = false
repeat {
(done, expected) = ref.compareExchange(
expected: expected,
desired: expected === a ? b : a,
ordering: .relaxed)
} while !done
}
}
}
func testCompareExchange1() { checkCompareExchange(count: 1, iterations: iterations) }
func testCompareExchange2() { checkCompareExchange(count: 2, iterations: iterations) }
func testCompareExchange4() { checkCompareExchange(count: 4, iterations: iterations) }
func testCompareExchange8() { checkCompareExchange(count: 8, iterations: iterations) }
func testCompareExchange16() { checkCompareExchange(count: 16, iterations: iterations) }
func checkCompareExchangeNil(count: Int, iterations: Int, file: StaticString = #file, line: UInt = #line) {
let a = Node()
let b = Node()
let ref = UnsafeAtomic<Node?>.create(nil)
defer { ref.destroy() }
DispatchQueue.concurrentPerform(iterations: count) { id in
var expected: Node? = nil
for _ in 0 ..< iterations {
var done = false
repeat {
(done, expected) = ref.compareExchange(
expected: expected,
desired: expected == nil ? a : expected === a ? b : nil,
ordering: .relaxed)
} while !done
}
}
}
func testCompareExchangeNil_01() { checkCompareExchangeNil(count: 1, iterations: iterations) }
func testCompareExchangeNil_02() { checkCompareExchangeNil(count: 2, iterations: iterations) }
func testCompareExchangeNil_04() { checkCompareExchangeNil(count: 4, iterations: iterations) }
func testCompareExchangeNil_08() { checkCompareExchangeNil(count: 8, iterations: iterations) }
func testCompareExchangeNil_16() { checkCompareExchangeNil(count: 16, iterations: iterations) }
func checkLoadStore(readers: Int, writers: Int, iterations: Int, file: StaticString = #file, line: UInt = #line) {
let a = Node()
let b = Node()
let ref = UnsafeAtomic<Node>.create(a)
defer { ref.destroy() }
DispatchQueue.concurrentPerform(iterations: readers + writers) { id in
if id < writers {
var next = b
for _ in 0 ..< iterations {
ref.store(next, ordering: .relaxed)
next = next === a ? b : a
}
} else {
for _ in 0 ..< iterations {
blackHole(ref.load(ordering: .relaxed))
}
}
}
}
func testLoadStore_01_01() { checkLoadStore(readers: 1, writers: 1, iterations: iterations) }
func testLoadStore_02_01() { checkLoadStore(readers: 2, writers: 1, iterations: iterations) }
func testLoadStore_04_01() { checkLoadStore(readers: 4, writers: 1, iterations: iterations) }
func testLoadStore_08_01() { checkLoadStore(readers: 8, writers: 1, iterations: iterations) }
func testLoadStore_16_01() { checkLoadStore(readers: 16, writers: 1, iterations: iterations) }
func testLoadStore_01_02() { checkLoadStore(readers: 1, writers: 2, iterations: iterations) }
func testLoadStore_02_02() { checkLoadStore(readers: 2, writers: 2, iterations: iterations) }
func testLoadStore_04_02() { checkLoadStore(readers: 4, writers: 2, iterations: iterations) }
func testLoadStore_08_02() { checkLoadStore(readers: 8, writers: 2, iterations: iterations) }
func testLoadStore_16_02() { checkLoadStore(readers: 16, writers: 2, iterations: iterations) }
func testLoadStore_01_04() { checkLoadStore(readers: 1, writers: 4, iterations: iterations) }
func testLoadStore_02_04() { checkLoadStore(readers: 2, writers: 4, iterations: iterations) }
func testLoadStore_04_04() { checkLoadStore(readers: 4, writers: 4, iterations: iterations) }
func testLoadStore_08_04() { checkLoadStore(readers: 8, writers: 4, iterations: iterations) }
func testLoadStore_16_04() { checkLoadStore(readers: 16, writers: 4, iterations: iterations) }
func checkExchange(readers: Int, writers: Int, iterations: Int, file: StaticString = #file, line: UInt = #line) {
let a = Node()
let b = Node()
let ref = UnsafeAtomic<Node?>.create(nil)
defer { ref.destroy() }
DispatchQueue.concurrentPerform(iterations: readers + writers) { id in
if id < writers {
var next: Node? = nil
for _ in 0 ..< iterations {
let old = ref.exchange(next, ordering: .relaxed)
if old == nil { next = a }
else if old === a { next = b }
else { next = nil }
}
} else {
for _ in 0 ..< iterations {
blackHole(ref.load(ordering: .relaxed))
}
}
}
}
func testExchange_01_01() { checkExchange(readers: 1, writers: 1, iterations: iterations) }
func testExchange_02_01() { checkExchange(readers: 2, writers: 1, iterations: iterations) }
func testExchange_04_01() { checkExchange(readers: 4, writers: 1, iterations: iterations) }
func testExchange_08_01() { checkExchange(readers: 8, writers: 1, iterations: iterations) }
func testExchange_16_01() { checkExchange(readers: 16, writers: 1, iterations: iterations) }
func testExchange_01_02() { checkExchange(readers: 1, writers: 2, iterations: iterations) }
func testExchange_02_02() { checkExchange(readers: 2, writers: 2, iterations: iterations) }
func testExchange_04_02() { checkExchange(readers: 4, writers: 2, iterations: iterations) }
func testExchange_08_02() { checkExchange(readers: 8, writers: 2, iterations: iterations) }
func testExchange_16_02() { checkExchange(readers: 16, writers: 2, iterations: iterations) }
func testExchange_01_04() { checkExchange(readers: 1, writers: 4, iterations: iterations) }
func testExchange_02_04() { checkExchange(readers: 2, writers: 4, iterations: iterations) }
func testExchange_04_04() { checkExchange(readers: 4, writers: 4, iterations: iterations) }
func testExchange_08_04() { checkExchange(readers: 8, writers: 4, iterations: iterations) }
func testExchange_16_04() { checkExchange(readers: 16, writers: 4, iterations: iterations) }
func checkLifetimes(count: Int, iterations: Int, file: StaticString = #file, line: UInt = #line) {
precondition(count > 1)
let objects = (0 ..< count).map { ManagedAtomic<Node>(Node($0)) }
let originals = objects.map { $0.load(ordering: .relaxed).value }
DispatchQueue.concurrentPerform(iterations: count) { id in
var object = objects[id].load(ordering: .acquiring)
var i = id
for _ in 0 ..< iterations {
let j = (i + 1) % count
object = objects[j].exchange(object, ordering: .acquiringAndReleasing)
object = objects[i].exchange(object, ordering: .acquiringAndReleasing)
i = j
}
while true {
let new = objects[object.value].exchange(object, ordering: .acquiringAndReleasing)
if new === object { break }
object = new
}
}
let reordered = objects.map { $0.load(ordering: .relaxed).value }
print(originals, reordered)
XCTAssertEqual(Set(originals), Set(reordered))
}
func testLifetimes_02() { checkLifetimes(count: 2, iterations: iterations) }
func testLifetimes_04() { checkLifetimes(count: 4, iterations: iterations) }
func testLifetimes_08() { checkLifetimes(count: 8, iterations: iterations) }
func testLifetimes_16() { checkLifetimes(count: 16, iterations: iterations) }
}
@@ -0,0 +1,258 @@
//===----------------------------------------------------------------------===//
//
// This source file is part of the Swift Atomics open source project
//
// Copyright (c) 2020 Apple Inc. and the Swift project authors
// Licensed under Apache License v2.0 with Runtime Library Exception
//
// See https://swift.org/LICENSE.txt for license information
// See https://swift.org/CONTRIBUTORS.txt for the list of Swift project authors
//
//===----------------------------------------------------------------------===//
// A lock-free linked list concurrency test. `n` threads are each
// racing on a linked list containing `n` values; in each iteration,
// each thread unlinks its corresponding value then reinserts it at
// the end of the list.
import XCTest
import Atomics
import Dispatch
let nodeCount = ManagedAtomic<Int>(0)
private class List<Value: Equatable> {
class Node: AtomicReference {
private let _next: UnsafeAtomic<Node?>
private let _value: UnsafeAtomic<UnsafeMutablePointer<Value>?>
init(pointer: UnsafeMutablePointer<Value>?, next: Node? = nil) {
self._next = .create(next)
self._value = .create(pointer)
nodeCount.wrappingIncrement(ordering: .relaxed)
}
convenience init(_ value: Value?, next: Node? = nil) {
if let value = value {
let p = UnsafeMutablePointer<Value>.allocate(capacity: 1)
p.initialize(to: value)
self.init(pointer: p, next: next)
} else {
self.init(pointer: nil, next: next)
}
}
deinit {
// Prevent stack overflow when deinitializing a long chain
var node = self._next.destroy()
while node != nil && isKnownUniquelyReferenced(&node) {
let next = node!._next.exchange(nil, ordering: .relaxed)
withExtendedLifetime(node) {}
node = next
}
if let p = self._value.destroy() {
p.deinitialize(count: 1)
p.deallocate()
}
nodeCount.wrappingDecrement(ordering: .relaxed)
}
var value: Value? { _value.load(ordering: .sequentiallyConsistent)?.pointee }
func clearValue() -> UnsafeMutablePointer<Value>? {
withExtendedLifetime(self) {
_value.exchange(nil, ordering: .sequentiallyConsistent)
}
}
var next: Node? { _next.load(ordering: .sequentiallyConsistent) }
func clearNext() -> Node? {
withExtendedLifetime(self) {
_next.exchange(nil, ordering: .sequentiallyConsistent)
}
}
func compareExchangeNext(expected: Node?, desired: Node?) -> (exchanged: Bool, original: Node?) {
withExtendedLifetime(self) {
_next.compareExchange(expected: expected, desired: desired, ordering: .sequentiallyConsistent)
}
}
}
let head: Node
init<Elements: Collection>(from elements: Elements)
where Elements.Element == Value {
var n: Node? = nil
for value in elements.reversed() {
n = Node(value, next: n)
}
self.head = Node(nil, next: n)
}
deinit {
// Prevent a stack overflow while recursively releasing list nodes.
var node = head.clearNext()
while let n = node {
let next = n.clearNext()
node = next
}
}
}
extension List {
/// Move the given value to the end of this list. It is safe to call
/// this concurrently on multiple threads as long as all invocations
/// are using distinct values.
func sink(_ value: Value) {
var current = self.head
var next = self.head.next
var anchor = current
var anchorNext = next
var found = false
var new: Node?
while true {
if let n = next {
var v = n.value
if !found, v == value {
// Mark this node as deleted.
found = true
new = Node(pointer: n.clearValue(), next: nil)
precondition(new?.value == value, "Concurrent sink invocations with the same value")
v = nil
}
current = n
next = n.next
if v != nil {
if current !== anchor {
// Opportunistically unlink the chain of deleted nodes between `anchor` and `n`.
_ = anchor.compareExchangeNext(expected: anchorNext, desired: n)
}
anchor = current
anchorNext = next
}
} else {
// `next` is nil. Append `new` to the end of the list.
precondition(found, "Lost value \(value) in \(read())")
let (exchanged, original) = current.compareExchangeNext(expected: nil, desired: new)
if exchanged {
if current !== anchor {
// Opportunistically unlink the chain of deleted nodes between `anchor` and `new`.
_ = anchor.compareExchangeNext(expected: anchorNext, desired: new)
}
return
}
next = original
}
}
}
/// Read out and return the contents of this list in an array. Note
/// that this may return duplicate or missing elements if it is
/// called concurrently with `sink`. (This is still safe -- the
/// results may be useless, but the returned values are still
/// valid.)
@inline(never)
func read() -> [Value?] {
var result: [Value?] = []
var node = head
while let next = node.next {
result.append(next.value)
node = next
}
withExtendedLifetime(node) {}
return result
}
@inline(never)
func readUnique(expectedCount: Int = 0) -> Set<Value> where Value: Hashable {
var result: Set<Value> = []
result.reserveCapacity(expectedCount)
var node = head
while let next = node.next {
if let value = next.value {
result.insert(value)
}
node = next
}
withExtendedLifetime(node) {}
return result
}
}
class StrongReferenceShuffleTests: XCTestCase {
override func tearDown() {
XCTAssertEqual(nodeCount.load(ordering: .relaxed), 0)
}
func checkSink(
writers: Int,
readers: Int,
iterations: Int,
file: StaticString = #file,
line: UInt = #line
) {
let list = List<Int>(from: 0 ..< writers)
XCTAssertEqual(list.read(), Array(0 ..< writers),
"Unexpected list contents at start",
file: file, line: line)
DispatchQueue.concurrentPerform(iterations: readers + writers) { id in
if id < writers {
for _ in 0 ..< iterations {
list.sink(id)
}
} else {
for _ in 0 ..< iterations {
let elements = list.readUnique(expectedCount: writers)
precondition(elements.count <= writers)
}
}
}
let contents = list.read()
print(
contents
.map { value -> String in
if let value = value { return "\(value)" }
return "nil"
}
.joined(separator: ", "))
let values = Set(contents.compactMap { $0 })
XCTAssertEqual(values, Set(0 ..< writers),
"Unexpected list contents at end",
file: file, line: line)
}
private let iterations = 1_000_000
func test_sink_01_00() { checkSink(writers: 1, readers: 0, iterations: iterations) }
func test_sink_02_00() { checkSink(writers: 2, readers: 0, iterations: iterations) }
func test_sink_04_00() { checkSink(writers: 4, readers: 0, iterations: iterations) }
func test_sink_08_00() { checkSink(writers: 8, readers: 0, iterations: iterations) }
func test_sink_01_01() { checkSink(writers: 1, readers: 1, iterations: iterations) }
func test_sink_02_01() { checkSink(writers: 2, readers: 1, iterations: iterations) }
func test_sink_04_01() { checkSink(writers: 4, readers: 1, iterations: iterations) }
func test_sink_08_01() { checkSink(writers: 8, readers: 1, iterations: iterations) }
func test_sink_01_02() { checkSink(writers: 1, readers: 2, iterations: iterations) }
func test_sink_02_02() { checkSink(writers: 2, readers: 2, iterations: iterations) }
func test_sink_04_02() { checkSink(writers: 4, readers: 2, iterations: iterations) }
func test_sink_08_02() { checkSink(writers: 8, readers: 2, iterations: iterations) }
func test_sink_01_04() { checkSink(writers: 1, readers: 4, iterations: iterations) }
func test_sink_02_04() { checkSink(writers: 2, readers: 4, iterations: iterations) }
func test_sink_04_04() { checkSink(writers: 4, readers: 4, iterations: iterations) }
func test_sink_08_04() { checkSink(writers: 8, readers: 4, iterations: iterations) }
func test_sink_01_08() { checkSink(writers: 1, readers: 8, iterations: iterations) }
func test_sink_02_08() { checkSink(writers: 2, readers: 8, iterations: iterations) }
func test_sink_04_08() { checkSink(writers: 4, readers: 8, iterations: iterations) }
func test_sink_08_08() { checkSink(writers: 8, readers: 8, iterations: iterations) }
}
@@ -0,0 +1,40 @@
//===----------------------------------------------------------------------===//
//
// This source file is part of the Swift Atomics open source project
//
// Copyright (c) 2020 Apple Inc. and the Swift project authors
// Licensed under Apache License v2.0 with Runtime Library Exception
//
// See https://swift.org/LICENSE.txt for license information
// See https://swift.org/CONTRIBUTORS.txt for the list of Swift project authors
//
//===----------------------------------------------------------------------===//
import XCTest
import Atomics
class UnsafeAtomicLazyReferenceTests: XCTestCase {
func test_create_destroy() {
let v = UnsafeAtomicLazyReference<LifetimeTracked>.create()
defer { v.destroy() }
XCTAssertNil(v.load())
}
func test_storeIfNilThenLoad() {
do {
let v = UnsafeAtomicLazyReference<LifetimeTracked>.create()
XCTAssertNil(v.load())
let ref = LifetimeTracked(42)
XCTAssertTrue(v.storeIfNilThenLoad(ref) === ref)
XCTAssertTrue(v.load() === ref)
let ref2 = LifetimeTracked(23)
XCTAssertTrue(v.storeIfNilThenLoad(ref2) === ref)
XCTAssertTrue(v.load() === ref)
v.destroy()
}
XCTAssertEqual(LifetimeTracked.instances, 0)
}
}
File diff suppressed because it is too large. Load diff
File diff suppressed because it is too large. Load diff
+8
View File
@@ -0,0 +1,8 @@
import XCTest
import AtomicsTests
var tests = [XCTestCaseEntry]()
tests += AtomicsTests.__allTests()
XCTMain(tests)
+550
View File
@@ -0,0 +1,550 @@
//===----------------------------------------------------------------------===//
//
// This source file is part of the Swift Atomics open source project
//
// Copyright (c) 2020 Apple Inc. and the Swift project authors
// Licensed under Apache License v2.0 with Runtime Library Exception
//
// See https://swift.org/LICENSE.txt for license information
// See https://swift.org/CONTRIBUTORS.txt for the list of Swift project authors
//
//===----------------------------------------------------------------------===//
// RUN: %empty-directory(%t)
// RUN: %gyb %s > %t/AtomicsFolding.swift
//
// RUN: %target-swift-frontend -parse-as-library -emit-sil -Onone -module-name AtomicsFolding %t/AtomicsFolding.swift > %t/AtomicsFolding-Onone.sil
// RUN: %FileCheck --check-prefixes=SECTIONING,CHECK --input-file=%t/AtomicsFolding-Onone.sil %t/AtomicsFolding.swift
// RUN: %FileCheck --check-prefixes=SECTIONING,EXCLUSIONS --input-file=%t/AtomicsFolding-Onone.sil %t/AtomicsFolding.swift
//
// RUN: %target-swift-frontend -parse-as-library -emit-sil -O -module-name AtomicsFolding %t/AtomicsFolding.swift > %t/AtomicsFolding-O.sil
// RUN: %FileCheck --check-prefixes=SECTIONING,CHECK --input-file=%t/AtomicsFolding-O.sil %t/AtomicsFolding.swift
// RUN: %FileCheck --check-prefixes=SECTIONING,EXCLUSIONS --input-file=%t/AtomicsFolding-O.sil %t/AtomicsFolding.swift
//
// RUN: %target-swift-frontend -parse-as-library -emit-sil -Osize -module-name AtomicsFolding %t/AtomicsFolding.swift > %t/AtomicsFolding-Osize.sil
// RUN: %FileCheck --check-prefixes=SECTIONING,CHECK --input-file=%t/AtomicsFolding-Osize.sil %t/AtomicsFolding.swift
// RUN: %FileCheck --check-prefixes=SECTIONING,EXCLUSIONS --input-file=%t/AtomicsFolding-Osize.sil %t/AtomicsFolding.swift
// Tests that atomic operations get compiled to the expected builtins, and only
// the expected builtins. (I.e., that the switch statements over the orderings
// get constant folded away.)
%{
from gyb_utils import autogenerated_warning
integer_types = [
# Label Type Builtin
("Int", "Int", "Word"),
("Int64", "Int64", "Int64"),
("Int32", "Int32", "Int32"),
("Int16", "Int16", "Int16"),
("Int8", "Int8", "Int8"),
("UInt", "UInt", "Word"),
("UInt64", "UInt64", "Int64"),
("UInt32", "UInt32", "Int32"),
("UInt16", "UInt16", "Int16"),
("UInt8", "UInt8", "Int8"),
]
pointer_types = [
# Label Type Builtin
("UnsafeRawPointer", "UnsafeRawPointer", "Word"),
("UnsafeMutableRawPointer", "UnsafeMutableRawPointer", "Word"),
("UnsafePointer", "UnsafePointer<Foo>", "Word"),
("UnsafeMutablePointer", "UnsafeMutablePointer<Foo>", "Word"),
("Unmanaged", "Unmanaged<Bar>", "Word"),
("OptionalUnsafeRawPointer", "Optional<UnsafeRawPointer>", "Word"),
("OptionalUnsafeMutableRawPointer", "Optional<UnsafeMutableRawPointer>", "Word"),
("OptionalUnsafePointer", "Optional<UnsafePointer<Foo>>", "Word"),
("OptionalUnsafeMutablePointer", "Optional<UnsafeMutablePointer<Foo>>", "Word"),
("OptionalUnmanaged", "Optional<Unmanaged<Bar>>", "Word"),
]
all_types = integer_types + pointer_types
integer_operations = [
# API Name LLVM Op Arg Label
('loadThenWrappingIncrement', 'add', "by"),
('loadThenWrappingDecrement', 'sub', "by"),
('loadThenBitwiseAnd', 'and', "with"),
('loadThenBitwiseOr', 'or', "with"),
('loadThenBitwiseXor', 'xor', "with"),
('wrappingIncrementThenLoad', 'add', "by"),
('wrappingDecrementThenLoad', 'sub', "by"),
('bitwiseAndThenLoad', 'and', "with"),
('bitwiseOrThenLoad', 'or', "with"),
('bitwiseXorThenLoad', 'xor', "with"),
]
}%
${autogenerated_warning()}
import Atomics
public struct Foo {}
public class Bar {}
// -----------------------------------------------------------------------------
public struct AtomicFoldingTests {
% for (label, type, builtin) in all_types:
// SECTIONING-LABEL: // AtomicFoldingTests.load_${label}_relaxed(_:)
public func load_${label}_relaxed(
_ value: UnsafeAtomic<${type}>
) -> ${type} {
// CHECK: builtin "atomicload_monotonic_${builtin}"
// EXCLUSIONS-NOT: builtin "atomicload_acquire_${builtin}"
// EXCLUSIONS-NOT: builtin "atomicload_seqcst_${builtin}"
return value.load(ordering: .relaxed)
}
// SECTIONING-LABEL: } // end sil function '$s14AtomicsFolding
// SECTIONING-LABEL: // AtomicFoldingTests.load_${label}_acquiring(_:)
public func load_${label}_acquiring(
_ value: UnsafeAtomic<${type}>
) -> ${type} {
// EXCLUSIONS-NOT: builtin "atomicload_monotonic_${builtin}"
// CHECK: builtin "atomicload_acquire_${builtin}"
// EXCLUSIONS-NOT: builtin "atomicload_seqcst_${builtin}"
return value.load(ordering: .acquiring)
}
// SECTIONING-LABEL: } // end sil function '$s14AtomicsFolding
// SECTIONING-LABEL: // AtomicFoldingTests.load_${label}_sequential(_:)
public func load_${label}_sequential(
_ value: UnsafeAtomic<${type}>
) -> ${type} {
// EXCLUSIONS-NOT: builtin "atomicload_monotonic_${builtin}"
// EXCLUSIONS-NOT: builtin "atomicload_acquire_${builtin}"
// CHECK: builtin "atomicload_seqcst_${builtin}"
return value.load(ordering: .sequentiallyConsistent)
}
// SECTIONING-LABEL: } // end sil function '$s14AtomicsFolding
%end
// ---------------------------------------------------------------------------
% for (label, type, builtin) in all_types:
// SECTIONING-LABEL: // AtomicFoldingTests.store_${label}_relaxed(_:_:)
public func store_${label}_relaxed(
_ value: UnsafeAtomic<${type}>,
_ desired: ${type}
) {
// CHECK: builtin "atomicstore_monotonic_${builtin}"
// EXCLUSIONS-NOT: builtin "atomicstore_release_${builtin}"
// EXCLUSIONS-NOT: builtin "atomicstore_seqcst_${builtin}"
value.store(desired, ordering: .relaxed)
}
// SECTIONING-LABEL: } // end sil function '$s14AtomicsFolding
// SECTIONING-LABEL: // AtomicFoldingTests.store_${label}_releasing(_:_:)
public func store_${label}_releasing(
_ value: UnsafeAtomic<${type}>,
_ desired: ${type}
) {
// EXCLUSIONS-NOT: builtin "atomicstore_monotonic_${builtin}"
// CHECK: builtin "atomicstore_release_${builtin}"
// EXCLUSIONS-NOT: builtin "atomicstore_seqcst_${builtin}"
value.store(desired, ordering: .releasing)
}
// SECTIONING-LABEL: } // end sil function '$s14AtomicsFolding
// SECTIONING-LABEL: // AtomicFoldingTests.store_${label}_sequential(_:_:)
public func store_${label}_sequential(
_ value: UnsafeAtomic<${type}>,
_ desired: ${type}
) {
// EXCLUSIONS-NOT: builtin "atomicstore_monotonic_${builtin}"
// EXCLUSIONS-NOT: builtin "atomicstore_release_${builtin}"
// CHECK: builtin "atomicstore_seqcst_${builtin}"
value.store(desired, ordering: .sequentiallyConsistent)
}
// SECTIONING-LABEL: } // end sil function '$s14AtomicsFolding
%end
// ---------------------------------------------------------------------------
% for (label, type, builtin) in all_types:
// SECTIONING-LABEL: // AtomicFoldingTests.exchange_${label}_relaxed(_:_:)
public func exchange_${label}_relaxed(
_ value: UnsafeAtomic<${type}>,
_ desired: ${type}
) -> ${type} {
// CHECK: builtin "atomicrmw_xchg_monotonic_${builtin}"
// EXCLUSIONS-NOT: builtin "atomicrmw_xchg_acquire_${builtin}"
// EXCLUSIONS-NOT: builtin "atomicrmw_xchg_release_${builtin}"
// EXCLUSIONS-NOT: builtin "atomicrmw_xchg_acqrel_${builtin}"
// EXCLUSIONS-NOT: builtin "atomicrmw_xchg_seqcst_${builtin}"
return value.exchange(desired, ordering: .relaxed)
}
// SECTIONING-LABEL: } // end sil function '$s14AtomicsFolding
// SECTIONING-LABEL: // AtomicFoldingTests.exchange_${label}_acquiring(_:_:)
public func exchange_${label}_acquiring(
_ value: UnsafeAtomic<${type}>,
_ desired: ${type}
) -> ${type} {
// EXCLUSIONS-NOT: builtin "atomicrmw_xchg_monotonic_${builtin}"
// CHECK: builtin "atomicrmw_xchg_acquire_${builtin}"
// EXCLUSIONS-NOT: builtin "atomicrmw_xchg_release_${builtin}"
// EXCLUSIONS-NOT: builtin "atomicrmw_xchg_acqrel_${builtin}"
// EXCLUSIONS-NOT: builtin "atomicrmw_xchg_seqcst_${builtin}"
return value.exchange(desired, ordering: .acquiring)
}
// SECTIONING-LABEL: } // end sil function '$s14AtomicsFolding
// SECTIONING-LABEL: // AtomicFoldingTests.exchange_${label}_releasing(_:_:)
public func exchange_${label}_releasing(
_ value: UnsafeAtomic<${type}>,
_ desired: ${type}
) -> ${type} {
// EXCLUSIONS-NOT: builtin "atomicrmw_xchg_monotonic_${builtin}"
// EXCLUSIONS-NOT: builtin "atomicrmw_xchg_acquire_${builtin}"
// CHECK: builtin "atomicrmw_xchg_release_${builtin}"
// EXCLUSIONS-NOT: builtin "atomicrmw_xchg_acqrel_${builtin}"
// EXCLUSIONS-NOT: builtin "atomicrmw_xchg_seqcst_${builtin}"
return value.exchange(desired, ordering: .releasing)
}
// SECTIONING-LABEL: } // end sil function '$s14AtomicsFolding
// SECTIONING-LABEL: // AtomicFoldingTests.exchange_${label}_acquiringAndReleasing(_:_:)
public func exchange_${label}_acquiringAndReleasing(
_ value: UnsafeAtomic<${type}>,
_ desired: ${type}
) -> ${type} {
// EXCLUSIONS-NOT: builtin "atomicrmw_xchg_monotonic_${builtin}"
// EXCLUSIONS-NOT: builtin "atomicrmw_xchg_acquire_${builtin}"
// EXCLUSIONS-NOT: builtin "atomicrmw_xchg_release_${builtin}"
// CHECK: builtin "atomicrmw_xchg_acqrel_${builtin}"
// EXCLUSIONS-NOT: builtin "atomicrmw_xchg_seqcst_${builtin}"
return value.exchange(desired, ordering: .acquiringAndReleasing)
}
// SECTIONING-LABEL: } // end sil function '$s14AtomicsFolding
// SECTIONING-LABEL: // AtomicFoldingTests.exchange_${label}_sequential(_:_:)
public func exchange_${label}_sequential(
_ value: UnsafeAtomic<${type}>,
_ desired: ${type}
) -> ${type} {
// EXCLUSIONS-NOT: builtin "atomicrmw_xchg_monotonic_${builtin}"
// EXCLUSIONS-NOT: builtin "atomicrmw_xchg_acquire_${builtin}"
// EXCLUSIONS-NOT: builtin "atomicrmw_xchg_release_${builtin}"
// EXCLUSIONS-NOT: builtin "atomicrmw_xchg_acqrel_${builtin}"
// CHECK: builtin "atomicrmw_xchg_seqcst_${builtin}"
return value.exchange(desired, ordering: .sequentiallyConsistent)
}
// SECTIONING-LABEL: } // end sil function '$s14AtomicsFolding
%end
// ---------------------------------------------------------------------------
% for (label, type, builtin) in all_types:
// SECTIONING-LABEL: // AtomicFoldingTests.compareExchange_${label}_relaxed(_:_:_:)
public func compareExchange_${label}_relaxed(
_ value: UnsafeAtomic<${type}>,
_ expected: ${type},
_ desired: ${type}
) -> (exchanged: Bool, original: ${type}) {
// CHECK: builtin "cmpxchg_monotonic_monotonic_${builtin}"
// EXCLUSIONS-NOT: builtin "cmpxchg_acquire_{{[a-z0-9_]*}}_${builtin}"
// EXCLUSIONS-NOT: builtin "cmpxchg_release_{{[a-z0-9_]*}}_${builtin}"
// EXCLUSIONS-NOT: builtin "cmpxchg_acqrel_{{[a-z0-9_]*}}_${builtin}"
// EXCLUSIONS-NOT: builtin "cmpxchg_seqcst_{{[a-z0-9_]*}}_${builtin}"
return value.compareExchange(
expected: expected,
desired: desired,
ordering: .relaxed)
}
// SECTIONING-LABEL: } // end sil function '$s14AtomicsFolding
// SECTIONING-LABEL: // AtomicFoldingTests.compareExchange_${label}_acquiring(_:_:_:)
public func compareExchange_${label}_acquiring(
_ value: UnsafeAtomic<${type}>,
_ expected: ${type},
_ desired: ${type}
) -> (exchanged: Bool, original: ${type}) {
// EXCLUSIONS-NOT: builtin "cmpxchg_monotonic_{{[a-z0-9_]*}}_${builtin}"
// CHECK: builtin "cmpxchg_acquire_acquire_${builtin}"
// EXCLUSIONS-NOT: builtin "cmpxchg_release_{{[a-z0-9_]*}}_${builtin}"
// EXCLUSIONS-NOT: builtin "cmpxchg_acqrel_{{[a-z0-9_]*}}_${builtin}"
// EXCLUSIONS-NOT: builtin "cmpxchg_seqcst_{{[a-z0-9_]*}}_${builtin}"
return value.compareExchange(
expected: expected,
desired: desired,
ordering: .acquiring)
}
// SECTIONING-LABEL: } // end sil function '$s14AtomicsFolding
// SECTIONING-LABEL: // AtomicFoldingTests.compareExchange_${label}_releasing(_:_:_:)
public func compareExchange_${label}_releasing(
_ value: UnsafeAtomic<${type}>,
_ expected: ${type},
_ desired: ${type}
) -> (exchanged: Bool, original: ${type}) {
// EXCLUSIONS-NOT: builtin "cmpxchg_monotonic_{{[a-z0-9_]*}}_${builtin}"
// EXCLUSIONS-NOT: builtin "cmpxchg_acquire_{{[a-z0-9_]*}}_${builtin}"
// CHECK: builtin "cmpxchg_release_monotonic_${builtin}"
// EXCLUSIONS-NOT: builtin "cmpxchg_acqrel_{{[a-z0-9_]*}}_${builtin}"
// EXCLUSIONS-NOT: builtin "cmpxchg_seqcst_{{[a-z0-9_]*}}_${builtin}"
return value.compareExchange(
expected: expected,
desired: desired,
ordering: .releasing)
}
// SECTIONING-LABEL: } // end sil function '$s14AtomicsFolding
// SECTIONING-LABEL: // AtomicFoldingTests.compareExchange_${label}_acquiringAndReleasing(_:_:_:)
public func compareExchange_${label}_acquiringAndReleasing(
_ value: UnsafeAtomic<${type}>,
_ expected: ${type},
_ desired: ${type}
) -> (exchanged: Bool, original: ${type}) {
// EXCLUSIONS-NOT: builtin "cmpxchg_monotonic_{{[a-z0-9_]*}}_${builtin}"
// EXCLUSIONS-NOT: builtin "cmpxchg_acquire_{{[a-z0-9_]*}}_${builtin}"
// EXCLUSIONS-NOT: builtin "cmpxchg_release_{{[a-z0-9_]*}}_${builtin}"
// CHECK: builtin "cmpxchg_acqrel_acquire_${builtin}"
// EXCLUSIONS-NOT: builtin "cmpxchg_seqcst_{{[a-z0-9_]*}}_${builtin}"
return value.compareExchange(
expected: expected,
desired: desired,
ordering: .acquiringAndReleasing)
}
// SECTIONING-LABEL: } // end sil function '$s14AtomicsFolding
// SECTIONING-LABEL: // AtomicFoldingTests.compareExchange_${label}_sequential(_:_:_:)
public func compareExchange_${label}_sequential(
_ value: UnsafeAtomic<${type}>,
_ expected: ${type},
_ desired: ${type}
) -> (exchanged: Bool, original: ${type}) {
// EXCLUSIONS-NOT: builtin "cmpxchg_monotonic_{{[a-z0-9_]*}}_${builtin}"
// EXCLUSIONS-NOT: builtin "cmpxchg_acquire_{{[a-z0-9_]*}}_${builtin}"
// EXCLUSIONS-NOT: builtin "cmpxchg_release_{{[a-z0-9_]*}}_${builtin}"
// EXCLUSIONS-NOT: builtin "cmpxchg_acqrel_{{[a-z0-9_]*}}_${builtin}"
// CHECK: builtin "cmpxchg_seqcst_seqcst_${builtin}"
return value.compareExchange(
expected: expected,
desired: desired,
ordering: .sequentiallyConsistent)
}
// SECTIONING-LABEL: } // end sil function '$s14AtomicsFolding
% end
% for (operation, modifier) in [("compareExchange", ""), ("weakCompareExchange", "_weak")]:
// ---------------------------------------------------------------------------
// FIXME: There are 15 valid combinations of success/failure orderings;
// here we're only testing 6 of them.
% for (label, type, builtin) in all_types:
// SECTIONING-LABEL: // AtomicFoldingTests.compareExchange2_${label}_relaxed_relaxed${modifier}(_:_:_:)
public func compareExchange2_${label}_relaxed_relaxed${modifier}(
_ value: UnsafeAtomic<${type}>,
_ expected: ${type},
_ desired: ${type}
) -> (exchanged: Bool, original: ${type}) {
// CHECK: builtin "cmpxchg_monotonic_monotonic${modifier}_${builtin}"
// EXCLUSIONS-NOT: builtin "cmpxchg_monotonic_{{[^m][a-z0-9_]*}}${modifier}_${builtin}"
// EXCLUSIONS-NOT: builtin "cmpxchg_acquire_{{[a-z0-9_]*}}${modifier}_${builtin}"
// EXCLUSIONS-NOT: builtin "cmpxchg_release_{{[a-z0-9_]*}}${modifier}_${builtin}"
// EXCLUSIONS-NOT: builtin "cmpxchg_acqrel_{{[a-z0-9_]*}}${modifier}_${builtin}"
// EXCLUSIONS-NOT: builtin "cmpxchg_seqcst_{{[a-z0-9_]*}}${modifier}_${builtin}"
return value.${operation}(
expected: expected,
desired: desired,
successOrdering: .relaxed,
failureOrdering: .relaxed)
}
// SECTIONING-LABEL: } // end sil function '$s14AtomicsFolding
// SECTIONING-LABEL: // AtomicFoldingTests.compareExchange2_${label}_acquiring_relaxed${modifier}(_:_:_:)
public func compareExchange2_${label}_acquiring_relaxed${modifier}(
_ value: UnsafeAtomic<${type}>,
_ expected: ${type},
_ desired: ${type}
) -> (exchanged: Bool, original: ${type}) {
// CHECK: builtin "cmpxchg_acquire_monotonic${modifier}_${builtin}"
// EXCLUSIONS-NOT: builtin "cmpxchg_monotonic_{{[a-z0-9_]*}}${modifier}_${builtin}"
// EXCLUSIONS-NOT: builtin "cmpxchg_acquire_{{[^m][a-z0-9_]*}}${modifier}_${builtin}"
// EXCLUSIONS-NOT: builtin "cmpxchg_release_{{[a-z0-9_]*}}${modifier}_${builtin}"
// EXCLUSIONS-NOT: builtin "cmpxchg_acqrel_{{[a-z0-9_]*}}${modifier}_${builtin}"
// EXCLUSIONS-NOT: builtin "cmpxchg_seqcst_{{[a-z0-9_]*}}${modifier}_${builtin}"
return value.${operation}(
expected: expected,
desired: desired,
successOrdering: .acquiring,
failureOrdering: .relaxed)
}
// SECTIONING-LABEL: } // end sil function '$s14AtomicsFolding
// SECTIONING-LABEL: // AtomicFoldingTests.compareExchange2_${label}_acquiring_acquiring${modifier}(_:_:_:)
public func compareExchange2_${label}_acquiring_acquiring${modifier}(
_ value: UnsafeAtomic<${type}>,
_ expected: ${type},
_ desired: ${type}
) -> (exchanged: Bool, original: ${type}) {
// CHECK: builtin "cmpxchg_acquire_acquire${modifier}_${builtin}"
// EXCLUSIONS-NOT: builtin "cmpxchg_monotonic_{{[a-z0-9_]*}}${modifier}_${builtin}"
// EXCLUSIONS-NOT: builtin "cmpxchg_acquire_{{[^a][a-z0-9_]*}}${modifier}_${builtin}"
// EXCLUSIONS-NOT: builtin "cmpxchg_release_{{[a-z0-9_]*}}${modifier}_${builtin}"
// EXCLUSIONS-NOT: builtin "cmpxchg_acqrel_{{[a-z0-9_]*}}${modifier}_${builtin}"
// EXCLUSIONS-NOT: builtin "cmpxchg_seqcst_{{[a-z0-9_]*}}${modifier}_${builtin}"
return value.${operation}(
expected: expected,
desired: desired,
successOrdering: .acquiring,
failureOrdering: .acquiring)
}
// SECTIONING-LABEL: } // end sil function '$s14AtomicsFolding
// SECTIONING-LABEL: // AtomicFoldingTests.compareExchange2_${label}_sequential_acquiring${modifier}(_:_:_:)
public func compareExchange2_${label}_sequential_acquiring${modifier}(
_ value: UnsafeAtomic<${type}>,
_ expected: ${type},
_ desired: ${type}
) -> (exchanged: Bool, original: ${type}) {
// CHECK: builtin "cmpxchg_seqcst_acquire${modifier}_${builtin}"
// EXCLUSIONS-NOT: builtin "cmpxchg_monotonic_{{[a-z0-9_]*}}${modifier}_${builtin}"
// EXCLUSIONS-NOT: builtin "cmpxchg_acquire_{{[a-z0-9_]*}}${modifier}_${builtin}"
// EXCLUSIONS-NOT: builtin "cmpxchg_release_{{[a-z0-9_]*}}${modifier}_${builtin}"
// EXCLUSIONS-NOT: builtin "cmpxchg_acqrel_{{[a-z0-9_]*}}${modifier}_${builtin}"
// EXCLUSIONS-NOT: builtin "cmpxchg_seqcst_{{[^a][a-z0-9_]*}}${modifier}_${builtin}"
return value.${operation}(
expected: expected,
desired: desired,
successOrdering: .sequentiallyConsistent,
failureOrdering: .acquiring)
}
// SECTIONING-LABEL: } // end sil function '$s14AtomicsFolding
// SECTIONING-LABEL: // AtomicFoldingTests.compareExchange2_${label}_releasing_acquiring${modifier}(_:_:_:)
public func compareExchange2_${label}_releasing_acquiring${modifier}(
_ value: UnsafeAtomic<${type}>,
_ expected: ${type},
_ desired: ${type}
) -> (exchanged: Bool, original: ${type}) {
// FIXME: This should be checking for "cmpxchg_release_acquire", but we don't
// support arbitrary failure orderings yet, so internally we promote success
// orderings to be at least as strong as the failure ordering.
// CHECK: builtin "cmpxchg_acqrel_acquire${modifier}_${builtin}"
// EXCLUSIONS-NOT: builtin "cmpxchg_monotonic_{{[a-z0-9_]*}}${modifier}_${builtin}"
// EXCLUSIONS-NOT: builtin "cmpxchg_acquire_{{[a-z0-9_]*}}${modifier}_${builtin}"
// EXCLUSIONS-NOT: builtin "cmpxchg_release_{{[a-z0-9_]*}}${modifier}_${builtin}"
// EXCLUSIONS-NOT: builtin "cmpxchg_acqrel_{{[^a][a-z0-9_]*}}${modifier}_${builtin}"
// EXCLUSIONS-NOT: builtin "cmpxchg_seqcst_{{[a-z0-9_]*}}${modifier}_${builtin}"
return value.${operation}(
expected: expected,
desired: desired,
successOrdering: .releasing,
failureOrdering: .acquiring)
}
// SECTIONING-LABEL: } // end sil function '$s14AtomicsFolding
// SECTIONING-LABEL: // AtomicFoldingTests.compareExchange2_${label}_relaxed_sequential${modifier}(_:_:_:)
public func compareExchange2_${label}_relaxed_sequential${modifier}(
_ value: UnsafeAtomic<${type}>,
_ expected: ${type},
_ desired: ${type}
) -> (exchanged: Bool, original: ${type}) {
// FIXME: This should be checking for "cmpxchg_monotonic_seqcst", but we don't
// support arbitrary failure orderings yet, so internally we promote success
// orderings to be at least as strong as the failure ordering.
// CHECK: builtin "cmpxchg_seqcst_seqcst${modifier}_${builtin}"
// EXCLUSIONS-NOT: builtin "cmpxchg_monotonic_{{[a-z0-9_]*}}${modifier}_${builtin}"
// EXCLUSIONS-NOT: builtin "cmpxchg_acquire_{{[a-z0-9_]*}}${modifier}_${builtin}"
// EXCLUSIONS-NOT: builtin "cmpxchg_release_{{[a-z0-9_]*}}${modifier}_${builtin}"
// EXCLUSIONS-NOT: builtin "cmpxchg_acqrel_{{[a-z0-9_]*}}${modifier}_${builtin}"
// EXCLUSIONS-NOT: builtin "cmpxchg_seqcst_{{[^s][a-z0-9_]*}}${modifier}_${builtin}"
return value.${operation}(
expected: expected,
desired: desired,
successOrdering: .relaxed,
failureOrdering: .sequentiallyConsistent)
}
// SECTIONING-LABEL: } // end sil function '$s14AtomicsFolding
% end
% end
// ---------------------------------------------------------------------------
% for (label, type, builtin) in integer_types:
% for (operation, llvmOp, argLabel) in integer_operations:
// SECTIONING-LABEL: // AtomicFoldingTests.${operation}_${label}_relaxed(_:_:)
public func ${operation}_${label}_relaxed(
_ value: UnsafeAtomic<${type}>,
_ operand: ${type}
) -> ${type} {
// CHECK: builtin "atomicrmw_${llvmOp}_monotonic_${builtin}"
// EXCLUSIONS-NOT: builtin "atomicrmw_${llvmOp}_acquire_${builtin}"
// EXCLUSIONS-NOT: builtin "atomicrmw_${llvmOp}_release_${builtin}"
// EXCLUSIONS-NOT: builtin "atomicrmw_${llvmOp}_acqrel_${builtin}"
// EXCLUSIONS-NOT: builtin "atomicrmw_${llvmOp}_seqcst_${builtin}"
return value.${operation}(${argLabel}: operand, ordering: .relaxed)
}
// SECTIONING-LABEL: } // end sil function '$s14AtomicsFolding
// SECTIONING-LABEL: // AtomicFoldingTests.${operation}_${label}_acquiring(_:_:)
public func ${operation}_${label}_acquiring(
_ value: UnsafeAtomic<${type}>,
_ operand: ${type}
) -> ${type} {
// CHECK: builtin "atomicrmw_${llvmOp}_acquire_${builtin}"
// EXCLUSIONS-NOT: builtin "atomicrmw_${llvmOp}_monotonic_${builtin}"
// EXCLUSIONS-NOT: builtin "atomicrmw_${llvmOp}_release_${builtin}"
// EXCLUSIONS-NOT: builtin "atomicrmw_${llvmOp}_acqrel_${builtin}"
// EXCLUSIONS-NOT: builtin "atomicrmw_${llvmOp}_seqcst_${builtin}"
return value.${operation}(${argLabel}: operand, ordering: .acquiring)
}
// SECTIONING-LABEL: } // end sil function '$s14AtomicsFolding
// SECTIONING-LABEL: // AtomicFoldingTests.${operation}_${label}_releasing(_:_:)
public func ${operation}_${label}_releasing(
_ value: UnsafeAtomic<${type}>,
_ operand: ${type}
) -> ${type} {
// CHECK: builtin "atomicrmw_${llvmOp}_release_${builtin}"
// EXCLUSIONS-NOT: builtin "atomicrmw_${llvmOp}_monotonic_${builtin}"
// EXCLUSIONS-NOT: builtin "atomicrmw_${llvmOp}_acquire_${builtin}"
// EXCLUSIONS-NOT: builtin "atomicrmw_${llvmOp}_acqrel_${builtin}"
// EXCLUSIONS-NOT: builtin "atomicrmw_${llvmOp}_seqcst_${builtin}"
return value.${operation}(${argLabel}: operand, ordering: .releasing)
}
// SECTIONING-LABEL: } // end sil function '$s14AtomicsFolding
// SECTIONING-LABEL: // AtomicFoldingTests.${operation}_${label}_acquiringAndReleasing(_:_:)
public func ${operation}_${label}_acquiringAndReleasing(
_ value: UnsafeAtomic<${type}>,
_ operand: ${type}
) -> ${type} {
// CHECK: builtin "atomicrmw_${llvmOp}_acqrel_${builtin}"
// EXCLUSIONS-NOT: builtin "atomicrmw_${llvmOp}_monotonic_${builtin}"
// EXCLUSIONS-NOT: builtin "atomicrmw_${llvmOp}_acquire_${builtin}"
// EXCLUSIONS-NOT: builtin "atomicrmw_${llvmOp}_release_${builtin}"
// EXCLUSIONS-NOT: builtin "atomicrmw_${llvmOp}_seqcst_${builtin}"
return value.${operation}(
${argLabel}: operand,
ordering: .acquiringAndReleasing)
}
// SECTIONING-LABEL: } // end sil function '$s14AtomicsFolding
// SECTIONING-LABEL: // AtomicFoldingTests.${operation}_${label}_sequential(_:_:)
public func ${operation}_${label}_sequential(
_ value: UnsafeAtomic<${type}>,
_ operand: ${type}
) -> ${type} {
// CHECK: builtin "atomicrmw_${llvmOp}_seqcst_${builtin}"
// EXCLUSIONS-NOT: builtin "atomicrmw_${llvmOp}_monotonic_${builtin}"
// EXCLUSIONS-NOT: builtin "atomicrmw_${llvmOp}_acquire_${builtin}"
// EXCLUSIONS-NOT: builtin "atomicrmw_${llvmOp}_release_${builtin}"
// EXCLUSIONS-NOT: builtin "atomicrmw_${llvmOp}_acqrel_${builtin}"
return value.${operation}(
${argLabel}: operand,
ordering: .sequentiallyConsistent)
}
// SECTIONING-LABEL: } // end sil function '$s14AtomicsFolding
% end
% end
// ---------------------------------------------------------------------------
}
@@ -0,0 +1,170 @@
//===----------------------------------------------------------------------===//
//
// This source file is part of the Swift Atomics open source project
//
// Copyright (c) 2020 Apple Inc. and the Swift project authors
// Licensed under Apache License v2.0 with Runtime Library Exception
//
// See https://swift.org/LICENSE.txt for license information
// See https://swift.org/CONTRIBUTORS.txt for the list of Swift project authors
//
//===----------------------------------------------------------------------===//
// RUN: %empty-directory(%t)
// RUN: %gyb %s -o %t/UnsafeAtomicInitializers.swift
// RUN: %line-directive %t/UnsafeAtomicInitializers.swift -- %target-swift-frontend -typecheck -verify %t/UnsafeAtomicInitializers.swift
%{
from gyb_utils import autogenerated_warning
types = [
# id type initial
("Int", "Int", "0"),
("Int64", "Int64", "0"),
("Int32", "Int32", "0"),
("Int16", "Int16", "0"),
("Int8", "Int8", "0"),
("UInt", "UInt", "0"),
("UInt64", "UInt64", "0"),
("UInt32", "UInt32", "0"),
("UInt16", "UInt16", "0"),
("UInt8", "UInt8", "0"),
# id type initial
("URP", "UnsafeRawPointer", "UnsafeRawPointer(UnsafeMutableRawPointer.allocate(byteCount: 8, alignment: 8))"),
("UP", "UnsafePointer<Bar>", "UnsafePointer(UnsafeMutablePointer<Bar>.allocate(capacity: 1))"),
("UMRP", "UnsafeMutableRawPointer", ".allocate(byteCount: 8, alignment: 8)"),
("UMP", "UnsafeMutablePointer<Bar>", ".allocate(capacity: 1)"),
("Unmanaged", "Unmanaged<Foo>", "Unmanaged.passRetained(Foo())"),
# id type initial
("URPOpt", "UnsafeRawPointer?", "nil"),
("UPOpt", "UnsafePointer<Bar>?", "nil"),
("UMRPOpt", "UnsafeMutableRawPointer?", "nil"),
("UMPOpt", "UnsafeMutablePointer<Bar>?", "nil"),
("UnmanagedOpt", "Unmanaged<Foo>?", "nil"),
]
}%
${autogenerated_warning()}
import Atomics
class Foo {
var value = 0
}
struct Bar {
var value = 0
}
% for (id, type, initial) in types:
func test_${id}() -> UnsafeAtomic<${type}> {
var storage = UnsafeAtomic<${type}>.Storage(${initial})
let atomic = UnsafeAtomic<${type}>(at: &storage) // expected-warning {{inout expression creates a temporary pointer, but argument 'at' should be a pointer that outlives the call to 'init(at:)'}}
// expected-note@-1 {{implicit argument conversion from 'UnsafeAtomic<${type}>.Storage' to 'UnsafeMutablePointer<UnsafeAtomic<${type}>.Storage>' produces a pointer valid only for the duration of the call to 'init(at:)'}}
// expected-note@-2 {{use 'withUnsafeMutablePointer' in order to explicitly convert argument to pointer valid for a defined scope}}
return atomic
}
% end
func test_UnsafeAtomicLazyReference() -> UnsafeAtomicLazyReference<Foo> {
var value = UnsafeAtomicLazyReference<Foo>.Storage()
let atomic = UnsafeAtomicLazyReference(at: &value) // expected-warning {{inout expression creates a temporary pointer, but argument 'at' should be a pointer that outlives the call to 'init(at:)'}}
// expected-note@-1 {{implicit argument conversion from 'UnsafeAtomicLazyReference<Foo>.Storage' to 'UnsafeMutablePointer<UnsafeAtomicLazyReference<Foo>.Storage>' produces a pointer valid only for the duration of the call to 'init(at:)'}}
// expected-note@-2 {{use 'withUnsafeMutablePointer' in order to explicitly convert argument to pointer valid for a defined scope}}
return atomic
}
class BrokenAtomicCounter { // THIS IS BROKEN; DO NOT USE
private var _storage = UnsafeAtomic<Int>.Storage(0)
private var _value: UnsafeAtomic<Int>?
init() {
// This escapes the ephemeral pointer generated by the inout expression,
// so it leads to undefined behavior when the pointer gets dereferenced
// in the atomic operations below. DO NOT DO THIS.
_value = UnsafeAtomic(at: &_storage) // expected-warning {{inout expression creates a temporary pointer, but argument 'at' should be a pointer that outlives the call to 'init(at:)'}}
// expected-note@-1 {{implicit argument conversion from 'UnsafeAtomic<Int>.Storage' to 'UnsafeMutablePointer<UnsafeAtomic<Int>.Storage>' produces a pointer valid only for the duration of the call to 'init(at:)'}}
// expected-note@-2 {{use 'withUnsafeMutablePointer' in order to explicitly convert argument to pointer valid for a defined scope}}
}
func increment() {
_value!.wrappingIncrement(by: 1, ordering: .relaxed)
}
func get() -> Int {
_value!.load(ordering: .relaxed)
}
}
struct AtomicCounter {
typealias Value = Int
typealias Header = UnsafeAtomic<Value>.Storage
class Buffer: ManagedBuffer<Header, Void> {
deinit {
withUnsafeMutablePointerToHeader { header in
_ = header.pointee.dispose()
}
}
}
let buffer: Buffer
init() {
buffer = Buffer.create(minimumCapacity: 0) { _ in
Header(0)
} as! Buffer
}
private func _withAtomicPointer<R>(
_ body: (UnsafeAtomic<Int>) throws -> R
) rethrows -> R {
try buffer.withUnsafeMutablePointerToHeader { header in
try body(UnsafeAtomic<Int>(at: header))
}
}
func increment() {
_withAtomicPointer { $0.wrappingIncrement(ordering: .relaxed) }
}
func load() -> Int {
_withAtomicPointer { $0.load(ordering: .relaxed) }
}
}
struct AtomicUnmanagedRef<Instance: AnyObject> {
typealias Value = Unmanaged<Instance>?
typealias Header = UnsafeAtomic<Value>.Storage
class Buffer: ManagedBuffer<Header, Void> {
deinit {
withUnsafeMutablePointerToHeader { header in
_ = header.pointee.dispose()
}
}
}
let buffer: Buffer
init() {
buffer = Buffer.create(minimumCapacity: 0) { _ in
Header(nil)
} as! Buffer
}
private func _withAtomicPointer<R>(
_ body: (UnsafeAtomic<Value>) throws -> R
) rethrows -> R {
try buffer.withUnsafeMutablePointerToHeader { header in
try body(UnsafeAtomic<Value>(at: header))
}
}
func store(_ desired: Value) {
_withAtomicPointer { $0.store(desired, ordering: .sequentiallyConsistent) }
}
func load() -> Value {
_withAtomicPointer { $0.load(ordering: .sequentiallyConsistent) }
}
}
File diff suppressed because it is too large. Load diff
@@ -0,0 +1,276 @@
//===----------------------------------------------------------------------===//
//
// This source file is part of the Swift Atomics open source project
//
// Copyright (c) 2020 Apple Inc. and the Swift project authors
// Licensed under Apache License v2.0 with Runtime Library Exception
//
// See https://swift.org/LICENSE.txt for license information
// See https://swift.org/CONTRIBUTORS.txt for the list of Swift project authors
//
//===----------------------------------------------------------------------===//
// RUN: %empty-directory(%t)
// RUN: %gyb %s -o %t/UnsafeAtomicInitializers.swift
// RUN: %line-directive %t/UnsafeAtomicInitializers.swift -- %target-swift-frontend -typecheck -verify %t/UnsafeAtomicInitializers.swift
// #############################################################################
// # #
// # DO NOT EDIT THIS FILE; IT IS AUTOGENERATED. #
// # #
// #############################################################################
import Atomics
class Foo {
var value = 0
}
struct Bar {
var value = 0
}
func test_Int() -> UnsafeAtomic<Int> {
var storage = UnsafeAtomic<Int>.Storage(0)
let atomic = UnsafeAtomic<Int>(at: &storage) // expected-warning {{inout expression creates a temporary pointer, but argument 'at' should be a pointer that outlives the call to 'init(at:)'}}
// expected-note@-1 {{implicit argument conversion from 'UnsafeAtomic<Int>.Storage' to 'UnsafeMutablePointer<UnsafeAtomic<Int>.Storage>' produces a pointer valid only for the duration of the call to 'init(at:)'}}
// expected-note@-2 {{use 'withUnsafeMutablePointer' in order to explicitly convert argument to pointer valid for a defined scope}}
return atomic
}
func test_Int64() -> UnsafeAtomic<Int64> {
var storage = UnsafeAtomic<Int64>.Storage(0)
let atomic = UnsafeAtomic<Int64>(at: &storage) // expected-warning {{inout expression creates a temporary pointer, but argument 'at' should be a pointer that outlives the call to 'init(at:)'}}
// expected-note@-1 {{implicit argument conversion from 'UnsafeAtomic<Int64>.Storage' to 'UnsafeMutablePointer<UnsafeAtomic<Int64>.Storage>' produces a pointer valid only for the duration of the call to 'init(at:)'}}
// expected-note@-2 {{use 'withUnsafeMutablePointer' in order to explicitly convert argument to pointer valid for a defined scope}}
return atomic
}
func test_Int32() -> UnsafeAtomic<Int32> {
var storage = UnsafeAtomic<Int32>.Storage(0)
let atomic = UnsafeAtomic<Int32>(at: &storage) // expected-warning {{inout expression creates a temporary pointer, but argument 'at' should be a pointer that outlives the call to 'init(at:)'}}
// expected-note@-1 {{implicit argument conversion from 'UnsafeAtomic<Int32>.Storage' to 'UnsafeMutablePointer<UnsafeAtomic<Int32>.Storage>' produces a pointer valid only for the duration of the call to 'init(at:)'}}
// expected-note@-2 {{use 'withUnsafeMutablePointer' in order to explicitly convert argument to pointer valid for a defined scope}}
return atomic
}
func test_Int16() -> UnsafeAtomic<Int16> {
var storage = UnsafeAtomic<Int16>.Storage(0)
let atomic = UnsafeAtomic<Int16>(at: &storage) // expected-warning {{inout expression creates a temporary pointer, but argument 'at' should be a pointer that outlives the call to 'init(at:)'}}
// expected-note@-1 {{implicit argument conversion from 'UnsafeAtomic<Int16>.Storage' to 'UnsafeMutablePointer<UnsafeAtomic<Int16>.Storage>' produces a pointer valid only for the duration of the call to 'init(at:)'}}
// expected-note@-2 {{use 'withUnsafeMutablePointer' in order to explicitly convert argument to pointer valid for a defined scope}}
return atomic
}
func test_Int8() -> UnsafeAtomic<Int8> {
var storage = UnsafeAtomic<Int8>.Storage(0)
let atomic = UnsafeAtomic<Int8>(at: &storage) // expected-warning {{inout expression creates a temporary pointer, but argument 'at' should be a pointer that outlives the call to 'init(at:)'}}
// expected-note@-1 {{implicit argument conversion from 'UnsafeAtomic<Int8>.Storage' to 'UnsafeMutablePointer<UnsafeAtomic<Int8>.Storage>' produces a pointer valid only for the duration of the call to 'init(at:)'}}
// expected-note@-2 {{use 'withUnsafeMutablePointer' in order to explicitly convert argument to pointer valid for a defined scope}}
return atomic
}
func test_UInt() -> UnsafeAtomic<UInt> {
var storage = UnsafeAtomic<UInt>.Storage(0)
let atomic = UnsafeAtomic<UInt>(at: &storage) // expected-warning {{inout expression creates a temporary pointer, but argument 'at' should be a pointer that outlives the call to 'init(at:)'}}
// expected-note@-1 {{implicit argument conversion from 'UnsafeAtomic<UInt>.Storage' to 'UnsafeMutablePointer<UnsafeAtomic<UInt>.Storage>' produces a pointer valid only for the duration of the call to 'init(at:)'}}
// expected-note@-2 {{use 'withUnsafeMutablePointer' in order to explicitly convert argument to pointer valid for a defined scope}}
return atomic
}
func test_UInt64() -> UnsafeAtomic<UInt64> {
var storage = UnsafeAtomic<UInt64>.Storage(0)
let atomic = UnsafeAtomic<UInt64>(at: &storage) // expected-warning {{inout expression creates a temporary pointer, but argument 'at' should be a pointer that outlives the call to 'init(at:)'}}
// expected-note@-1 {{implicit argument conversion from 'UnsafeAtomic<UInt64>.Storage' to 'UnsafeMutablePointer<UnsafeAtomic<UInt64>.Storage>' produces a pointer valid only for the duration of the call to 'init(at:)'}}
// expected-note@-2 {{use 'withUnsafeMutablePointer' in order to explicitly convert argument to pointer valid for a defined scope}}
return atomic
}
func test_UInt32() -> UnsafeAtomic<UInt32> {
var storage = UnsafeAtomic<UInt32>.Storage(0)
let atomic = UnsafeAtomic<UInt32>(at: &storage) // expected-warning {{inout expression creates a temporary pointer, but argument 'at' should be a pointer that outlives the call to 'init(at:)'}}
// expected-note@-1 {{implicit argument conversion from 'UnsafeAtomic<UInt32>.Storage' to 'UnsafeMutablePointer<UnsafeAtomic<UInt32>.Storage>' produces a pointer valid only for the duration of the call to 'init(at:)'}}
// expected-note@-2 {{use 'withUnsafeMutablePointer' in order to explicitly convert argument to pointer valid for a defined scope}}
return atomic
}
func test_UInt16() -> UnsafeAtomic<UInt16> {
var storage = UnsafeAtomic<UInt16>.Storage(0)
let atomic = UnsafeAtomic<UInt16>(at: &storage) // expected-warning {{inout expression creates a temporary pointer, but argument 'at' should be a pointer that outlives the call to 'init(at:)'}}
// expected-note@-1 {{implicit argument conversion from 'UnsafeAtomic<UInt16>.Storage' to 'UnsafeMutablePointer<UnsafeAtomic<UInt16>.Storage>' produces a pointer valid only for the duration of the call to 'init(at:)'}}
// expected-note@-2 {{use 'withUnsafeMutablePointer' in order to explicitly convert argument to pointer valid for a defined scope}}
return atomic
}
func test_UInt8() -> UnsafeAtomic<UInt8> {
var storage = UnsafeAtomic<UInt8>.Storage(0)
let atomic = UnsafeAtomic<UInt8>(at: &storage) // expected-warning {{inout expression creates a temporary pointer, but argument 'at' should be a pointer that outlives the call to 'init(at:)'}}
// expected-note@-1 {{implicit argument conversion from 'UnsafeAtomic<UInt8>.Storage' to 'UnsafeMutablePointer<UnsafeAtomic<UInt8>.Storage>' produces a pointer valid only for the duration of the call to 'init(at:)'}}
// expected-note@-2 {{use 'withUnsafeMutablePointer' in order to explicitly convert argument to pointer valid for a defined scope}}
return atomic
}
func test_URP() -> UnsafeAtomic<UnsafeRawPointer> {
var storage = UnsafeAtomic<UnsafeRawPointer>.Storage(UnsafeRawPointer(UnsafeMutableRawPointer.allocate(byteCount: 8, alignment: 8)))
let atomic = UnsafeAtomic<UnsafeRawPointer>(at: &storage) // expected-warning {{inout expression creates a temporary pointer, but argument 'at' should be a pointer that outlives the call to 'init(at:)'}}
// expected-note@-1 {{implicit argument conversion from 'UnsafeAtomic<UnsafeRawPointer>.Storage' to 'UnsafeMutablePointer<UnsafeAtomic<UnsafeRawPointer>.Storage>' produces a pointer valid only for the duration of the call to 'init(at:)'}}
// expected-note@-2 {{use 'withUnsafeMutablePointer' in order to explicitly convert argument to pointer valid for a defined scope}}
return atomic
}
func test_UP() -> UnsafeAtomic<UnsafePointer<Bar>> {
var storage = UnsafeAtomic<UnsafePointer<Bar>>.Storage(UnsafePointer(UnsafeMutablePointer<Bar>.allocate(capacity: 1)))
let atomic = UnsafeAtomic<UnsafePointer<Bar>>(at: &storage) // expected-warning {{inout expression creates a temporary pointer, but argument 'at' should be a pointer that outlives the call to 'init(at:)'}}
// expected-note@-1 {{implicit argument conversion from 'UnsafeAtomic<UnsafePointer<Bar>>.Storage' to 'UnsafeMutablePointer<UnsafeAtomic<UnsafePointer<Bar>>.Storage>' produces a pointer valid only for the duration of the call to 'init(at:)'}}
// expected-note@-2 {{use 'withUnsafeMutablePointer' in order to explicitly convert argument to pointer valid for a defined scope}}
return atomic
}
func test_UMRP() -> UnsafeAtomic<UnsafeMutableRawPointer> {
var storage = UnsafeAtomic<UnsafeMutableRawPointer>.Storage(.allocate(byteCount: 8, alignment: 8))
let atomic = UnsafeAtomic<UnsafeMutableRawPointer>(at: &storage) // expected-warning {{inout expression creates a temporary pointer, but argument 'at' should be a pointer that outlives the call to 'init(at:)'}}
// expected-note@-1 {{implicit argument conversion from 'UnsafeAtomic<UnsafeMutableRawPointer>.Storage' to 'UnsafeMutablePointer<UnsafeAtomic<UnsafeMutableRawPointer>.Storage>' produces a pointer valid only for the duration of the call to 'init(at:)'}}
// expected-note@-2 {{use 'withUnsafeMutablePointer' in order to explicitly convert argument to pointer valid for a defined scope}}
return atomic
}
func test_UMP() -> UnsafeAtomic<UnsafeMutablePointer<Bar>> {
var storage = UnsafeAtomic<UnsafeMutablePointer<Bar>>.Storage(.allocate(capacity: 1))
let atomic = UnsafeAtomic<UnsafeMutablePointer<Bar>>(at: &storage) // expected-warning {{inout expression creates a temporary pointer, but argument 'at' should be a pointer that outlives the call to 'init(at:)'}}
// expected-note@-1 {{implicit argument conversion from 'UnsafeAtomic<UnsafeMutablePointer<Bar>>.Storage' to 'UnsafeMutablePointer<UnsafeAtomic<UnsafeMutablePointer<Bar>>.Storage>' produces a pointer valid only for the duration of the call to 'init(at:)'}}
// expected-note@-2 {{use 'withUnsafeMutablePointer' in order to explicitly convert argument to pointer valid for a defined scope}}
return atomic
}
func test_Unmanaged() -> UnsafeAtomic<Unmanaged<Foo>> {
var storage = UnsafeAtomic<Unmanaged<Foo>>.Storage(Unmanaged.passRetained(Foo()))
let atomic = UnsafeAtomic<Unmanaged<Foo>>(at: &storage) // expected-warning {{inout expression creates a temporary pointer, but argument 'at' should be a pointer that outlives the call to 'init(at:)'}}
// expected-note@-1 {{implicit argument conversion from 'UnsafeAtomic<Unmanaged<Foo>>.Storage' to 'UnsafeMutablePointer<UnsafeAtomic<Unmanaged<Foo>>.Storage>' produces a pointer valid only for the duration of the call to 'init(at:)'}}
// expected-note@-2 {{use 'withUnsafeMutablePointer' in order to explicitly convert argument to pointer valid for a defined scope}}
return atomic
}
func test_URPOpt() -> UnsafeAtomic<UnsafeRawPointer?> {
var storage = UnsafeAtomic<UnsafeRawPointer?>.Storage(nil)
let atomic = UnsafeAtomic<UnsafeRawPointer?>(at: &storage) // expected-warning {{inout expression creates a temporary pointer, but argument 'at' should be a pointer that outlives the call to 'init(at:)'}}
// expected-note@-1 {{implicit argument conversion from 'UnsafeAtomic<UnsafeRawPointer?>.Storage' to 'UnsafeMutablePointer<UnsafeAtomic<UnsafeRawPointer?>.Storage>' produces a pointer valid only for the duration of the call to 'init(at:)'}}
// expected-note@-2 {{use 'withUnsafeMutablePointer' in order to explicitly convert argument to pointer valid for a defined scope}}
return atomic
}
func test_UPOpt() -> UnsafeAtomic<UnsafePointer<Bar>?> {
var storage = UnsafeAtomic<UnsafePointer<Bar>?>.Storage(nil)
let atomic = UnsafeAtomic<UnsafePointer<Bar>?>(at: &storage) // expected-warning {{inout expression creates a temporary pointer, but argument 'at' should be a pointer that outlives the call to 'init(at:)'}}
// expected-note@-1 {{implicit argument conversion from 'UnsafeAtomic<UnsafePointer<Bar>?>.Storage' to 'UnsafeMutablePointer<UnsafeAtomic<UnsafePointer<Bar>?>.Storage>' produces a pointer valid only for the duration of the call to 'init(at:)'}}
// expected-note@-2 {{use 'withUnsafeMutablePointer' in order to explicitly convert argument to pointer valid for a defined scope}}
return atomic
}
func test_UMRPOpt() -> UnsafeAtomic<UnsafeMutableRawPointer?> {
var storage = UnsafeAtomic<UnsafeMutableRawPointer?>.Storage(nil)
let atomic = UnsafeAtomic<UnsafeMutableRawPointer?>(at: &storage) // expected-warning {{inout expression creates a temporary pointer, but argument 'at' should be a pointer that outlives the call to 'init(at:)'}}
// expected-note@-1 {{implicit argument conversion from 'UnsafeAtomic<UnsafeMutableRawPointer?>.Storage' to 'UnsafeMutablePointer<UnsafeAtomic<UnsafeMutableRawPointer?>.Storage>' produces a pointer valid only for the duration of the call to 'init(at:)'}}
// expected-note@-2 {{use 'withUnsafeMutablePointer' in order to explicitly convert argument to pointer valid for a defined scope}}
return atomic
}
func test_UMPOpt() -> UnsafeAtomic<UnsafeMutablePointer<Bar>?> {
var storage = UnsafeAtomic<UnsafeMutablePointer<Bar>?>.Storage(nil)
let atomic = UnsafeAtomic<UnsafeMutablePointer<Bar>?>(at: &storage) // expected-warning {{inout expression creates a temporary pointer, but argument 'at' should be a pointer that outlives the call to 'init(at:)'}}
// expected-note@-1 {{implicit argument conversion from 'UnsafeAtomic<UnsafeMutablePointer<Bar>?>.Storage' to 'UnsafeMutablePointer<UnsafeAtomic<UnsafeMutablePointer<Bar>?>.Storage>' produces a pointer valid only for the duration of the call to 'init(at:)'}}
// expected-note@-2 {{use 'withUnsafeMutablePointer' in order to explicitly convert argument to pointer valid for a defined scope}}
return atomic
}
func test_UnmanagedOpt() -> UnsafeAtomic<Unmanaged<Foo>?> {
var storage = UnsafeAtomic<Unmanaged<Foo>?>.Storage(nil)
let atomic = UnsafeAtomic<Unmanaged<Foo>?>(at: &storage) // expected-warning {{inout expression creates a temporary pointer, but argument 'at' should be a pointer that outlives the call to 'init(at:)'}}
// expected-note@-1 {{implicit argument conversion from 'UnsafeAtomic<Unmanaged<Foo>?>.Storage' to 'UnsafeMutablePointer<UnsafeAtomic<Unmanaged<Foo>?>.Storage>' produces a pointer valid only for the duration of the call to 'init(at:)'}}
// expected-note@-2 {{use 'withUnsafeMutablePointer' in order to explicitly convert argument to pointer valid for a defined scope}}
return atomic
}
func test_UnsafeAtomicLazyReference() -> UnsafeAtomicLazyReference<Foo> {
var value = UnsafeAtomicLazyReference<Foo>.Storage()
let atomic = UnsafeAtomicLazyReference(at: &value) // expected-warning {{inout expression creates a temporary pointer, but argument 'at' should be a pointer that outlives the call to 'init(at:)'}}
// expected-note@-1 {{implicit argument conversion from 'UnsafeAtomicLazyReference<Foo>.Storage' to 'UnsafeMutablePointer<UnsafeAtomicLazyReference<Foo>.Storage>' produces a pointer valid only for the duration of the call to 'init(at:)'}}
// expected-note@-2 {{use 'withUnsafeMutablePointer' in order to explicitly convert argument to pointer valid for a defined scope}}
return atomic
}
class BrokenAtomicCounter { // THIS IS BROKEN; DO NOT USE
private var _storage = UnsafeAtomic<Int>.Storage(0)
private var _value: UnsafeAtomic<Int>?
init() {
// This escapes the ephemeral pointer generated by the inout expression,
// so it leads to undefined behavior when the pointer gets dereferenced
// in the atomic operations below. DO NOT DO THIS.
_value = UnsafeAtomic(at: &_storage) // expected-warning {{inout expression creates a temporary pointer, but argument 'at' should be a pointer that outlives the call to 'init(at:)'}}
// expected-note@-1 {{implicit argument conversion from 'UnsafeAtomic<Int>.Storage' to 'UnsafeMutablePointer<UnsafeAtomic<Int>.Storage>' produces a pointer valid only for the duration of the call to 'init(at:)'}}
// expected-note@-2 {{use 'withUnsafeMutablePointer' in order to explicitly convert argument to pointer valid for a defined scope}}
}
func increment() {
_value!.wrappingIncrement(by: 1, ordering: .relaxed)
}
func get() -> Int {
_value!.load(ordering: .relaxed)
}
}
struct AtomicCounter {
typealias Value = Int
typealias Header = UnsafeAtomic<Value>.Storage
class Buffer: ManagedBuffer<Header, Void> {
deinit {
withUnsafeMutablePointerToHeader { header in
_ = header.pointee.dispose()
}
}
}
let buffer: Buffer
init() {
buffer = Buffer.create(minimumCapacity: 0) { _ in
Header(0)
} as! Buffer
}
private func _withAtomicPointer<R>(
_ body: (UnsafeAtomic<Int>) throws -> R
) rethrows -> R {
try buffer.withUnsafeMutablePointerToHeader { header in
try body(UnsafeAtomic<Int>(at: header))
}
}
func increment() {
_withAtomicPointer { $0.wrappingIncrement(ordering: .relaxed) }
}
func load() -> Int {
_withAtomicPointer { $0.load(ordering: .relaxed) }
}
}
struct AtomicUnmanagedRef<Instance: AnyObject> {
typealias Value = Unmanaged<Instance>?
typealias Header = UnsafeAtomic<Value>.Storage
class Buffer: ManagedBuffer<Header, Void> {
deinit {
withUnsafeMutablePointerToHeader { header in
_ = header.pointee.dispose()
}
}
}
let buffer: Buffer
init() {
buffer = Buffer.create(minimumCapacity: 0) { _ in
Header(nil)
} as! Buffer
}
private func _withAtomicPointer<R>(
_ body: (UnsafeAtomic<Value>) throws -> R
) rethrows -> R {
try buffer.withUnsafeMutablePointerToHeader { header in
try body(UnsafeAtomic<Value>(at: header))
}
}
func store(_ desired: Value) {
_withAtomicPointer { $0.store(desired, ordering: .sequentiallyConsistent) }
}
func load() -> Value {
_withAtomicPointer { $0.load(ordering: .sequentiallyConsistent) }
}
}
Executable
+3
View File
@@ -0,0 +1,3 @@
#!/usr/bin/env python2.7
import gyb
gyb.main()
+1260
View File
File diff suppressed because it is too large. Load diff
+85
View File
@@ -0,0 +1,85 @@
#===-----------------------------------------------------------------------===//
#
# This source file is part of the Swift.org open source project
#
# Copyright (c) 2020 Apple Inc. and the Swift project authors
# Licensed under Apache License v2.0 with Runtime Library Exception
#
# See https://swift.org/LICENSE.txt for license information
# See https://swift.org/CONTRIBUTORS.txt for the list of Swift project authors
#
#===-----------------------------------------------------------------------===//
def autogenerated_warning():
return """
// #############################################################################
// # #
// # DO NOT EDIT THIS FILE; IT IS AUTOGENERATED. #
// # #
// #############################################################################
"""
loadOrderings = [
# Swift enum case, C order
('relaxed', 'relaxed'),
('acquiring', 'acquire'),
('sequentiallyConsistent', 'seq_cst'),
]
storeOrderings = [
# Swift enum case, C order
('relaxed', 'relaxed'),
('releasing', 'release'),
('sequentiallyConsistent', 'seq_cst'),
]
updateOrderings = [
# Swift enum case, C order fail order
('relaxed', 'relaxed', 'relaxed'),
('acquiring', 'acquire', 'acquire'),
('releasing', 'release', 'relaxed'),
('acquiringAndReleasing', 'acq_rel', 'acquire'),
('sequentiallyConsistent', 'seq_cst', 'seq_cst'),
]
# stdatomic.h (and LLVM underneath) doesn't support arbitrary ordering
# combinations yet, so for the two-ordering cmpxchg variants we need
# to upgrade the success ordering when necessary so that it is at
# least as "strong" as the failure case. This function implements that
# mapping.
#
# See llvm/Support/AtomicOrdering.h
def shimOrders(success, failure):
def max(success, failure):
if failure == "acquire":
if success == "relaxed":
return "acquire"
if success == "release":
return "acq_rel"
if failure == "seq_cst":
return "seq_cst"
return success
return max(success, failure) + "_" + failure
integerOperations = [
# Swift name, llvm name, operator, label, doc
('WrappingIncrement', 'add', '&+', "by", "wrapping add"),
('WrappingDecrement', 'sub', '&-', "by", "wrapping subtract"),
('BitwiseAnd', 'and', '&', "with", "bitwise AND"),
('BitwiseOr', 'or', '|', "with", "bitwise OR"),
('BitwiseXor', 'xor', '^', "with", "bitwise XOR")
]
boolOperations = [
# Swift name, C name, operator, label, doc
('LogicalAnd', 'and', '&&', "with", "logical AND"),
('LogicalOr', 'or', '||', "with", "logical OR"),
('LogicalXor', 'xor', '!=', "with", "logical XOR")
]
def lowerFirst(str):
return str[:1].lower() + str[1:] if str else ""
def argLabel(label):
return label + ": " if label <> "_" else ""
+72
View File
@@ -0,0 +1,72 @@
#!/bin/sh
#===----------------------------------------------------------------------===//
#
# This source file is part of the Swift.org open source project
#
# Copyright (c) 2020 Apple Inc. and the Swift project authors
# Licensed under Apache License v2.0 with Runtime Library Exception
#
# See https://swift.org/LICENSE.txt for license information
# See https://swift.org/CONTRIBUTORS.txt for the list of Swift project authors
#
#===----------------------------------------------------------------------===//
set -eu
srcroot="$(dirname "$0")"
cd "$srcroot"
gyb="./Utilities/gyb"
# Disable line directives in gyb output. We commit generated sources
# into the package repository, so we do not want absolute file names
# in them.
lineDirective=''
# Uncomment the following line to enable #sourceLocation directives.
# This is useful for local development.
#lineDirective='#sourceLocation(file: "%(file)s", line: %(line)d)'
echo "Running gyb..."
# Create a temporary directory; remove it on exit.
tmpdir="$(mktemp -d -t "$(basename "$0")")"
trap "rm -rf \"$tmpdir\"" EXIT
# Run gyb on each gyb file in the source tree and put results in
# subdirectories named 'autogenerated'.
for input in ./Sources/*/*.gyb ./Tests/*/*.gyb; do
basename="$(basename "$input")"
targetdir="$(dirname "$input")/autogenerated"
output="$targetdir/"${basename%.gyb}
tmpfile="$tmpdir/${basename%.gyb}"
# Make sure the output directory exists.
mkdir -p "$targetdir"
# Run gyb, making sure to only update files when they change.
"$gyb" --line-directive "$lineDirective" -o "$tmpfile" "$input"
if [ -e "$output" ] && cmp -s "$tmpfile" "$output"; then
: Ignore unchanged file
else
echo "Updated $output"
cp "$tmpfile" "$output"
fi
echo "$output" >> "$tmpdir/generated-files.txt"
done
# Warn user if there are any autogenerated files without a corresponding gyb.
find . -path '*/autogenerated/*.swift' >> "$tmpdir/generated-files.txt"
sort "$tmpdir/generated-files.txt" | uniq -u | while read obsolete; do
echo "Removing $obsolete"
rm "$obsolete"
done
# Generate XCTest test registry for platforms without the Objective-C runtime.
if [ "$(uname)" = Darwin ]; then
echo "Registering tests..."
swift test --generate-linuxmain
fi
echo "All done!"