annotate src/timer/SDL_timer.c @ 5240:1e28342cb15d

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