[project @ 1999-05-03 13:22:29 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.4 1999/05/03 13:22:29 sof 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 #ifdef HAVE_WINDOWS_H
40 #include <windows.h>
41 #include <sys/types.h>
42 #include <sys/timeb.h>
43 #endif
44
45 StgInt
46 getClockTime(StgByteArray sec, StgByteArray nsec)
47 {
48 #if defined(_WIN32)
49   struct timeb t;
50
51   _ftime(&t);
52
53   ((unsigned int *)sec)[0] = (unsigned int)t.time;
54   ((unsigned int *)nsec)[0] = (unsigned int)t.millitm * 1000;
55   return 0;
56 #elif defined(HAVE_GETCLOCK)
57     struct timespec tp;
58
59     if (getclock(TIMEOFDAY, &tp) != 0) {
60         cvtErrno();
61         stdErrno();
62         return -1;
63     }
64     ((unsigned long int *)sec)[0] = tp.tv_sec;
65     ((unsigned long int *)nsec)[0] = tp.tv_nsec;
66     return 0;
67 #elif defined(HAVE_GETTIMEOFDAY)
68     struct timeval tp;
69  
70     if (gettimeofday(&tp, NULL) != 0) {
71         cvtErrno();
72         stdErrno();
73         return -1;
74     }
75     ((unsigned long int *)sec)[0] = tp.tv_sec;
76     ((unsigned long int *)nsec)[0] = tp.tv_usec * 1000;
77     return 0;
78 #else
79     time_t t;
80     if ((t = time(NULL)) == (time_t) -1) {
81         cvtErrno();
82         stdErrno();
83         return -1;
84     }
85     ((unsigned long int *)sec)[0] = t;
86     ((unsigned long int *)nsec)[0] = 0;
87     return 0;
88 #endif
89 }
90
91 StgInt
92 prim_getClockTime(StgByteArray sec, StgByteArray nsec)
93 {
94 #if defined(_WIN32)
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_GETCLOCK)
103     struct timespec tp;
104
105     if (getclock(TIMEOFDAY, &tp) != 0) {
106         cvtErrno();
107         stdErrno();
108         return -1;
109     }
110     ((StgInt64*)sec)[0] = tp.tv_sec;
111     ((StgInt64*)nsec)[0] = tp.tv_nsec;
112     return 0;
113 #elif defined(HAVE_GETTIMEOFDAY)
114     struct timeval tp;
115  
116     if (gettimeofday(&tp, NULL) != 0) {
117         cvtErrno();
118         stdErrno();
119         return -1;
120     }
121     ((StgInt64*)sec)[0] = tp.tv_sec;
122     ((StgInt64*)nsec)[0] = tp.tv_usec * 1000;
123     return 0;
124 #else
125     time_t t;
126     if ((t = time(NULL)) == (time_t) -1) {
127         cvtErrno();
128         stdErrno();
129         return -1;
130     }
131     ((StgInt64*)sec)[0] = t;
132     ((StgInt64*)nsec)[0] = 0;
133     return 0;
134 #endif
135 }