1 ------------------------------------------------------------------------------
3 -- GNU ADA RUN-TIME LIBRARY (GNARL) COMPONENTS --
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 --
12 -- Copyright (C) 1998-2001 Ada Core Technologies --
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. --
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. --
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). --
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
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,
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
;
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
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
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
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
124 Entry_Call
: Entry_Call_Link
;
125 Wakeup_Time
: Duration;
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
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
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
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
;
165 if E
/= Ada
.Exceptions
.Null_Id
then
170 ------------------------
171 -- Send_Program_Error --
172 ------------------------
174 procedure Send_Program_Error
176 Entry_Call
: Entry_Call_Link
)
178 Caller
: constant Task_ID
:= Entry_Call
.Self
;
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
194 Entry_Call
: Entry_Call_Link
) is
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
229 Entry_Call
: Entry_Call_Link
;
230 Wakeup_Time
: Duration;
236 use type Ada
.Exceptions
.Exception_Id
;
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
;
246 (Self_ID
, Wakeup_Time
, Mode
, Entry_Caller_Sleep
, Timedout
, Yielded
);
249 Entry_Call
.State
:= Cancelled
;
251 Entry_Call
.State
:= Done
;
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.
274 -- The caller might be in Wait_For_Completion,
275 -- waiting for a rendezvous (possibly requeued without abort)
278 procedure Wakeup_Entry_Caller
280 Entry_Call
: Entry_Call_Link
;
281 New_State
: Entry_Call_State
)
283 Caller
: constant Task_ID
:= Entry_Call
.Self
;
285 pragma Assert
(New_State
= Done
or else New_State
= Cancelled
);
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
304 -- Nothing needs to be done since
305 -- Object.Call_In_Progress.Exception_To_Raise has already been set to
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
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
;
334 if Init_Priority
= Unspecified_Priority
then
335 Init_Priority
:= System
.Priority
'Last;
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
;
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;
356 STPO
.Write_Lock
(Object
.L
'Access, Ceiling_Violation
);
358 if Ceiling_Violation
then
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;
373 STPO
.Read_Lock
(Object
.L
'Access, Ceiling_Violation
);
375 if Ceiling_Violation
then
378 end Lock_Read_Only_Entry
;
384 procedure PO_Do_Or_Queue
386 Object
: Protection_Entry_Access
;
387 Entry_Call
: Entry_Call_Link
)
389 Barrier_Value
: Boolean;
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
);
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
;
416 STPO
.Write_Lock
(Entry_Call
.Self
);
417 Wakeup_Entry_Caller
(Self_Id
, Entry_Call
, Cancelled
);
418 STPO
.Unlock
(Entry_Call
.Self
);
424 (Self_Id
, Entry_Call
);
427 ----------------------------
428 -- Protected_Single_Count --
429 ----------------------------
431 function Protected_Count_Entry
(Object
: Protection_Entry
) return Natural is
433 if Object
.Call_In_Progress
/= null then
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
;
449 Self_Id
: constant Task_ID
:= STPO
.Self
;
450 Entry_Call
: Entry_Call_Record
renames Self_Id
.Entry_Calls
(1);
451 Ceiling_Violation
: Boolean;
454 STPO
.Write_Lock
(Object
.L
'Access, Ceiling_Violation
);
456 if Ceiling_Violation
then
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
);
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
489 return Object
.Call_In_Progress
.Self
;
490 end Protected_Single_Entry_Caller
;
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
;
500 Barrier_Value
: Boolean;
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
);
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
);
528 Send_Program_Error
(Self_Id
, Entry_Call
);
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
;
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;
549 STPO
.Write_Lock
(Object
.L
'Access, Ceiling_Violation
);
551 if Ceiling_Violation
then
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;
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
;
588 procedure Unlock_Entry
(Object
: Protection_Entry_Access
) is
590 STPO
.Unlock
(Object
.L
'Access);
593 end System
.Tasking
.Protected_Objects
.Single_Entry
;