プロトコル


AtomicRepresentable


個別のアトミックス記憶領域表現を通じてアトミック操作をサポートする型。


iOS 18.0+ iPadOS 18.0+ Mac Catalyst 18.0+

macOS 15.0+ tvOS 18.0+ watchOS 2.0+ visionOS 11.0+

protocol AtomicRepresentable





概観


AtomicRepresentable プロトコルに準拠する型は、Atomic 型の Value 型パラメータとして使用できます。既存のアトミック記憶領域表現を独自の表現として利用する準拠では、Atomic で使用できる基本的なアトミック操作が自由に提供されます。このような操作には、load、store、exchange、compareExchange、weakCompareExchange などがあります。


AtomicRepresentable プロトコルに準拠


独自のカスタム型を準拠させると、それを Atomic 型で使用し、上で説明したすべての基本的なアトミック操作にアクセスできるようになります。あなたの型を AtomicRepresentable に準拠させるには、主に 2 つの方法があります。


  1. 定義済みの RawRepresentable 準拠の使用

  2. AtomicRepresentable へのマニュアルでの準拠

あなたがカスタム型をすでに RawRepresentable に準拠している場合は、AtomicRepresentable 準拠を追加するのは非常に簡単です。あなたの型の RawValue 関連型自体がすでに AtomicRepresentable である場合は、あなたがすべき事は準拠を追加するだけで終わりです。


  1. enum TrafficLight: UInt8 {
  2. case red
  3. case yellow
  4. case green
  5. }
  6. extension TrafficLight: AtomicRepresentable {}

これだけです!ここでは、Swift の自動 RawRepresentable 準拠合成を列挙型に利用し、"生の値" を UInt8 として宣言しています。AtomicRepresentable 準拠を追加することで、RawRepresentable 実装から準拠を行う方法を自動的に判断し、必要な作業をすべて実行します。ただし、以下で説明するマニュアルのメソッドを使用して、この動作をカスタマイズすることは可能です。


あなた独自の AtomicRepresentable 準拠を定義するのは非常に簡単です。必要なのは、自分の型に最適なアトミック記憶領域表現を決定し、二つの間の双方向関係を作成することだけです。


  1. // A point in an x-y coordinate system.
  2. struct GridPoint {
  3.   var x: Int
  4.   var y: Int
  5. }
  6. extension GridPoint: AtomicRepresentable {
  7.   typealias AtomicRepresentation = WordPair.AtomicRepresentation
  8.   font color="fuchsia">static func encodeAtomicRepresentation(
  9. _ value: consuming GridPoint
  10.   ) -> AtomicRepresentation {
  11. let wordPair = WordPair(
  12. first: UInt(bitPattern: value.x),
  13. second: UInt(bitPattern: value.y)
  14. )
  15. return WordPair.encodeAtomicRepresentation(wordPair)
  16.   }
  17.   static func decodeAtomicRepresentation(
  18. _ representation: consuming AtomicRepresentation
  19.   ) -> GridPoint {
  20. let wordPair = WordPair.decodeAtomicRepresentation(representation)
  21. return GridPoint(
  22. x: Int(bitPattern: wordPair.first),
  23. y: Int(bitPattern: wordPair.second)
  24. )
  25.   }
  26. }

ここでは、WordPair のアトミック記憶領域表現を私たち独自のものとして選択します。これは非常に重要です。なぜなら、私たちの表現が 基本的な 記憶領域表現の 1 つである場合に限り、load や store などのアトミック操作が実行されるからです。幸いなことに、WordPair はこれらのタイプの 1 つを記憶領域の型として使用します。


私たちの型が使用する記憶領域表現を選択することに加えて、カスタムの型からその表現へ、そして表現から私たちの型へ戻る 2 つの静的関数を定義します。私たちの表現は WordPair.AtomicRepresentation と同じなので、実際に WordPair の AtomicRepresentable 準拠を調べて、私たち独自の表現を定義します。


あなたのカスタムの型を AtomicRepresentable プロトコルに準拠させるために必要なことはこれだけです。ここから、以下に示すように、この型をすべての基本的なアトミック操作で使用できます。


  1. func atomicGridPoint(_ gridPoint: Atomic<GridPoint>) {
  2.   let newGridPoint = GridPoint(x: 123, y: -456)
  3.   let oldGridPoint1 = gridPoint.load(ordering: .relaxed)
  4.   gridPoint.store(newGridPoint, ordering: .releasing)
  5.   let oldGridPoint2 = gridPoint.exchange(
  6. desired: oldGridPoint1,
  7. ordering: .acquiringAndReleasing
  8.   )
  9.   let (exchanged1, oldGridPoint2) = gridPoint.compareExchange(
  10. expected: oldGridPoint1,
  11. desired: newGridPoint,
  12. ordering: .sequentiallyConsistent
  13.   )
  14.   let (exchanged2, oldGridPoint3) = gridPoint.weakCompareExchange(
  15. expected: newGridPoint,
  16. desired: oldGridPoint2,
  17. ordering: .relaxed
  18.   )
  19. }

