[project @ 1996-01-08 20:28:12 by partain]
[ghc-hetmet.git] / ghc / runtime / io / getClockTime.lc
1 %
2 % (c) The GRASP/AQUA Project, Glasgow University, 1995
3 %
4 \subsection[getClockTime.lc]{getClockTime Runtime Support}
5
6 \begin{code}
7 #define NON_POSIX_SOURCE    /* gettimeofday */
8
9 #include "rtsdefs.h"
10 #include "stgio.h"
11
12 #ifdef HAVE_GETCLOCK
13
14 # ifdef HAVE_SYS_TIMERS_H
15 #  define POSIX_4D9 1
16 #  include <sys/timers.h>
17 # endif
18
19 #else
20 # ifdef HAVE_GETTIMEOFDAY
21
22 #  ifdef HAVE_SYS_TIME_H
23 #   include <sys/time.h>
24 #  endif
25
26 # else
27
28 #  ifdef HAVE_TIME_H
29 #   include <time.h>
30 #  endif
31
32 # endif
33 #endif
34
35 StgInt
36 getClockTime(sec, nsec)
37 StgByteArray sec;
38 StgByteArray nsec;
39 {
40 #ifdef HAVE_GETCLOCK
41     struct timespec tp;
42
43     if (getclock(TIMEOFDAY, &tp) != 0) {
44         cvtErrno();
45         stdErrno();
46         return -1;
47     }
48     ((unsigned long int *)sec)[0] = tp.tv_sec;
49     ((unsigned long int *)nsec)[0] = tp.tv_nsec;
50     return 0;
51 #else
52 #ifdef HAVE_GETTIMEOFDAY
53     struct timeval tp;
54  
55     if (gettimeofday(&tp, NULL) != 0) {
56         cvtErrno();
57         stdErrno();
58         return -1;
59     }
60     ((unsigned long int *)sec)[0] = tp.tv_sec;
61     ((unsigned long int *)nsec)[0] = tp.tv_usec * 1000;
62     return 0;
63 #else
64     time_t t;
65     if ((t = time(NULL)) == (time_t) -1) {
66         cvtErrno();
67         stdErrno();
68         return -1;
69     }
70     ((unsigned long int *)sec)[0] = t;
71     ((unsigned long int *)nsec)[0] = 0;
72     return 0;
73 #endif
74 #endif
75 }
76
77 \end{code}