/* Implementation of the SYSTEM_CLOCK intrinsic. Copyright (C) 2004 Free Software Foundation, Inc. This file is part of the GNU Fortran 95 runtime library (libgfortran). Libgfortran is free software; you can redistribute it and/or modify it under the terms of the GNU Lesser General Public License as published by the Free Software Foundation; either version 2.1 of the License, or (at your option) any later version. Libgfortran is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License for more details. You should have received a copy of the GNU Lesser General Public License along with libgfortran; see the file COPYING.LIB. If not, write to the Free Software Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. */ #include "config.h" #include #include "libgfortran.h" #include #if defined(HAVE_SYS_TIME_H) && defined(HAVE_GETTIMEOFDAY) # include # define TCK 1000 #elif HAVE_TIME_H # include # ifdef HAVE_UNISTD_H # include # define TCK sysconf(_SC_CLK_TCK) # else # define TCK CLK_TCK # endif #endif /* __system_clock_1 is the workhorse for the SYSTEM_CLOCK intrinsic subroutine. It returns the number of clock time for the current system time, the number of ticks per second, and the maximum possible value for COUNT. On the first call to SYSTEM_CLOCK, COUNT is set to zero. */ void __system_clock_1(int *count, int *count_rate, int *count_max) { #if defined(HAVE_SYS_TIME_H) && defined(HAVE_GETTIMEOFDAY) /* SYSTEM_CLOCK has millisecond resolution */ static double t0 = -1.; struct timeval tp; struct timezone tzp; double t; if (gettimeofday(&tp, &tzp) == 0) { if (t0 < 0) { t0 = ((double)tp.tv_sec + tp.tv_usec * 1.e-6) * TCK; *count = 0; } else { t = ((double)tp.tv_sec + tp.tv_usec * 1.e-6) * TCK; if (t - t0 < (double)INT_MAX) *count = (int) (t - t0); else { *count = 0; t0 = t; } } *count_rate = TCK; *count_max = INT_MAX - 1; } else { *count = -1; *count_rate = 0; *count_max = 0; } #elif HAVE_TIME_H time_t t, t1; static time_t init_time = (time_t) -2; t = time(NULL); if (t == (time_t) -1) { *count = -1; *count_rate = 0; *count_max = 0; } else if (init_time == (time_t) -2) { init_time = t; *count = 0; *count_rate = TCK; *count_max = INT_MAX-1; } else { t1 = TCK * (t - init_time); if (t1 < (time_t) INT_MAX) *count = (int) t1; else { *count = 0; init_time = t; } *count_rate = TCK; *count_max = INT_MAX-1; } #else *count = -1; *count_rate = 0; *count_max = 0; #endif } #undef SYSTEM_CLOCK #define SYSTEM_CLOCK(KIND) \ void prefix(system_clock_##KIND) (GFC_INTEGER_##KIND *__count, \ GFC_INTEGER_##KIND *__count_rate, GFC_INTEGER_##KIND *__count_max) \ { \ int count, count_rate, count_max; \ __system_clock_1(&count, &count_rate, &count_max); \ if (__count != NULL) \ { \ if (count >= 0) \ *__count = (GFC_INTEGER_##KIND) count; \ else \ *__count = - GFC_INTEGER_##KIND##_HUGE; \ } \ if (__count_rate != NULL) \ *__count_rate = (GFC_INTEGER_##KIND) count_rate; \ if (__count_max != NULL) \ *__count_max = (GFC_INTEGER_##KIND) count_max; \ } SYSTEM_CLOCK(4) SYSTEM_CLOCK(8)