| 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 |
-- B o d y -- |
| 8 |
-- -- |
| 9 |
-- Copyright (C) 1991-2017, Florida State University -- |
| 10 |
-- Copyright (C) 1995-2025, AdaCore -- |
| 11 |
-- -- |
| 12 |
-- GNAT is free software; you can redistribute it and/or modify it under -- |
| 13 |
-- terms of the GNU General Public License as published by the Free Soft- -- |
| 14 |
-- ware Foundation; either version 3, or (at your option) any later ver- -- |
| 15 |
-- sion. GNAT is distributed in the hope that it will be useful, but WITH- -- |
| 16 |
-- OUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY -- |
| 17 |
-- or FITNESS FOR A PARTICULAR PURPOSE. -- |
| 18 |
-- -- |
| 19 |
-- As a special exception under Section 7 of GPL version 3, you are granted -- |
| 20 |
-- additional permissions described in the GCC Runtime Library Exception, -- |
| 21 |
-- version 3.1, as published by the Free Software Foundation. -- |
| 22 |
-- -- |
| 23 |
-- You should have received a copy of the GNU General Public License and -- |
| 24 |
-- a copy of the GCC Runtime Library Exception along with this program; -- |
| 25 |
-- see the files COPYING3 and COPYING.RUNTIME respectively. If not, see -- |
| 26 |
-- <http://www.gnu.org/licenses/>. -- |
| 27 |
-- -- |
| 28 |
-- GNARL was developed by the GNARL team at Florida State University. -- |
| 29 |
-- Extensive contributions were provided by Ada Core Technologies, Inc. -- |
| 30 |
-- -- |
| 31 |
------------------------------------------------------------------------------ |
| 32 |
|
| 33 |
with System.Task_Primitives.Operations; |
| 34 |
with System.Soft_Links.Tasking; |
| 35 |
|
| 36 |
with System.Secondary_Stack; |
| 37 |
pragma Elaborate_All (System.Secondary_Stack); |
| 38 |
pragma Unreferenced (System.Secondary_Stack); |
| 39 |
-- Make sure the body of Secondary_Stack is elaborated before calling |
| 40 |
-- Init_Tasking_Soft_Links. See comments for this routine for explanation. |
| 41 |
|
| 42 |
package body System.Tasking.Protected_Objects is |
| 43 |
|
| 44 |
use System.Task_Primitives.Operations; |
| 45 |
|
| 46 |
---------------- |
| 47 |
-- Local Data -- |
| 48 |
---------------- |
| 49 |
|
| 50 |
Locking_Policy : constant Character; |
| 51 |
pragma Import (C, Locking_Policy, "__gl_locking_policy"); |
| 52 |
|
| 53 |
------------------------- |
| 54 |
-- Finalize_Protection -- |
| 55 |
------------------------- |
| 56 |
|
| 57 |
procedure Finalize_Protection (Object : in out Protection) is |
| 58 |
begin |
| 59 |
Finalize_Lock (Object.L'Unrestricted_Access); |
| 60 |
end Finalize_Protection; |
| 61 |
|
| 62 |
--------------------------- |
| 63 |
-- Initialize_Protection -- |
| 64 |
--------------------------- |
| 65 |
|
| 66 |
procedure Initialize_Protection |
| 67 |
(Object : Protection_Access; |
| 68 |
Ceiling_Priority : Integer) |
| 69 |
is |
| 70 |
Init_Priority : Integer := Ceiling_Priority; |
| 71 |
|
| 72 |
begin |
| 73 |
if Init_Priority = Unspecified_Priority then |
| 74 |
Init_Priority := System.Priority'Last; |
| 75 |
end if; |
| 76 |
|
| 77 |
Initialize_Lock (Init_Priority, Object.L'Access); |
| 78 |
Object.Ceiling := System.Any_Priority (Init_Priority); |
| 79 |
Object.New_Ceiling := System.Any_Priority (Init_Priority); |
| 80 |
Object.Owner := Null_Task; |
| 81 |
end Initialize_Protection; |
| 82 |
|
| 83 |
----------------- |
| 84 |
-- Get_Ceiling -- |
| 85 |
----------------- |
| 86 |
|
| 87 |
function Get_Ceiling |
| 88 |
(Object : Protection_Access) return System.Any_Priority is |
| 89 |
begin |
| 90 |
return Object.New_Ceiling; |
| 91 |
end Get_Ceiling; |
| 92 |
|
| 93 |
---------- |
| 94 |
-- Lock -- |
| 95 |
---------- |
| 96 |
|
| 97 |
procedure Lock (Object : Protection_Access) is |
| 98 |
Ceiling_Violation : Boolean; |
| 99 |
|
| 100 |
begin |
| 101 |
-- The lock is made without deferring abort |
| 102 |
|
| 103 |
-- Therefore the abort has to be deferred before calling this routine. |
| 104 |
-- This means that the compiler has to generate a Defer_Abort call |
| 105 |
-- before the call to Lock. |
| 106 |
|
| 107 |
-- The caller is responsible for undeferring abort, and compiler |
| 108 |
-- generated calls must be protected with cleanup handlers to ensure |
| 109 |
-- that abort is undeferred in all cases. |
| 110 |
|
| 111 |
-- If pragma Detect_Blocking is active then, as described in the ARM |
| 112 |
-- 9.5.1, par. 15, we must check whether this is an external call on a |
| 113 |
-- protected subprogram with the same target object as that of the |
| 114 |
-- protected action that is currently in progress (i.e., if the caller |
| 115 |
-- is already the protected object's owner). If this is the case hence |
| 116 |
-- Program_Error must be raised. |
| 117 |
|
| 118 |
if Detect_Blocking and then Object.Owner = Self then |
| 119 |
raise Program_Error; |
| 120 |
end if; |
| 121 |
|
| 122 |
Write_Lock (Object.L'Access, Ceiling_Violation); |
| 123 |
|
| 124 |
if Ceiling_Violation then |
| 125 |
raise Program_Error; |
| 126 |
end if; |
| 127 |
|
| 128 |
-- We are entering in a protected action, so that we increase the |
| 129 |
-- protected object nesting level (if pragma Detect_Blocking is |
| 130 |
-- active), and update the protected object's owner. |
| 131 |
|
| 132 |
if Detect_Blocking then |
| 133 |
declare |
| 134 |
Self_Id : constant Task_Id := Self; |
| 135 |
begin |
| 136 |
-- Update the protected object's owner |
| 137 |
|
| 138 |
Object.Owner := Self_Id; |
| 139 |
|
| 140 |
-- Increase protected object nesting level |
| 141 |
|
| 142 |
Self_Id.Common.Protected_Action_Nesting := |
| 143 |
Self_Id.Common.Protected_Action_Nesting + 1; |
| 144 |
end; |
| 145 |
end if; |
| 146 |
end Lock; |
| 147 |
|
| 148 |
-------------------- |
| 149 |
-- Lock_Read_Only -- |
| 150 |
-------------------- |
| 151 |
|
| 152 |
procedure Lock_Read_Only (Object : Protection_Access) is |
| 153 |
Ceiling_Violation : Boolean; |
| 154 |
|
| 155 |
begin |
| 156 |
-- If pragma Detect_Blocking is active then, as described in the ARM |
| 157 |
-- 9.5.1, par. 15, we must check whether this is an external call on |
| 158 |
-- protected subprogram with the same target object as that of the |
| 159 |
-- protected action that is currently in progress (i.e., if the caller |
| 160 |
-- is already the protected object's owner). If this is the case hence |
| 161 |
-- Program_Error must be raised. |
| 162 |
-- |
| 163 |
-- Note that in this case (getting read access), several tasks may have |
| 164 |
-- read ownership of the protected object, so that this method of |
| 165 |
-- storing the (single) protected object's owner does not work reliably |
| 166 |
-- for read locks. However, this is the approach taken for two major |
| 167 |
-- reasons: first, this function is not currently being used (it is |
| 168 |
-- provided for possible future use), and second, it largely simplifies |
| 169 |
-- the implementation. |
| 170 |
|
| 171 |
if Detect_Blocking and then Object.Owner = Self then |
| 172 |
raise Program_Error; |
| 173 |
end if; |
| 174 |
|
| 175 |
Read_Lock (Object.L'Access, Ceiling_Violation); |
| 176 |
|
| 177 |
if Ceiling_Violation then |
| 178 |
raise Program_Error; |
| 179 |
end if; |
| 180 |
|
| 181 |
-- We are entering in a protected action, so we increase the protected |
| 182 |
-- object nesting level (if pragma Detect_Blocking is active). |
| 183 |
|
| 184 |
if Detect_Blocking then |
| 185 |
declare |
| 186 |
Self_Id : constant Task_Id := Self; |
| 187 |
begin |
| 188 |
-- Update the protected object's owner |
| 189 |
|
| 190 |
Object.Owner := Self_Id; |
| 191 |
|
| 192 |
-- Increase protected object nesting level |
| 193 |
|
| 194 |
Self_Id.Common.Protected_Action_Nesting := |
| 195 |
Self_Id.Common.Protected_Action_Nesting + 1; |
| 196 |
end; |
| 197 |
end if; |
| 198 |
end Lock_Read_Only; |
| 199 |
|
| 200 |
----------------- |
| 201 |
-- Set_Ceiling -- |
| 202 |
----------------- |
| 203 |
|
| 204 |
procedure Set_Ceiling |
| 205 |
(Object : Protection_Access; |
| 206 |
Prio : System.Any_Priority) is |
| 207 |
begin |
| 208 |
Object.New_Ceiling := Prio; |
| 209 |
end Set_Ceiling; |
| 210 |
|
| 211 |
------------ |
| 212 |
-- Unlock -- |
| 213 |
------------ |
| 214 |
|
| 215 |
procedure Unlock (Object : Protection_Access) is |
| 216 |
begin |
| 217 |
-- We are exiting from a protected action, so that we decrease the |
| 218 |
-- protected object nesting level (if pragma Detect_Blocking is |
| 219 |
-- active), and remove ownership of the protected object. |
| 220 |
|
| 221 |
if Detect_Blocking then |
| 222 |
declare |
| 223 |
Self_Id : constant Task_Id := Self; |
| 224 |
|
| 225 |
begin |
| 226 |
-- Calls to this procedure can only take place when being within |
| 227 |
-- a protected action and when the caller is the protected |
| 228 |
-- object's owner. |
| 229 |
|
| 230 |
pragma Assert (Self_Id.Common.Protected_Action_Nesting > 0 |
| 231 |
and then Object.Owner = Self_Id); |
| 232 |
|
| 233 |
-- Remove ownership of the protected object |
| 234 |
|
| 235 |
Object.Owner := Null_Task; |
| 236 |
|
| 237 |
-- We are exiting from a protected action, so we decrease the |
| 238 |
-- protected object nesting level. |
| 239 |
|
| 240 |
Self_Id.Common.Protected_Action_Nesting := |
| 241 |
Self_Id.Common.Protected_Action_Nesting - 1; |
| 242 |
end; |
| 243 |
end if; |
| 244 |
|
| 245 |
-- Before releasing the mutex we must actually update its ceiling |
| 246 |
-- priority if it has been changed. |
| 247 |
|
| 248 |
if Object.New_Ceiling /= Object.Ceiling then |
| 249 |
if Locking_Policy = 'C' then |
| 250 |
System.Task_Primitives.Operations.Set_Ceiling |
| 251 |
(Object.L'Access, Object.New_Ceiling); |
| 252 |
end if; |
| 253 |
|
| 254 |
Object.Ceiling := Object.New_Ceiling; |
| 255 |
end if; |
| 256 |
|
| 257 |
Unlock (Object.L'Access); |
| 258 |
|
| 259 |
end Unlock; |
| 260 |
|
| 261 |
begin |
| 262 |
-- Ensure that tasking is initialized, as well as tasking soft links |
| 263 |
-- when using protected objects. |
| 264 |
|
| 265 |
Tasking.Initialize; |
| 266 |
System.Soft_Links.Tasking.Init_Tasking_Soft_Links; |
| 267 |
end System.Tasking.Protected_Objects; |