gnat_arm_elf_13.2.1_db1e9283/arm-eabi/lib/gnat/embedded-stm32f4/gnarl/s-bbtiev.adb

  1
  2
  3
  4
  5
  6
  7
  8
  9
 10
 11
 12
 13
 14
 15
 16
 17
 18
 19
 20
 21
 22
 23
 24
 25
 26
 27
 28
 29
 30
 31
 32
 33
 34
 35
 36
 37
 38
 39
 40
 41
 42
 43
 44
 45
 46
 47
 48
 49
 50
 51
 52
 53
 54
 55
 56
 57
 58
 59
 60
 61
 62
 63
 64
 65
 66
 67
 68
 69
 70
 71
 72
 73
 74
 75
 76
 77
 78
 79
 80
 81
 82
 83
 84
 85
 86
 87
 88
 89
 90
 91
 92
 93
 94
 95
 96
 97
 98
 99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
------------------------------------------------------------------------------
--                                                                          --
--                  GNAT RUN-TIME LIBRARY (GNARL) COMPONENTS                --
--                                                                          --
--                S Y S T E M . B B . T I M I N G _ E V E N T S             --
--                                                                          --
--                                  B o d y                                 --
--                                                                          --
--                     Copyright (C) 2011-2023, AdaCore                     --
--                                                                          --
-- GNARL is free software; you can  redistribute it  and/or modify it under --
-- terms of the  GNU General Public License as published  by the Free Soft- --
-- ware  Foundation;  either version 3,  or (at your option) any later ver- --
-- sion. GNARL is distributed in the hope that it will be useful, but WITH- --
-- OUT ANY WARRANTY;  without even the  implied warranty of MERCHANTABILITY --
-- or FITNESS FOR A PARTICULAR PURPOSE.                                     --
--                                                                          --
-- As a special exception under Section 7 of GPL version 3, you are granted --
-- additional permissions described in the GCC Runtime Library Exception,   --
-- version 3.1, as published by the Free Software Foundation.               --
--                                                                          --
-- You should have received a copy of the GNU General Public License and    --
-- a copy of the GCC Runtime Library Exception along with this program;     --
-- see the files COPYING3 and COPYING.RUNTIME respectively.  If not, see    --
-- <http://www.gnu.org/licenses/>.                                          --
--                                                                          --
------------------------------------------------------------------------------

with System.BB.Parameters;
with System.BB.Threads;
with System.BB.Threads.Queues;
with System.BB.Board_Support;

