You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
SE-0258: property wrappers, revise the Atomic example.
Use 'class' rather than 'struct' because there is no way to make a
struct property atomic from within the wrapper.
TSAN will correctly report an error if you use a struct Atomic
wrapper. Some developers have been baffled by the TSAN failure, which
actually correctly identifies the bug.
Fixes rdar://79173755 (TSAN violation on standard Atomic property wrappers)
Copy file name to clipboardExpand all lines: proposals/0258-property-wrappers.md
+18-6Lines changed: 18 additions & 6 deletions
Original file line number
Diff line number
Diff line change
@@ -451,12 +451,12 @@ This implementation would address the problem detailed in
451
451
452
452
### `Atomic`
453
453
454
-
Support for atomic operations (load, store, increment/decrement, compare-and-exchange) is a commonly-requested Swift feature. While the implementation details for such a feature would involve compiler and standard library magic, the interface itself can be nicely expressed as a property wrapper type:
454
+
Support for atomic operations (load, store, increment/decrement, compare-and-exchange) is a commonly-requested Swift feature. While the implementation details for such a feature would involve compiler and standard library magic, the interface itself can be expressed as a property wrapper reference type:
The Atomic property wrapper is class rather than a struct because the
489
+
memory guarded by synchronization primitives must be independent from
490
+
the wrapper. The wrapper's value will be loaded before the call to
491
+
`wrappedValue.getter` and written back after the call to
492
+
`wrappedValue.setter`. Therefore, synchronization within the wrapper
493
+
cannot provide atomic access to its own value. However, when the
494
+
wrapped value is a separate stored property within the wrapper object,
495
+
then it is accessed independently, only after calling the
496
+
wrappedValue's getter and setter. Note that a class type property
497
+
wrapper gives the wrapped value reference semantics. All copies of the
498
+
parent object will share the same atomic value.
499
+
488
500
Here are some simple uses of `Atomic`. With atomic types, it's fairly common
489
501
to weave lower-level atomic operations (`increment`, `load`, `compareAndExchange`) where we need specific semantics (such as memory ordering) with simple queries, so both the property and the synthesized storage property are used often:
0 commit comments