Mercurial > sdl-ios-xcode
annotate src/timer/linux/SDL_systimer.c @ 1225:09bc22169702
Windows should use _beginthreadex() instead of CreateThread(), to avoid a
memory leak on each joined thread.
author | Ryan C. Gordon <icculus@icculus.org> |
---|---|
date | Wed, 04 Jan 2006 21:01:49 +0000 |
parents | ec446adf8cb2 |
children | c9b51268668f |
rev | line source |
---|---|
0 | 1 /* |
2 SDL - Simple DirectMedia Layer | |
769
b8d311d90021
Updated copyright information for 2004 (Happy New Year!)
Sam Lantinga <slouken@libsdl.org>
parents:
415
diff
changeset
|
3 Copyright (C) 1997-2004 Sam Lantinga |
0 | 4 |
5 This library is free software; you can redistribute it and/or | |
6 modify it under the terms of the GNU Library General Public | |
7 License as published by the Free Software Foundation; either | |
8 version 2 of the License, or (at your option) any later version. | |
9 | |
10 This library is distributed in the hope that it will be useful, | |
11 but WITHOUT ANY WARRANTY; without even the implied warranty of | |
12 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU | |
13 Library General Public License for more details. | |
14 | |
15 You should have received a copy of the GNU Library General Public | |
16 License along with this library; if not, write to the Free | |
17 Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA | |
18 | |
19 Sam Lantinga | |
252
e8157fcb3114
Updated the source with the correct e-mail address
Sam Lantinga <slouken@libsdl.org>
parents:
166
diff
changeset
|
20 slouken@libsdl.org |
316
d85fc19bf840
Added UNIX RDTSC code by Lompak (disabled by default)
Sam Lantinga <slouken@libsdl.org>
parents:
297
diff
changeset
|
21 |
d85fc19bf840
Added UNIX RDTSC code by Lompak (disabled by default)
Sam Lantinga <slouken@libsdl.org>
parents:
297
diff
changeset
|
22 RDTSC stuff by lompik (lompik@voila.fr) 20/03/2002 |
0 | 23 */ |
24 | |
25 #ifdef SAVE_RCSID | |
26 static char rcsid = | |
27 "@(#) $Id$"; | |
28 #endif | |
29 | |
30 #include <stdio.h> | |
31 #include <sys/time.h> | |
32 #include <signal.h> | |
33 #include <unistd.h> | |
34 #include <string.h> | |
35 #include <errno.h> | |
36 | |
1157
baf35853ad54
Date: Mon, 10 Oct 2005 13:09:32 +0300
Ryan C. Gordon <icculus@icculus.org>
parents:
1156
diff
changeset
|
37 /* The clock_gettime provides monotonous time, so we should use it if |
baf35853ad54
Date: Mon, 10 Oct 2005 13:09:32 +0300
Ryan C. Gordon <icculus@icculus.org>
parents:
1156
diff
changeset
|
38 it's available. The clock_gettime function is behind ifdef |
baf35853ad54
Date: Mon, 10 Oct 2005 13:09:32 +0300
Ryan C. Gordon <icculus@icculus.org>
parents:
1156
diff
changeset
|
39 for __USE_POSIX199309 |
baf35853ad54
Date: Mon, 10 Oct 2005 13:09:32 +0300
Ryan C. Gordon <icculus@icculus.org>
parents:
1156
diff
changeset
|
40 Tommi Kyntola (tommi.kyntola@ray.fi) 27/09/2005 |
baf35853ad54
Date: Mon, 10 Oct 2005 13:09:32 +0300
Ryan C. Gordon <icculus@icculus.org>
parents:
1156
diff
changeset
|
41 */ |
baf35853ad54
Date: Mon, 10 Oct 2005 13:09:32 +0300
Ryan C. Gordon <icculus@icculus.org>
parents:
1156
diff
changeset
|
42 #if (defined _POSIX_TIMERS && _POSIX_TIMERS > 0) |
baf35853ad54
Date: Mon, 10 Oct 2005 13:09:32 +0300
Ryan C. Gordon <icculus@icculus.org>
parents:
1156
diff
changeset
|
43 #include <time.h> |
1158
ec446adf8cb2
POSIX clock_gettime() isn't available on Linux before 2.6, and the symbol may
Ryan C. Gordon <icculus@icculus.org>
parents:
1157
diff
changeset
|
44 /* |
ec446adf8cb2
POSIX clock_gettime() isn't available on Linux before 2.6, and the symbol may
Ryan C. Gordon <icculus@icculus.org>
parents:
1157
diff
changeset
|
45 * clock_gettime() is missing in my system's glibc, and apparently isn't |
ec446adf8cb2
POSIX clock_gettime() isn't available on Linux before 2.6, and the symbol may
Ryan C. Gordon <icculus@icculus.org>
parents:
1157
diff
changeset
|
46 * available before Linux kernel 2.6...you can uncomment the following |
ec446adf8cb2
POSIX clock_gettime() isn't available on Linux before 2.6, and the symbol may
Ryan C. Gordon <icculus@icculus.org>
parents:
1157
diff
changeset
|
47 * define to use it, since it may be a better solution than |
ec446adf8cb2
POSIX clock_gettime() isn't available on Linux before 2.6, and the symbol may
Ryan C. Gordon <icculus@icculus.org>
parents:
1157
diff
changeset
|
48 * gettimeofday() on systems that support the newer syscall. --ryan. |
ec446adf8cb2
POSIX clock_gettime() isn't available on Linux before 2.6, and the symbol may
Ryan C. Gordon <icculus@icculus.org>
parents:
1157
diff
changeset
|
49 */ |
ec446adf8cb2
POSIX clock_gettime() isn't available on Linux before 2.6, and the symbol may
Ryan C. Gordon <icculus@icculus.org>
parents:
1157
diff
changeset
|
50 /*#define USE_CLOCK_GETTIME*/ |
1157
baf35853ad54
Date: Mon, 10 Oct 2005 13:09:32 +0300
Ryan C. Gordon <icculus@icculus.org>
parents:
1156
diff
changeset
|
51 #endif |
baf35853ad54
Date: Mon, 10 Oct 2005 13:09:32 +0300
Ryan C. Gordon <icculus@icculus.org>
parents:
1156
diff
changeset
|
52 |
0 | 53 #include "SDL_error.h" |
54 #include "SDL_timer.h" | |
55 #include "SDL_timer_c.h" | |
56 | |
1
cf2af46e9e2a
Changes since SDL 1.2.0 release
Sam Lantinga <slouken@lokigames.com>
parents:
0
diff
changeset
|
57 #if _POSIX_THREAD_SYSCALL_SOFT |
cf2af46e9e2a
Changes since SDL 1.2.0 release
Sam Lantinga <slouken@lokigames.com>
parents:
0
diff
changeset
|
58 #include <pthread.h> |
cf2af46e9e2a
Changes since SDL 1.2.0 release
Sam Lantinga <slouken@lokigames.com>
parents:
0
diff
changeset
|
59 #endif |
415
104f32d04cd1
Fixed building with pthreads and the pth library
Sam Lantinga <slouken@libsdl.org>
parents:
392
diff
changeset
|
60 #ifdef ENABLE_PTH |
104f32d04cd1
Fixed building with pthreads and the pth library
Sam Lantinga <slouken@libsdl.org>
parents:
392
diff
changeset
|
61 #include <pth.h> |
104f32d04cd1
Fixed building with pthreads and the pth library
Sam Lantinga <slouken@libsdl.org>
parents:
392
diff
changeset
|
62 #endif |
1
cf2af46e9e2a
Changes since SDL 1.2.0 release
Sam Lantinga <slouken@lokigames.com>
parents:
0
diff
changeset
|
63 |
0 | 64 #if defined(DISABLE_THREADS) || defined(FORK_HACK) |
65 #define USE_ITIMER | |
66 #endif | |
67 | |
68 /* The following defines should really be determined at configure time */ | |
69 | |
166
39877400bd1e
Fixed Solaris nitpicks (thanks Mattias!)
Sam Lantinga <slouken@libsdl.org>
parents:
1
diff
changeset
|
70 #if defined(linux) |
0 | 71 /* Linux select() changes its timeout parameter upon return to contain |
72 the remaining time. Most other unixen leave it unchanged or undefined. */ | |
73 #define SELECT_SETS_REMAINING | |
1156
8521404a33c7
Date: Tue, 11 Oct 2005 16:58:12 +0300 (EEST)
Ryan C. Gordon <icculus@icculus.org>
parents:
769
diff
changeset
|
74 #elif defined(__bsdi__) || defined(__FreeBSD__) || defined(__sun) || defined(MACOSX) |
0 | 75 #define USE_NANOSLEEP |
76 #endif | |
77 | |
316
d85fc19bf840
Added UNIX RDTSC code by Lompak (disabled by default)
Sam Lantinga <slouken@libsdl.org>
parents:
297
diff
changeset
|
78 #if defined(i386) || defined(__i386__) |
317
4e8827521296
*** empty log message ***
Sam Lantinga <slouken@libsdl.org>
parents:
316
diff
changeset
|
79 /* This only works on pentium or newer x86 processors */ |
316
d85fc19bf840
Added UNIX RDTSC code by Lompak (disabled by default)
Sam Lantinga <slouken@libsdl.org>
parents:
297
diff
changeset
|
80 /* Actually, this isn't reliable on multi-cpu systems, so is disabled */ |
d85fc19bf840
Added UNIX RDTSC code by Lompak (disabled by default)
Sam Lantinga <slouken@libsdl.org>
parents:
297
diff
changeset
|
81 /*#define USE_RDTSC*/ |
d85fc19bf840
Added UNIX RDTSC code by Lompak (disabled by default)
Sam Lantinga <slouken@libsdl.org>
parents:
297
diff
changeset
|
82 #endif |
d85fc19bf840
Added UNIX RDTSC code by Lompak (disabled by default)
Sam Lantinga <slouken@libsdl.org>
parents:
297
diff
changeset
|
83 |
d85fc19bf840
Added UNIX RDTSC code by Lompak (disabled by default)
Sam Lantinga <slouken@libsdl.org>
parents:
297
diff
changeset
|
84 |
d85fc19bf840
Added UNIX RDTSC code by Lompak (disabled by default)
Sam Lantinga <slouken@libsdl.org>
parents:
297
diff
changeset
|
85 #ifdef USE_RDTSC |
d85fc19bf840
Added UNIX RDTSC code by Lompak (disabled by default)
Sam Lantinga <slouken@libsdl.org>
parents:
297
diff
changeset
|
86 |
d85fc19bf840
Added UNIX RDTSC code by Lompak (disabled by default)
Sam Lantinga <slouken@libsdl.org>
parents:
297
diff
changeset
|
87 /* The first ticks value of the application */ |
d85fc19bf840
Added UNIX RDTSC code by Lompak (disabled by default)
Sam Lantinga <slouken@libsdl.org>
parents:
297
diff
changeset
|
88 static unsigned long long start; |
d85fc19bf840
Added UNIX RDTSC code by Lompak (disabled by default)
Sam Lantinga <slouken@libsdl.org>
parents:
297
diff
changeset
|
89 static float cpu_mhz1000 = 0.0f; |
d85fc19bf840
Added UNIX RDTSC code by Lompak (disabled by default)
Sam Lantinga <slouken@libsdl.org>
parents:
297
diff
changeset
|
90 |
d85fc19bf840
Added UNIX RDTSC code by Lompak (disabled by default)
Sam Lantinga <slouken@libsdl.org>
parents:
297
diff
changeset
|
91 #if 1 |
d85fc19bf840
Added UNIX RDTSC code by Lompak (disabled by default)
Sam Lantinga <slouken@libsdl.org>
parents:
297
diff
changeset
|
92 /* This is for old binutils version that don't recognize rdtsc mnemonics. |
d85fc19bf840
Added UNIX RDTSC code by Lompak (disabled by default)
Sam Lantinga <slouken@libsdl.org>
parents:
297
diff
changeset
|
93 But all binutils version supports this. |
d85fc19bf840
Added UNIX RDTSC code by Lompak (disabled by default)
Sam Lantinga <slouken@libsdl.org>
parents:
297
diff
changeset
|
94 */ |
392
71fe0b713085
*** empty log message ***
Sam Lantinga <slouken@libsdl.org>
parents:
317
diff
changeset
|
95 #define rdtsc(t) asm __volatile__ (".byte 0x0f, 0x31; " : "=A" (t)) |
316
d85fc19bf840
Added UNIX RDTSC code by Lompak (disabled by default)
Sam Lantinga <slouken@libsdl.org>
parents:
297
diff
changeset
|
96 #else |
392
71fe0b713085
*** empty log message ***
Sam Lantinga <slouken@libsdl.org>
parents:
317
diff
changeset
|
97 #define rdtsc(t) asm __volatile__ ("rdtsc" : "=A" (t)) |
316
d85fc19bf840
Added UNIX RDTSC code by Lompak (disabled by default)
Sam Lantinga <slouken@libsdl.org>
parents:
297
diff
changeset
|
98 #endif |
d85fc19bf840
Added UNIX RDTSC code by Lompak (disabled by default)
Sam Lantinga <slouken@libsdl.org>
parents:
297
diff
changeset
|
99 |
d85fc19bf840
Added UNIX RDTSC code by Lompak (disabled by default)
Sam Lantinga <slouken@libsdl.org>
parents:
297
diff
changeset
|
100 static float calc_cpu_mhz(void) |
d85fc19bf840
Added UNIX RDTSC code by Lompak (disabled by default)
Sam Lantinga <slouken@libsdl.org>
parents:
297
diff
changeset
|
101 { |
d85fc19bf840
Added UNIX RDTSC code by Lompak (disabled by default)
Sam Lantinga <slouken@libsdl.org>
parents:
297
diff
changeset
|
102 float cpu_mhz; |
d85fc19bf840
Added UNIX RDTSC code by Lompak (disabled by default)
Sam Lantinga <slouken@libsdl.org>
parents:
297
diff
changeset
|
103 unsigned long long tsc_start; |
d85fc19bf840
Added UNIX RDTSC code by Lompak (disabled by default)
Sam Lantinga <slouken@libsdl.org>
parents:
297
diff
changeset
|
104 unsigned long long tsc_end; |
1157
baf35853ad54
Date: Mon, 10 Oct 2005 13:09:32 +0300
Ryan C. Gordon <icculus@icculus.org>
parents:
1156
diff
changeset
|
105 /* Slight code doubling here for the sake of readability */ |
baf35853ad54
Date: Mon, 10 Oct 2005 13:09:32 +0300
Ryan C. Gordon <icculus@icculus.org>
parents:
1156
diff
changeset
|
106 #ifdef USE_CLOCK_GETTIME |
baf35853ad54
Date: Mon, 10 Oct 2005 13:09:32 +0300
Ryan C. Gordon <icculus@icculus.org>
parents:
1156
diff
changeset
|
107 struct timespec tv_start, tv_end; |
baf35853ad54
Date: Mon, 10 Oct 2005 13:09:32 +0300
Ryan C. Gordon <icculus@icculus.org>
parents:
1156
diff
changeset
|
108 long usec_delay; |
baf35853ad54
Date: Mon, 10 Oct 2005 13:09:32 +0300
Ryan C. Gordon <icculus@icculus.org>
parents:
1156
diff
changeset
|
109 |
baf35853ad54
Date: Mon, 10 Oct 2005 13:09:32 +0300
Ryan C. Gordon <icculus@icculus.org>
parents:
1156
diff
changeset
|
110 rdtsc(tsc_start); |
baf35853ad54
Date: Mon, 10 Oct 2005 13:09:32 +0300
Ryan C. Gordon <icculus@icculus.org>
parents:
1156
diff
changeset
|
111 clock_gettime(CLOCK_MONOTONIC,&tv_start); |
baf35853ad54
Date: Mon, 10 Oct 2005 13:09:32 +0300
Ryan C. Gordon <icculus@icculus.org>
parents:
1156
diff
changeset
|
112 sleep(1); |
baf35853ad54
Date: Mon, 10 Oct 2005 13:09:32 +0300
Ryan C. Gordon <icculus@icculus.org>
parents:
1156
diff
changeset
|
113 rdtsc(tsc_end); |
baf35853ad54
Date: Mon, 10 Oct 2005 13:09:32 +0300
Ryan C. Gordon <icculus@icculus.org>
parents:
1156
diff
changeset
|
114 clock_gettime(CLOCK_MONOTONIC,&tv_end); |
baf35853ad54
Date: Mon, 10 Oct 2005 13:09:32 +0300
Ryan C. Gordon <icculus@icculus.org>
parents:
1156
diff
changeset
|
115 usec_delay = (1000000000L * (tv_end.tv_sec - tv_start.tv_sec) + |
baf35853ad54
Date: Mon, 10 Oct 2005 13:09:32 +0300
Ryan C. Gordon <icculus@icculus.org>
parents:
1156
diff
changeset
|
116 (tv_end.tv_nsec - tv_start.tv_nsec)) / 1000; |
baf35853ad54
Date: Mon, 10 Oct 2005 13:09:32 +0300
Ryan C. Gordon <icculus@icculus.org>
parents:
1156
diff
changeset
|
117 #else |
316
d85fc19bf840
Added UNIX RDTSC code by Lompak (disabled by default)
Sam Lantinga <slouken@libsdl.org>
parents:
297
diff
changeset
|
118 struct timeval tv_start, tv_end; |
d85fc19bf840
Added UNIX RDTSC code by Lompak (disabled by default)
Sam Lantinga <slouken@libsdl.org>
parents:
297
diff
changeset
|
119 long usec_delay; |
d85fc19bf840
Added UNIX RDTSC code by Lompak (disabled by default)
Sam Lantinga <slouken@libsdl.org>
parents:
297
diff
changeset
|
120 |
d85fc19bf840
Added UNIX RDTSC code by Lompak (disabled by default)
Sam Lantinga <slouken@libsdl.org>
parents:
297
diff
changeset
|
121 rdtsc(tsc_start); |
d85fc19bf840
Added UNIX RDTSC code by Lompak (disabled by default)
Sam Lantinga <slouken@libsdl.org>
parents:
297
diff
changeset
|
122 gettimeofday(&tv_start, NULL); |
d85fc19bf840
Added UNIX RDTSC code by Lompak (disabled by default)
Sam Lantinga <slouken@libsdl.org>
parents:
297
diff
changeset
|
123 sleep(1); |
d85fc19bf840
Added UNIX RDTSC code by Lompak (disabled by default)
Sam Lantinga <slouken@libsdl.org>
parents:
297
diff
changeset
|
124 rdtsc(tsc_end); |
d85fc19bf840
Added UNIX RDTSC code by Lompak (disabled by default)
Sam Lantinga <slouken@libsdl.org>
parents:
297
diff
changeset
|
125 gettimeofday(&tv_end, NULL); |
d85fc19bf840
Added UNIX RDTSC code by Lompak (disabled by default)
Sam Lantinga <slouken@libsdl.org>
parents:
297
diff
changeset
|
126 usec_delay = 1000000L * (tv_end.tv_sec - tv_start.tv_sec) + |
d85fc19bf840
Added UNIX RDTSC code by Lompak (disabled by default)
Sam Lantinga <slouken@libsdl.org>
parents:
297
diff
changeset
|
127 (tv_end.tv_usec - tv_start.tv_usec); |
1157
baf35853ad54
Date: Mon, 10 Oct 2005 13:09:32 +0300
Ryan C. Gordon <icculus@icculus.org>
parents:
1156
diff
changeset
|
128 #endif /* USE_CLOCK_GETTIME */ |
316
d85fc19bf840
Added UNIX RDTSC code by Lompak (disabled by default)
Sam Lantinga <slouken@libsdl.org>
parents:
297
diff
changeset
|
129 cpu_mhz = (float)(tsc_end-tsc_start) / usec_delay; |
d85fc19bf840
Added UNIX RDTSC code by Lompak (disabled by default)
Sam Lantinga <slouken@libsdl.org>
parents:
297
diff
changeset
|
130 #if 0 |
d85fc19bf840
Added UNIX RDTSC code by Lompak (disabled by default)
Sam Lantinga <slouken@libsdl.org>
parents:
297
diff
changeset
|
131 printf("cpu MHz\t\t: %.3f\n", cpu_mhz); |
d85fc19bf840
Added UNIX RDTSC code by Lompak (disabled by default)
Sam Lantinga <slouken@libsdl.org>
parents:
297
diff
changeset
|
132 #endif |
d85fc19bf840
Added UNIX RDTSC code by Lompak (disabled by default)
Sam Lantinga <slouken@libsdl.org>
parents:
297
diff
changeset
|
133 return cpu_mhz; |
d85fc19bf840
Added UNIX RDTSC code by Lompak (disabled by default)
Sam Lantinga <slouken@libsdl.org>
parents:
297
diff
changeset
|
134 } |
d85fc19bf840
Added UNIX RDTSC code by Lompak (disabled by default)
Sam Lantinga <slouken@libsdl.org>
parents:
297
diff
changeset
|
135 |
d85fc19bf840
Added UNIX RDTSC code by Lompak (disabled by default)
Sam Lantinga <slouken@libsdl.org>
parents:
297
diff
changeset
|
136 #else |
0 | 137 |
138 /* The first ticks value of the application */ | |
1157
baf35853ad54
Date: Mon, 10 Oct 2005 13:09:32 +0300
Ryan C. Gordon <icculus@icculus.org>
parents:
1156
diff
changeset
|
139 #ifdef USE_CLOCK_GETTIME |
baf35853ad54
Date: Mon, 10 Oct 2005 13:09:32 +0300
Ryan C. Gordon <icculus@icculus.org>
parents:
1156
diff
changeset
|
140 static struct timespec start; |
baf35853ad54
Date: Mon, 10 Oct 2005 13:09:32 +0300
Ryan C. Gordon <icculus@icculus.org>
parents:
1156
diff
changeset
|
141 #else |
0 | 142 static struct timeval start; |
1157
baf35853ad54
Date: Mon, 10 Oct 2005 13:09:32 +0300
Ryan C. Gordon <icculus@icculus.org>
parents:
1156
diff
changeset
|
143 #endif /* USE_CLOCK_GETTIME */ |
0 | 144 |
316
d85fc19bf840
Added UNIX RDTSC code by Lompak (disabled by default)
Sam Lantinga <slouken@libsdl.org>
parents:
297
diff
changeset
|
145 #endif /* USE_RDTSC */ |
d85fc19bf840
Added UNIX RDTSC code by Lompak (disabled by default)
Sam Lantinga <slouken@libsdl.org>
parents:
297
diff
changeset
|
146 |
d85fc19bf840
Added UNIX RDTSC code by Lompak (disabled by default)
Sam Lantinga <slouken@libsdl.org>
parents:
297
diff
changeset
|
147 |
0 | 148 void SDL_StartTicks(void) |
149 { | |
150 /* Set first ticks value */ | |
316
d85fc19bf840
Added UNIX RDTSC code by Lompak (disabled by default)
Sam Lantinga <slouken@libsdl.org>
parents:
297
diff
changeset
|
151 #ifdef USE_RDTSC |
d85fc19bf840
Added UNIX RDTSC code by Lompak (disabled by default)
Sam Lantinga <slouken@libsdl.org>
parents:
297
diff
changeset
|
152 if ( ! cpu_mhz1000 ) { |
d85fc19bf840
Added UNIX RDTSC code by Lompak (disabled by default)
Sam Lantinga <slouken@libsdl.org>
parents:
297
diff
changeset
|
153 cpu_mhz1000 = calc_cpu_mhz() * 1000.0f; |
d85fc19bf840
Added UNIX RDTSC code by Lompak (disabled by default)
Sam Lantinga <slouken@libsdl.org>
parents:
297
diff
changeset
|
154 } |
d85fc19bf840
Added UNIX RDTSC code by Lompak (disabled by default)
Sam Lantinga <slouken@libsdl.org>
parents:
297
diff
changeset
|
155 rdtsc(start); |
1157
baf35853ad54
Date: Mon, 10 Oct 2005 13:09:32 +0300
Ryan C. Gordon <icculus@icculus.org>
parents:
1156
diff
changeset
|
156 #elif defined(USE_CLOCK_GETTIME) |
baf35853ad54
Date: Mon, 10 Oct 2005 13:09:32 +0300
Ryan C. Gordon <icculus@icculus.org>
parents:
1156
diff
changeset
|
157 clock_gettime(CLOCK_MONOTONIC,&start); |
316
d85fc19bf840
Added UNIX RDTSC code by Lompak (disabled by default)
Sam Lantinga <slouken@libsdl.org>
parents:
297
diff
changeset
|
158 #else |
0 | 159 gettimeofday(&start, NULL); |
1157
baf35853ad54
Date: Mon, 10 Oct 2005 13:09:32 +0300
Ryan C. Gordon <icculus@icculus.org>
parents:
1156
diff
changeset
|
160 #endif |
0 | 161 } |
162 | |
163 Uint32 SDL_GetTicks (void) | |
164 { | |
316
d85fc19bf840
Added UNIX RDTSC code by Lompak (disabled by default)
Sam Lantinga <slouken@libsdl.org>
parents:
297
diff
changeset
|
165 #ifdef USE_RDTSC |
d85fc19bf840
Added UNIX RDTSC code by Lompak (disabled by default)
Sam Lantinga <slouken@libsdl.org>
parents:
297
diff
changeset
|
166 unsigned long long now; |
d85fc19bf840
Added UNIX RDTSC code by Lompak (disabled by default)
Sam Lantinga <slouken@libsdl.org>
parents:
297
diff
changeset
|
167 if ( ! cpu_mhz1000 ) { |
d85fc19bf840
Added UNIX RDTSC code by Lompak (disabled by default)
Sam Lantinga <slouken@libsdl.org>
parents:
297
diff
changeset
|
168 return 0; /* Shouldn't happen. BUG!! */ |
d85fc19bf840
Added UNIX RDTSC code by Lompak (disabled by default)
Sam Lantinga <slouken@libsdl.org>
parents:
297
diff
changeset
|
169 } |
d85fc19bf840
Added UNIX RDTSC code by Lompak (disabled by default)
Sam Lantinga <slouken@libsdl.org>
parents:
297
diff
changeset
|
170 rdtsc(now); |
d85fc19bf840
Added UNIX RDTSC code by Lompak (disabled by default)
Sam Lantinga <slouken@libsdl.org>
parents:
297
diff
changeset
|
171 return (Uint32)((now-start)/cpu_mhz1000); |
1157
baf35853ad54
Date: Mon, 10 Oct 2005 13:09:32 +0300
Ryan C. Gordon <icculus@icculus.org>
parents:
1156
diff
changeset
|
172 #elif defined(USE_CLOCK_GETTIME) |
baf35853ad54
Date: Mon, 10 Oct 2005 13:09:32 +0300
Ryan C. Gordon <icculus@icculus.org>
parents:
1156
diff
changeset
|
173 Uint32 ticks; |
baf35853ad54
Date: Mon, 10 Oct 2005 13:09:32 +0300
Ryan C. Gordon <icculus@icculus.org>
parents:
1156
diff
changeset
|
174 struct timespec now; |
baf35853ad54
Date: Mon, 10 Oct 2005 13:09:32 +0300
Ryan C. Gordon <icculus@icculus.org>
parents:
1156
diff
changeset
|
175 clock_gettime(CLOCK_MONOTONIC,&now); |
baf35853ad54
Date: Mon, 10 Oct 2005 13:09:32 +0300
Ryan C. Gordon <icculus@icculus.org>
parents:
1156
diff
changeset
|
176 ticks=(now.tv_sec-start.tv_sec)*1000+(now.tv_nsec-start.tv_nsec)/1000000; |
baf35853ad54
Date: Mon, 10 Oct 2005 13:09:32 +0300
Ryan C. Gordon <icculus@icculus.org>
parents:
1156
diff
changeset
|
177 return(ticks); |
316
d85fc19bf840
Added UNIX RDTSC code by Lompak (disabled by default)
Sam Lantinga <slouken@libsdl.org>
parents:
297
diff
changeset
|
178 #else |
1157
baf35853ad54
Date: Mon, 10 Oct 2005 13:09:32 +0300
Ryan C. Gordon <icculus@icculus.org>
parents:
1156
diff
changeset
|
179 Uint32 ticks; |
0 | 180 struct timeval now; |
181 gettimeofday(&now, NULL); | |
182 ticks=(now.tv_sec-start.tv_sec)*1000+(now.tv_usec-start.tv_usec)/1000; | |
183 return(ticks); | |
1157
baf35853ad54
Date: Mon, 10 Oct 2005 13:09:32 +0300
Ryan C. Gordon <icculus@icculus.org>
parents:
1156
diff
changeset
|
184 #endif |
0 | 185 } |
186 | |
187 void SDL_Delay (Uint32 ms) | |
188 { | |
415
104f32d04cd1
Fixed building with pthreads and the pth library
Sam Lantinga <slouken@libsdl.org>
parents:
392
diff
changeset
|
189 #ifdef ENABLE_PTH |
104f32d04cd1
Fixed building with pthreads and the pth library
Sam Lantinga <slouken@libsdl.org>
parents:
392
diff
changeset
|
190 pth_time_t tv; |
104f32d04cd1
Fixed building with pthreads and the pth library
Sam Lantinga <slouken@libsdl.org>
parents:
392
diff
changeset
|
191 tv.tv_sec = ms/1000; |
104f32d04cd1
Fixed building with pthreads and the pth library
Sam Lantinga <slouken@libsdl.org>
parents:
392
diff
changeset
|
192 tv.tv_usec = (ms%1000)*1000; |
104f32d04cd1
Fixed building with pthreads and the pth library
Sam Lantinga <slouken@libsdl.org>
parents:
392
diff
changeset
|
193 pth_nap(tv); |
104f32d04cd1
Fixed building with pthreads and the pth library
Sam Lantinga <slouken@libsdl.org>
parents:
392
diff
changeset
|
194 #else |
0 | 195 int was_error; |
196 | |
197 #ifdef USE_NANOSLEEP | |
198 struct timespec elapsed, tv; | |
199 #else | |
200 struct timeval tv; | |
201 #ifndef SELECT_SETS_REMAINING | |
202 Uint32 then, now, elapsed; | |
203 #endif | |
204 #endif | |
205 | |
206 /* Set the timeout interval - Linux only needs to do this once */ | |
207 #ifdef SELECT_SETS_REMAINING | |
208 tv.tv_sec = ms/1000; | |
209 tv.tv_usec = (ms%1000)*1000; | |
210 #elif defined(USE_NANOSLEEP) | |
211 elapsed.tv_sec = ms/1000; | |
212 elapsed.tv_nsec = (ms%1000)*1000000; | |
213 #else | |
214 then = SDL_GetTicks(); | |
215 #endif | |
216 do { | |
217 errno = 0; | |
218 | |
1
cf2af46e9e2a
Changes since SDL 1.2.0 release
Sam Lantinga <slouken@lokigames.com>
parents:
0
diff
changeset
|
219 #if _POSIX_THREAD_SYSCALL_SOFT |
cf2af46e9e2a
Changes since SDL 1.2.0 release
Sam Lantinga <slouken@lokigames.com>
parents:
0
diff
changeset
|
220 pthread_yield_np(); |
cf2af46e9e2a
Changes since SDL 1.2.0 release
Sam Lantinga <slouken@lokigames.com>
parents:
0
diff
changeset
|
221 #endif |
0 | 222 #ifdef USE_NANOSLEEP |
223 tv.tv_sec = elapsed.tv_sec; | |
224 tv.tv_nsec = elapsed.tv_nsec; | |
225 was_error = nanosleep(&tv, &elapsed); | |
226 #else | |
227 #ifndef SELECT_SETS_REMAINING | |
228 /* Calculate the time interval left (in case of interrupt) */ | |
229 now = SDL_GetTicks(); | |
230 elapsed = (now-then); | |
231 then = now; | |
232 if ( elapsed >= ms ) { | |
233 break; | |
234 } | |
235 ms -= elapsed; | |
236 tv.tv_sec = ms/1000; | |
237 tv.tv_usec = (ms%1000)*1000; | |
238 #endif | |
239 was_error = select(0, NULL, NULL, NULL, &tv); | |
240 #endif /* USE_NANOSLEEP */ | |
241 } while ( was_error && (errno == EINTR) ); | |
415
104f32d04cd1
Fixed building with pthreads and the pth library
Sam Lantinga <slouken@libsdl.org>
parents:
392
diff
changeset
|
242 #endif /* ENABLE_PTH */ |
0 | 243 } |
244 | |
245 #ifdef USE_ITIMER | |
246 | |
247 static void HandleAlarm(int sig) | |
248 { | |
249 Uint32 ms; | |
250 | |
251 if ( SDL_alarm_callback ) { | |
252 ms = (*SDL_alarm_callback)(SDL_alarm_interval); | |
253 if ( ms != SDL_alarm_interval ) { | |
254 SDL_SetTimer(ms, SDL_alarm_callback); | |
255 } | |
256 } | |
257 } | |
258 | |
259 int SDL_SYS_TimerInit(void) | |
260 { | |
261 struct sigaction action; | |
262 | |
263 /* Set the alarm handler (Linux specific) */ | |
264 memset(&action, 0, sizeof(action)); | |
265 action.sa_handler = HandleAlarm; | |
266 action.sa_flags = SA_RESTART; | |
267 sigemptyset(&action.sa_mask); | |
268 sigaction(SIGALRM, &action, NULL); | |
269 return(0); | |
270 } | |
271 | |
272 void SDL_SYS_TimerQuit(void) | |
273 { | |
274 SDL_SetTimer(0, NULL); | |
275 } | |
276 | |
277 int SDL_SYS_StartTimer(void) | |
278 { | |
279 struct itimerval timer; | |
280 | |
281 timer.it_value.tv_sec = (SDL_alarm_interval/1000); | |
282 timer.it_value.tv_usec = (SDL_alarm_interval%1000)*1000; | |
283 timer.it_interval.tv_sec = (SDL_alarm_interval/1000); | |
284 timer.it_interval.tv_usec = (SDL_alarm_interval%1000)*1000; | |
285 setitimer(ITIMER_REAL, &timer, NULL); | |
286 return(0); | |
287 } | |
288 | |
289 void SDL_SYS_StopTimer(void) | |
290 { | |
291 struct itimerval timer; | |
292 | |
293 memset(&timer, 0, (sizeof timer)); | |
294 setitimer(ITIMER_REAL, &timer, NULL); | |
295 } | |
296 | |
297 #else /* USE_ITIMER */ | |
298 | |
299 #include "SDL_thread.h" | |
300 | |
301 /* Data to handle a single periodic alarm */ | |
302 static int timer_alive = 0; | |
303 static SDL_Thread *timer = NULL; | |
304 | |
305 static int RunTimer(void *unused) | |
306 { | |
307 while ( timer_alive ) { | |
308 if ( SDL_timer_running ) { | |
309 SDL_ThreadedTimerCheck(); | |
310 } | |
311 SDL_Delay(1); | |
312 } | |
313 return(0); | |
314 } | |
315 | |
316 /* This is only called if the event thread is not running */ | |
317 int SDL_SYS_TimerInit(void) | |
318 { | |
319 timer_alive = 1; | |
320 timer = SDL_CreateThread(RunTimer, NULL); | |
321 if ( timer == NULL ) | |
322 return(-1); | |
323 return(SDL_SetTimerThreaded(1)); | |
324 } | |
325 | |
326 void SDL_SYS_TimerQuit(void) | |
327 { | |
328 timer_alive = 0; | |
329 if ( timer ) { | |
330 SDL_WaitThread(timer, NULL); | |
331 timer = NULL; | |
332 } | |
333 } | |
334 | |
335 int SDL_SYS_StartTimer(void) | |
336 { | |
337 SDL_SetError("Internal logic error: Linux uses threaded timer"); | |
338 return(-1); | |
339 } | |
340 | |
341 void SDL_SYS_StopTimer(void) | |
342 { | |
343 return; | |
344 } | |
345 | |
346 #endif /* USE_ITIMER */ |