1 ------------------------------------------------------------------------------
3 -- GNU ADA RUN-TIME LIBRARY (GNARL) COMPONENTS --
5 -- SYSTEM.TASKING.PROTECTED_OBJECTS.SINGLE_ENTRY --
9 -- Copyright (C) 1998-2002, 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, 59 Temple Place - Suite 330, Boston, --
20 -- MA 02111-1307, 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. It is --
30 -- now maintained by Ada Core Technologies, Inc. (http://www.gnat.com). --
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
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,
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
;
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
;
81 -----------------------
82 -- Local Subprograms --
83 -----------------------
85 procedure Send_Program_Error
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
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;
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
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
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
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
;
159 if E
/= Ada
.Exceptions
.Null_Id
then
164 ------------------------
165 -- Send_Program_Error --
166 ------------------------
168 procedure Send_Program_Error
170 Entry_Call
: Entry_Call_Link
)
172 Caller
: constant Task_ID
:= Entry_Call
.Self
;
174 Entry_Call
.Exception_To_Raise
:= Program_Error
'Identity;
180 STPO
.Write_Lock
(Caller
);
181 Wakeup_Entry_Caller
(Self_Id
, Entry_Call
, Done
);
182 STPO
.Unlock
(Caller
);
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
;
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;
210 Self_Id
: constant Task_ID
:= Entry_Call
.Self
;
214 use type Ada
.Exceptions
.Exception_Id
;
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
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
234 pragma Assert
(Entry_Call
.Mode
= Timed_Call
);
235 Self_Id
.Common
.State
:= Entry_Caller_Sleep
;
238 (Self_Id
, Wakeup_Time
, Mode
, Entry_Caller_Sleep
, Timedout
, Yielded
);
241 Entry_Call
.State
:= Cancelled
;
243 Entry_Call
.State
:= Done
;
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.
265 -- The caller might be in Wait_For_Completion,
266 -- waiting for a rendezvous (possibly requeued without abort)
269 procedure Wakeup_Entry_Caller
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
;
279 pragma Assert
(New_State
= Done
or else New_State
= Cancelled
);
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
);
300 -- Nothing needs to do (Object.Call_In_Progress.Exception_To_Raise
301 -- has already been set to Null_Id).
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
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
;
329 if Init_Priority
= Unspecified_Priority
then
330 Init_Priority
:= System
.Priority
'Last;
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
;
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;
351 STPO
.Write_Lock
(Object
.L
'Access, Ceiling_Violation
);
353 if Ceiling_Violation
then
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;
368 STPO
.Read_Lock
(Object
.L
'Access, Ceiling_Violation
);
370 if Ceiling_Violation
then
373 end Lock_Read_Only_Entry
;
379 procedure PO_Do_Or_Queue
381 Object
: Protection_Entry_Access
;
382 Entry_Call
: Entry_Call_Link
)
384 Barrier_Value
: Boolean;
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
);
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;
409 STPO
.Write_Lock
(Entry_Call
.Self
);
410 Wakeup_Entry_Caller
(Self_Id
, Entry_Call
, Done
);
411 STPO
.Unlock
(Entry_Call
.Self
);
417 elsif Entry_Call
.Mode
/= Conditional_Call
then
418 Object
.Entry_Queue
:= Entry_Call
;
426 STPO
.Write_Lock
(Entry_Call
.Self
);
427 Wakeup_Entry_Caller
(Self_Id
, Entry_Call
, Cancelled
);
428 STPO
.Unlock
(Entry_Call
.Self
);
438 (Self_Id
, Entry_Call
);
441 ----------------------------
442 -- Protected_Single_Count --
443 ----------------------------
445 function Protected_Count_Entry
(Object
: Protection_Entry
) return Natural is
447 if Object
.Call_In_Progress
/= null then
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
;
463 Self_Id
: constant Task_ID
:= STPO
.Self
;
464 Entry_Call
: Entry_Call_Record
renames Self_Id
.Entry_Calls
(1);
465 Ceiling_Violation
: Boolean;
468 STPO
.Write_Lock
(Object
.L
'Access, Ceiling_Violation
);
470 if Ceiling_Violation
then
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
492 STPO
.Write_Lock
(Self_Id
);
493 Wait_For_Completion
(Entry_Call
'Access);
494 STPO
.Unlock
(Self_Id
);
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
511 return Object
.Call_In_Progress
.Self
;
512 end Protected_Single_Entry_Caller
;
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
;
522 Barrier_Value
: Boolean;
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
);
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
;
547 STPO
.Write_Lock
(Caller
);
548 Wakeup_Entry_Caller
(Self_Id
, Entry_Call
, Done
);
549 STPO
.Unlock
(Caller
);
559 Send_Program_Error
(Self_Id
, Entry_Call
);
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
;
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;
580 STPO
.Write_Lock
(Object
.L
'Access, Ceiling_Violation
);
582 if Ceiling_Violation
then
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;
609 STPO
.Write_Lock
(Self_Id
);
612 Wait_For_Completion_With_Timeout
(Entry_Call
'Access, Timeout
, Mode
);
617 STPO
.Unlock
(Self_Id
);
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
;
630 procedure Unlock_Entry
(Object
: Protection_Entry_Access
) is
632 STPO
.Unlock
(Object
.L
'Access);
635 end System
.Tasking
.Protected_Objects
.Single_Entry
;