| 1 |
------------------------------------------------------------------------------ |
| 2 |
-- -- |
| 3 |
-- GNAT RUN-TIME COMPONENTS -- |
| 4 |
-- -- |
| 5 |
-- A D A . R E A L _ T I M E . T I M I N G _ E V E N T S -- |
| 6 |
-- -- |
| 7 |
-- B o d y -- |
| 8 |
-- -- |
| 9 |
-- Copyright (C) 2005-2025, Free Software Foundation, Inc. -- |
| 10 |
-- -- |
| 11 |
-- GNAT 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 |
-- GNAT was originally developed by the GNAT team at New York University. -- |
| 28 |
-- Extensive contributions were provided by Ada Core Technologies Inc. -- |
| 29 |
-- -- |
| 30 |
------------------------------------------------------------------------------ |
| 31 |
|
| 32 |
with System.Interrupt_Management.Operations; |
| 33 |
with System.OS_Locks; |
| 34 |
with System.Soft_Links; |
| 35 |
with System.Task_Primitives.Operations; |
| 36 |
with System.Tasking.Utilities; |
| 37 |
|
| 38 |
with Ada.Containers.Doubly_Linked_Lists; |
| 39 |
pragma Elaborate_All (Ada.Containers.Doubly_Linked_Lists); |
| 40 |
|
| 41 |
--------------------------------- |
| 42 |
-- Ada.Real_Time.Timing_Events -- |
| 43 |
--------------------------------- |
| 44 |
|
| 45 |
package body Ada.Real_Time.Timing_Events is |
| 46 |
|
| 47 |
use System.Task_Primitives.Operations; |
| 48 |
|
| 49 |
package SSL renames System.Soft_Links; |
| 50 |
|
| 51 |
type Any_Timing_Event is access all Timing_Event'Class; |
| 52 |
-- We must also handle user-defined types derived from Timing_Event |
| 53 |
|
| 54 |
------------ |
| 55 |
-- Events -- |
| 56 |
------------ |
| 57 |
|
| 58 |
package Events is new Ada.Containers.Doubly_Linked_Lists (Any_Timing_Event); |
| 59 |
-- Provides the type for the container holding pointers to events |
| 60 |
|
| 61 |
All_Events : Events.List; |
| 62 |
-- The queue of pending events, ordered by increasing timeout value, that |
| 63 |
-- have been "set" by the user via Set_Handler. |
| 64 |
|
| 65 |
Event_Queue_Lock : aliased System.OS_Locks.RTS_Lock; |
| 66 |
-- Used for mutually exclusive access to All_Events |
| 67 |
|
| 68 |
-- We need to Initialize_Lock before Timer is activated. The purpose of the |
| 69 |
-- Dummy package is to get around Ada's syntax rules. |
| 70 |
|
| 71 |
package Dummy is end Dummy; |
| 72 |
package body Dummy is |
| 73 |
begin |
| 74 |
Initialize_Lock (Event_Queue_Lock'Access, Level => PO_Level); |
| 75 |
end Dummy; |
| 76 |
|
| 77 |
procedure Process_Queued_Events; |
| 78 |
-- Examine the queue of pending events for any that have timed out. For |
| 79 |
-- those that have timed out, remove them from the queue and invoke their |
| 80 |
-- handler (unless the user has cancelled the event by setting the handler |
| 81 |
-- pointer to null). Mutually exclusive access is held via Event_Queue_Lock |
| 82 |
-- during part of the processing. |
| 83 |
|
| 84 |
procedure Insert_Into_Queue (This : Any_Timing_Event); |
| 85 |
-- Insert the specified event pointer into the queue of pending events |
| 86 |
-- with mutually exclusive access via Event_Queue_Lock. |
| 87 |
|
| 88 |
procedure Remove_From_Queue (This : Any_Timing_Event); |
| 89 |
-- Remove the specified event pointer from the queue of pending events with |
| 90 |
-- mutually exclusive access via Event_Queue_Lock. This procedure is used |
| 91 |
-- by the client-side routines (Set_Handler, etc.). |
| 92 |
|
| 93 |
----------- |
| 94 |
-- Timer -- |
| 95 |
----------- |
| 96 |
|
| 97 |
task Timer is |
| 98 |
pragma Priority (System.Priority'Last); |
| 99 |
end Timer; |
| 100 |
|
| 101 |
task body Timer is |
| 102 |
Period : constant Time_Span := Milliseconds (100); |
| 103 |
-- This is a "chiming" clock timer that fires periodically. The period |
| 104 |
-- selected is arbitrary and could be changed to suit the application |
| 105 |
-- requirements. Obviously a shorter period would give better resolution |
| 106 |
-- at the cost of more overhead. |
| 107 |
|
| 108 |
Ignore : constant Boolean := System.Tasking.Utilities.Make_Independent; |
| 109 |
pragma Unreferenced (Ignore); |
| 110 |
|
| 111 |
begin |
| 112 |
-- Since this package may be elaborated before System.Interrupt, |
| 113 |
-- we need to call Setup_Interrupt_Mask explicitly to ensure that |
| 114 |
-- this task has the proper signal mask. |
| 115 |
|
| 116 |
System.Interrupt_Management.Operations.Setup_Interrupt_Mask; |
| 117 |
|
| 118 |
loop |
| 119 |
Process_Queued_Events; |
| 120 |
delay until Clock + Period; |
| 121 |
end loop; |
| 122 |
end Timer; |
| 123 |
|
| 124 |
--------------------------- |
| 125 |
-- Process_Queued_Events -- |
| 126 |
--------------------------- |
| 127 |
|
| 128 |
procedure Process_Queued_Events is |
| 129 |
Next_Event : Any_Timing_Event; |
| 130 |
|
| 131 |
begin |
| 132 |
loop |
| 133 |
SSL.Abort_Defer.all; |
| 134 |
|
| 135 |
Write_Lock (Event_Queue_Lock'Access); |
| 136 |
|
| 137 |
if All_Events.Is_Empty then |
| 138 |
Unlock (Event_Queue_Lock'Access); |
| 139 |
SSL.Abort_Undefer.all; |
| 140 |
return; |
| 141 |
else |
| 142 |
Next_Event := All_Events.First_Element; |
| 143 |
end if; |
| 144 |
|
| 145 |
if Next_Event.Timeout > Clock then |
| 146 |
|
| 147 |
-- We found one that has not yet timed out. The queue is in |
| 148 |
-- ascending order by Timeout so there is no need to continue |
| 149 |
-- processing (and indeed we must not continue since we always |
| 150 |
-- delete the first element). |
| 151 |
|
| 152 |
Unlock (Event_Queue_Lock'Access); |
| 153 |
SSL.Abort_Undefer.all; |
| 154 |
return; |
| 155 |
end if; |
| 156 |
|
| 157 |
-- We have an event that has timed out so we will process it. It must |
| 158 |
-- be the first in the queue so no search is needed. |
| 159 |
|
| 160 |
All_Events.Delete_First; |
| 161 |
|
| 162 |
-- A fundamental issue is that the invocation of the event's handler |
| 163 |
-- might call Set_Handler on itself to re-insert itself back into the |
| 164 |
-- queue of future events. Thus we cannot hold the lock on the queue |
| 165 |
-- while invoking the event's handler. |
| 166 |
|
| 167 |
Unlock (Event_Queue_Lock'Access); |
| 168 |
|
| 169 |
SSL.Abort_Undefer.all; |
| 170 |
|
| 171 |
-- There is no race condition with the user changing the handler |
| 172 |
-- pointer while we are processing because we are executing at the |
| 173 |
-- highest possible application task priority and are not doing |
| 174 |
-- anything to block prior to invoking their handler. |
| 175 |
|
| 176 |
declare |
| 177 |
Handler : constant Timing_Event_Handler := Next_Event.Handler; |
| 178 |
|
| 179 |
begin |
| 180 |
-- The first act is to clear the event, per D.15(13/2). Besides, |
| 181 |
-- we cannot clear the handler pointer *after* invoking the |
| 182 |
-- handler because the handler may have re-inserted the event via |
| 183 |
-- Set_Event. Thus we take a copy and then clear the component. |
| 184 |
|
| 185 |
Next_Event.Handler := null; |
| 186 |
|
| 187 |
if Handler /= null then |
| 188 |
Handler.all (Timing_Event (Next_Event.all)); |
| 189 |
end if; |
| 190 |
|
| 191 |
-- Ignore exceptions propagated by Handler.all, as required by |
| 192 |
-- RM D.15(21/2). |
| 193 |
|
| 194 |
exception |
| 195 |
when others => |
| 196 |
null; |
| 197 |
end; |
| 198 |
end loop; |
| 199 |
end Process_Queued_Events; |
| 200 |
|
| 201 |
----------------------- |
| 202 |
-- Insert_Into_Queue -- |
| 203 |
----------------------- |
| 204 |
|
| 205 |
procedure Insert_Into_Queue (This : Any_Timing_Event) is |
| 206 |
|
| 207 |
function Sooner (Left, Right : Any_Timing_Event) return Boolean; |
| 208 |
-- Compares events in terms of timeout values |
| 209 |
|
| 210 |
package By_Timeout is new Events.Generic_Sorting (Sooner); |
| 211 |
-- Used to keep the events in ascending order by timeout value |
| 212 |
|
| 213 |
------------ |
| 214 |
-- Sooner -- |
| 215 |
------------ |
| 216 |
|
| 217 |
function Sooner (Left, Right : Any_Timing_Event) return Boolean is |
| 218 |
begin |
| 219 |
return Left.Timeout < Right.Timeout; |
| 220 |
end Sooner; |
| 221 |
|
| 222 |
-- Start of processing for Insert_Into_Queue |
| 223 |
|
| 224 |
begin |
| 225 |
SSL.Abort_Defer.all; |
| 226 |
|
| 227 |
Write_Lock (Event_Queue_Lock'Access); |
| 228 |
|
| 229 |
All_Events.Append (This); |
| 230 |
|
| 231 |
-- A critical property of the implementation of this package is that |
| 232 |
-- all occurrences are in ascending order by Timeout. Thus the first |
| 233 |
-- event in the queue always has the "next" value for the Timer task |
| 234 |
-- to use in its delay statement. |
| 235 |
|
| 236 |
By_Timeout.Sort (All_Events); |
| 237 |
|
| 238 |
Unlock (Event_Queue_Lock'Access); |
| 239 |
|
| 240 |
SSL.Abort_Undefer.all; |
| 241 |
end Insert_Into_Queue; |
| 242 |
|
| 243 |
----------------------- |
| 244 |
-- Remove_From_Queue -- |
| 245 |
----------------------- |
| 246 |
|
| 247 |
procedure Remove_From_Queue (This : Any_Timing_Event) is |
| 248 |
use Events; |
| 249 |
Location : Cursor; |
| 250 |
|
| 251 |
begin |
| 252 |
SSL.Abort_Defer.all; |
| 253 |
|
| 254 |
Write_Lock (Event_Queue_Lock'Access); |
| 255 |
|
| 256 |
Location := All_Events.Find (This); |
| 257 |
|
| 258 |
if Location /= No_Element then |
| 259 |
All_Events.Delete (Location); |
| 260 |
end if; |
| 261 |
|
| 262 |
Unlock (Event_Queue_Lock'Access); |
| 263 |
|
| 264 |
SSL.Abort_Undefer.all; |
| 265 |
end Remove_From_Queue; |
| 266 |
|
| 267 |
----------------- |
| 268 |
-- Set_Handler -- |
| 269 |
----------------- |
| 270 |
|
| 271 |
procedure Set_Handler |
| 272 |
(Event : in out Timing_Event; |
| 273 |
At_Time : Time; |
| 274 |
Handler : Timing_Event_Handler) |
| 275 |
is |
| 276 |
begin |
| 277 |
Remove_From_Queue (Event'Unchecked_Access); |
| 278 |
Event.Handler := null; |
| 279 |
|
| 280 |
-- RM D.15(15/2) required that at this point, we check whether the time |
| 281 |
-- has already passed, and if so, call Handler.all directly from here |
| 282 |
-- instead of doing the enqueuing below. However, this caused a nasty |
| 283 |
-- race condition and potential deadlock. If the current task has |
| 284 |
-- already locked the protected object of Handler.all, and the time has |
| 285 |
-- passed, deadlock would occur. It has been fixed by AI05-0094-1, which |
| 286 |
-- says that the handler should be executed as soon as possible, meaning |
| 287 |
-- that the timing event will be executed after the protected action |
| 288 |
-- finishes (Handler.all should not be called directly from here). |
| 289 |
-- The same comment applies to the other Set_Handler below. |
| 290 |
|
| 291 |
if Handler /= null then |
| 292 |
Event.Timeout := At_Time; |
| 293 |
Event.Handler := Handler; |
| 294 |
Insert_Into_Queue (Event'Unchecked_Access); |
| 295 |
end if; |
| 296 |
end Set_Handler; |
| 297 |
|
| 298 |
----------------- |
| 299 |
-- Set_Handler -- |
| 300 |
----------------- |
| 301 |
|
| 302 |
procedure Set_Handler |
| 303 |
(Event : in out Timing_Event; |
| 304 |
In_Time : Time_Span; |
| 305 |
Handler : Timing_Event_Handler) |
| 306 |
is |
| 307 |
begin |
| 308 |
Remove_From_Queue (Event'Unchecked_Access); |
| 309 |
Event.Handler := null; |
| 310 |
|
| 311 |
-- See comment in the other Set_Handler above |
| 312 |
|
| 313 |
if Handler /= null then |
| 314 |
Event.Timeout := Clock + In_Time; |
| 315 |
Event.Handler := Handler; |
| 316 |
Insert_Into_Queue (Event'Unchecked_Access); |
| 317 |
end if; |
| 318 |
end Set_Handler; |
| 319 |
|
| 320 |
--------------------- |
| 321 |
-- Current_Handler -- |
| 322 |
--------------------- |
| 323 |
|
| 324 |
function Current_Handler |
| 325 |
(Event : Timing_Event) return Timing_Event_Handler |
| 326 |
is |
| 327 |
begin |
| 328 |
return Event.Handler; |
| 329 |
end Current_Handler; |
| 330 |
|
| 331 |
-------------------- |
| 332 |
-- Cancel_Handler -- |
| 333 |
-------------------- |
| 334 |
|
| 335 |
procedure Cancel_Handler |
| 336 |
(Event : in out Timing_Event; |
| 337 |
Cancelled : out Boolean) |
| 338 |
is |
| 339 |
begin |
| 340 |
Remove_From_Queue (Event'Unchecked_Access); |
| 341 |
Cancelled := Event.Handler /= null; |
| 342 |
Event.Handler := null; |
| 343 |
end Cancel_Handler; |
| 344 |
|
| 345 |
------------------- |
| 346 |
-- Time_Of_Event -- |
| 347 |
------------------- |
| 348 |
|
| 349 |
function Time_Of_Event (Event : Timing_Event) return Time is |
| 350 |
begin |
| 351 |
-- RM D.15(18/2): Time_First must be returned in the event is not set |
| 352 |
|
| 353 |
return (if Event.Handler = null then Time_First else Event.Timeout); |
| 354 |
end Time_Of_Event; |
| 355 |
|
| 356 |
-------------- |
| 357 |
-- Finalize -- |
| 358 |
-------------- |
| 359 |
|
| 360 |
procedure Finalize (This : in out Timing_Event) is |
| 361 |
begin |
| 362 |
-- D.15 (19/2) says finalization clears the event |
| 363 |
|
| 364 |
This.Handler := null; |
| 365 |
Remove_From_Queue (This'Unchecked_Access); |
| 366 |
end Finalize; |
| 367 |
|
| 368 |
end Ada.Real_Time.Timing_Events; |