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, 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.
36 package System
.Atomic_Primitives
is
39 type uint
is mod 2 ** Long_Integer'Size;
41 type uint8
is mod 2**8
44 type uint16
is mod 2**16
47 type uint32
is mod 2**32
50 type uint64
is mod 2**64
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;
61 subtype Mem_Model
is Integer range Relaxed
.. Last
;
63 ------------------------------------
64 -- GCC built-in atomic primitives --
65 ------------------------------------
67 function Atomic_Load_8
69 Model
: Mem_Model
:= Seq_Cst
) return uint8
;
70 pragma Import
(Intrinsic
, Atomic_Load_8
, "__atomic_load_1");
72 function Atomic_Load_16
74 Model
: Mem_Model
:= Seq_Cst
) return uint16
;
75 pragma Import
(Intrinsic
, Atomic_Load_16
, "__atomic_load_2");
77 function Atomic_Load_32
79 Model
: Mem_Model
:= Seq_Cst
) return uint32
;
80 pragma Import
(Intrinsic
, Atomic_Load_32
, "__atomic_load_4");
82 function Atomic_Load_64
84 Model
: Mem_Model
:= Seq_Cst
) return uint64
;
85 pragma Import
(Intrinsic
, Atomic_Load_64
, "__atomic_load_8");
87 function Sync_Compare_And_Swap_8
90 Desired
: uint8
) return uint8
;
91 pragma Import
(Intrinsic
,
92 Sync_Compare_And_Swap_8
,
93 "__sync_val_compare_and_swap_1");
95 -- ??? Should use __atomic_compare_exchange_1 (doesn't work yet):
96 -- function Sync_Compare_And_Swap_8
98 -- Expected : Address;
100 -- Weak : Boolean := False;
101 -- Success_Model : Mem_Model := Seq_Cst;
102 -- Failure_Model : Mem_Model := Seq_Cst) return Boolean;
103 -- pragma Import (Intrinsic,
104 -- Sync_Compare_And_Swap_8,
105 -- "__atomic_compare_exchange_1");
107 function Sync_Compare_And_Swap_16
110 Desired
: uint16
) return uint16
;
111 pragma Import
(Intrinsic
,
112 Sync_Compare_And_Swap_16
,
113 "__sync_val_compare_and_swap_2");
115 function Sync_Compare_And_Swap_32
118 Desired
: uint32
) return uint32
;
119 pragma Import
(Intrinsic
,
120 Sync_Compare_And_Swap_32
,
121 "__sync_val_compare_and_swap_4");
123 function Sync_Compare_And_Swap_64
126 Desired
: uint64
) return uint64
;
127 pragma Import
(Intrinsic
,
128 Sync_Compare_And_Swap_64
,
129 "__sync_val_compare_and_swap_8");
131 --------------------------
132 -- Lock-free operations --
133 --------------------------
135 -- The lock-free implementation uses two atomic instructions for the
136 -- expansion of protected operations:
138 -- * Lock_Free_Read_N atomically loads the value of the protected component
139 -- accessed by the current protected operation.
141 -- * Lock_Free_Try_Write_N tries to write the Desired value into Ptr only
142 -- if Expected and Desired mismatch.
144 function Lock_Free_Read_8
(Ptr
: Address
) return uint8
;
146 function Lock_Free_Read_16
(Ptr
: Address
) return uint16
;
148 function Lock_Free_Read_32
(Ptr
: Address
) return uint32
;
150 function Lock_Free_Read_64
(Ptr
: Address
) return uint64
;
152 function Lock_Free_Try_Write_8
154 Expected
: in out uint8
;
155 Desired
: uint8
) return Boolean;
157 function Lock_Free_Try_Write_16
159 Expected
: in out uint16
;
160 Desired
: uint16
) return Boolean;
162 function Lock_Free_Try_Write_32
164 Expected
: in out uint32
;
165 Desired
: uint32
) return Boolean;
167 function Lock_Free_Try_Write_64
169 Expected
: in out uint64
;
170 Desired
: uint64
) return Boolean;
172 pragma Inline
(Lock_Free_Read_8
);
173 pragma Inline
(Lock_Free_Read_16
);
174 pragma Inline
(Lock_Free_Read_32
);
175 pragma Inline
(Lock_Free_Read_64
);
176 pragma Inline
(Lock_Free_Try_Write_8
);
177 pragma Inline
(Lock_Free_Try_Write_16
);
178 pragma Inline
(Lock_Free_Try_Write_32
);
179 pragma Inline
(Lock_Free_Try_Write_64
);
180 end System
.Atomic_Primitives
;