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
);
145 ---------------------
146 -- Check_Exception --
147 ---------------------
149 procedure Check_Exception
151 Entry_Call
: Entry_Call_Link
)
153 use type Ada
.Exceptions
.Exception_Id
;
155 procedure Internal_Raise
(X
: Ada
.Exceptions
.Exception_Id
);
156 pragma Import
(C
, Internal_Raise
, "__gnat_raise_with_msg");
158 E
: constant Ada
.Exceptions
.Exception_Id
:=
159 Entry_Call
.Exception_To_Raise
;
162 if E
/= Ada
.Exceptions
.Null_Id
then
167 ------------------------
168 -- Send_Program_Error --
169 ------------------------
171 procedure Send_Program_Error
173 Entry_Call
: Entry_Call_Link
)
175 Caller
: constant Task_ID
:= Entry_Call
.Self
;
177 Entry_Call
.Exception_To_Raise
:= Program_Error
'Identity;
178 STPO
.Write_Lock
(Caller
);
179 Wakeup_Entry_Caller
(Self_Id
, Entry_Call
, Done
);
180 STPO
.Unlock
(Caller
);
181 end Send_Program_Error
;
183 -------------------------
184 -- Wait_For_Completion --
185 -------------------------
187 -- Call this only when holding Self_ID locked
189 procedure Wait_For_Completion
191 Entry_Call
: Entry_Call_Link
)
194 pragma Assert
(Self_ID
= Entry_Call
.Self
);
195 Self_ID
.Common
.State
:= Entry_Caller_Sleep
;
197 STPO
.Sleep
(Self_ID
, Entry_Caller_Sleep
);
199 Self_ID
.Common
.State
:= Runnable
;
200 end Wait_For_Completion
;
202 --------------------------------------
203 -- Wait_For_Completion_With_Timeout --
204 --------------------------------------
206 -- This routine will lock Self_ID.
208 -- This procedure waits for the entry call to
209 -- be served, with a timeout. It tries to cancel the
210 -- call if the timeout expires before the call is served.
212 -- If we wake up from the timed sleep operation here,
213 -- it may be for the following possible reasons:
215 -- 1) The entry call is done being served.
216 -- 2) The timeout has expired (Timedout = True)
218 -- Once the timeout has expired we may need to continue to wait if
219 -- the call is already being serviced. In that case, we want to go
220 -- back to sleep, but without any timeout. The variable Timedout is
221 -- used to control this. If the Timedout flag is set, we do not need
222 -- to Sleep with a timeout. We just sleep until we get a wakeup for
223 -- some status change.
225 procedure Wait_For_Completion_With_Timeout
227 Entry_Call
: Entry_Call_Link
;
228 Wakeup_Time
: Duration;
234 use type Ada
.Exceptions
.Exception_Id
;
237 STPO
.Write_Lock
(Self_ID
);
239 pragma Assert
(Entry_Call
.Self
= Self_ID
);
240 pragma Assert
(Entry_Call
.Mode
= Timed_Call
);
241 Self_ID
.Common
.State
:= Entry_Caller_Sleep
;
244 (Self_ID
, Wakeup_Time
, Mode
, Entry_Caller_Sleep
, Timedout
, Yielded
);
247 Entry_Call
.State
:= Cancelled
;
249 Entry_Call
.State
:= Done
;
252 Self_ID
.Common
.State
:= Runnable
;
253 STPO
.Unlock
(Self_ID
);
254 end Wait_For_Completion_With_Timeout
;
256 -------------------------
257 -- Wakeup_Entry_Caller --
258 -------------------------
260 -- This is called at the end of service of an entry call, to abort the
261 -- caller if he is in an abortable part, and to wake up the caller if it
262 -- is on Entry_Caller_Sleep. It assumes that the call is already off-queue.
264 -- (This enforces the rule that a task must be off-queue if its state is
265 -- Done or Cancelled.) Call it holding the lock of Entry_Call.Self.
267 -- Timed_Call or Simple_Call:
268 -- The caller is waiting on Entry_Caller_Sleep, in
269 -- Wait_For_Completion, or Wait_For_Completion_With_Timeout.
272 -- The caller might be in Wait_For_Completion,
273 -- waiting for a rendezvous (possibly requeued without abort)
276 procedure Wakeup_Entry_Caller
278 Entry_Call
: Entry_Call_Link
;
279 New_State
: Entry_Call_State
)
281 Caller
: constant Task_ID
:= Entry_Call
.Self
;
283 pragma Assert
(New_State
= Done
or else New_State
= Cancelled
);
285 (Caller
.Common
.State
/= Terminated
and then
286 Caller
.Common
.State
/= Unactivated
);
288 Entry_Call
.State
:= New_State
;
289 STPO
.Wakeup
(Caller
, Entry_Caller_Sleep
);
290 end Wakeup_Entry_Caller
;
292 -----------------------
293 -- Restricted GNARLI --
294 -----------------------
296 --------------------------------
297 -- Complete_Single_Entry_Body --
298 --------------------------------
300 procedure Complete_Single_Entry_Body
(Object
: Protection_Entry_Access
) is
302 -- Nothing needs to be done since
303 -- Object.Call_In_Progress.Exception_To_Raise has already been set to
306 end Complete_Single_Entry_Body
;
308 --------------------------------------------
309 -- Exceptional_Complete_Single_Entry_Body --
310 --------------------------------------------
312 procedure Exceptional_Complete_Single_Entry_Body
313 (Object
: Protection_Entry_Access
;
314 Ex
: Ada
.Exceptions
.Exception_Id
) is
316 Object
.Call_In_Progress
.Exception_To_Raise
:= Ex
;
317 end Exceptional_Complete_Single_Entry_Body
;
319 ---------------------------------
320 -- Initialize_Protection_Entry --
321 ---------------------------------
323 procedure Initialize_Protection_Entry
324 (Object
: Protection_Entry_Access
;
325 Ceiling_Priority
: Integer;
326 Compiler_Info
: System
.Address
;
327 Entry_Body
: Entry_Body_Access
)
329 Init_Priority
: Integer := Ceiling_Priority
;
332 if Init_Priority
= Unspecified_Priority
then
333 Init_Priority
:= System
.Priority
'Last;
336 STPO
.Initialize_Lock
(Init_Priority
, Object
.L
'Access);
337 Object
.Ceiling
:= System
.Any_Priority
(Init_Priority
);
338 Object
.Compiler_Info
:= Compiler_Info
;
339 Object
.Call_In_Progress
:= null;
340 Object
.Entry_Body
:= Entry_Body
;
341 Object
.Entry_Queue
:= null;
342 end Initialize_Protection_Entry
;
348 -- Compiler interface only.
349 -- Do not call this procedure from within the run-time system.
351 procedure Lock_Entry
(Object
: Protection_Entry_Access
) is
352 Ceiling_Violation
: Boolean;
354 STPO
.Write_Lock
(Object
.L
'Access, Ceiling_Violation
);
356 if Ceiling_Violation
then
361 --------------------------
362 -- Lock_Read_Only_Entry --
363 --------------------------
365 -- Compiler interface only.
366 -- Do not call this procedure from within the runtime system.
368 procedure Lock_Read_Only_Entry
(Object
: Protection_Entry_Access
) is
369 Ceiling_Violation
: Boolean;
371 STPO
.Read_Lock
(Object
.L
'Access, Ceiling_Violation
);
373 if Ceiling_Violation
then
376 end Lock_Read_Only_Entry
;
382 procedure PO_Do_Or_Queue
384 Object
: Protection_Entry_Access
;
385 Entry_Call
: Entry_Call_Link
)
387 Barrier_Value
: Boolean;
389 -- When the Action procedure for an entry body returns, it must be
390 -- completed (having called [Exceptional_]Complete_Entry_Body).
392 Barrier_Value
:= Object
.Entry_Body
.Barrier
(Object
.Compiler_Info
, 1);
394 if Barrier_Value
then
395 if Object
.Call_In_Progress
/= null then
396 -- This violates the No_Entry_Queue restriction, send
397 -- Program_Error to the caller.
399 Send_Program_Error
(Self_Id
, Entry_Call
);
403 Object
.Call_In_Progress
:= Entry_Call
;
404 Object
.Entry_Body
.Action
405 (Object
.Compiler_Info
, Entry_Call
.Uninterpreted_Data
, 1);
406 Object
.Call_In_Progress
:= null;
407 Wakeup_Entry_Caller
(Self_Id
, Entry_Call
, Done
);
409 elsif Entry_Call
.Mode
/= Conditional_Call
then
410 Object
.Entry_Queue
:= Entry_Call
;
414 STPO
.Write_Lock
(Entry_Call
.Self
);
415 Wakeup_Entry_Caller
(Self_Id
, Entry_Call
, Cancelled
);
416 STPO
.Unlock
(Entry_Call
.Self
);
419 exception -- not needed in no exc mode
420 when others => -- not needed in no exc mode
421 Send_Program_Error
-- not needed in no exc mode
422 (Self_Id
, Entry_Call
); -- not needed in no exc mode
425 ----------------------------
426 -- Protected_Single_Count --
427 ----------------------------
429 function Protected_Count_Entry
430 (Object
: Protection_Entry
) return Natural is
432 if Object
.Call_In_Progress
/= null then
437 end Protected_Count_Entry
;
439 ---------------------------------
440 -- Protected_Single_Entry_Call --
441 ---------------------------------
443 procedure Protected_Single_Entry_Call
444 (Object
: Protection_Entry_Access
;
445 Uninterpreted_Data
: System
.Address
;
448 Self_Id
: constant Task_ID
:= STPO
.Self
;
449 Entry_Call
: Entry_Call_Record
renames Self_Id
.Entry_Calls
(1);
450 Ceiling_Violation
: Boolean;
453 STPO
.Write_Lock
(Object
.L
'Access, Ceiling_Violation
);
455 if Ceiling_Violation
then
459 Entry_Call
.Mode
:= Mode
;
460 Entry_Call
.State
:= Now_Abortable
;
461 Entry_Call
.Uninterpreted_Data
:= Uninterpreted_Data
;
462 Entry_Call
.Exception_To_Raise
:= Ada
.Exceptions
.Null_Id
;
464 PO_Do_Or_Queue
(Self_Id
, Object
, Entry_Call
'Access);
465 Unlock_Entry
(Object
);
467 -- The call is either `Done' or not. It cannot be cancelled since there
468 -- is no ATC construct.
470 pragma Assert
(Entry_Call
.State
/= Cancelled
);
472 if Entry_Call
.State
= Done
then
473 Check_Exception
(Self_Id
, Entry_Call
'Access);
477 STPO
.Write_Lock
(Self_Id
);
478 Wait_For_Completion
(Self_Id
, Entry_Call
'Access);
479 STPO
.Unlock
(Self_Id
);
480 Check_Exception
(Self_Id
, Entry_Call
'Access);
481 end Protected_Single_Entry_Call
;
483 -----------------------------------
484 -- Protected_Single_Entry_Caller --
485 -----------------------------------
487 function Protected_Single_Entry_Caller
488 (Object
: Protection_Entry
) return Task_ID
is
490 return Object
.Call_In_Progress
.Self
;
491 end Protected_Single_Entry_Caller
;
497 procedure Service_Entry
(Object
: Protection_Entry_Access
) is
498 Self_Id
: constant Task_ID
:= STPO
.Self
;
499 Entry_Call
: Entry_Call_Link
;
501 Barrier_Value
: Boolean;
504 Entry_Call
:= Object
.Entry_Queue
;
506 if Entry_Call
/= null then
508 Object
.Entry_Body
.Barrier
(Object
.Compiler_Info
, 1);
510 if Barrier_Value
then
511 if Object
.Call_In_Progress
/= null then
513 -- This violates the No_Entry_Queue restriction, send
514 -- Program_Error to the caller.
516 Send_Program_Error
(Self_Id
, Entry_Call
);
520 Object
.Call_In_Progress
:= Entry_Call
;
521 Object
.Entry_Body
.Action
522 (Object
.Compiler_Info
, Entry_Call
.Uninterpreted_Data
, 1);
523 Object
.Call_In_Progress
:= null;
524 Caller
:= Entry_Call
.Self
;
525 STPO
.Write_Lock
(Caller
);
526 Wakeup_Entry_Caller
(Self_Id
, Entry_Call
, Done
);
527 STPO
.Unlock
(Caller
);
531 exception -- not needed in no exc mode
532 when others => -- not needed in no exc mode
533 Send_Program_Error
-- not needed in no exc mode
534 (Self_Id
, Entry_Call
); -- not needed in no exc mode
537 ---------------------------------------
538 -- Timed_Protected_Single_Entry_Call --
539 ---------------------------------------
541 -- Compiler interface only. Do not call from within the RTS.
543 procedure Timed_Protected_Single_Entry_Call
544 (Object
: Protection_Entry_Access
;
545 Uninterpreted_Data
: System
.Address
;
548 Entry_Call_Successful
: out Boolean)
550 Self_Id
: constant Task_ID
:= STPO
.Self
;
551 Entry_Call
: Entry_Call_Record
renames Self_Id
.Entry_Calls
(1);
552 Ceiling_Violation
: Boolean;
555 STPO
.Write_Lock
(Object
.L
'Access, Ceiling_Violation
);
557 if Ceiling_Violation
then
561 Entry_Call
.Mode
:= Timed_Call
;
562 Entry_Call
.State
:= Now_Abortable
;
563 Entry_Call
.Uninterpreted_Data
:= Uninterpreted_Data
;
564 Entry_Call
.Exception_To_Raise
:= Ada
.Exceptions
.Null_Id
;
566 PO_Do_Or_Queue
(Self_Id
, Object
, Entry_Call
'Access);
567 Unlock_Entry
(Object
);
569 -- Try to avoid waiting for completed calls.
570 -- The call is either `Done' or not. It cannot be cancelled since there
571 -- is no ATC construct and the timed wait has not started yet.
573 pragma Assert
(Entry_Call
.State
/= Cancelled
);
575 if Entry_Call
.State
= Done
then
576 Check_Exception
(Self_Id
, Entry_Call
'Access);
577 Entry_Call_Successful
:= True;
581 Wait_For_Completion_With_Timeout
582 (Self_Id
, Entry_Call
'Access, Timeout
, Mode
);
584 pragma Assert
(Entry_Call
.State
>= Done
);
586 Check_Exception
(Self_Id
, Entry_Call
'Access);
587 Entry_Call_Successful
:= Entry_Call
.State
= Done
;
588 end Timed_Protected_Single_Entry_Call
;
594 procedure Unlock_Entry
(Object
: Protection_Entry_Access
) is
596 STPO
.Unlock
(Object
.L
'Access);
599 end System
.Tasking
.Protected_Objects
.Single_Entry
;