1 ------------------------------------------------------------------------------
3 -- GNAT RUN-TIME LIBRARY (GNARL) COMPONENTS --
5 -- S Y S T E M . T A S K I N G . A S Y N C _ D E L A Y S --
9 -- Copyright (C) 1998-2014, 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 3, or (at your option) any later ver- --
14 -- sion. GNAT 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. --
18 -- As a special exception under Section 7 of GPL version 3, you are granted --
19 -- additional permissions described in the GCC Runtime Library Exception, --
20 -- version 3.1, as published by the Free Software Foundation. --
22 -- You should have received a copy of the GNU General Public License and --
23 -- a copy of the GCC Runtime Library Exception along with this program; --
24 -- see the files COPYING3 and COPYING.RUNTIME respectively. If not, see --
25 -- <http://www.gnu.org/licenses/>. --
27 -- GNARL was developed by the GNARL team at Florida State University. --
28 -- Extensive contributions were provided by Ada Core Technologies, Inc. --
30 ------------------------------------------------------------------------------
33 -- Turn off polling, we do not want ATC polling to take place during
34 -- tasking operations. It causes infinite loops and other problems.
36 with Ada.Unchecked_Conversion;
37 with Ada.Task_Identification;
39 with System.Task_Primitives.Operations;
40 with System.Tasking.Utilities;
41 with System.Tasking.Initialization;
42 with System.Tasking.Debug;
43 with System.OS_Primitives;
44 with System.Interrupt_Management.Operations;
45 with System.Parameters;
46 with System.Traces.Tasking;
48 package body System.Tasking.Async_Delays is
50 package STPO renames System.Task_Primitives.Operations;
51 package ST renames System.Tasking;
52 package STU renames System.Tasking.Utilities;
53 package STI renames System.Tasking.Initialization;
54 package OSP renames System.OS_Primitives;
58 use System.Traces.Tasking;
60 function To_System is new Ada.Unchecked_Conversion
61 (Ada.Task_Identification.Task_Id, Task_Id);
63 Timer_Attention : Boolean := False;
64 pragma Atomic (Timer_Attention);
67 pragma Interrupt_Priority (System.Any_Priority'Last);
70 Timer_Server_ID : constant ST.Task_Id := To_System (Timer_Server'Identity);
72 -- The timer queue is a circular doubly linked list, ordered by absolute
73 -- wakeup time. The first item in the queue is Timer_Queue.Succ.
74 -- It is given a Resume_Time that is larger than any legitimate wakeup
75 -- time, so that the ordered insertion will always stop searching when it
76 -- gets back to the queue header block.
78 Timer_Queue : aliased Delay_Block;
80 package Init_Timer_Queue is end Init_Timer_Queue;
81 pragma Unreferenced (Init_Timer_Queue);
82 -- Initialize the Timer_Queue. This is a package to work around the
83 -- fact that statements are syntactically illegal here. We want this
84 -- initialization to happen before the Timer_Server is activated. A
85 -- build-in-place function would also work, but that's not supported
86 -- on all platforms (e.g. cil).
88 package body Init_Timer_Queue is
90 Timer_Queue.Succ := Timer_Queue'Unchecked_Access;
91 Timer_Queue.Pred := Timer_Queue'Unchecked_Access;
92 Timer_Queue.Resume_Time := Duration'Last;
95 ------------------------
96 -- Cancel_Async_Delay --
97 ------------------------
99 -- This should (only) be called from the compiler-generated cleanup routine
100 -- for an async. select statement with delay statement as trigger. The
101 -- effect should be to remove the delay from the timer queue, and exit one
102 -- ATC nesting level.
103 -- The usage and logic are similar to Cancel_Protected_Entry_Call, but
104 -- simplified because this is not a true entry call.
106 procedure Cancel_Async_Delay (D : Delay_Block_Access) is
107 Dpred : Delay_Block_Access;
108 Dsucc : Delay_Block_Access;
111 -- Note that we mark the delay as being cancelled
112 -- using a level value that is reserved.
114 -- make this operation idempotent
116 if D.Level = ATC_Level_Infinity then
120 D.Level := ATC_Level_Infinity;
122 -- remove self from timer queue
124 STI.Defer_Abort_Nestable (D.Self_Id);
130 STPO.Write_Lock (Timer_Server_ID);
137 STPO.Unlock (Timer_Server_ID);
139 -- Note that the above deletion code is required to be
140 -- idempotent, since the block may have been dequeued
141 -- previously by the Timer_Server.
143 -- leave the asynchronous select
145 STPO.Write_Lock (D.Self_Id);
146 STU.Exit_One_ATC_Level (D.Self_Id);
147 STPO.Unlock (D.Self_Id);
153 STI.Undefer_Abort_Nestable (D.Self_Id);
154 end Cancel_Async_Delay;
156 ----------------------
157 -- Enqueue_Duration --
158 ----------------------
160 function Enqueue_Duration
162 D : Delay_Block_Access) return Boolean
171 -- The corresponding call to Undefer_Abort is performed by the
172 -- expanded code (see exp_ch9).
174 STI.Defer_Abort (STPO.Self);
176 (STPO.Monotonic_Clock
177 + Duration'Min (T, OSP.Max_Sensible_Delay), D);
180 end Enqueue_Duration;
186 -- Allocate a queue element for the wakeup time T and put it in the
187 -- queue in wakeup time order. Assume we are on an asynchronous
188 -- select statement with delay trigger. Put the calling task to
189 -- sleep until either the delay expires or is cancelled.
191 -- We use one entry call record for this delay, since we have
192 -- to increment the ATC nesting level, but since it is not a
193 -- real entry call we do not need to use any of the fields of
194 -- the call record. The following code implements a subset of
195 -- the actions for the asynchronous case of Protected_Entry_Call,
196 -- much simplified since we know this never blocks, and does not
197 -- have the full semantics of a protected entry call.
199 procedure Time_Enqueue
201 D : Delay_Block_Access)
203 Self_Id : constant Task_Id := STPO.Self;
204 Q : Delay_Block_Access;
207 -- for visibility of operator "="
210 pragma Debug (Debug.Trace (Self_Id, "Async_Delay", 'P'));
211 pragma Assert (Self_Id.Deferral_Level = 1,
212 "async delay from within abort-deferred region");
214 if Self_Id.ATC_Nesting_Level = ATC_Level'Last then
215 raise Storage_Error with "not enough ATC nesting levels";
218 Self_Id.ATC_Nesting_Level := Self_Id.ATC_Nesting_Level + 1;
221 (Debug.Trace (Self_Id, "ASD: entered ATC level: " &
222 ATC_Level'Image (Self_Id.ATC_Nesting_Level), 'A'));
224 D.Level := Self_Id.ATC_Nesting_Level;
225 D.Self_Id := Self_Id;
232 STPO.Write_Lock (Timer_Server_ID);
234 -- Previously, there was code here to dynamically create
235 -- the Timer_Server task, if one did not already exist.
236 -- That code had a timing window that could allow multiple
237 -- timer servers to be created. Luckily, the need for
238 -- postponing creation of the timer server should now be
239 -- gone, since this package will only be linked in if
240 -- there are calls to enqueue calls on the timer server.
242 -- Insert D in the timer queue, at the position determined
243 -- by the wakeup time T.
245 Q := Timer_Queue.Succ;
247 while Q.Resume_Time < T loop
251 -- Q is the block that has Resume_Time equal to or greater than
252 -- T. After the insertion we want Q to be the successor of D.
259 -- If the new element became the head of the queue,
260 -- signal the Timer_Server to wake up.
262 if Timer_Queue.Succ = D then
263 Timer_Attention := True;
264 STPO.Wakeup (Timer_Server_ID, ST.Timer_Server_Sleep);
267 STPO.Unlock (Timer_Server_ID);
278 function Timed_Out (D : Delay_Block_Access) return Boolean is
287 task body Timer_Server is
288 Ignore : constant Boolean := STU.Make_Independent;
290 -- Local Declarations
292 Next_Wakeup_Time : Duration := Duration'Last;
296 Dequeued : Delay_Block_Access;
297 Dequeued_Task : Task_Id;
299 pragma Unreferenced (Timedout, Yielded);
302 pragma Assert (Timer_Server_ID = STPO.Self);
304 -- Since this package may be elaborated before System.Interrupt,
305 -- we need to call Setup_Interrupt_Mask explicitly to ensure that
306 -- this task has the proper signal mask.
308 Interrupt_Management.Operations.Setup_Interrupt_Mask;
310 -- Initialize the timer queue to empty, and make the wakeup time of the
311 -- header node be larger than any real wakeup time we will ever use.
314 STI.Defer_Abort (Timer_Server_ID);
320 STPO.Write_Lock (Timer_Server_ID);
322 -- The timer server needs to catch pending aborts after finalization
323 -- of library packages. If it doesn't poll for it, the server will
326 if not Timer_Attention then
327 Timer_Server_ID.Common.State := ST.Timer_Server_Sleep;
329 if Next_Wakeup_Time = Duration'Last then
330 Timer_Server_ID.User_State := 1;
332 STPO.Monotonic_Clock + OSP.Max_Sensible_Delay;
335 Timer_Server_ID.User_State := 2;
339 (Timer_Server_ID, Next_Wakeup_Time,
340 OSP.Absolute_RT, ST.Timer_Server_Sleep,
342 Timer_Server_ID.Common.State := ST.Runnable;
345 -- Service all of the wakeup requests on the queue whose times have
346 -- been reached, and update Next_Wakeup_Time to next wakeup time
347 -- after that (the wakeup time of the head of the queue if any, else
348 -- a time far in the future).
350 Timer_Server_ID.User_State := 3;
351 Timer_Attention := False;
353 Now := STPO.Monotonic_Clock;
354 while Timer_Queue.Succ.Resume_Time <= Now loop
356 -- Dequeue the waiting task from the front of the queue
358 pragma Debug (System.Tasking.Debug.Trace
359 (Timer_Server_ID, "Timer service: waking up waiting task", 'E'));
361 Dequeued := Timer_Queue.Succ;
362 Timer_Queue.Succ := Dequeued.Succ;
363 Dequeued.Succ.Pred := Dequeued.Pred;
364 Dequeued.Succ := Dequeued;
365 Dequeued.Pred := Dequeued;
367 -- We want to abort the queued task to the level of the async.
368 -- select statement with the delay. To do that, we need to lock
369 -- the ATCB of that task, but to avoid deadlock we need to release
370 -- the lock of the Timer_Server. This leaves a window in which
371 -- another task might perform an enqueue or dequeue operation on
372 -- the timer queue, but that is OK because we always restart the
373 -- next iteration at the head of the queue.
375 if Parameters.Runtime_Traces then
376 Send_Trace_Info (E_Kill, Dequeued.Self_Id);
379 STPO.Unlock (Timer_Server_ID);
380 STPO.Write_Lock (Dequeued.Self_Id);
381 Dequeued_Task := Dequeued.Self_Id;
382 Dequeued.Timed_Out := True;
383 STI.Locked_Abort_To_Level
384 (Timer_Server_ID, Dequeued_Task, Dequeued.Level - 1);
385 STPO.Unlock (Dequeued_Task);
386 STPO.Write_Lock (Timer_Server_ID);
389 Next_Wakeup_Time := Timer_Queue.Succ.Resume_Time;
391 -- Service returns the Next_Wakeup_Time.
392 -- The Next_Wakeup_Time is either an infinity (no delay request)
393 -- or the wakeup time of the queue head. This value is used for
394 -- an actual delay in this server.
396 STPO.Unlock (Timer_Server_ID);
402 STI.Undefer_Abort (Timer_Server_ID);
406 end System.Tasking.Async_Delays;