File: s-tpopmo.adb

package info (click to toggle)
gcc-arm-none-eabi 15%3A8-2019-q3-1
  • links: PTS, VCS
  • area: main
  • in suites: bullseye
  • size: 571,828 kB
  • sloc: ansic: 2,937,651; cpp: 881,644; ada: 597,189; makefile: 65,528; asm: 56,499; xml: 46,621; exp: 24,747; sh: 19,684; python: 7,256; pascal: 4,370; awk: 3,497; perl: 2,695; yacc: 316; ml: 285; f90: 234; lex: 198; objc: 194; haskell: 119
file content (283 lines) | stat: -rw-r--r-- 9,541 bytes parent folder | download | duplicates (2)
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
------------------------------------------------------------------------------
--                                                                          --
--                 GNAT RUN-TIME LIBRARY (GNARL) COMPONENTS                 --
--                                                                          --
--               SYSTEM.TASK_PRIMITIVES.OPERATIONS.MONOTONIC                --
--                                                                          --
--                                 B o d y                                  --
--                                                                          --
--         Copyright (C) 1992-2018, Free Software Foundation, Inc.          --
--                                                                          --
-- 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.  GNAT 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/>.                                          --
--                                                                          --
-- GNARL was developed by the GNARL team at Florida State University.       --
-- Extensive contributions were provided by Ada Core Technologies, Inc.     --
--                                                                          --
------------------------------------------------------------------------------

--  This is the Monotonic version of this package for Posix and Linux targets.

