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