1 ------------------------------------------------------------------------------
3 -- GNAT RUN-TIME LIBRARY (GNARL) COMPONENTS --
5 -- S Y S T E M . T A S K I N G . E N T R Y _ C A L L S --
9 -- Copyright (C) 1992-2007, Free Software Foundation, Inc. --
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. --
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. --
29 -- GNARL was developed by the GNARL team at Florida State University. --
30 -- Extensive contributions were provided by Ada Core Technologies, Inc. --
32 ------------------------------------------------------------------------------
34 with System
.Task_Primitives
.Operations
;
35 -- used for STPO.Write_Lock
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
56 with System
.Tasking
.Utilities
;
57 -- used for Exit_One_ATC_Level
59 with System
.Parameters
;
60 -- used for Single_Lock
64 -- used for Send_Trace_Info
66 package body System
.Tasking
.Entry_Calls
is
68 package STPO
renames System
.Task_Primitives
.Operations
;
72 use Protected_Objects
.Entries
;
73 use Protected_Objects
.Operations
;
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
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
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
133 procedure Poll_Base_Priority_Change_At_Entry_Call
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
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
;
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
170 ------------------------------------------
171 -- Check_Pending_Actions_For_Entry_Call --
172 ------------------------------------------
174 procedure Check_Pending_Actions_For_Entry_Call
176 Entry_Call
: Entry_Call_Link
)
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
186 STPO
.Unlock
(Self_ID
);
187 Lock_Server
(Entry_Call
);
189 if Queuing
.Onqueue
(Entry_Call
)
190 and then Entry_Call
.State
= Now_Abortable
192 Queuing
.Dequeue_Call
(Entry_Call
);
194 if Entry_Call
.Cancellation_Attempted
then
195 Entry_Call
.State
:= Cancelled
;
197 Entry_Call
.State
:= Done
;
200 Unlock_And_Update_Server
(Self_ID
, Entry_Call
);
203 Unlock_Server
(Entry_Call
);
206 STPO
.Write_Lock
(Self_ID
);
208 end Check_Pending_Actions_For_Entry_Call
;
214 procedure Lock_Server
(Entry_Call
: Entry_Call_Link
) is
216 Test_PO
: Protection_Entries_Access
;
217 Ceiling_Violation
: Boolean;
218 Failures
: Integer := 0;
221 Test_Task
:= Entry_Call
.Called_Task
;
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.
249 Lock_Entries
(Test_PO
, Ceiling_Violation
);
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
264 Current_Task
: constant Task_Id
:= STPO
.Self
;
265 Old_Base_Priority
: System
.Any_Priority
;
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
277 STPO
.Unlock
(Current_Task
);
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;
292 exit when To_Address
(Test_PO
) = Entry_Call
.Called_PO
;
293 Unlock_Entries
(Test_PO
);
301 STPO
.Write_Lock
(Test_Task
);
302 exit when Test_Task
= Entry_Call
.Called_Task
;
303 STPO
.Unlock
(Test_Task
);
306 Test_Task
:= Entry_Call
.Called_Task
;
307 Failures
:= Failures
+ 1;
308 pragma Assert
(Failures
<= 5);
312 ---------------------------------------------
313 -- Poll_Base_Priority_Change_At_Entry_Call --
314 ---------------------------------------------
316 procedure Poll_Base_Priority_Change_At_Entry_Call
318 Entry_Call
: Entry_Call_Link
)
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
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
);
338 end Poll_Base_Priority_Change_At_Entry_Call
;
344 procedure Reset_Priority
346 Acceptor_Prev_Priority
: Rendezvous_Priority
)
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);
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
;
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
);
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;
391 Entry_Calls
.Wait_For_Completion
(Entry_Call
);
392 STPO
.Unlock
(Self_ID
);
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
);
412 Entry_Calls
.Check_Exception
(Self_ID
, Entry_Call
);
414 end Try_To_Cancel_Entry_Call
;
416 ------------------------------
417 -- Unlock_And_Update_Server --
418 ------------------------------
420 procedure Unlock_And_Update_Server
422 Entry_Call
: Entry_Call_Link
)
424 Called_PO
: Protection_Entries_Access
;
428 if Entry_Call
.Called_Task
/= null then
429 STPO
.Unlock
(Entry_Call
.Called_Task
);
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;
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
);
452 Unlock_Entries
(Called_PO
);
458 end Unlock_And_Update_Server
;
464 procedure Unlock_Server
(Entry_Call
: Entry_Call_Link
) is
466 Called_PO
: Protection_Entries_Access
;
469 if Entry_Call
.Called_Task
/= null then
470 STPO
.Unlock
(Entry_Call
.Called_Task
);
472 Called_PO
:= To_Protection
(Entry_Call
.Called_PO
);
474 if Called_PO
.Pending_Action
then
475 Called_PO
.Pending_Action
:= False;
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
);
492 Unlock_Entries
(Called_PO
);
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
;
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
);
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.
524 STPO
.Unlock
(Self_Id
);
527 if Entry_Call
.State
< Done
then
534 STPO
.Write_Lock
(Self_Id
);
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
);
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
);
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;
561 Yielded
: out Boolean)
563 Self_Id
: constant Task_Id
:= Entry_Call
.Self
;
564 Timedout
: Boolean := False;
566 use type Ada
.Exceptions
.Exception_Id
;
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
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
);
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
);
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
);
614 if Parameters
.Runtime_Traces
then
615 Send_Trace_Info
(E_Timeout
);
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;
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.
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
);
638 Self_Id
.Common
.State
:= Runnable
;
639 Utilities
.Exit_One_ATC_Level
(Self_Id
);
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
);
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
662 Call
: Entry_Call_Link
)
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
);
672 STPO
.Write_Lock
(Self_ID
);
673 Self_ID
.Common
.State
:= Entry_Caller_Sleep
;
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
);
681 Self_ID
.Common
.State
:= Runnable
;
682 STPO
.Unlock
(Self_ID
);
684 if Parameters
.Runtime_Traces
then
685 Send_Trace_Info
(M_Call_Complete
);
687 end Wait_Until_Abortable
;
689 end System
.Tasking
.Entry_Calls
;