| 1 |
------------------------------------------------------------------------------ |
| 2 |
-- -- |
| 3 |
-- GNAT RUN-TIME LIBRARY (GNARL) COMPONENTS -- |
| 4 |
-- -- |
| 5 |
-- A D A . D Y N A M I C _ P R I O R I T I E S -- |
| 6 |
-- -- |
| 7 |
-- B o d y -- |
| 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 |
with System.Task_Primitives.Operations; |
| 33 |
with System.Tasking; |
| 34 |
with System.Soft_Links; |
| 35 |
|
| 36 |
with Ada.Unchecked_Conversion; |
| 37 |
|
| 38 |
package body Ada.Dynamic_Priorities is |
| 39 |
|
| 40 |
package STPO renames System.Task_Primitives.Operations; |
| 41 |
package SSL renames System.Soft_Links; |
| 42 |
|
| 43 |
use System.Tasking; |
| 44 |
|
| 45 |
function Convert_Ids is new |
| 46 |
Ada.Unchecked_Conversion |
| 47 |
(Task_Identification.Task_Id, System.Tasking.Task_Id); |
| 48 |
|
| 49 |
------------------ |
| 50 |
-- Get_Priority -- |
| 51 |
------------------ |
| 52 |
|
| 53 |
-- Inquire base priority of a task |
| 54 |
|
| 55 |
function Get_Priority |
| 56 |
(T : Ada.Task_Identification.Task_Id := |
| 57 |
Ada.Task_Identification.Current_Task) return System.Any_Priority |
| 58 |
is |
| 59 |
Target : constant Task_Id := Convert_Ids (T); |
| 60 |
Error_Message : constant String := "Trying to get the priority of a "; |
| 61 |
|
| 62 |
begin |
| 63 |
if Target = Convert_Ids (Ada.Task_Identification.Null_Task_Id) then |
| 64 |
raise Program_Error with Error_Message & "null task"; |
| 65 |
end if; |
| 66 |
|
| 67 |
if Task_Identification.Is_Terminated (T) then |
| 68 |
raise Tasking_Error with Error_Message & "terminated task"; |
| 69 |
end if; |
| 70 |
|
| 71 |
return Target.Common.Base_Priority; |
| 72 |
end Get_Priority; |
| 73 |
|
| 74 |
------------------ |
| 75 |
-- Set_Priority -- |
| 76 |
------------------ |
| 77 |
|
| 78 |
-- Change base priority of a task dynamically |
| 79 |
|
| 80 |
procedure Set_Priority |
| 81 |
(Priority : System.Any_Priority; |
| 82 |
T : Ada.Task_Identification.Task_Id := |
| 83 |
Ada.Task_Identification.Current_Task) |
| 84 |
is |
| 85 |
Target : constant Task_Id := Convert_Ids (T); |
| 86 |
Error_Message : constant String := "Trying to set the priority of a "; |
| 87 |
Yield_Needed : Boolean; |
| 88 |
|
| 89 |
begin |
| 90 |
if Target = Convert_Ids (Ada.Task_Identification.Null_Task_Id) then |
| 91 |
raise Program_Error with Error_Message & "null task"; |
| 92 |
end if; |
| 93 |
|
| 94 |
-- Setting the priority of an already-terminated task doesn't do |
| 95 |
-- anything (see RM-D.5.1(7)). Note that Get_Priority is different in |
| 96 |
-- this regard. |
| 97 |
|
| 98 |
if Task_Identification.Is_Terminated (T) then |
| 99 |
return; |
| 100 |
end if; |
| 101 |
|
| 102 |
SSL.Abort_Defer.all; |
| 103 |
|
| 104 |
STPO.Write_Lock (Target); |
| 105 |
|
| 106 |
Target.Common.Base_Priority := Priority; |
| 107 |
|
| 108 |
if Target.Common.Call /= null |
| 109 |
and then |
| 110 |
Target.Common.Call.Acceptor_Prev_Priority /= Priority_Not_Boosted |
| 111 |
then |
| 112 |
-- Target is within a rendezvous, so ensure the correct priority |
| 113 |
-- will be reset when finishing the rendezvous, and only change the |
| 114 |
-- priority immediately if the new priority is greater than the |
| 115 |
-- current (inherited) priority. |
| 116 |
|
| 117 |
Target.Common.Call.Acceptor_Prev_Priority := Priority; |
| 118 |
|
| 119 |
if Priority >= Target.Common.Current_Priority then |
| 120 |
Yield_Needed := True; |
| 121 |
STPO.Set_Priority (Target, Priority); |
| 122 |
else |
| 123 |
Yield_Needed := False; |
| 124 |
end if; |
| 125 |
|
| 126 |
else |
| 127 |
Yield_Needed := True; |
| 128 |
STPO.Set_Priority (Target, Priority); |
| 129 |
|
| 130 |
if Target.Common.State = Entry_Caller_Sleep then |
| 131 |
Target.Pending_Priority_Change := True; |
| 132 |
STPO.Wakeup (Target, Target.Common.State); |
| 133 |
end if; |
| 134 |
end if; |
| 135 |
|
| 136 |
STPO.Unlock (Target); |
| 137 |
|
| 138 |
if STPO.Self = Target and then Yield_Needed then |
| 139 |
|
| 140 |
-- Yield is needed to enforce FIFO task dispatching |
| 141 |
|
| 142 |
-- LL Set_Priority is made while holding the RTS lock so that it is |
| 143 |
-- inheriting high priority until it release all the RTS locks. |
| 144 |
|
| 145 |
-- If this is used in a system where Ceiling Locking is not enforced |
| 146 |
-- we may end up getting two Yield effects. |
| 147 |
|
| 148 |
STPO.Yield; |
| 149 |
end if; |
| 150 |
|
| 151 |
SSL.Abort_Undefer.all; |
| 152 |
end Set_Priority; |
| 153 |
|
| 154 |
end Ada.Dynamic_Priorities; |