annotate src/timer/SDL_timer.c @ 5074:906d7293bb47

Fixed bug in timer when the list of timers changed. Fix contributed by Michael Bicha
author Sam Lantinga <slouken@libsdl.org>
date Sat, 22 Jan 2011 00:33:37 -0800
parents f7b03b6838cb
children 481dabb098ef
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"
cf2af46e9e2a Changes since SDL 1.2.0 release
Sam Lantinga <slouken@lokigames.com>
parents:
diff changeset
26 #include "SDL_mutex.h"
cf2af46e9e2a Changes since SDL 1.2.0 release
Sam Lantinga <slouken@lokigames.com>
parents:
diff changeset
27 #include "SDL_systimer.h"
cf2af46e9e2a Changes since SDL 1.2.0 release
Sam Lantinga <slouken@lokigames.com>
parents:
diff changeset
28
cf2af46e9e2a Changes since SDL 1.2.0 release
Sam Lantinga <slouken@lokigames.com>
parents:
diff changeset
29 /* #define DEBUG_TIMERS */
cf2af46e9e2a Changes since SDL 1.2.0 release
Sam Lantinga <slouken@lokigames.com>
parents:
diff changeset
30
cf2af46e9e2a Changes since SDL 1.2.0 release
Sam Lantinga <slouken@lokigames.com>
parents:
diff changeset
31 int SDL_timer_started = 0;
cf2af46e9e2a Changes since SDL 1.2.0 release
Sam Lantinga <slouken@lokigames.com>
parents:
diff changeset
32 int SDL_timer_running = 0;
cf2af46e9e2a Changes since SDL 1.2.0 release
Sam Lantinga <slouken@lokigames.com>
parents:
diff changeset
33
cf2af46e9e2a Changes since SDL 1.2.0 release
Sam Lantinga <slouken@lokigames.com>
parents:
diff changeset
34 /* Data to handle a single periodic alarm */
cf2af46e9e2a Changes since SDL 1.2.0 release
Sam Lantinga <slouken@lokigames.com>
parents:
diff changeset
35 Uint32 SDL_alarm_interval = 0;
cf2af46e9e2a Changes since SDL 1.2.0 release
Sam Lantinga <slouken@lokigames.com>
parents:
diff changeset
36 SDL_TimerCallback SDL_alarm_callback;
cf2af46e9e2a Changes since SDL 1.2.0 release
Sam Lantinga <slouken@lokigames.com>
parents:
diff changeset
37
cf2af46e9e2a Changes since SDL 1.2.0 release
Sam Lantinga <slouken@lokigames.com>
parents:
diff changeset
38 /* Data used for a thread-based timer */
cf2af46e9e2a Changes since SDL 1.2.0 release
Sam Lantinga <slouken@lokigames.com>
parents:
diff changeset
39 static int SDL_timer_threaded = 0;
cf2af46e9e2a Changes since SDL 1.2.0 release
Sam Lantinga <slouken@lokigames.com>
parents:
diff changeset
40
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
41 struct _SDL_TimerID
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
42 {
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
43 Uint32 interval;
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
44 SDL_NewTimerCallback cb;
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
45 void *param;
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
46 Uint32 last_alarm;
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
47 struct _SDL_TimerID *next;
1
cf2af46e9e2a Changes since SDL 1.2.0 release
Sam Lantinga <slouken@lokigames.com>
parents:
diff changeset
48 };
cf2af46e9e2a Changes since SDL 1.2.0 release
Sam Lantinga <slouken@lokigames.com>
parents:
diff changeset
49
cf2af46e9e2a Changes since SDL 1.2.0 release
Sam Lantinga <slouken@lokigames.com>
parents:
diff changeset
50 static SDL_TimerID SDL_timers = NULL;
cf2af46e9e2a Changes since SDL 1.2.0 release
Sam Lantinga <slouken@lokigames.com>
parents:
diff changeset
51 static SDL_mutex *SDL_timer_mutex;
1028
5ba65305c954 Fix various problems with the timer code.
Sam Lantinga <slouken@libsdl.org>
parents: 1023
diff changeset
52 static volatile SDL_bool list_changed = SDL_FALSE;
1
cf2af46e9e2a Changes since SDL 1.2.0 release
Sam Lantinga <slouken@lokigames.com>
parents:
diff changeset
53
cf2af46e9e2a Changes since SDL 1.2.0 release
Sam Lantinga <slouken@lokigames.com>
parents:
diff changeset
54 /* Set whether or not the timer should use a thread.
cf2af46e9e2a Changes since SDL 1.2.0 release
Sam Lantinga <slouken@lokigames.com>
parents:
diff changeset
55 This should not be called while the timer subsystem is running.
cf2af46e9e2a Changes since SDL 1.2.0 release
Sam Lantinga <slouken@lokigames.com>
parents:
diff changeset
56 */
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
57 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
58 SDL_SetTimerThreaded(int value)
1
cf2af46e9e2a Changes since SDL 1.2.0 release
Sam Lantinga <slouken@lokigames.com>
parents:
diff changeset
59 {
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
60 int retval;
1
cf2af46e9e2a Changes since SDL 1.2.0 release
Sam Lantinga <slouken@lokigames.com>
parents:
diff changeset
61
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
62 if (SDL_timer_started) {
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
63 SDL_SetError("Timer already initialized");
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
64 retval = -1;
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
65 } else {
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
66 retval = 0;
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
67 SDL_timer_threaded = value;
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
68 }
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
69 return retval;
1
cf2af46e9e2a Changes since SDL 1.2.0 release
Sam Lantinga <slouken@lokigames.com>
parents:
diff changeset
70 }
cf2af46e9e2a Changes since SDL 1.2.0 release
Sam Lantinga <slouken@lokigames.com>
parents:
diff changeset
71
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
72 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
73 SDL_TimerInit(void)
1
cf2af46e9e2a Changes since SDL 1.2.0 release
Sam Lantinga <slouken@lokigames.com>
parents:
diff changeset
74 {
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
75 int retval;
1
cf2af46e9e2a Changes since SDL 1.2.0 release
Sam Lantinga <slouken@lokigames.com>
parents:
diff changeset
76
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
77 retval = 0;
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
78 if (SDL_timer_started) {
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
79 SDL_TimerQuit();
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
80 }
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
81 if (!SDL_timer_threaded) {
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
82 retval = SDL_SYS_TimerInit();
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 }
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
84 if (SDL_timer_threaded) {
c121d94672cb SDL 1.2 is moving to a branch, and SDL 1.3 is becoming the head.
Sam Lantinga <slouken@libsdl.org>
parents: 1788
diff changeset
85 SDL_timer_mutex = SDL_CreateMutex();
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
86 }
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
87 if (retval == 0) {
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
88 SDL_timer_started = 1;
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
89 }
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
90 return (retval);
1
cf2af46e9e2a Changes since SDL 1.2.0 release
Sam Lantinga <slouken@lokigames.com>
parents:
diff changeset
91 }
cf2af46e9e2a Changes since SDL 1.2.0 release
Sam Lantinga <slouken@lokigames.com>
parents:
diff changeset
92
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
93 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
94 SDL_TimerQuit(void)
1
cf2af46e9e2a Changes since SDL 1.2.0 release
Sam Lantinga <slouken@lokigames.com>
parents:
diff changeset
95 {
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
96 SDL_SetTimer(0, NULL);
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
97 if (SDL_timer_threaded < 2) {
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
98 SDL_SYS_TimerQuit();
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
99 }
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
100 if (SDL_timer_threaded) {
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
101 SDL_DestroyMutex(SDL_timer_mutex);
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
102 SDL_timer_mutex = NULL;
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
103 }
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
104 SDL_timer_started = 0;
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
105 SDL_timer_threaded = 0;
1
cf2af46e9e2a Changes since SDL 1.2.0 release
Sam Lantinga <slouken@lokigames.com>
parents:
diff changeset
106 }
cf2af46e9e2a Changes since SDL 1.2.0 release
Sam Lantinga <slouken@lokigames.com>
parents:
diff changeset
107
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
108 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
109 SDL_ThreadedTimerCheck(void)
1
cf2af46e9e2a Changes since SDL 1.2.0 release
Sam Lantinga <slouken@lokigames.com>
parents:
diff changeset
110 {
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
111 Uint32 now, ms;
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
112 SDL_TimerID t, prev, next;
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
113 SDL_bool removed;
1
cf2af46e9e2a Changes since SDL 1.2.0 release
Sam Lantinga <slouken@lokigames.com>
parents:
diff changeset
114
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
115 SDL_mutexP(SDL_timer_mutex);
5074
906d7293bb47 Fixed bug in timer when the list of timers changed.
Sam Lantinga <slouken@libsdl.org>
parents: 3697
diff changeset
116
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
117 now = SDL_GetTicks();
5074
906d7293bb47 Fixed bug in timer when the list of timers changed.
Sam Lantinga <slouken@libsdl.org>
parents: 3697
diff changeset
118 do {
906d7293bb47 Fixed bug in timer when the list of timers changed.
Sam Lantinga <slouken@libsdl.org>
parents: 3697
diff changeset
119 list_changed = SDL_FALSE;
906d7293bb47 Fixed bug in timer when the list of timers changed.
Sam Lantinga <slouken@libsdl.org>
parents: 3697
diff changeset
120 for (prev = NULL, t = SDL_timers; t; t = next) {
906d7293bb47 Fixed bug in timer when the list of timers changed.
Sam Lantinga <slouken@libsdl.org>
parents: 3697
diff changeset
121 removed = SDL_FALSE;
906d7293bb47 Fixed bug in timer when the list of timers changed.
Sam Lantinga <slouken@libsdl.org>
parents: 3697
diff changeset
122 ms = t->interval - SDL_TIMESLICE;
906d7293bb47 Fixed bug in timer when the list of timers changed.
Sam Lantinga <slouken@libsdl.org>
parents: 3697
diff changeset
123 next = t->next;
906d7293bb47 Fixed bug in timer when the list of timers changed.
Sam Lantinga <slouken@libsdl.org>
parents: 3697
diff changeset
124 if ((int) (now - t->last_alarm) > (int) ms) {
906d7293bb47 Fixed bug in timer when the list of timers changed.
Sam Lantinga <slouken@libsdl.org>
parents: 3697
diff changeset
125 struct _SDL_TimerID timer;
1028
5ba65305c954 Fix various problems with the timer code.
Sam Lantinga <slouken@libsdl.org>
parents: 1023
diff changeset
126
5074
906d7293bb47 Fixed bug in timer when the list of timers changed.
Sam Lantinga <slouken@libsdl.org>
parents: 3697
diff changeset
127 if ((now - t->last_alarm) < t->interval) {
906d7293bb47 Fixed bug in timer when the list of timers changed.
Sam Lantinga <slouken@libsdl.org>
parents: 3697
diff changeset
128 t->last_alarm += t->interval;
906d7293bb47 Fixed bug in timer when the list of timers changed.
Sam Lantinga <slouken@libsdl.org>
parents: 3697
diff changeset
129 } else {
906d7293bb47 Fixed bug in timer when the list of timers changed.
Sam Lantinga <slouken@libsdl.org>
parents: 3697
diff changeset
130 t->last_alarm = now;
906d7293bb47 Fixed bug in timer when the list of timers changed.
Sam Lantinga <slouken@libsdl.org>
parents: 3697
diff changeset
131 }
1
cf2af46e9e2a Changes since SDL 1.2.0 release
Sam Lantinga <slouken@lokigames.com>
parents:
diff changeset
132 #ifdef DEBUG_TIMERS
5074
906d7293bb47 Fixed bug in timer when the list of timers changed.
Sam Lantinga <slouken@libsdl.org>
parents: 3697
diff changeset
133 printf("Executing timer %p (thread = %lu)\n",
906d7293bb47 Fixed bug in timer when the list of timers changed.
Sam Lantinga <slouken@libsdl.org>
parents: 3697
diff changeset
134 t, SDL_ThreadID());
1
cf2af46e9e2a Changes since SDL 1.2.0 release
Sam Lantinga <slouken@lokigames.com>
parents:
diff changeset
135 #endif
5074
906d7293bb47 Fixed bug in timer when the list of timers changed.
Sam Lantinga <slouken@libsdl.org>
parents: 3697
diff changeset
136 timer = *t;
906d7293bb47 Fixed bug in timer when the list of timers changed.
Sam Lantinga <slouken@libsdl.org>
parents: 3697
diff changeset
137 SDL_mutexV(SDL_timer_mutex);
906d7293bb47 Fixed bug in timer when the list of timers changed.
Sam Lantinga <slouken@libsdl.org>
parents: 3697
diff changeset
138 ms = timer.cb(timer.interval, timer.param);
906d7293bb47 Fixed bug in timer when the list of timers changed.
Sam Lantinga <slouken@libsdl.org>
parents: 3697
diff changeset
139 SDL_mutexP(SDL_timer_mutex);
906d7293bb47 Fixed bug in timer when the list of timers changed.
Sam Lantinga <slouken@libsdl.org>
parents: 3697
diff changeset
140 if (list_changed) {
906d7293bb47 Fixed bug in timer when the list of timers changed.
Sam Lantinga <slouken@libsdl.org>
parents: 3697
diff changeset
141 next = t->next;
906d7293bb47 Fixed bug in timer when the list of timers changed.
Sam Lantinga <slouken@libsdl.org>
parents: 3697
diff changeset
142 for (prev = SDL_timers; prev; prev = prev->next) {
906d7293bb47 Fixed bug in timer when the list of timers changed.
Sam Lantinga <slouken@libsdl.org>
parents: 3697
diff changeset
143 if (prev->next == t)
906d7293bb47 Fixed bug in timer when the list of timers changed.
Sam Lantinga <slouken@libsdl.org>
parents: 3697
diff changeset
144 break;
906d7293bb47 Fixed bug in timer when the list of timers changed.
Sam Lantinga <slouken@libsdl.org>
parents: 3697
diff changeset
145 }
906d7293bb47 Fixed bug in timer when the list of timers changed.
Sam Lantinga <slouken@libsdl.org>
parents: 3697
diff changeset
146 }
906d7293bb47 Fixed bug in timer when the list of timers changed.
Sam Lantinga <slouken@libsdl.org>
parents: 3697
diff changeset
147 if (ms != t->interval) {
906d7293bb47 Fixed bug in timer when the list of timers changed.
Sam Lantinga <slouken@libsdl.org>
parents: 3697
diff changeset
148 if (ms) {
906d7293bb47 Fixed bug in timer when the list of timers changed.
Sam Lantinga <slouken@libsdl.org>
parents: 3697
diff changeset
149 t->interval = ROUND_RESOLUTION(ms);
906d7293bb47 Fixed bug in timer when the list of timers changed.
Sam Lantinga <slouken@libsdl.org>
parents: 3697
diff changeset
150 } else {
906d7293bb47 Fixed bug in timer when the list of timers changed.
Sam Lantinga <slouken@libsdl.org>
parents: 3697
diff changeset
151 /* Remove timer from the list */
1
cf2af46e9e2a Changes since SDL 1.2.0 release
Sam Lantinga <slouken@lokigames.com>
parents:
diff changeset
152 #ifdef DEBUG_TIMERS
5074
906d7293bb47 Fixed bug in timer when the list of timers changed.
Sam Lantinga <slouken@libsdl.org>
parents: 3697
diff changeset
153 printf("SDL: Removing timer %p\n", t);
1
cf2af46e9e2a Changes since SDL 1.2.0 release
Sam Lantinga <slouken@lokigames.com>
parents:
diff changeset
154 #endif
5074
906d7293bb47 Fixed bug in timer when the list of timers changed.
Sam Lantinga <slouken@libsdl.org>
parents: 3697
diff changeset
155 if (prev) {
906d7293bb47 Fixed bug in timer when the list of timers changed.
Sam Lantinga <slouken@libsdl.org>
parents: 3697
diff changeset
156 prev->next = next;
906d7293bb47 Fixed bug in timer when the list of timers changed.
Sam Lantinga <slouken@libsdl.org>
parents: 3697
diff changeset
157 } else {
906d7293bb47 Fixed bug in timer when the list of timers changed.
Sam Lantinga <slouken@libsdl.org>
parents: 3697
diff changeset
158 SDL_timers = next;
906d7293bb47 Fixed bug in timer when the list of timers changed.
Sam Lantinga <slouken@libsdl.org>
parents: 3697
diff changeset
159 }
906d7293bb47 Fixed bug in timer when the list of timers changed.
Sam Lantinga <slouken@libsdl.org>
parents: 3697
diff changeset
160 SDL_free(t);
906d7293bb47 Fixed bug in timer when the list of timers changed.
Sam Lantinga <slouken@libsdl.org>
parents: 3697
diff changeset
161 --SDL_timer_running;
906d7293bb47 Fixed bug in timer when the list of timers changed.
Sam Lantinga <slouken@libsdl.org>
parents: 3697
diff changeset
162 removed = SDL_TRUE;
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
163 }
5074
906d7293bb47 Fixed bug in timer when the list of timers changed.
Sam Lantinga <slouken@libsdl.org>
parents: 3697
diff changeset
164 }
906d7293bb47 Fixed bug in timer when the list of timers changed.
Sam Lantinga <slouken@libsdl.org>
parents: 3697
diff changeset
165 if (list_changed) {
906d7293bb47 Fixed bug in timer when the list of timers changed.
Sam Lantinga <slouken@libsdl.org>
parents: 3697
diff changeset
166 /* Abort, list of timers modified */
906d7293bb47 Fixed bug in timer when the list of timers changed.
Sam Lantinga <slouken@libsdl.org>
parents: 3697
diff changeset
167 break;
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
168 }
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
169 }
5074
906d7293bb47 Fixed bug in timer when the list of timers changed.
Sam Lantinga <slouken@libsdl.org>
parents: 3697
diff changeset
170 /* Don't update prev if the timer has disappeared */
906d7293bb47 Fixed bug in timer when the list of timers changed.
Sam Lantinga <slouken@libsdl.org>
parents: 3697
diff changeset
171 if (!removed) {
906d7293bb47 Fixed bug in timer when the list of timers changed.
Sam Lantinga <slouken@libsdl.org>
parents: 3697
diff changeset
172 prev = t;
906d7293bb47 Fixed bug in timer when the list of timers changed.
Sam Lantinga <slouken@libsdl.org>
parents: 3697
diff changeset
173 }
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
174 }
5074
906d7293bb47 Fixed bug in timer when the list of timers changed.
Sam Lantinga <slouken@libsdl.org>
parents: 3697
diff changeset
175 } while (list_changed);
906d7293bb47 Fixed bug in timer when the list of timers changed.
Sam Lantinga <slouken@libsdl.org>
parents: 3697
diff changeset
176
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
177 SDL_mutexV(SDL_timer_mutex);
1
cf2af46e9e2a Changes since SDL 1.2.0 release
Sam Lantinga <slouken@lokigames.com>
parents:
diff changeset
178 }
cf2af46e9e2a Changes since SDL 1.2.0 release
Sam Lantinga <slouken@lokigames.com>
parents:
diff changeset
179
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
180 static SDL_TimerID
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
181 SDL_AddTimerInternal(Uint32 interval, SDL_NewTimerCallback callback,
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
182 void *param)
1028
5ba65305c954 Fix various problems with the timer code.
Sam Lantinga <slouken@libsdl.org>
parents: 1023
diff changeset
183 {
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
184 SDL_TimerID t;
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
185 t = (SDL_TimerID) SDL_malloc(sizeof(struct _SDL_TimerID));
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
186 if (t) {
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
187 t->interval = ROUND_RESOLUTION(interval);
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
188 t->cb = callback;
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
189 t->param = param;
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
190 t->last_alarm = SDL_GetTicks();
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
191 t->next = SDL_timers;
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
192 SDL_timers = t;
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
193 ++SDL_timer_running;
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
194 list_changed = SDL_TRUE;
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
195 }
1028
5ba65305c954 Fix various problems with the timer code.
Sam Lantinga <slouken@libsdl.org>
parents: 1023
diff changeset
196 #ifdef DEBUG_TIMERS
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
197 printf("SDL_AddTimer(%d) = %08x num_timers = %d\n", interval, (Uint32) t,
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
198 SDL_timer_running);
1028
5ba65305c954 Fix various problems with the timer code.
Sam Lantinga <slouken@libsdl.org>
parents: 1023
diff changeset
199 #endif
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
200 return t;
1028
5ba65305c954 Fix various problems with the timer code.
Sam Lantinga <slouken@libsdl.org>
parents: 1023
diff changeset
201 }
5ba65305c954 Fix various problems with the timer code.
Sam Lantinga <slouken@libsdl.org>
parents: 1023
diff changeset
202
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 SDL_TimerID
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
204 SDL_AddTimer(Uint32 interval, SDL_NewTimerCallback callback, void *param)
1
cf2af46e9e2a Changes since SDL 1.2.0 release
Sam Lantinga <slouken@lokigames.com>
parents:
diff changeset
205 {
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
206 SDL_TimerID t;
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 if (!SDL_timer_mutex) {
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 if (SDL_timer_started) {
c121d94672cb SDL 1.2 is moving to a branch, and SDL 1.3 is becoming the head.
Sam Lantinga <slouken@libsdl.org>
parents: 1788
diff changeset
209 SDL_SetError("This platform doesn't support multiple timers");
c121d94672cb SDL 1.2 is moving to a branch, and SDL 1.3 is becoming the head.
Sam Lantinga <slouken@libsdl.org>
parents: 1788
diff changeset
210 } else {
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
211 SDL_SetError("You must call SDL_Init(SDL_INIT_TIMER) first");
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
212 }
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
213 return NULL;
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 }
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
215 if (!SDL_timer_threaded) {
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
216 SDL_SetError("Multiple timers require threaded events!");
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
217 return NULL;
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 }
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_mutexP(SDL_timer_mutex);
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
220 t = SDL_AddTimerInternal(interval, callback, param);
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
221 SDL_mutexV(SDL_timer_mutex);
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
222 return t;
1
cf2af46e9e2a Changes since SDL 1.2.0 release
Sam Lantinga <slouken@lokigames.com>
parents:
diff changeset
223 }
cf2af46e9e2a Changes since SDL 1.2.0 release
Sam Lantinga <slouken@lokigames.com>
parents:
diff changeset
224
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
225 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
226 SDL_RemoveTimer(SDL_TimerID id)
1
cf2af46e9e2a Changes since SDL 1.2.0 release
Sam Lantinga <slouken@lokigames.com>
parents:
diff changeset
227 {
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
228 SDL_TimerID t, prev = NULL;
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
229 SDL_bool removed;
1
cf2af46e9e2a Changes since SDL 1.2.0 release
Sam Lantinga <slouken@lokigames.com>
parents:
diff changeset
230
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
231 removed = SDL_FALSE;
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
232 SDL_mutexP(SDL_timer_mutex);
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
233 /* Look for id in the linked list of timers */
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
234 for (t = SDL_timers; t; prev = t, t = t->next) {
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
235 if (t == id) {
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
236 if (prev) {
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
237 prev->next = t->next;
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 } else {
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
239 SDL_timers = t->next;
c121d94672cb SDL 1.2 is moving to a branch, and SDL 1.3 is becoming the head.
Sam Lantinga <slouken@libsdl.org>
parents: 1788
diff changeset
240 }
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
241 SDL_free(t);
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 --SDL_timer_running;
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 removed = SDL_TRUE;
c121d94672cb SDL 1.2 is moving to a branch, and SDL 1.3 is becoming the head.
Sam Lantinga <slouken@libsdl.org>
parents: 1788
diff changeset
244 list_changed = SDL_TRUE;
c121d94672cb SDL 1.2 is moving to a branch, and SDL 1.3 is becoming the head.
Sam Lantinga <slouken@libsdl.org>
parents: 1788
diff changeset
245 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
246 }
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
247 }
1
cf2af46e9e2a Changes since SDL 1.2.0 release
Sam Lantinga <slouken@lokigames.com>
parents:
diff changeset
248 #ifdef DEBUG_TIMERS
3578
0d1b16ee0bca Fixed bug #741
Sam Lantinga <slouken@libsdl.org>
parents: 2859
diff changeset
249 printf("SDL_RemoveTimer(%08x) = %d num_timers = %d thread = %lu\n",
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 (Uint32) id, removed, SDL_timer_running, SDL_ThreadID());
1
cf2af46e9e2a Changes since SDL 1.2.0 release
Sam Lantinga <slouken@lokigames.com>
parents:
diff changeset
251 #endif
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
252 SDL_mutexV(SDL_timer_mutex);
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
253 return removed;
1
cf2af46e9e2a Changes since SDL 1.2.0 release
Sam Lantinga <slouken@lokigames.com>
parents:
diff changeset
254 }
cf2af46e9e2a Changes since SDL 1.2.0 release
Sam Lantinga <slouken@lokigames.com>
parents:
diff changeset
255
cf2af46e9e2a Changes since SDL 1.2.0 release
Sam Lantinga <slouken@lokigames.com>
parents:
diff changeset
256 /* Old style callback functions are wrapped through this */
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
257 static Uint32 SDLCALL
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
258 callback_wrapper(Uint32 ms, void *param)
1
cf2af46e9e2a Changes since SDL 1.2.0 release
Sam Lantinga <slouken@lokigames.com>
parents:
diff changeset
259 {
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
260 SDL_TimerCallback func = (SDL_TimerCallback) param;
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
261 return (*func) (ms);
1
cf2af46e9e2a Changes since SDL 1.2.0 release
Sam Lantinga <slouken@lokigames.com>
parents:
diff changeset
262 }
cf2af46e9e2a Changes since SDL 1.2.0 release
Sam Lantinga <slouken@lokigames.com>
parents:
diff changeset
263
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
264 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
265 SDL_SetTimer(Uint32 ms, SDL_TimerCallback callback)
1
cf2af46e9e2a Changes since SDL 1.2.0 release
Sam Lantinga <slouken@lokigames.com>
parents:
diff changeset
266 {
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 int retval;
1
cf2af46e9e2a Changes since SDL 1.2.0 release
Sam Lantinga <slouken@lokigames.com>
parents:
diff changeset
268
cf2af46e9e2a Changes since SDL 1.2.0 release
Sam Lantinga <slouken@lokigames.com>
parents:
diff changeset
269 #ifdef DEBUG_TIMERS
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
270 printf("SDL_SetTimer(%d)\n", ms);
1
cf2af46e9e2a Changes since SDL 1.2.0 release
Sam Lantinga <slouken@lokigames.com>
parents:
diff changeset
271 #endif
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
272 retval = 0;
1028
5ba65305c954 Fix various problems with the timer code.
Sam Lantinga <slouken@libsdl.org>
parents: 1023
diff changeset
273
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
274 if (SDL_timer_threaded) {
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
275 SDL_mutexP(SDL_timer_mutex);
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
276 }
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
277 if (SDL_timer_running) { /* Stop any currently running timer */
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
278 if (SDL_timer_threaded) {
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 while (SDL_timers) {
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
280 SDL_TimerID freeme = SDL_timers;
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 SDL_timers = SDL_timers->next;
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
282 SDL_free(freeme);
c121d94672cb SDL 1.2 is moving to a branch, and SDL 1.3 is becoming the head.
Sam Lantinga <slouken@libsdl.org>
parents: 1788
diff changeset
283 }
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_timer_running = 0;
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
285 list_changed = SDL_TRUE;
c121d94672cb SDL 1.2 is moving to a branch, and SDL 1.3 is becoming the head.
Sam Lantinga <slouken@libsdl.org>
parents: 1788
diff changeset
286 } else {
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
287 SDL_SYS_StopTimer();
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
288 SDL_timer_running = 0;
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
289 }
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
290 }
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
291 if (ms) {
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
292 if (SDL_timer_threaded) {
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 if (SDL_AddTimerInternal
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
294 (ms, callback_wrapper, (void *) callback) == NULL) {
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
295 retval = -1;
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 }
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
297 } else {
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
298 SDL_timer_running = 1;
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
299 SDL_alarm_interval = ms;
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
300 SDL_alarm_callback = callback;
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
301 retval = SDL_SYS_StartTimer();
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 }
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
303 }
c121d94672cb SDL 1.2 is moving to a branch, and SDL 1.3 is becoming the head.
Sam Lantinga <slouken@libsdl.org>
parents: 1788
diff changeset
304 if (SDL_timer_threaded) {
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
305 SDL_mutexV(SDL_timer_mutex);
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
306 }
1028
5ba65305c954 Fix various problems with the timer code.
Sam Lantinga <slouken@libsdl.org>
parents: 1023
diff changeset
307
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
308 return retval;
1
cf2af46e9e2a Changes since SDL 1.2.0 release
Sam Lantinga <slouken@lokigames.com>
parents:
diff changeset
309 }
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
310
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
311 /* vi: set ts=4 sw=4 expandtab: */