Ada 202x (32日目) - Atomic ========================== .. post:: Jan 01, 2020 :tags: ada, ada_2022 .. |ZWSP| unicode:: U+200B .. ZERO WIDTH SPACE :trim: 終わらない……。 Atomicへの部分アクセス ---------------------- Ada 202xでは ``Atomic`` (または ``Volatile``)ではない型が ``Atomic`` (または ``Volatile``)の型の要素になっていた場合、アクセスはアトミックに行われることが確定しました。 従来は曖昧だった部分です。 .. code-block:: ada declare type Unsigned_4 is mod 2 ** 4; -- not atomic type type T is -- atomic type record A, B, C, D : Unsigned_4; end record with Atomic, Pack; X : T; Y : Unsigned_4; begin Y := X.A; -- atomic genericの引数 ------------- Ada 202xでは ``generic`` の引数の型に ``Atomic`` 、 ``Atomic_Components`` 、 ``Volatile`` 、 ``Volatile_Components`` 、 ``Independent`` 、 ``Independent_Components`` といった同期関係のアスペクトが指定できるようになりました。 仮引数にこれらが指定されていた場合には実引数にも同じものが指定されている型を渡さなければなりません。 .. code-block:: ada generic type T is (<>) with Atomic; package Pkg1 is -- 以降省略 Atomic操作を行うライブラリ -------------------------- 確かに型に ``Atomic`` アスペクトを付けるだけで読み書き自体はアトミックにはなるのですが、実際のロックフリーなアルゴリズムでは読み書き分岐等をセットにした操作が必要になります。 従来はコンパイラの ``Intrinsic`` 関数が用いられていました。 gccであれば ``__sync_*`` や ``__atomic_*`` のビルトイン関数群が利用できます。 | https://gcc.gnu.org/onlinedocs/gcc/_005f_005fsync-Builtins.html | https://gcc.gnu.org/onlinedocs/gcc/_005f_005fatomic-Builtins.html 当然これらは処理系ごとにバラバラです。 Ada 202xではそのためのライブラリが追加されました。 System.Atomic_Operations ++++++++++++++++++++++++ ライブラリの親となる空のパッケージです。 | http://www.ada-auth.org/standards/2xrm/html/RM-C-6-1.html#p3 System.Atomic_Operations.Integer_Arithmetic |ZWSP| /Modular_Arithmetic ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ 加減算を行い演算前の値を返す関数や演算後の値を返す関数群です。 gccの ``__atomic_fetch_add`` や ``__atomic_add_fetch`` 等に相当します。 ``generic`` パッケージになっていて引数に操作対象のAtomic型を取ります。 | http://www.ada-auth.org/standards/2xrm/html/RM-C-6-4.html#p3 System.Atomic_Operations.Exchange +++++++++++++++++++++++++++++++++ 所謂swapやcompare-and-swapです。 gccの ``__atomic_exchange`` や ``__atomic_compare_exchange`` 等に相当します。 ``generic`` パッケージになっていて引数に操作対象のAtomic型を取ります。 | http://www.ada-auth.org/standards/2xrm/html/RM-C-6-2.html#p5 System.Atomic_Operations.Test_And_Set +++++++++++++++++++++++++++++++++++++ アトミックな(少なくとも)1ビットのフラグ型です。 gccの ``__atomic_test_and_set`` と ``__atomic_clear`` に相当します。 ``Exchange`` があれば簡単に実現可能ではありますがその場合ユーザーが適切な型をCPUに合わせて宣言しないといけないため、専用の型も含めて用意されています。 | http://www.ada-auth.org/standards/2xrm/html/RM-C-6-3.html#p3 関連AI ------ - `AI12-0128-1`_: **Exact size access to parts of composite atomic objects** - `AI12-0234-1`_: **Compare-and-swap for atomic objects** - `AI12-0282-1`_: **Atomic, Volatile, and Independent generic formal types** - `AI12-0321-1`_: **Support for Arithmetic Atomic Operations and Test and Set** .. _`AI12-0128-1`: http://www.ada-auth.org/cgi-bin/cvsweb.cgi/ai12s/ai12-0128-1.txt .. _`AI12-0234-1`: http://www.ada-auth.org/cgi-bin/cvsweb.cgi/ai12s/ai12-0234-1.txt .. _`AI12-0282-1`: http://www.ada-auth.org/cgi-bin/cvsweb.cgi/ai12s/ai12-0282-1.txt .. _`AI12-0321-1`: http://www.ada-auth.org/cgi-bin/cvsweb.cgi/ai12s/ai12-0321-1.txt