| 1 |
------------------------------------------------------------------------------ |
| 2 |
-- -- |
| 3 |
-- GNAT RUN-TIME LIBRARY (GNARL) COMPONENTS -- |
| 4 |
-- -- |
| 5 |
-- S Y S T E M . T A S K I N G . P R O T E C T E D _ O B J E C T S -- |
| 6 |
-- -- |
| 7 |
-- S p e c -- |
| 8 |
-- -- |
| 9 |
-- Copyright (C) 1992-2025, Free Software Foundation, Inc. -- |
| 10 |
-- -- |
| 11 |
-- GNARL 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 |
|
| 32 |
-- This package provides necessary definitions to handle simple (i.e without |
| 33 |
-- entries) protected objects. |
| 34 |
|
| 35 |
-- All the routines that handle protected objects with entries have been moved |
| 36 |
-- to two children: Entries and Operations. Note that Entries only contains |
| 37 |
-- the type declaration and the OO primitives. This is needed to avoid |
| 38 |
-- circular dependency. |
| 39 |
|
| 40 |
-- This package is part of the high level tasking interface used by the |
| 41 |
-- compiler to expand Ada 95 tasking constructs into simpler run time calls |
| 42 |
-- (aka GNARLI, GNU Ada Run-time Library Interface) |
| 43 |
|
| 44 |
-- Note: the compiler generates direct calls to this interface, via Rtsfind. |
| 45 |
-- Any changes to this interface may require corresponding compiler changes |
| 46 |
-- in exp_ch9.adb and possibly exp_ch7.adb and exp_attr.adb |
| 47 |
|
| 48 |
package System.Tasking.Protected_Objects is |
| 49 |
pragma Elaborate_Body; |
| 50 |
|
| 51 |
--------------------------------- |
| 52 |
-- Compiler Interface (GNARLI) -- |
| 53 |
--------------------------------- |
| 54 |
|
| 55 |
-- The compiler will expand in the GNAT tree the following construct: |
| 56 |
|
| 57 |
-- protected PO is |
| 58 |
-- procedure P; |
| 59 |
-- private |
| 60 |
-- open : boolean := false; |
| 61 |
-- end PO; |
| 62 |
|
| 63 |
-- protected body PO is |
| 64 |
-- procedure P is |
| 65 |
-- ...variable declarations... |
| 66 |
-- begin |
| 67 |
-- ...B... |
| 68 |
-- end P; |
| 69 |
-- end PO; |
| 70 |
|
| 71 |
-- as follows: |
| 72 |
|
| 73 |
-- protected type poT is |
| 74 |
-- procedure p; |
| 75 |
-- private |
| 76 |
-- open : boolean := false; |
| 77 |
-- end poT; |
| 78 |
-- type poTV is limited record |
| 79 |
-- open : boolean := false; |
| 80 |
-- _object : aliased protection; |
| 81 |
-- end record; |
| 82 |
-- procedure poPT__pN (_object : in out poTV); |
| 83 |
-- procedure poPT__pP (_object : in out poTV); |
| 84 |
-- freeze poTV [ |
| 85 |
-- procedure poTVI (_init : in out poTV) is |
| 86 |
-- begin |
| 87 |
-- _init.open := false; |
| 88 |
-- object-init-proc (_init._object); |
| 89 |
-- initialize_protection (_init._object'unchecked_access, |
| 90 |
-- unspecified_priority); |
| 91 |
-- return; |
| 92 |
-- end _init_proc; |
| 93 |
-- ] |
| 94 |
-- po : poT; |
| 95 |
-- poTVI (poTV!(po)); |
| 96 |
|
| 97 |
-- procedure poPT__pN (_object : in out poTV) is |
| 98 |
-- poR : protection renames _object._object; |
| 99 |
-- openP : boolean renames _object.open; |
| 100 |
-- ...variable declarations... |
| 101 |
-- begin |
| 102 |
-- ...B... |
| 103 |
-- return; |
| 104 |
-- end poPT__pN; |
| 105 |
|
| 106 |
-- procedure poPT__pP (_object : in out poTV) is |
| 107 |
-- procedure _clean is |
| 108 |
-- begin |
| 109 |
-- unlock (_object._object'unchecked_access); |
| 110 |
-- return; |
| 111 |
-- end _clean; |
| 112 |
-- begin |
| 113 |
-- lock (_object._object'unchecked_access); |
| 114 |
-- B2b : begin |
| 115 |
-- poPT__pN (_object); |
| 116 |
-- at end |
| 117 |
-- _clean; |
| 118 |
-- end B2b; |
| 119 |
-- return; |
| 120 |
-- end poPT__pP; |
| 121 |
|
| 122 |
Null_Protected_Entry : constant := Null_Entry; |
| 123 |
|
| 124 |
Max_Protected_Entry : constant := Max_Entry; |
| 125 |
|
| 126 |
type Protected_Entry_Index is new Entry_Index |
| 127 |
range Null_Protected_Entry .. Max_Protected_Entry; |
| 128 |
|
| 129 |
type Barrier_Function_Pointer is access |
| 130 |
function |
| 131 |
(O : System.Address; |
| 132 |
E : Protected_Entry_Index) |
| 133 |
return Boolean; |
| 134 |
-- Pointer to a function which evaluates the barrier of a protected |
| 135 |
-- entry body. O is a pointer to the compiler-generated record |
| 136 |
-- representing the protected object, and E is the index of the |
| 137 |
-- entry serviced by the body. |
| 138 |
|
| 139 |
type Entry_Action_Pointer is access |
| 140 |
procedure |
| 141 |
(O : System.Address; |
| 142 |
P : System.Address; |
| 143 |
E : Protected_Entry_Index); |
| 144 |
-- Pointer to a procedure which executes the sequence of statements |
| 145 |
-- of a protected entry body. O is a pointer to the compiler-generated |
| 146 |
-- record representing the protected object, P is a pointer to the |
| 147 |
-- record of entry parameters, and E is the index of the |
| 148 |
-- entry serviced by the body. |
| 149 |
|
| 150 |
type Entry_Body is record |
| 151 |
Barrier : Barrier_Function_Pointer; |
| 152 |
Action : Entry_Action_Pointer; |
| 153 |
end record; |
| 154 |
-- The compiler-generated code passes objects of this type to the GNARL |
| 155 |
-- to allow it to access the executable code of an entry body and its |
| 156 |
-- barrier. |
| 157 |
|
| 158 |
type Protection is limited private; |
| 159 |
-- This type contains the GNARL state of a protected object. The |
| 160 |
-- application-defined portion of the state (i.e. private objects) |
| 161 |
-- is maintained by the compiler-generated code. |
| 162 |
-- Note that there are now 2 Protection types. One for the simple |
| 163 |
-- case (no entries) and one for the general case that needs the whole |
| 164 |
-- Finalization mechanism. |
| 165 |
-- This split helps in the case of restricted run time where we want to |
| 166 |
-- minimize the size of the code. |
| 167 |
|
| 168 |
type Protection_Access is access all Protection; |
| 169 |
|
| 170 |
Null_PO : constant Protection_Access := null; |
| 171 |
|
| 172 |
function Get_Ceiling |
| 173 |
(Object : Protection_Access) return System.Any_Priority; |
| 174 |
-- Returns the new ceiling priority of the protected object |
| 175 |
|
| 176 |
procedure Initialize_Protection |
| 177 |
(Object : Protection_Access; |
| 178 |
Ceiling_Priority : Integer); |
| 179 |
-- Initialize the Object parameter so that it can be used by the runtime |
| 180 |
-- to keep track of the runtime state of a protected object. |
| 181 |
|
| 182 |
procedure Lock (Object : Protection_Access); |
| 183 |
-- Lock a protected object for write access. Upon return, the caller |
| 184 |
-- owns the lock to this object, and no other call to Lock or |
| 185 |
-- Lock_Read_Only with the same argument will return until the |
| 186 |
-- corresponding call to Unlock has been made by the caller. |
| 187 |
|
| 188 |
procedure Lock_Read_Only (Object : Protection_Access); |
| 189 |
-- Lock a protected object for read access. Upon return, the caller |
| 190 |
-- owns the lock for read access, and no other calls to Lock with the |
| 191 |
-- same argument will return until the corresponding call to Unlock |
| 192 |
-- has been made by the caller. Other calls to Lock_Read_Only may (but |
| 193 |
-- need not) return before the call to Unlock, and the corresponding |
| 194 |
-- callers will also own the lock for read access. |
| 195 |
|
| 196 |
procedure Set_Ceiling |
| 197 |
(Object : Protection_Access; |
| 198 |
Prio : System.Any_Priority); |
| 199 |
-- Sets the new ceiling priority of the protected object |
| 200 |
|
| 201 |
procedure Unlock (Object : Protection_Access); |
| 202 |
-- Relinquish ownership of the lock for the object represented by |
| 203 |
-- the Object parameter. If this ownership was for write access, or |
| 204 |
-- if it was for read access where there are no other read access |
| 205 |
-- locks outstanding, one (or more, in the case of Lock_Read_Only) |
| 206 |
-- of the tasks waiting on this lock (if any) will be given the |
| 207 |
-- lock and allowed to return from the Lock or Lock_Read_Only call. |
| 208 |
|
| 209 |
private |
| 210 |
-- The following type contains the GNARL state of a protected object. |
| 211 |
-- The application-defined portion of the state (i.e. private objects) |
| 212 |
-- is maintained by the compiler-generated code. Note that there is |
| 213 |
-- another version declared in System.Tasking.Protected_Objects.Entries |
| 214 |
-- that handles the case with entries and is controlled. |
| 215 |
|
| 216 |
type Protection is record |
| 217 |
L : aliased Task_Primitives.Lock; |
| 218 |
-- Lock used to ensure mutual exclusive access to the protected object |
| 219 |
|
| 220 |
Ceiling : System.Any_Priority; |
| 221 |
-- Ceiling priority associated to the protected object |
| 222 |
|
| 223 |
New_Ceiling : System.Any_Priority; |
| 224 |
-- New ceiling priority associated to the protected object. In case |
| 225 |
-- of assignment of a new ceiling priority to the protected object the |
| 226 |
-- frontend generates a call to set_ceiling to save the new value in |
| 227 |
-- this field. After such assignment this value can be read by means |
| 228 |
-- of the 'Priority attribute, which generates a call to get_ceiling. |
| 229 |
-- However, the ceiling of the protected object will not be changed |
| 230 |
-- until completion of the protected action in which the assignment |
| 231 |
-- has been executed (AARM D.5.2 (10/2)). |
| 232 |
|
| 233 |
Owner : Task_Id; |
| 234 |
-- This field contains the protected object's owner. Null_Task |
| 235 |
-- indicates that the protected object is not currently being used. |
| 236 |
-- This information is used for detecting the type of potentially |
| 237 |
-- blocking operations described in the ARM 9.5.1, par. 15 (external |
| 238 |
-- calls on a protected subprogram with the same target object as that |
| 239 |
-- of the protected action). |
| 240 |
end record; |
| 241 |
|
| 242 |
procedure Finalize_Protection (Object : in out Protection); |
| 243 |
-- Clean up a Protection object (in particular, finalize the associated |
| 244 |
-- Lock object). The compiler generates calls automatically to this |
| 245 |
-- procedure |
| 246 |
|
| 247 |
end System.Tasking.Protected_Objects; |