PR preprocessor/63831
[official-gcc.git] / gcc / ada / a-dynpri.adb
blob4e67934b7d93a33d04abb877872391e42e0454e6
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-2012, 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 ------------------------------------------------------------------------------
32 with System.Task_Primitives.Operations;
33 with System.Tasking;
34 with System.Parameters;
35 with System.Soft_Links;
37 with Ada.Unchecked_Conversion;
39 package body Ada.Dynamic_Priorities is
41 package STPO renames System.Task_Primitives.Operations;
42 package SSL renames System.Soft_Links;
44 use System.Parameters;
45 use System.Tasking;
47 function Convert_Ids is new
48 Ada.Unchecked_Conversion
49 (Task_Identification.Task_Id, System.Tasking.Task_Id);
51 ------------------
52 -- Get_Priority --
53 ------------------
55 -- Inquire base priority of a task
57 function Get_Priority
58 (T : Ada.Task_Identification.Task_Id :=
59 Ada.Task_Identification.Current_Task) return System.Any_Priority
61 Target : constant Task_Id := Convert_Ids (T);
62 Error_Message : constant String := "Trying to get the priority of a ";
64 begin
65 if Target = Convert_Ids (Ada.Task_Identification.Null_Task_Id) then
66 raise Program_Error with Error_Message & "null task";
67 end if;
69 if Task_Identification.Is_Terminated (T) then
70 raise Tasking_Error with Error_Message & "terminated task";
71 end if;
73 return Target.Common.Base_Priority;
74 end Get_Priority;
76 ------------------
77 -- Set_Priority --
78 ------------------
80 -- Change base priority of a task dynamically
82 procedure Set_Priority
83 (Priority : System.Any_Priority;
84 T : Ada.Task_Identification.Task_Id :=
85 Ada.Task_Identification.Current_Task)
87 Target : constant Task_Id := Convert_Ids (T);
88 Error_Message : constant String := "Trying to set the priority of a ";
89 Yield_Needed : Boolean;
91 begin
92 if Target = Convert_Ids (Ada.Task_Identification.Null_Task_Id) then
93 raise Program_Error with Error_Message & "null task";
94 end if;
96 -- Setting the priority of an already-terminated task doesn't do
97 -- anything (see RM-D.5.1(7)). Note that Get_Priority is different in
98 -- this regard.
100 if Task_Identification.Is_Terminated (T) then
101 return;
102 end if;
104 SSL.Abort_Defer.all;
106 if Single_Lock then
107 STPO.Lock_RTS;
108 end if;
110 STPO.Write_Lock (Target);
112 Target.Common.Base_Priority := Priority;
114 if Target.Common.Call /= null
115 and then
116 Target.Common.Call.Acceptor_Prev_Priority /= Priority_Not_Boosted
117 then
118 -- Target is within a rendezvous, so ensure the correct priority
119 -- will be reset when finishing the rendezvous, and only change the
120 -- priority immediately if the new priority is greater than the
121 -- current (inherited) priority.
123 Target.Common.Call.Acceptor_Prev_Priority := Priority;
125 if Priority >= Target.Common.Current_Priority then
126 Yield_Needed := True;
127 STPO.Set_Priority (Target, Priority);
128 else
129 Yield_Needed := False;
130 end if;
132 else
133 Yield_Needed := True;
134 STPO.Set_Priority (Target, Priority);
136 if Target.Common.State = Entry_Caller_Sleep then
137 Target.Pending_Priority_Change := True;
138 STPO.Wakeup (Target, Target.Common.State);
139 end if;
140 end if;
142 STPO.Unlock (Target);
144 if Single_Lock then
145 STPO.Unlock_RTS;
146 end if;
148 if STPO.Self = Target and then Yield_Needed then
150 -- Yield is needed to enforce FIFO task dispatching
152 -- LL Set_Priority is made while holding the RTS lock so that it is
153 -- inheriting high priority until it release all the RTS locks.
155 -- If this is used in a system where Ceiling Locking is not enforced
156 -- we may end up getting two Yield effects.
158 STPO.Yield;
159 end if;
161 SSL.Abort_Undefer.all;
162 end Set_Priority;
164 end Ada.Dynamic_Priorities;