OSDN Git Service

Minor reformatting.
[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-2007, 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 with System.Task_Primitives.Operations;
35 --  used for Write_Lock
36 --           Unlock
37 --           Set_Priority
38 --           Wakeup
39 --           Self
40
41 with System.Tasking;
42 --  used for Task_Id
43
44 with System.Parameters;
45 --  used for Single_Lock
46
47 with System.Soft_Links;
48 --  use for Abort_Defer
49 --          Abort_Undefer
50
51 with Ada.Unchecked_Conversion;
52
53 package body Ada.Dynamic_Priorities is
54
55    package STPO renames System.Task_Primitives.Operations;
56    package SSL renames System.Soft_Links;
57
58    use System.Parameters;
59    use System.Tasking;
60
61    function Convert_Ids is new
62      Ada.Unchecked_Conversion
63        (Task_Identification.Task_Id, System.Tasking.Task_Id);
64
65    ------------------
66    -- Get_Priority --
67    ------------------
68
69    --  Inquire base priority of a task
70
71    function Get_Priority
72      (T : Ada.Task_Identification.Task_Id :=
73         Ada.Task_Identification.Current_Task) return System.Any_Priority
74    is
75       Target : constant Task_Id := Convert_Ids (T);
76       Error_Message : constant String := "Trying to get the priority of a ";
77
78    begin
79       if Target = Convert_Ids (Ada.Task_Identification.Null_Task_Id) then
80          raise Program_Error with Error_Message & "null task";
81       end if;
82
83       if Task_Identification.Is_Terminated (T) then
84          raise Tasking_Error with Error_Message & "null task";
85       end if;
86
87       return Target.Common.Base_Priority;
88    end Get_Priority;
89
90    ------------------
91    -- Set_Priority --
92    ------------------
93
94    --  Change base priority of a task dynamically
95
96    procedure Set_Priority
97      (Priority : System.Any_Priority;
98       T        : Ada.Task_Identification.Task_Id :=
99                    Ada.Task_Identification.Current_Task)
100    is
101       Target        : constant Task_Id := Convert_Ids (T);
102       Error_Message : constant String := "Trying to set the priority of a ";
103       Yield_Needed  : Boolean;
104
105    begin
106       if Target = Convert_Ids (Ada.Task_Identification.Null_Task_Id) then
107          raise Program_Error with Error_Message & "null task";
108       end if;
109
110       if Task_Identification.Is_Terminated (T) then
111          raise Tasking_Error with Error_Message & "terminated task";
112       end if;
113
114       SSL.Abort_Defer.all;
115
116       if Single_Lock then
117          STPO.Lock_RTS;
118       end if;
119
120       STPO.Write_Lock (Target);
121
122       Target.Common.Base_Priority := Priority;
123
124       if Target.Common.Call /= null
125         and then
126           Target.Common.Call.Acceptor_Prev_Priority /= Priority_Not_Boosted
127       then
128          --  Target is within a rendezvous, so ensure the correct priority
129          --  will be reset when finishing the rendezvous, and only change the
130          --  priority immediately if the new priority is greater than the
131          --  current (inherited) priority.
132
133          Target.Common.Call.Acceptor_Prev_Priority := Priority;
134
135          if Priority >= Target.Common.Current_Priority then
136             Yield_Needed := True;
137             STPO.Set_Priority (Target, Priority);
138          else
139             Yield_Needed := False;
140          end if;
141
142       else
143          Yield_Needed := True;
144          STPO.Set_Priority (Target, Priority);
145
146          if Target.Common.State = Entry_Caller_Sleep then
147             Target.Pending_Priority_Change := True;
148             STPO.Wakeup (Target, Target.Common.State);
149          end if;
150       end if;
151
152       STPO.Unlock (Target);
153
154       if Single_Lock then
155          STPO.Unlock_RTS;
156       end if;
157
158       if STPO.Self = Target and then Yield_Needed then
159
160          --  Yield is needed to enforce FIFO task dispatching
161
162          --  LL Set_Priority is made while holding the RTS lock so that it is
163          --  inheriting high priority until it release all the RTS locks.
164
165          --  If this is used in a system where Ceiling Locking is not enforced
166          --  we may end up getting two Yield effects.
167
168          STPO.Yield;
169       end if;
170
171       SSL.Abort_Undefer.all;
172    end Set_Priority;
173
174 end Ada.Dynamic_Priorities;