Daily bump.
[official-gcc.git] / gcc / ada / s-taenca.adb
blob3da82bf60bad99ec8ce1014f5734523207002084
1 ------------------------------------------------------------------------------
2 -- --
3 -- GNAT RUN-TIME LIBRARY (GNARL) COMPONENTS --
4 -- --
5 -- S Y S T E M . T A S K I N G . E N T R Y _ C A L L S --
6 -- --
7 -- B o d y --
8 -- --
9 -- Copyright (C) 1992-2007, 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, 51 Franklin Street, Fifth Floor, --
20 -- Boston, MA 02110-1301, 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. --
30 -- Extensive contributions were provided by Ada Core Technologies, Inc. --
31 -- --
32 ------------------------------------------------------------------------------
34 with System.Task_Primitives.Operations;
35 -- used for STPO.Write_Lock
36 -- Unlock
37 -- STPO.Get_Priority
38 -- Sleep
39 -- Timed_Sleep
41 with System.Tasking.Initialization;
42 -- used for Change_Base_Priority
43 -- Defer_Abort/Undefer_Abort
45 with System.Tasking.Protected_Objects.Entries;
46 -- used for To_Protection
48 with System.Tasking.Protected_Objects.Operations;
49 -- used for PO_Service_Entries
51 with System.Tasking.Queuing;
52 -- used for Requeue_Call_With_New_Prio
53 -- Onqueue
54 -- Dequeue_Call
56 with System.Tasking.Utilities;
57 -- used for Exit_One_ATC_Level
59 with System.Parameters;
60 -- used for Single_Lock
61 -- Runtime_Traces
63 with System.Traces;
64 -- used for Send_Trace_Info
66 package body System.Tasking.Entry_Calls is
68 package STPO renames System.Task_Primitives.Operations;
70 use Parameters;
71 use Task_Primitives;
72 use Protected_Objects.Entries;
73 use Protected_Objects.Operations;
74 use System.Traces;
76 -- DO NOT use Protected_Objects.Lock or Protected_Objects.Unlock
77 -- internally. Those operations will raise Program_Error, which
78 -- we are not prepared to handle inside the RTS. Instead, use
79 -- System.Task_Primitives lock operations directly on Protection.L.
81 -----------------------
82 -- Local Subprograms --
83 -----------------------
85 procedure Lock_Server (Entry_Call : Entry_Call_Link);
87 -- This locks the server targeted by Entry_Call
89 -- This may be a task or a protected object, depending on the target of the
90 -- original call or any subsequent requeues.
92 -- This routine is needed because the field specifying the server for this
93 -- call must be protected by the server's mutex. If it were protected by
94 -- the caller's mutex, accessing the server's queues would require locking
95 -- the caller to get the server, locking the server, and then accessing the
96 -- queues. This involves holding two ATCB locks at once, something which we
97 -- can guarantee that it will always be done in the same order, or locking
98 -- a protected object while we hold an ATCB lock, something which is not
99 -- permitted. Since the server cannot be obtained reliably, it must be
100 -- obtained unreliably and then checked again once it has been locked.
102 -- If Single_Lock and server is a PO, release RTS_Lock
104 -- This should only be called by the Entry_Call.Self.
105 -- It should be holding no other ATCB locks at the time.
107 procedure Unlock_Server (Entry_Call : Entry_Call_Link);
108 -- STPO.Unlock the server targeted by Entry_Call. The server must
109 -- be locked before calling this.
111 -- If Single_Lock and server is a PO, take RTS_Lock on exit.
113 procedure Unlock_And_Update_Server
114 (Self_ID : Task_Id;
115 Entry_Call : Entry_Call_Link);
116 -- Similar to Unlock_Server, but services entry calls if the
117 -- server is a protected object.
119 -- If Single_Lock and server is a PO, take RTS_Lock on exit.
121 procedure Check_Pending_Actions_For_Entry_Call
122 (Self_ID : Task_Id;
123 Entry_Call : Entry_Call_Link);
124 -- This procedure performs priority change of a queued call and dequeuing
125 -- of an entry call when the call is cancelled. If the call is dequeued the
126 -- state should be set to Cancelled. Call only with abort deferred and
127 -- holding lock of Self_ID. This is a bit of common code for all entry
128 -- calls. The effect is to do any deferred base priority change operation,
129 -- in case some other task called STPO.Set_Priority while the current task
130 -- had abort deferred, and to dequeue the call if the call has been
131 -- aborted.
133 procedure Poll_Base_Priority_Change_At_Entry_Call
134 (Self_ID : Task_Id;
135 Entry_Call : Entry_Call_Link);
136 pragma Inline (Poll_Base_Priority_Change_At_Entry_Call);
137 -- A specialized version of Poll_Base_Priority_Change, that does the
138 -- optional entry queue reordering. Has to be called with the Self_ID's
139 -- ATCB write-locked. May temporariliy release the lock.
141 ---------------------
142 -- Check_Exception --
143 ---------------------
145 procedure Check_Exception
146 (Self_ID : Task_Id;
147 Entry_Call : Entry_Call_Link)
149 pragma Warnings (Off, Self_ID);
151 use type Ada.Exceptions.Exception_Id;
153 procedure Internal_Raise (X : Ada.Exceptions.Exception_Id);
154 pragma Import (C, Internal_Raise, "__gnat_raise_with_msg");
156 E : constant Ada.Exceptions.Exception_Id :=
157 Entry_Call.Exception_To_Raise;
158 begin
159 -- pragma Assert (Self_ID.Deferral_Level = 0);
161 -- The above may be useful for debugging, but the Florist packages
162 -- contain critical sections that defer abort and then do entry calls,
163 -- which causes the above Assert to trip.
165 if E /= Ada.Exceptions.Null_Id then
166 Internal_Raise (E);
167 end if;
168 end Check_Exception;
170 ------------------------------------------
171 -- Check_Pending_Actions_For_Entry_Call --
172 ------------------------------------------
174 procedure Check_Pending_Actions_For_Entry_Call
175 (Self_ID : Task_Id;
176 Entry_Call : Entry_Call_Link)
178 begin
179 pragma Assert (Self_ID = Entry_Call.Self);
181 Poll_Base_Priority_Change_At_Entry_Call (Self_ID, Entry_Call);
183 if Self_ID.Pending_ATC_Level < Self_ID.ATC_Nesting_Level
184 and then Entry_Call.State = Now_Abortable
185 then
186 STPO.Unlock (Self_ID);
187 Lock_Server (Entry_Call);
189 if Queuing.Onqueue (Entry_Call)
190 and then Entry_Call.State = Now_Abortable
191 then
192 Queuing.Dequeue_Call (Entry_Call);
194 if Entry_Call.Cancellation_Attempted then
195 Entry_Call.State := Cancelled;
196 else
197 Entry_Call.State := Done;
198 end if;
200 Unlock_And_Update_Server (Self_ID, Entry_Call);
202 else
203 Unlock_Server (Entry_Call);
204 end if;
206 STPO.Write_Lock (Self_ID);
207 end if;
208 end Check_Pending_Actions_For_Entry_Call;
210 -----------------
211 -- Lock_Server --
212 -----------------
214 procedure Lock_Server (Entry_Call : Entry_Call_Link) is
215 Test_Task : Task_Id;
216 Test_PO : Protection_Entries_Access;
217 Ceiling_Violation : Boolean;
218 Failures : Integer := 0;
220 begin
221 Test_Task := Entry_Call.Called_Task;
223 loop
224 if Test_Task = null then
226 -- Entry_Call was queued on a protected object, or in transition,
227 -- when we last fetched Test_Task.
229 Test_PO := To_Protection (Entry_Call.Called_PO);
231 if Test_PO = null then
233 -- We had very bad luck, interleaving with TWO different
234 -- requeue operations. Go around the loop and try again.
236 if Single_Lock then
237 STPO.Unlock_RTS;
238 STPO.Yield;
239 STPO.Lock_RTS;
240 else
241 STPO.Yield;
242 end if;
244 else
245 if Single_Lock then
246 STPO.Unlock_RTS;
247 end if;
249 Lock_Entries (Test_PO, Ceiling_Violation);
251 -- ???
253 -- The following code allows Lock_Server to be called when
254 -- cancelling a call, to allow for the possibility that the
255 -- priority of the caller has been raised beyond that of the
256 -- protected entry call by Ada.Dynamic_Priorities.Set_Priority.
258 -- If the current task has a higher priority than the ceiling
259 -- of the protected object, temporarily lower it. It will
260 -- be reset in Unlock.
262 if Ceiling_Violation then
263 declare
264 Current_Task : constant Task_Id := STPO.Self;
265 Old_Base_Priority : System.Any_Priority;
267 begin
268 if Single_Lock then
269 STPO.Lock_RTS;
270 end if;
272 STPO.Write_Lock (Current_Task);
273 Old_Base_Priority := Current_Task.Common.Base_Priority;
274 Current_Task.New_Base_Priority := Test_PO.Ceiling;
275 System.Tasking.Initialization.Change_Base_Priority
276 (Current_Task);
277 STPO.Unlock (Current_Task);
279 if Single_Lock then
280 STPO.Unlock_RTS;
281 end if;
283 -- Following lock should not fail
285 Lock_Entries (Test_PO);
287 Test_PO.Old_Base_Priority := Old_Base_Priority;
288 Test_PO.Pending_Action := True;
289 end;
290 end if;
292 exit when To_Address (Test_PO) = Entry_Call.Called_PO;
293 Unlock_Entries (Test_PO);
295 if Single_Lock then
296 STPO.Lock_RTS;
297 end if;
298 end if;
300 else
301 STPO.Write_Lock (Test_Task);
302 exit when Test_Task = Entry_Call.Called_Task;
303 STPO.Unlock (Test_Task);
304 end if;
306 Test_Task := Entry_Call.Called_Task;
307 Failures := Failures + 1;
308 pragma Assert (Failures <= 5);
309 end loop;
310 end Lock_Server;
312 ---------------------------------------------
313 -- Poll_Base_Priority_Change_At_Entry_Call --
314 ---------------------------------------------
316 procedure Poll_Base_Priority_Change_At_Entry_Call
317 (Self_ID : Task_Id;
318 Entry_Call : Entry_Call_Link)
320 begin
321 if Self_ID.Pending_Priority_Change then
323 -- Check for ceiling violations ???
325 Self_ID.Pending_Priority_Change := False;
327 -- Requeue the entry call at the new priority. We need to requeue
328 -- even if the new priority is the same than the previous (see ACATS
329 -- test cxd4006).
331 STPO.Unlock (Self_ID);
332 Lock_Server (Entry_Call);
333 Queuing.Requeue_Call_With_New_Prio
334 (Entry_Call, STPO.Get_Priority (Self_ID));
335 Unlock_And_Update_Server (Self_ID, Entry_Call);
336 STPO.Write_Lock (Self_ID);
337 end if;
338 end Poll_Base_Priority_Change_At_Entry_Call;
340 --------------------
341 -- Reset_Priority --
342 --------------------
344 procedure Reset_Priority
345 (Acceptor : Task_Id;
346 Acceptor_Prev_Priority : Rendezvous_Priority)
348 begin
349 pragma Assert (Acceptor = STPO.Self);
351 -- Since we limit this kind of "active" priority change to be done
352 -- by the task for itself, we don't need to lock Acceptor.
354 if Acceptor_Prev_Priority /= Priority_Not_Boosted then
355 STPO.Set_Priority (Acceptor, Acceptor_Prev_Priority,
356 Loss_Of_Inheritance => True);
357 end if;
358 end Reset_Priority;
360 ------------------------------
361 -- Try_To_Cancel_Entry_Call --
362 ------------------------------
364 procedure Try_To_Cancel_Entry_Call (Succeeded : out Boolean) is
365 Entry_Call : Entry_Call_Link;
366 Self_ID : constant Task_Id := STPO.Self;
368 use type Ada.Exceptions.Exception_Id;
370 begin
371 Entry_Call := Self_ID.Entry_Calls (Self_ID.ATC_Nesting_Level)'Access;
373 -- Experimentation has shown that abort is sometimes (but not
374 -- always) already deferred when Cancel_xxx_Entry_Call is called.
375 -- That may indicate an error. Find out what is going on. ???
377 pragma Assert (Entry_Call.Mode = Asynchronous_Call);
378 Initialization.Defer_Abort_Nestable (Self_ID);
380 if Single_Lock then
381 STPO.Lock_RTS;
382 end if;
384 STPO.Write_Lock (Self_ID);
385 Entry_Call.Cancellation_Attempted := True;
387 if Self_ID.Pending_ATC_Level >= Entry_Call.Level then
388 Self_ID.Pending_ATC_Level := Entry_Call.Level - 1;
389 end if;
391 Entry_Calls.Wait_For_Completion (Entry_Call);
392 STPO.Unlock (Self_ID);
394 if Single_Lock then
395 STPO.Unlock_RTS;
396 end if;
398 Succeeded := Entry_Call.State = Cancelled;
400 Initialization.Undefer_Abort_Nestable (Self_ID);
402 -- Ideally, abort should no longer be deferred at this point, so we
403 -- should be able to call Check_Exception. The loop below should be
404 -- considered temporary, to work around the possibility that abort
405 -- may be deferred more than one level deep ???
407 if Entry_Call.Exception_To_Raise /= Ada.Exceptions.Null_Id then
408 while Self_ID.Deferral_Level > 0 loop
409 System.Tasking.Initialization.Undefer_Abort_Nestable (Self_ID);
410 end loop;
412 Entry_Calls.Check_Exception (Self_ID, Entry_Call);
413 end if;
414 end Try_To_Cancel_Entry_Call;
416 ------------------------------
417 -- Unlock_And_Update_Server --
418 ------------------------------
420 procedure Unlock_And_Update_Server
421 (Self_ID : Task_Id;
422 Entry_Call : Entry_Call_Link)
424 Called_PO : Protection_Entries_Access;
425 Caller : Task_Id;
427 begin
428 if Entry_Call.Called_Task /= null then
429 STPO.Unlock (Entry_Call.Called_Task);
430 else
431 Called_PO := To_Protection (Entry_Call.Called_PO);
432 PO_Service_Entries (Self_ID, Called_PO, False);
434 if Called_PO.Pending_Action then
435 Called_PO.Pending_Action := False;
436 Caller := STPO.Self;
438 if Single_Lock then
439 STPO.Lock_RTS;
440 end if;
442 STPO.Write_Lock (Caller);
443 Caller.New_Base_Priority := Called_PO.Old_Base_Priority;
444 Initialization.Change_Base_Priority (Caller);
445 STPO.Unlock (Caller);
447 if Single_Lock then
448 STPO.Unlock_RTS;
449 end if;
450 end if;
452 Unlock_Entries (Called_PO);
454 if Single_Lock then
455 STPO.Lock_RTS;
456 end if;
457 end if;
458 end Unlock_And_Update_Server;
460 -------------------
461 -- Unlock_Server --
462 -------------------
464 procedure Unlock_Server (Entry_Call : Entry_Call_Link) is
465 Caller : Task_Id;
466 Called_PO : Protection_Entries_Access;
468 begin
469 if Entry_Call.Called_Task /= null then
470 STPO.Unlock (Entry_Call.Called_Task);
471 else
472 Called_PO := To_Protection (Entry_Call.Called_PO);
474 if Called_PO.Pending_Action then
475 Called_PO.Pending_Action := False;
476 Caller := STPO.Self;
478 if Single_Lock then
479 STPO.Lock_RTS;
480 end if;
482 STPO.Write_Lock (Caller);
483 Caller.New_Base_Priority := Called_PO.Old_Base_Priority;
484 Initialization.Change_Base_Priority (Caller);
485 STPO.Unlock (Caller);
487 if Single_Lock then
488 STPO.Unlock_RTS;
489 end if;
490 end if;
492 Unlock_Entries (Called_PO);
494 if Single_Lock then
495 STPO.Lock_RTS;
496 end if;
497 end if;
498 end Unlock_Server;
500 -------------------------
501 -- Wait_For_Completion --
502 -------------------------
504 procedure Wait_For_Completion (Entry_Call : Entry_Call_Link) is
505 Self_Id : constant Task_Id := Entry_Call.Self;
507 begin
508 -- If this is a conditional call, it should be cancelled when it
509 -- becomes abortable. This is checked in the loop below.
511 if Parameters.Runtime_Traces then
512 Send_Trace_Info (W_Completion);
513 end if;
515 Self_Id.Common.State := Entry_Caller_Sleep;
517 -- Try to remove calls to Sleep in the loop below by letting the caller
518 -- a chance of getting ready immediately, using Unlock & Yield.
519 -- See similar action in Wait_For_Call & Timed_Selective_Wait.
521 if Single_Lock then
522 STPO.Unlock_RTS;
523 else
524 STPO.Unlock (Self_Id);
525 end if;
527 if Entry_Call.State < Done then
528 STPO.Yield;
529 end if;
531 if Single_Lock then
532 STPO.Lock_RTS;
533 else
534 STPO.Write_Lock (Self_Id);
535 end if;
537 loop
538 Check_Pending_Actions_For_Entry_Call (Self_Id, Entry_Call);
540 exit when Entry_Call.State >= Done;
542 STPO.Sleep (Self_Id, Entry_Caller_Sleep);
543 end loop;
545 Self_Id.Common.State := Runnable;
546 Utilities.Exit_One_ATC_Level (Self_Id);
548 if Parameters.Runtime_Traces then
549 Send_Trace_Info (M_Call_Complete);
550 end if;
551 end Wait_For_Completion;
553 --------------------------------------
554 -- Wait_For_Completion_With_Timeout --
555 --------------------------------------
557 procedure Wait_For_Completion_With_Timeout
558 (Entry_Call : Entry_Call_Link;
559 Wakeup_Time : Duration;
560 Mode : Delay_Modes;
561 Yielded : out Boolean)
563 Self_Id : constant Task_Id := Entry_Call.Self;
564 Timedout : Boolean := False;
566 use type Ada.Exceptions.Exception_Id;
568 begin
569 -- This procedure waits for the entry call to be served, with a timeout.
570 -- It tries to cancel the call if the timeout expires before the call is
571 -- served.
573 -- If we wake up from the timed sleep operation here, it may be for
574 -- several possible reasons:
576 -- 1) The entry call is done being served.
577 -- 2) There is an abort or priority change to be served.
578 -- 3) The timeout has expired (Timedout = True)
579 -- 4) There has been a spurious wakeup.
581 -- Once the timeout has expired we may need to continue to wait if the
582 -- call is already being serviced. In that case, we want to go back to
583 -- sleep, but without any timeout. The variable Timedout is used to
584 -- control this. If the Timedout flag is set, we do not need to
585 -- STPO.Sleep with a timeout. We just sleep until we get a wakeup for
586 -- some status change.
588 -- The original call may have become abortable after waking up. We want
589 -- to check Check_Pending_Actions_For_Entry_Call again in any case.
591 pragma Assert (Entry_Call.Mode = Timed_Call);
593 Yielded := False;
594 Self_Id.Common.State := Entry_Caller_Sleep;
596 -- Looping is necessary in case the task wakes up early from the timed
597 -- sleep, due to a "spurious wakeup". Spurious wakeups are a weakness of
598 -- POSIX condition variables. A thread waiting for a condition variable
599 -- is allowed to wake up at any time, not just when the condition is
600 -- signaled. See same loop in the ordinary Wait_For_Completion, above.
602 if Parameters.Runtime_Traces then
603 Send_Trace_Info (WT_Completion, Wakeup_Time);
604 end if;
606 loop
607 Check_Pending_Actions_For_Entry_Call (Self_Id, Entry_Call);
608 exit when Entry_Call.State >= Done;
610 STPO.Timed_Sleep (Self_Id, Wakeup_Time, Mode,
611 Entry_Caller_Sleep, Timedout, Yielded);
613 if Timedout then
614 if Parameters.Runtime_Traces then
615 Send_Trace_Info (E_Timeout);
616 end if;
618 -- Try to cancel the call (see Try_To_Cancel_Entry_Call for
619 -- corresponding code in the ATC case).
621 Entry_Call.Cancellation_Attempted := True;
623 if Self_Id.Pending_ATC_Level >= Entry_Call.Level then
624 Self_Id.Pending_ATC_Level := Entry_Call.Level - 1;
625 end if;
627 -- The following loop is the same as the loop and exit code
628 -- from the ordinary Wait_For_Completion. If we get here, we
629 -- have timed out but we need to keep waiting until the call
630 -- has actually completed or been cancelled successfully.
632 loop
633 Check_Pending_Actions_For_Entry_Call (Self_Id, Entry_Call);
634 exit when Entry_Call.State >= Done;
635 STPO.Sleep (Self_Id, Entry_Caller_Sleep);
636 end loop;
638 Self_Id.Common.State := Runnable;
639 Utilities.Exit_One_ATC_Level (Self_Id);
641 return;
642 end if;
643 end loop;
645 -- This last part is the same as ordinary Wait_For_Completion,
646 -- and is only executed if the call completed without timing out.
648 if Parameters.Runtime_Traces then
649 Send_Trace_Info (M_Call_Complete);
650 end if;
652 Self_Id.Common.State := Runnable;
653 Utilities.Exit_One_ATC_Level (Self_Id);
654 end Wait_For_Completion_With_Timeout;
656 --------------------------
657 -- Wait_Until_Abortable --
658 --------------------------
660 procedure Wait_Until_Abortable
661 (Self_ID : Task_Id;
662 Call : Entry_Call_Link)
664 begin
665 pragma Assert (Self_ID.ATC_Nesting_Level > 0);
666 pragma Assert (Call.Mode = Asynchronous_Call);
668 if Parameters.Runtime_Traces then
669 Send_Trace_Info (W_Completion);
670 end if;
672 STPO.Write_Lock (Self_ID);
673 Self_ID.Common.State := Entry_Caller_Sleep;
675 loop
676 Check_Pending_Actions_For_Entry_Call (Self_ID, Call);
677 exit when Call.State >= Was_Abortable;
678 STPO.Sleep (Self_ID, Async_Select_Sleep);
679 end loop;
681 Self_ID.Common.State := Runnable;
682 STPO.Unlock (Self_ID);
684 if Parameters.Runtime_Traces then
685 Send_Trace_Info (M_Call_Complete);
686 end if;
687 end Wait_Until_Abortable;
689 end System.Tasking.Entry_Calls;