OSDN Git Service

2004-04-23 Emmanuel Briot <briot@act-europe.fr>
[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 --          Copyright (C) 1992-2001, 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,  59 Temple Place - Suite 330,  Boston, --
20 -- MA 02111-1307, 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 with Ada.Task_Identification;
35 --  used for Task_Id
36 --           Current_Task
37 --           Null_Task_Id
38 --           Is_Terminated
39
40 with System.Task_Primitives.Operations;
41 --  used for Write_Lock
42 --           Unlock
43 --           Set_Priority
44 --           Wakeup
45 --           Self
46
47 with System.Tasking;
48 --  used for Task_ID
49
50 with Ada.Exceptions;
51 --  used for Raise_Exception
52
53 with System.Tasking.Initialization;
54 --  used for Defer/Undefer_Abort
55
56 with System.Parameters;
57 --  used for Single_Lock
58
59 with Unchecked_Conversion;
60
61 package body Ada.Dynamic_Priorities is
62
63    package STPO renames System.Task_Primitives.Operations;
64
65    use System.Parameters;
66    use System.Tasking;
67    use Ada.Exceptions;
68
69    function Convert_Ids is new
70      Unchecked_Conversion
71        (Task_Identification.Task_Id, System.Tasking.Task_ID);
72
73    ------------------
74    -- Get_Priority --
75    ------------------
76
77    --  Inquire base priority of a task
78
79    function Get_Priority
80      (T : Ada.Task_Identification.Task_Id :=
81           Ada.Task_Identification.Current_Task)
82       return System.Any_Priority is
83
84       Target : constant Task_ID := Convert_Ids (T);
85       Error_Message : constant String := "Trying to get the priority of a ";
86
87    begin
88       if Target = Convert_Ids (Ada.Task_Identification.Null_Task_Id) then
89          Raise_Exception (Program_Error'Identity,
90            Error_Message & "null task");
91       end if;
92
93       if Task_Identification.Is_Terminated (T) then
94          Raise_Exception (Tasking_Error'Identity,
95            Error_Message & "null task");
96       end if;
97
98       return Target.Common.Base_Priority;
99    end Get_Priority;
100
101    ------------------
102    -- Set_Priority --
103    ------------------
104
105    --  Change base priority of a task dynamically
106
107    procedure Set_Priority
108      (Priority : System.Any_Priority;
109       T : Ada.Task_Identification.Task_Id :=
110           Ada.Task_Identification.Current_Task)
111    is
112       Target  : constant Task_ID := Convert_Ids (T);
113       Self_ID : constant Task_ID := STPO.Self;
114       Error_Message : constant String := "Trying to set the priority of a ";
115
116    begin
117       if Target = Convert_Ids (Ada.Task_Identification.Null_Task_Id) then
118          Raise_Exception (Program_Error'Identity,
119            Error_Message & "null task");
120       end if;
121
122       if Task_Identification.Is_Terminated (T) then
123          Raise_Exception (Tasking_Error'Identity,
124            Error_Message & "terminated task");
125       end if;
126
127       Initialization.Defer_Abort (Self_ID);
128
129       if Single_Lock then
130          STPO.Lock_RTS;
131       end if;
132
133       STPO.Write_Lock (Target);
134
135       if Self_ID = Target then
136          Target.Common.Base_Priority := Priority;
137          STPO.Set_Priority (Target, Priority);
138
139          STPO.Unlock (Target);
140
141          if Single_Lock then
142             STPO.Unlock_RTS;
143          end if;
144
145          STPO.Yield;
146          --  Yield is needed to enforce FIFO task dispatching.
147          --  LL Set_Priority is made while holding the RTS lock so that
148          --  it is inheriting high priority until it release all the RTS
149          --  locks.
150          --  If this is used in a system where Ceiling Locking is
151          --  not enforced we may end up getting two Yield effects.
152
153       else
154          Target.New_Base_Priority := Priority;
155          Target.Pending_Priority_Change := True;
156          Target.Pending_Action := True;
157
158          STPO.Wakeup (Target, Target.Common.State);
159          --  If the task is suspended, wake it up to perform the change.
160          --  check for ceiling violations ???
161
162          STPO.Unlock (Target);
163
164          if Single_Lock then
165             STPO.Unlock_RTS;
166          end if;
167       end if;
168
169       Initialization.Undefer_Abort (Self_ID);
170    end Set_Priority;
171
172 end Ada.Dynamic_Priorities;