separate (System.Task_Primitives.Operations)
package body Monotonic is

   -----------------------
   -- Local Subprograms --
   -----------------------

   procedure Compute_Deadline
     (Time       : Duration;
      Mode       : ST.Delay_Modes;
      Check_Time : out Duration;
      Abs_Time   : out Duration;
      Rel_Time   : out Duration);
   --  Helper for Timed_Sleep and Timed_Delay: given a deadline specified by
   --  Time and Mode, compute the current clock reading (Check_Time), and the
   --  target absolute and relative clock readings (Abs_Time, Rel_Time). The
   --  epoch for Time depends on Mode; the epoch for Check_Time and Abs_Time
   --  is always that of CLOCK_RT_Ada.

   ---------------------
   -- Monotonic_Clock --
   ---------------------

   function Monotonic_Clock return Duration is
      TS     : aliased timespec;
      Result : Interfaces.C.int;
   begin
      Result := clock_gettime
        (clock_id => OSC.CLOCK_RT_Ada, tp => TS'Unchecked_Access);
      pragma Assert (Result = 0);

      return To_Duration (TS);
   end Monotonic_Clock;

   -------------------
   -- RT_Resolution --
   -------------------

   function RT_Resolution return Duration is
      TS     : aliased timespec;
      Result : Interfaces.C.int;

   begin
      Result := clock_getres (OSC.CLOCK_REALTIME, TS'Unchecked_Access);
      pragma Assert (Result = 0);

      return To_Duration (TS);
   end RT_Resolution;

   ----------------------
   -- Compute_Deadline --
   ----------------------

   procedure Compute_Deadline
     (Time       : Duration;
      Mode       : ST.Delay_Modes;
      Check_Time : out Duration;
      Abs_Time   : out Duration;
      Rel_Time   : out Duration)
   is
   begin
      Check_Time := Monotonic_Clock;

      --  Relative deadline

      if Mode = Relative then
         Abs_Time := Duration'Min (Time, Max_Sensible_Delay) + Check_Time;

         if Relative_Timed_Wait then
            Rel_Time := Duration'Min (Max_Sensible_Delay, Time);
         end if;

         pragma Warnings (Off);
         --  Comparison "OSC.CLOCK_RT_Ada = OSC.CLOCK_REALTIME" is compile
         --  time known.

      --  Absolute deadline specified using the tasking clock (CLOCK_RT_Ada)

      elsif Mode = Absolute_RT
        or else OSC.CLOCK_RT_Ada = OSC.CLOCK_REALTIME
      then
         pragma Warnings (On);
         Abs_Time := Duration'Min (Check_Time + Max_Sensible_Delay, Time);

         if Relative_Timed_Wait then
            Rel_Time := Duration'Min (Max_Sensible_Delay, Time - Check_Time);
         end if;

      --  Absolute deadline specified using the calendar clock, in the
      --  case where it is not the same as the tasking clock: compensate for
      --  difference between clock epochs (Base_Time - Base_Cal_Time).

      else
         declare
            Cal_Check_Time : constant Duration := OS_Primitives.Clock;
            RT_Time        : constant Duration :=
                               Time + Check_Time - Cal_Check_Time;

         begin
            Abs_Time :=
              Duration'Min (Check_Time + Max_Sensible_Delay, RT_Time);

            if Relative_Timed_Wait then
               Rel_Time :=
                 Duration'Min (Max_Sensible_Delay, RT_Time - Check_Time);
            end if;
         end;
      end if;
   end Compute_Deadline;

   -----------------
   -- Timed_Sleep --
   -----------------

   --  This is for use within the run-time system, so abort is
   --  assumed to be already deferred, and the caller should be
   --  holding its own ATCB lock.

   procedure Timed_Sleep
     (Self_ID  : ST.Task_Id;
      Time     : Duration;
      Mode     : ST.Delay_Modes;
      Reason   : System.Tasking.Task_States;
      Timedout : out Boolean;
      Yielded  : out Boolean)
   is
      pragma Unreferenced (Reason);

      Base_Time  : Duration;
      Check_Time : Duration;
      Abs_Time   : Duration;
      Rel_Time   : Duration;

      Request    : aliased timespec;
      Result     : Interfaces.C.int;

   begin
      Timedout := True;
      Yielded := False;

      Compute_Deadline
        (Time       => Time,
         Mode       => Mode,
         Check_Time => Check_Time,
         Abs_Time   => Abs_Time,
         Rel_Time   => Rel_Time);
      Base_Time := Check_Time;

      if Abs_Time > Check_Time then
         Request :=
           To_Timespec (if Relative_Timed_Wait then Rel_Time else Abs_Time);

         loop
            exit when Self_ID.Pending_ATC_Level < Self_ID.ATC_Nesting_Level;

            Result :=
              pthread_cond_timedwait
                (cond    => Self_ID.Common.LL.CV'Access,
                 mutex   => (if Single_Lock
                             then Single_RTS_Lock'Access
                             else Self_ID.Common.LL.L'Access),
                 abstime => Request'Access);

            Check_Time := Monotonic_Clock;
            exit when Abs_Time <= Check_Time or else Check_Time < Base_Time;

            if Result in 0 | EINTR then

               --  Somebody may have called Wakeup for us

               Timedout := False;
               exit;
            end if;

            pragma Assert (Result = ETIMEDOUT);
         end loop;
      end if;
   end Timed_Sleep;

   -----------------
   -- Timed_Delay --
   -----------------

   --  This is for use in implementing delay statements, so we assume the
   --  caller is abort-deferred but is holding no locks.

   procedure Timed_Delay
     (Self_ID : ST.Task_Id;
      Time    : Duration;
      Mode    : ST.Delay_Modes)
   is
      Base_Time  : Duration;
      Check_Time : Duration;
      Abs_Time   : Duration;
      Rel_Time   : Duration;
      Request    : aliased timespec;

      Result : Interfaces.C.int;
      pragma Warnings (Off, Result);

   begin
      if Single_Lock then
         Lock_RTS;
      end if;

      Write_Lock (Self_ID);

      Compute_Deadline
        (Time       => Time,
         Mode       => Mode,
         Check_Time => Check_Time,
         Abs_Time   => Abs_Time,
         Rel_Time   => Rel_Time);
      Base_Time := Check_Time;

      if Abs_Time > Check_Time then
         Request :=
           To_Timespec (if Relative_Timed_Wait then Rel_Time else Abs_Time);
         Self_ID.Common.State := Delay_Sleep;

         loop
            exit when Self_ID.Pending_ATC_Level < Self_ID.ATC_Nesting_Level;

            Result :=
              pthread_cond_timedwait
                (cond    => Self_ID.Common.LL.CV'Access,
                 mutex   => (if Single_Lock
                             then Single_RTS_Lock'Access
                             else Self_ID.Common.LL.L'Access),
                 abstime => Request'Access);

            Check_Time := Monotonic_Clock;
            exit when Abs_Time <= Check_Time or else Check_Time < Base_Time;

            pragma Assert (Result in 0 | ETIMEDOUT | EINTR);
         end loop;

         Self_ID.Common.State := Runnable;
      end if;

      Unlock (Self_ID);

      if Single_Lock then
         Unlock_RTS;
      end if;

      Result := sched_yield;
   end Timed_Delay;

end Monotonic;