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--                                 B o d y                                  --
8--                                                                          --
9--              Copyright (C) 2012-2021, 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
32package body System.Atomic_Primitives is
33
34   --------------------
35   -- Lock_Free_Read --
36   --------------------
37
38   function Lock_Free_Read (Ptr : Address) return Atomic_Type is
39      function My_Atomic_Load is new Atomic_Load (Atomic_Type);
40
41   begin
42      if Atomic_Type'Atomic_Always_Lock_Free then
43         return My_Atomic_Load (Ptr, Acquire);
44      else
45         raise Program_Error;
46      end if;
47   end Lock_Free_Read;
48
49   -------------------------
50   -- Lock_Free_Try_Write --
51   -------------------------
52
53   function Lock_Free_Try_Write
54      (Ptr      : Address;
55       Expected : in out Atomic_Type;
56       Desired  : Atomic_Type) return Boolean
57   is
58      function My_Sync_Compare_And_Swap is
59        new Sync_Compare_And_Swap (Atomic_Type);
60
61      Actual : Atomic_Type;
62
63   begin
64      if Expected /= Desired then
65         if Atomic_Type'Atomic_Always_Lock_Free then
66            Actual := My_Sync_Compare_And_Swap (Ptr, Expected, Desired);
67         else
68            raise Program_Error;
69         end if;
70
71         if Actual /= Expected then
72            Expected := Actual;
73            return False;
74         end if;
75      end if;
76
77      return True;
78   end Lock_Free_Try_Write;
79
80end System.Atomic_Primitives;
81