1------------------------------------------------------------------------------
2--                                                                          --
3--                 GNAT RUN-TIME LIBRARY (GNARL) COMPONENTS                 --
4--                                                                          --
5--         S Y S T E M . T A S K I N G . T A S K _ A T T R I B U T E S      --
6--                                                                          --
7--                                  B o d y                                 --
8--                                                                          --
9--          Copyright (C) 2014-2019, 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-- GNARL was developed by the GNARL team at Florida State University.       --
28-- Extensive contributions were provided by Ada Core Technologies, Inc.     --
29--                                                                          --
30------------------------------------------------------------------------------
31
32with System.Parameters; use System.Parameters;
33with System.Tasking.Initialization; use System.Tasking.Initialization;
34with System.Task_Primitives.Operations;
35
36package body System.Tasking.Task_Attributes is
37
38   package STPO renames System.Task_Primitives.Operations;
39
40   type Index_Info is record
41      Used : Boolean;
42      --  Used is True if a given index is used by an instantiation of
43      --  Ada.Task_Attributes, False otherwise.
44
45      Require_Finalization : Boolean;
46      --  Require_Finalization is True if the attribute requires finalization
47   end record;
48
49   Index_Array : array (1 .. Max_Attribute_Count) of Index_Info :=
50                   (others => (False, False));
51
52   --  Note that this package will use an efficient implementation with no
53   --  locks and no extra dynamic memory allocation if Attribute can fit in a
54   --  System.Address type and Initial_Value is 0 (or null for an access type).
55
56   function Next_Index (Require_Finalization : Boolean) return Integer is
57      Self_Id : constant Task_Id := STPO.Self;
58
59   begin
60      Task_Lock (Self_Id);
61
62      for J in Index_Array'Range loop
63         if not Index_Array (J).Used then
64            Index_Array (J).Used := True;
65            Index_Array (J).Require_Finalization := Require_Finalization;
66            Task_Unlock (Self_Id);
67            return J;
68         end if;
69      end loop;
70
71      Task_Unlock (Self_Id);
72      raise Storage_Error with "Out of task attributes";
73   end Next_Index;
74
75   --------------
76   -- Finalize --
77   --------------
78
79   procedure Finalize (Index : Integer) is
80      Self_Id : constant Task_Id := STPO.Self;
81   begin
82      pragma Assert (Index in Index_Array'Range);
83      Task_Lock (Self_Id);
84      Index_Array (Index).Used := False;
85      Task_Unlock (Self_Id);
86   end Finalize;
87
88   --------------------------
89   -- Require_Finalization --
90   --------------------------
91
92   function Require_Finalization (Index : Integer) return Boolean is
93   begin
94      pragma Assert (Index in Index_Array'Range);
95      return Index_Array (Index).Require_Finalization;
96   end Require_Finalization;
97
98end System.Tasking.Task_Attributes;
99