[project @ 1998-12-02 13:17:09 by simonm]
[ghc-hetmet.git] / ghc / lib / std / cbits / getClockTime.c
1 /* 
2  * (c) The GRASP/AQUA Project, Glasgow University, 1994-1998
3  *
4  * $Id: getClockTime.c,v 1.3 1998/12/02 13:27:38 simonm Exp $
5  *
6  * getClockTime Runtime Support
7  */
8
9 #ifndef _AIX
10 #define NON_POSIX_SOURCE    /* gettimeofday */
11 #endif
12
13 #include "Rts.h"
14 #include "stgio.h"
15
16 #ifdef HAVE_GETCLOCK
17
18 # ifdef HAVE_SYS_TIMERS_H
19 #  define POSIX_4D9 1
20 #  include <sys/timers.h>
21 # endif
22
23 #else
24 # ifdef HAVE_GETTIMEOFDAY
25
26 #  ifdef HAVE_SYS_TIME_H
27 #   include <sys/time.h>
28 #  endif
29
30 # else
31
32 #  ifdef HAVE_TIME_H
33 #   include <time.h>
34 #  endif
35
36 # endif
37 #endif
38
39 StgInt
40 getClockTime(StgByteArray sec, StgByteArray nsec)
41 {
42 #ifdef HAVE_GETCLOCK
43     struct timespec tp;
44
45     if (getclock(TIMEOFDAY, &tp) != 0) {
46         cvtErrno();
47         stdErrno();
48         return -1;
49     }
50     ((unsigned long int *)sec)[0] = tp.tv_sec;
51     ((unsigned long int *)nsec)[0] = tp.tv_nsec;
52     return 0;
53 #else
54 #ifdef HAVE_GETTIMEOFDAY
55     struct timeval tp;
56  
57     if (gettimeofday(&tp, NULL) != 0) {
58         cvtErrno();
59         stdErrno();
60         return -1;
61     }
62     ((unsigned long int *)sec)[0] = tp.tv_sec;
63     ((unsigned long int *)nsec)[0] = tp.tv_usec * 1000;
64     return 0;
65 #else
66     time_t t;
67     if ((t = time(NULL)) == (time_t) -1) {
68         cvtErrno();
69         stdErrno();
70         return -1;
71     }
72     ((unsigned long int *)sec)[0] = t;
73     ((unsigned long int *)nsec)[0] = 0;
74     return 0;
75 #endif
76 #endif
77 }
78
79 StgInt
80 prim_getClockTime(StgByteArray sec, StgByteArray nsec)
81 {
82 #ifdef HAVE_GETCLOCK
83     struct timespec tp;
84
85     if (getclock(TIMEOFDAY, &tp) != 0) {
86         cvtErrno();
87         stdErrno();
88         return -1;
89     }
90     ((StgInt64*)sec)[0] = tp.tv_sec;
91     ((StgInt64*)nsec)[0] = tp.tv_nsec;
92     return 0;
93 #else
94 #ifdef HAVE_GETTIMEOFDAY
95     struct timeval tp;
96  
97     if (gettimeofday(&tp, NULL) != 0) {
98         cvtErrno();
99         stdErrno();
100         return -1;
101     }
102     ((StgInt64*)sec)[0] = tp.tv_sec;
103     ((StgInt64*)nsec)[0] = tp.tv_usec * 1000;
104     return 0;
105 #else
106     time_t t;
107     if ((t = time(NULL)) == (time_t) -1) {
108         cvtErrno();
109         stdErrno();
110         return -1;
111     }
112     ((StgInt64*)sec)[0] = t;
113     ((StgInt64*)nsec)[0] = 0;
114     return 0;
115 #endif
116 #endif
117 }