OSDN Git Service

PR c++/40373
[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-2009, 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 3,  or (at your option) any later ver- --
14 -- sion.  GNAT 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.                                     --
17 --                                                                          --
18 -- As a special exception under Section 7 of GPL version 3, you are granted --
19 -- additional permissions described in the GCC Runtime Library Exception,   --
20 -- version 3.1, as published by the Free Software Foundation.               --
21 --                                                                          --
22 -- You should have received a copy of the GNU General Public License and    --
23 -- a copy of the GCC Runtime Library Exception along with this program;     --
24 -- see the files COPYING3 and COPYING.RUNTIME respectively.  If not, see    --
25 -- <http://www.gnu.org/licenses/>.                                          --
26 --                                                                          --
27 -- GNARL was developed by the GNARL team at Florida State University.       --
28 -- Extensive contributions were provided by Ada Core Technologies, Inc.     --
29 --                                                                          --
30 ------------------------------------------------------------------------------
31
32 with System.Task_Primitives.Operations;
33 with System.Tasking;
34 with System.Parameters;
35 with System.Soft_Links;
36
37 with Ada.Unchecked_Conversion;
38
39 package body Ada.Dynamic_Priorities is
40
41    package STPO renames System.Task_Primitives.Operations;
42    package SSL renames System.Soft_Links;
43
44    use System.Parameters;
45    use System.Tasking;
46
47    function Convert_Ids is new
48      Ada.Unchecked_Conversion
49        (Task_Identification.Task_Id, System.Tasking.Task_Id);
50
51    ------------------
52    -- Get_Priority --
53    ------------------
54
55    --  Inquire base priority of a task
56
57    function Get_Priority
58      (T : Ada.Task_Identification.Task_Id :=
59         Ada.Task_Identification.Current_Task) return System.Any_Priority
60    is
61       Target : constant Task_Id := Convert_Ids (T);
62       Error_Message : constant String := "Trying to get the priority of a ";
63
64    begin
65       if Target = Convert_Ids (Ada.Task_Identification.Null_Task_Id) then
66          raise Program_Error with Error_Message & "null task";
67       end if;
68
69       if Task_Identification.Is_Terminated (T) then
70          raise Tasking_Error with Error_Message & "null task";
71       end if;
72
73       return Target.Common.Base_Priority;
74    end Get_Priority;
75
76    ------------------
77    -- Set_Priority --
78    ------------------
79
80    --  Change base priority of a task dynamically
81
82    procedure Set_Priority
83      (Priority : System.Any_Priority;
84       T        : Ada.Task_Identification.Task_Id :=
85                    Ada.Task_Identification.Current_Task)
86    is
87       Target        : constant Task_Id := Convert_Ids (T);
88       Error_Message : constant String := "Trying to set the priority of a ";
89       Yield_Needed  : Boolean;
90
91    begin
92       if Target = Convert_Ids (Ada.Task_Identification.Null_Task_Id) then
93          raise Program_Error with Error_Message & "null task";
94       end if;
95
96       if Task_Identification.Is_Terminated (T) then
97          raise Tasking_Error with Error_Message & "terminated task";
98       end if;
99
100       SSL.Abort_Defer.all;
101
102       if Single_Lock then
103          STPO.Lock_RTS;
104       end if;
105
106       STPO.Write_Lock (Target);
107
108       Target.Common.Base_Priority := Priority;
109
110       if Target.Common.Call /= null
111         and then
112           Target.Common.Call.Acceptor_Prev_Priority /= Priority_Not_Boosted
113       then
114          --  Target is within a rendezvous, so ensure the correct priority
115          --  will be reset when finishing the rendezvous, and only change the
116          --  priority immediately if the new priority is greater than the
117          --  current (inherited) priority.
118
119          Target.Common.Call.Acceptor_Prev_Priority := Priority;
120
121          if Priority >= Target.Common.Current_Priority then
122             Yield_Needed := True;
123             STPO.Set_Priority (Target, Priority);
124          else
125             Yield_Needed := False;
126          end if;
127
128       else
129          Yield_Needed := True;
130          STPO.Set_Priority (Target, Priority);
131
132          if Target.Common.State = Entry_Caller_Sleep then
133             Target.Pending_Priority_Change := True;
134             STPO.Wakeup (Target, Target.Common.State);
135          end if;
136       end if;
137
138       STPO.Unlock (Target);
139
140       if Single_Lock then
141          STPO.Unlock_RTS;
142       end if;
143
144       if STPO.Self = Target and then Yield_Needed then
145
146          --  Yield is needed to enforce FIFO task dispatching
147
148          --  LL Set_Priority is made while holding the RTS lock so that it is
149          --  inheriting high priority until it release all the RTS locks.
150
151          --  If this is used in a system where Ceiling Locking is not enforced
152          --  we may end up getting two Yield effects.
153
154          STPO.Yield;
155       end if;
156
157       SSL.Abort_Undefer.all;
158    end Set_Priority;
159
160 end Ada.Dynamic_Priorities;