From ad85af8e5a6f2e7c3482b23c2e2153228889635e Mon Sep 17 00:00:00 2001 From: Piotr Trojanek Date: Thu, 30 Dec 2021 18:07:19 +0100 Subject: [PATCH] [Ada] Switch from __sync to __atomic builtins for atomic counters gcc/ada/ * libgnat/s-atocou__builtin.adb (Decrement, Increment): Switch from __sync to __atomic builtins; use 'Address to be consistent with System.Atomic_Primitives. --- gcc/ada/libgnat/s-atocou__builtin.adb | 42 +++++++++++++++++---------- 1 file changed, 26 insertions(+), 16 deletions(-) diff --git a/gcc/ada/libgnat/s-atocou__builtin.adb b/gcc/ada/libgnat/s-atocou__builtin.adb index d87f9ad74943..e17268bfffd4 100644 --- a/gcc/ada/libgnat/s-atocou__builtin.adb +++ b/gcc/ada/libgnat/s-atocou__builtin.adb @@ -29,21 +29,27 @@ -- -- ------------------------------------------------------------------------------ --- This package implements Atomic_Counter and Atomic_Unsigned operations --- for platforms where GCC supports __sync_add_and_fetch_4 and --- __sync_sub_and_fetch_4 builtins. +-- This package implements Atomic_Counter and Atomic_Unsigned operations for +-- platforms where GCC supports __atomic_add_fetch and __atomic_sub_fetch +-- builtins. + +with System.Atomic_Primitives; use System.Atomic_Primitives; package body System.Atomic_Counters is - procedure Sync_Add_And_Fetch - (Ptr : access Atomic_Unsigned; - Value : Atomic_Unsigned); - pragma Import (Intrinsic, Sync_Add_And_Fetch, "__sync_add_and_fetch_4"); + function Atomic_Add_Fetch + (Ptr : System.Address; + Val : Atomic_Unsigned; + Model : Mem_Model := Seq_Cst) + return Atomic_Unsigned; + pragma Import (Intrinsic, Atomic_Add_Fetch, "__atomic_add_fetch"); - function Sync_Sub_And_Fetch - (Ptr : access Atomic_Unsigned; - Value : Atomic_Unsigned) return Atomic_Unsigned; - pragma Import (Intrinsic, Sync_Sub_And_Fetch, "__sync_sub_and_fetch_4"); + function Atomic_Sub_Fetch + (Ptr : System.Address; + Val : Atomic_Unsigned; + Model : Mem_Model := Seq_Cst) + return Atomic_Unsigned; + pragma Import (Intrinsic, Atomic_Sub_Fetch, "__atomic_sub_fetch"); --------------- -- Decrement -- @@ -51,19 +57,19 @@ package body System.Atomic_Counters is procedure Decrement (Item : aliased in out Atomic_Unsigned) is begin - if Sync_Sub_And_Fetch (Item'Unchecked_Access, 1) = 0 then + if Atomic_Sub_Fetch (Item'Address, 1) = 0 then null; end if; end Decrement; function Decrement (Item : aliased in out Atomic_Unsigned) return Boolean is begin - return Sync_Sub_And_Fetch (Item'Unchecked_Access, 1) = 0; + return Atomic_Sub_Fetch (Item'Address, 1) = 0; end Decrement; function Decrement (Item : in out Atomic_Counter) return Boolean is begin - return Sync_Sub_And_Fetch (Item.Value'Unchecked_Access, 1) = 0; + return Atomic_Sub_Fetch (Item.Value'Address, 1) = 0; end Decrement; --------------- @@ -72,12 +78,16 @@ package body System.Atomic_Counters is procedure Increment (Item : aliased in out Atomic_Unsigned) is begin - Sync_Add_And_Fetch (Item'Unchecked_Access, 1); + if Atomic_Add_Fetch (Item'Address, 1) = 0 then + null; + end if; end Increment; procedure Increment (Item : in out Atomic_Counter) is begin - Sync_Add_And_Fetch (Item.Value'Unchecked_Access, 1); + if Atomic_Add_Fetch (Item.Value'Address, 1) = 0 then + null; + end if; end Increment; ---------------- -- 2.47.2