ViewVC Help
View File | Revision Log | Show Annotations | Revision Graph | Root Listing
root/cebix/BasiliskII/src/Unix/timer_unix.cpp
Revision: 1.5
Committed: 1999-10-19T17:41:39Z (25 years ago) by cebix
Branch: MAIN
CVS Tags: snapshot-21101999, snapshot-22121999, release-0_8-1, snapshot-02111999
Changes since 1.4: +0 -2 lines
Log Message:
- added external file system
- moved most init/deinit code to InitAll()/ExitAll() in main.cpp

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     uint32 TimerDateTime(void)
54     {
55 cebix 1.3 time_t utc_now = time(NULL);
56     #if defined(__linux__) || defined(__SVR4)
57 cebix 1.2 long tz = timezone;
58 cebix 1.4 time_t local_now = utc_now - tz;
59 cebix 1.2 if (daylight)
60     local_now += 3600;
61 cebix 1.3 #elif defined(__FreeBSD__) || defined(__NetBSD__)
62     time_t local_now = utc_now + localtime(&utc_now)->tm_gmtoff;
63     #else
64     time_t local_now = utc_now;
65     #endif
66 cebix 1.1 return (uint32)local_now + TIME_OFFSET;
67     }
68    
69    
70     /*
71     * Get current time
72     */
73    
74     void timer_current_time(tm_time_t &t)
75     {
76     #ifdef HAVE_CLOCK_GETTIME
77     clock_gettime(CLOCK_REALTIME, &t);
78     #else
79     gettimeofday(&t, NULL);
80     #endif
81     }
82    
83    
84     /*
85     * Add times
86     */
87    
88     void timer_add_time(tm_time_t &res, tm_time_t a, tm_time_t b)
89     {
90     #ifdef HAVE_CLOCK_GETTIME
91     res.tv_sec = a.tv_sec + b.tv_sec;
92     res.tv_nsec = a.tv_nsec + b.tv_nsec;
93     if (res.tv_nsec >= 1000000000) {
94     res.tv_sec++;
95     res.tv_nsec -= 1000000000;
96     }
97     #else
98     res.tv_sec = a.tv_sec + b.tv_sec;
99     res.tv_usec = a.tv_usec + b.tv_usec;
100     if (res.tv_usec >= 1000000) {
101     res.tv_sec++;
102     res.tv_usec -= 1000000;
103     }
104     #endif
105     }
106    
107    
108     /*
109     * Subtract times
110     */
111    
112     void timer_sub_time(tm_time_t &res, tm_time_t a, tm_time_t b)
113     {
114     #ifdef HAVE_CLOCK_GETTIME
115     res.tv_sec = a.tv_sec - b.tv_sec;
116     res.tv_nsec = a.tv_nsec - b.tv_nsec;
117     if (res.tv_nsec < 0) {
118     res.tv_sec--;
119     res.tv_nsec += 1000000000;
120     }
121     #else
122     res.tv_sec = a.tv_sec - b.tv_sec;
123     res.tv_usec = a.tv_usec - b.tv_usec;
124     if (res.tv_usec < 0) {
125     res.tv_sec--;
126     res.tv_usec += 1000000;
127     }
128     #endif
129     }
130    
131    
132     /*
133     * Compare times (<0: a < b, =0: a = b, >0: a > b)
134     */
135    
136     int timer_cmp_time(tm_time_t a, tm_time_t b)
137     {
138     #ifdef HAVE_CLOCK_GETTIME
139     if (a.tv_sec == b.tv_sec)
140     return a.tv_nsec - b.tv_nsec;
141     else
142     return a.tv_sec - b.tv_sec;
143     #else
144     if (a.tv_sec == b.tv_sec)
145     return a.tv_usec - b.tv_usec;
146     else
147     return a.tv_sec - b.tv_sec;
148     #endif
149     }
150    
151    
152     /*
153     * Convert Mac time value (>0: microseconds, <0: microseconds) to tm_time_t
154     */
155    
156     void timer_mac2host_time(tm_time_t &res, int32 mactime)
157     {
158     #ifdef HAVE_CLOCK_GETTIME
159     if (mactime > 0) {
160     // Time in milliseconds
161     res.tv_sec = mactime / 1000;
162     res.tv_nsec = (mactime % 1000) * 1000000;
163     } else {
164     // Time in negative microseconds
165     res.tv_sec = -mactime / 1000000;
166     res.tv_nsec = (-mactime % 1000000) * 1000;
167     }
168     #else
169     if (mactime > 0) {
170     // Time in milliseconds
171     res.tv_sec = mactime / 1000;
172     res.tv_usec = (mactime % 1000) * 1000;
173     } else {
174     // Time in negative microseconds
175     res.tv_sec = -mactime / 1000000;
176     res.tv_usec = -mactime % 1000000;
177     }
178     #endif
179     }
180    
181    
182     /*
183     * Convert positive tm_time_t to Mac time value (>0: microseconds, <0: microseconds)
184     * A negative input value for hosttime results in a zero return value
185     * As long as the microseconds value fits in 32 bit, it must not be converted to milliseconds!
186     */
187    
188     int32 timer_host2mac_time(tm_time_t hosttime)
189     {
190     if (hosttime.tv_sec < 0)
191     return 0;
192     else {
193     #ifdef HAVE_CLOCK_GETTIME
194     uint64 t = (uint64)hosttime.tv_sec * 1000000 + hosttime.tv_nsec / 1000;
195     #else
196     uint64 t = (uint64)hosttime.tv_sec * 1000000 + hosttime.tv_usec;
197     #endif
198     if (t > 0x7fffffff)
199     return t / 1000; // Time in milliseconds
200     else
201     return -t; // Time in negative microseconds
202     }
203     }