* tree-loop-distribution.c (struct partition): New field recording
[official-gcc.git] / gcc / ada / s-taasde.adb
blobd7be38473eaff777f850015a35e3c261f972c757
1 ------------------------------------------------------------------------------
2 -- --
3 -- GNAT RUN-TIME LIBRARY (GNARL) COMPONENTS --
4 -- --
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 --
6 -- --
7 -- B o d y --
8 -- --
9 -- Copyright (C) 1998-2017, Free Software Foundation, Inc. --
10 -- --
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. --
17 -- --
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. --
21 -- --
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/>. --
26 -- --
27 -- GNARL was developed by the GNARL team at Florida State University. --
28 -- Extensive contributions were provided by Ada Core Technologies, Inc. --
29 -- --
30 ------------------------------------------------------------------------------
32 pragma Polling (Off);
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;
56 use Parameters;
57 use System.Traces;
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);
66 task Timer_Server is
67 pragma Interrupt_Priority (System.Any_Priority'Last);
68 end Timer_Server;
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
89 begin
90 Timer_Queue.Succ := Timer_Queue'Unchecked_Access;
91 Timer_Queue.Pred := Timer_Queue'Unchecked_Access;
92 Timer_Queue.Resume_Time := Duration'Last;
93 end Init_Timer_Queue;
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;
110 begin
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
117 return;
118 end if;
120 D.Level := ATC_Level_Infinity;
122 -- remove self from timer queue
124 STI.Defer_Abort_Nestable (D.Self_Id);
126 if Single_Lock then
127 STPO.Lock_RTS;
128 end if;
130 STPO.Write_Lock (Timer_Server_ID);
131 Dpred := D.Pred;
132 Dsucc := D.Succ;
133 Dpred.Succ := Dsucc;
134 Dsucc.Pred := Dpred;
135 D.Succ := D;
136 D.Pred := D;
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);
149 if Single_Lock then
150 STPO.Unlock_RTS;
151 end if;
153 STI.Undefer_Abort_Nestable (D.Self_Id);
154 end Cancel_Async_Delay;
156 ----------------------
157 -- Enqueue_Duration --
158 ----------------------
160 function Enqueue_Duration
161 (T : Duration;
162 D : Delay_Block_Access) return Boolean
164 begin
165 if T <= 0.0 then
166 D.Timed_Out := True;
167 STPO.Yield;
168 return False;
170 else
171 -- The corresponding call to Undefer_Abort is performed by the
172 -- expanded code (see exp_ch9).
174 STI.Defer_Abort (STPO.Self);
175 Time_Enqueue
176 (STPO.Monotonic_Clock
177 + Duration'Min (T, OSP.Max_Sensible_Delay), D);
178 return True;
179 end if;
180 end Enqueue_Duration;
182 ------------------
183 -- Time_Enqueue --
184 ------------------
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
200 (T : Duration;
201 D : Delay_Block_Access)
203 Self_Id : constant Task_Id := STPO.Self;
204 Q : Delay_Block_Access;
206 begin
207 pragma Debug (Debug.Trace (Self_Id, "Async_Delay", 'P'));
208 pragma Assert (Self_Id.Deferral_Level = 1,
209 "async delay from within abort-deferred region");
211 if Self_Id.ATC_Nesting_Level = ATC_Level'Last then
212 raise Storage_Error with "not enough ATC nesting levels";
213 end if;
215 Self_Id.ATC_Nesting_Level := Self_Id.ATC_Nesting_Level + 1;
217 pragma Debug
218 (Debug.Trace (Self_Id, "ASD: entered ATC level: " &
219 ATC_Level'Image (Self_Id.ATC_Nesting_Level), 'A'));
221 D.Level := Self_Id.ATC_Nesting_Level;
222 D.Self_Id := Self_Id;
223 D.Resume_Time := T;
225 if Single_Lock then
226 STPO.Lock_RTS;
227 end if;
229 STPO.Write_Lock (Timer_Server_ID);
231 -- Previously, there was code here to dynamically create
232 -- the Timer_Server task, if one did not already exist.
233 -- That code had a timing window that could allow multiple
234 -- timer servers to be created. Luckily, the need for
235 -- postponing creation of the timer server should now be
236 -- gone, since this package will only be linked in if
237 -- there are calls to enqueue calls on the timer server.
239 -- Insert D in the timer queue, at the position determined
240 -- by the wakeup time T.
242 Q := Timer_Queue.Succ;
244 while Q.Resume_Time < T loop
245 Q := Q.Succ;
246 end loop;
248 -- Q is the block that has Resume_Time equal to or greater than
249 -- T. After the insertion we want Q to be the successor of D.
251 D.Succ := Q;
252 D.Pred := Q.Pred;
253 D.Pred.Succ := D;
254 Q.Pred := D;
256 -- If the new element became the head of the queue,
257 -- signal the Timer_Server to wake up.
259 if Timer_Queue.Succ = D then
260 Timer_Attention := True;
261 STPO.Wakeup (Timer_Server_ID, ST.Timer_Server_Sleep);
262 end if;
264 STPO.Unlock (Timer_Server_ID);
266 if Single_Lock then
267 STPO.Unlock_RTS;
268 end if;
269 end Time_Enqueue;
271 ---------------
272 -- Timed_Out --
273 ---------------
275 function Timed_Out (D : Delay_Block_Access) return Boolean is
276 begin
277 return D.Timed_Out;
278 end Timed_Out;
280 ------------------
281 -- Timer_Server --
282 ------------------
284 task body Timer_Server is
285 Ignore : constant Boolean := STU.Make_Independent;
287 -- Local Declarations
289 Next_Wakeup_Time : Duration := Duration'Last;
290 Timedout : Boolean;
291 Yielded : Boolean;
292 Now : Duration;
293 Dequeued : Delay_Block_Access;
294 Dequeued_Task : Task_Id;
296 pragma Unreferenced (Timedout, Yielded);
298 begin
299 pragma Assert (Timer_Server_ID = STPO.Self);
301 -- Since this package may be elaborated before System.Interrupt,
302 -- we need to call Setup_Interrupt_Mask explicitly to ensure that
303 -- this task has the proper signal mask.
305 Interrupt_Management.Operations.Setup_Interrupt_Mask;
307 -- Initialize the timer queue to empty, and make the wakeup time of the
308 -- header node be larger than any real wakeup time we will ever use.
310 loop
311 STI.Defer_Abort (Timer_Server_ID);
313 if Single_Lock then
314 STPO.Lock_RTS;
315 end if;
317 STPO.Write_Lock (Timer_Server_ID);
319 -- The timer server needs to catch pending aborts after finalization
320 -- of library packages. If it doesn't poll for it, the server will
321 -- sometimes hang.
323 if not Timer_Attention then
324 Timer_Server_ID.Common.State := ST.Timer_Server_Sleep;
326 if Next_Wakeup_Time = Duration'Last then
327 Timer_Server_ID.User_State := 1;
328 Next_Wakeup_Time :=
329 STPO.Monotonic_Clock + OSP.Max_Sensible_Delay;
331 else
332 Timer_Server_ID.User_State := 2;
333 end if;
335 STPO.Timed_Sleep
336 (Timer_Server_ID, Next_Wakeup_Time,
337 OSP.Absolute_RT, ST.Timer_Server_Sleep,
338 Timedout, Yielded);
339 Timer_Server_ID.Common.State := ST.Runnable;
340 end if;
342 -- Service all of the wakeup requests on the queue whose times have
343 -- been reached, and update Next_Wakeup_Time to next wakeup time
344 -- after that (the wakeup time of the head of the queue if any, else
345 -- a time far in the future).
347 Timer_Server_ID.User_State := 3;
348 Timer_Attention := False;
350 Now := STPO.Monotonic_Clock;
351 while Timer_Queue.Succ.Resume_Time <= Now loop
353 -- Dequeue the waiting task from the front of the queue
355 pragma Debug (System.Tasking.Debug.Trace
356 (Timer_Server_ID, "Timer service: waking up waiting task", 'E'));
358 Dequeued := Timer_Queue.Succ;
359 Timer_Queue.Succ := Dequeued.Succ;
360 Dequeued.Succ.Pred := Dequeued.Pred;
361 Dequeued.Succ := Dequeued;
362 Dequeued.Pred := Dequeued;
364 -- We want to abort the queued task to the level of the async.
365 -- select statement with the delay. To do that, we need to lock
366 -- the ATCB of that task, but to avoid deadlock we need to release
367 -- the lock of the Timer_Server. This leaves a window in which
368 -- another task might perform an enqueue or dequeue operation on
369 -- the timer queue, but that is OK because we always restart the
370 -- next iteration at the head of the queue.
372 if Parameters.Runtime_Traces then
373 Send_Trace_Info (E_Kill, Dequeued.Self_Id);
374 end if;
376 STPO.Unlock (Timer_Server_ID);
377 STPO.Write_Lock (Dequeued.Self_Id);
378 Dequeued_Task := Dequeued.Self_Id;
379 Dequeued.Timed_Out := True;
380 STI.Locked_Abort_To_Level
381 (Timer_Server_ID, Dequeued_Task, Dequeued.Level - 1);
382 STPO.Unlock (Dequeued_Task);
383 STPO.Write_Lock (Timer_Server_ID);
384 end loop;
386 Next_Wakeup_Time := Timer_Queue.Succ.Resume_Time;
388 -- Service returns the Next_Wakeup_Time.
389 -- The Next_Wakeup_Time is either an infinity (no delay request)
390 -- or the wakeup time of the queue head. This value is used for
391 -- an actual delay in this server.
393 STPO.Unlock (Timer_Server_ID);
395 if Single_Lock then
396 STPO.Unlock_RTS;
397 end if;
399 STI.Undefer_Abort (Timer_Server_ID);
400 end loop;
401 end Timer_Server;
403 end System.Tasking.Async_Delays;