OSDN Git Service

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