annotate src/timer/SDL_timer.c @ 5116:e337f792c6a7

Fixed compiling under Visual Studio
author Sam Lantinga <slouken@libsdl.org>
date Thu, 27 Jan 2011 15:31:00 -0800
parents 481dabb098ef
children 427998ff3bcf
rev   line source
1
cf2af46e9e2a Changes since SDL 1.2.0 release
Sam Lantinga <slouken@lokigames.com>
parents:
diff changeset
1 /*
cf2af46e9e2a Changes since SDL 1.2.0 release
Sam Lantinga <slouken@lokigames.com>
parents:
diff changeset
2 SDL - Simple DirectMedia Layer
3697
f7b03b6838cb Fixed bug #926
Sam Lantinga <slouken@libsdl.org>
parents: 3578
diff changeset
3 Copyright (C) 1997-2010 Sam Lantinga
1
cf2af46e9e2a Changes since SDL 1.2.0 release
Sam Lantinga <slouken@lokigames.com>
parents:
diff changeset
4
cf2af46e9e2a Changes since SDL 1.2.0 release
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: 1190
diff changeset
6 modify it under the terms of the GNU Lesser General Public
1
cf2af46e9e2a Changes since SDL 1.2.0 release
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: 1190
diff changeset
8 version 2.1 of the License, or (at your option) any later version.
1
cf2af46e9e2a Changes since SDL 1.2.0 release
Sam Lantinga <slouken@lokigames.com>
parents:
diff changeset
9
cf2af46e9e2a Changes since SDL 1.2.0 release
Sam Lantinga <slouken@lokigames.com>
parents:
diff changeset
10 This library is distributed in the hope that it will be useful,
cf2af46e9e2a Changes since SDL 1.2.0 release
Sam Lantinga <slouken@lokigames.com>
parents:
diff changeset
11 but WITHOUT ANY WARRANTY; without even the implied warranty of
cf2af46e9e2a Changes since SDL 1.2.0 release
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: 1190
diff changeset
13 Lesser General Public License for more details.
1
cf2af46e9e2a Changes since SDL 1.2.0 release
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: 1190
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: 1190
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: 1190
diff changeset
17 Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
1
cf2af46e9e2a Changes since SDL 1.2.0 release
Sam Lantinga <slouken@lokigames.com>
parents:
diff changeset
18
cf2af46e9e2a Changes since SDL 1.2.0 release
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: 1
diff changeset
20 slouken@libsdl.org
1
cf2af46e9e2a Changes since SDL 1.2.0 release
Sam Lantinga <slouken@lokigames.com>
parents:
diff changeset
21 */
1402
d910939febfa Use consistent identifiers for the various platforms we support.
Sam Lantinga <slouken@libsdl.org>
parents: 1358
diff changeset
22 #include "SDL_config.h"
1
cf2af46e9e2a Changes since SDL 1.2.0 release
Sam Lantinga <slouken@lokigames.com>
parents:
diff changeset
23
cf2af46e9e2a Changes since SDL 1.2.0 release
Sam Lantinga <slouken@lokigames.com>
parents:
diff changeset
24 #include "SDL_timer.h"
cf2af46e9e2a Changes since SDL 1.2.0 release
Sam Lantinga <slouken@lokigames.com>
parents:
diff changeset
25 #include "SDL_timer_c.h"
5113
481dabb098ef Improved timer implementation
Sam Lantinga <slouken@libsdl.org>
parents: 5074
diff changeset
26 #include "SDL_atomic.h"
481dabb098ef Improved timer implementation
Sam Lantinga <slouken@libsdl.org>
parents: 5074
diff changeset
27 #include "SDL_thread.h"
1
cf2af46e9e2a Changes since SDL 1.2.0 release
Sam Lantinga <slouken@lokigames.com>
parents:
diff changeset
28
cf2af46e9e2a Changes since SDL 1.2.0 release
Sam Lantinga <slouken@lokigames.com>
parents:
diff changeset
29 /* #define DEBUG_TIMERS */
cf2af46e9e2a Changes since SDL 1.2.0 release
Sam Lantinga <slouken@lokigames.com>
parents:
diff changeset
30
5113
481dabb098ef Improved timer implementation
Sam Lantinga <slouken@libsdl.org>
parents: 5074
diff changeset
31 typedef struct _SDL_Timer
481dabb098ef Improved timer implementation
Sam Lantinga <slouken@libsdl.org>
parents: 5074
diff changeset
32 {
481dabb098ef Improved timer implementation
Sam Lantinga <slouken@libsdl.org>
parents: 5074
diff changeset
33 int timerID;
481dabb098ef Improved timer implementation
Sam Lantinga <slouken@libsdl.org>
parents: 5074
diff changeset
34 SDL_TimerCallback callback;
481dabb098ef Improved timer implementation
Sam Lantinga <slouken@libsdl.org>
parents: 5074
diff changeset
35 void *param;
481dabb098ef Improved timer implementation
Sam Lantinga <slouken@libsdl.org>
parents: 5074
diff changeset
36 Uint32 interval;
481dabb098ef Improved timer implementation
Sam Lantinga <slouken@libsdl.org>
parents: 5074
diff changeset
37 Uint32 scheduled;
481dabb098ef Improved timer implementation
Sam Lantinga <slouken@libsdl.org>
parents: 5074
diff changeset
38 volatile SDL_bool canceled;
481dabb098ef Improved timer implementation
Sam Lantinga <slouken@libsdl.org>
parents: 5074
diff changeset
39 struct _SDL_Timer *next;
481dabb098ef Improved timer implementation
Sam Lantinga <slouken@libsdl.org>
parents: 5074
diff changeset
40 } SDL_Timer;
481dabb098ef Improved timer implementation
Sam Lantinga <slouken@libsdl.org>
parents: 5074
diff changeset
41
481dabb098ef Improved timer implementation
Sam Lantinga <slouken@libsdl.org>
parents: 5074
diff changeset
42 typedef struct _SDL_TimerMap
481dabb098ef Improved timer implementation
Sam Lantinga <slouken@libsdl.org>
parents: 5074
diff changeset
43 {
481dabb098ef Improved timer implementation
Sam Lantinga <slouken@libsdl.org>
parents: 5074
diff changeset
44 int timerID;
481dabb098ef Improved timer implementation
Sam Lantinga <slouken@libsdl.org>
parents: 5074
diff changeset
45 SDL_Timer *timer;
481dabb098ef Improved timer implementation
Sam Lantinga <slouken@libsdl.org>
parents: 5074
diff changeset
46 struct _SDL_TimerMap *next;
481dabb098ef Improved timer implementation
Sam Lantinga <slouken@libsdl.org>
parents: 5074
diff changeset
47 } SDL_TimerMap;
1
cf2af46e9e2a Changes since SDL 1.2.0 release
Sam Lantinga <slouken@lokigames.com>
parents:
diff changeset
48
5113
481dabb098ef Improved timer implementation
Sam Lantinga <slouken@libsdl.org>
parents: 5074
diff changeset
49 /* A reasonable guess */
481dabb098ef Improved timer implementation
Sam Lantinga <slouken@libsdl.org>
parents: 5074
diff changeset
50 #define CACHELINE_SIZE 128
481dabb098ef Improved timer implementation
Sam Lantinga <slouken@libsdl.org>
parents: 5074
diff changeset
51
481dabb098ef Improved timer implementation
Sam Lantinga <slouken@libsdl.org>
parents: 5074
diff changeset
52 /* The timers are kept in a sorted list */
481dabb098ef Improved timer implementation
Sam Lantinga <slouken@libsdl.org>
parents: 5074
diff changeset
53 typedef struct {
481dabb098ef Improved timer implementation
Sam Lantinga <slouken@libsdl.org>
parents: 5074
diff changeset
54 /* Data used by the main thread */
481dabb098ef Improved timer implementation
Sam Lantinga <slouken@libsdl.org>
parents: 5074
diff changeset
55 SDL_Thread *thread;
481dabb098ef Improved timer implementation
Sam Lantinga <slouken@libsdl.org>
parents: 5074
diff changeset
56 SDL_atomic_t nextID;
481dabb098ef Improved timer implementation
Sam Lantinga <slouken@libsdl.org>
parents: 5074
diff changeset
57 SDL_TimerMap *timermap;
481dabb098ef Improved timer implementation
Sam Lantinga <slouken@libsdl.org>
parents: 5074
diff changeset
58 SDL_mutex *timermap_lock;
481dabb098ef Improved timer implementation
Sam Lantinga <slouken@libsdl.org>
parents: 5074
diff changeset
59
481dabb098ef Improved timer implementation
Sam Lantinga <slouken@libsdl.org>
parents: 5074
diff changeset
60 /* Padding to separate cache lines between threads */
481dabb098ef Improved timer implementation
Sam Lantinga <slouken@libsdl.org>
parents: 5074
diff changeset
61 char pad[CACHELINE_SIZE];
481dabb098ef Improved timer implementation
Sam Lantinga <slouken@libsdl.org>
parents: 5074
diff changeset
62
481dabb098ef Improved timer implementation
Sam Lantinga <slouken@libsdl.org>
parents: 5074
diff changeset
63 /* Data used to communicate with the timer thread */
481dabb098ef Improved timer implementation
Sam Lantinga <slouken@libsdl.org>
parents: 5074
diff changeset
64 SDL_SpinLock lock;
481dabb098ef Improved timer implementation
Sam Lantinga <slouken@libsdl.org>
parents: 5074
diff changeset
65 SDL_sem *sem;
481dabb098ef Improved timer implementation
Sam Lantinga <slouken@libsdl.org>
parents: 5074
diff changeset
66 SDL_Timer * volatile pending;
481dabb098ef Improved timer implementation
Sam Lantinga <slouken@libsdl.org>
parents: 5074
diff changeset
67 SDL_Timer * volatile freelist;
481dabb098ef Improved timer implementation
Sam Lantinga <slouken@libsdl.org>
parents: 5074
diff changeset
68 volatile SDL_bool active;
1
cf2af46e9e2a Changes since SDL 1.2.0 release
Sam Lantinga <slouken@lokigames.com>
parents:
diff changeset
69
5113
481dabb098ef Improved timer implementation
Sam Lantinga <slouken@libsdl.org>
parents: 5074
diff changeset
70 /* List of timers - this is only touched by the timer thread */
481dabb098ef Improved timer implementation
Sam Lantinga <slouken@libsdl.org>
parents: 5074
diff changeset
71 SDL_Timer *timers;
481dabb098ef Improved timer implementation
Sam Lantinga <slouken@libsdl.org>
parents: 5074
diff changeset
72 } SDL_TimerData;
481dabb098ef Improved timer implementation
Sam Lantinga <slouken@libsdl.org>
parents: 5074
diff changeset
73
481dabb098ef Improved timer implementation
Sam Lantinga <slouken@libsdl.org>
parents: 5074
diff changeset
74 static SDL_TimerData SDL_timer_data;
481dabb098ef Improved timer implementation
Sam Lantinga <slouken@libsdl.org>
parents: 5074
diff changeset
75
1
cf2af46e9e2a Changes since SDL 1.2.0 release
Sam Lantinga <slouken@lokigames.com>
parents:
diff changeset
76
5113
481dabb098ef Improved timer implementation
Sam Lantinga <slouken@libsdl.org>
parents: 5074
diff changeset
77 /* The idea here is that any thread might add a timer, but a single
481dabb098ef Improved timer implementation
Sam Lantinga <slouken@libsdl.org>
parents: 5074
diff changeset
78 * thread manages the active timer queue, sorted by scheduling time.
481dabb098ef Improved timer implementation
Sam Lantinga <slouken@libsdl.org>
parents: 5074
diff changeset
79 *
481dabb098ef Improved timer implementation
Sam Lantinga <slouken@libsdl.org>
parents: 5074
diff changeset
80 * Timers are removed by simply setting a canceled flag
481dabb098ef Improved timer implementation
Sam Lantinga <slouken@libsdl.org>
parents: 5074
diff changeset
81 */
481dabb098ef Improved timer implementation
Sam Lantinga <slouken@libsdl.org>
parents: 5074
diff changeset
82
481dabb098ef Improved timer implementation
Sam Lantinga <slouken@libsdl.org>
parents: 5074
diff changeset
83 static void
481dabb098ef Improved timer implementation
Sam Lantinga <slouken@libsdl.org>
parents: 5074
diff changeset
84 SDL_AddTimerInternal(SDL_TimerData *data, SDL_Timer *timer)
1895
c121d94672cb SDL 1.2 is moving to a branch, and SDL 1.3 is becoming the head.
Sam Lantinga <slouken@libsdl.org>
parents: 1788
diff changeset
85 {
5113
481dabb098ef Improved timer implementation
Sam Lantinga <slouken@libsdl.org>
parents: 5074
diff changeset
86 SDL_Timer *prev, *curr;
481dabb098ef Improved timer implementation
Sam Lantinga <slouken@libsdl.org>
parents: 5074
diff changeset
87
481dabb098ef Improved timer implementation
Sam Lantinga <slouken@libsdl.org>
parents: 5074
diff changeset
88 prev = NULL;
481dabb098ef Improved timer implementation
Sam Lantinga <slouken@libsdl.org>
parents: 5074
diff changeset
89 for (curr = data->timers; curr; prev = curr, curr = curr->next) {
481dabb098ef Improved timer implementation
Sam Lantinga <slouken@libsdl.org>
parents: 5074
diff changeset
90 if ((Sint32)(timer->scheduled-curr->scheduled) < 0) {
481dabb098ef Improved timer implementation
Sam Lantinga <slouken@libsdl.org>
parents: 5074
diff changeset
91 break;
481dabb098ef Improved timer implementation
Sam Lantinga <slouken@libsdl.org>
parents: 5074
diff changeset
92 }
481dabb098ef Improved timer implementation
Sam Lantinga <slouken@libsdl.org>
parents: 5074
diff changeset
93 }
481dabb098ef Improved timer implementation
Sam Lantinga <slouken@libsdl.org>
parents: 5074
diff changeset
94
481dabb098ef Improved timer implementation
Sam Lantinga <slouken@libsdl.org>
parents: 5074
diff changeset
95 /* Insert the timer here! */
481dabb098ef Improved timer implementation
Sam Lantinga <slouken@libsdl.org>
parents: 5074
diff changeset
96 if (prev) {
481dabb098ef Improved timer implementation
Sam Lantinga <slouken@libsdl.org>
parents: 5074
diff changeset
97 prev->next = timer;
481dabb098ef Improved timer implementation
Sam Lantinga <slouken@libsdl.org>
parents: 5074
diff changeset
98 } else {
481dabb098ef Improved timer implementation
Sam Lantinga <slouken@libsdl.org>
parents: 5074
diff changeset
99 data->timers = timer;
481dabb098ef Improved timer implementation
Sam Lantinga <slouken@libsdl.org>
parents: 5074
diff changeset
100 }
481dabb098ef Improved timer implementation
Sam Lantinga <slouken@libsdl.org>
parents: 5074
diff changeset
101 timer->next = curr;
481dabb098ef Improved timer implementation
Sam Lantinga <slouken@libsdl.org>
parents: 5074
diff changeset
102 }
481dabb098ef Improved timer implementation
Sam Lantinga <slouken@libsdl.org>
parents: 5074
diff changeset
103
481dabb098ef Improved timer implementation
Sam Lantinga <slouken@libsdl.org>
parents: 5074
diff changeset
104 static int
481dabb098ef Improved timer implementation
Sam Lantinga <slouken@libsdl.org>
parents: 5074
diff changeset
105 SDL_TimerThread(void *_data)
481dabb098ef Improved timer implementation
Sam Lantinga <slouken@libsdl.org>
parents: 5074
diff changeset
106 {
481dabb098ef Improved timer implementation
Sam Lantinga <slouken@libsdl.org>
parents: 5074
diff changeset
107 SDL_TimerData *data = (SDL_TimerData *)_data;
481dabb098ef Improved timer implementation
Sam Lantinga <slouken@libsdl.org>
parents: 5074
diff changeset
108 SDL_Timer *pending;
481dabb098ef Improved timer implementation
Sam Lantinga <slouken@libsdl.org>
parents: 5074
diff changeset
109 SDL_Timer *current;
481dabb098ef Improved timer implementation
Sam Lantinga <slouken@libsdl.org>
parents: 5074
diff changeset
110 SDL_Timer *freelist_head = NULL;
481dabb098ef Improved timer implementation
Sam Lantinga <slouken@libsdl.org>
parents: 5074
diff changeset
111 SDL_Timer *freelist_tail = NULL;
481dabb098ef Improved timer implementation
Sam Lantinga <slouken@libsdl.org>
parents: 5074
diff changeset
112 Uint32 tick, now, interval, delay;
1
cf2af46e9e2a Changes since SDL 1.2.0 release
Sam Lantinga <slouken@lokigames.com>
parents:
diff changeset
113
5113
481dabb098ef Improved timer implementation
Sam Lantinga <slouken@libsdl.org>
parents: 5074
diff changeset
114 /* Threaded timer loop:
481dabb098ef Improved timer implementation
Sam Lantinga <slouken@libsdl.org>
parents: 5074
diff changeset
115 * 1. Queue timers added by other threads
481dabb098ef Improved timer implementation
Sam Lantinga <slouken@libsdl.org>
parents: 5074
diff changeset
116 * 2. Handle any timers that should dispatch this cycle
481dabb098ef Improved timer implementation
Sam Lantinga <slouken@libsdl.org>
parents: 5074
diff changeset
117 * 3. Wait until next dispatch time or new timer arrives
481dabb098ef Improved timer implementation
Sam Lantinga <slouken@libsdl.org>
parents: 5074
diff changeset
118 */
481dabb098ef Improved timer implementation
Sam Lantinga <slouken@libsdl.org>
parents: 5074
diff changeset
119 for ( ; ; ) {
481dabb098ef Improved timer implementation
Sam Lantinga <slouken@libsdl.org>
parents: 5074
diff changeset
120 /* Pending and freelist maintenance */
481dabb098ef Improved timer implementation
Sam Lantinga <slouken@libsdl.org>
parents: 5074
diff changeset
121 SDL_AtomicLock(&data->lock);
481dabb098ef Improved timer implementation
Sam Lantinga <slouken@libsdl.org>
parents: 5074
diff changeset
122 {
481dabb098ef Improved timer implementation
Sam Lantinga <slouken@libsdl.org>
parents: 5074
diff changeset
123 /* Get any timers ready to be queued */
481dabb098ef Improved timer implementation
Sam Lantinga <slouken@libsdl.org>
parents: 5074
diff changeset
124 pending = data->pending;
481dabb098ef Improved timer implementation
Sam Lantinga <slouken@libsdl.org>
parents: 5074
diff changeset
125 data->pending = NULL;
481dabb098ef Improved timer implementation
Sam Lantinga <slouken@libsdl.org>
parents: 5074
diff changeset
126
481dabb098ef Improved timer implementation
Sam Lantinga <slouken@libsdl.org>
parents: 5074
diff changeset
127 /* Make any unused timer structures available */
481dabb098ef Improved timer implementation
Sam Lantinga <slouken@libsdl.org>
parents: 5074
diff changeset
128 if (freelist_head) {
481dabb098ef Improved timer implementation
Sam Lantinga <slouken@libsdl.org>
parents: 5074
diff changeset
129 freelist_tail->next = data->freelist;
481dabb098ef Improved timer implementation
Sam Lantinga <slouken@libsdl.org>
parents: 5074
diff changeset
130 data->freelist = freelist_head;
481dabb098ef Improved timer implementation
Sam Lantinga <slouken@libsdl.org>
parents: 5074
diff changeset
131 }
481dabb098ef Improved timer implementation
Sam Lantinga <slouken@libsdl.org>
parents: 5074
diff changeset
132 }
481dabb098ef Improved timer implementation
Sam Lantinga <slouken@libsdl.org>
parents: 5074
diff changeset
133 SDL_AtomicUnlock(&data->lock);
481dabb098ef Improved timer implementation
Sam Lantinga <slouken@libsdl.org>
parents: 5074
diff changeset
134
481dabb098ef Improved timer implementation
Sam Lantinga <slouken@libsdl.org>
parents: 5074
diff changeset
135 /* Sort the pending timers into our list */
481dabb098ef Improved timer implementation
Sam Lantinga <slouken@libsdl.org>
parents: 5074
diff changeset
136 while (pending) {
481dabb098ef Improved timer implementation
Sam Lantinga <slouken@libsdl.org>
parents: 5074
diff changeset
137 current = pending;
481dabb098ef Improved timer implementation
Sam Lantinga <slouken@libsdl.org>
parents: 5074
diff changeset
138 pending = pending->next;
481dabb098ef Improved timer implementation
Sam Lantinga <slouken@libsdl.org>
parents: 5074
diff changeset
139 SDL_AddTimerInternal(data, current);
481dabb098ef Improved timer implementation
Sam Lantinga <slouken@libsdl.org>
parents: 5074
diff changeset
140 }
481dabb098ef Improved timer implementation
Sam Lantinga <slouken@libsdl.org>
parents: 5074
diff changeset
141 freelist_head = NULL;
481dabb098ef Improved timer implementation
Sam Lantinga <slouken@libsdl.org>
parents: 5074
diff changeset
142 freelist_tail = NULL;
481dabb098ef Improved timer implementation
Sam Lantinga <slouken@libsdl.org>
parents: 5074
diff changeset
143
481dabb098ef Improved timer implementation
Sam Lantinga <slouken@libsdl.org>
parents: 5074
diff changeset
144 /* Check to see if we're still running, after maintenance */
481dabb098ef Improved timer implementation
Sam Lantinga <slouken@libsdl.org>
parents: 5074
diff changeset
145 if (!data->active) {
481dabb098ef Improved timer implementation
Sam Lantinga <slouken@libsdl.org>
parents: 5074
diff changeset
146 break;
481dabb098ef Improved timer implementation
Sam Lantinga <slouken@libsdl.org>
parents: 5074
diff changeset
147 }
481dabb098ef Improved timer implementation
Sam Lantinga <slouken@libsdl.org>
parents: 5074
diff changeset
148
481dabb098ef Improved timer implementation
Sam Lantinga <slouken@libsdl.org>
parents: 5074
diff changeset
149 /* Initial delay if there are no timers */
481dabb098ef Improved timer implementation
Sam Lantinga <slouken@libsdl.org>
parents: 5074
diff changeset
150 delay = SDL_MUTEX_MAXWAIT;
481dabb098ef Improved timer implementation
Sam Lantinga <slouken@libsdl.org>
parents: 5074
diff changeset
151
481dabb098ef Improved timer implementation
Sam Lantinga <slouken@libsdl.org>
parents: 5074
diff changeset
152 tick = SDL_GetTicks();
481dabb098ef Improved timer implementation
Sam Lantinga <slouken@libsdl.org>
parents: 5074
diff changeset
153
481dabb098ef Improved timer implementation
Sam Lantinga <slouken@libsdl.org>
parents: 5074
diff changeset
154 /* Process all the pending timers for this tick */
481dabb098ef Improved timer implementation
Sam Lantinga <slouken@libsdl.org>
parents: 5074
diff changeset
155 while (data->timers) {
481dabb098ef Improved timer implementation
Sam Lantinga <slouken@libsdl.org>
parents: 5074
diff changeset
156 current = data->timers;
1
cf2af46e9e2a Changes since SDL 1.2.0 release
Sam Lantinga <slouken@lokigames.com>
parents:
diff changeset
157
5113
481dabb098ef Improved timer implementation
Sam Lantinga <slouken@libsdl.org>
parents: 5074
diff changeset
158 if ((Sint32)(tick-current->scheduled) < 0) {
481dabb098ef Improved timer implementation
Sam Lantinga <slouken@libsdl.org>
parents: 5074
diff changeset
159 /* Scheduled for the future, wait a bit */
481dabb098ef Improved timer implementation
Sam Lantinga <slouken@libsdl.org>
parents: 5074
diff changeset
160 delay = (current->scheduled - tick);
481dabb098ef Improved timer implementation
Sam Lantinga <slouken@libsdl.org>
parents: 5074
diff changeset
161 break;
481dabb098ef Improved timer implementation
Sam Lantinga <slouken@libsdl.org>
parents: 5074
diff changeset
162 }
481dabb098ef Improved timer implementation
Sam Lantinga <slouken@libsdl.org>
parents: 5074
diff changeset
163
481dabb098ef Improved timer implementation
Sam Lantinga <slouken@libsdl.org>
parents: 5074
diff changeset
164 /* We're going to do something with this timer */
481dabb098ef Improved timer implementation
Sam Lantinga <slouken@libsdl.org>
parents: 5074
diff changeset
165 data->timers = current->next;
481dabb098ef Improved timer implementation
Sam Lantinga <slouken@libsdl.org>
parents: 5074
diff changeset
166
481dabb098ef Improved timer implementation
Sam Lantinga <slouken@libsdl.org>
parents: 5074
diff changeset
167 if (current->canceled) {
481dabb098ef Improved timer implementation
Sam Lantinga <slouken@libsdl.org>
parents: 5074
diff changeset
168 interval = 0;
481dabb098ef Improved timer implementation
Sam Lantinga <slouken@libsdl.org>
parents: 5074
diff changeset
169 } else {
481dabb098ef Improved timer implementation
Sam Lantinga <slouken@libsdl.org>
parents: 5074
diff changeset
170 interval = current->callback(current->interval, current->param);
481dabb098ef Improved timer implementation
Sam Lantinga <slouken@libsdl.org>
parents: 5074
diff changeset
171 }
1
cf2af46e9e2a Changes since SDL 1.2.0 release
Sam Lantinga <slouken@lokigames.com>
parents:
diff changeset
172
5113
481dabb098ef Improved timer implementation
Sam Lantinga <slouken@libsdl.org>
parents: 5074
diff changeset
173 if (interval > 0) {
481dabb098ef Improved timer implementation
Sam Lantinga <slouken@libsdl.org>
parents: 5074
diff changeset
174 /* Reschedule this timer */
481dabb098ef Improved timer implementation
Sam Lantinga <slouken@libsdl.org>
parents: 5074
diff changeset
175 current->scheduled = tick + interval;
481dabb098ef Improved timer implementation
Sam Lantinga <slouken@libsdl.org>
parents: 5074
diff changeset
176 SDL_AddTimerInternal(data, current);
481dabb098ef Improved timer implementation
Sam Lantinga <slouken@libsdl.org>
parents: 5074
diff changeset
177 } else {
481dabb098ef Improved timer implementation
Sam Lantinga <slouken@libsdl.org>
parents: 5074
diff changeset
178 if (!freelist_head) {
481dabb098ef Improved timer implementation
Sam Lantinga <slouken@libsdl.org>
parents: 5074
diff changeset
179 freelist_head = current;
481dabb098ef Improved timer implementation
Sam Lantinga <slouken@libsdl.org>
parents: 5074
diff changeset
180 }
481dabb098ef Improved timer implementation
Sam Lantinga <slouken@libsdl.org>
parents: 5074
diff changeset
181 if (freelist_tail) {
481dabb098ef Improved timer implementation
Sam Lantinga <slouken@libsdl.org>
parents: 5074
diff changeset
182 freelist_tail->next = current;
481dabb098ef Improved timer implementation
Sam Lantinga <slouken@libsdl.org>
parents: 5074
diff changeset
183 }
481dabb098ef Improved timer implementation
Sam Lantinga <slouken@libsdl.org>
parents: 5074
diff changeset
184 freelist_tail = current;
481dabb098ef Improved timer implementation
Sam Lantinga <slouken@libsdl.org>
parents: 5074
diff changeset
185
481dabb098ef Improved timer implementation
Sam Lantinga <slouken@libsdl.org>
parents: 5074
diff changeset
186 current->canceled = SDL_TRUE;
481dabb098ef Improved timer implementation
Sam Lantinga <slouken@libsdl.org>
parents: 5074
diff changeset
187 }
481dabb098ef Improved timer implementation
Sam Lantinga <slouken@libsdl.org>
parents: 5074
diff changeset
188 }
481dabb098ef Improved timer implementation
Sam Lantinga <slouken@libsdl.org>
parents: 5074
diff changeset
189
481dabb098ef Improved timer implementation
Sam Lantinga <slouken@libsdl.org>
parents: 5074
diff changeset
190 /* Adjust the delay based on processing time */
481dabb098ef Improved timer implementation
Sam Lantinga <slouken@libsdl.org>
parents: 5074
diff changeset
191 now = SDL_GetTicks();
481dabb098ef Improved timer implementation
Sam Lantinga <slouken@libsdl.org>
parents: 5074
diff changeset
192 interval = (now - tick);
481dabb098ef Improved timer implementation
Sam Lantinga <slouken@libsdl.org>
parents: 5074
diff changeset
193 if (interval > delay) {
481dabb098ef Improved timer implementation
Sam Lantinga <slouken@libsdl.org>
parents: 5074
diff changeset
194 delay = 0;
481dabb098ef Improved timer implementation
Sam Lantinga <slouken@libsdl.org>
parents: 5074
diff changeset
195 } else {
481dabb098ef Improved timer implementation
Sam Lantinga <slouken@libsdl.org>
parents: 5074
diff changeset
196 delay -= interval;
481dabb098ef Improved timer implementation
Sam Lantinga <slouken@libsdl.org>
parents: 5074
diff changeset
197 }
481dabb098ef Improved timer implementation
Sam Lantinga <slouken@libsdl.org>
parents: 5074
diff changeset
198
481dabb098ef Improved timer implementation
Sam Lantinga <slouken@libsdl.org>
parents: 5074
diff changeset
199 /* Note that each time a timer is added, this will return
481dabb098ef Improved timer implementation
Sam Lantinga <slouken@libsdl.org>
parents: 5074
diff changeset
200 immediately, but we process the timers added all at once.
481dabb098ef Improved timer implementation
Sam Lantinga <slouken@libsdl.org>
parents: 5074
diff changeset
201 That's okay, it just means we run through the loop a few
481dabb098ef Improved timer implementation
Sam Lantinga <slouken@libsdl.org>
parents: 5074
diff changeset
202 extra times.
481dabb098ef Improved timer implementation
Sam Lantinga <slouken@libsdl.org>
parents: 5074
diff changeset
203 */
481dabb098ef Improved timer implementation
Sam Lantinga <slouken@libsdl.org>
parents: 5074
diff changeset
204 SDL_SemWaitTimeout(data->sem, delay);
1895
c121d94672cb SDL 1.2 is moving to a branch, and SDL 1.3 is becoming the head.
Sam Lantinga <slouken@libsdl.org>
parents: 1788
diff changeset
205 }
5113
481dabb098ef Improved timer implementation
Sam Lantinga <slouken@libsdl.org>
parents: 5074
diff changeset
206 return 0;
1
cf2af46e9e2a Changes since SDL 1.2.0 release
Sam Lantinga <slouken@lokigames.com>
parents:
diff changeset
207 }
cf2af46e9e2a Changes since SDL 1.2.0 release
Sam Lantinga <slouken@lokigames.com>
parents:
diff changeset
208
1895
c121d94672cb SDL 1.2 is moving to a branch, and SDL 1.3 is becoming the head.
Sam Lantinga <slouken@libsdl.org>
parents: 1788
diff changeset
209 int
c121d94672cb SDL 1.2 is moving to a branch, and SDL 1.3 is becoming the head.
Sam Lantinga <slouken@libsdl.org>
parents: 1788
diff changeset
210 SDL_TimerInit(void)
1
cf2af46e9e2a Changes since SDL 1.2.0 release
Sam Lantinga <slouken@lokigames.com>
parents:
diff changeset
211 {
5113
481dabb098ef Improved timer implementation
Sam Lantinga <slouken@libsdl.org>
parents: 5074
diff changeset
212 SDL_TimerData *data = &SDL_timer_data;
481dabb098ef Improved timer implementation
Sam Lantinga <slouken@libsdl.org>
parents: 5074
diff changeset
213
481dabb098ef Improved timer implementation
Sam Lantinga <slouken@libsdl.org>
parents: 5074
diff changeset
214 if (!data->active) {
481dabb098ef Improved timer implementation
Sam Lantinga <slouken@libsdl.org>
parents: 5074
diff changeset
215 data->timermap_lock = SDL_CreateMutex();
481dabb098ef Improved timer implementation
Sam Lantinga <slouken@libsdl.org>
parents: 5074
diff changeset
216 if (!data->timermap_lock) {
481dabb098ef Improved timer implementation
Sam Lantinga <slouken@libsdl.org>
parents: 5074
diff changeset
217 return -1;
481dabb098ef Improved timer implementation
Sam Lantinga <slouken@libsdl.org>
parents: 5074
diff changeset
218 }
1
cf2af46e9e2a Changes since SDL 1.2.0 release
Sam Lantinga <slouken@lokigames.com>
parents:
diff changeset
219
5113
481dabb098ef Improved timer implementation
Sam Lantinga <slouken@libsdl.org>
parents: 5074
diff changeset
220 data->sem = SDL_CreateSemaphore(0);
481dabb098ef Improved timer implementation
Sam Lantinga <slouken@libsdl.org>
parents: 5074
diff changeset
221 if (!data->sem) {
481dabb098ef Improved timer implementation
Sam Lantinga <slouken@libsdl.org>
parents: 5074
diff changeset
222 SDL_DestroyMutex(data->timermap_lock);
481dabb098ef Improved timer implementation
Sam Lantinga <slouken@libsdl.org>
parents: 5074
diff changeset
223 return -1;
481dabb098ef Improved timer implementation
Sam Lantinga <slouken@libsdl.org>
parents: 5074
diff changeset
224 }
481dabb098ef Improved timer implementation
Sam Lantinga <slouken@libsdl.org>
parents: 5074
diff changeset
225
481dabb098ef Improved timer implementation
Sam Lantinga <slouken@libsdl.org>
parents: 5074
diff changeset
226 data->active = SDL_TRUE;
5116
e337f792c6a7 Fixed compiling under Visual Studio
Sam Lantinga <slouken@libsdl.org>
parents: 5113
diff changeset
227 /* !!! FIXME: this is nasty. */
e337f792c6a7 Fixed compiling under Visual Studio
Sam Lantinga <slouken@libsdl.org>
parents: 5113
diff changeset
228 #if (defined(__WIN32__) && !defined(_WIN32_WCE)) && !defined(HAVE_LIBC)
e337f792c6a7 Fixed compiling under Visual Studio
Sam Lantinga <slouken@libsdl.org>
parents: 5113
diff changeset
229 #undef SDL_CreateThread
e337f792c6a7 Fixed compiling under Visual Studio
Sam Lantinga <slouken@libsdl.org>
parents: 5113
diff changeset
230 data->thread = SDL_CreateThread(SDL_TimerThread, data, NULL, NULL);
e337f792c6a7 Fixed compiling under Visual Studio
Sam Lantinga <slouken@libsdl.org>
parents: 5113
diff changeset
231 #else
5113
481dabb098ef Improved timer implementation
Sam Lantinga <slouken@libsdl.org>
parents: 5074
diff changeset
232 data->thread = SDL_CreateThread(SDL_TimerThread, data);
5116
e337f792c6a7 Fixed compiling under Visual Studio
Sam Lantinga <slouken@libsdl.org>
parents: 5113
diff changeset
233 #endif
5113
481dabb098ef Improved timer implementation
Sam Lantinga <slouken@libsdl.org>
parents: 5074
diff changeset
234 if (!data->thread) {
481dabb098ef Improved timer implementation
Sam Lantinga <slouken@libsdl.org>
parents: 5074
diff changeset
235 SDL_TimerQuit();
481dabb098ef Improved timer implementation
Sam Lantinga <slouken@libsdl.org>
parents: 5074
diff changeset
236 return -1;
481dabb098ef Improved timer implementation
Sam Lantinga <slouken@libsdl.org>
parents: 5074
diff changeset
237 }
481dabb098ef Improved timer implementation
Sam Lantinga <slouken@libsdl.org>
parents: 5074
diff changeset
238
481dabb098ef Improved timer implementation
Sam Lantinga <slouken@libsdl.org>
parents: 5074
diff changeset
239 SDL_AtomicSet(&data->nextID, 1);
1895
c121d94672cb SDL 1.2 is moving to a branch, and SDL 1.3 is becoming the head.
Sam Lantinga <slouken@libsdl.org>
parents: 1788
diff changeset
240 }
5113
481dabb098ef Improved timer implementation
Sam Lantinga <slouken@libsdl.org>
parents: 5074
diff changeset
241 return 0;
1
cf2af46e9e2a Changes since SDL 1.2.0 release
Sam Lantinga <slouken@lokigames.com>
parents:
diff changeset
242 }
cf2af46e9e2a Changes since SDL 1.2.0 release
Sam Lantinga <slouken@lokigames.com>
parents:
diff changeset
243
1895
c121d94672cb SDL 1.2 is moving to a branch, and SDL 1.3 is becoming the head.
Sam Lantinga <slouken@libsdl.org>
parents: 1788
diff changeset
244 void
c121d94672cb SDL 1.2 is moving to a branch, and SDL 1.3 is becoming the head.
Sam Lantinga <slouken@libsdl.org>
parents: 1788
diff changeset
245 SDL_TimerQuit(void)
1
cf2af46e9e2a Changes since SDL 1.2.0 release
Sam Lantinga <slouken@lokigames.com>
parents:
diff changeset
246 {
5113
481dabb098ef Improved timer implementation
Sam Lantinga <slouken@libsdl.org>
parents: 5074
diff changeset
247 SDL_TimerData *data = &SDL_timer_data;
481dabb098ef Improved timer implementation
Sam Lantinga <slouken@libsdl.org>
parents: 5074
diff changeset
248 SDL_Timer *timer;
481dabb098ef Improved timer implementation
Sam Lantinga <slouken@libsdl.org>
parents: 5074
diff changeset
249 SDL_TimerMap *entry;
481dabb098ef Improved timer implementation
Sam Lantinga <slouken@libsdl.org>
parents: 5074
diff changeset
250
481dabb098ef Improved timer implementation
Sam Lantinga <slouken@libsdl.org>
parents: 5074
diff changeset
251 if (data->active) {
481dabb098ef Improved timer implementation
Sam Lantinga <slouken@libsdl.org>
parents: 5074
diff changeset
252 data->active = SDL_FALSE;
1
cf2af46e9e2a Changes since SDL 1.2.0 release
Sam Lantinga <slouken@lokigames.com>
parents:
diff changeset
253
5113
481dabb098ef Improved timer implementation
Sam Lantinga <slouken@libsdl.org>
parents: 5074
diff changeset
254 /* Shutdown the timer thread */
481dabb098ef Improved timer implementation
Sam Lantinga <slouken@libsdl.org>
parents: 5074
diff changeset
255 if (data->thread) {
481dabb098ef Improved timer implementation
Sam Lantinga <slouken@libsdl.org>
parents: 5074
diff changeset
256 SDL_SemPost(data->sem);
481dabb098ef Improved timer implementation
Sam Lantinga <slouken@libsdl.org>
parents: 5074
diff changeset
257 SDL_WaitThread(data->thread, NULL);
481dabb098ef Improved timer implementation
Sam Lantinga <slouken@libsdl.org>
parents: 5074
diff changeset
258 data->thread = NULL;
481dabb098ef Improved timer implementation
Sam Lantinga <slouken@libsdl.org>
parents: 5074
diff changeset
259 }
5074
906d7293bb47 Fixed bug in timer when the list of timers changed.
Sam Lantinga <slouken@libsdl.org>
parents: 3697
diff changeset
260
5113
481dabb098ef Improved timer implementation
Sam Lantinga <slouken@libsdl.org>
parents: 5074
diff changeset
261 SDL_DestroySemaphore(data->sem);
481dabb098ef Improved timer implementation
Sam Lantinga <slouken@libsdl.org>
parents: 5074
diff changeset
262 data->sem = NULL;
1028
5ba65305c954 Fix various problems with the timer code.
Sam Lantinga <slouken@libsdl.org>
parents: 1023
diff changeset
263
5113
481dabb098ef Improved timer implementation
Sam Lantinga <slouken@libsdl.org>
parents: 5074
diff changeset
264 /* Clean up the timer entries */
481dabb098ef Improved timer implementation
Sam Lantinga <slouken@libsdl.org>
parents: 5074
diff changeset
265 while (data->timers) {
481dabb098ef Improved timer implementation
Sam Lantinga <slouken@libsdl.org>
parents: 5074
diff changeset
266 timer = data->timers;
481dabb098ef Improved timer implementation
Sam Lantinga <slouken@libsdl.org>
parents: 5074
diff changeset
267 data->timers = timer->next;
481dabb098ef Improved timer implementation
Sam Lantinga <slouken@libsdl.org>
parents: 5074
diff changeset
268 SDL_free(timer);
1895
c121d94672cb SDL 1.2 is moving to a branch, and SDL 1.3 is becoming the head.
Sam Lantinga <slouken@libsdl.org>
parents: 1788
diff changeset
269 }
5113
481dabb098ef Improved timer implementation
Sam Lantinga <slouken@libsdl.org>
parents: 5074
diff changeset
270 while (data->freelist) {
481dabb098ef Improved timer implementation
Sam Lantinga <slouken@libsdl.org>
parents: 5074
diff changeset
271 timer = data->freelist;
481dabb098ef Improved timer implementation
Sam Lantinga <slouken@libsdl.org>
parents: 5074
diff changeset
272 data->freelist = timer->next;
481dabb098ef Improved timer implementation
Sam Lantinga <slouken@libsdl.org>
parents: 5074
diff changeset
273 SDL_free(timer);
481dabb098ef Improved timer implementation
Sam Lantinga <slouken@libsdl.org>
parents: 5074
diff changeset
274 }
481dabb098ef Improved timer implementation
Sam Lantinga <slouken@libsdl.org>
parents: 5074
diff changeset
275 while (data->timermap) {
481dabb098ef Improved timer implementation
Sam Lantinga <slouken@libsdl.org>
parents: 5074
diff changeset
276 entry = data->timermap;
481dabb098ef Improved timer implementation
Sam Lantinga <slouken@libsdl.org>
parents: 5074
diff changeset
277 data->timermap = entry->next;
481dabb098ef Improved timer implementation
Sam Lantinga <slouken@libsdl.org>
parents: 5074
diff changeset
278 SDL_free(entry);
481dabb098ef Improved timer implementation
Sam Lantinga <slouken@libsdl.org>
parents: 5074
diff changeset
279 }
1
cf2af46e9e2a Changes since SDL 1.2.0 release
Sam Lantinga <slouken@lokigames.com>
parents:
diff changeset
280
5113
481dabb098ef Improved timer implementation
Sam Lantinga <slouken@libsdl.org>
parents: 5074
diff changeset
281 SDL_DestroyMutex(data->timermap_lock);
481dabb098ef Improved timer implementation
Sam Lantinga <slouken@libsdl.org>
parents: 5074
diff changeset
282 data->timermap_lock = NULL;
1895
c121d94672cb SDL 1.2 is moving to a branch, and SDL 1.3 is becoming the head.
Sam Lantinga <slouken@libsdl.org>
parents: 1788
diff changeset
283 }
1028
5ba65305c954 Fix various problems with the timer code.
Sam Lantinga <slouken@libsdl.org>
parents: 1023
diff changeset
284 }
5ba65305c954 Fix various problems with the timer code.
Sam Lantinga <slouken@libsdl.org>
parents: 1023
diff changeset
285
1895
c121d94672cb SDL 1.2 is moving to a branch, and SDL 1.3 is becoming the head.
Sam Lantinga <slouken@libsdl.org>
parents: 1788
diff changeset
286 SDL_TimerID
5113
481dabb098ef Improved timer implementation
Sam Lantinga <slouken@libsdl.org>
parents: 5074
diff changeset
287 SDL_AddTimer(Uint32 interval, SDL_TimerCallback callback, void *param)
1
cf2af46e9e2a Changes since SDL 1.2.0 release
Sam Lantinga <slouken@lokigames.com>
parents:
diff changeset
288 {
5113
481dabb098ef Improved timer implementation
Sam Lantinga <slouken@libsdl.org>
parents: 5074
diff changeset
289 SDL_TimerData *data = &SDL_timer_data;
481dabb098ef Improved timer implementation
Sam Lantinga <slouken@libsdl.org>
parents: 5074
diff changeset
290 SDL_Timer *timer;
481dabb098ef Improved timer implementation
Sam Lantinga <slouken@libsdl.org>
parents: 5074
diff changeset
291 SDL_TimerMap *entry;
481dabb098ef Improved timer implementation
Sam Lantinga <slouken@libsdl.org>
parents: 5074
diff changeset
292
481dabb098ef Improved timer implementation
Sam Lantinga <slouken@libsdl.org>
parents: 5074
diff changeset
293 if (!data->active) {
481dabb098ef Improved timer implementation
Sam Lantinga <slouken@libsdl.org>
parents: 5074
diff changeset
294 int status = 0;
481dabb098ef Improved timer implementation
Sam Lantinga <slouken@libsdl.org>
parents: 5074
diff changeset
295
481dabb098ef Improved timer implementation
Sam Lantinga <slouken@libsdl.org>
parents: 5074
diff changeset
296 SDL_AtomicLock(&data->lock);
481dabb098ef Improved timer implementation
Sam Lantinga <slouken@libsdl.org>
parents: 5074
diff changeset
297 if (!data->active) {
481dabb098ef Improved timer implementation
Sam Lantinga <slouken@libsdl.org>
parents: 5074
diff changeset
298 status = SDL_TimerInit();
481dabb098ef Improved timer implementation
Sam Lantinga <slouken@libsdl.org>
parents: 5074
diff changeset
299 }
481dabb098ef Improved timer implementation
Sam Lantinga <slouken@libsdl.org>
parents: 5074
diff changeset
300 SDL_AtomicUnlock(&data->lock);
481dabb098ef Improved timer implementation
Sam Lantinga <slouken@libsdl.org>
parents: 5074
diff changeset
301
481dabb098ef Improved timer implementation
Sam Lantinga <slouken@libsdl.org>
parents: 5074
diff changeset
302 if (status < 0) {
481dabb098ef Improved timer implementation
Sam Lantinga <slouken@libsdl.org>
parents: 5074
diff changeset
303 return 0;
1895
c121d94672cb SDL 1.2 is moving to a branch, and SDL 1.3 is becoming the head.
Sam Lantinga <slouken@libsdl.org>
parents: 1788
diff changeset
304 }
5113
481dabb098ef Improved timer implementation
Sam Lantinga <slouken@libsdl.org>
parents: 5074
diff changeset
305 }
481dabb098ef Improved timer implementation
Sam Lantinga <slouken@libsdl.org>
parents: 5074
diff changeset
306
481dabb098ef Improved timer implementation
Sam Lantinga <slouken@libsdl.org>
parents: 5074
diff changeset
307 SDL_AtomicLock(&data->lock);
481dabb098ef Improved timer implementation
Sam Lantinga <slouken@libsdl.org>
parents: 5074
diff changeset
308 timer = data->freelist;
481dabb098ef Improved timer implementation
Sam Lantinga <slouken@libsdl.org>
parents: 5074
diff changeset
309 if (timer) {
481dabb098ef Improved timer implementation
Sam Lantinga <slouken@libsdl.org>
parents: 5074
diff changeset
310 data->freelist = timer->next;
481dabb098ef Improved timer implementation
Sam Lantinga <slouken@libsdl.org>
parents: 5074
diff changeset
311 }
481dabb098ef Improved timer implementation
Sam Lantinga <slouken@libsdl.org>
parents: 5074
diff changeset
312 SDL_AtomicUnlock(&data->lock);
481dabb098ef Improved timer implementation
Sam Lantinga <slouken@libsdl.org>
parents: 5074
diff changeset
313
481dabb098ef Improved timer implementation
Sam Lantinga <slouken@libsdl.org>
parents: 5074
diff changeset
314 if (timer) {
481dabb098ef Improved timer implementation
Sam Lantinga <slouken@libsdl.org>
parents: 5074
diff changeset
315 SDL_RemoveTimer(timer->timerID);
481dabb098ef Improved timer implementation
Sam Lantinga <slouken@libsdl.org>
parents: 5074
diff changeset
316 } else {
481dabb098ef Improved timer implementation
Sam Lantinga <slouken@libsdl.org>
parents: 5074
diff changeset
317 timer = (SDL_Timer *)SDL_malloc(sizeof(*timer));
481dabb098ef Improved timer implementation
Sam Lantinga <slouken@libsdl.org>
parents: 5074
diff changeset
318 if (!timer) {
481dabb098ef Improved timer implementation
Sam Lantinga <slouken@libsdl.org>
parents: 5074
diff changeset
319 SDL_OutOfMemory();
481dabb098ef Improved timer implementation
Sam Lantinga <slouken@libsdl.org>
parents: 5074
diff changeset
320 return 0;
481dabb098ef Improved timer implementation
Sam Lantinga <slouken@libsdl.org>
parents: 5074
diff changeset
321 }
1895
c121d94672cb SDL 1.2 is moving to a branch, and SDL 1.3 is becoming the head.
Sam Lantinga <slouken@libsdl.org>
parents: 1788
diff changeset
322 }
5113
481dabb098ef Improved timer implementation
Sam Lantinga <slouken@libsdl.org>
parents: 5074
diff changeset
323 timer->timerID = SDL_AtomicIncRef(&data->nextID);
481dabb098ef Improved timer implementation
Sam Lantinga <slouken@libsdl.org>
parents: 5074
diff changeset
324 timer->callback = callback;
481dabb098ef Improved timer implementation
Sam Lantinga <slouken@libsdl.org>
parents: 5074
diff changeset
325 timer->param = param;
481dabb098ef Improved timer implementation
Sam Lantinga <slouken@libsdl.org>
parents: 5074
diff changeset
326 timer->interval = interval;
481dabb098ef Improved timer implementation
Sam Lantinga <slouken@libsdl.org>
parents: 5074
diff changeset
327 timer->scheduled = SDL_GetTicks() + interval;
481dabb098ef Improved timer implementation
Sam Lantinga <slouken@libsdl.org>
parents: 5074
diff changeset
328 timer->canceled = SDL_FALSE;
481dabb098ef Improved timer implementation
Sam Lantinga <slouken@libsdl.org>
parents: 5074
diff changeset
329
481dabb098ef Improved timer implementation
Sam Lantinga <slouken@libsdl.org>
parents: 5074
diff changeset
330 entry = (SDL_TimerMap *)SDL_malloc(sizeof(*entry));
481dabb098ef Improved timer implementation
Sam Lantinga <slouken@libsdl.org>
parents: 5074
diff changeset
331 if (!entry) {
481dabb098ef Improved timer implementation
Sam Lantinga <slouken@libsdl.org>
parents: 5074
diff changeset
332 SDL_free(timer);
481dabb098ef Improved timer implementation
Sam Lantinga <slouken@libsdl.org>
parents: 5074
diff changeset
333 SDL_OutOfMemory();
481dabb098ef Improved timer implementation
Sam Lantinga <slouken@libsdl.org>
parents: 5074
diff changeset
334 return 0;
1895
c121d94672cb SDL 1.2 is moving to a branch, and SDL 1.3 is becoming the head.
Sam Lantinga <slouken@libsdl.org>
parents: 1788
diff changeset
335 }
5113
481dabb098ef Improved timer implementation
Sam Lantinga <slouken@libsdl.org>
parents: 5074
diff changeset
336 entry->timer = timer;
481dabb098ef Improved timer implementation
Sam Lantinga <slouken@libsdl.org>
parents: 5074
diff changeset
337 entry->timerID = timer->timerID;
481dabb098ef Improved timer implementation
Sam Lantinga <slouken@libsdl.org>
parents: 5074
diff changeset
338
481dabb098ef Improved timer implementation
Sam Lantinga <slouken@libsdl.org>
parents: 5074
diff changeset
339 SDL_mutexP(data->timermap_lock);
481dabb098ef Improved timer implementation
Sam Lantinga <slouken@libsdl.org>
parents: 5074
diff changeset
340 entry->next = data->timermap;
481dabb098ef Improved timer implementation
Sam Lantinga <slouken@libsdl.org>
parents: 5074
diff changeset
341 data->timermap = entry;
481dabb098ef Improved timer implementation
Sam Lantinga <slouken@libsdl.org>
parents: 5074
diff changeset
342 SDL_mutexV(data->timermap_lock);
481dabb098ef Improved timer implementation
Sam Lantinga <slouken@libsdl.org>
parents: 5074
diff changeset
343
481dabb098ef Improved timer implementation
Sam Lantinga <slouken@libsdl.org>
parents: 5074
diff changeset
344 /* Add the timer to the pending list for the timer thread */
481dabb098ef Improved timer implementation
Sam Lantinga <slouken@libsdl.org>
parents: 5074
diff changeset
345 SDL_AtomicLock(&data->lock);
481dabb098ef Improved timer implementation
Sam Lantinga <slouken@libsdl.org>
parents: 5074
diff changeset
346 timer->next = data->pending;
481dabb098ef Improved timer implementation
Sam Lantinga <slouken@libsdl.org>
parents: 5074
diff changeset
347 data->pending = timer;
481dabb098ef Improved timer implementation
Sam Lantinga <slouken@libsdl.org>
parents: 5074
diff changeset
348 SDL_AtomicUnlock(&data->lock);
481dabb098ef Improved timer implementation
Sam Lantinga <slouken@libsdl.org>
parents: 5074
diff changeset
349
481dabb098ef Improved timer implementation
Sam Lantinga <slouken@libsdl.org>
parents: 5074
diff changeset
350 /* Wake up the timer thread if necessary */
481dabb098ef Improved timer implementation
Sam Lantinga <slouken@libsdl.org>
parents: 5074
diff changeset
351 SDL_SemPost(data->sem);
481dabb098ef Improved timer implementation
Sam Lantinga <slouken@libsdl.org>
parents: 5074
diff changeset
352
481dabb098ef Improved timer implementation
Sam Lantinga <slouken@libsdl.org>
parents: 5074
diff changeset
353 return entry->timerID;
1
cf2af46e9e2a Changes since SDL 1.2.0 release
Sam Lantinga <slouken@lokigames.com>
parents:
diff changeset
354 }
cf2af46e9e2a Changes since SDL 1.2.0 release
Sam Lantinga <slouken@lokigames.com>
parents:
diff changeset
355
1895
c121d94672cb SDL 1.2 is moving to a branch, and SDL 1.3 is becoming the head.
Sam Lantinga <slouken@libsdl.org>
parents: 1788
diff changeset
356 SDL_bool
c121d94672cb SDL 1.2 is moving to a branch, and SDL 1.3 is becoming the head.
Sam Lantinga <slouken@libsdl.org>
parents: 1788
diff changeset
357 SDL_RemoveTimer(SDL_TimerID id)
1
cf2af46e9e2a Changes since SDL 1.2.0 release
Sam Lantinga <slouken@lokigames.com>
parents:
diff changeset
358 {
5113
481dabb098ef Improved timer implementation
Sam Lantinga <slouken@libsdl.org>
parents: 5074
diff changeset
359 SDL_TimerData *data = &SDL_timer_data;
481dabb098ef Improved timer implementation
Sam Lantinga <slouken@libsdl.org>
parents: 5074
diff changeset
360 SDL_TimerMap *prev, *entry;
481dabb098ef Improved timer implementation
Sam Lantinga <slouken@libsdl.org>
parents: 5074
diff changeset
361 SDL_bool canceled = SDL_FALSE;
1
cf2af46e9e2a Changes since SDL 1.2.0 release
Sam Lantinga <slouken@lokigames.com>
parents:
diff changeset
362
5113
481dabb098ef Improved timer implementation
Sam Lantinga <slouken@libsdl.org>
parents: 5074
diff changeset
363 /* Find the timer */
481dabb098ef Improved timer implementation
Sam Lantinga <slouken@libsdl.org>
parents: 5074
diff changeset
364 SDL_mutexP(data->timermap_lock);
481dabb098ef Improved timer implementation
Sam Lantinga <slouken@libsdl.org>
parents: 5074
diff changeset
365 prev = NULL;
481dabb098ef Improved timer implementation
Sam Lantinga <slouken@libsdl.org>
parents: 5074
diff changeset
366 for (entry = data->timermap; entry; prev = entry, entry = entry->next) {
481dabb098ef Improved timer implementation
Sam Lantinga <slouken@libsdl.org>
parents: 5074
diff changeset
367 if (entry->timerID == id) {
1895
c121d94672cb SDL 1.2 is moving to a branch, and SDL 1.3 is becoming the head.
Sam Lantinga <slouken@libsdl.org>
parents: 1788
diff changeset
368 if (prev) {
5113
481dabb098ef Improved timer implementation
Sam Lantinga <slouken@libsdl.org>
parents: 5074
diff changeset
369 prev->next = entry->next;
1895
c121d94672cb SDL 1.2 is moving to a branch, and SDL 1.3 is becoming the head.
Sam Lantinga <slouken@libsdl.org>
parents: 1788
diff changeset
370 } else {
5113
481dabb098ef Improved timer implementation
Sam Lantinga <slouken@libsdl.org>
parents: 5074
diff changeset
371 data->timermap = entry->next;
1895
c121d94672cb SDL 1.2 is moving to a branch, and SDL 1.3 is becoming the head.
Sam Lantinga <slouken@libsdl.org>
parents: 1788
diff changeset
372 }
c121d94672cb SDL 1.2 is moving to a branch, and SDL 1.3 is becoming the head.
Sam Lantinga <slouken@libsdl.org>
parents: 1788
diff changeset
373 break;
c121d94672cb SDL 1.2 is moving to a branch, and SDL 1.3 is becoming the head.
Sam Lantinga <slouken@libsdl.org>
parents: 1788
diff changeset
374 }
c121d94672cb SDL 1.2 is moving to a branch, and SDL 1.3 is becoming the head.
Sam Lantinga <slouken@libsdl.org>
parents: 1788
diff changeset
375 }
5113
481dabb098ef Improved timer implementation
Sam Lantinga <slouken@libsdl.org>
parents: 5074
diff changeset
376 SDL_mutexV(data->timermap_lock);
1
cf2af46e9e2a Changes since SDL 1.2.0 release
Sam Lantinga <slouken@lokigames.com>
parents:
diff changeset
377
5113
481dabb098ef Improved timer implementation
Sam Lantinga <slouken@libsdl.org>
parents: 5074
diff changeset
378 if (entry) {
481dabb098ef Improved timer implementation
Sam Lantinga <slouken@libsdl.org>
parents: 5074
diff changeset
379 if (!entry->timer->canceled) {
481dabb098ef Improved timer implementation
Sam Lantinga <slouken@libsdl.org>
parents: 5074
diff changeset
380 entry->timer->canceled = SDL_TRUE;
481dabb098ef Improved timer implementation
Sam Lantinga <slouken@libsdl.org>
parents: 5074
diff changeset
381 canceled = SDL_TRUE;
481dabb098ef Improved timer implementation
Sam Lantinga <slouken@libsdl.org>
parents: 5074
diff changeset
382 }
481dabb098ef Improved timer implementation
Sam Lantinga <slouken@libsdl.org>
parents: 5074
diff changeset
383 SDL_free(entry);
1895
c121d94672cb SDL 1.2 is moving to a branch, and SDL 1.3 is becoming the head.
Sam Lantinga <slouken@libsdl.org>
parents: 1788
diff changeset
384 }
5113
481dabb098ef Improved timer implementation
Sam Lantinga <slouken@libsdl.org>
parents: 5074
diff changeset
385 return canceled;
1
cf2af46e9e2a Changes since SDL 1.2.0 release
Sam Lantinga <slouken@lokigames.com>
parents:
diff changeset
386 }
1895
c121d94672cb SDL 1.2 is moving to a branch, and SDL 1.3 is becoming the head.
Sam Lantinga <slouken@libsdl.org>
parents: 1788
diff changeset
387
c121d94672cb SDL 1.2 is moving to a branch, and SDL 1.3 is becoming the head.
Sam Lantinga <slouken@libsdl.org>
parents: 1788
diff changeset
388 /* vi: set ts=4 sw=4 expandtab: */