| 1 |
------------------------------------------------------------------------------ |
| 2 |
-- -- |
| 3 |
-- GNAT RUN-TIME COMPONENTS -- |
| 4 |
-- -- |
| 5 |
-- S Y S T E M . A T O M I C _ P R I M I T I V E S -- |
| 6 |
-- -- |
| 7 |
-- S p e c -- |
| 8 |
-- -- |
| 9 |
-- Copyright (C) 2012-2025, Free Software Foundation, Inc. -- |
| 10 |
-- -- |
| 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. -- |
| 17 |
-- -- |
| 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. -- |
| 21 |
-- -- |
| 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/>. -- |
| 26 |
-- -- |
| 27 |
-- GNAT was originally developed by the GNAT team at New York University. -- |
| 28 |
-- Extensive contributions were provided by Ada Core Technologies Inc. -- |
| 29 |
-- -- |
| 30 |
------------------------------------------------------------------------------ |
| 31 |
|
| 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 |
| 36 |
-- platforms. |
| 37 |
|
| 38 |
with Interfaces.C; |
| 39 |
|
| 40 |
package System.Atomic_Primitives is |
| 41 |
pragma Pure; |
| 42 |
|
| 43 |
type uint is mod 2 ** Long_Integer'Size; |
| 44 |
|
| 45 |
type uint8 is mod 2**8 |
| 46 |
with Size => 8; |
| 47 |
|
| 48 |
type uint16 is mod 2**16 |
| 49 |
with Size => 16; |
| 50 |
|
| 51 |
type uint32 is mod 2**32 |
| 52 |
with Size => 32; |
| 53 |
|
| 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; |
| 60 |
Last : constant := 6; |
| 61 |
|
| 62 |
subtype Mem_Model is Integer range Relaxed .. Last; |
| 63 |
|
| 64 |
------------------------------------ |
| 65 |
-- GCC built-in atomic primitives -- |
| 66 |
------------------------------------ |
| 67 |
|
| 68 |
generic |
| 69 |
type Atomic_Type is mod <>; |
| 70 |
function Atomic_Load |
| 71 |
(Ptr : Address; |
| 72 |
Model : Mem_Model := Seq_Cst) return Atomic_Type; |
| 73 |
pragma Import (Intrinsic, Atomic_Load, "__atomic_load_n"); |
| 74 |
|
| 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); |
| 78 |
|
| 79 |
generic |
| 80 |
type Atomic_Type is mod <>; |
| 81 |
procedure Atomic_Store |
| 82 |
(Ptr : Address; |
| 83 |
Value : Atomic_Type; |
| 84 |
Model : Mem_Model := Seq_Cst); |
| 85 |
pragma Import (Intrinsic, Atomic_Store, "__atomic_store_n"); |
| 86 |
|
| 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); |
| 90 |
|
| 91 |
generic |
| 92 |
type Atomic_Type is mod <>; |
| 93 |
function Atomic_Compare_Exchange |
| 94 |
(Ptr : Address; |
| 95 |
Expected : Address; |
| 96 |
Desired : Atomic_Type; |
| 97 |
Weak : Boolean := False; |
| 98 |
Success_Model : Mem_Model := Seq_Cst; |
| 99 |
Failure_Model : Mem_Model := Seq_Cst) return Boolean; |
| 100 |
pragma Import |
| 101 |
(Intrinsic, Atomic_Compare_Exchange, "__atomic_compare_exchange_n"); |
| 102 |
|
| 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); |
| 106 |
|
| 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"); |
| 111 |
|
| 112 |
procedure Atomic_Clear |
| 113 |
(Ptr : System.Address; |
| 114 |
Model : Mem_Model := Seq_Cst); |
| 115 |
pragma Import (Intrinsic, Atomic_Clear, "__atomic_clear"); |
| 116 |
|
| 117 |
function Atomic_Always_Lock_Free |
| 118 |
(Size : Interfaces.C.size_t; |
| 119 |
Ptr : System.Address := System.Null_Address) return Boolean; |
| 120 |
pragma Import |
| 121 |
(Intrinsic, Atomic_Always_Lock_Free, "__atomic_always_lock_free"); |
| 122 |
|
| 123 |
-------------------------- |
| 124 |
-- Lock-free operations -- |
| 125 |
-------------------------- |
| 126 |
|
| 127 |
-- The lock-free implementation uses two atomic instructions for the |
| 128 |
-- expansion of protected operations: |
| 129 |
|
| 130 |
-- * Lock_Free_Read atomically loads the value contained in Ptr (with the |
| 131 |
-- Acquire synchronization mode). |
| 132 |
|
| 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. |
| 138 |
|
| 139 |
generic |
| 140 |
type Atomic_Type is mod <>; |
| 141 |
function Lock_Free_Read (Ptr : Address) return Atomic_Type; |
| 142 |
|
| 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); |
| 146 |
|
| 147 |
generic |
| 148 |
type Atomic_Type is mod <>; |
| 149 |
function Lock_Free_Try_Write |
| 150 |
(Ptr : Address; |
| 151 |
Expected : in out Atomic_Type; |
| 152 |
Desired : Atomic_Type) return Boolean; |
| 153 |
|
| 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); |
| 157 |
|
| 158 |
private |
| 159 |
pragma Inline (Lock_Free_Read); |
| 160 |
pragma Inline (Lock_Free_Try_Write); |
| 161 |
end System.Atomic_Primitives; |