OSDN Git Service

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