基本的な Atomic 表現のリスト


あなた独自の AtomicRepresentable 準拠を定義する場合、あなたのカスタムの型は独自の AtomicRepresentation として以下の型のリストから選択することが非常に重要です。


  • UInt8.AtomicRepresentation

  • UInt16.AtomicRepresentation

  • UInt32.AtomicRepresentation

  • UInt64.AtomicRepresentation

  • UInt.AtomicRepresentation

  • Int8.AtomicRepresentation

  • Int16.AtomicRepresentation

  • Int32.AtomicRepresentation

  • Int64.AtomicRepresentation

  • Int.AtomicRepresentation

  • WordPair.AtomicRepresentation

  • 注意

    Int8.AtomicRepresentation は UInt8.AtomicRepresentation と同じ型であり、これは同じサイズの整数型すべてに当てはまります。あなたの型が符号なし整数を包み込む場合は、符号付き整数のアトミック表現ではなく、符号なし整数のアトミック表現を使用することをお勧めします。逆の場合も同様です。Int と UInt の表現は、64 ビットシステムでは 64 ビット幅、32 ビットシステムでは 32 ビット幅になります。Int64 と UInt64 は、64 ビットシステムでは常に AtomicRepresentable に準拠しますが、32 ビットシステムでは、プラットフォームが倍幅のアトミックをサポートしている場合にのみ準拠します。WordPair は、倍幅のアトミックをサポートするプラットフォームでのみ AtomicRepresentable に準拠しますが、サポートしている場合は、64 ビットシステムでは 128 ビット幅、32 ビットシステムでは 64 ビット幅になります。




    トピックス


    関連する型


    associatedtype AtomicRepresentation : BitwiseCopyable

    Self がコード化および復号する記憶領域表現型は、アトミック操作で使用する場合に適した型です。

    必須



    型メソッド


    static func decodeAtomicRepresentation(consuming Self.AtomicRepresentation) -> Self

    アトミック操作から返された AtomicRepresentation 記憶領域インスタンスを破棄することにより、論理アトミック型の Self を回復します。

    必須


    static func encodeAtomicRepresentation(consuming Self) -> Self.AtomicRepresentation

    Self の値を破棄し、アトミック操作に使用する AtomicRepresentation 記憶領域型を準備します。

    必須





    関連


    以下により継承


    AtomicOptionalRepresentation



    準拠する型


    Bool

    Double

    Duration

    Float

    Float16

    Int

    Int128

    Int16

    Int32

    Int64

    Int8

    Never

    ObjectIdentifier

    OpaquePointer

    Optional

    Wrapped が AtomicOptionalRepresentable に準拠している場合は準拠します。


    UInt

    UInt128

    UInt16

    UInt32

    UInt64

    UInt8

    Unmanaged

    Instance が Copyable および Escapable に準拠している場合に準拠します。


    UnsafeBufferPointer

    Element が Escapable に準拠している場合に準拠します。


    UnsafeMutableBufferPointer

    Element が Escapable に準拠している場合に準拠します。


    UnsafeMutablePointer

    Pointee が Escapable に準拠する場合に準拠します。


    UnsafeMutableRawBufferPointer

    UnsafeMutableRawPointer

    UnsafePointer

    Pointee が Escapable に準拠する場合に準拠します。


    UnsafeRawBufferPointer

    UnsafeRawPointer

    WordPair





    以下も見よ


    アトミックな値


    struct Atomic

    アトミックな値。


    struct AtomicLazyReference

    遅延して初期化可能なアトミックな強い参照。


    struct WordPair

    2 つの word サイズの UInt のペア。


    protocol AtomicOptionalRepresentable

    Optional に包み込まれたときにアトミック操作もサポートするアトミック値。アトミックなオプション表現可能な型には、オプションで包み込まれたバリアントのスタンドアロンのアトミック表現が付属しています。














    トップへ












    トップへ












    トップへ












    トップへ












    トップへ












    トップへ












    トップへ