OSDN Git Service

PR preprocessor/20348
[pf3gnuchains/gcc-fork.git] / gcc / ada / a-dynpri.adb
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-2005, 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 2,  or (at your option) any later ver- --
14 -- sion. GNARL 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.  See the GNU General Public License --
17 -- for  more details.  You should have  received  a copy of the GNU General --
18 -- Public License  distributed with GNARL; see file COPYING.  If not, write --
19 -- to  the  Free Software Foundation,  51  Franklin  Street,  Fifth  Floor, --
20 -- Boston, MA 02110-1301, USA.                                              --
21 --                                                                          --
22 -- As a special exception,  if other files  instantiate  generics from this --
23 -- unit, or you link  this unit with other files  to produce an executable, --
24 -- this  unit  does not  by itself cause  the resulting  executable  to  be --
25 -- covered  by the  GNU  General  Public  License.  This exception does not --
26 -- however invalidate  any other reasons why  the executable file  might be --
27 -- covered by the  GNU Public License.                                      --
28 --                                                                          --
29 -- GNARL was developed by the GNARL team at Florida State University.       --
30 -- Extensive contributions were provided by Ada Core Technologies, Inc.     --
31 --                                                                          --
32 ------------------------------------------------------------------------------
33
34 pragma Warnings (Off);
35 --  Allow withing of non-Preelaborated units in Ada 2005 mode where this
36 --  package will be categorized as Preelaborate. See AI-362 for details.
37 --  It is safe in the context of the run-time to violate the rules!
38
39 with Ada.Task_Identification;
40 --  used for Task_Id
41 --           Current_Task
42 --           Null_Task_Id
43 --           Is_Terminated
44
45 with System.Task_Primitives.Operations;
46 --  used for Write_Lock
47 --           Unlock
48 --           Set_Priority
49 --           Wakeup
50 --           Self
51
52 with System.Tasking;
53 --  used for Task_Id
54
55 with Ada.Exceptions;
56 --  used for Raise_Exception
57
58 with System.Tasking.Initialization;
59 --  used for Defer/Undefer_Abort
60
61 with System.Parameters;
62 --  used for Single_Lock
63
64 with Unchecked_Conversion;
65
66 pragma Warnings (On);
67
68 package body Ada.Dynamic_Priorities is
69
70    package STPO renames System.Task_Primitives.Operations;
71
72    use System.Parameters;
73    use System.Tasking;
74    use Ada.Exceptions;
75
76    function Convert_Ids is new
77      Unchecked_Conversion
78        (Task_Identification.Task_Id, System.Tasking.Task_Id);
79
80    ------------------
81    -- Get_Priority --
82    ------------------
83
84    --  Inquire base priority of a task
85
86    function Get_Priority
87      (T : Ada.Task_Identification.Task_Id :=
88         Ada.Task_Identification.Current_Task) return System.Any_Priority
89    is
90       Target : constant Task_Id := Convert_Ids (T);
91       Error_Message : constant String := "Trying to get the priority of a ";
92
93    begin
94       if Target = Convert_Ids (Ada.Task_Identification.Null_Task_Id) then
95          Raise_Exception (Program_Error'Identity,
96            Error_Message & "null task");
97       end if;
98
99       if Task_Identification.Is_Terminated (T) then
100          Raise_Exception (Tasking_Error'Identity,
101            Error_Message & "null task");
102       end if;
103
104       return Target.Common.Base_Priority;
105    end Get_Priority;
106
107    ------------------
108    -- Set_Priority --
109    ------------------
110
111    --  Change base priority of a task dynamically
112
113    procedure Set_Priority
114      (Priority : System.Any_Priority;
115       T        : Ada.Task_Identification.Task_Id :=
116                    Ada.Task_Identification.Current_Task)
117    is
118       Target  : constant Task_Id := Convert_Ids (T);
119       Self_ID : constant Task_Id := STPO.Self;
120       Error_Message : constant String := "Trying to set the priority of a ";
121
122    begin
123       if Target = Convert_Ids (Ada.Task_Identification.Null_Task_Id) then
124          Raise_Exception (Program_Error'Identity,
125            Error_Message & "null task");
126       end if;
127
128       if Task_Identification.Is_Terminated (T) then
129          Raise_Exception (Tasking_Error'Identity,
130            Error_Message & "terminated task");
131       end if;
132
133       Initialization.Defer_Abort (Self_ID);
134
135       if Single_Lock then
136          STPO.Lock_RTS;
137       end if;
138
139       STPO.Write_Lock (Target);
140
141       if Self_ID = Target then
142          Target.Common.Base_Priority := Priority;
143          STPO.Set_Priority (Target, Priority);
144
145          STPO.Unlock (Target);
146
147          if Single_Lock then
148             STPO.Unlock_RTS;
149          end if;
150
151          --  Yield is needed to enforce FIFO task dispatching.
152
153          --  LL Set_Priority is made while holding the RTS lock so that it
154          --  is inheriting high priority until it release all the RTS locks.
155
156          --  If this is used in a system where Ceiling Locking is
157          --  not enforced we may end up getting two Yield effects.
158
159          STPO.Yield;
160
161       else
162          Target.New_Base_Priority := Priority;
163          Target.Pending_Priority_Change := True;
164          Target.Pending_Action := True;
165
166          STPO.Wakeup (Target, Target.Common.State);
167
168          --  If the task is suspended, wake it up to perform the change.
169          --  check for ceiling violations ???
170
171          STPO.Unlock (Target);
172
173          if Single_Lock then
174             STPO.Unlock_RTS;
175          end if;
176       end if;
177
178       Initialization.Undefer_Abort (Self_ID);
179    end Set_Priority;
180
181 end Ada.Dynamic_Priorities;