1 ------------------------------------------------------------------------------
3 -- GNAT RUN-TIME COMPONENTS --
5 -- S Y S T E M . A T O M I C _ P R I M I T I V E S --
9 -- Copyright (C) 2012-2023, Free Software Foundation, Inc. --
11 -- GNAT is free software; you can redistribute it and/or modify it under --
12 -- terms of the GNU General Public License as published by the Free Soft- --
13 -- ware Foundation; either version 3, or (at your option) any later ver- --
14 -- sion. GNAT is distributed in the hope that it will be useful, but WITH- --
15 -- OUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY --
16 -- or FITNESS FOR A PARTICULAR PURPOSE. --
18 -- As a special exception under Section 7 of GPL version 3, you are granted --
19 -- additional permissions described in the GCC Runtime Library Exception, --
20 -- version 3.1, as published by the Free Software Foundation. --
22 -- You should have received a copy of the GNU General Public License and --
23 -- a copy of the GCC Runtime Library Exception along with this program; --
24 -- see the files COPYING3 and COPYING.RUNTIME respectively. If not, see --
25 -- <http://www.gnu.org/licenses/>. --
27 -- GNAT was originally developed by the GNAT team at New York University. --
28 -- Extensive contributions were provided by Ada Core Technologies Inc. --
30 ------------------------------------------------------------------------------
32 -- This package contains both atomic primitives defined from GCC built-in
33 -- functions and operations used by the compiler to generate the lock-free
34 -- implementation of protected objects.
35 -- This is the version that only contains primitives available on 32 bit
40 package System
.Atomic_Primitives
is
43 type uint
is mod 2 ** Long_Integer'Size;
45 type uint8
is mod 2**8
48 type uint16
is mod 2**16
51 type uint32
is mod 2**32
54 Relaxed
: constant := 0;
55 Consume
: constant := 1;
56 Acquire
: constant := 2;
57 Release
: constant := 3;
58 Acq_Rel
: constant := 4;
59 Seq_Cst
: constant := 5;
62 subtype Mem_Model
is Integer range Relaxed
.. Last
;
64 ------------------------------------
65 -- GCC built-in atomic primitives --
66 ------------------------------------
69 type Atomic_Type
is mod <>;
72 Model
: Mem_Model
:= Seq_Cst
) return Atomic_Type
;
73 pragma Import
(Intrinsic
, Atomic_Load
, "__atomic_load_n");
75 function Atomic_Load_8
is new Atomic_Load
(uint8
);
76 function Atomic_Load_16
is new Atomic_Load
(uint16
);
77 function Atomic_Load_32
is new Atomic_Load
(uint32
);
80 type Atomic_Type
is mod <>;
81 procedure Atomic_Store
84 Model
: Mem_Model
:= Seq_Cst
);
85 pragma Import
(Intrinsic
, Atomic_Store
, "__atomic_store_n");
87 procedure Atomic_Store_8
is new Atomic_Store
(uint8
);
88 procedure Atomic_Store_16
is new Atomic_Store
(uint16
);
89 procedure Atomic_Store_32
is new Atomic_Store
(uint32
);
92 type Atomic_Type
is mod <>;
93 function Atomic_Compare_Exchange
96 Desired
: Atomic_Type
;
97 Weak
: Boolean := False;
98 Success_Model
: Mem_Model
:= Seq_Cst
;
99 Failure_Model
: Mem_Model
:= Seq_Cst
) return Boolean;
101 (Intrinsic
, Atomic_Compare_Exchange
, "__atomic_compare_exchange_n");
103 function Atomic_Compare_Exchange_8
is new Atomic_Compare_Exchange
(uint8
);
104 function Atomic_Compare_Exchange_16
is new Atomic_Compare_Exchange
(uint16
);
105 function Atomic_Compare_Exchange_32
is new Atomic_Compare_Exchange
(uint32
);
107 function Atomic_Test_And_Set
108 (Ptr
: System
.Address
;
109 Model
: Mem_Model
:= Seq_Cst
) return Boolean;
110 pragma Import
(Intrinsic
, Atomic_Test_And_Set
, "__atomic_test_and_set");
112 procedure Atomic_Clear
113 (Ptr
: System
.Address
;
114 Model
: Mem_Model
:= Seq_Cst
);
115 pragma Import
(Intrinsic
, Atomic_Clear
, "__atomic_clear");
117 function Atomic_Always_Lock_Free
118 (Size
: Interfaces
.C
.size_t
;
119 Ptr
: System
.Address
:= System
.Null_Address
) return Boolean;
121 (Intrinsic
, Atomic_Always_Lock_Free
, "__atomic_always_lock_free");
123 --------------------------
124 -- Lock-free operations --
125 --------------------------
127 -- The lock-free implementation uses two atomic instructions for the
128 -- expansion of protected operations:
130 -- * Lock_Free_Read atomically loads the value contained in Ptr (with the
131 -- Acquire synchronization mode).
133 -- * Lock_Free_Try_Write atomically tries to write the Desired value into
134 -- Ptr if Ptr contains the Expected value. It returns true if the value
135 -- in Ptr was changed, or False if it was not, in which case Expected is
136 -- updated to the unexpected value in Ptr. Note that it does nothing and
137 -- returns true if Desired and Expected are equal.
140 type Atomic_Type
is mod <>;
141 function Lock_Free_Read
(Ptr
: Address
) return Atomic_Type
;
143 function Lock_Free_Read_8
is new Lock_Free_Read
(uint8
);
144 function Lock_Free_Read_16
is new Lock_Free_Read
(uint16
);
145 function Lock_Free_Read_32
is new Lock_Free_Read
(uint32
);
148 type Atomic_Type
is mod <>;
149 function Lock_Free_Try_Write
151 Expected
: in out Atomic_Type
;
152 Desired
: Atomic_Type
) return Boolean;
154 function Lock_Free_Try_Write_8
is new Lock_Free_Try_Write
(uint8
);
155 function Lock_Free_Try_Write_16
is new Lock_Free_Try_Write
(uint16
);
156 function Lock_Free_Try_Write_32
is new Lock_Free_Try_Write
(uint32
);
159 pragma Inline
(Lock_Free_Read
);
160 pragma Inline
(Lock_Free_Try_Write
);
161 end System
.Atomic_Primitives
;