1 |
/* |
2 |
* timer_unix.cpp - Time Manager emulation, Unix specific stuff |
3 |
* |
4 |
* Basilisk II (C) 1997-2000 Christian Bauer |
5 |
* |
6 |
* This program is free software; you can redistribute it and/or modify |
7 |
* it under the terms of the GNU General Public License as published by |
8 |
* the Free Software Foundation; either version 2 of the License, or |
9 |
* (at your option) any later version. |
10 |
* |
11 |
* This program is distributed in the hope that it will be useful, |
12 |
* but WITHOUT ANY WARRANTY; without even the implied warranty of |
13 |
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
14 |
* GNU General Public License for more details. |
15 |
* |
16 |
* You should have received a copy of the GNU General Public License |
17 |
* along with this program; if not, write to the Free Software |
18 |
* Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA |
19 |
*/ |
20 |
|
21 |
#include "sysdeps.h" |
22 |
#include "timer.h" |
23 |
|
24 |
#define DEBUG 0 |
25 |
#include "debug.h" |
26 |
|
27 |
|
28 |
/* |
29 |
* Return microseconds since boot (64 bit) |
30 |
*/ |
31 |
|
32 |
void Microseconds(uint32 &hi, uint32 &lo) |
33 |
{ |
34 |
D(bug("Microseconds\n")); |
35 |
#ifdef HAVE_CLOCK_GETTIME |
36 |
struct timespec t; |
37 |
clock_gettime(CLOCK_REALTIME, &t); |
38 |
uint64 tl = (uint64)t.tv_sec * 1000000 + t.tv_nsec / 1000; |
39 |
#else |
40 |
struct timeval t; |
41 |
gettimeofday(&t, NULL); |
42 |
uint64 tl = (uint64)t.tv_sec * 1000000 + t.tv_usec; |
43 |
#endif |
44 |
hi = tl >> 32; |
45 |
lo = tl; |
46 |
} |
47 |
|
48 |
|
49 |
/* |
50 |
* Return local date/time in Mac format (seconds since 1.1.1904) |
51 |
*/ |
52 |
|
53 |
uint32 TimerDateTime(void) |
54 |
{ |
55 |
time_t utc_now = time(NULL); |
56 |
#if defined(__linux__) || defined(__SVR4) |
57 |
time_t local_now = utc_now - timezone; |
58 |
#elif defined(__FreeBSD__) || defined(__NetBSD__) |
59 |
time_t local_now = utc_now + localtime(&utc_now)->tm_gmtoff; |
60 |
#else |
61 |
time_t local_now = utc_now; |
62 |
#endif |
63 |
return (uint32)local_now + TIME_OFFSET; |
64 |
} |
65 |
|
66 |
|
67 |
/* |
68 |
* Get current time |
69 |
*/ |
70 |
|
71 |
void timer_current_time(tm_time_t &t) |
72 |
{ |
73 |
#ifdef HAVE_CLOCK_GETTIME |
74 |
clock_gettime(CLOCK_REALTIME, &t); |
75 |
#else |
76 |
gettimeofday(&t, NULL); |
77 |
#endif |
78 |
} |
79 |
|
80 |
|
81 |
/* |
82 |
* Add times |
83 |
*/ |
84 |
|
85 |
void timer_add_time(tm_time_t &res, tm_time_t a, tm_time_t b) |
86 |
{ |
87 |
#ifdef HAVE_CLOCK_GETTIME |
88 |
res.tv_sec = a.tv_sec + b.tv_sec; |
89 |
res.tv_nsec = a.tv_nsec + b.tv_nsec; |
90 |
if (res.tv_nsec >= 1000000000) { |
91 |
res.tv_sec++; |
92 |
res.tv_nsec -= 1000000000; |
93 |
} |
94 |
#else |
95 |
res.tv_sec = a.tv_sec + b.tv_sec; |
96 |
res.tv_usec = a.tv_usec + b.tv_usec; |
97 |
if (res.tv_usec >= 1000000) { |
98 |
res.tv_sec++; |
99 |
res.tv_usec -= 1000000; |
100 |
} |
101 |
#endif |
102 |
} |
103 |
|
104 |
|
105 |
/* |
106 |
* Subtract times |
107 |
*/ |
108 |
|
109 |
void timer_sub_time(tm_time_t &res, tm_time_t a, tm_time_t b) |
110 |
{ |
111 |
#ifdef HAVE_CLOCK_GETTIME |
112 |
res.tv_sec = a.tv_sec - b.tv_sec; |
113 |
res.tv_nsec = a.tv_nsec - b.tv_nsec; |
114 |
if (res.tv_nsec < 0) { |
115 |
res.tv_sec--; |
116 |
res.tv_nsec += 1000000000; |
117 |
} |
118 |
#else |
119 |
res.tv_sec = a.tv_sec - b.tv_sec; |
120 |
res.tv_usec = a.tv_usec - b.tv_usec; |
121 |
if (res.tv_usec < 0) { |
122 |
res.tv_sec--; |
123 |
res.tv_usec += 1000000; |
124 |
} |
125 |
#endif |
126 |
} |
127 |
|
128 |
|
129 |
/* |
130 |
* Compare times (<0: a < b, =0: a = b, >0: a > b) |
131 |
*/ |
132 |
|
133 |
int timer_cmp_time(tm_time_t a, tm_time_t b) |
134 |
{ |
135 |
#ifdef HAVE_CLOCK_GETTIME |
136 |
if (a.tv_sec == b.tv_sec) |
137 |
return a.tv_nsec - b.tv_nsec; |
138 |
else |
139 |
return a.tv_sec - b.tv_sec; |
140 |
#else |
141 |
if (a.tv_sec == b.tv_sec) |
142 |
return a.tv_usec - b.tv_usec; |
143 |
else |
144 |
return a.tv_sec - b.tv_sec; |
145 |
#endif |
146 |
} |
147 |
|
148 |
|
149 |
/* |
150 |
* Convert Mac time value (>0: microseconds, <0: microseconds) to tm_time_t |
151 |
*/ |
152 |
|
153 |
void timer_mac2host_time(tm_time_t &res, int32 mactime) |
154 |
{ |
155 |
#ifdef HAVE_CLOCK_GETTIME |
156 |
if (mactime > 0) { |
157 |
// Time in milliseconds |
158 |
res.tv_sec = mactime / 1000; |
159 |
res.tv_nsec = (mactime % 1000) * 1000000; |
160 |
} else { |
161 |
// Time in negative microseconds |
162 |
res.tv_sec = -mactime / 1000000; |
163 |
res.tv_nsec = (-mactime % 1000000) * 1000; |
164 |
} |
165 |
#else |
166 |
if (mactime > 0) { |
167 |
// Time in milliseconds |
168 |
res.tv_sec = mactime / 1000; |
169 |
res.tv_usec = (mactime % 1000) * 1000; |
170 |
} else { |
171 |
// Time in negative microseconds |
172 |
res.tv_sec = -mactime / 1000000; |
173 |
res.tv_usec = -mactime % 1000000; |
174 |
} |
175 |
#endif |
176 |
} |
177 |
|
178 |
|
179 |
/* |
180 |
* Convert positive tm_time_t to Mac time value (>0: microseconds, <0: microseconds) |
181 |
* A negative input value for hosttime results in a zero return value |
182 |
* As long as the microseconds value fits in 32 bit, it must not be converted to milliseconds! |
183 |
*/ |
184 |
|
185 |
int32 timer_host2mac_time(tm_time_t hosttime) |
186 |
{ |
187 |
if (hosttime.tv_sec < 0) |
188 |
return 0; |
189 |
else { |
190 |
#ifdef HAVE_CLOCK_GETTIME |
191 |
uint64 t = (uint64)hosttime.tv_sec * 1000000 + hosttime.tv_nsec / 1000; |
192 |
#else |
193 |
uint64 t = (uint64)hosttime.tv_sec * 1000000 + hosttime.tv_usec; |
194 |
#endif |
195 |
if (t > 0x7fffffff) |
196 |
return t / 1000; // Time in milliseconds |
197 |
else |
198 |
return -t; // Time in negative microseconds |
199 |
} |
200 |
} |