[project @ 1999-09-19 19:26:14 by sof]
[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.5 1999/09/12 14:33:56 sof Exp $
5  *
6  * getClockTime Runtime Support
7  */
8
9 #include "Rts.h"
10 #include "stgio.h"
11
12 /* It seems morally wrong to skew this in favour of
13    using non-POSIX calls (gettimeofday(), ftime()..),
14    rather than time()....so, let's re-order it all
15    (and hope OS idiosyncracies won't get in the way
16    of using time(), the moral elite's favourite.)
17  */
18
19 #if defined(HAVE_TIME_H)
20 # include <time.h>
21 #elif defined(HAVE_GETCLOCK)
22 # ifdef HAVE_SYS_TIMERS_H
23 #  define POSIX_4D9 1
24 #  include <sys/timers.h>
25 # endif
26 #elif defined(HAVE_GETTIMEOFDAY)
27 #  ifdef HAVE_SYS_TIME_H
28 #   include <sys/time.h>
29 #  endif
30 #endif
31
32 #ifdef HAVE_WINDOWS_H
33 #include <windows.h>
34 #include <sys/types.h>
35 #include <sys/timeb.h>
36 #endif
37
38 StgInt
39 getClockTime(StgByteArray sec, StgByteArray nsec)
40 {
41 #if defined(_WIN32) && !defined(cygwin32_TARGET_OS)
42   /*
43    * time() as is implemented by cygwin (in B20.1) is
44    * not right, so stay away (and use time()) instead.
45    */
46   struct timeb t;
47
48   _ftime(&t);
49
50   ((unsigned int *)sec)[0] = (unsigned int)t.time;
51   ((unsigned int *)nsec)[0] = (unsigned int)t.millitm * 1000;
52   return 0;
53 #elif defined(HAVE_TIME_H)
54     time_t t;
55     if ((t = time(NULL)) == (time_t) -1) {
56         cvtErrno();
57         stdErrno();
58         return -1;
59     }
60     ((unsigned long int *)sec)[0] = t;
61     ((unsigned long int *)nsec)[0] = 0;
62     return 0;
63 #elif defined(HAVE_GETCLOCK)
64     struct timespec tp;
65
66     if (getclock(TIMEOFDAY, &tp) != 0) {
67         cvtErrno();
68         stdErrno();
69         return -1;
70     }
71     ((unsigned long int *)sec)[0] = tp.tv_sec;
72     ((unsigned long int *)nsec)[0] = tp.tv_nsec;
73     return 0;
74 #elif defined(HAVE_GETTIMEOFDAY)
75     struct timeval tp;
76  
77     if (gettimeofday(&tp, NULL) != 0) {
78         cvtErrno();
79         stdErrno();
80         return -1;
81     }
82     ((unsigned long int *)sec)[0] = tp.tv_sec;
83     ((unsigned long int *)nsec)[0] = tp.tv_usec * 1000;
84     return 0;
85 #else
86 #error "getClockTime: don't know how to get at the clock's time"
87 #endif
88 }
89
90 StgInt
91 prim_getClockTime(StgByteArray sec, StgByteArray nsec)
92 {
93 #if defined(_WIN32) && !defined(cygwin32_TARGET_OS)
94   /* see getClockTime() comment re: ftime() & cygwin */
95   struct timeb t;
96
97   _ftime(&t);
98
99   ((unsigned long int *)sec)[0] = t.time;
100   ((unsigned long int *)nsec)[0] = t.millitm * 1000;
101   return 0;
102 #elif defined(HAVE_TIME_H)
103     time_t t;
104     if ((t = time(NULL)) == (time_t) -1) {
105         cvtErrno();
106         stdErrno();
107         return -1;
108     }
109     ((StgInt64*)sec)[0] = t;
110     ((StgInt64*)nsec)[0] = 0;
111     return 0;
112 #elif defined(HAVE_GETCLOCK)
113     struct timespec tp;
114
115     if (getclock(TIMEOFDAY, &tp) != 0) {
116         cvtErrno();
117         stdErrno();
118         return -1;
119     }
120     ((StgInt64*)sec)[0] = tp.tv_sec;
121     ((StgInt64*)nsec)[0] = tp.tv_nsec;
122     return 0;
123 #elif defined(HAVE_GETTIMEOFDAY)
124     struct timeval tp;
125  
126     if (gettimeofday(&tp, NULL) != 0) {
127         cvtErrno();
128         stdErrno();
129         return -1;
130     }
131     ((StgInt64*)sec)[0] = tp.tv_sec;
132     ((StgInt64*)nsec)[0] = tp.tv_usec * 1000;
133     return 0;
134 #else
135 #error "getClockTime: don't know how to get at the clock's time"
136 #endif
137 }