| 1 |
------------------------------------------------------------------------------ |
| 2 |
-- -- |
| 3 |
-- GNAT RUN-TIME COMPONENTS -- |
| 4 |
-- -- |
| 5 |
-- S Y S T E M . A T O M I C _ C O U N T E R S -- |
| 6 |
-- -- |
| 7 |
-- B o d y -- |
| 8 |
-- -- |
| 9 |
-- Copyright (C) 2011-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 implements unsigned and signed atomic counters using GCC's |
| 33 |
-- __atomic_add_fetch and __atomic_sub_fetch builtins. |
| 34 |
|
| 35 |
with System.Atomic_Primitives; use System.Atomic_Primitives; |
| 36 |
|
| 37 |
package body System.Atomic_Counters is |
| 38 |
|
| 39 |
function Atomic_Add_Fetch |
| 40 |
(Ptr : System.Address; |
| 41 |
Val : Atomic_Unsigned; |
| 42 |
Model : Mem_Model := Seq_Cst) |
| 43 |
return Atomic_Unsigned; |
| 44 |
pragma Import (Intrinsic, Atomic_Add_Fetch, "__atomic_add_fetch"); |
| 45 |
|
| 46 |
function Atomic_Sub_Fetch |
| 47 |
(Ptr : System.Address; |
| 48 |
Val : Atomic_Unsigned; |
| 49 |
Model : Mem_Model := Seq_Cst) |
| 50 |
return Atomic_Unsigned; |
| 51 |
pragma Import (Intrinsic, Atomic_Sub_Fetch, "__atomic_sub_fetch"); |
| 52 |
|
| 53 |
--------------- |
| 54 |
-- Decrement -- |
| 55 |
--------------- |
| 56 |
|
| 57 |
procedure Decrement (Item : aliased in out Atomic_Unsigned) is |
| 58 |
begin |
| 59 |
if Atomic_Sub_Fetch (Item'Address, 1) = 0 then |
| 60 |
null; |
| 61 |
end if; |
| 62 |
end Decrement; |
| 63 |
|
| 64 |
function Decrement (Item : aliased in out Atomic_Unsigned) return Boolean is |
| 65 |
begin |
| 66 |
return Atomic_Sub_Fetch (Item'Address, 1) = 0; |
| 67 |
end Decrement; |
| 68 |
|
| 69 |
function Decrement (Item : in out Atomic_Counter) return Boolean is |
| 70 |
begin |
| 71 |
return Atomic_Sub_Fetch (Item.Value'Address, 1) = 0; |
| 72 |
end Decrement; |
| 73 |
|
| 74 |
--------------- |
| 75 |
-- Increment -- |
| 76 |
--------------- |
| 77 |
|
| 78 |
procedure Increment (Item : aliased in out Atomic_Unsigned) is |
| 79 |
begin |
| 80 |
if Atomic_Add_Fetch (Item'Address, 1) = 0 then |
| 81 |
null; |
| 82 |
end if; |
| 83 |
end Increment; |
| 84 |
|
| 85 |
procedure Increment (Item : in out Atomic_Counter) is |
| 86 |
begin |
| 87 |
if Atomic_Add_Fetch (Item.Value'Address, 1) = 0 then |
| 88 |
null; |
| 89 |
end if; |
| 90 |
end Increment; |
| 91 |
|
| 92 |
---------------- |
| 93 |
-- Initialize -- |
| 94 |
---------------- |
| 95 |
|
| 96 |
procedure Initialize (Item : out Atomic_Counter) is |
| 97 |
begin |
| 98 |
Item.Value := 1; |
| 99 |
end Initialize; |
| 100 |
|
| 101 |
------------ |
| 102 |
-- Is_One -- |
| 103 |
------------ |
| 104 |
|
| 105 |
function Is_One (Item : Atomic_Counter) return Boolean is |
| 106 |
begin |
| 107 |
return Item.Value = 1; |
| 108 |
end Is_One; |
| 109 |
|
| 110 |
end System.Atomic_Counters; |