OSDN Git Service

2005-03-08 Robert Dewar <dewar@adacore.com>
[pf3gnuchains/gcc-fork.git] / gcc / ada / s-taprob.adb
1 ------------------------------------------------------------------------------
2 --                                                                          --
3 --                GNU ADA RUN-TIME LIBRARY (GNARL) COMPONENTS               --
4 --                                                                          --
5 --      S Y S T E M . T A S K I N G . P R O T E C T E D _ O B J E C T S     --
6 --                                                                          --
7 --                                  B o d y                                 --
8 --                                                                          --
9 --             Copyright (C) 1991-1994, Florida State University            --
10 --             Copyright (C) 1995-2005, Ada Core Technologies               --
11 --                                                                          --
12 -- GNARL is free software; you can  redistribute it  and/or modify it under --
13 -- terms of the  GNU General Public License as published  by the Free Soft- --
14 -- ware  Foundation;  either version 2,  or (at your option) any later ver- --
15 -- sion. GNARL is distributed in the hope that it will be useful, but WITH- --
16 -- OUT ANY WARRANTY;  without even the  implied warranty of MERCHANTABILITY --
17 -- or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License --
18 -- for  more details.  You should have  received  a copy of the GNU General --
19 -- Public License  distributed with GNARL; see file COPYING.  If not, write --
20 -- to  the Free Software Foundation,  59 Temple Place - Suite 330,  Boston, --
21 -- MA 02111-1307, USA.                                                      --
22 --                                                                          --
23 -- As a special exception,  if other files  instantiate  generics from this --
24 -- unit, or you link  this unit with other files  to produce an executable, --
25 -- this  unit  does not  by itself cause  the resulting  executable  to  be --
26 -- covered  by the  GNU  General  Public  License.  This exception does not --
27 -- however invalidate  any other reasons why  the executable file  might be --
28 -- covered by the  GNU Public License.                                      --
29 --                                                                          --
30 -- GNARL was developed by the GNARL team at Florida State University.       --
31 -- Extensive contributions were provided by Ada Core Technologies, Inc.     --
32 --                                                                          --
33 ------------------------------------------------------------------------------
34
35 pragma Polling (Off);
36 --  Turn off polling, we do not want ATC polling to take place during
37 --  tasking operations. It causes infinite loops and other problems.
38
39 with System.Task_Primitives.Operations;
40 --  used for Write_Lock
41 --           Unlock
42 --           Self
43
44 with System.Parameters;
45 --  used for Runtime_Traces
46
47 with System.Traces;
48 --  used for Send_Trace_Info
49
50 with System.Soft_Links.Tasking;
51 --  Used for Init_Tasking_Soft_Links
52
53 package body System.Tasking.Protected_Objects is
54
55    use System.Task_Primitives.Operations;
56    use System.Traces;
57
58    -------------------------
59    -- Finalize_Protection --
60    -------------------------
61
62    procedure Finalize_Protection (Object : in out Protection) is
63    begin
64       Finalize_Lock (Object.L'Unrestricted_Access);
65    end Finalize_Protection;
66
67    ---------------------------
68    -- Initialize_Protection --
69    ---------------------------
70
71    procedure Initialize_Protection
72      (Object           : Protection_Access;
73       Ceiling_Priority : Integer)
74    is
75       Init_Priority : Integer := Ceiling_Priority;
76    begin
77       if Init_Priority = Unspecified_Priority then
78          Init_Priority  := System.Priority'Last;
79       end if;
80
81       Initialize_Lock (Init_Priority, Object.L'Access);
82       Object.Ceiling := System.Any_Priority (Init_Priority);
83    end Initialize_Protection;
84
85    ----------
86    -- Lock --
87    ----------
88
89    procedure Lock (Object : Protection_Access) is
90       Ceiling_Violation : Boolean;
91
92    begin
93       --  The lock is made without defering abort
94
95       --  Therefore the abort has to be deferred before calling this routine.
96       --  This means that the compiler has to generate a Defer_Abort call
97       --  before the call to Lock.
98
99       --  The caller is responsible for undeferring abort, and compiler
100       --  generated calls must be protected with cleanup handlers to ensure
101       --  that abort is undeferred in all cases.
102
103       Write_Lock (Object.L'Access, Ceiling_Violation);
104
105       if Parameters.Runtime_Traces then
106          Send_Trace_Info (PO_Lock);
107       end if;
108
109       if Ceiling_Violation then
110          raise Program_Error;
111       end if;
112
113       --  We are entering in a protected action, so that we increase the
114       --  protected object nesting level (if pragma Detect_Blocking is
115       --  active).
116
117       if Detect_Blocking then
118          declare
119             Self_Id : constant Task_Id := Self;
120          begin
121             Self_Id.Common.Protected_Action_Nesting :=
122               Self_Id.Common.Protected_Action_Nesting + 1;
123          end;
124       end if;
125    end Lock;
126
127    --------------------
128    -- Lock_Read_Only --
129    --------------------
130
131    procedure Lock_Read_Only (Object : Protection_Access) is
132       Ceiling_Violation : Boolean;
133
134    begin
135       Read_Lock (Object.L'Access, Ceiling_Violation);
136
137       if Parameters.Runtime_Traces then
138          Send_Trace_Info (PO_Lock);
139       end if;
140
141       if Ceiling_Violation then
142          raise Program_Error;
143       end if;
144
145       --  We are entering in a protected action, so that we increase the
146       --  protected object nesting level (if pragma Detect_Blocking is
147       --  active).
148
149       if Detect_Blocking then
150          declare
151             Self_Id : constant Task_Id := Self;
152          begin
153             Self_Id.Common.Protected_Action_Nesting :=
154               Self_Id.Common.Protected_Action_Nesting + 1;
155          end;
156       end if;
157    end Lock_Read_Only;
158
159    ------------
160    -- Unlock --
161    ------------
162
163    procedure Unlock (Object : Protection_Access) is
164    begin
165       --  We are exiting from a protected action, so that we decrease the
166       --  protected object nesting level (if pragma Detect_Blocking is
167       --  active).
168
169       if Detect_Blocking then
170          declare
171             Self_Id : constant Task_Id := Self;
172
173          begin
174             --  Cannot call this procedure without being within a protected
175             --  action.
176
177             pragma Assert (Self_Id.Common.Protected_Action_Nesting > 0);
178
179             Self_Id.Common.Protected_Action_Nesting :=
180               Self_Id.Common.Protected_Action_Nesting - 1;
181          end;
182       end if;
183
184       Unlock (Object.L'Access);
185
186       if Parameters.Runtime_Traces then
187          Send_Trace_Info (PO_Unlock);
188       end if;
189    end Unlock;
190
191 begin
192    --  Ensure that tasking soft links are set when using protected objects
193
194    System.Soft_Links.Tasking.Init_Tasking_Soft_Links;
195 end System.Tasking.Protected_Objects;