include/ChangeLog:
[official-gcc.git] / gcc / ada / s-tposen.adb
blobdf132a544ea1bc85b4fa5f83c5985637493220e4
1 ------------------------------------------------------------------------------
2 -- --
3 -- GNU ADA RUN-TIME LIBRARY (GNARL) COMPONENTS --
4 -- --
5 -- SYSTEM.TASKING.PROTECTED_OBJECTS.SINGLE_ENTRY --
6 -- --
7 -- B o d y --
8 -- --
9 -- Copyright (C) 1998-2002, 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, 59 Temple Place - Suite 330, Boston, --
20 -- MA 02111-1307, 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. It is --
30 -- now maintained by Ada Core Technologies, Inc. (http://www.gnat.com). --
31 -- --
32 ------------------------------------------------------------------------------
34 pragma Style_Checks (All_Checks);
35 -- Turn off subprogram ordering check, since restricted GNARLI
36 -- subprograms are gathered together at end.
38 -- This package provides an optimized version of Protected_Objects.Operations
39 -- and Protected_Objects.Entries making the following assumptions:
41 -- PO have only one entry
42 -- There is only one caller at a time (No_Entry_Queue)
43 -- There is no dynamic priority support (No_Dynamic_Priorities)
44 -- No Abort Statements
45 -- (No_Abort_Statements, Max_Asynchronous_Select_Nesting => 0)
46 -- PO are at library level
47 -- No Requeue
48 -- None of the tasks will terminate (no need for finalization)
50 -- This interface is intended to be used in the ravenscar and restricted
51 -- profiles, the compiler is responsible for ensuring that the conditions
52 -- mentioned above are respected, except for the No_Entry_Queue restriction
53 -- that is checked dynamically in this package, since the check cannot be
54 -- performed at compile time, and is relatively cheap (see PO_Do_Or_Queue,
55 -- PO_Service_Entry).
57 pragma Polling (Off);
58 -- Turn off polling, we do not want polling to take place during tasking
59 -- operations. It can cause infinite loops and other problems.
61 pragma Suppress (All_Checks);
63 with System.Task_Primitives.Operations;
64 -- used for Self
65 -- Finalize_Lock
66 -- Write_Lock
67 -- Unlock
69 with Ada.Exceptions;
70 -- used for Exception_Id;
72 with System.Parameters;
73 -- used for Single_Lock
75 package body System.Tasking.Protected_Objects.Single_Entry is
77 package STPO renames System.Task_Primitives.Operations;
79 use Parameters;
81 -----------------------
82 -- Local Subprograms --
83 -----------------------
85 procedure Send_Program_Error
86 (Self_Id : Task_ID;
87 Entry_Call : Entry_Call_Link);
88 pragma Inline (Send_Program_Error);
89 -- Raise Program_Error in the caller of the specified entry call
91 --------------------------
92 -- Entry Calls Handling --
93 --------------------------
95 procedure Wakeup_Entry_Caller
96 (Self_ID : Task_ID;
97 Entry_Call : Entry_Call_Link;
98 New_State : Entry_Call_State);
99 pragma Inline (Wakeup_Entry_Caller);
100 -- This is called at the end of service of an entry call,
101 -- to abort the caller if he is in an abortable part, and
102 -- to wake up the caller if he is on Entry_Caller_Sleep.
103 -- Call it holding the lock of Entry_Call.Self.
105 -- Timed_Call or Simple_Call:
106 -- The caller is waiting on Entry_Caller_Sleep, in
107 -- Wait_For_Completion, or Wait_For_Completion_With_Timeout.
109 procedure Wait_For_Completion (Entry_Call : Entry_Call_Link);
110 pragma Inline (Wait_For_Completion);
111 -- This procedure suspends the calling task until the specified entry call
112 -- has either been completed or cancelled. On exit, the call will not be
113 -- queued. This waits for calls on protected entries.
114 -- Call this only when holding Self_ID locked.
116 procedure Wait_For_Completion_With_Timeout
117 (Entry_Call : Entry_Call_Link;
118 Wakeup_Time : Duration;
119 Mode : Delay_Modes);
120 -- Same as Wait_For_Completion but it waits for a timeout with the value
121 -- specified in Wakeup_Time as well.
123 procedure Check_Exception
124 (Self_ID : Task_ID;
125 Entry_Call : Entry_Call_Link);
126 pragma Inline (Check_Exception);
127 -- Raise any pending exception from the Entry_Call.
128 -- This should be called at the end of every compiler interface procedure
129 -- that implements an entry call.
130 -- The caller should not be holding any locks, or there will be deadlock.
132 procedure PO_Do_Or_Queue
133 (Self_Id : Task_ID;
134 Object : Protection_Entry_Access;
135 Entry_Call : Entry_Call_Link);
136 -- This procedure executes or queues an entry call, depending
137 -- on the status of the corresponding barrier. It assumes that the
138 -- specified object is locked.
140 ---------------------
141 -- Check_Exception --
142 ---------------------
144 procedure Check_Exception
145 (Self_ID : Task_ID;
146 Entry_Call : Entry_Call_Link)
148 pragma Warnings (Off, Self_ID);
150 procedure Internal_Raise (X : Ada.Exceptions.Exception_Id);
151 pragma Import (C, Internal_Raise, "__gnat_raise_with_msg");
153 use type Ada.Exceptions.Exception_Id;
155 E : constant Ada.Exceptions.Exception_Id :=
156 Entry_Call.Exception_To_Raise;
158 begin
159 if E /= Ada.Exceptions.Null_Id then
160 Internal_Raise (E);
161 end if;
162 end Check_Exception;
164 ------------------------
165 -- Send_Program_Error --
166 ------------------------
168 procedure Send_Program_Error
169 (Self_Id : Task_ID;
170 Entry_Call : Entry_Call_Link)
172 Caller : constant Task_ID := Entry_Call.Self;
173 begin
174 Entry_Call.Exception_To_Raise := Program_Error'Identity;
176 if Single_Lock then
177 STPO.Lock_RTS;
178 end if;
180 STPO.Write_Lock (Caller);
181 Wakeup_Entry_Caller (Self_Id, Entry_Call, Done);
182 STPO.Unlock (Caller);
184 if Single_Lock then
185 STPO.Unlock_RTS;
186 end if;
187 end Send_Program_Error;
189 -------------------------
190 -- Wait_For_Completion --
191 -------------------------
193 procedure Wait_For_Completion (Entry_Call : Entry_Call_Link) is
194 Self_Id : constant Task_ID := Entry_Call.Self;
195 begin
196 Self_Id.Common.State := Entry_Caller_Sleep;
197 STPO.Sleep (Self_Id, Entry_Caller_Sleep);
198 Self_Id.Common.State := Runnable;
199 end Wait_For_Completion;
201 --------------------------------------
202 -- Wait_For_Completion_With_Timeout --
203 --------------------------------------
205 procedure Wait_For_Completion_With_Timeout
206 (Entry_Call : Entry_Call_Link;
207 Wakeup_Time : Duration;
208 Mode : Delay_Modes)
210 Self_Id : constant Task_ID := Entry_Call.Self;
211 Timedout : Boolean;
212 Yielded : Boolean;
214 use type Ada.Exceptions.Exception_Id;
216 begin
217 -- This procedure waits for the entry call to be served, with a timeout.
218 -- It tries to cancel the call if the timeout expires before the call is
219 -- served.
221 -- If we wake up from the timed sleep operation here, it may be for the
222 -- following possible reasons:
224 -- 1) The entry call is done being served.
225 -- 2) The timeout has expired (Timedout = True)
227 -- Once the timeout has expired we may need to continue to wait if the
228 -- call is already being serviced. In that case, we want to go back to
229 -- sleep, but without any timeout. The variable Timedout is used to
230 -- control this. If the Timedout flag is set, we do not need to Sleep
231 -- with a timeout. We just sleep until we get a wakeup for some status
232 -- change.
234 pragma Assert (Entry_Call.Mode = Timed_Call);
235 Self_Id.Common.State := Entry_Caller_Sleep;
237 STPO.Timed_Sleep
238 (Self_Id, Wakeup_Time, Mode, Entry_Caller_Sleep, Timedout, Yielded);
240 if Timedout then
241 Entry_Call.State := Cancelled;
242 else
243 Entry_Call.State := Done;
244 end if;
246 Self_Id.Common.State := Runnable;
247 end Wait_For_Completion_With_Timeout;
249 -------------------------
250 -- Wakeup_Entry_Caller --
251 -------------------------
253 -- This is called at the end of service of an entry call, to abort the
254 -- caller if he is in an abortable part, and to wake up the caller if it
255 -- is on Entry_Caller_Sleep. It assumes that the call is already off-queue.
257 -- (This enforces the rule that a task must be off-queue if its state is
258 -- Done or Cancelled.) Call it holding the lock of Entry_Call.Self.
260 -- Timed_Call or Simple_Call:
261 -- The caller is waiting on Entry_Caller_Sleep, in
262 -- Wait_For_Completion, or Wait_For_Completion_With_Timeout.
264 -- Conditional_Call:
265 -- The caller might be in Wait_For_Completion,
266 -- waiting for a rendezvous (possibly requeued without abort)
267 -- to complete.
269 procedure Wakeup_Entry_Caller
270 (Self_ID : Task_ID;
271 Entry_Call : Entry_Call_Link;
272 New_State : Entry_Call_State)
274 pragma Warnings (Off, Self_ID);
276 Caller : constant Task_ID := Entry_Call.Self;
278 begin
279 pragma Assert (New_State = Done or else New_State = Cancelled);
280 pragma Assert
281 (Caller.Common.State /= Terminated and then
282 Caller.Common.State /= Unactivated);
284 Entry_Call.State := New_State;
285 STPO.Wakeup (Caller, Entry_Caller_Sleep);
286 end Wakeup_Entry_Caller;
288 -----------------------
289 -- Restricted GNARLI --
290 -----------------------
292 --------------------------------
293 -- Complete_Single_Entry_Body --
294 --------------------------------
296 procedure Complete_Single_Entry_Body (Object : Protection_Entry_Access) is
297 pragma Warnings (Off, Object);
299 begin
300 -- Nothing needs to do (Object.Call_In_Progress.Exception_To_Raise
301 -- has already been set to Null_Id).
303 null;
304 end Complete_Single_Entry_Body;
306 --------------------------------------------
307 -- Exceptional_Complete_Single_Entry_Body --
308 --------------------------------------------
310 procedure Exceptional_Complete_Single_Entry_Body
311 (Object : Protection_Entry_Access;
312 Ex : Ada.Exceptions.Exception_Id) is
313 begin
314 Object.Call_In_Progress.Exception_To_Raise := Ex;
315 end Exceptional_Complete_Single_Entry_Body;
317 ---------------------------------
318 -- Initialize_Protection_Entry --
319 ---------------------------------
321 procedure Initialize_Protection_Entry
322 (Object : Protection_Entry_Access;
323 Ceiling_Priority : Integer;
324 Compiler_Info : System.Address;
325 Entry_Body : Entry_Body_Access)
327 Init_Priority : Integer := Ceiling_Priority;
328 begin
329 if Init_Priority = Unspecified_Priority then
330 Init_Priority := System.Priority'Last;
331 end if;
333 STPO.Initialize_Lock (Init_Priority, Object.L'Access);
334 Object.Ceiling := System.Any_Priority (Init_Priority);
335 Object.Compiler_Info := Compiler_Info;
336 Object.Call_In_Progress := null;
337 Object.Entry_Body := Entry_Body;
338 Object.Entry_Queue := null;
339 end Initialize_Protection_Entry;
341 ----------------
342 -- Lock_Entry --
343 ----------------
345 -- Compiler interface only.
346 -- Do not call this procedure from within the run-time system.
348 procedure Lock_Entry (Object : Protection_Entry_Access) is
349 Ceiling_Violation : Boolean;
350 begin
351 STPO.Write_Lock (Object.L'Access, Ceiling_Violation);
353 if Ceiling_Violation then
354 raise Program_Error;
355 end if;
356 end Lock_Entry;
358 --------------------------
359 -- Lock_Read_Only_Entry --
360 --------------------------
362 -- Compiler interface only.
363 -- Do not call this procedure from within the runtime system.
365 procedure Lock_Read_Only_Entry (Object : Protection_Entry_Access) is
366 Ceiling_Violation : Boolean;
367 begin
368 STPO.Read_Lock (Object.L'Access, Ceiling_Violation);
370 if Ceiling_Violation then
371 raise Program_Error;
372 end if;
373 end Lock_Read_Only_Entry;
375 --------------------
376 -- PO_Do_Or_Queue --
377 --------------------
379 procedure PO_Do_Or_Queue
380 (Self_Id : Task_ID;
381 Object : Protection_Entry_Access;
382 Entry_Call : Entry_Call_Link)
384 Barrier_Value : Boolean;
385 begin
386 -- When the Action procedure for an entry body returns, it must be
387 -- completed (having called [Exceptional_]Complete_Entry_Body).
389 Barrier_Value := Object.Entry_Body.Barrier (Object.Compiler_Info, 1);
391 if Barrier_Value then
392 if Object.Call_In_Progress /= null then
393 -- This violates the No_Entry_Queue restriction, send
394 -- Program_Error to the caller.
396 Send_Program_Error (Self_Id, Entry_Call);
397 return;
398 end if;
400 Object.Call_In_Progress := Entry_Call;
401 Object.Entry_Body.Action
402 (Object.Compiler_Info, Entry_Call.Uninterpreted_Data, 1);
403 Object.Call_In_Progress := null;
405 if Single_Lock then
406 STPO.Lock_RTS;
407 end if;
409 STPO.Write_Lock (Entry_Call.Self);
410 Wakeup_Entry_Caller (Self_Id, Entry_Call, Done);
411 STPO.Unlock (Entry_Call.Self);
413 if Single_Lock then
414 STPO.Unlock_RTS;
415 end if;
417 elsif Entry_Call.Mode /= Conditional_Call then
418 Object.Entry_Queue := Entry_Call;
419 else
420 -- Conditional_Call
422 if Single_Lock then
423 STPO.Lock_RTS;
424 end if;
426 STPO.Write_Lock (Entry_Call.Self);
427 Wakeup_Entry_Caller (Self_Id, Entry_Call, Cancelled);
428 STPO.Unlock (Entry_Call.Self);
430 if Single_Lock then
431 STPO.Unlock_RTS;
432 end if;
433 end if;
435 exception
436 when others =>
437 Send_Program_Error
438 (Self_Id, Entry_Call);
439 end PO_Do_Or_Queue;
441 ----------------------------
442 -- Protected_Single_Count --
443 ----------------------------
445 function Protected_Count_Entry (Object : Protection_Entry) return Natural is
446 begin
447 if Object.Call_In_Progress /= null then
448 return 1;
449 else
450 return 0;
451 end if;
452 end Protected_Count_Entry;
454 ---------------------------------
455 -- Protected_Single_Entry_Call --
456 ---------------------------------
458 procedure Protected_Single_Entry_Call
459 (Object : Protection_Entry_Access;
460 Uninterpreted_Data : System.Address;
461 Mode : Call_Modes)
463 Self_Id : constant Task_ID := STPO.Self;
464 Entry_Call : Entry_Call_Record renames Self_Id.Entry_Calls (1);
465 Ceiling_Violation : Boolean;
467 begin
468 STPO.Write_Lock (Object.L'Access, Ceiling_Violation);
470 if Ceiling_Violation then
471 raise Program_Error;
472 end if;
474 Entry_Call.Mode := Mode;
475 Entry_Call.State := Now_Abortable;
476 Entry_Call.Uninterpreted_Data := Uninterpreted_Data;
477 Entry_Call.Exception_To_Raise := Ada.Exceptions.Null_Id;
479 PO_Do_Or_Queue (Self_Id, Object, Entry_Call'Access);
480 Unlock_Entry (Object);
482 -- The call is either `Done' or not. It cannot be cancelled since there
483 -- is no ATC construct.
485 pragma Assert (Entry_Call.State /= Cancelled);
487 if Entry_Call.State /= Done then
488 if Single_Lock then
489 STPO.Lock_RTS;
490 end if;
492 STPO.Write_Lock (Self_Id);
493 Wait_For_Completion (Entry_Call'Access);
494 STPO.Unlock (Self_Id);
496 if Single_Lock then
497 STPO.Unlock_RTS;
498 end if;
499 end if;
501 Check_Exception (Self_Id, Entry_Call'Access);
502 end Protected_Single_Entry_Call;
504 -----------------------------------
505 -- Protected_Single_Entry_Caller --
506 -----------------------------------
508 function Protected_Single_Entry_Caller
509 (Object : Protection_Entry) return Task_ID is
510 begin
511 return Object.Call_In_Progress.Self;
512 end Protected_Single_Entry_Caller;
514 -------------------
515 -- Service_Entry --
516 -------------------
518 procedure Service_Entry (Object : Protection_Entry_Access) is
519 Self_Id : constant Task_ID := STPO.Self;
520 Entry_Call : constant Entry_Call_Link := Object.Entry_Queue;
521 Caller : Task_ID;
522 Barrier_Value : Boolean;
524 begin
525 if Entry_Call /= null then
526 Barrier_Value := Object.Entry_Body.Barrier (Object.Compiler_Info, 1);
528 if Barrier_Value then
529 if Object.Call_In_Progress /= null then
530 -- This violates the No_Entry_Queue restriction, send
531 -- Program_Error to the caller.
533 Send_Program_Error (Self_Id, Entry_Call);
534 return;
535 end if;
537 Object.Call_In_Progress := Entry_Call;
538 Object.Entry_Body.Action
539 (Object.Compiler_Info, Entry_Call.Uninterpreted_Data, 1);
540 Object.Call_In_Progress := null;
541 Caller := Entry_Call.Self;
543 if Single_Lock then
544 STPO.Lock_RTS;
545 end if;
547 STPO.Write_Lock (Caller);
548 Wakeup_Entry_Caller (Self_Id, Entry_Call, Done);
549 STPO.Unlock (Caller);
551 if Single_Lock then
552 STPO.Unlock_RTS;
553 end if;
554 end if;
555 end if;
557 exception
558 when others =>
559 Send_Program_Error (Self_Id, Entry_Call);
560 end Service_Entry;
562 ---------------------------------------
563 -- Timed_Protected_Single_Entry_Call --
564 ---------------------------------------
566 -- Compiler interface only. Do not call from within the RTS.
568 procedure Timed_Protected_Single_Entry_Call
569 (Object : Protection_Entry_Access;
570 Uninterpreted_Data : System.Address;
571 Timeout : Duration;
572 Mode : Delay_Modes;
573 Entry_Call_Successful : out Boolean)
575 Self_Id : constant Task_ID := STPO.Self;
576 Entry_Call : Entry_Call_Record renames Self_Id.Entry_Calls (1);
577 Ceiling_Violation : Boolean;
579 begin
580 STPO.Write_Lock (Object.L'Access, Ceiling_Violation);
582 if Ceiling_Violation then
583 raise Program_Error;
584 end if;
586 Entry_Call.Mode := Timed_Call;
587 Entry_Call.State := Now_Abortable;
588 Entry_Call.Uninterpreted_Data := Uninterpreted_Data;
589 Entry_Call.Exception_To_Raise := Ada.Exceptions.Null_Id;
591 PO_Do_Or_Queue (Self_Id, Object, Entry_Call'Access);
592 Unlock_Entry (Object);
594 -- Try to avoid waiting for completed calls.
595 -- The call is either `Done' or not. It cannot be cancelled since there
596 -- is no ATC construct and the timed wait has not started yet.
598 pragma Assert (Entry_Call.State /= Cancelled);
600 if Entry_Call.State = Done then
601 Check_Exception (Self_Id, Entry_Call'Access);
602 Entry_Call_Successful := True;
603 return;
604 end if;
606 if Single_Lock then
607 STPO.Lock_RTS;
608 else
609 STPO.Write_Lock (Self_Id);
610 end if;
612 Wait_For_Completion_With_Timeout (Entry_Call'Access, Timeout, Mode);
614 if Single_Lock then
615 STPO.Unlock_RTS;
616 else
617 STPO.Unlock (Self_Id);
618 end if;
620 pragma Assert (Entry_Call.State >= Done);
622 Check_Exception (Self_Id, Entry_Call'Access);
623 Entry_Call_Successful := Entry_Call.State = Done;
624 end Timed_Protected_Single_Entry_Call;
626 ------------------
627 -- Unlock_Entry --
628 ------------------
630 procedure Unlock_Entry (Object : Protection_Entry_Access) is
631 begin
632 STPO.Unlock (Object.L'Access);
633 end Unlock_Entry;
635 end System.Tasking.Protected_Objects.Single_Entry;