Annotated Ada Reference Manual (Ada 202y Draft 3)Legal Information
Contents   Index   References   Search   Previous   Next 

C.6.5 The Package System.Atomic_Operations.Modular_Arithmetic

1/5
{AI12-0364-1} The language-defined generic package System.Atomic_Operations.Modular_Arithmetic provides operations to perform arithmetic atomically on objects of modular types. 

Static Semantics

2/5
{AI12-0364-1} The generic library package System.Atomic_Operations.Modular_Arithmetic has the following declaration:
3/5
generic
   type Atomic_Type is mod <> with Atomic;
package System.Atomic_Operations.Modular_Arithmetic
   with Pure, Nonblocking is
4/5
   procedure Atomic_Add (Item  : aliased in out Atomic_Type;
                         Value : Atomic_Type)
      with Convention => Intrinsic;
5/5
   procedure Atomic_Subtract (Item  : aliased in out Atomic_Type;
                              Value : Atomic_Type)
      with Convention => Intrinsic;
6/5
   function Atomic_Fetch_And_Add
     (Item  : aliased in out Atomic_Type;
      Value : Atomic_Type) return Atomic_Type
      with Convention => Intrinsic;
7/5
   function Atomic_Fetch_And_Subtract
     (Item  : aliased in out Atomic_Type;
      Value : Atomic_Type) return Atomic_Type
      with Convention => Intrinsic;
8/5
   function Is_Lock_Free (Item : aliased Atomic_Type) return Boolean
      with Convention => Intrinsic;
9/5
end System.Atomic_Operations.Modular_Arithmetic;
10/5
{AI12-0364-1} The operations of this package are defined as follows:
11/5
procedure Atomic_Add (Item  : aliased in out Atomic_Type;
                      Value : Atomic_Type)
   with Convention => Intrinsic;
12/5
{AI12-0364-1} Atomically performs: Item := Item + Value;
13/5
procedure Atomic_Subtract (Item  : aliased in out Atomic_Type;
                           Value : Atomic_Type)
   with Convention => Intrinsic;
14/5
{AI12-0364-1} Atomically performs: Item := Item - Value;
15/5
function Atomic_Fetch_And_Add
  (Item  : aliased in out Atomic_Type;
   Value : Atomic_Type) return Atomic_Type
   with Convention => Intrinsic;
16/5
{AI12-0364-1} Atomically performs: Tmp := Item; Item := Item + Value; return Tmp;
17/5
function Atomic_Fetch_And_Subtract
  (Item  : aliased in out Atomic_Type;
   Value : Atomic_Type) return Atomic_Type
   with Convention => Intrinsic;
18/5
{AI12-0364-1} Atomically performs: Tmp := Item; Item := Item - Value; return Tmp;

Implementation Permissions

19/6
{AI22-0125-1} An implementation may allow Item to be modified if the operation defined in an instance of System.Atomic_Operations.Modular_Arithmetic fails a language-defined check. Additionally, an implementation may check that predicates are satisfied after the atomic operation.
19.a/6
Reason: {AI22-0125-1} We want to allow the check to be made after the atomic operation, but using the results of the operation. We do not want to require the arbitrary code of predicate checks to be part of the atomic operation, as that could cause unusually long atomic operations.
19.b/6
Ramification: {AI22-0125-1} Range checks will be performed, but if they fail the object value can be invalid afterwards. 

Usage

20/6
{AI22-0125-1} The subtype used to instantiate System.Atomic_Operations.Modular_Arithmetic should not have predicates, as those probably will not be checked atomically, potentially causing incorrect check failures if another task has modified the value between the operation and the check. For the best performance, the subtype should be a base subtype so that no range check need be performed.

Extensions to Ada 2012

20.a/5
{AI12-0364-1} This package is new. 

Inconsistencies With Ada 2022

20.b/6
{AI22-0125-1} Corrigendum: We now allow language-defined checks to be performed after the atomic operation so that they do not have to harm performance. If an Ada 2022 program depends on the checks being made atomically, it might fail with an updated compiler. However, the only known implementation of this package fails to make language-defined checks at all, so it is very unlikely that any such program exists. 

Contents   Index   References   Search   Previous   Next 
Ada-Europe Ada 2005 and 2012 Editions sponsored in part by Ada-Europe