跳轉至正文

Ada 程式設計/庫/System.Atomic Operations.Integer Arithmetic

來自維基文庫,開放世界開放書籍

此語言功能已在 Ada 2022 中引入。

System.Atomic Operations.Integer_Arithmetic 是 預定義語言環境 自 Ada 2022 以來提出的一個單元。

generic
   type Atomic_Type is range <> with Atomic;
package System.Atomic_Operations.Integer_Arithmetic
   with Pure, Nonblocking is

   procedure Atomic_Add (Item  : aliased in out Atomic_Type;
                         Value : Atomic_Type)
      with Convention => Intrinsic;

   procedure Atomic_Subtract (Item  : aliased in out Atomic_Type;
                              Value : Atomic_Type)
      with Convention => Intrinsic;

   function Atomic_Fetch_And_Add
     (Item  : aliased in out Atomic_Type;
      Value : Atomic_Type) return Atomic_Type
      with Convention => Intrinsic;

   function Atomic_Fetch_And_Subtract
     (Item  : aliased in out Atomic_Type;
      Value : Atomic_Type) return Atomic_Type
      with Convention => Intrinsic;

   function Is_Lock_Free (Item : aliased Atomic_Type) return Boolean
      with Convention => Intrinsic;

end System.Atomic_Operations.Integer_Arithmetic;

此程式包的操作定義如下

procedure Atomic_Add (Item  : aliased in out Atomic_Type;
                      Value : Atomic_Type)
   with Convention => Intrinsic;

Atomically performs: Item := Item + Value;

procedure Atomic_Subtract (Item  : aliased in out Atomic_Type;
                           Value : Atomic_Type)
   with Convention => Intrinsic;

Atomically performs: Item := Item - Value;

function Atomic_Fetch_And_Add
  (Item  : aliased in out Atomic_Type;
   Value : Atomic_Type) return Atomic_Type
   with Convention => Intrinsic;

Atomically performs: Tmp := Item; Item := Item + Value; return Tmp;

function Atomic_Fetch_And_Subtract
  (Item  : aliased in out Atomic_Type;
   Value : Atomic_Type) return Atomic_Type
   with Convention => Intrinsic;

Atomically performs: Tmp := Item; Item := Item - Value; return Tmp;
華夏公益教科書