Daily bump.
[official-gcc.git] / gcc / ada / s-tposen.adb
blob7b2005da9b3ea2f562096047e494a69d3313a414
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 -- S I N G L E _ E N T R Y --
7 -- --
8 -- B o d y --
9 -- --
10 -- $Revision$
11 -- --
12 -- Copyright (C) 1998-2001 Ada Core Technologies --
13 -- --
14 -- GNARL is free software; you can redistribute it and/or modify it under --
15 -- terms of the GNU General Public License as published by the Free Soft- --
16 -- ware Foundation; either version 2, or (at your option) any later ver- --
17 -- sion. GNARL is distributed in the hope that it will be useful, but WITH- --
18 -- OUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY --
19 -- or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License --
20 -- for more details. You should have received a copy of the GNU General --
21 -- Public License distributed with GNARL; see file COPYING. If not, write --
22 -- to the Free Software Foundation, 59 Temple Place - Suite 330, Boston, --
23 -- MA 02111-1307, USA. --
24 -- --
25 -- As a special exception, if other files instantiate generics from this --
26 -- unit, or you link this unit with other files to produce an executable, --
27 -- this unit does not by itself cause the resulting executable to be --
28 -- covered by the GNU General Public License. This exception does not --
29 -- however invalidate any other reasons why the executable file might be --
30 -- covered by the GNU Public License. --
31 -- --
32 -- GNARL was developed by the GNARL team at Florida State University. It is --
33 -- now maintained by Ada Core Technologies Inc. in cooperation with Florida --
34 -- State University (http://www.gnat.com). --
35 -- --
36 ------------------------------------------------------------------------------
38 pragma Style_Checks (All_Checks);
39 -- Turn off subprogram ordering check, since restricted GNARLI
40 -- subprograms are gathered together at end.
42 -- This package provides an optimized version of Protected_Objects.Operations
43 -- and Protected_Objects.Entries making the following assumptions:
45 -- PO have only one entry
46 -- There is only one caller at a time (No_Entry_Queue)
47 -- There is no dynamic priority support (No_Dynamic_Priorities)
48 -- No Abort Statements
49 -- (No_Abort_Statements, Max_Asynchronous_Select_Nesting => 0)
50 -- PO are at library level
51 -- No Requeue
52 -- None of the tasks will terminate (no need for finalization)
54 -- This interface is intended to be used in the ravenscar and restricted
55 -- profiles, the compiler is responsible for ensuring that the conditions
56 -- mentioned above are respected, except for the No_Entry_Queue restriction
57 -- that is checked dynamically in this package, since the check cannot be
58 -- performed at compile time, and is relatively cheap (see PO_Do_Or_Queue,
59 -- PO_Service_Entry).
61 pragma Polling (Off);
62 -- Turn off polling, we do not want polling to take place during tasking
63 -- operations. It can cause infinite loops and other problems.
65 pragma Suppress (All_Checks);
67 with System.Task_Primitives.Operations;
68 -- used for Self
69 -- Finalize_Lock
70 -- Write_Lock
71 -- Unlock
73 with Ada.Exceptions;
74 -- used for Exception_Id;
76 with Unchecked_Conversion;
78 package body System.Tasking.Protected_Objects.Single_Entry is
80 package STPO renames System.Task_Primitives.Operations;
82 function To_Address is new
83 Unchecked_Conversion (Protection_Entry_Access, System.Address);
85 -----------------------
86 -- Local Subprograms --
87 -----------------------
89 procedure Send_Program_Error
90 (Self_Id : Task_ID;
91 Entry_Call : Entry_Call_Link);
92 pragma Inline (Send_Program_Error);
93 -- Raise Program_Error in the caller of the specified entry call
95 --------------------------
96 -- Entry Calls Handling --
97 --------------------------
99 procedure Wakeup_Entry_Caller
100 (Self_ID : Task_ID;
101 Entry_Call : Entry_Call_Link;
102 New_State : Entry_Call_State);
103 pragma Inline (Wakeup_Entry_Caller);
104 -- This is called at the end of service of an entry call,
105 -- to abort the caller if he is in an abortable part, and
106 -- to wake up the caller if he is on Entry_Caller_Sleep.
107 -- Call it holding the lock of Entry_Call.Self.
109 -- Timed_Call or Simple_Call:
110 -- The caller is waiting on Entry_Caller_Sleep, in
111 -- Wait_For_Completion, or Wait_For_Completion_With_Timeout.
113 procedure Wait_For_Completion
114 (Self_ID : Task_ID;
115 Entry_Call : Entry_Call_Link);
116 pragma Inline (Wait_For_Completion);
117 -- This procedure suspends the calling task until the specified entry call
118 -- has either been completed or cancelled. On exit, the call will not be
119 -- queued. This waits for calls on protected entries.
120 -- Call this only when holding Self_ID locked.
122 procedure Wait_For_Completion_With_Timeout
123 (Self_ID : Task_ID;
124 Entry_Call : Entry_Call_Link;
125 Wakeup_Time : Duration;
126 Mode : Delay_Modes);
127 -- Same as Wait_For_Completion but it waits for a timeout with the value
128 -- specified in Wakeup_Time as well.
129 -- Self_ID will be locked by this procedure.
131 procedure Check_Exception
132 (Self_ID : Task_ID;
133 Entry_Call : Entry_Call_Link);
134 pragma Inline (Check_Exception);
135 -- Raise any pending exception from the Entry_Call.
136 -- This should be called at the end of every compiler interface procedure
137 -- that implements an entry call.
138 -- The caller should not be holding any locks, or there will be deadlock.
140 procedure PO_Do_Or_Queue
141 (Self_Id : Task_ID;
142 Object : Protection_Entry_Access;
143 Entry_Call : Entry_Call_Link);
144 -- This procedure executes or queues an entry call, depending
145 -- on the status of the corresponding barrier. It assumes that the
146 -- specified object is locked.
148 ---------------------
149 -- Check_Exception --
150 ---------------------
152 procedure Check_Exception
153 (Self_ID : Task_ID;
154 Entry_Call : Entry_Call_Link)
156 procedure Internal_Raise (X : Ada.Exceptions.Exception_Id);
157 pragma Import (C, Internal_Raise, "__gnat_raise_with_msg");
159 use type Ada.Exceptions.Exception_Id;
161 E : constant Ada.Exceptions.Exception_Id :=
162 Entry_Call.Exception_To_Raise;
164 begin
165 if E /= Ada.Exceptions.Null_Id then
166 Internal_Raise (E);
167 end if;
168 end Check_Exception;
170 ------------------------
171 -- Send_Program_Error --
172 ------------------------
174 procedure Send_Program_Error
175 (Self_Id : Task_ID;
176 Entry_Call : Entry_Call_Link)
178 Caller : constant Task_ID := Entry_Call.Self;
179 begin
180 Entry_Call.Exception_To_Raise := Program_Error'Identity;
181 STPO.Write_Lock (Caller);
182 Wakeup_Entry_Caller (Self_Id, Entry_Call, Done);
183 STPO.Unlock (Caller);
184 end Send_Program_Error;
186 -------------------------
187 -- Wait_For_Completion --
188 -------------------------
190 -- Call this only when holding Self_ID locked
192 procedure Wait_For_Completion
193 (Self_ID : Task_ID;
194 Entry_Call : Entry_Call_Link) is
195 begin
196 pragma Assert (Self_ID = Entry_Call.Self);
197 Self_ID.Common.State := Entry_Caller_Sleep;
199 STPO.Sleep (Self_ID, Entry_Caller_Sleep);
201 Self_ID.Common.State := Runnable;
202 end Wait_For_Completion;
204 --------------------------------------
205 -- Wait_For_Completion_With_Timeout --
206 --------------------------------------
208 -- This routine will lock Self_ID.
210 -- This procedure waits for the entry call to
211 -- be served, with a timeout. It tries to cancel the
212 -- call if the timeout expires before the call is served.
214 -- If we wake up from the timed sleep operation here,
215 -- it may be for the following possible reasons:
217 -- 1) The entry call is done being served.
218 -- 2) The timeout has expired (Timedout = True)
220 -- Once the timeout has expired we may need to continue to wait if
221 -- the call is already being serviced. In that case, we want to go
222 -- back to sleep, but without any timeout. The variable Timedout is
223 -- used to control this. If the Timedout flag is set, we do not need
224 -- to Sleep with a timeout. We just sleep until we get a wakeup for
225 -- some status change.
227 procedure Wait_For_Completion_With_Timeout
228 (Self_ID : Task_ID;
229 Entry_Call : Entry_Call_Link;
230 Wakeup_Time : Duration;
231 Mode : Delay_Modes)
233 Timedout : Boolean;
234 Yielded : Boolean;
236 use type Ada.Exceptions.Exception_Id;
238 begin
239 STPO.Write_Lock (Self_ID);
241 pragma Assert (Entry_Call.Self = Self_ID);
242 pragma Assert (Entry_Call.Mode = Timed_Call);
243 Self_ID.Common.State := Entry_Caller_Sleep;
245 STPO.Timed_Sleep
246 (Self_ID, Wakeup_Time, Mode, Entry_Caller_Sleep, Timedout, Yielded);
248 if Timedout then
249 Entry_Call.State := Cancelled;
250 else
251 Entry_Call.State := Done;
252 end if;
254 Self_ID.Common.State := Runnable;
255 STPO.Unlock (Self_ID);
256 end Wait_For_Completion_With_Timeout;
258 -------------------------
259 -- Wakeup_Entry_Caller --
260 -------------------------
262 -- This is called at the end of service of an entry call, to abort the
263 -- caller if he is in an abortable part, and to wake up the caller if it
264 -- is on Entry_Caller_Sleep. It assumes that the call is already off-queue.
266 -- (This enforces the rule that a task must be off-queue if its state is
267 -- Done or Cancelled.) Call it holding the lock of Entry_Call.Self.
269 -- Timed_Call or Simple_Call:
270 -- The caller is waiting on Entry_Caller_Sleep, in
271 -- Wait_For_Completion, or Wait_For_Completion_With_Timeout.
273 -- Conditional_Call:
274 -- The caller might be in Wait_For_Completion,
275 -- waiting for a rendezvous (possibly requeued without abort)
276 -- to complete.
278 procedure Wakeup_Entry_Caller
279 (Self_ID : Task_ID;
280 Entry_Call : Entry_Call_Link;
281 New_State : Entry_Call_State)
283 Caller : constant Task_ID := Entry_Call.Self;
284 begin
285 pragma Assert (New_State = Done or else New_State = Cancelled);
286 pragma Assert
287 (Caller.Common.State /= Terminated and then
288 Caller.Common.State /= Unactivated);
290 Entry_Call.State := New_State;
291 STPO.Wakeup (Caller, Entry_Caller_Sleep);
292 end Wakeup_Entry_Caller;
294 -----------------------
295 -- Restricted GNARLI --
296 -----------------------
298 --------------------------------
299 -- Complete_Single_Entry_Body --
300 --------------------------------
302 procedure Complete_Single_Entry_Body (Object : Protection_Entry_Access) is
303 begin
304 -- Nothing needs to be done since
305 -- Object.Call_In_Progress.Exception_To_Raise has already been set to
306 -- Null_Id
307 null;
308 end Complete_Single_Entry_Body;
310 --------------------------------------------
311 -- Exceptional_Complete_Single_Entry_Body --
312 --------------------------------------------
314 procedure Exceptional_Complete_Single_Entry_Body
315 (Object : Protection_Entry_Access;
316 Ex : Ada.Exceptions.Exception_Id) is
317 begin
318 Object.Call_In_Progress.Exception_To_Raise := Ex;
319 end Exceptional_Complete_Single_Entry_Body;
321 ---------------------------------
322 -- Initialize_Protection_Entry --
323 ---------------------------------
325 procedure Initialize_Protection_Entry
326 (Object : Protection_Entry_Access;
327 Ceiling_Priority : Integer;
328 Compiler_Info : System.Address;
329 Entry_Body : Entry_Body_Access)
331 Init_Priority : Integer := Ceiling_Priority;
333 begin
334 if Init_Priority = Unspecified_Priority then
335 Init_Priority := System.Priority'Last;
336 end if;
338 STPO.Initialize_Lock (Init_Priority, Object.L'Access);
339 Object.Ceiling := System.Any_Priority (Init_Priority);
340 Object.Compiler_Info := Compiler_Info;
341 Object.Call_In_Progress := null;
342 Object.Entry_Body := Entry_Body;
343 Object.Entry_Queue := null;
344 end Initialize_Protection_Entry;
346 ----------------
347 -- Lock_Entry --
348 ----------------
350 -- Compiler interface only.
351 -- Do not call this procedure from within the run-time system.
353 procedure Lock_Entry (Object : Protection_Entry_Access) is
354 Ceiling_Violation : Boolean;
355 begin
356 STPO.Write_Lock (Object.L'Access, Ceiling_Violation);
358 if Ceiling_Violation then
359 raise Program_Error;
360 end if;
361 end Lock_Entry;
363 --------------------------
364 -- Lock_Read_Only_Entry --
365 --------------------------
367 -- Compiler interface only.
368 -- Do not call this procedure from within the runtime system.
370 procedure Lock_Read_Only_Entry (Object : Protection_Entry_Access) is
371 Ceiling_Violation : Boolean;
372 begin
373 STPO.Read_Lock (Object.L'Access, Ceiling_Violation);
375 if Ceiling_Violation then
376 raise Program_Error;
377 end if;
378 end Lock_Read_Only_Entry;
380 --------------------
381 -- PO_Do_Or_Queue --
382 --------------------
384 procedure PO_Do_Or_Queue
385 (Self_Id : Task_ID;
386 Object : Protection_Entry_Access;
387 Entry_Call : Entry_Call_Link)
389 Barrier_Value : Boolean;
390 begin
391 -- When the Action procedure for an entry body returns, it must be
392 -- completed (having called [Exceptional_]Complete_Entry_Body).
394 Barrier_Value := Object.Entry_Body.Barrier (Object.Compiler_Info, 1);
396 if Barrier_Value then
397 if Object.Call_In_Progress /= null then
398 -- This violates the No_Entry_Queue restriction, send
399 -- Program_Error to the caller.
401 Send_Program_Error (Self_Id, Entry_Call);
402 return;
403 end if;
405 Object.Call_In_Progress := Entry_Call;
406 Object.Entry_Body.Action
407 (Object.Compiler_Info, Entry_Call.Uninterpreted_Data, 1);
408 Object.Call_In_Progress := null;
409 Wakeup_Entry_Caller (Self_Id, Entry_Call, Done);
411 elsif Entry_Call.Mode /= Conditional_Call then
412 Object.Entry_Queue := Entry_Call;
413 else
414 -- Conditional_Call
416 STPO.Write_Lock (Entry_Call.Self);
417 Wakeup_Entry_Caller (Self_Id, Entry_Call, Cancelled);
418 STPO.Unlock (Entry_Call.Self);
419 end if;
421 exception
422 when others =>
423 Send_Program_Error
424 (Self_Id, Entry_Call);
425 end PO_Do_Or_Queue;
427 ----------------------------
428 -- Protected_Single_Count --
429 ----------------------------
431 function Protected_Count_Entry (Object : Protection_Entry) return Natural is
432 begin
433 if Object.Call_In_Progress /= null then
434 return 1;
435 else
436 return 0;
437 end if;
438 end Protected_Count_Entry;
440 ---------------------------------
441 -- Protected_Single_Entry_Call --
442 ---------------------------------
444 procedure Protected_Single_Entry_Call
445 (Object : Protection_Entry_Access;
446 Uninterpreted_Data : System.Address;
447 Mode : Call_Modes)
449 Self_Id : constant Task_ID := STPO.Self;
450 Entry_Call : Entry_Call_Record renames Self_Id.Entry_Calls (1);
451 Ceiling_Violation : Boolean;
453 begin
454 STPO.Write_Lock (Object.L'Access, Ceiling_Violation);
456 if Ceiling_Violation then
457 raise Program_Error;
458 end if;
460 Entry_Call.Mode := Mode;
461 Entry_Call.State := Now_Abortable;
462 Entry_Call.Uninterpreted_Data := Uninterpreted_Data;
463 Entry_Call.Exception_To_Raise := Ada.Exceptions.Null_Id;
465 PO_Do_Or_Queue (Self_Id, Object, Entry_Call'Access);
466 Unlock_Entry (Object);
468 -- The call is either `Done' or not. It cannot be cancelled since there
469 -- is no ATC construct.
471 pragma Assert (Entry_Call.State /= Cancelled);
473 if Entry_Call.State /= Done then
474 STPO.Write_Lock (Self_Id);
475 Wait_For_Completion (Self_Id, Entry_Call'Access);
476 STPO.Unlock (Self_Id);
477 end if;
479 Check_Exception (Self_Id, Entry_Call'Access);
480 end Protected_Single_Entry_Call;
482 -----------------------------------
483 -- Protected_Single_Entry_Caller --
484 -----------------------------------
486 function Protected_Single_Entry_Caller
487 (Object : Protection_Entry) return Task_ID is
488 begin
489 return Object.Call_In_Progress.Self;
490 end Protected_Single_Entry_Caller;
492 -------------------
493 -- Service_Entry --
494 -------------------
496 procedure Service_Entry (Object : Protection_Entry_Access) is
497 Self_Id : constant Task_ID := STPO.Self;
498 Entry_Call : constant Entry_Call_Link := Object.Entry_Queue;
499 Caller : Task_ID;
500 Barrier_Value : Boolean;
502 begin
503 if Entry_Call /= null then
504 Barrier_Value := Object.Entry_Body.Barrier (Object.Compiler_Info, 1);
506 if Barrier_Value then
507 if Object.Call_In_Progress /= null then
508 -- This violates the No_Entry_Queue restriction, send
509 -- Program_Error to the caller.
511 Send_Program_Error (Self_Id, Entry_Call);
512 return;
513 end if;
515 Object.Call_In_Progress := Entry_Call;
516 Object.Entry_Body.Action
517 (Object.Compiler_Info, Entry_Call.Uninterpreted_Data, 1);
518 Object.Call_In_Progress := null;
519 Caller := Entry_Call.Self;
520 STPO.Write_Lock (Caller);
521 Wakeup_Entry_Caller (Self_Id, Entry_Call, Done);
522 STPO.Unlock (Caller);
523 end if;
524 end if;
526 exception
527 when others =>
528 Send_Program_Error (Self_Id, Entry_Call);
529 end Service_Entry;
531 ---------------------------------------
532 -- Timed_Protected_Single_Entry_Call --
533 ---------------------------------------
535 -- Compiler interface only. Do not call from within the RTS.
537 procedure Timed_Protected_Single_Entry_Call
538 (Object : Protection_Entry_Access;
539 Uninterpreted_Data : System.Address;
540 Timeout : Duration;
541 Mode : Delay_Modes;
542 Entry_Call_Successful : out Boolean)
544 Self_Id : constant Task_ID := STPO.Self;
545 Entry_Call : Entry_Call_Record renames Self_Id.Entry_Calls (1);
546 Ceiling_Violation : Boolean;
548 begin
549 STPO.Write_Lock (Object.L'Access, Ceiling_Violation);
551 if Ceiling_Violation then
552 raise Program_Error;
553 end if;
555 Entry_Call.Mode := Timed_Call;
556 Entry_Call.State := Now_Abortable;
557 Entry_Call.Uninterpreted_Data := Uninterpreted_Data;
558 Entry_Call.Exception_To_Raise := Ada.Exceptions.Null_Id;
560 PO_Do_Or_Queue (Self_Id, Object, Entry_Call'Access);
561 Unlock_Entry (Object);
563 -- Try to avoid waiting for completed calls.
564 -- The call is either `Done' or not. It cannot be cancelled since there
565 -- is no ATC construct and the timed wait has not started yet.
567 pragma Assert (Entry_Call.State /= Cancelled);
569 if Entry_Call.State = Done then
570 Check_Exception (Self_Id, Entry_Call'Access);
571 Entry_Call_Successful := True;
572 return;
573 end if;
575 Wait_For_Completion_With_Timeout
576 (Self_Id, Entry_Call'Access, Timeout, Mode);
578 pragma Assert (Entry_Call.State >= Done);
580 Check_Exception (Self_Id, Entry_Call'Access);
581 Entry_Call_Successful := Entry_Call.State = Done;
582 end Timed_Protected_Single_Entry_Call;
584 ------------------
585 -- Unlock_Entry --
586 ------------------
588 procedure Unlock_Entry (Object : Protection_Entry_Access) is
589 begin
590 STPO.Unlock (Object.L'Access);
591 end Unlock_Entry;
593 end System.Tasking.Protected_Objects.Single_Entry;