1 |
cebix |
1.1 |
/* |
2 |
|
|
* timer_unix.cpp - Time Manager emulation, Unix specific stuff |
3 |
|
|
* |
4 |
cebix |
1.7 |
* Basilisk II (C) 1997-2000 Christian Bauer |
5 |
cebix |
1.1 |
* |
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 |
cebix |
1.8 |
// For NetBSD with broken pthreads headers |
28 |
|
|
#ifndef CLOCK_REALTIME |
29 |
|
|
#define CLOCK_REALTIME 0 |
30 |
|
|
#endif |
31 |
|
|
|
32 |
cebix |
1.1 |
|
33 |
|
|
/* |
34 |
|
|
* Return microseconds since boot (64 bit) |
35 |
|
|
*/ |
36 |
|
|
|
37 |
|
|
void Microseconds(uint32 &hi, uint32 &lo) |
38 |
|
|
{ |
39 |
|
|
D(bug("Microseconds\n")); |
40 |
|
|
#ifdef HAVE_CLOCK_GETTIME |
41 |
|
|
struct timespec t; |
42 |
|
|
clock_gettime(CLOCK_REALTIME, &t); |
43 |
|
|
uint64 tl = (uint64)t.tv_sec * 1000000 + t.tv_nsec / 1000; |
44 |
|
|
#else |
45 |
|
|
struct timeval t; |
46 |
|
|
gettimeofday(&t, NULL); |
47 |
|
|
uint64 tl = (uint64)t.tv_sec * 1000000 + t.tv_usec; |
48 |
|
|
#endif |
49 |
|
|
hi = tl >> 32; |
50 |
|
|
lo = tl; |
51 |
|
|
} |
52 |
|
|
|
53 |
|
|
|
54 |
|
|
/* |
55 |
|
|
* Return local date/time in Mac format (seconds since 1.1.1904) |
56 |
|
|
*/ |
57 |
|
|
|
58 |
|
|
uint32 TimerDateTime(void) |
59 |
|
|
{ |
60 |
cebix |
1.3 |
time_t utc_now = time(NULL); |
61 |
|
|
#if defined(__linux__) || defined(__SVR4) |
62 |
cebix |
1.6 |
time_t local_now = utc_now - timezone; |
63 |
cebix |
1.3 |
#elif defined(__FreeBSD__) || defined(__NetBSD__) |
64 |
|
|
time_t local_now = utc_now + localtime(&utc_now)->tm_gmtoff; |
65 |
|
|
#else |
66 |
|
|
time_t local_now = utc_now; |
67 |
|
|
#endif |
68 |
cebix |
1.1 |
return (uint32)local_now + TIME_OFFSET; |
69 |
|
|
} |
70 |
|
|
|
71 |
|
|
|
72 |
|
|
/* |
73 |
|
|
* Get current time |
74 |
|
|
*/ |
75 |
|
|
|
76 |
|
|
void timer_current_time(tm_time_t &t) |
77 |
|
|
{ |
78 |
|
|
#ifdef HAVE_CLOCK_GETTIME |
79 |
|
|
clock_gettime(CLOCK_REALTIME, &t); |
80 |
|
|
#else |
81 |
|
|
gettimeofday(&t, NULL); |
82 |
|
|
#endif |
83 |
|
|
} |
84 |
|
|
|
85 |
|
|
|
86 |
|
|
/* |
87 |
|
|
* Add times |
88 |
|
|
*/ |
89 |
|
|
|
90 |
|
|
void timer_add_time(tm_time_t &res, tm_time_t a, tm_time_t b) |
91 |
|
|
{ |
92 |
|
|
#ifdef HAVE_CLOCK_GETTIME |
93 |
|
|
res.tv_sec = a.tv_sec + b.tv_sec; |
94 |
|
|
res.tv_nsec = a.tv_nsec + b.tv_nsec; |
95 |
|
|
if (res.tv_nsec >= 1000000000) { |
96 |
|
|
res.tv_sec++; |
97 |
|
|
res.tv_nsec -= 1000000000; |
98 |
|
|
} |
99 |
|
|
#else |
100 |
|
|
res.tv_sec = a.tv_sec + b.tv_sec; |
101 |
|
|
res.tv_usec = a.tv_usec + b.tv_usec; |
102 |
|
|
if (res.tv_usec >= 1000000) { |
103 |
|
|
res.tv_sec++; |
104 |
|
|
res.tv_usec -= 1000000; |
105 |
|
|
} |
106 |
|
|
#endif |
107 |
|
|
} |
108 |
|
|
|
109 |
|
|
|
110 |
|
|
/* |
111 |
|
|
* Subtract times |
112 |
|
|
*/ |
113 |
|
|
|
114 |
|
|
void timer_sub_time(tm_time_t &res, tm_time_t a, tm_time_t b) |
115 |
|
|
{ |
116 |
|
|
#ifdef HAVE_CLOCK_GETTIME |
117 |
|
|
res.tv_sec = a.tv_sec - b.tv_sec; |
118 |
|
|
res.tv_nsec = a.tv_nsec - b.tv_nsec; |
119 |
|
|
if (res.tv_nsec < 0) { |
120 |
|
|
res.tv_sec--; |
121 |
|
|
res.tv_nsec += 1000000000; |
122 |
|
|
} |
123 |
|
|
#else |
124 |
|
|
res.tv_sec = a.tv_sec - b.tv_sec; |
125 |
|
|
res.tv_usec = a.tv_usec - b.tv_usec; |
126 |
|
|
if (res.tv_usec < 0) { |
127 |
|
|
res.tv_sec--; |
128 |
|
|
res.tv_usec += 1000000; |
129 |
|
|
} |
130 |
|
|
#endif |
131 |
|
|
} |
132 |
|
|
|
133 |
|
|
|
134 |
|
|
/* |
135 |
|
|
* Compare times (<0: a < b, =0: a = b, >0: a > b) |
136 |
|
|
*/ |
137 |
|
|
|
138 |
|
|
int timer_cmp_time(tm_time_t a, tm_time_t b) |
139 |
|
|
{ |
140 |
|
|
#ifdef HAVE_CLOCK_GETTIME |
141 |
|
|
if (a.tv_sec == b.tv_sec) |
142 |
|
|
return a.tv_nsec - b.tv_nsec; |
143 |
|
|
else |
144 |
|
|
return a.tv_sec - b.tv_sec; |
145 |
|
|
#else |
146 |
|
|
if (a.tv_sec == b.tv_sec) |
147 |
|
|
return a.tv_usec - b.tv_usec; |
148 |
|
|
else |
149 |
|
|
return a.tv_sec - b.tv_sec; |
150 |
|
|
#endif |
151 |
|
|
} |
152 |
|
|
|
153 |
|
|
|
154 |
|
|
/* |
155 |
|
|
* Convert Mac time value (>0: microseconds, <0: microseconds) to tm_time_t |
156 |
|
|
*/ |
157 |
|
|
|
158 |
|
|
void timer_mac2host_time(tm_time_t &res, int32 mactime) |
159 |
|
|
{ |
160 |
|
|
#ifdef HAVE_CLOCK_GETTIME |
161 |
|
|
if (mactime > 0) { |
162 |
|
|
// Time in milliseconds |
163 |
|
|
res.tv_sec = mactime / 1000; |
164 |
|
|
res.tv_nsec = (mactime % 1000) * 1000000; |
165 |
|
|
} else { |
166 |
|
|
// Time in negative microseconds |
167 |
|
|
res.tv_sec = -mactime / 1000000; |
168 |
|
|
res.tv_nsec = (-mactime % 1000000) * 1000; |
169 |
|
|
} |
170 |
|
|
#else |
171 |
|
|
if (mactime > 0) { |
172 |
|
|
// Time in milliseconds |
173 |
|
|
res.tv_sec = mactime / 1000; |
174 |
|
|
res.tv_usec = (mactime % 1000) * 1000; |
175 |
|
|
} else { |
176 |
|
|
// Time in negative microseconds |
177 |
|
|
res.tv_sec = -mactime / 1000000; |
178 |
|
|
res.tv_usec = -mactime % 1000000; |
179 |
|
|
} |
180 |
|
|
#endif |
181 |
|
|
} |
182 |
|
|
|
183 |
|
|
|
184 |
|
|
/* |
185 |
|
|
* Convert positive tm_time_t to Mac time value (>0: microseconds, <0: microseconds) |
186 |
|
|
* A negative input value for hosttime results in a zero return value |
187 |
|
|
* As long as the microseconds value fits in 32 bit, it must not be converted to milliseconds! |
188 |
|
|
*/ |
189 |
|
|
|
190 |
|
|
int32 timer_host2mac_time(tm_time_t hosttime) |
191 |
|
|
{ |
192 |
|
|
if (hosttime.tv_sec < 0) |
193 |
|
|
return 0; |
194 |
|
|
else { |
195 |
|
|
#ifdef HAVE_CLOCK_GETTIME |
196 |
|
|
uint64 t = (uint64)hosttime.tv_sec * 1000000 + hosttime.tv_nsec / 1000; |
197 |
|
|
#else |
198 |
|
|
uint64 t = (uint64)hosttime.tv_sec * 1000000 + hosttime.tv_usec; |
199 |
|
|
#endif |
200 |
|
|
if (t > 0x7fffffff) |
201 |
|
|
return t / 1000; // Time in milliseconds |
202 |
|
|
else |
203 |
|
|
return -t; // Time in negative microseconds |
204 |
|
|
} |
205 |
|
|
} |