annotate src/timer/linux/SDL_systimer.c @ 1336:3692456e7b0f

Use SDL_ prefixed versions of C library functions. FIXME: Change #include <stdlib.h> to #include "SDL_stdlib.h" Change #include <string.h> to #include "SDL_string.h" Make sure nothing else broke because of this...
author Sam Lantinga <slouken@libsdl.org>
date Tue, 07 Feb 2006 06:59:48 +0000
parents 9439c2f1da89
children c71e05b4dc2e
rev   line source
0
74212992fb08 Initial revision
Sam Lantinga <slouken@lokigames.com>
parents:
diff changeset
1 /*
74212992fb08 Initial revision
Sam Lantinga <slouken@lokigames.com>
parents:
diff changeset
2 SDL - Simple DirectMedia Layer
1312
c9b51268668f Updated copyright information and removed rcs id lines (problematic in branch merges)
Sam Lantinga <slouken@libsdl.org>
parents: 1158
diff changeset
3 Copyright (C) 1997-2006 Sam Lantinga
0
74212992fb08 Initial revision
Sam Lantinga <slouken@lokigames.com>
parents:
diff changeset
4
74212992fb08 Initial revision
Sam Lantinga <slouken@lokigames.com>
parents:
diff changeset
5 This library is free software; you can redistribute it and/or
1312
c9b51268668f Updated copyright information and removed rcs id lines (problematic in branch merges)
Sam Lantinga <slouken@libsdl.org>
parents: 1158
diff changeset
6 modify it under the terms of the GNU Lesser General Public
0
74212992fb08 Initial revision
Sam Lantinga <slouken@lokigames.com>
parents:
diff changeset
7 License as published by the Free Software Foundation; either
1312
c9b51268668f Updated copyright information and removed rcs id lines (problematic in branch merges)
Sam Lantinga <slouken@libsdl.org>
parents: 1158
diff changeset
8 version 2.1 of the License, or (at your option) any later version.
0
74212992fb08 Initial revision
Sam Lantinga <slouken@lokigames.com>
parents:
diff changeset
9
74212992fb08 Initial revision
Sam Lantinga <slouken@lokigames.com>
parents:
diff changeset
10 This library is distributed in the hope that it will be useful,
74212992fb08 Initial revision
Sam Lantinga <slouken@lokigames.com>
parents:
diff changeset
11 but WITHOUT ANY WARRANTY; without even the implied warranty of
74212992fb08 Initial revision
Sam Lantinga <slouken@lokigames.com>
parents:
diff changeset
12 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
1312
c9b51268668f Updated copyright information and removed rcs id lines (problematic in branch merges)
Sam Lantinga <slouken@libsdl.org>
parents: 1158
diff changeset
13 Lesser General Public License for more details.
0
74212992fb08 Initial revision
Sam Lantinga <slouken@lokigames.com>
parents:
diff changeset
14
1312
c9b51268668f Updated copyright information and removed rcs id lines (problematic in branch merges)
Sam Lantinga <slouken@libsdl.org>
parents: 1158
diff changeset
15 You should have received a copy of the GNU Lesser General Public
c9b51268668f Updated copyright information and removed rcs id lines (problematic in branch merges)
Sam Lantinga <slouken@libsdl.org>
parents: 1158
diff changeset
16 License along with this library; if not, write to the Free Software
c9b51268668f Updated copyright information and removed rcs id lines (problematic in branch merges)
Sam Lantinga <slouken@libsdl.org>
parents: 1158
diff changeset
17 Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
0
74212992fb08 Initial revision
Sam Lantinga <slouken@lokigames.com>
parents:
diff changeset
18
74212992fb08 Initial revision
Sam Lantinga <slouken@lokigames.com>
parents:
diff changeset
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
74212992fb08 Initial revision
Sam Lantinga <slouken@lokigames.com>
parents:
diff changeset
23 */
74212992fb08 Initial revision
Sam Lantinga <slouken@lokigames.com>
parents:
diff changeset
24
74212992fb08 Initial revision
Sam Lantinga <slouken@lokigames.com>
parents:
diff changeset
25 #include <stdio.h>
74212992fb08 Initial revision
Sam Lantinga <slouken@lokigames.com>
parents:
diff changeset
26 #include <sys/time.h>
74212992fb08 Initial revision
Sam Lantinga <slouken@lokigames.com>
parents:
diff changeset
27 #include <signal.h>
74212992fb08 Initial revision
Sam Lantinga <slouken@lokigames.com>
parents:
diff changeset
28 #include <unistd.h>
74212992fb08 Initial revision
Sam Lantinga <slouken@lokigames.com>
parents:
diff changeset
29 #include <string.h>
74212992fb08 Initial revision
Sam Lantinga <slouken@lokigames.com>
parents:
diff changeset
30 #include <errno.h>
74212992fb08 Initial revision
Sam Lantinga <slouken@lokigames.com>
parents:
diff changeset
31
1157
baf35853ad54 Date: Mon, 10 Oct 2005 13:09:32 +0300
Ryan C. Gordon <icculus@icculus.org>
parents: 1156
diff changeset
32 /* 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
33 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
34 for __USE_POSIX199309
baf35853ad54 Date: Mon, 10 Oct 2005 13:09:32 +0300
Ryan C. Gordon <icculus@icculus.org>
parents: 1156
diff changeset
35 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
36 */
1326
9439c2f1da89 From: Hayashi Naoyuki
Sam Lantinga <slouken@libsdl.org>
parents: 1312
diff changeset
37 #if (defined _POSIX_TIMERS && _POSIX_TIMERS + 0 > 0)
1157
baf35853ad54 Date: Mon, 10 Oct 2005 13:09:32 +0300
Ryan C. Gordon <icculus@icculus.org>
parents: 1156
diff changeset
38 #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
39 /*
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
40 * 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
41 * 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
42 * 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
43 * 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
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 /*#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
46 #endif
baf35853ad54 Date: Mon, 10 Oct 2005 13:09:32 +0300
Ryan C. Gordon <icculus@icculus.org>
parents: 1156
diff changeset
47
0
74212992fb08 Initial revision
Sam Lantinga <slouken@lokigames.com>
parents:
diff changeset
48 #include "SDL_error.h"
74212992fb08 Initial revision
Sam Lantinga <slouken@lokigames.com>
parents:
diff changeset
49 #include "SDL_timer.h"
74212992fb08 Initial revision
Sam Lantinga <slouken@lokigames.com>
parents:
diff changeset
50 #include "SDL_timer_c.h"
74212992fb08 Initial revision
Sam Lantinga <slouken@lokigames.com>
parents:
diff changeset
51
1
cf2af46e9e2a Changes since SDL 1.2.0 release
Sam Lantinga <slouken@lokigames.com>
parents: 0
diff changeset
52 #if _POSIX_THREAD_SYSCALL_SOFT
cf2af46e9e2a Changes since SDL 1.2.0 release
Sam Lantinga <slouken@lokigames.com>
parents: 0
diff changeset
53 #include <pthread.h>
cf2af46e9e2a Changes since SDL 1.2.0 release
Sam Lantinga <slouken@lokigames.com>
parents: 0
diff changeset
54 #endif
415
104f32d04cd1 Fixed building with pthreads and the pth library
Sam Lantinga <slouken@libsdl.org>
parents: 392
diff changeset
55 #ifdef ENABLE_PTH
104f32d04cd1 Fixed building with pthreads and the pth library
Sam Lantinga <slouken@libsdl.org>
parents: 392
diff changeset
56 #include <pth.h>
104f32d04cd1 Fixed building with pthreads and the pth library
Sam Lantinga <slouken@libsdl.org>
parents: 392
diff changeset
57 #endif
1
cf2af46e9e2a Changes since SDL 1.2.0 release
Sam Lantinga <slouken@lokigames.com>
parents: 0
diff changeset
58
0
74212992fb08 Initial revision
Sam Lantinga <slouken@lokigames.com>
parents:
diff changeset
59 #if defined(DISABLE_THREADS) || defined(FORK_HACK)
74212992fb08 Initial revision
Sam Lantinga <slouken@lokigames.com>
parents:
diff changeset
60 #define USE_ITIMER
74212992fb08 Initial revision
Sam Lantinga <slouken@lokigames.com>
parents:
diff changeset
61 #endif
74212992fb08 Initial revision
Sam Lantinga <slouken@lokigames.com>
parents:
diff changeset
62
74212992fb08 Initial revision
Sam Lantinga <slouken@lokigames.com>
parents:
diff changeset
63 /* The following defines should really be determined at configure time */
74212992fb08 Initial revision
Sam Lantinga <slouken@lokigames.com>
parents:
diff changeset
64
166
39877400bd1e Fixed Solaris nitpicks (thanks Mattias!)
Sam Lantinga <slouken@libsdl.org>
parents: 1
diff changeset
65 #if defined(linux)
0
74212992fb08 Initial revision
Sam Lantinga <slouken@lokigames.com>
parents:
diff changeset
66 /* Linux select() changes its timeout parameter upon return to contain
74212992fb08 Initial revision
Sam Lantinga <slouken@lokigames.com>
parents:
diff changeset
67 the remaining time. Most other unixen leave it unchanged or undefined. */
74212992fb08 Initial revision
Sam Lantinga <slouken@lokigames.com>
parents:
diff changeset
68 #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
69 #elif defined(__bsdi__) || defined(__FreeBSD__) || defined(__sun) || defined(MACOSX)
0
74212992fb08 Initial revision
Sam Lantinga <slouken@lokigames.com>
parents:
diff changeset
70 #define USE_NANOSLEEP
74212992fb08 Initial revision
Sam Lantinga <slouken@lokigames.com>
parents:
diff changeset
71 #endif
74212992fb08 Initial revision
Sam Lantinga <slouken@lokigames.com>
parents:
diff changeset
72
316
d85fc19bf840 Added UNIX RDTSC code by Lompak (disabled by default)
Sam Lantinga <slouken@libsdl.org>
parents: 297
diff changeset
73 #if defined(i386) || defined(__i386__)
317
4e8827521296 *** empty log message ***
Sam Lantinga <slouken@libsdl.org>
parents: 316
diff changeset
74 /* 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
75 /* 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
76 /*#define USE_RDTSC*/
d85fc19bf840 Added UNIX RDTSC code by Lompak (disabled by default)
Sam Lantinga <slouken@libsdl.org>
parents: 297
diff changeset
77 #endif
d85fc19bf840 Added UNIX RDTSC code by Lompak (disabled by default)
Sam Lantinga <slouken@libsdl.org>
parents: 297
diff changeset
78
d85fc19bf840 Added UNIX RDTSC code by Lompak (disabled by default)
Sam Lantinga <slouken@libsdl.org>
parents: 297
diff changeset
79
d85fc19bf840 Added UNIX RDTSC code by Lompak (disabled by default)
Sam Lantinga <slouken@libsdl.org>
parents: 297
diff changeset
80 #ifdef USE_RDTSC
d85fc19bf840 Added UNIX RDTSC code by Lompak (disabled by default)
Sam Lantinga <slouken@libsdl.org>
parents: 297
diff changeset
81
d85fc19bf840 Added UNIX RDTSC code by Lompak (disabled by default)
Sam Lantinga <slouken@libsdl.org>
parents: 297
diff changeset
82 /* 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
83 static unsigned long long start;
d85fc19bf840 Added UNIX RDTSC code by Lompak (disabled by default)
Sam Lantinga <slouken@libsdl.org>
parents: 297
diff changeset
84 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
85
d85fc19bf840 Added UNIX RDTSC code by Lompak (disabled by default)
Sam Lantinga <slouken@libsdl.org>
parents: 297
diff changeset
86 #if 1
d85fc19bf840 Added UNIX RDTSC code by Lompak (disabled by default)
Sam Lantinga <slouken@libsdl.org>
parents: 297
diff changeset
87 /* 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
88 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
89 */
392
71fe0b713085 *** empty log message ***
Sam Lantinga <slouken@libsdl.org>
parents: 317
diff changeset
90 #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
91 #else
392
71fe0b713085 *** empty log message ***
Sam Lantinga <slouken@libsdl.org>
parents: 317
diff changeset
92 #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
93 #endif
d85fc19bf840 Added UNIX RDTSC code by Lompak (disabled by default)
Sam Lantinga <slouken@libsdl.org>
parents: 297
diff changeset
94
d85fc19bf840 Added UNIX RDTSC code by Lompak (disabled by default)
Sam Lantinga <slouken@libsdl.org>
parents: 297
diff changeset
95 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
96 {
d85fc19bf840 Added UNIX RDTSC code by Lompak (disabled by default)
Sam Lantinga <slouken@libsdl.org>
parents: 297
diff changeset
97 float cpu_mhz;
d85fc19bf840 Added UNIX RDTSC code by Lompak (disabled by default)
Sam Lantinga <slouken@libsdl.org>
parents: 297
diff changeset
98 unsigned long long tsc_start;
d85fc19bf840 Added UNIX RDTSC code by Lompak (disabled by default)
Sam Lantinga <slouken@libsdl.org>
parents: 297
diff changeset
99 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
100 /* 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
101 #ifdef USE_CLOCK_GETTIME
baf35853ad54 Date: Mon, 10 Oct 2005 13:09:32 +0300
Ryan C. Gordon <icculus@icculus.org>
parents: 1156
diff changeset
102 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
103 long usec_delay;
baf35853ad54 Date: Mon, 10 Oct 2005 13:09:32 +0300
Ryan C. Gordon <icculus@icculus.org>
parents: 1156
diff changeset
104
baf35853ad54 Date: Mon, 10 Oct 2005 13:09:32 +0300
Ryan C. Gordon <icculus@icculus.org>
parents: 1156
diff changeset
105 rdtsc(tsc_start);
baf35853ad54 Date: Mon, 10 Oct 2005 13:09:32 +0300
Ryan C. Gordon <icculus@icculus.org>
parents: 1156
diff changeset
106 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
107 sleep(1);
baf35853ad54 Date: Mon, 10 Oct 2005 13:09:32 +0300
Ryan C. Gordon <icculus@icculus.org>
parents: 1156
diff changeset
108 rdtsc(tsc_end);
baf35853ad54 Date: Mon, 10 Oct 2005 13:09:32 +0300
Ryan C. Gordon <icculus@icculus.org>
parents: 1156
diff changeset
109 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
110 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
111 (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
112 #else
316
d85fc19bf840 Added UNIX RDTSC code by Lompak (disabled by default)
Sam Lantinga <slouken@libsdl.org>
parents: 297
diff changeset
113 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
114 long usec_delay;
d85fc19bf840 Added UNIX RDTSC code by Lompak (disabled by default)
Sam Lantinga <slouken@libsdl.org>
parents: 297
diff changeset
115
d85fc19bf840 Added UNIX RDTSC code by Lompak (disabled by default)
Sam Lantinga <slouken@libsdl.org>
parents: 297
diff changeset
116 rdtsc(tsc_start);
d85fc19bf840 Added UNIX RDTSC code by Lompak (disabled by default)
Sam Lantinga <slouken@libsdl.org>
parents: 297
diff changeset
117 gettimeofday(&tv_start, NULL);
d85fc19bf840 Added UNIX RDTSC code by Lompak (disabled by default)
Sam Lantinga <slouken@libsdl.org>
parents: 297
diff changeset
118 sleep(1);
d85fc19bf840 Added UNIX RDTSC code by Lompak (disabled by default)
Sam Lantinga <slouken@libsdl.org>
parents: 297
diff changeset
119 rdtsc(tsc_end);
d85fc19bf840 Added UNIX RDTSC code by Lompak (disabled by default)
Sam Lantinga <slouken@libsdl.org>
parents: 297
diff changeset
120 gettimeofday(&tv_end, NULL);
d85fc19bf840 Added UNIX RDTSC code by Lompak (disabled by default)
Sam Lantinga <slouken@libsdl.org>
parents: 297
diff changeset
121 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
122 (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
123 #endif /* USE_CLOCK_GETTIME */
316
d85fc19bf840 Added UNIX RDTSC code by Lompak (disabled by default)
Sam Lantinga <slouken@libsdl.org>
parents: 297
diff changeset
124 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
125 #if 0
d85fc19bf840 Added UNIX RDTSC code by Lompak (disabled by default)
Sam Lantinga <slouken@libsdl.org>
parents: 297
diff changeset
126 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
127 #endif
d85fc19bf840 Added UNIX RDTSC code by Lompak (disabled by default)
Sam Lantinga <slouken@libsdl.org>
parents: 297
diff changeset
128 return cpu_mhz;
d85fc19bf840 Added UNIX RDTSC code by Lompak (disabled by default)
Sam Lantinga <slouken@libsdl.org>
parents: 297
diff changeset
129 }
d85fc19bf840 Added UNIX RDTSC code by Lompak (disabled by default)
Sam Lantinga <slouken@libsdl.org>
parents: 297
diff changeset
130
d85fc19bf840 Added UNIX RDTSC code by Lompak (disabled by default)
Sam Lantinga <slouken@libsdl.org>
parents: 297
diff changeset
131 #else
0
74212992fb08 Initial revision
Sam Lantinga <slouken@lokigames.com>
parents:
diff changeset
132
74212992fb08 Initial revision
Sam Lantinga <slouken@lokigames.com>
parents:
diff changeset
133 /* 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
134 #ifdef USE_CLOCK_GETTIME
baf35853ad54 Date: Mon, 10 Oct 2005 13:09:32 +0300
Ryan C. Gordon <icculus@icculus.org>
parents: 1156
diff changeset
135 static struct timespec start;
baf35853ad54 Date: Mon, 10 Oct 2005 13:09:32 +0300
Ryan C. Gordon <icculus@icculus.org>
parents: 1156
diff changeset
136 #else
0
74212992fb08 Initial revision
Sam Lantinga <slouken@lokigames.com>
parents:
diff changeset
137 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
138 #endif /* USE_CLOCK_GETTIME */
0
74212992fb08 Initial revision
Sam Lantinga <slouken@lokigames.com>
parents:
diff changeset
139
316
d85fc19bf840 Added UNIX RDTSC code by Lompak (disabled by default)
Sam Lantinga <slouken@libsdl.org>
parents: 297
diff changeset
140 #endif /* USE_RDTSC */
d85fc19bf840 Added UNIX RDTSC code by Lompak (disabled by default)
Sam Lantinga <slouken@libsdl.org>
parents: 297
diff changeset
141
d85fc19bf840 Added UNIX RDTSC code by Lompak (disabled by default)
Sam Lantinga <slouken@libsdl.org>
parents: 297
diff changeset
142
0
74212992fb08 Initial revision
Sam Lantinga <slouken@lokigames.com>
parents:
diff changeset
143 void SDL_StartTicks(void)
74212992fb08 Initial revision
Sam Lantinga <slouken@lokigames.com>
parents:
diff changeset
144 {
74212992fb08 Initial revision
Sam Lantinga <slouken@lokigames.com>
parents:
diff changeset
145 /* Set first ticks value */
316
d85fc19bf840 Added UNIX RDTSC code by Lompak (disabled by default)
Sam Lantinga <slouken@libsdl.org>
parents: 297
diff changeset
146 #ifdef USE_RDTSC
d85fc19bf840 Added UNIX RDTSC code by Lompak (disabled by default)
Sam Lantinga <slouken@libsdl.org>
parents: 297
diff changeset
147 if ( ! cpu_mhz1000 ) {
d85fc19bf840 Added UNIX RDTSC code by Lompak (disabled by default)
Sam Lantinga <slouken@libsdl.org>
parents: 297
diff changeset
148 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
149 }
d85fc19bf840 Added UNIX RDTSC code by Lompak (disabled by default)
Sam Lantinga <slouken@libsdl.org>
parents: 297
diff changeset
150 rdtsc(start);
1157
baf35853ad54 Date: Mon, 10 Oct 2005 13:09:32 +0300
Ryan C. Gordon <icculus@icculus.org>
parents: 1156
diff changeset
151 #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
152 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
153 #else
0
74212992fb08 Initial revision
Sam Lantinga <slouken@lokigames.com>
parents:
diff changeset
154 gettimeofday(&start, NULL);
1157
baf35853ad54 Date: Mon, 10 Oct 2005 13:09:32 +0300
Ryan C. Gordon <icculus@icculus.org>
parents: 1156
diff changeset
155 #endif
0
74212992fb08 Initial revision
Sam Lantinga <slouken@lokigames.com>
parents:
diff changeset
156 }
74212992fb08 Initial revision
Sam Lantinga <slouken@lokigames.com>
parents:
diff changeset
157
74212992fb08 Initial revision
Sam Lantinga <slouken@lokigames.com>
parents:
diff changeset
158 Uint32 SDL_GetTicks (void)
74212992fb08 Initial revision
Sam Lantinga <slouken@lokigames.com>
parents:
diff changeset
159 {
316
d85fc19bf840 Added UNIX RDTSC code by Lompak (disabled by default)
Sam Lantinga <slouken@libsdl.org>
parents: 297
diff changeset
160 #ifdef USE_RDTSC
d85fc19bf840 Added UNIX RDTSC code by Lompak (disabled by default)
Sam Lantinga <slouken@libsdl.org>
parents: 297
diff changeset
161 unsigned long long now;
d85fc19bf840 Added UNIX RDTSC code by Lompak (disabled by default)
Sam Lantinga <slouken@libsdl.org>
parents: 297
diff changeset
162 if ( ! cpu_mhz1000 ) {
d85fc19bf840 Added UNIX RDTSC code by Lompak (disabled by default)
Sam Lantinga <slouken@libsdl.org>
parents: 297
diff changeset
163 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
164 }
d85fc19bf840 Added UNIX RDTSC code by Lompak (disabled by default)
Sam Lantinga <slouken@libsdl.org>
parents: 297
diff changeset
165 rdtsc(now);
d85fc19bf840 Added UNIX RDTSC code by Lompak (disabled by default)
Sam Lantinga <slouken@libsdl.org>
parents: 297
diff changeset
166 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
167 #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
168 Uint32 ticks;
baf35853ad54 Date: Mon, 10 Oct 2005 13:09:32 +0300
Ryan C. Gordon <icculus@icculus.org>
parents: 1156
diff changeset
169 struct timespec now;
baf35853ad54 Date: Mon, 10 Oct 2005 13:09:32 +0300
Ryan C. Gordon <icculus@icculus.org>
parents: 1156
diff changeset
170 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
171 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
172 return(ticks);
316
d85fc19bf840 Added UNIX RDTSC code by Lompak (disabled by default)
Sam Lantinga <slouken@libsdl.org>
parents: 297
diff changeset
173 #else
1157
baf35853ad54 Date: Mon, 10 Oct 2005 13:09:32 +0300
Ryan C. Gordon <icculus@icculus.org>
parents: 1156
diff changeset
174 Uint32 ticks;
0
74212992fb08 Initial revision
Sam Lantinga <slouken@lokigames.com>
parents:
diff changeset
175 struct timeval now;
74212992fb08 Initial revision
Sam Lantinga <slouken@lokigames.com>
parents:
diff changeset
176 gettimeofday(&now, NULL);
74212992fb08 Initial revision
Sam Lantinga <slouken@lokigames.com>
parents:
diff changeset
177 ticks=(now.tv_sec-start.tv_sec)*1000+(now.tv_usec-start.tv_usec)/1000;
74212992fb08 Initial revision
Sam Lantinga <slouken@lokigames.com>
parents:
diff changeset
178 return(ticks);
1157
baf35853ad54 Date: Mon, 10 Oct 2005 13:09:32 +0300
Ryan C. Gordon <icculus@icculus.org>
parents: 1156
diff changeset
179 #endif
0
74212992fb08 Initial revision
Sam Lantinga <slouken@lokigames.com>
parents:
diff changeset
180 }
74212992fb08 Initial revision
Sam Lantinga <slouken@lokigames.com>
parents:
diff changeset
181
74212992fb08 Initial revision
Sam Lantinga <slouken@lokigames.com>
parents:
diff changeset
182 void SDL_Delay (Uint32 ms)
74212992fb08 Initial revision
Sam Lantinga <slouken@lokigames.com>
parents:
diff changeset
183 {
415
104f32d04cd1 Fixed building with pthreads and the pth library
Sam Lantinga <slouken@libsdl.org>
parents: 392
diff changeset
184 #ifdef ENABLE_PTH
104f32d04cd1 Fixed building with pthreads and the pth library
Sam Lantinga <slouken@libsdl.org>
parents: 392
diff changeset
185 pth_time_t tv;
104f32d04cd1 Fixed building with pthreads and the pth library
Sam Lantinga <slouken@libsdl.org>
parents: 392
diff changeset
186 tv.tv_sec = ms/1000;
104f32d04cd1 Fixed building with pthreads and the pth library
Sam Lantinga <slouken@libsdl.org>
parents: 392
diff changeset
187 tv.tv_usec = (ms%1000)*1000;
104f32d04cd1 Fixed building with pthreads and the pth library
Sam Lantinga <slouken@libsdl.org>
parents: 392
diff changeset
188 pth_nap(tv);
104f32d04cd1 Fixed building with pthreads and the pth library
Sam Lantinga <slouken@libsdl.org>
parents: 392
diff changeset
189 #else
0
74212992fb08 Initial revision
Sam Lantinga <slouken@lokigames.com>
parents:
diff changeset
190 int was_error;
74212992fb08 Initial revision
Sam Lantinga <slouken@lokigames.com>
parents:
diff changeset
191
74212992fb08 Initial revision
Sam Lantinga <slouken@lokigames.com>
parents:
diff changeset
192 #ifdef USE_NANOSLEEP
74212992fb08 Initial revision
Sam Lantinga <slouken@lokigames.com>
parents:
diff changeset
193 struct timespec elapsed, tv;
74212992fb08 Initial revision
Sam Lantinga <slouken@lokigames.com>
parents:
diff changeset
194 #else
74212992fb08 Initial revision
Sam Lantinga <slouken@lokigames.com>
parents:
diff changeset
195 struct timeval tv;
74212992fb08 Initial revision
Sam Lantinga <slouken@lokigames.com>
parents:
diff changeset
196 #ifndef SELECT_SETS_REMAINING
74212992fb08 Initial revision
Sam Lantinga <slouken@lokigames.com>
parents:
diff changeset
197 Uint32 then, now, elapsed;
74212992fb08 Initial revision
Sam Lantinga <slouken@lokigames.com>
parents:
diff changeset
198 #endif
74212992fb08 Initial revision
Sam Lantinga <slouken@lokigames.com>
parents:
diff changeset
199 #endif
74212992fb08 Initial revision
Sam Lantinga <slouken@lokigames.com>
parents:
diff changeset
200
74212992fb08 Initial revision
Sam Lantinga <slouken@lokigames.com>
parents:
diff changeset
201 /* Set the timeout interval - Linux only needs to do this once */
74212992fb08 Initial revision
Sam Lantinga <slouken@lokigames.com>
parents:
diff changeset
202 #ifdef SELECT_SETS_REMAINING
74212992fb08 Initial revision
Sam Lantinga <slouken@lokigames.com>
parents:
diff changeset
203 tv.tv_sec = ms/1000;
74212992fb08 Initial revision
Sam Lantinga <slouken@lokigames.com>
parents:
diff changeset
204 tv.tv_usec = (ms%1000)*1000;
74212992fb08 Initial revision
Sam Lantinga <slouken@lokigames.com>
parents:
diff changeset
205 #elif defined(USE_NANOSLEEP)
74212992fb08 Initial revision
Sam Lantinga <slouken@lokigames.com>
parents:
diff changeset
206 elapsed.tv_sec = ms/1000;
74212992fb08 Initial revision
Sam Lantinga <slouken@lokigames.com>
parents:
diff changeset
207 elapsed.tv_nsec = (ms%1000)*1000000;
74212992fb08 Initial revision
Sam Lantinga <slouken@lokigames.com>
parents:
diff changeset
208 #else
74212992fb08 Initial revision
Sam Lantinga <slouken@lokigames.com>
parents:
diff changeset
209 then = SDL_GetTicks();
74212992fb08 Initial revision
Sam Lantinga <slouken@lokigames.com>
parents:
diff changeset
210 #endif
74212992fb08 Initial revision
Sam Lantinga <slouken@lokigames.com>
parents:
diff changeset
211 do {
74212992fb08 Initial revision
Sam Lantinga <slouken@lokigames.com>
parents:
diff changeset
212 errno = 0;
74212992fb08 Initial revision
Sam Lantinga <slouken@lokigames.com>
parents:
diff changeset
213
1
cf2af46e9e2a Changes since SDL 1.2.0 release
Sam Lantinga <slouken@lokigames.com>
parents: 0
diff changeset
214 #if _POSIX_THREAD_SYSCALL_SOFT
cf2af46e9e2a Changes since SDL 1.2.0 release
Sam Lantinga <slouken@lokigames.com>
parents: 0
diff changeset
215 pthread_yield_np();
cf2af46e9e2a Changes since SDL 1.2.0 release
Sam Lantinga <slouken@lokigames.com>
parents: 0
diff changeset
216 #endif
0
74212992fb08 Initial revision
Sam Lantinga <slouken@lokigames.com>
parents:
diff changeset
217 #ifdef USE_NANOSLEEP
74212992fb08 Initial revision
Sam Lantinga <slouken@lokigames.com>
parents:
diff changeset
218 tv.tv_sec = elapsed.tv_sec;
74212992fb08 Initial revision
Sam Lantinga <slouken@lokigames.com>
parents:
diff changeset
219 tv.tv_nsec = elapsed.tv_nsec;
74212992fb08 Initial revision
Sam Lantinga <slouken@lokigames.com>
parents:
diff changeset
220 was_error = nanosleep(&tv, &elapsed);
74212992fb08 Initial revision
Sam Lantinga <slouken@lokigames.com>
parents:
diff changeset
221 #else
74212992fb08 Initial revision
Sam Lantinga <slouken@lokigames.com>
parents:
diff changeset
222 #ifndef SELECT_SETS_REMAINING
74212992fb08 Initial revision
Sam Lantinga <slouken@lokigames.com>
parents:
diff changeset
223 /* Calculate the time interval left (in case of interrupt) */
74212992fb08 Initial revision
Sam Lantinga <slouken@lokigames.com>
parents:
diff changeset
224 now = SDL_GetTicks();
74212992fb08 Initial revision
Sam Lantinga <slouken@lokigames.com>
parents:
diff changeset
225 elapsed = (now-then);
74212992fb08 Initial revision
Sam Lantinga <slouken@lokigames.com>
parents:
diff changeset
226 then = now;
74212992fb08 Initial revision
Sam Lantinga <slouken@lokigames.com>
parents:
diff changeset
227 if ( elapsed >= ms ) {
74212992fb08 Initial revision
Sam Lantinga <slouken@lokigames.com>
parents:
diff changeset
228 break;
74212992fb08 Initial revision
Sam Lantinga <slouken@lokigames.com>
parents:
diff changeset
229 }
74212992fb08 Initial revision
Sam Lantinga <slouken@lokigames.com>
parents:
diff changeset
230 ms -= elapsed;
74212992fb08 Initial revision
Sam Lantinga <slouken@lokigames.com>
parents:
diff changeset
231 tv.tv_sec = ms/1000;
74212992fb08 Initial revision
Sam Lantinga <slouken@lokigames.com>
parents:
diff changeset
232 tv.tv_usec = (ms%1000)*1000;
74212992fb08 Initial revision
Sam Lantinga <slouken@lokigames.com>
parents:
diff changeset
233 #endif
74212992fb08 Initial revision
Sam Lantinga <slouken@lokigames.com>
parents:
diff changeset
234 was_error = select(0, NULL, NULL, NULL, &tv);
74212992fb08 Initial revision
Sam Lantinga <slouken@lokigames.com>
parents:
diff changeset
235 #endif /* USE_NANOSLEEP */
74212992fb08 Initial revision
Sam Lantinga <slouken@lokigames.com>
parents:
diff changeset
236 } while ( was_error && (errno == EINTR) );
415
104f32d04cd1 Fixed building with pthreads and the pth library
Sam Lantinga <slouken@libsdl.org>
parents: 392
diff changeset
237 #endif /* ENABLE_PTH */
0
74212992fb08 Initial revision
Sam Lantinga <slouken@lokigames.com>
parents:
diff changeset
238 }
74212992fb08 Initial revision
Sam Lantinga <slouken@lokigames.com>
parents:
diff changeset
239
74212992fb08 Initial revision
Sam Lantinga <slouken@lokigames.com>
parents:
diff changeset
240 #ifdef USE_ITIMER
74212992fb08 Initial revision
Sam Lantinga <slouken@lokigames.com>
parents:
diff changeset
241
74212992fb08 Initial revision
Sam Lantinga <slouken@lokigames.com>
parents:
diff changeset
242 static void HandleAlarm(int sig)
74212992fb08 Initial revision
Sam Lantinga <slouken@lokigames.com>
parents:
diff changeset
243 {
74212992fb08 Initial revision
Sam Lantinga <slouken@lokigames.com>
parents:
diff changeset
244 Uint32 ms;
74212992fb08 Initial revision
Sam Lantinga <slouken@lokigames.com>
parents:
diff changeset
245
74212992fb08 Initial revision
Sam Lantinga <slouken@lokigames.com>
parents:
diff changeset
246 if ( SDL_alarm_callback ) {
74212992fb08 Initial revision
Sam Lantinga <slouken@lokigames.com>
parents:
diff changeset
247 ms = (*SDL_alarm_callback)(SDL_alarm_interval);
74212992fb08 Initial revision
Sam Lantinga <slouken@lokigames.com>
parents:
diff changeset
248 if ( ms != SDL_alarm_interval ) {
74212992fb08 Initial revision
Sam Lantinga <slouken@lokigames.com>
parents:
diff changeset
249 SDL_SetTimer(ms, SDL_alarm_callback);
74212992fb08 Initial revision
Sam Lantinga <slouken@lokigames.com>
parents:
diff changeset
250 }
74212992fb08 Initial revision
Sam Lantinga <slouken@lokigames.com>
parents:
diff changeset
251 }
74212992fb08 Initial revision
Sam Lantinga <slouken@lokigames.com>
parents:
diff changeset
252 }
74212992fb08 Initial revision
Sam Lantinga <slouken@lokigames.com>
parents:
diff changeset
253
74212992fb08 Initial revision
Sam Lantinga <slouken@lokigames.com>
parents:
diff changeset
254 int SDL_SYS_TimerInit(void)
74212992fb08 Initial revision
Sam Lantinga <slouken@lokigames.com>
parents:
diff changeset
255 {
74212992fb08 Initial revision
Sam Lantinga <slouken@lokigames.com>
parents:
diff changeset
256 struct sigaction action;
74212992fb08 Initial revision
Sam Lantinga <slouken@lokigames.com>
parents:
diff changeset
257
74212992fb08 Initial revision
Sam Lantinga <slouken@lokigames.com>
parents:
diff changeset
258 /* Set the alarm handler (Linux specific) */
1336
3692456e7b0f Use SDL_ prefixed versions of C library functions.
Sam Lantinga <slouken@libsdl.org>
parents: 1326
diff changeset
259 SDL_memset(&action, 0, sizeof(action));
0
74212992fb08 Initial revision
Sam Lantinga <slouken@lokigames.com>
parents:
diff changeset
260 action.sa_handler = HandleAlarm;
74212992fb08 Initial revision
Sam Lantinga <slouken@lokigames.com>
parents:
diff changeset
261 action.sa_flags = SA_RESTART;
74212992fb08 Initial revision
Sam Lantinga <slouken@lokigames.com>
parents:
diff changeset
262 sigemptyset(&action.sa_mask);
74212992fb08 Initial revision
Sam Lantinga <slouken@lokigames.com>
parents:
diff changeset
263 sigaction(SIGALRM, &action, NULL);
74212992fb08 Initial revision
Sam Lantinga <slouken@lokigames.com>
parents:
diff changeset
264 return(0);
74212992fb08 Initial revision
Sam Lantinga <slouken@lokigames.com>
parents:
diff changeset
265 }
74212992fb08 Initial revision
Sam Lantinga <slouken@lokigames.com>
parents:
diff changeset
266
74212992fb08 Initial revision
Sam Lantinga <slouken@lokigames.com>
parents:
diff changeset
267 void SDL_SYS_TimerQuit(void)
74212992fb08 Initial revision
Sam Lantinga <slouken@lokigames.com>
parents:
diff changeset
268 {
74212992fb08 Initial revision
Sam Lantinga <slouken@lokigames.com>
parents:
diff changeset
269 SDL_SetTimer(0, NULL);
74212992fb08 Initial revision
Sam Lantinga <slouken@lokigames.com>
parents:
diff changeset
270 }
74212992fb08 Initial revision
Sam Lantinga <slouken@lokigames.com>
parents:
diff changeset
271
74212992fb08 Initial revision
Sam Lantinga <slouken@lokigames.com>
parents:
diff changeset
272 int SDL_SYS_StartTimer(void)
74212992fb08 Initial revision
Sam Lantinga <slouken@lokigames.com>
parents:
diff changeset
273 {
74212992fb08 Initial revision
Sam Lantinga <slouken@lokigames.com>
parents:
diff changeset
274 struct itimerval timer;
74212992fb08 Initial revision
Sam Lantinga <slouken@lokigames.com>
parents:
diff changeset
275
74212992fb08 Initial revision
Sam Lantinga <slouken@lokigames.com>
parents:
diff changeset
276 timer.it_value.tv_sec = (SDL_alarm_interval/1000);
74212992fb08 Initial revision
Sam Lantinga <slouken@lokigames.com>
parents:
diff changeset
277 timer.it_value.tv_usec = (SDL_alarm_interval%1000)*1000;
74212992fb08 Initial revision
Sam Lantinga <slouken@lokigames.com>
parents:
diff changeset
278 timer.it_interval.tv_sec = (SDL_alarm_interval/1000);
74212992fb08 Initial revision
Sam Lantinga <slouken@lokigames.com>
parents:
diff changeset
279 timer.it_interval.tv_usec = (SDL_alarm_interval%1000)*1000;
74212992fb08 Initial revision
Sam Lantinga <slouken@lokigames.com>
parents:
diff changeset
280 setitimer(ITIMER_REAL, &timer, NULL);
74212992fb08 Initial revision
Sam Lantinga <slouken@lokigames.com>
parents:
diff changeset
281 return(0);
74212992fb08 Initial revision
Sam Lantinga <slouken@lokigames.com>
parents:
diff changeset
282 }
74212992fb08 Initial revision
Sam Lantinga <slouken@lokigames.com>
parents:
diff changeset
283
74212992fb08 Initial revision
Sam Lantinga <slouken@lokigames.com>
parents:
diff changeset
284 void SDL_SYS_StopTimer(void)
74212992fb08 Initial revision
Sam Lantinga <slouken@lokigames.com>
parents:
diff changeset
285 {
74212992fb08 Initial revision
Sam Lantinga <slouken@lokigames.com>
parents:
diff changeset
286 struct itimerval timer;
74212992fb08 Initial revision
Sam Lantinga <slouken@lokigames.com>
parents:
diff changeset
287
1336
3692456e7b0f Use SDL_ prefixed versions of C library functions.
Sam Lantinga <slouken@libsdl.org>
parents: 1326
diff changeset
288 SDL_memset(&timer, 0, (sizeof timer));
0
74212992fb08 Initial revision
Sam Lantinga <slouken@lokigames.com>
parents:
diff changeset
289 setitimer(ITIMER_REAL, &timer, NULL);
74212992fb08 Initial revision
Sam Lantinga <slouken@lokigames.com>
parents:
diff changeset
290 }
74212992fb08 Initial revision
Sam Lantinga <slouken@lokigames.com>
parents:
diff changeset
291
74212992fb08 Initial revision
Sam Lantinga <slouken@lokigames.com>
parents:
diff changeset
292 #else /* USE_ITIMER */
74212992fb08 Initial revision
Sam Lantinga <slouken@lokigames.com>
parents:
diff changeset
293
74212992fb08 Initial revision
Sam Lantinga <slouken@lokigames.com>
parents:
diff changeset
294 #include "SDL_thread.h"
74212992fb08 Initial revision
Sam Lantinga <slouken@lokigames.com>
parents:
diff changeset
295
74212992fb08 Initial revision
Sam Lantinga <slouken@lokigames.com>
parents:
diff changeset
296 /* Data to handle a single periodic alarm */
74212992fb08 Initial revision
Sam Lantinga <slouken@lokigames.com>
parents:
diff changeset
297 static int timer_alive = 0;
74212992fb08 Initial revision
Sam Lantinga <slouken@lokigames.com>
parents:
diff changeset
298 static SDL_Thread *timer = NULL;
74212992fb08 Initial revision
Sam Lantinga <slouken@lokigames.com>
parents:
diff changeset
299
74212992fb08 Initial revision
Sam Lantinga <slouken@lokigames.com>
parents:
diff changeset
300 static int RunTimer(void *unused)
74212992fb08 Initial revision
Sam Lantinga <slouken@lokigames.com>
parents:
diff changeset
301 {
74212992fb08 Initial revision
Sam Lantinga <slouken@lokigames.com>
parents:
diff changeset
302 while ( timer_alive ) {
74212992fb08 Initial revision
Sam Lantinga <slouken@lokigames.com>
parents:
diff changeset
303 if ( SDL_timer_running ) {
74212992fb08 Initial revision
Sam Lantinga <slouken@lokigames.com>
parents:
diff changeset
304 SDL_ThreadedTimerCheck();
74212992fb08 Initial revision
Sam Lantinga <slouken@lokigames.com>
parents:
diff changeset
305 }
74212992fb08 Initial revision
Sam Lantinga <slouken@lokigames.com>
parents:
diff changeset
306 SDL_Delay(1);
74212992fb08 Initial revision
Sam Lantinga <slouken@lokigames.com>
parents:
diff changeset
307 }
74212992fb08 Initial revision
Sam Lantinga <slouken@lokigames.com>
parents:
diff changeset
308 return(0);
74212992fb08 Initial revision
Sam Lantinga <slouken@lokigames.com>
parents:
diff changeset
309 }
74212992fb08 Initial revision
Sam Lantinga <slouken@lokigames.com>
parents:
diff changeset
310
74212992fb08 Initial revision
Sam Lantinga <slouken@lokigames.com>
parents:
diff changeset
311 /* This is only called if the event thread is not running */
74212992fb08 Initial revision
Sam Lantinga <slouken@lokigames.com>
parents:
diff changeset
312 int SDL_SYS_TimerInit(void)
74212992fb08 Initial revision
Sam Lantinga <slouken@lokigames.com>
parents:
diff changeset
313 {
74212992fb08 Initial revision
Sam Lantinga <slouken@lokigames.com>
parents:
diff changeset
314 timer_alive = 1;
74212992fb08 Initial revision
Sam Lantinga <slouken@lokigames.com>
parents:
diff changeset
315 timer = SDL_CreateThread(RunTimer, NULL);
74212992fb08 Initial revision
Sam Lantinga <slouken@lokigames.com>
parents:
diff changeset
316 if ( timer == NULL )
74212992fb08 Initial revision
Sam Lantinga <slouken@lokigames.com>
parents:
diff changeset
317 return(-1);
74212992fb08 Initial revision
Sam Lantinga <slouken@lokigames.com>
parents:
diff changeset
318 return(SDL_SetTimerThreaded(1));
74212992fb08 Initial revision
Sam Lantinga <slouken@lokigames.com>
parents:
diff changeset
319 }
74212992fb08 Initial revision
Sam Lantinga <slouken@lokigames.com>
parents:
diff changeset
320
74212992fb08 Initial revision
Sam Lantinga <slouken@lokigames.com>
parents:
diff changeset
321 void SDL_SYS_TimerQuit(void)
74212992fb08 Initial revision
Sam Lantinga <slouken@lokigames.com>
parents:
diff changeset
322 {
74212992fb08 Initial revision
Sam Lantinga <slouken@lokigames.com>
parents:
diff changeset
323 timer_alive = 0;
74212992fb08 Initial revision
Sam Lantinga <slouken@lokigames.com>
parents:
diff changeset
324 if ( timer ) {
74212992fb08 Initial revision
Sam Lantinga <slouken@lokigames.com>
parents:
diff changeset
325 SDL_WaitThread(timer, NULL);
74212992fb08 Initial revision
Sam Lantinga <slouken@lokigames.com>
parents:
diff changeset
326 timer = NULL;
74212992fb08 Initial revision
Sam Lantinga <slouken@lokigames.com>
parents:
diff changeset
327 }
74212992fb08 Initial revision
Sam Lantinga <slouken@lokigames.com>
parents:
diff changeset
328 }
74212992fb08 Initial revision
Sam Lantinga <slouken@lokigames.com>
parents:
diff changeset
329
74212992fb08 Initial revision
Sam Lantinga <slouken@lokigames.com>
parents:
diff changeset
330 int SDL_SYS_StartTimer(void)
74212992fb08 Initial revision
Sam Lantinga <slouken@lokigames.com>
parents:
diff changeset
331 {
74212992fb08 Initial revision
Sam Lantinga <slouken@lokigames.com>
parents:
diff changeset
332 SDL_SetError("Internal logic error: Linux uses threaded timer");
74212992fb08 Initial revision
Sam Lantinga <slouken@lokigames.com>
parents:
diff changeset
333 return(-1);
74212992fb08 Initial revision
Sam Lantinga <slouken@lokigames.com>
parents:
diff changeset
334 }
74212992fb08 Initial revision
Sam Lantinga <slouken@lokigames.com>
parents:
diff changeset
335
74212992fb08 Initial revision
Sam Lantinga <slouken@lokigames.com>
parents:
diff changeset
336 void SDL_SYS_StopTimer(void)
74212992fb08 Initial revision
Sam Lantinga <slouken@lokigames.com>
parents:
diff changeset
337 {
74212992fb08 Initial revision
Sam Lantinga <slouken@lokigames.com>
parents:
diff changeset
338 return;
74212992fb08 Initial revision
Sam Lantinga <slouken@lokigames.com>
parents:
diff changeset
339 }
74212992fb08 Initial revision
Sam Lantinga <slouken@lokigames.com>
parents:
diff changeset
340
74212992fb08 Initial revision
Sam Lantinga <slouken@lokigames.com>
parents:
diff changeset
341 #endif /* USE_ITIMER */