package body System.BB.Timing_Events is

   use type System.BB.Time.Time;
   use System.Multiprocessors;
   use System.BB.Board_Support.Multiprocessors;
   use System.BB.Threads;

   Events_Table : array (CPU) of Timing_Event_Access := (others => null);
   --  One event list for each CPU

   procedure Insert
     (Event    : not null Timing_Event_Access;
      Is_First : out Boolean) with
   --  Insert an event in the event list of the current CPU (Timeout order
   --  then FIFO). Is_First is set to True when Event becomes the next timing
   --  event to serve, False otherwise.

     Pre =>

       --  The first element in the list (if it exists) cannot have a previous
       --  element.

       (if Events_Table (Current_CPU) /= null then
         Events_Table (Current_CPU).Prev = null)

         --  The event should be set

         and then Event.Handler /= null

         --  The event should not be already inserted in a list

         and then Event.Next = null and then Event.Prev = null

         --  Timing Events must always be handled by the same CPU

         and then (not System.BB.Parameters.Multiprocessor
                    or else Event.CPU = Current_CPU),

     Post =>

       --  Is_First is set to True when Event becomes the next timing event to
       --  serve (because the list was empty or the list contained only events
       --  with a later expiration time).

       (if Events_Table (Current_CPU) = Event then
           Is_First
             and then Event.all.Prev = null
             and then Event.all.Next = Events_Table'Old (Current_CPU)

        --  If the event is not first then the head of queue does not change

        else
           Events_Table (Current_CPU) = Events_Table'Old (Current_CPU)
             and then Event.all.Prev /= null)

         --  The queue cannot be empty after insertion

         and then Events_Table (Current_CPU) /= null

         --  The first element in the list can never have a previous element

         and then Events_Table (Current_CPU).Prev = null

         --  The queue is always ordered by expiration time and then FIFO

         and then (Event.all.Next = null
                    or else Event.all.Next.Timeout > Event.Timeout)
         and then (Event.all.Prev = null
                    or else Event.all.Prev.Timeout <= Event.Timeout);

   procedure Extract (Event     : not null Timing_Event_Access;
                      Was_First : out Boolean) with
   --  Extract an event from the event list of the current CPU. Was_First is
   --  True when we extract the event that was first in the queue, else False.

     Pre =>

       --  There must be at least one element in the queue

       Events_Table (Current_CPU) /= null

         --  The first element in the list can never have a previous element

         and then Events_Table (Current_CPU).Prev = null

         --  The first element has Prev equal to null, but the others have Prev
         --  pointing to another timing event.

         and then (if Event /= Events_Table (Current_CPU) then
                     Event.Prev /= null)

         --  The queue is always ordered by expiration time and then FIFO

         and then (Event.Next = null
                    or else Event.Next.Timeout >= Event.Timeout)
         and then (Event.Prev = null
                    or else Event.Prev.Timeout <= Event.Timeout)

         --  Timing Events must always be handled by the same CPU

         and then (not System.BB.Parameters.Multiprocessor
                    or else Event.CPU = Current_CPU),

     Post =>

       --  Was_First is set to True when we extract the event that was first
       --  in the queue.

       (if Events_Table'Old (Current_CPU) = Event then
          Events_Table (Current_CPU) /= Events_Table'Old (Current_CPU)
            and then Was_First)

         --  The first element in the list (if it exists) cannot have a
         --  previous element.

         and then (if Events_Table (Current_CPU) /= null then
                     Events_Table (Current_CPU).Prev = null)

         --  The Prev and Next pointers are set to null to indicate that the
         --  event is no longer in the list.

         and then Event.all.Prev = null
         and then Event.all.Next = null;

   -----------------
   -- Set_Handler --
   -----------------

   procedure Set_Handler
     (Event   : in out Timing_Event;
      At_Time : System.BB.Time.Time;
      Handler : Timing_Event_Handler)
   is
      Next_Alarm : System.BB.Time.Time;
      CPU_Id     : constant CPU        := Current_CPU;
      Was_First  : Boolean             := False;
      Is_First   : Boolean             := False;

   begin
      if Event.Handler /= null then

         --  Extract if the event is already set

         Extract (Event'Unchecked_Access, Was_First);
      end if;

      Event.Handler := Handler;

      if Handler /= null then

         --  Update event fields

         Event.Timeout := At_Time;
         Event.CPU     := CPU_Id;

         --  Insert event in the list

         Insert (Event'Unchecked_Access, Is_First);
      end if;

      if Was_First or else Is_First then
         --  Set the timer for the next alarm

         Next_Alarm := Time.Get_Next_Timeout (CPU_Id);
         Time.Update_Alarm (Next_Alarm);
      end if;

      --  The following pragma cannot be transformed into a post-condition
      --  because the call to Leave_Kernel is a dispatching operation and the
      --  status of the timing event handler may change (if may expire, for
      --  example).

      pragma Assert
        ((if Handler = null then

             --  If Handler is null the event is cleared

             Event.Handler = null

          else
             --  If Handler is not null then the timing event handler is set,
             --  and the execution time for the event is set to At_Time in the
             --  current CPU. Next timeout events can never be later than the
             --  event that we have just inserted.

             Event.Handler = Handler
               and then Event.Timeout = At_Time
               and then Time.Get_Next_Timeout (CPU_Id) <= At_Time));
   end Set_Handler;

   --------------------
   -- Cancel_Handler --
   --------------------

   procedure Cancel_Handler
     (Event     : in out Timing_Event;
      Cancelled : out Boolean)
   is
      Next_Alarm : System.BB.Time.Time;
      CPU_Id     : constant CPU := Current_CPU;
      Was_First  : Boolean;

   begin
      if Event.Handler /= null then

         --  Extract if the event is already set

         Extract (Event'Unchecked_Access, Was_First);

         Cancelled     := True;
         Event.Handler := null;

         if Was_First then
            Next_Alarm := Time.Get_Next_Timeout (CPU_Id);
            Time.Update_Alarm (Next_Alarm);
         end if;
      else
         Cancelled := False;
      end if;

      pragma Assert (Event.Handler = null);
   end Cancel_Handler;

   -----------------------------------
   -- Execute_Expired_Timing_Events --
   -----------------------------------

   procedure Execute_Expired_Timing_Events (Now : System.BB.Time.Time) is
      CPU_Id          : constant CPU := Current_CPU;
      Event           : Timing_Event_Access := Events_Table (CPU_Id);
      Handler         : Timing_Event_Handler;
      Was_First       : Boolean;
      Self_Id         : Thread_Id;
      Caller_Priority : Integer;

   begin
      --  Fast path: no timing event

      if Event = null then
         return;
      end if;

      --  As required by RM D.15 (14/2), timing events must be executed at
      --  the highest priority (Interrupt_Priority'Last). This is ensured by
      --  executing this part at the highest interrupt priority (and not at the
      --  one corresponding to the timer hardware interrupt). At the end of the
      --  execution of any timing event handler the priority that is restored
      --  is that of the alarm handler. If this part of the alarm handler
      --  executes at a priority lower than Interrupt_Priority'Last then
      --  the protection of the queues would not be guaranteed.

      Self_Id := Thread_Self;
      Caller_Priority := Get_Priority (Self_Id);

      Queues.Change_Priority (Self_Id, Interrupt_Priority'Last);

      --  Extract and execute all the expired timing events

      while Event /= null and then Event.Timeout <= Now loop

         --  Get handler

         Handler := Event.Handler;

         pragma Assert (Handler /= null);

         --  Extract first event from the list

         Extract (Event, Was_First);

         pragma Assert (Was_First);

         --  Clear the event. Do it before executing the handler before the
         --  timing event can be reinserted in the handler.

         Event.Handler := null;

         --  Execute the handler

         Handler (Event.all);

         Event := Events_Table (CPU_Id);
      end loop;

      Queues.Change_Priority (Self_Id, Caller_Priority);

      --  No more events to handle with an expiration time before Now

      pragma Assert (Events_Table (CPU_Id) = null
                       or else Events_Table (CPU_Id).Timeout > Now);
   end Execute_Expired_Timing_Events;

   ----------------------
   -- Get_Next_Timeout --
   ----------------------

   function Get_Next_Timeout
     (CPU_Id : System.Multiprocessors.CPU) return System.BB.Time.Time
   is
      Event : constant Timing_Event_Access := Events_Table (CPU_Id);
   begin
      if Event = null then
         return System.BB.Time.Time'Last;
      else
         return Event.all.Timeout;
      end if;
   end Get_Next_Timeout;

   -------------------
   -- Time_Of_Event --
   -------------------

   function Time_Of_Event (Event : Timing_Event) return System.BB.Time.Time is
   begin
      if Event.Handler = null then
         return System.BB.Time.Time'First;
      else
         return Event.Timeout;
      end if;
   end Time_Of_Event;

   -------------
   -- Extract --
   -------------

   procedure Extract (Event     : not null Timing_Event_Access;
                      Was_First : out Boolean)
   is
      CPU_Id : constant CPU := Current_CPU;

   begin
      --  Head extraction

      if Events_Table (CPU_Id) = Event then
         Was_First := True;
         Events_Table (CPU_Id) := Event.Next;

      --  Middle or tail extraction

      else
         pragma Assert (Event.Prev /= null);

         Was_First := False;
         Event.Prev.Next := Event.Next;
      end if;

      if Event.Next /= null then
         Event.Next.Prev := Event.Prev;
      end if;

      Event.Next := null;
      Event.Prev := null;
   end Extract;

   -------------
   -- Insert --
   -------------

   procedure Insert
     (Event    : not null Timing_Event_Access;
      Is_First : out Boolean)
   is
      CPU_Id      : constant CPU := Current_CPU;
      Aux_Pointer : Timing_Event_Access;

   begin
      --  Insert at the head if there is no other events with a smaller timeout

      if Events_Table (CPU_Id) = null
        or else Events_Table (CPU_Id).Timeout > Event.Timeout
      then
         Is_First := True;

         Event.Next := Events_Table (CPU_Id);

         if Events_Table (CPU_Id) /= null then
            Events_Table (CPU_Id).Prev := Event;
         end if;

         Events_Table (CPU_Id) := Event;

      --  Middle or tail insertion

      else
         pragma Assert (Events_Table (CPU_Id) /= null);

         Is_First := False;

         Aux_Pointer := Events_Table (CPU_Id);

         while Aux_Pointer.Next /= null
           and then Aux_Pointer.Next.Timeout <= Event.Timeout
         loop
            Aux_Pointer := Aux_Pointer.Next;
         end loop;

         --  Insert after the Aux_Pointer

         Event.Next := Aux_Pointer.Next;
         Event.Prev := Aux_Pointer;

         if Aux_Pointer.Next /= null then
            Aux_Pointer.Next.Prev := Event;
         end if;

         Aux_Pointer.Next := Event;
      end if;
   end Insert;

end System.BB.Timing_Events;