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-2019, 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
36package System.Atomic_Primitives is
37   pragma Preelaborate;
38
39   type uint is mod 2 ** Long_Integer'Size;
40
41   type uint8  is mod 2**8
42     with Size => 8;
43
44   type uint16 is mod 2**16
45     with Size => 16;
46
47   type uint32 is mod 2**32
48     with Size => 32;
49
50   type uint64 is mod 2**64
51     with Size => 64;
52
53   Relaxed : constant := 0;
54   Consume : constant := 1;
55   Acquire : constant := 2;
56   Release : constant := 3;
57   Acq_Rel : constant := 4;
58   Seq_Cst : constant := 5;
59   Last    : constant := 6;
60
61   subtype Mem_Model is Integer range Relaxed .. Last;
62
63   ------------------------------------
64   -- GCC built-in atomic primitives --
65   ------------------------------------
66
67   function Atomic_Load_8
68     (Ptr   : Address;
69      Model : Mem_Model := Seq_Cst) return uint8;
70   pragma Import (Intrinsic, Atomic_Load_8, "__atomic_load_1");
71
72   function Atomic_Load_16
73     (Ptr   : Address;
74      Model : Mem_Model := Seq_Cst) return uint16;
75   pragma Import (Intrinsic, Atomic_Load_16, "__atomic_load_2");
76
77   function Atomic_Load_32
78     (Ptr   : Address;
79      Model : Mem_Model := Seq_Cst) return uint32;
80   pragma Import (Intrinsic, Atomic_Load_32, "__atomic_load_4");
81
82   function Atomic_Load_64
83     (Ptr   : Address;
84      Model : Mem_Model := Seq_Cst) return uint64;
85   pragma Import (Intrinsic, Atomic_Load_64, "__atomic_load_8");
86
87   function Sync_Compare_And_Swap_8
88     (Ptr      : Address;
89      Expected : uint8;
90      Desired  : uint8) return uint8;
91   pragma Import (Intrinsic,
92                  Sync_Compare_And_Swap_8,
93                  "__sync_val_compare_and_swap_1");
94
95   function Sync_Compare_And_Swap_16
96     (Ptr      : Address;
97      Expected : uint16;
98      Desired  : uint16) return uint16;
99   pragma Import (Intrinsic,
100                  Sync_Compare_And_Swap_16,
101                  "__sync_val_compare_and_swap_2");
102
103   function Sync_Compare_And_Swap_32
104     (Ptr      : Address;
105      Expected : uint32;
106      Desired  : uint32) return uint32;
107   pragma Import (Intrinsic,
108                  Sync_Compare_And_Swap_32,
109                  "__sync_val_compare_and_swap_4");
110
111   function Sync_Compare_And_Swap_64
112     (Ptr      : Address;
113      Expected : uint64;
114      Desired  : uint64) return uint64;
115   pragma Import (Intrinsic,
116                  Sync_Compare_And_Swap_64,
117                  "__sync_val_compare_and_swap_8");
118
119   --  ??? We might want to switch to the __atomic series of builtins for
120   --  compare-and-swap operations at some point.
121
122   --  function Atomic_Compare_Exchange_8
123   --    (Ptr           : Address;
124   --     Expected      : Address;
125   --     Desired       : uint8;
126   --     Weak          : Boolean   := False;
127   --     Success_Model : Mem_Model := Seq_Cst;
128   --     Failure_Model : Mem_Model := Seq_Cst) return Boolean;
129   --  pragma Import (Intrinsic,
130   --                 Atomic_Compare_Exchange_8,
131   --                 "__atomic_compare_exchange_1");
132
133   --------------------------
134   -- Lock-free operations --
135   --------------------------
136
137   --  The lock-free implementation uses two atomic instructions for the
138   --  expansion of protected operations:
139
140   --  * Lock_Free_Read_N atomically loads the value of the protected component
141   --    accessed by the current protected operation.
142
143   --  * Lock_Free_Try_Write_N tries to write the Desired value into Ptr only
144   --    if Expected and Desired mismatch.
145
146   function Lock_Free_Read_8 (Ptr : Address) return uint8;
147
148   function Lock_Free_Read_16 (Ptr : Address) return uint16;
149
150   function Lock_Free_Read_32 (Ptr : Address) return uint32;
151
152   function Lock_Free_Read_64 (Ptr : Address) return uint64;
153
154   function Lock_Free_Try_Write_8
155      (Ptr      : Address;
156       Expected : in out uint8;
157       Desired  : uint8) return Boolean;
158
159   function Lock_Free_Try_Write_16
160      (Ptr      : Address;
161       Expected : in out uint16;
162       Desired  : uint16) return Boolean;
163
164   function Lock_Free_Try_Write_32
165      (Ptr      : Address;
166       Expected : in out uint32;
167       Desired  : uint32) return Boolean;
168
169   function Lock_Free_Try_Write_64
170      (Ptr      : Address;
171       Expected : in out uint64;
172       Desired  : uint64) return Boolean;
173
174   pragma Inline (Lock_Free_Read_8);
175   pragma Inline (Lock_Free_Read_16);
176   pragma Inline (Lock_Free_Read_32);
177   pragma Inline (Lock_Free_Read_64);
178   pragma Inline (Lock_Free_Try_Write_8);
179   pragma Inline (Lock_Free_Try_Write_16);
180   pragma Inline (Lock_Free_Try_Write_32);
181   pragma Inline (Lock_Free_Try_Write_64);
182end System.Atomic_Primitives;
183