PR sanitizer/80403
[official-gcc.git] / gcc / ada / a-exetim-posix.adb
blob9c7ad57166ee8d6ae4e84bfe8b83c2fd0e6f838c
1 ------------------------------------------------------------------------------
2 -- --
3 -- GNAT RUN-TIME COMPONENTS --
4 -- --
5 -- A D A . E X E C U T I O N _ T I M E --
6 -- --
7 -- B o d y --
8 -- --
9 -- Copyright (C) 2007-2015, 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 ------------------------------------------------------------------------------
32 -- This is the POSIX (Realtime Extension) version of this package
34 with Ada.Task_Identification; use Ada.Task_Identification;
35 with Ada.Unchecked_Conversion;
37 with System.Tasking;
38 with System.OS_Interface; use System.OS_Interface;
39 with System.Task_Primitives.Operations; use System.Task_Primitives.Operations;
41 with Interfaces.C; use Interfaces.C;
43 package body Ada.Execution_Time is
45 pragma Linker_Options ("-lrt");
46 -- POSIX.1b Realtime Extensions library. Needed to have access to function
47 -- clock_gettime.
49 ---------
50 -- "+" --
51 ---------
53 function "+"
54 (Left : CPU_Time;
55 Right : Ada.Real_Time.Time_Span) return CPU_Time
57 use type Ada.Real_Time.Time;
58 begin
59 return CPU_Time (Ada.Real_Time.Time (Left) + Right);
60 end "+";
62 function "+"
63 (Left : Ada.Real_Time.Time_Span;
64 Right : CPU_Time) return CPU_Time
66 use type Ada.Real_Time.Time;
67 begin
68 return CPU_Time (Left + Ada.Real_Time.Time (Right));
69 end "+";
71 ---------
72 -- "-" --
73 ---------
75 function "-"
76 (Left : CPU_Time;
77 Right : Ada.Real_Time.Time_Span) return CPU_Time
79 use type Ada.Real_Time.Time;
80 begin
81 return CPU_Time (Ada.Real_Time.Time (Left) - Right);
82 end "-";
84 function "-"
85 (Left : CPU_Time;
86 Right : CPU_Time) return Ada.Real_Time.Time_Span
88 use type Ada.Real_Time.Time;
89 begin
90 return (Ada.Real_Time.Time (Left) - Ada.Real_Time.Time (Right));
91 end "-";
93 -----------
94 -- Clock --
95 -----------
97 function Clock
98 (T : Ada.Task_Identification.Task_Id :=
99 Ada.Task_Identification.Current_Task) return CPU_Time
101 TS : aliased timespec;
102 Clock_Id : aliased Interfaces.C.int;
103 Result : Interfaces.C.int;
105 function To_CPU_Time is
106 new Ada.Unchecked_Conversion (Duration, CPU_Time);
107 -- Time is equal to Duration (although it is a private type) and
108 -- CPU_Time is equal to Time.
110 function Convert_Ids is new
111 Ada.Unchecked_Conversion (Task_Id, System.Tasking.Task_Id);
113 function clock_gettime
114 (clock_id : Interfaces.C.int;
115 tp : access timespec)
116 return int;
117 pragma Import (C, clock_gettime, "clock_gettime");
118 -- Function from the POSIX.1b Realtime Extensions library
120 function pthread_getcpuclockid
121 (tid : Thread_Id;
122 clock_id : access Interfaces.C.int)
123 return int;
124 pragma Import (C, pthread_getcpuclockid, "pthread_getcpuclockid");
125 -- Function from the Thread CPU-Time Clocks option
127 begin
128 if T = Ada.Task_Identification.Null_Task_Id then
129 raise Program_Error;
130 else
131 -- Get the CPU clock for the task passed as parameter
133 Result := pthread_getcpuclockid
134 (Get_Thread_Id (Convert_Ids (T)), Clock_Id'Access);
135 pragma Assert (Result = 0);
136 end if;
138 Result := clock_gettime
139 (clock_id => Clock_Id, tp => TS'Unchecked_Access);
140 pragma Assert (Result = 0);
142 return To_CPU_Time (To_Duration (TS));
143 end Clock;
145 --------------------------
146 -- Clock_For_Interrupts --
147 --------------------------
149 function Clock_For_Interrupts return CPU_Time is
150 begin
151 -- According to AI 0170-1, D.14(18.1/3), if Interrupt_Clocks_Supported
152 -- is set to False the function raises Program_Error.
154 raise Program_Error;
155 return CPU_Time_First;
156 end Clock_For_Interrupts;
158 -----------
159 -- Split --
160 -----------
162 procedure Split
163 (T : CPU_Time;
164 SC : out Ada.Real_Time.Seconds_Count;
165 TS : out Ada.Real_Time.Time_Span)
167 use type Ada.Real_Time.Time;
168 begin
169 Ada.Real_Time.Split (Ada.Real_Time.Time (T), SC, TS);
170 end Split;
172 -------------
173 -- Time_Of --
174 -------------
176 function Time_Of
177 (SC : Ada.Real_Time.Seconds_Count;
178 TS : Ada.Real_Time.Time_Span := Ada.Real_Time.Time_Span_Zero)
179 return CPU_Time
181 begin
182 return CPU_Time (Ada.Real_Time.Time_Of (SC, TS));
183 end Time_Of;
185 end Ada.Execution_Time;