OSDN Git Service

* a-calend-mingw.adb: Add call to OS_Primitives.Initialize;
[pf3gnuchains/gcc-fork.git] / gcc / ada / s-taprob.adb
1 ------------------------------------------------------------------------------
2 --                                                                          --
3 --                 GNAT 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, AdaCore                     --
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,  51  Franklin  Street,  Fifth  Floor, --
21 -- Boston, MA 02110-1301, 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
77    begin
78       if Init_Priority = Unspecified_Priority then
79          Init_Priority  := System.Priority'Last;
80       end if;
81
82       Initialize_Lock (Init_Priority, Object.L'Access);
83       Object.Ceiling := System.Any_Priority (Init_Priority);
84       Object.Owner := Null_Task;
85    end Initialize_Protection;
86
87    ----------
88    -- Lock --
89    ----------
90
91    procedure Lock (Object : Protection_Access) is
92       Ceiling_Violation : Boolean;
93
94    begin
95       --  The lock is made without defering abort
96
97       --  Therefore the abort has to be deferred before calling this routine.
98       --  This means that the compiler has to generate a Defer_Abort call
99       --  before the call to Lock.
100
101       --  The caller is responsible for undeferring abort, and compiler
102       --  generated calls must be protected with cleanup handlers to ensure
103       --  that abort is undeferred in all cases.
104
105       --  If pragma Detect_Blocking is active then, as described in the ARM
106       --  9.5.1, par. 15, we must check whether this is an external call on a
107       --  protected subprogram with the same target object as that of the
108       --  protected action that is currently in progress (i.e., if the caller
109       --  is already the protected object's owner). If this is the case hence
110       --  Program_Error must be raised.
111
112       if Detect_Blocking and then Object.Owner = Self then
113          raise Program_Error;
114       end if;
115
116       Write_Lock (Object.L'Access, Ceiling_Violation);
117
118       if Parameters.Runtime_Traces then
119          Send_Trace_Info (PO_Lock);
120       end if;
121
122       if Ceiling_Violation then
123          raise Program_Error;
124       end if;
125
126       --  We are entering in a protected action, so that we increase the
127       --  protected object nesting level (if pragma Detect_Blocking is
128       --  active), and update the protected object's owner.
129
130       if Detect_Blocking then
131          declare
132             Self_Id : constant Task_Id := Self;
133          begin
134             --  Update the protected object's owner
135
136             Object.Owner := Self_Id;
137
138             --  Increase protected object nesting level
139
140             Self_Id.Common.Protected_Action_Nesting :=
141               Self_Id.Common.Protected_Action_Nesting + 1;
142          end;
143       end if;
144    end Lock;
145
146    --------------------
147    -- Lock_Read_Only --
148    --------------------
149
150    procedure Lock_Read_Only (Object : Protection_Access) is
151       Ceiling_Violation : Boolean;
152
153    begin
154       --  If pragma Detect_Blocking is active then, as described in the ARM
155       --  9.5.1, par. 15, we must check whether this is an external call on
156       --  protected subprogram with the same target object as that of the
157       --  protected action that is currently in progress (i.e., if the caller
158       --  is already the protected object's owner). If this is the case hence
159       --  Program_Error must be raised.
160       --
161       --  Note that in this case (getting read access), several tasks may have
162       --  read ownership of the protected object, so that this method of
163       --  storing the (single) protected object's owner does not work reliably
164       --  for read locks. However, this is the approach taken for two major
165       --  reasosn: first, this function is not currently being used (it is
166       --  provided for possible future use), and second, it largely simplifies
167       --  the implementation.
168
169       if Detect_Blocking and then Object.Owner = Self then
170          raise Program_Error;
171       end if;
172
173       Read_Lock (Object.L'Access, Ceiling_Violation);
174
175       if Parameters.Runtime_Traces then
176          Send_Trace_Info (PO_Lock);
177       end if;
178
179       if Ceiling_Violation then
180          raise Program_Error;
181       end if;
182
183       --  We are entering in a protected action, so we increase the protected
184       --  object nesting level (if pragma Detect_Blocking is active).
185
186       if Detect_Blocking then
187          declare
188             Self_Id : constant Task_Id := Self;
189          begin
190             --  Update the protected object's owner
191
192             Object.Owner := Self_Id;
193
194             --  Increase protected object nesting level
195
196             Self_Id.Common.Protected_Action_Nesting :=
197               Self_Id.Common.Protected_Action_Nesting + 1;
198          end;
199       end if;
200    end Lock_Read_Only;
201
202    ------------
203    -- Unlock --
204    ------------
205
206    procedure Unlock (Object : Protection_Access) is
207    begin
208       --  We are exiting from a protected action, so that we decrease the
209       --  protected object nesting level (if pragma Detect_Blocking is
210       --  active), and remove ownership of the protected object.
211
212       if Detect_Blocking then
213          declare
214             Self_Id : constant Task_Id := Self;
215
216          begin
217             --  Calls to this procedure can only take place when being within
218             --  a protected action and when the caller is the protected
219             --  object's owner.
220
221             pragma Assert (Self_Id.Common.Protected_Action_Nesting > 0
222                              and then Object.Owner = Self_Id);
223
224             --  Remove ownership of the protected object
225
226             Object.Owner := Null_Task;
227
228             --  We are exiting from a protected action, so we decrease the
229             --  protected object nesting level.
230
231             Self_Id.Common.Protected_Action_Nesting :=
232               Self_Id.Common.Protected_Action_Nesting - 1;
233          end;
234       end if;
235
236       Unlock (Object.L'Access);
237
238       if Parameters.Runtime_Traces then
239          Send_Trace_Info (PO_Unlock);
240       end if;
241    end Unlock;
242
243 begin
244    --  Ensure that tasking is initialized, as well as tasking soft links
245    --  when using protected objects.
246
247    Tasking.Initialize;
248    System.Soft_Links.Tasking.Init_Tasking_Soft_Links;
249 end System.Tasking.Protected_Objects;