author | Sam Lantinga <slouken@libsdl.org> |
Tue, 30 Jul 2013 21:39:38 -0700 | |
changeset 7550 | ff3e041c27f3 |
parent 7191 | 75360622e65f |
child 7649 | 4f801cd08f3f |
permissions | -rw-r--r-- |
1
cf2af46e9e2a
Changes since SDL 1.2.0 release
Sam Lantinga <slouken@lokigames.com>
parents:
diff
changeset
|
1 |
/* |
5535
96594ac5fd1a
SDL 1.3 is now under the zlib license.
Sam Lantinga <slouken@libsdl.org>
parents:
5262
diff
changeset
|
2 |
Simple DirectMedia Layer |
6885 | 3 |
Copyright (C) 1997-2013 Sam Lantinga <slouken@libsdl.org> |
1
cf2af46e9e2a
Changes since SDL 1.2.0 release
Sam Lantinga <slouken@lokigames.com>
parents:
diff
changeset
|
4 |
|
5535
96594ac5fd1a
SDL 1.3 is now under the zlib license.
Sam Lantinga <slouken@libsdl.org>
parents:
5262
diff
changeset
|
5 |
This software is provided 'as-is', without any express or implied |
96594ac5fd1a
SDL 1.3 is now under the zlib license.
Sam Lantinga <slouken@libsdl.org>
parents:
5262
diff
changeset
|
6 |
warranty. In no event will the authors be held liable for any damages |
96594ac5fd1a
SDL 1.3 is now under the zlib license.
Sam Lantinga <slouken@libsdl.org>
parents:
5262
diff
changeset
|
7 |
arising from the use of this software. |
1
cf2af46e9e2a
Changes since SDL 1.2.0 release
Sam Lantinga <slouken@lokigames.com>
parents:
diff
changeset
|
8 |
|
5535
96594ac5fd1a
SDL 1.3 is now under the zlib license.
Sam Lantinga <slouken@libsdl.org>
parents:
5262
diff
changeset
|
9 |
Permission is granted to anyone to use this software for any purpose, |
96594ac5fd1a
SDL 1.3 is now under the zlib license.
Sam Lantinga <slouken@libsdl.org>
parents:
5262
diff
changeset
|
10 |
including commercial applications, and to alter it and redistribute it |
96594ac5fd1a
SDL 1.3 is now under the zlib license.
Sam Lantinga <slouken@libsdl.org>
parents:
5262
diff
changeset
|
11 |
freely, subject to the following restrictions: |
1
cf2af46e9e2a
Changes since SDL 1.2.0 release
Sam Lantinga <slouken@lokigames.com>
parents:
diff
changeset
|
12 |
|
5535
96594ac5fd1a
SDL 1.3 is now under the zlib license.
Sam Lantinga <slouken@libsdl.org>
parents:
5262
diff
changeset
|
13 |
1. The origin of this software must not be misrepresented; you must not |
96594ac5fd1a
SDL 1.3 is now under the zlib license.
Sam Lantinga <slouken@libsdl.org>
parents:
5262
diff
changeset
|
14 |
claim that you wrote the original software. If you use this software |
96594ac5fd1a
SDL 1.3 is now under the zlib license.
Sam Lantinga <slouken@libsdl.org>
parents:
5262
diff
changeset
|
15 |
in a product, an acknowledgment in the product documentation would be |
96594ac5fd1a
SDL 1.3 is now under the zlib license.
Sam Lantinga <slouken@libsdl.org>
parents:
5262
diff
changeset
|
16 |
appreciated but is not required. |
96594ac5fd1a
SDL 1.3 is now under the zlib license.
Sam Lantinga <slouken@libsdl.org>
parents:
5262
diff
changeset
|
17 |
2. Altered source versions must be plainly marked as such, and must not be |
96594ac5fd1a
SDL 1.3 is now under the zlib license.
Sam Lantinga <slouken@libsdl.org>
parents:
5262
diff
changeset
|
18 |
misrepresented as being the original software. |
96594ac5fd1a
SDL 1.3 is now under the zlib license.
Sam Lantinga <slouken@libsdl.org>
parents:
5262
diff
changeset
|
19 |
3. This notice may not be removed or altered from any source distribution. |
1
cf2af46e9e2a
Changes since SDL 1.2.0 release
Sam Lantinga <slouken@lokigames.com>
parents:
diff
changeset
|
20 |
*/ |
1402
d910939febfa
Use consistent identifiers for the various platforms we support.
Sam Lantinga <slouken@libsdl.org>
parents:
1358
diff
changeset
|
21 |
#include "SDL_config.h" |
1
cf2af46e9e2a
Changes since SDL 1.2.0 release
Sam Lantinga <slouken@lokigames.com>
parents:
diff
changeset
|
22 |
|
cf2af46e9e2a
Changes since SDL 1.2.0 release
Sam Lantinga <slouken@lokigames.com>
parents:
diff
changeset
|
23 |
#include "SDL_timer.h" |
cf2af46e9e2a
Changes since SDL 1.2.0 release
Sam Lantinga <slouken@lokigames.com>
parents:
diff
changeset
|
24 |
#include "SDL_timer_c.h" |
5111
481dabb098ef
Improved timer implementation
Sam Lantinga <slouken@libsdl.org>
parents:
5074
diff
changeset
|
25 |
#include "SDL_atomic.h" |
5115
427998ff3bcf
Added cache line size info in SDL_cpuinfo.h
Sam Lantinga <slouken@libsdl.org>
parents:
5114
diff
changeset
|
26 |
#include "SDL_cpuinfo.h" |
5111
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 |
|
7139
dd722e7460c5
move Ticks initialization tracking to separate function and ensure it's called with SDL_VideoInit is called to init SDL instead of SDL_Init
Edward Rudd <urkle@outoforder.cc>
parents:
6977
diff
changeset
|
29 |
extern void SDL_StartTicks(void); |
dd722e7460c5
move Ticks initialization tracking to separate function and ensure it's called with SDL_VideoInit is called to init SDL instead of SDL_Init
Edward Rudd <urkle@outoforder.cc>
parents:
6977
diff
changeset
|
30 |
|
1
cf2af46e9e2a
Changes since SDL 1.2.0 release
Sam Lantinga <slouken@lokigames.com>
parents:
diff
changeset
|
31 |
/* #define DEBUG_TIMERS */ |
cf2af46e9e2a
Changes since SDL 1.2.0 release
Sam Lantinga <slouken@lokigames.com>
parents:
diff
changeset
|
32 |
|
5111
481dabb098ef
Improved timer implementation
Sam Lantinga <slouken@libsdl.org>
parents:
5074
diff
changeset
|
33 |
typedef struct _SDL_Timer |
481dabb098ef
Improved timer implementation
Sam Lantinga <slouken@libsdl.org>
parents:
5074
diff
changeset
|
34 |
{ |
481dabb098ef
Improved timer implementation
Sam Lantinga <slouken@libsdl.org>
parents:
5074
diff
changeset
|
35 |
int timerID; |
481dabb098ef
Improved timer implementation
Sam Lantinga <slouken@libsdl.org>
parents:
5074
diff
changeset
|
36 |
SDL_TimerCallback callback; |
481dabb098ef
Improved timer implementation
Sam Lantinga <slouken@libsdl.org>
parents:
5074
diff
changeset
|
37 |
void *param; |
481dabb098ef
Improved timer implementation
Sam Lantinga <slouken@libsdl.org>
parents:
5074
diff
changeset
|
38 |
Uint32 interval; |
481dabb098ef
Improved timer implementation
Sam Lantinga <slouken@libsdl.org>
parents:
5074
diff
changeset
|
39 |
Uint32 scheduled; |
481dabb098ef
Improved timer implementation
Sam Lantinga <slouken@libsdl.org>
parents:
5074
diff
changeset
|
40 |
volatile SDL_bool canceled; |
481dabb098ef
Improved timer implementation
Sam Lantinga <slouken@libsdl.org>
parents:
5074
diff
changeset
|
41 |
struct _SDL_Timer *next; |
481dabb098ef
Improved timer implementation
Sam Lantinga <slouken@libsdl.org>
parents:
5074
diff
changeset
|
42 |
} SDL_Timer; |
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 |
typedef struct _SDL_TimerMap |
481dabb098ef
Improved timer implementation
Sam Lantinga <slouken@libsdl.org>
parents:
5074
diff
changeset
|
45 |
{ |
481dabb098ef
Improved timer implementation
Sam Lantinga <slouken@libsdl.org>
parents:
5074
diff
changeset
|
46 |
int timerID; |
481dabb098ef
Improved timer implementation
Sam Lantinga <slouken@libsdl.org>
parents:
5074
diff
changeset
|
47 |
SDL_Timer *timer; |
481dabb098ef
Improved timer implementation
Sam Lantinga <slouken@libsdl.org>
parents:
5074
diff
changeset
|
48 |
struct _SDL_TimerMap *next; |
481dabb098ef
Improved timer implementation
Sam Lantinga <slouken@libsdl.org>
parents:
5074
diff
changeset
|
49 |
} SDL_TimerMap; |
1
cf2af46e9e2a
Changes since SDL 1.2.0 release
Sam Lantinga <slouken@lokigames.com>
parents:
diff
changeset
|
50 |
|
5111
481dabb098ef
Improved timer implementation
Sam Lantinga <slouken@libsdl.org>
parents:
5074
diff
changeset
|
51 |
/* The timers are kept in a sorted list */ |
481dabb098ef
Improved timer implementation
Sam Lantinga <slouken@libsdl.org>
parents:
5074
diff
changeset
|
52 |
typedef struct { |
481dabb098ef
Improved timer implementation
Sam Lantinga <slouken@libsdl.org>
parents:
5074
diff
changeset
|
53 |
/* Data used by the main thread */ |
481dabb098ef
Improved timer implementation
Sam Lantinga <slouken@libsdl.org>
parents:
5074
diff
changeset
|
54 |
SDL_Thread *thread; |
481dabb098ef
Improved timer implementation
Sam Lantinga <slouken@libsdl.org>
parents:
5074
diff
changeset
|
55 |
SDL_atomic_t nextID; |
481dabb098ef
Improved timer implementation
Sam Lantinga <slouken@libsdl.org>
parents:
5074
diff
changeset
|
56 |
SDL_TimerMap *timermap; |
481dabb098ef
Improved timer implementation
Sam Lantinga <slouken@libsdl.org>
parents:
5074
diff
changeset
|
57 |
SDL_mutex *timermap_lock; |
481dabb098ef
Improved timer implementation
Sam Lantinga <slouken@libsdl.org>
parents:
5074
diff
changeset
|
58 |
|
481dabb098ef
Improved timer implementation
Sam Lantinga <slouken@libsdl.org>
parents:
5074
diff
changeset
|
59 |
/* Padding to separate cache lines between threads */ |
5115
427998ff3bcf
Added cache line size info in SDL_cpuinfo.h
Sam Lantinga <slouken@libsdl.org>
parents:
5114
diff
changeset
|
60 |
char cache_pad[SDL_CACHELINE_SIZE]; |
5111
481dabb098ef
Improved timer implementation
Sam Lantinga <slouken@libsdl.org>
parents:
5074
diff
changeset
|
61 |
|
481dabb098ef
Improved timer implementation
Sam Lantinga <slouken@libsdl.org>
parents:
5074
diff
changeset
|
62 |
/* Data used to communicate with the timer thread */ |
481dabb098ef
Improved timer implementation
Sam Lantinga <slouken@libsdl.org>
parents:
5074
diff
changeset
|
63 |
SDL_SpinLock lock; |
481dabb098ef
Improved timer implementation
Sam Lantinga <slouken@libsdl.org>
parents:
5074
diff
changeset
|
64 |
SDL_sem *sem; |
481dabb098ef
Improved timer implementation
Sam Lantinga <slouken@libsdl.org>
parents:
5074
diff
changeset
|
65 |
SDL_Timer * volatile pending; |
481dabb098ef
Improved timer implementation
Sam Lantinga <slouken@libsdl.org>
parents:
5074
diff
changeset
|
66 |
SDL_Timer * volatile freelist; |
481dabb098ef
Improved timer implementation
Sam Lantinga <slouken@libsdl.org>
parents:
5074
diff
changeset
|
67 |
volatile SDL_bool active; |
1
cf2af46e9e2a
Changes since SDL 1.2.0 release
Sam Lantinga <slouken@lokigames.com>
parents:
diff
changeset
|
68 |
|
5111
481dabb098ef
Improved timer implementation
Sam Lantinga <slouken@libsdl.org>
parents:
5074
diff
changeset
|
69 |
/* List of timers - this is only touched by the timer thread */ |
481dabb098ef
Improved timer implementation
Sam Lantinga <slouken@libsdl.org>
parents:
5074
diff
changeset
|
70 |
SDL_Timer *timers; |
481dabb098ef
Improved timer implementation
Sam Lantinga <slouken@libsdl.org>
parents:
5074
diff
changeset
|
71 |
} SDL_TimerData; |
481dabb098ef
Improved timer implementation
Sam Lantinga <slouken@libsdl.org>
parents:
5074
diff
changeset
|
72 |
|
481dabb098ef
Improved timer implementation
Sam Lantinga <slouken@libsdl.org>
parents:
5074
diff
changeset
|
73 |
static SDL_TimerData SDL_timer_data; |
481dabb098ef
Improved timer implementation
Sam Lantinga <slouken@libsdl.org>
parents:
5074
diff
changeset
|
74 |
|
7139
dd722e7460c5
move Ticks initialization tracking to separate function and ensure it's called with SDL_VideoInit is called to init SDL instead of SDL_Init
Edward Rudd <urkle@outoforder.cc>
parents:
6977
diff
changeset
|
75 |
static Uint32 ticks_started = 0; |
dd722e7460c5
move Ticks initialization tracking to separate function and ensure it's called with SDL_VideoInit is called to init SDL instead of SDL_Init
Edward Rudd <urkle@outoforder.cc>
parents:
6977
diff
changeset
|
76 |
|
dd722e7460c5
move Ticks initialization tracking to separate function and ensure it's called with SDL_VideoInit is called to init SDL instead of SDL_Init
Edward Rudd <urkle@outoforder.cc>
parents:
6977
diff
changeset
|
77 |
void |
dd722e7460c5
move Ticks initialization tracking to separate function and ensure it's called with SDL_VideoInit is called to init SDL instead of SDL_Init
Edward Rudd <urkle@outoforder.cc>
parents:
6977
diff
changeset
|
78 |
SDL_InitTicks(void) |
dd722e7460c5
move Ticks initialization tracking to separate function and ensure it's called with SDL_VideoInit is called to init SDL instead of SDL_Init
Edward Rudd <urkle@outoforder.cc>
parents:
6977
diff
changeset
|
79 |
{ |
dd722e7460c5
move Ticks initialization tracking to separate function and ensure it's called with SDL_VideoInit is called to init SDL instead of SDL_Init
Edward Rudd <urkle@outoforder.cc>
parents:
6977
diff
changeset
|
80 |
if (!ticks_started) { |
dd722e7460c5
move Ticks initialization tracking to separate function and ensure it's called with SDL_VideoInit is called to init SDL instead of SDL_Init
Edward Rudd <urkle@outoforder.cc>
parents:
6977
diff
changeset
|
81 |
SDL_StartTicks(); |
dd722e7460c5
move Ticks initialization tracking to separate function and ensure it's called with SDL_VideoInit is called to init SDL instead of SDL_Init
Edward Rudd <urkle@outoforder.cc>
parents:
6977
diff
changeset
|
82 |
ticks_started = 1; |
dd722e7460c5
move Ticks initialization tracking to separate function and ensure it's called with SDL_VideoInit is called to init SDL instead of SDL_Init
Edward Rudd <urkle@outoforder.cc>
parents:
6977
diff
changeset
|
83 |
} |
dd722e7460c5
move Ticks initialization tracking to separate function and ensure it's called with SDL_VideoInit is called to init SDL instead of SDL_Init
Edward Rudd <urkle@outoforder.cc>
parents:
6977
diff
changeset
|
84 |
} |
1
cf2af46e9e2a
Changes since SDL 1.2.0 release
Sam Lantinga <slouken@lokigames.com>
parents:
diff
changeset
|
85 |
|
5111
481dabb098ef
Improved timer implementation
Sam Lantinga <slouken@libsdl.org>
parents:
5074
diff
changeset
|
86 |
/* 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
|
87 |
* thread manages the active timer queue, sorted by scheduling time. |
481dabb098ef
Improved timer implementation
Sam Lantinga <slouken@libsdl.org>
parents:
5074
diff
changeset
|
88 |
* |
481dabb098ef
Improved timer implementation
Sam Lantinga <slouken@libsdl.org>
parents:
5074
diff
changeset
|
89 |
* Timers are removed by simply setting a canceled flag |
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 |
static void |
481dabb098ef
Improved timer implementation
Sam Lantinga <slouken@libsdl.org>
parents:
5074
diff
changeset
|
93 |
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
|
94 |
{ |
5111
481dabb098ef
Improved timer implementation
Sam Lantinga <slouken@libsdl.org>
parents:
5074
diff
changeset
|
95 |
SDL_Timer *prev, *curr; |
481dabb098ef
Improved timer implementation
Sam Lantinga <slouken@libsdl.org>
parents:
5074
diff
changeset
|
96 |
|
481dabb098ef
Improved timer implementation
Sam Lantinga <slouken@libsdl.org>
parents:
5074
diff
changeset
|
97 |
prev = NULL; |
481dabb098ef
Improved timer implementation
Sam Lantinga <slouken@libsdl.org>
parents:
5074
diff
changeset
|
98 |
for (curr = data->timers; curr; prev = curr, curr = curr->next) { |
481dabb098ef
Improved timer implementation
Sam Lantinga <slouken@libsdl.org>
parents:
5074
diff
changeset
|
99 |
if ((Sint32)(timer->scheduled-curr->scheduled) < 0) { |
481dabb098ef
Improved timer implementation
Sam Lantinga <slouken@libsdl.org>
parents:
5074
diff
changeset
|
100 |
break; |
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 |
} |
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 |
/* Insert the timer here! */ |
481dabb098ef
Improved timer implementation
Sam Lantinga <slouken@libsdl.org>
parents:
5074
diff
changeset
|
105 |
if (prev) { |
481dabb098ef
Improved timer implementation
Sam Lantinga <slouken@libsdl.org>
parents:
5074
diff
changeset
|
106 |
prev->next = timer; |
481dabb098ef
Improved timer implementation
Sam Lantinga <slouken@libsdl.org>
parents:
5074
diff
changeset
|
107 |
} else { |
481dabb098ef
Improved timer implementation
Sam Lantinga <slouken@libsdl.org>
parents:
5074
diff
changeset
|
108 |
data->timers = timer; |
481dabb098ef
Improved timer implementation
Sam Lantinga <slouken@libsdl.org>
parents:
5074
diff
changeset
|
109 |
} |
481dabb098ef
Improved timer implementation
Sam Lantinga <slouken@libsdl.org>
parents:
5074
diff
changeset
|
110 |
timer->next = curr; |
481dabb098ef
Improved timer implementation
Sam Lantinga <slouken@libsdl.org>
parents:
5074
diff
changeset
|
111 |
} |
481dabb098ef
Improved timer implementation
Sam Lantinga <slouken@libsdl.org>
parents:
5074
diff
changeset
|
112 |
|
481dabb098ef
Improved timer implementation
Sam Lantinga <slouken@libsdl.org>
parents:
5074
diff
changeset
|
113 |
static int |
481dabb098ef
Improved timer implementation
Sam Lantinga <slouken@libsdl.org>
parents:
5074
diff
changeset
|
114 |
SDL_TimerThread(void *_data) |
481dabb098ef
Improved timer implementation
Sam Lantinga <slouken@libsdl.org>
parents:
5074
diff
changeset
|
115 |
{ |
481dabb098ef
Improved timer implementation
Sam Lantinga <slouken@libsdl.org>
parents:
5074
diff
changeset
|
116 |
SDL_TimerData *data = (SDL_TimerData *)_data; |
481dabb098ef
Improved timer implementation
Sam Lantinga <slouken@libsdl.org>
parents:
5074
diff
changeset
|
117 |
SDL_Timer *pending; |
481dabb098ef
Improved timer implementation
Sam Lantinga <slouken@libsdl.org>
parents:
5074
diff
changeset
|
118 |
SDL_Timer *current; |
481dabb098ef
Improved timer implementation
Sam Lantinga <slouken@libsdl.org>
parents:
5074
diff
changeset
|
119 |
SDL_Timer *freelist_head = NULL; |
481dabb098ef
Improved timer implementation
Sam Lantinga <slouken@libsdl.org>
parents:
5074
diff
changeset
|
120 |
SDL_Timer *freelist_tail = NULL; |
481dabb098ef
Improved timer implementation
Sam Lantinga <slouken@libsdl.org>
parents:
5074
diff
changeset
|
121 |
Uint32 tick, now, interval, delay; |
1
cf2af46e9e2a
Changes since SDL 1.2.0 release
Sam Lantinga <slouken@lokigames.com>
parents:
diff
changeset
|
122 |
|
5111
481dabb098ef
Improved timer implementation
Sam Lantinga <slouken@libsdl.org>
parents:
5074
diff
changeset
|
123 |
/* Threaded timer loop: |
481dabb098ef
Improved timer implementation
Sam Lantinga <slouken@libsdl.org>
parents:
5074
diff
changeset
|
124 |
* 1. Queue timers added by other threads |
481dabb098ef
Improved timer implementation
Sam Lantinga <slouken@libsdl.org>
parents:
5074
diff
changeset
|
125 |
* 2. Handle any timers that should dispatch this cycle |
481dabb098ef
Improved timer implementation
Sam Lantinga <slouken@libsdl.org>
parents:
5074
diff
changeset
|
126 |
* 3. Wait until next dispatch time or new timer arrives |
481dabb098ef
Improved timer implementation
Sam Lantinga <slouken@libsdl.org>
parents:
5074
diff
changeset
|
127 |
*/ |
481dabb098ef
Improved timer implementation
Sam Lantinga <slouken@libsdl.org>
parents:
5074
diff
changeset
|
128 |
for ( ; ; ) { |
481dabb098ef
Improved timer implementation
Sam Lantinga <slouken@libsdl.org>
parents:
5074
diff
changeset
|
129 |
/* Pending and freelist maintenance */ |
481dabb098ef
Improved timer implementation
Sam Lantinga <slouken@libsdl.org>
parents:
5074
diff
changeset
|
130 |
SDL_AtomicLock(&data->lock); |
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 |
/* Get any timers ready to be queued */ |
481dabb098ef
Improved timer implementation
Sam Lantinga <slouken@libsdl.org>
parents:
5074
diff
changeset
|
133 |
pending = data->pending; |
481dabb098ef
Improved timer implementation
Sam Lantinga <slouken@libsdl.org>
parents:
5074
diff
changeset
|
134 |
data->pending = NULL; |
481dabb098ef
Improved timer implementation
Sam Lantinga <slouken@libsdl.org>
parents:
5074
diff
changeset
|
135 |
|
481dabb098ef
Improved timer implementation
Sam Lantinga <slouken@libsdl.org>
parents:
5074
diff
changeset
|
136 |
/* Make any unused timer structures available */ |
481dabb098ef
Improved timer implementation
Sam Lantinga <slouken@libsdl.org>
parents:
5074
diff
changeset
|
137 |
if (freelist_head) { |
481dabb098ef
Improved timer implementation
Sam Lantinga <slouken@libsdl.org>
parents:
5074
diff
changeset
|
138 |
freelist_tail->next = data->freelist; |
481dabb098ef
Improved timer implementation
Sam Lantinga <slouken@libsdl.org>
parents:
5074
diff
changeset
|
139 |
data->freelist = freelist_head; |
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 |
} |
481dabb098ef
Improved timer implementation
Sam Lantinga <slouken@libsdl.org>
parents:
5074
diff
changeset
|
142 |
SDL_AtomicUnlock(&data->lock); |
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 |
/* Sort the pending timers into our list */ |
481dabb098ef
Improved timer implementation
Sam Lantinga <slouken@libsdl.org>
parents:
5074
diff
changeset
|
145 |
while (pending) { |
481dabb098ef
Improved timer implementation
Sam Lantinga <slouken@libsdl.org>
parents:
5074
diff
changeset
|
146 |
current = pending; |
481dabb098ef
Improved timer implementation
Sam Lantinga <slouken@libsdl.org>
parents:
5074
diff
changeset
|
147 |
pending = pending->next; |
481dabb098ef
Improved timer implementation
Sam Lantinga <slouken@libsdl.org>
parents:
5074
diff
changeset
|
148 |
SDL_AddTimerInternal(data, current); |
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 |
freelist_head = NULL; |
481dabb098ef
Improved timer implementation
Sam Lantinga <slouken@libsdl.org>
parents:
5074
diff
changeset
|
151 |
freelist_tail = NULL; |
481dabb098ef
Improved timer implementation
Sam Lantinga <slouken@libsdl.org>
parents:
5074
diff
changeset
|
152 |
|
481dabb098ef
Improved timer implementation
Sam Lantinga <slouken@libsdl.org>
parents:
5074
diff
changeset
|
153 |
/* Check to see if we're still running, after maintenance */ |
481dabb098ef
Improved timer implementation
Sam Lantinga <slouken@libsdl.org>
parents:
5074
diff
changeset
|
154 |
if (!data->active) { |
481dabb098ef
Improved timer implementation
Sam Lantinga <slouken@libsdl.org>
parents:
5074
diff
changeset
|
155 |
break; |
481dabb098ef
Improved timer implementation
Sam Lantinga <slouken@libsdl.org>
parents:
5074
diff
changeset
|
156 |
} |
481dabb098ef
Improved timer implementation
Sam Lantinga <slouken@libsdl.org>
parents:
5074
diff
changeset
|
157 |
|
481dabb098ef
Improved timer implementation
Sam Lantinga <slouken@libsdl.org>
parents:
5074
diff
changeset
|
158 |
/* Initial delay if there are no timers */ |
481dabb098ef
Improved timer implementation
Sam Lantinga <slouken@libsdl.org>
parents:
5074
diff
changeset
|
159 |
delay = SDL_MUTEX_MAXWAIT; |
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 |
tick = SDL_GetTicks(); |
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 |
/* Process all the pending timers for this tick */ |
481dabb098ef
Improved timer implementation
Sam Lantinga <slouken@libsdl.org>
parents:
5074
diff
changeset
|
164 |
while (data->timers) { |
481dabb098ef
Improved timer implementation
Sam Lantinga <slouken@libsdl.org>
parents:
5074
diff
changeset
|
165 |
current = data->timers; |
1
cf2af46e9e2a
Changes since SDL 1.2.0 release
Sam Lantinga <slouken@lokigames.com>
parents:
diff
changeset
|
166 |
|
5111
481dabb098ef
Improved timer implementation
Sam Lantinga <slouken@libsdl.org>
parents:
5074
diff
changeset
|
167 |
if ((Sint32)(tick-current->scheduled) < 0) { |
481dabb098ef
Improved timer implementation
Sam Lantinga <slouken@libsdl.org>
parents:
5074
diff
changeset
|
168 |
/* Scheduled for the future, wait a bit */ |
481dabb098ef
Improved timer implementation
Sam Lantinga <slouken@libsdl.org>
parents:
5074
diff
changeset
|
169 |
delay = (current->scheduled - tick); |
481dabb098ef
Improved timer implementation
Sam Lantinga <slouken@libsdl.org>
parents:
5074
diff
changeset
|
170 |
break; |
481dabb098ef
Improved timer implementation
Sam Lantinga <slouken@libsdl.org>
parents:
5074
diff
changeset
|
171 |
} |
481dabb098ef
Improved timer implementation
Sam Lantinga <slouken@libsdl.org>
parents:
5074
diff
changeset
|
172 |
|
481dabb098ef
Improved timer implementation
Sam Lantinga <slouken@libsdl.org>
parents:
5074
diff
changeset
|
173 |
/* We're going to do something with this timer */ |
481dabb098ef
Improved timer implementation
Sam Lantinga <slouken@libsdl.org>
parents:
5074
diff
changeset
|
174 |
data->timers = current->next; |
481dabb098ef
Improved timer implementation
Sam Lantinga <slouken@libsdl.org>
parents:
5074
diff
changeset
|
175 |
|
481dabb098ef
Improved timer implementation
Sam Lantinga <slouken@libsdl.org>
parents:
5074
diff
changeset
|
176 |
if (current->canceled) { |
481dabb098ef
Improved timer implementation
Sam Lantinga <slouken@libsdl.org>
parents:
5074
diff
changeset
|
177 |
interval = 0; |
481dabb098ef
Improved timer implementation
Sam Lantinga <slouken@libsdl.org>
parents:
5074
diff
changeset
|
178 |
} else { |
481dabb098ef
Improved timer implementation
Sam Lantinga <slouken@libsdl.org>
parents:
5074
diff
changeset
|
179 |
interval = current->callback(current->interval, current->param); |
481dabb098ef
Improved timer implementation
Sam Lantinga <slouken@libsdl.org>
parents:
5074
diff
changeset
|
180 |
} |
1
cf2af46e9e2a
Changes since SDL 1.2.0 release
Sam Lantinga <slouken@lokigames.com>
parents:
diff
changeset
|
181 |
|
5111
481dabb098ef
Improved timer implementation
Sam Lantinga <slouken@libsdl.org>
parents:
5074
diff
changeset
|
182 |
if (interval > 0) { |
481dabb098ef
Improved timer implementation
Sam Lantinga <slouken@libsdl.org>
parents:
5074
diff
changeset
|
183 |
/* Reschedule this timer */ |
481dabb098ef
Improved timer implementation
Sam Lantinga <slouken@libsdl.org>
parents:
5074
diff
changeset
|
184 |
current->scheduled = tick + interval; |
481dabb098ef
Improved timer implementation
Sam Lantinga <slouken@libsdl.org>
parents:
5074
diff
changeset
|
185 |
SDL_AddTimerInternal(data, current); |
481dabb098ef
Improved timer implementation
Sam Lantinga <slouken@libsdl.org>
parents:
5074
diff
changeset
|
186 |
} else { |
481dabb098ef
Improved timer implementation
Sam Lantinga <slouken@libsdl.org>
parents:
5074
diff
changeset
|
187 |
if (!freelist_head) { |
481dabb098ef
Improved timer implementation
Sam Lantinga <slouken@libsdl.org>
parents:
5074
diff
changeset
|
188 |
freelist_head = current; |
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 |
if (freelist_tail) { |
481dabb098ef
Improved timer implementation
Sam Lantinga <slouken@libsdl.org>
parents:
5074
diff
changeset
|
191 |
freelist_tail->next = current; |
481dabb098ef
Improved timer implementation
Sam Lantinga <slouken@libsdl.org>
parents:
5074
diff
changeset
|
192 |
} |
481dabb098ef
Improved timer implementation
Sam Lantinga <slouken@libsdl.org>
parents:
5074
diff
changeset
|
193 |
freelist_tail = current; |
481dabb098ef
Improved timer implementation
Sam Lantinga <slouken@libsdl.org>
parents:
5074
diff
changeset
|
194 |
|
481dabb098ef
Improved timer implementation
Sam Lantinga <slouken@libsdl.org>
parents:
5074
diff
changeset
|
195 |
current->canceled = SDL_TRUE; |
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 |
} |
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 |
/* Adjust the delay based on processing time */ |
481dabb098ef
Improved timer implementation
Sam Lantinga <slouken@libsdl.org>
parents:
5074
diff
changeset
|
200 |
now = SDL_GetTicks(); |
481dabb098ef
Improved timer implementation
Sam Lantinga <slouken@libsdl.org>
parents:
5074
diff
changeset
|
201 |
interval = (now - tick); |
481dabb098ef
Improved timer implementation
Sam Lantinga <slouken@libsdl.org>
parents:
5074
diff
changeset
|
202 |
if (interval > delay) { |
481dabb098ef
Improved timer implementation
Sam Lantinga <slouken@libsdl.org>
parents:
5074
diff
changeset
|
203 |
delay = 0; |
481dabb098ef
Improved timer implementation
Sam Lantinga <slouken@libsdl.org>
parents:
5074
diff
changeset
|
204 |
} else { |
481dabb098ef
Improved timer implementation
Sam Lantinga <slouken@libsdl.org>
parents:
5074
diff
changeset
|
205 |
delay -= interval; |
481dabb098ef
Improved timer implementation
Sam Lantinga <slouken@libsdl.org>
parents:
5074
diff
changeset
|
206 |
} |
481dabb098ef
Improved timer implementation
Sam Lantinga <slouken@libsdl.org>
parents:
5074
diff
changeset
|
207 |
|
481dabb098ef
Improved timer implementation
Sam Lantinga <slouken@libsdl.org>
parents:
5074
diff
changeset
|
208 |
/* Note that each time a timer is added, this will return |
481dabb098ef
Improved timer implementation
Sam Lantinga <slouken@libsdl.org>
parents:
5074
diff
changeset
|
209 |
immediately, but we process the timers added all at once. |
481dabb098ef
Improved timer implementation
Sam Lantinga <slouken@libsdl.org>
parents:
5074
diff
changeset
|
210 |
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
|
211 |
extra times. |
481dabb098ef
Improved timer implementation
Sam Lantinga <slouken@libsdl.org>
parents:
5074
diff
changeset
|
212 |
*/ |
481dabb098ef
Improved timer implementation
Sam Lantinga <slouken@libsdl.org>
parents:
5074
diff
changeset
|
213 |
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
|
214 |
} |
5111
481dabb098ef
Improved timer implementation
Sam Lantinga <slouken@libsdl.org>
parents:
5074
diff
changeset
|
215 |
return 0; |
1
cf2af46e9e2a
Changes since SDL 1.2.0 release
Sam Lantinga <slouken@lokigames.com>
parents:
diff
changeset
|
216 |
} |
cf2af46e9e2a
Changes since SDL 1.2.0 release
Sam Lantinga <slouken@lokigames.com>
parents:
diff
changeset
|
217 |
|
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
|
218 |
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
|
219 |
SDL_TimerInit(void) |
1
cf2af46e9e2a
Changes since SDL 1.2.0 release
Sam Lantinga <slouken@lokigames.com>
parents:
diff
changeset
|
220 |
{ |
5111
481dabb098ef
Improved timer implementation
Sam Lantinga <slouken@libsdl.org>
parents:
5074
diff
changeset
|
221 |
SDL_TimerData *data = &SDL_timer_data; |
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 |
if (!data->active) { |
5969
3a041d215edc
1.3 API CHANGE: Add support for naming threads.
Ryan C. Gordon <icculus@icculus.org>
parents:
5535
diff
changeset
|
224 |
const char *name = "SDLTimer"; |
5111
481dabb098ef
Improved timer implementation
Sam Lantinga <slouken@libsdl.org>
parents:
5074
diff
changeset
|
225 |
data->timermap_lock = SDL_CreateMutex(); |
481dabb098ef
Improved timer implementation
Sam Lantinga <slouken@libsdl.org>
parents:
5074
diff
changeset
|
226 |
if (!data->timermap_lock) { |
481dabb098ef
Improved timer implementation
Sam Lantinga <slouken@libsdl.org>
parents:
5074
diff
changeset
|
227 |
return -1; |
481dabb098ef
Improved timer implementation
Sam Lantinga <slouken@libsdl.org>
parents:
5074
diff
changeset
|
228 |
} |
1
cf2af46e9e2a
Changes since SDL 1.2.0 release
Sam Lantinga <slouken@lokigames.com>
parents:
diff
changeset
|
229 |
|
5111
481dabb098ef
Improved timer implementation
Sam Lantinga <slouken@libsdl.org>
parents:
5074
diff
changeset
|
230 |
data->sem = SDL_CreateSemaphore(0); |
481dabb098ef
Improved timer implementation
Sam Lantinga <slouken@libsdl.org>
parents:
5074
diff
changeset
|
231 |
if (!data->sem) { |
481dabb098ef
Improved timer implementation
Sam Lantinga <slouken@libsdl.org>
parents:
5074
diff
changeset
|
232 |
SDL_DestroyMutex(data->timermap_lock); |
481dabb098ef
Improved timer implementation
Sam Lantinga <slouken@libsdl.org>
parents:
5074
diff
changeset
|
233 |
return -1; |
481dabb098ef
Improved timer implementation
Sam Lantinga <slouken@libsdl.org>
parents:
5074
diff
changeset
|
234 |
} |
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 |
data->active = SDL_TRUE; |
5114
e337f792c6a7
Fixed compiling under Visual Studio
Sam Lantinga <slouken@libsdl.org>
parents:
5111
diff
changeset
|
237 |
/* !!! FIXME: this is nasty. */ |
6430
48d519500f7e
Removed Windows CE support from SDL 2.0.
Ryan C. Gordon <icculus@icculus.org>
parents:
6138
diff
changeset
|
238 |
#if defined(__WIN32__) && !defined(HAVE_LIBC) |
5114
e337f792c6a7
Fixed compiling under Visual Studio
Sam Lantinga <slouken@libsdl.org>
parents:
5111
diff
changeset
|
239 |
#undef SDL_CreateThread |
5969
3a041d215edc
1.3 API CHANGE: Add support for naming threads.
Ryan C. Gordon <icculus@icculus.org>
parents:
5535
diff
changeset
|
240 |
data->thread = SDL_CreateThread(SDL_TimerThread, name, data, NULL, NULL); |
5114
e337f792c6a7
Fixed compiling under Visual Studio
Sam Lantinga <slouken@libsdl.org>
parents:
5111
diff
changeset
|
241 |
#else |
5969
3a041d215edc
1.3 API CHANGE: Add support for naming threads.
Ryan C. Gordon <icculus@icculus.org>
parents:
5535
diff
changeset
|
242 |
data->thread = SDL_CreateThread(SDL_TimerThread, name, data); |
5114
e337f792c6a7
Fixed compiling under Visual Studio
Sam Lantinga <slouken@libsdl.org>
parents:
5111
diff
changeset
|
243 |
#endif |
5111
481dabb098ef
Improved timer implementation
Sam Lantinga <slouken@libsdl.org>
parents:
5074
diff
changeset
|
244 |
if (!data->thread) { |
481dabb098ef
Improved timer implementation
Sam Lantinga <slouken@libsdl.org>
parents:
5074
diff
changeset
|
245 |
SDL_TimerQuit(); |
481dabb098ef
Improved timer implementation
Sam Lantinga <slouken@libsdl.org>
parents:
5074
diff
changeset
|
246 |
return -1; |
481dabb098ef
Improved timer implementation
Sam Lantinga <slouken@libsdl.org>
parents:
5074
diff
changeset
|
247 |
} |
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 |
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
|
250 |
} |
5111
481dabb098ef
Improved timer implementation
Sam Lantinga <slouken@libsdl.org>
parents:
5074
diff
changeset
|
251 |
return 0; |
1
cf2af46e9e2a
Changes since SDL 1.2.0 release
Sam Lantinga <slouken@lokigames.com>
parents:
diff
changeset
|
252 |
} |
cf2af46e9e2a
Changes since SDL 1.2.0 release
Sam Lantinga <slouken@lokigames.com>
parents:
diff
changeset
|
253 |
|
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
|
254 |
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
|
255 |
SDL_TimerQuit(void) |
1
cf2af46e9e2a
Changes since SDL 1.2.0 release
Sam Lantinga <slouken@lokigames.com>
parents:
diff
changeset
|
256 |
{ |
5111
481dabb098ef
Improved timer implementation
Sam Lantinga <slouken@libsdl.org>
parents:
5074
diff
changeset
|
257 |
SDL_TimerData *data = &SDL_timer_data; |
481dabb098ef
Improved timer implementation
Sam Lantinga <slouken@libsdl.org>
parents:
5074
diff
changeset
|
258 |
SDL_Timer *timer; |
481dabb098ef
Improved timer implementation
Sam Lantinga <slouken@libsdl.org>
parents:
5074
diff
changeset
|
259 |
SDL_TimerMap *entry; |
481dabb098ef
Improved timer implementation
Sam Lantinga <slouken@libsdl.org>
parents:
5074
diff
changeset
|
260 |
|
481dabb098ef
Improved timer implementation
Sam Lantinga <slouken@libsdl.org>
parents:
5074
diff
changeset
|
261 |
if (data->active) { |
481dabb098ef
Improved timer implementation
Sam Lantinga <slouken@libsdl.org>
parents:
5074
diff
changeset
|
262 |
data->active = SDL_FALSE; |
1
cf2af46e9e2a
Changes since SDL 1.2.0 release
Sam Lantinga <slouken@lokigames.com>
parents:
diff
changeset
|
263 |
|
5111
481dabb098ef
Improved timer implementation
Sam Lantinga <slouken@libsdl.org>
parents:
5074
diff
changeset
|
264 |
/* Shutdown the timer thread */ |
481dabb098ef
Improved timer implementation
Sam Lantinga <slouken@libsdl.org>
parents:
5074
diff
changeset
|
265 |
if (data->thread) { |
481dabb098ef
Improved timer implementation
Sam Lantinga <slouken@libsdl.org>
parents:
5074
diff
changeset
|
266 |
SDL_SemPost(data->sem); |
481dabb098ef
Improved timer implementation
Sam Lantinga <slouken@libsdl.org>
parents:
5074
diff
changeset
|
267 |
SDL_WaitThread(data->thread, NULL); |
481dabb098ef
Improved timer implementation
Sam Lantinga <slouken@libsdl.org>
parents:
5074
diff
changeset
|
268 |
data->thread = NULL; |
481dabb098ef
Improved timer implementation
Sam Lantinga <slouken@libsdl.org>
parents:
5074
diff
changeset
|
269 |
} |
5074
906d7293bb47
Fixed bug in timer when the list of timers changed.
Sam Lantinga <slouken@libsdl.org>
parents:
3697
diff
changeset
|
270 |
|
5111
481dabb098ef
Improved timer implementation
Sam Lantinga <slouken@libsdl.org>
parents:
5074
diff
changeset
|
271 |
SDL_DestroySemaphore(data->sem); |
481dabb098ef
Improved timer implementation
Sam Lantinga <slouken@libsdl.org>
parents:
5074
diff
changeset
|
272 |
data->sem = NULL; |
1028
5ba65305c954
Fix various problems with the timer code.
Sam Lantinga <slouken@libsdl.org>
parents:
1023
diff
changeset
|
273 |
|
5111
481dabb098ef
Improved timer implementation
Sam Lantinga <slouken@libsdl.org>
parents:
5074
diff
changeset
|
274 |
/* Clean up the timer entries */ |
481dabb098ef
Improved timer implementation
Sam Lantinga <slouken@libsdl.org>
parents:
5074
diff
changeset
|
275 |
while (data->timers) { |
481dabb098ef
Improved timer implementation
Sam Lantinga <slouken@libsdl.org>
parents:
5074
diff
changeset
|
276 |
timer = data->timers; |
481dabb098ef
Improved timer implementation
Sam Lantinga <slouken@libsdl.org>
parents:
5074
diff
changeset
|
277 |
data->timers = timer->next; |
481dabb098ef
Improved timer implementation
Sam Lantinga <slouken@libsdl.org>
parents:
5074
diff
changeset
|
278 |
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
|
279 |
} |
5111
481dabb098ef
Improved timer implementation
Sam Lantinga <slouken@libsdl.org>
parents:
5074
diff
changeset
|
280 |
while (data->freelist) { |
481dabb098ef
Improved timer implementation
Sam Lantinga <slouken@libsdl.org>
parents:
5074
diff
changeset
|
281 |
timer = data->freelist; |
481dabb098ef
Improved timer implementation
Sam Lantinga <slouken@libsdl.org>
parents:
5074
diff
changeset
|
282 |
data->freelist = timer->next; |
481dabb098ef
Improved timer implementation
Sam Lantinga <slouken@libsdl.org>
parents:
5074
diff
changeset
|
283 |
SDL_free(timer); |
481dabb098ef
Improved timer implementation
Sam Lantinga <slouken@libsdl.org>
parents:
5074
diff
changeset
|
284 |
} |
481dabb098ef
Improved timer implementation
Sam Lantinga <slouken@libsdl.org>
parents:
5074
diff
changeset
|
285 |
while (data->timermap) { |
481dabb098ef
Improved timer implementation
Sam Lantinga <slouken@libsdl.org>
parents:
5074
diff
changeset
|
286 |
entry = data->timermap; |
481dabb098ef
Improved timer implementation
Sam Lantinga <slouken@libsdl.org>
parents:
5074
diff
changeset
|
287 |
data->timermap = entry->next; |
481dabb098ef
Improved timer implementation
Sam Lantinga <slouken@libsdl.org>
parents:
5074
diff
changeset
|
288 |
SDL_free(entry); |
481dabb098ef
Improved timer implementation
Sam Lantinga <slouken@libsdl.org>
parents:
5074
diff
changeset
|
289 |
} |
1
cf2af46e9e2a
Changes since SDL 1.2.0 release
Sam Lantinga <slouken@lokigames.com>
parents:
diff
changeset
|
290 |
|
5111
481dabb098ef
Improved timer implementation
Sam Lantinga <slouken@libsdl.org>
parents:
5074
diff
changeset
|
291 |
SDL_DestroyMutex(data->timermap_lock); |
481dabb098ef
Improved timer implementation
Sam Lantinga <slouken@libsdl.org>
parents:
5074
diff
changeset
|
292 |
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
|
293 |
} |
1028
5ba65305c954
Fix various problems with the timer code.
Sam Lantinga <slouken@libsdl.org>
parents:
1023
diff
changeset
|
294 |
} |
5ba65305c954
Fix various problems with the timer code.
Sam Lantinga <slouken@libsdl.org>
parents:
1023
diff
changeset
|
295 |
|
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
|
296 |
SDL_TimerID |
5111
481dabb098ef
Improved timer implementation
Sam Lantinga <slouken@libsdl.org>
parents:
5074
diff
changeset
|
297 |
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
|
298 |
{ |
5111
481dabb098ef
Improved timer implementation
Sam Lantinga <slouken@libsdl.org>
parents:
5074
diff
changeset
|
299 |
SDL_TimerData *data = &SDL_timer_data; |
481dabb098ef
Improved timer implementation
Sam Lantinga <slouken@libsdl.org>
parents:
5074
diff
changeset
|
300 |
SDL_Timer *timer; |
481dabb098ef
Improved timer implementation
Sam Lantinga <slouken@libsdl.org>
parents:
5074
diff
changeset
|
301 |
SDL_TimerMap *entry; |
481dabb098ef
Improved timer implementation
Sam Lantinga <slouken@libsdl.org>
parents:
5074
diff
changeset
|
302 |
|
481dabb098ef
Improved timer implementation
Sam Lantinga <slouken@libsdl.org>
parents:
5074
diff
changeset
|
303 |
if (!data->active) { |
481dabb098ef
Improved timer implementation
Sam Lantinga <slouken@libsdl.org>
parents:
5074
diff
changeset
|
304 |
int status = 0; |
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 |
SDL_AtomicLock(&data->lock); |
481dabb098ef
Improved timer implementation
Sam Lantinga <slouken@libsdl.org>
parents:
5074
diff
changeset
|
307 |
if (!data->active) { |
481dabb098ef
Improved timer implementation
Sam Lantinga <slouken@libsdl.org>
parents:
5074
diff
changeset
|
308 |
status = SDL_TimerInit(); |
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 (status < 0) { |
481dabb098ef
Improved timer implementation
Sam Lantinga <slouken@libsdl.org>
parents:
5074
diff
changeset
|
313 |
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
|
314 |
} |
5111
481dabb098ef
Improved timer implementation
Sam Lantinga <slouken@libsdl.org>
parents:
5074
diff
changeset
|
315 |
} |
481dabb098ef
Improved timer implementation
Sam Lantinga <slouken@libsdl.org>
parents:
5074
diff
changeset
|
316 |
|
481dabb098ef
Improved timer implementation
Sam Lantinga <slouken@libsdl.org>
parents:
5074
diff
changeset
|
317 |
SDL_AtomicLock(&data->lock); |
481dabb098ef
Improved timer implementation
Sam Lantinga <slouken@libsdl.org>
parents:
5074
diff
changeset
|
318 |
timer = data->freelist; |
481dabb098ef
Improved timer implementation
Sam Lantinga <slouken@libsdl.org>
parents:
5074
diff
changeset
|
319 |
if (timer) { |
481dabb098ef
Improved timer implementation
Sam Lantinga <slouken@libsdl.org>
parents:
5074
diff
changeset
|
320 |
data->freelist = timer->next; |
481dabb098ef
Improved timer implementation
Sam Lantinga <slouken@libsdl.org>
parents:
5074
diff
changeset
|
321 |
} |
481dabb098ef
Improved timer implementation
Sam Lantinga <slouken@libsdl.org>
parents:
5074
diff
changeset
|
322 |
SDL_AtomicUnlock(&data->lock); |
481dabb098ef
Improved timer implementation
Sam Lantinga <slouken@libsdl.org>
parents:
5074
diff
changeset
|
323 |
|
481dabb098ef
Improved timer implementation
Sam Lantinga <slouken@libsdl.org>
parents:
5074
diff
changeset
|
324 |
if (timer) { |
481dabb098ef
Improved timer implementation
Sam Lantinga <slouken@libsdl.org>
parents:
5074
diff
changeset
|
325 |
SDL_RemoveTimer(timer->timerID); |
481dabb098ef
Improved timer implementation
Sam Lantinga <slouken@libsdl.org>
parents:
5074
diff
changeset
|
326 |
} else { |
481dabb098ef
Improved timer implementation
Sam Lantinga <slouken@libsdl.org>
parents:
5074
diff
changeset
|
327 |
timer = (SDL_Timer *)SDL_malloc(sizeof(*timer)); |
481dabb098ef
Improved timer implementation
Sam Lantinga <slouken@libsdl.org>
parents:
5074
diff
changeset
|
328 |
if (!timer) { |
481dabb098ef
Improved timer implementation
Sam Lantinga <slouken@libsdl.org>
parents:
5074
diff
changeset
|
329 |
SDL_OutOfMemory(); |
481dabb098ef
Improved timer implementation
Sam Lantinga <slouken@libsdl.org>
parents:
5074
diff
changeset
|
330 |
return 0; |
481dabb098ef
Improved timer implementation
Sam Lantinga <slouken@libsdl.org>
parents:
5074
diff
changeset
|
331 |
} |
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
|
332 |
} |
5111
481dabb098ef
Improved timer implementation
Sam Lantinga <slouken@libsdl.org>
parents:
5074
diff
changeset
|
333 |
timer->timerID = SDL_AtomicIncRef(&data->nextID); |
481dabb098ef
Improved timer implementation
Sam Lantinga <slouken@libsdl.org>
parents:
5074
diff
changeset
|
334 |
timer->callback = callback; |
481dabb098ef
Improved timer implementation
Sam Lantinga <slouken@libsdl.org>
parents:
5074
diff
changeset
|
335 |
timer->param = param; |
481dabb098ef
Improved timer implementation
Sam Lantinga <slouken@libsdl.org>
parents:
5074
diff
changeset
|
336 |
timer->interval = interval; |
481dabb098ef
Improved timer implementation
Sam Lantinga <slouken@libsdl.org>
parents:
5074
diff
changeset
|
337 |
timer->scheduled = SDL_GetTicks() + interval; |
481dabb098ef
Improved timer implementation
Sam Lantinga <slouken@libsdl.org>
parents:
5074
diff
changeset
|
338 |
timer->canceled = SDL_FALSE; |
7191
75360622e65f
File style cleanup for the SDL 2.0 release
Sam Lantinga <slouken@libsdl.org>
parents:
7139
diff
changeset
|
339 |
|
5111
481dabb098ef
Improved timer implementation
Sam Lantinga <slouken@libsdl.org>
parents:
5074
diff
changeset
|
340 |
entry = (SDL_TimerMap *)SDL_malloc(sizeof(*entry)); |
481dabb098ef
Improved timer implementation
Sam Lantinga <slouken@libsdl.org>
parents:
5074
diff
changeset
|
341 |
if (!entry) { |
481dabb098ef
Improved timer implementation
Sam Lantinga <slouken@libsdl.org>
parents:
5074
diff
changeset
|
342 |
SDL_free(timer); |
481dabb098ef
Improved timer implementation
Sam Lantinga <slouken@libsdl.org>
parents:
5074
diff
changeset
|
343 |
SDL_OutOfMemory(); |
481dabb098ef
Improved timer implementation
Sam Lantinga <slouken@libsdl.org>
parents:
5074
diff
changeset
|
344 |
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
|
345 |
} |
5111
481dabb098ef
Improved timer implementation
Sam Lantinga <slouken@libsdl.org>
parents:
5074
diff
changeset
|
346 |
entry->timer = timer; |
481dabb098ef
Improved timer implementation
Sam Lantinga <slouken@libsdl.org>
parents:
5074
diff
changeset
|
347 |
entry->timerID = timer->timerID; |
481dabb098ef
Improved timer implementation
Sam Lantinga <slouken@libsdl.org>
parents:
5074
diff
changeset
|
348 |
|
6977
b73d51026c68
Changed the name of SDL_mutexP() SDL_mutexV()
Sam Lantinga <slouken@libsdl.org>
parents:
6885
diff
changeset
|
349 |
SDL_LockMutex(data->timermap_lock); |
5111
481dabb098ef
Improved timer implementation
Sam Lantinga <slouken@libsdl.org>
parents:
5074
diff
changeset
|
350 |
entry->next = data->timermap; |
481dabb098ef
Improved timer implementation
Sam Lantinga <slouken@libsdl.org>
parents:
5074
diff
changeset
|
351 |
data->timermap = entry; |
6977
b73d51026c68
Changed the name of SDL_mutexP() SDL_mutexV()
Sam Lantinga <slouken@libsdl.org>
parents:
6885
diff
changeset
|
352 |
SDL_UnlockMutex(data->timermap_lock); |
5111
481dabb098ef
Improved timer implementation
Sam Lantinga <slouken@libsdl.org>
parents:
5074
diff
changeset
|
353 |
|
481dabb098ef
Improved timer implementation
Sam Lantinga <slouken@libsdl.org>
parents:
5074
diff
changeset
|
354 |
/* Add the timer to the pending list for the timer thread */ |
481dabb098ef
Improved timer implementation
Sam Lantinga <slouken@libsdl.org>
parents:
5074
diff
changeset
|
355 |
SDL_AtomicLock(&data->lock); |
481dabb098ef
Improved timer implementation
Sam Lantinga <slouken@libsdl.org>
parents:
5074
diff
changeset
|
356 |
timer->next = data->pending; |
481dabb098ef
Improved timer implementation
Sam Lantinga <slouken@libsdl.org>
parents:
5074
diff
changeset
|
357 |
data->pending = timer; |
481dabb098ef
Improved timer implementation
Sam Lantinga <slouken@libsdl.org>
parents:
5074
diff
changeset
|
358 |
SDL_AtomicUnlock(&data->lock); |
481dabb098ef
Improved timer implementation
Sam Lantinga <slouken@libsdl.org>
parents:
5074
diff
changeset
|
359 |
|
481dabb098ef
Improved timer implementation
Sam Lantinga <slouken@libsdl.org>
parents:
5074
diff
changeset
|
360 |
/* Wake up the timer thread if necessary */ |
481dabb098ef
Improved timer implementation
Sam Lantinga <slouken@libsdl.org>
parents:
5074
diff
changeset
|
361 |
SDL_SemPost(data->sem); |
481dabb098ef
Improved timer implementation
Sam Lantinga <slouken@libsdl.org>
parents:
5074
diff
changeset
|
362 |
|
481dabb098ef
Improved timer implementation
Sam Lantinga <slouken@libsdl.org>
parents:
5074
diff
changeset
|
363 |
return entry->timerID; |
1
cf2af46e9e2a
Changes since SDL 1.2.0 release
Sam Lantinga <slouken@lokigames.com>
parents:
diff
changeset
|
364 |
} |
cf2af46e9e2a
Changes since SDL 1.2.0 release
Sam Lantinga <slouken@lokigames.com>
parents:
diff
changeset
|
365 |
|
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 |
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
|
367 |
SDL_RemoveTimer(SDL_TimerID id) |
1
cf2af46e9e2a
Changes since SDL 1.2.0 release
Sam Lantinga <slouken@lokigames.com>
parents:
diff
changeset
|
368 |
{ |
5111
481dabb098ef
Improved timer implementation
Sam Lantinga <slouken@libsdl.org>
parents:
5074
diff
changeset
|
369 |
SDL_TimerData *data = &SDL_timer_data; |
481dabb098ef
Improved timer implementation
Sam Lantinga <slouken@libsdl.org>
parents:
5074
diff
changeset
|
370 |
SDL_TimerMap *prev, *entry; |
481dabb098ef
Improved timer implementation
Sam Lantinga <slouken@libsdl.org>
parents:
5074
diff
changeset
|
371 |
SDL_bool canceled = SDL_FALSE; |
1
cf2af46e9e2a
Changes since SDL 1.2.0 release
Sam Lantinga <slouken@lokigames.com>
parents:
diff
changeset
|
372 |
|
5111
481dabb098ef
Improved timer implementation
Sam Lantinga <slouken@libsdl.org>
parents:
5074
diff
changeset
|
373 |
/* Find the timer */ |
6977
b73d51026c68
Changed the name of SDL_mutexP() SDL_mutexV()
Sam Lantinga <slouken@libsdl.org>
parents:
6885
diff
changeset
|
374 |
SDL_LockMutex(data->timermap_lock); |
5111
481dabb098ef
Improved timer implementation
Sam Lantinga <slouken@libsdl.org>
parents:
5074
diff
changeset
|
375 |
prev = NULL; |
481dabb098ef
Improved timer implementation
Sam Lantinga <slouken@libsdl.org>
parents:
5074
diff
changeset
|
376 |
for (entry = data->timermap; entry; prev = entry, entry = entry->next) { |
481dabb098ef
Improved timer implementation
Sam Lantinga <slouken@libsdl.org>
parents:
5074
diff
changeset
|
377 |
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
|
378 |
if (prev) { |
5111
481dabb098ef
Improved timer implementation
Sam Lantinga <slouken@libsdl.org>
parents:
5074
diff
changeset
|
379 |
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
|
380 |
} else { |
5111
481dabb098ef
Improved timer implementation
Sam Lantinga <slouken@libsdl.org>
parents:
5074
diff
changeset
|
381 |
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
|
382 |
} |
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
|
383 |
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
|
384 |
} |
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 |
} |
6977
b73d51026c68
Changed the name of SDL_mutexP() SDL_mutexV()
Sam Lantinga <slouken@libsdl.org>
parents:
6885
diff
changeset
|
386 |
SDL_UnlockMutex(data->timermap_lock); |
1
cf2af46e9e2a
Changes since SDL 1.2.0 release
Sam Lantinga <slouken@lokigames.com>
parents:
diff
changeset
|
387 |
|
5111
481dabb098ef
Improved timer implementation
Sam Lantinga <slouken@libsdl.org>
parents:
5074
diff
changeset
|
388 |
if (entry) { |
481dabb098ef
Improved timer implementation
Sam Lantinga <slouken@libsdl.org>
parents:
5074
diff
changeset
|
389 |
if (!entry->timer->canceled) { |
481dabb098ef
Improved timer implementation
Sam Lantinga <slouken@libsdl.org>
parents:
5074
diff
changeset
|
390 |
entry->timer->canceled = SDL_TRUE; |
481dabb098ef
Improved timer implementation
Sam Lantinga <slouken@libsdl.org>
parents:
5074
diff
changeset
|
391 |
canceled = SDL_TRUE; |
481dabb098ef
Improved timer implementation
Sam Lantinga <slouken@libsdl.org>
parents:
5074
diff
changeset
|
392 |
} |
481dabb098ef
Improved timer implementation
Sam Lantinga <slouken@libsdl.org>
parents:
5074
diff
changeset
|
393 |
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
|
394 |
} |
5111
481dabb098ef
Improved timer implementation
Sam Lantinga <slouken@libsdl.org>
parents:
5074
diff
changeset
|
395 |
return canceled; |
1
cf2af46e9e2a
Changes since SDL 1.2.0 release
Sam Lantinga <slouken@lokigames.com>
parents:
diff
changeset
|
396 |
} |
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
|
397 |
|
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
|
398 |
/* vi: set ts=4 sw=4 expandtab: */ |