ViewVC Help
View File | Revision Log | Show Annotations | Revision Graph | Root Listing
root/cebix/BasiliskII/src/Unix/timer_unix.cpp
Revision: 1.1
Committed: 1999-10-03T14:16:25Z (24 years, 9 months ago) by cebix
Branch: MAIN
Branch point for: cebix
Log Message:
Initial revision

File Contents

# User Rev Content
1 cebix 1.1 /*
2     * timer_unix.cpp - Time Manager emulation, Unix specific stuff
3     *
4     * Basilisk II (C) 1997-1999 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     const uint32 TIME_OFFSET = 0x7c25cca0; // Offset Mac->Unix time in seconds
54    
55     uint32 TimerDateTime(void)
56     {
57     time_t uct_now = time(NULL);
58     struct tm tm;
59     localtime_r(&uct_now, &tm);
60     time_t local_now = mktime(&tm);
61     return (uint32)local_now + TIME_OFFSET;
62     }
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     }