OSDN Git Service

2010-10-26 Tobias Burnus <burnus@net-b.de>
[pf3gnuchains/gcc-fork.git] / gcc / ada / a-exetim-posix.adb
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-2010, 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 --  This is the POSIX (Realtime Extension) version of this package
33
34 with Ada.Task_Identification;  use Ada.Task_Identification;
35 with Ada.Unchecked_Conversion;
36
37 with System.OS_Interface; use System.OS_Interface;
38
39 with Interfaces.C; use Interfaces.C;
40
41 package body Ada.Execution_Time is
42
43    pragma Linker_Options ("-lrt");
44    --  POSIX.1b Realtime Extensions library. Needed to have access to function
45    --  clock_gettime.
46
47    ---------
48    -- "+" --
49    ---------
50
51    function "+"
52      (Left  : CPU_Time;
53       Right : Ada.Real_Time.Time_Span) return CPU_Time
54    is
55       use type Ada.Real_Time.Time;
56    begin
57       return CPU_Time (Ada.Real_Time.Time (Left) + Right);
58    end "+";
59
60    function "+"
61      (Left  : Ada.Real_Time.Time_Span;
62       Right : CPU_Time) return CPU_Time
63    is
64       use type Ada.Real_Time.Time;
65    begin
66       return CPU_Time (Left + Ada.Real_Time.Time (Right));
67    end "+";
68
69    ---------
70    -- "-" --
71    ---------
72
73    function "-"
74      (Left  : CPU_Time;
75       Right : Ada.Real_Time.Time_Span) return CPU_Time
76    is
77       use type Ada.Real_Time.Time;
78    begin
79       return CPU_Time (Ada.Real_Time.Time (Left) - Right);
80    end "-";
81
82    function "-"
83      (Left  : CPU_Time;
84       Right : CPU_Time) return Ada.Real_Time.Time_Span
85    is
86       use type Ada.Real_Time.Time;
87    begin
88       return (Ada.Real_Time.Time (Left) - Ada.Real_Time.Time (Right));
89    end "-";
90
91    -----------
92    -- Clock --
93    -----------
94
95    function Clock
96      (T : Ada.Task_Identification.Task_Id :=
97             Ada.Task_Identification.Current_Task)
98       return CPU_Time
99    is
100       TS     : aliased timespec;
101       Result : Interfaces.C.int;
102
103       function To_CPU_Time is
104         new Ada.Unchecked_Conversion (Duration, CPU_Time);
105       --  Time is equal to Duration (although it is a private type) and
106       --  CPU_Time is equal to Time.
107
108       function clock_gettime
109         (clock_id : Interfaces.C.int;
110          tp       : access timespec)
111          return int;
112       pragma Import (C, clock_gettime, "clock_gettime");
113       --  Function from the POSIX.1b Realtime Extensions library
114
115       CLOCK_THREAD_CPUTIME_ID : constant := 3;
116       --  Identifier for the clock returning per-task CPU time
117
118    begin
119       if T = Ada.Task_Identification.Null_Task_Id then
120          raise Program_Error;
121       end if;
122
123       Result := clock_gettime
124         (clock_id => CLOCK_THREAD_CPUTIME_ID, tp => TS'Unchecked_Access);
125       pragma Assert (Result = 0);
126
127       return To_CPU_Time (To_Duration (TS));
128    end Clock;
129
130    -----------
131    -- Split --
132    -----------
133
134    procedure Split
135      (T  : CPU_Time;
136       SC : out Ada.Real_Time.Seconds_Count;
137       TS : out Ada.Real_Time.Time_Span)
138    is
139       use type Ada.Real_Time.Time;
140    begin
141       Ada.Real_Time.Split (Ada.Real_Time.Time (T), SC, TS);
142    end Split;
143
144    -------------
145    -- Time_Of --
146    -------------
147
148    function Time_Of
149      (SC : Ada.Real_Time.Seconds_Count;
150       TS : Ada.Real_Time.Time_Span := Ada.Real_Time.Time_Span_Zero)
151       return CPU_Time
152    is
153    begin
154       return CPU_Time (Ada.Real_Time.Time_Of (SC, TS));
155    end Time_Of;
156
157 end Ada.Execution_Time;