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--                                 S p e c                                  --
8--                                                                          --
9--          Copyright (C) 2011-2015, 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 provides atomic counter on platforms where it is supported:
33--    - all Alpha platforms
34--    - all ia64 platforms
35--    - all PowerPC platforms
36--    - all SPARC V9 platforms
37--    - all x86 platforms
38--    - all x86_64 platforms
39
40package System.Atomic_Counters is
41
42   pragma Pure;
43   pragma Preelaborate;
44
45   type Atomic_Counter is limited private;
46   --  Type for atomic counter objects. Note, initial value of the counter is
47   --  one. This allows using an atomic counter as member of record types when
48   --  object of these types are created at library level in preelaborable
49   --  compilation units.
50   --
51   --  Atomic_Counter is declared as private limited type to provide highest
52   --  level of protection from unexpected use. All available operations are
53   --  declared below, and this set should be as small as possible.
54   --  Increment/Decrement operations for this type raise Program_Error on
55   --  platforms not supporting the atomic primitives.
56
57   procedure Increment (Item : in out Atomic_Counter);
58   pragma Inline_Always (Increment);
59   --  Increments value of atomic counter.
60
61   function Decrement (Item : in out Atomic_Counter) return Boolean;
62   pragma Inline_Always (Decrement);
63   --  Decrements value of atomic counter, returns True when value reach zero
64
65   function Is_One (Item : Atomic_Counter) return Boolean;
66   pragma Inline_Always (Is_One);
67   --  Returns True when value of the atomic counter is one
68
69   procedure Initialize (Item : out Atomic_Counter);
70   pragma Inline_Always (Initialize);
71   --  Initialize counter by setting its value to one. This subprogram is
72   --  intended to be used in special cases when the counter object cannot be
73   --  initialized in standard way.
74
75   type Atomic_Unsigned is mod 2 ** 32 with Default_Value => 0, Atomic;
76   --  Modular compatible atomic unsigned type.
77   --  Increment/Decrement operations for this type are atomic only on
78   --  supported platforms. See top of the file.
79
80   procedure Increment
81     (Item : aliased in out Atomic_Unsigned) with Inline_Always;
82   --  Increments value of atomic counter
83
84   function Decrement
85     (Item : aliased in out Atomic_Unsigned) return Boolean with Inline_Always;
86
87   procedure Decrement
88     (Item : aliased in out Atomic_Unsigned) with Inline_Always;
89   --  Decrements value of atomic counter
90
91   --  The "+" and "-" abstract routine provided below to disable BT := BT + 1
92   --  constructions.
93
94   function "+"
95     (Left, Right : Atomic_Unsigned) return Atomic_Unsigned is abstract;
96
97   function "-"
98     (Left, Right : Atomic_Unsigned) return Atomic_Unsigned is abstract;
99
100private
101
102   type Atomic_Counter is record
103      Value : aliased Atomic_Unsigned := 1;
104      pragma Atomic (Value);
105   end record;
106
107end System.Atomic_Counters;
108