OSDN Git Service

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