1 /* Implementation of the SYSTEM_CLOCK intrinsic.
2 Copyright (C) 2004-2013 Free Software Foundation, Inc.
4 This file is part of the GNU Fortran runtime library (libgfortran).
6 Libgfortran is free software; you can redistribute it and/or
7 modify it under the terms of the GNU General Public
8 License as published by the Free Software Foundation; either
9 version 3 of the License, or (at your option) any later version.
11 Libgfortran is distributed in the hope that it will be useful,
12 but WITHOUT ANY WARRANTY; without even the implied warranty of
13 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 GNU General Public License for more details.
16 Under Section 7 of GPL version 3, you are granted additional
17 permissions described in the GCC Runtime Library Exception, version
18 3.1, as published by the Free Software Foundation.
20 You should have received a copy of the GNU General Public License and
21 a copy of the GCC Runtime Library Exception along with this program;
22 see the files COPYING3 and COPYING.RUNTIME respectively. If not, see
23 <http://www.gnu.org/licenses/>. */
25 #include "libgfortran.h"
32 #if !defined(__MINGW32__) && !defined(__CYGWIN__)
34 /* POSIX states that CLOCK_REALTIME must be present if clock_gettime
35 is available, others are optional. */
36 #if defined(HAVE_CLOCK_GETTIME) || defined(HAVE_CLOCK_GETTIME_LIBRT)
37 #if defined(CLOCK_MONOTONIC) && defined(_POSIX_MONOTONIC_CLOCK) \
38 && _POSIX_MONOTONIC_CLOCK >= 0
39 #define GF_CLOCK_MONOTONIC CLOCK_MONOTONIC
41 #define GF_CLOCK_MONOTONIC CLOCK_REALTIME
45 /* Weakref trickery for clock_gettime(). On Glibc <= 2.16,
46 clock_gettime() requires us to link in librt, which also pulls in
47 libpthread. In order to avoid this by default, only call
48 clock_gettime() through a weak reference.
50 Some targets don't support weak undefined references; on these
51 GTHREAD_USE_WEAK is 0. So we need to define it to 1 on other
53 #ifndef GTHREAD_USE_WEAK
54 #define GTHREAD_USE_WEAK 1
57 #if SUPPORTS_WEAK && GTHREAD_USE_WEAK && defined(HAVE_CLOCK_GETTIME_LIBRT)
58 static int weak_gettime (clockid_t
, struct timespec
*)
59 __attribute__((__weakref__("clock_gettime")));
63 /* High resolution monotonic clock, falling back to the realtime clock
64 if the target does not support such a clock.
67 secs - OUTPUT, seconds
68 nanosecs - OUTPUT, nanoseconds
69 tk - OUTPUT, clock resolution [counts/sec]
71 If the target supports a monotonic clock, the OUTPUT arguments
72 represent a monotonically incrementing clock starting from some
73 unspecified time in the past.
75 If a monotonic clock is not available, falls back to the realtime
76 clock which is not monotonic.
78 Return value: 0 for success, -1 for error. In case of error, errno
82 gf_gettime_mono (time_t * secs
, long * nanosecs
, long * tck
)
85 #ifdef HAVE_CLOCK_GETTIME
88 err
= clock_gettime (GF_CLOCK_MONOTONIC
, &ts
);
90 *nanosecs
= ts
.tv_nsec
;
93 #if defined(HAVE_CLOCK_GETTIME_LIBRT) && SUPPORTS_WEAK && GTHREAD_USE_WEAK
98 err
= weak_gettime (GF_CLOCK_MONOTONIC
, &ts
);
100 *nanosecs
= ts
.tv_nsec
;
105 err
= gf_gettime (secs
, nanosecs
);
111 #endif /* !__MINGW32 && !__CYGWIN__ */
113 extern void system_clock_4 (GFC_INTEGER_4
*, GFC_INTEGER_4
*, GFC_INTEGER_4
*);
114 export_proto(system_clock_4
);
116 extern void system_clock_8 (GFC_INTEGER_8
*, GFC_INTEGER_8
*, GFC_INTEGER_8
*);
117 export_proto(system_clock_8
);
120 /* prefix(system_clock_4) is the INTEGER(4) version of the SYSTEM_CLOCK
121 intrinsic subroutine. It returns the number of clock ticks for the current
122 system time, the number of ticks per second, and the maximum possible value
126 system_clock_4(GFC_INTEGER_4
*count
, GFC_INTEGER_4
*count_rate
,
127 GFC_INTEGER_4
*count_max
)
129 #if defined(__MINGW32__) || defined(__CYGWIN__)
132 /* Use GetTickCount here as the resolution and range is
133 sufficient for the INTEGER(kind=4) version, and
134 QueryPerformanceCounter has potential issues. */
135 uint32_t cnt
= GetTickCount ();
136 if (cnt
> GFC_INTEGER_4_HUGE
)
137 cnt
= cnt
- GFC_INTEGER_4_HUGE
- 1;
143 *count_max
= GFC_INTEGER_4_HUGE
;
151 if (sizeof (secs
) < sizeof (GFC_INTEGER_4
))
152 internal_error (NULL
, "secs too small");
154 if (gf_gettime_mono (&secs
, &nanosecs
, &tck
) == 0)
156 tck
= tck
>1000 ? 1000 : tck
;
157 GFC_UINTEGER_4 ucnt
= (GFC_UINTEGER_4
) secs
* tck
;
158 ucnt
+= (nanosecs
+ 500000000 / tck
) / (1000000000 / tck
);
159 if (ucnt
> GFC_INTEGER_4_HUGE
)
160 cnt
= ucnt
- GFC_INTEGER_4_HUGE
- 1;
163 mx
= GFC_INTEGER_4_HUGE
;
168 *count
= - GFC_INTEGER_4_HUGE
;
169 if (count_rate
!= NULL
)
171 if (count_max
!= NULL
)
178 if (count_rate
!= NULL
)
180 if (count_max
!= NULL
)
186 /* INTEGER(8) version of the above routine. */
189 system_clock_8 (GFC_INTEGER_8
*count
, GFC_INTEGER_8
*count_rate
,
190 GFC_INTEGER_8
*count_max
)
192 #if defined(__MINGW32__) || defined(__CYGWIN__)
196 if (count
&& !QueryPerformanceCounter (&cnt
))
198 if (count_rate
&& !QueryPerformanceFrequency (&freq
))
203 *count
= - GFC_INTEGER_8_HUGE
;
212 *count
= cnt
.QuadPart
;
214 *count_rate
= freq
.QuadPart
;
216 *count_max
= GFC_INTEGER_8_HUGE
;
225 if (sizeof (secs
) < sizeof (GFC_INTEGER_4
))
226 internal_error (NULL
, "secs too small");
228 if (gf_gettime_mono (&secs
, &nanosecs
, &tck
) == 0)
230 GFC_UINTEGER_8 ucnt
= (GFC_UINTEGER_8
) secs
* tck
;
231 ucnt
+= (nanosecs
+ 500000000 / tck
) / (1000000000 / tck
);
232 if (ucnt
> GFC_INTEGER_8_HUGE
)
233 cnt
= ucnt
- GFC_INTEGER_8_HUGE
- 1;
236 mx
= GFC_INTEGER_8_HUGE
;
241 *count
= - GFC_INTEGER_8_HUGE
;
242 if (count_rate
!= NULL
)
244 if (count_max
!= NULL
)
252 if (count_rate
!= NULL
)
254 if (count_max
!= NULL
)