src/timer/SDL_timer.c
author Sam Lantinga <slouken@libsdl.org>
Tue, 04 Jan 2005 19:05:32 +0000
changeset 1023 e719ee25439d
parent 252 e8157fcb3114
child 1028 5ba65305c954
permissions -rw-r--r--
Fix potential use of freed memory in the threaded timer system
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
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
cf2af46e9e2a Changes since SDL 1.2.0 release
Sam Lantinga <slouken@lokigames.com>
parents:
diff changeset
     3
    Copyright (C) 1997, 1998  Sam Lantinga
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
cf2af46e9e2a Changes since SDL 1.2.0 release
Sam Lantinga <slouken@lokigames.com>
parents:
diff changeset
     6
    modify it under the terms of the GNU Library General Public
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
cf2af46e9e2a Changes since SDL 1.2.0 release
Sam Lantinga <slouken@lokigames.com>
parents:
diff changeset
     8
    version 2 of the License, or (at your option) any later version.
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
cf2af46e9e2a Changes since SDL 1.2.0 release
Sam Lantinga <slouken@lokigames.com>
parents:
diff changeset
    13
    Library General Public License for more details.
cf2af46e9e2a Changes since SDL 1.2.0 release
Sam Lantinga <slouken@lokigames.com>
parents:
diff changeset
    14
cf2af46e9e2a Changes since SDL 1.2.0 release
Sam Lantinga <slouken@lokigames.com>
parents:
diff changeset
    15
    You should have received a copy of the GNU Library General Public
cf2af46e9e2a Changes since SDL 1.2.0 release
Sam Lantinga <slouken@lokigames.com>
parents:
diff changeset
    16
    License along with this library; if not, write to the Free
cf2af46e9e2a Changes since SDL 1.2.0 release
Sam Lantinga <slouken@lokigames.com>
parents:
diff changeset
    17
    Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
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
cf2af46e9e2a Changes since SDL 1.2.0 release
Sam Lantinga <slouken@lokigames.com>
parents:
diff changeset
    20
    5635-34 Springhouse Dr.
cf2af46e9e2a Changes since SDL 1.2.0 release
Sam Lantinga <slouken@lokigames.com>
parents:
diff changeset
    21
    Pleasanton, CA 94588 (USA)
252
e8157fcb3114 Updated the source with the correct e-mail address
Sam Lantinga <slouken@libsdl.org>
parents: 1
diff changeset
    22
    slouken@libsdl.org
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
cf2af46e9e2a Changes since SDL 1.2.0 release
Sam Lantinga <slouken@lokigames.com>
parents:
diff changeset
    25
#ifdef SAVE_RCSID
cf2af46e9e2a Changes since SDL 1.2.0 release
Sam Lantinga <slouken@lokigames.com>
parents:
diff changeset
    26
static char rcsid =
cf2af46e9e2a Changes since SDL 1.2.0 release
Sam Lantinga <slouken@lokigames.com>
parents:
diff changeset
    27
 "@(#) $Id$";
cf2af46e9e2a Changes since SDL 1.2.0 release
Sam Lantinga <slouken@lokigames.com>
parents:
diff changeset
    28
#endif
cf2af46e9e2a Changes since SDL 1.2.0 release
Sam Lantinga <slouken@lokigames.com>
parents:
diff changeset
    29
cf2af46e9e2a Changes since SDL 1.2.0 release
Sam Lantinga <slouken@lokigames.com>
parents:
diff changeset
    30
#include <stdlib.h>
cf2af46e9e2a Changes since SDL 1.2.0 release
Sam Lantinga <slouken@lokigames.com>
parents:
diff changeset
    31
#include <stdio.h>			/* For the definition of NULL */
cf2af46e9e2a Changes since SDL 1.2.0 release
Sam Lantinga <slouken@lokigames.com>
parents:
diff changeset
    32
cf2af46e9e2a Changes since SDL 1.2.0 release
Sam Lantinga <slouken@lokigames.com>
parents:
diff changeset
    33
#include "SDL_error.h"
cf2af46e9e2a Changes since SDL 1.2.0 release
Sam Lantinga <slouken@lokigames.com>
parents:
diff changeset
    34
#include "SDL_timer.h"
cf2af46e9e2a Changes since SDL 1.2.0 release
Sam Lantinga <slouken@lokigames.com>
parents:
diff changeset
    35
#include "SDL_timer_c.h"
cf2af46e9e2a Changes since SDL 1.2.0 release
Sam Lantinga <slouken@lokigames.com>
parents:
diff changeset
    36
#include "SDL_mutex.h"
cf2af46e9e2a Changes since SDL 1.2.0 release
Sam Lantinga <slouken@lokigames.com>
parents:
diff changeset
    37
#include "SDL_systimer.h"
cf2af46e9e2a Changes since SDL 1.2.0 release
Sam Lantinga <slouken@lokigames.com>
parents:
diff changeset
    38
cf2af46e9e2a Changes since SDL 1.2.0 release
Sam Lantinga <slouken@lokigames.com>
parents:
diff changeset
    39
/* #define DEBUG_TIMERS */
cf2af46e9e2a Changes since SDL 1.2.0 release
Sam Lantinga <slouken@lokigames.com>
parents:
diff changeset
    40
cf2af46e9e2a Changes since SDL 1.2.0 release
Sam Lantinga <slouken@lokigames.com>
parents:
diff changeset
    41
int SDL_timer_started = 0;
cf2af46e9e2a Changes since SDL 1.2.0 release
Sam Lantinga <slouken@lokigames.com>
parents:
diff changeset
    42
int SDL_timer_running = 0;
cf2af46e9e2a Changes since SDL 1.2.0 release
Sam Lantinga <slouken@lokigames.com>
parents:
diff changeset
    43
cf2af46e9e2a Changes since SDL 1.2.0 release
Sam Lantinga <slouken@lokigames.com>
parents:
diff changeset
    44
/* Data to handle a single periodic alarm */
cf2af46e9e2a Changes since SDL 1.2.0 release
Sam Lantinga <slouken@lokigames.com>
parents:
diff changeset
    45
Uint32 SDL_alarm_interval = 0;
cf2af46e9e2a Changes since SDL 1.2.0 release
Sam Lantinga <slouken@lokigames.com>
parents:
diff changeset
    46
SDL_TimerCallback SDL_alarm_callback;
cf2af46e9e2a Changes since SDL 1.2.0 release
Sam Lantinga <slouken@lokigames.com>
parents:
diff changeset
    47
1023
e719ee25439d Fix potential use of freed memory in the threaded timer system
Sam Lantinga <slouken@libsdl.org>
parents: 252
diff changeset
    48
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
    49
cf2af46e9e2a Changes since SDL 1.2.0 release
Sam Lantinga <slouken@lokigames.com>
parents:
diff changeset
    50
/* Data used for a thread-based timer */
cf2af46e9e2a Changes since SDL 1.2.0 release
Sam Lantinga <slouken@lokigames.com>
parents:
diff changeset
    51
static int SDL_timer_threaded = 0;
cf2af46e9e2a Changes since SDL 1.2.0 release
Sam Lantinga <slouken@lokigames.com>
parents:
diff changeset
    52
cf2af46e9e2a Changes since SDL 1.2.0 release
Sam Lantinga <slouken@lokigames.com>
parents:
diff changeset
    53
struct _SDL_TimerID {
cf2af46e9e2a Changes since SDL 1.2.0 release
Sam Lantinga <slouken@lokigames.com>
parents:
diff changeset
    54
	Uint32 interval;
cf2af46e9e2a Changes since SDL 1.2.0 release
Sam Lantinga <slouken@lokigames.com>
parents:
diff changeset
    55
	SDL_NewTimerCallback cb;
cf2af46e9e2a Changes since SDL 1.2.0 release
Sam Lantinga <slouken@lokigames.com>
parents:
diff changeset
    56
	void *param;
cf2af46e9e2a Changes since SDL 1.2.0 release
Sam Lantinga <slouken@lokigames.com>
parents:
diff changeset
    57
	Uint32 last_alarm;
cf2af46e9e2a Changes since SDL 1.2.0 release
Sam Lantinga <slouken@lokigames.com>
parents:
diff changeset
    58
	struct _SDL_TimerID *next;
cf2af46e9e2a Changes since SDL 1.2.0 release
Sam Lantinga <slouken@lokigames.com>
parents:
diff changeset
    59
};
cf2af46e9e2a Changes since SDL 1.2.0 release
Sam Lantinga <slouken@lokigames.com>
parents:
diff changeset
    60
cf2af46e9e2a Changes since SDL 1.2.0 release
Sam Lantinga <slouken@lokigames.com>
parents:
diff changeset
    61
static SDL_TimerID SDL_timers = NULL;
cf2af46e9e2a Changes since SDL 1.2.0 release
Sam Lantinga <slouken@lokigames.com>
parents:
diff changeset
    62
static Uint32 num_timers = 0;
cf2af46e9e2a Changes since SDL 1.2.0 release
Sam Lantinga <slouken@lokigames.com>
parents:
diff changeset
    63
static SDL_mutex *SDL_timer_mutex;
cf2af46e9e2a Changes since SDL 1.2.0 release
Sam Lantinga <slouken@lokigames.com>
parents:
diff changeset
    64
cf2af46e9e2a Changes since SDL 1.2.0 release
Sam Lantinga <slouken@lokigames.com>
parents:
diff changeset
    65
/* 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
    66
   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
    67
*/
cf2af46e9e2a Changes since SDL 1.2.0 release
Sam Lantinga <slouken@lokigames.com>
parents:
diff changeset
    68
int SDL_SetTimerThreaded(int value)
cf2af46e9e2a Changes since SDL 1.2.0 release
Sam Lantinga <slouken@lokigames.com>
parents:
diff changeset
    69
{
cf2af46e9e2a Changes since SDL 1.2.0 release
Sam Lantinga <slouken@lokigames.com>
parents:
diff changeset
    70
	int retval;
cf2af46e9e2a Changes since SDL 1.2.0 release
Sam Lantinga <slouken@lokigames.com>
parents:
diff changeset
    71
cf2af46e9e2a Changes since SDL 1.2.0 release
Sam Lantinga <slouken@lokigames.com>
parents:
diff changeset
    72
	if ( SDL_timer_started ) {
cf2af46e9e2a Changes since SDL 1.2.0 release
Sam Lantinga <slouken@lokigames.com>
parents:
diff changeset
    73
		SDL_SetError("Timer already initialized");
cf2af46e9e2a Changes since SDL 1.2.0 release
Sam Lantinga <slouken@lokigames.com>
parents:
diff changeset
    74
		retval = -1;
cf2af46e9e2a Changes since SDL 1.2.0 release
Sam Lantinga <slouken@lokigames.com>
parents:
diff changeset
    75
	} else {
cf2af46e9e2a Changes since SDL 1.2.0 release
Sam Lantinga <slouken@lokigames.com>
parents:
diff changeset
    76
		retval = 0;
cf2af46e9e2a Changes since SDL 1.2.0 release
Sam Lantinga <slouken@lokigames.com>
parents:
diff changeset
    77
		SDL_timer_threaded = value;
cf2af46e9e2a Changes since SDL 1.2.0 release
Sam Lantinga <slouken@lokigames.com>
parents:
diff changeset
    78
	}
cf2af46e9e2a Changes since SDL 1.2.0 release
Sam Lantinga <slouken@lokigames.com>
parents:
diff changeset
    79
	return retval;
cf2af46e9e2a Changes since SDL 1.2.0 release
Sam Lantinga <slouken@lokigames.com>
parents:
diff changeset
    80
}
cf2af46e9e2a Changes since SDL 1.2.0 release
Sam Lantinga <slouken@lokigames.com>
parents:
diff changeset
    81
cf2af46e9e2a Changes since SDL 1.2.0 release
Sam Lantinga <slouken@lokigames.com>
parents:
diff changeset
    82
int SDL_TimerInit(void)
cf2af46e9e2a Changes since SDL 1.2.0 release
Sam Lantinga <slouken@lokigames.com>
parents:
diff changeset
    83
{
cf2af46e9e2a Changes since SDL 1.2.0 release
Sam Lantinga <slouken@lokigames.com>
parents:
diff changeset
    84
	int retval;
cf2af46e9e2a Changes since SDL 1.2.0 release
Sam Lantinga <slouken@lokigames.com>
parents:
diff changeset
    85
cf2af46e9e2a Changes since SDL 1.2.0 release
Sam Lantinga <slouken@lokigames.com>
parents:
diff changeset
    86
	SDL_timer_running = 0;
cf2af46e9e2a Changes since SDL 1.2.0 release
Sam Lantinga <slouken@lokigames.com>
parents:
diff changeset
    87
	SDL_SetTimer(0, NULL);
cf2af46e9e2a Changes since SDL 1.2.0 release
Sam Lantinga <slouken@lokigames.com>
parents:
diff changeset
    88
	retval = 0;
cf2af46e9e2a Changes since SDL 1.2.0 release
Sam Lantinga <slouken@lokigames.com>
parents:
diff changeset
    89
	if ( ! SDL_timer_threaded ) {
cf2af46e9e2a Changes since SDL 1.2.0 release
Sam Lantinga <slouken@lokigames.com>
parents:
diff changeset
    90
		retval = SDL_SYS_TimerInit();
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
	if ( SDL_timer_threaded ) {
cf2af46e9e2a Changes since SDL 1.2.0 release
Sam Lantinga <slouken@lokigames.com>
parents:
diff changeset
    93
		SDL_timer_mutex = SDL_CreateMutex();
cf2af46e9e2a Changes since SDL 1.2.0 release
Sam Lantinga <slouken@lokigames.com>
parents:
diff changeset
    94
	}
cf2af46e9e2a Changes since SDL 1.2.0 release
Sam Lantinga <slouken@lokigames.com>
parents:
diff changeset
    95
	SDL_timer_started = 1;
cf2af46e9e2a Changes since SDL 1.2.0 release
Sam Lantinga <slouken@lokigames.com>
parents:
diff changeset
    96
	return(retval);
cf2af46e9e2a Changes since SDL 1.2.0 release
Sam Lantinga <slouken@lokigames.com>
parents:
diff changeset
    97
}
cf2af46e9e2a Changes since SDL 1.2.0 release
Sam Lantinga <slouken@lokigames.com>
parents:
diff changeset
    98
cf2af46e9e2a Changes since SDL 1.2.0 release
Sam Lantinga <slouken@lokigames.com>
parents:
diff changeset
    99
void SDL_TimerQuit(void)
cf2af46e9e2a Changes since SDL 1.2.0 release
Sam Lantinga <slouken@lokigames.com>
parents:
diff changeset
   100
{
cf2af46e9e2a Changes since SDL 1.2.0 release
Sam Lantinga <slouken@lokigames.com>
parents:
diff changeset
   101
	SDL_SetTimer(0, NULL);
cf2af46e9e2a Changes since SDL 1.2.0 release
Sam Lantinga <slouken@lokigames.com>
parents:
diff changeset
   102
	if ( SDL_timer_threaded < 2 ) {
cf2af46e9e2a Changes since SDL 1.2.0 release
Sam Lantinga <slouken@lokigames.com>
parents:
diff changeset
   103
		SDL_SYS_TimerQuit();
cf2af46e9e2a Changes since SDL 1.2.0 release
Sam Lantinga <slouken@lokigames.com>
parents:
diff changeset
   104
	}
cf2af46e9e2a Changes since SDL 1.2.0 release
Sam Lantinga <slouken@lokigames.com>
parents:
diff changeset
   105
	if ( SDL_timer_threaded ) {
cf2af46e9e2a Changes since SDL 1.2.0 release
Sam Lantinga <slouken@lokigames.com>
parents:
diff changeset
   106
		SDL_DestroyMutex(SDL_timer_mutex);
cf2af46e9e2a Changes since SDL 1.2.0 release
Sam Lantinga <slouken@lokigames.com>
parents:
diff changeset
   107
	}
cf2af46e9e2a Changes since SDL 1.2.0 release
Sam Lantinga <slouken@lokigames.com>
parents:
diff changeset
   108
	SDL_timer_started = 0;
cf2af46e9e2a Changes since SDL 1.2.0 release
Sam Lantinga <slouken@lokigames.com>
parents:
diff changeset
   109
	SDL_timer_threaded = 0;
cf2af46e9e2a Changes since SDL 1.2.0 release
Sam Lantinga <slouken@lokigames.com>
parents:
diff changeset
   110
}
cf2af46e9e2a Changes since SDL 1.2.0 release
Sam Lantinga <slouken@lokigames.com>
parents:
diff changeset
   111
cf2af46e9e2a Changes since SDL 1.2.0 release
Sam Lantinga <slouken@lokigames.com>
parents:
diff changeset
   112
void SDL_ThreadedTimerCheck(void)
cf2af46e9e2a Changes since SDL 1.2.0 release
Sam Lantinga <slouken@lokigames.com>
parents:
diff changeset
   113
{
cf2af46e9e2a Changes since SDL 1.2.0 release
Sam Lantinga <slouken@lokigames.com>
parents:
diff changeset
   114
	Uint32 now, ms;
cf2af46e9e2a Changes since SDL 1.2.0 release
Sam Lantinga <slouken@lokigames.com>
parents:
diff changeset
   115
	SDL_TimerID t, prev, next;
cf2af46e9e2a Changes since SDL 1.2.0 release
Sam Lantinga <slouken@lokigames.com>
parents:
diff changeset
   116
	int removed;
1023
e719ee25439d Fix potential use of freed memory in the threaded timer system
Sam Lantinga <slouken@libsdl.org>
parents: 252
diff changeset
   117
	SDL_NewTimerCallback callback;
e719ee25439d Fix potential use of freed memory in the threaded timer system
Sam Lantinga <slouken@libsdl.org>
parents: 252
diff changeset
   118
	Uint32 interval;
e719ee25439d Fix potential use of freed memory in the threaded timer system
Sam Lantinga <slouken@libsdl.org>
parents: 252
diff changeset
   119
	void *param;
1
cf2af46e9e2a Changes since SDL 1.2.0 release
Sam Lantinga <slouken@lokigames.com>
parents:
diff changeset
   120
cf2af46e9e2a Changes since SDL 1.2.0 release
Sam Lantinga <slouken@lokigames.com>
parents:
diff changeset
   121
	now = SDL_GetTicks();
cf2af46e9e2a Changes since SDL 1.2.0 release
Sam Lantinga <slouken@lokigames.com>
parents:
diff changeset
   122
cf2af46e9e2a Changes since SDL 1.2.0 release
Sam Lantinga <slouken@lokigames.com>
parents:
diff changeset
   123
	SDL_mutexP(SDL_timer_mutex);
cf2af46e9e2a Changes since SDL 1.2.0 release
Sam Lantinga <slouken@lokigames.com>
parents:
diff changeset
   124
	for ( prev = NULL, t = SDL_timers; t; t = next ) {
cf2af46e9e2a Changes since SDL 1.2.0 release
Sam Lantinga <slouken@lokigames.com>
parents:
diff changeset
   125
		removed = 0;
cf2af46e9e2a Changes since SDL 1.2.0 release
Sam Lantinga <slouken@lokigames.com>
parents:
diff changeset
   126
		ms = t->interval - SDL_TIMESLICE;
cf2af46e9e2a Changes since SDL 1.2.0 release
Sam Lantinga <slouken@lokigames.com>
parents:
diff changeset
   127
		next = t->next;
cf2af46e9e2a Changes since SDL 1.2.0 release
Sam Lantinga <slouken@lokigames.com>
parents:
diff changeset
   128
		if ( (t->last_alarm < now) && ((now - t->last_alarm) > ms) ) {
cf2af46e9e2a Changes since SDL 1.2.0 release
Sam Lantinga <slouken@lokigames.com>
parents:
diff changeset
   129
			if ( (now - t->last_alarm) < t->interval ) {
cf2af46e9e2a Changes since SDL 1.2.0 release
Sam Lantinga <slouken@lokigames.com>
parents:
diff changeset
   130
				t->last_alarm += t->interval;
cf2af46e9e2a Changes since SDL 1.2.0 release
Sam Lantinga <slouken@lokigames.com>
parents:
diff changeset
   131
			} else {
cf2af46e9e2a Changes since SDL 1.2.0 release
Sam Lantinga <slouken@lokigames.com>
parents:
diff changeset
   132
				t->last_alarm = now;
cf2af46e9e2a Changes since SDL 1.2.0 release
Sam Lantinga <slouken@lokigames.com>
parents:
diff changeset
   133
			}
cf2af46e9e2a Changes since SDL 1.2.0 release
Sam Lantinga <slouken@lokigames.com>
parents:
diff changeset
   134
			list_changed = SDL_FALSE;
cf2af46e9e2a Changes since SDL 1.2.0 release
Sam Lantinga <slouken@lokigames.com>
parents:
diff changeset
   135
#ifdef DEBUG_TIMERS
cf2af46e9e2a Changes since SDL 1.2.0 release
Sam Lantinga <slouken@lokigames.com>
parents:
diff changeset
   136
			printf("Executing timer %p (thread = %d)\n",
cf2af46e9e2a Changes since SDL 1.2.0 release
Sam Lantinga <slouken@lokigames.com>
parents:
diff changeset
   137
						t, SDL_ThreadID());
cf2af46e9e2a Changes since SDL 1.2.0 release
Sam Lantinga <slouken@lokigames.com>
parents:
diff changeset
   138
#endif
1023
e719ee25439d Fix potential use of freed memory in the threaded timer system
Sam Lantinga <slouken@libsdl.org>
parents: 252
diff changeset
   139
			callback = t->cb;
e719ee25439d Fix potential use of freed memory in the threaded timer system
Sam Lantinga <slouken@libsdl.org>
parents: 252
diff changeset
   140
			interval = t->interval;
e719ee25439d Fix potential use of freed memory in the threaded timer system
Sam Lantinga <slouken@libsdl.org>
parents: 252
diff changeset
   141
			param = t->param;
1
cf2af46e9e2a Changes since SDL 1.2.0 release
Sam Lantinga <slouken@lokigames.com>
parents:
diff changeset
   142
			SDL_mutexV(SDL_timer_mutex);
1023
e719ee25439d Fix potential use of freed memory in the threaded timer system
Sam Lantinga <slouken@libsdl.org>
parents: 252
diff changeset
   143
			ms = callback(interval, param);
1
cf2af46e9e2a Changes since SDL 1.2.0 release
Sam Lantinga <slouken@lokigames.com>
parents:
diff changeset
   144
			SDL_mutexP(SDL_timer_mutex);
cf2af46e9e2a Changes since SDL 1.2.0 release
Sam Lantinga <slouken@lokigames.com>
parents:
diff changeset
   145
			if ( list_changed ) {
cf2af46e9e2a Changes since SDL 1.2.0 release
Sam Lantinga <slouken@lokigames.com>
parents:
diff changeset
   146
				/* Abort, list of timers has been modified */
cf2af46e9e2a Changes since SDL 1.2.0 release
Sam Lantinga <slouken@lokigames.com>
parents:
diff changeset
   147
				break;
cf2af46e9e2a Changes since SDL 1.2.0 release
Sam Lantinga <slouken@lokigames.com>
parents:
diff changeset
   148
			}
cf2af46e9e2a Changes since SDL 1.2.0 release
Sam Lantinga <slouken@lokigames.com>
parents:
diff changeset
   149
			if ( ms != t->interval ) {
cf2af46e9e2a Changes since SDL 1.2.0 release
Sam Lantinga <slouken@lokigames.com>
parents:
diff changeset
   150
				if ( ms ) {
cf2af46e9e2a Changes since SDL 1.2.0 release
Sam Lantinga <slouken@lokigames.com>
parents:
diff changeset
   151
					t->interval = ROUND_RESOLUTION(ms);
cf2af46e9e2a Changes since SDL 1.2.0 release
Sam Lantinga <slouken@lokigames.com>
parents:
diff changeset
   152
				} else { /* Remove the timer from the linked list */
cf2af46e9e2a Changes since SDL 1.2.0 release
Sam Lantinga <slouken@lokigames.com>
parents:
diff changeset
   153
#ifdef DEBUG_TIMERS
cf2af46e9e2a Changes since SDL 1.2.0 release
Sam Lantinga <slouken@lokigames.com>
parents:
diff changeset
   154
					printf("SDL: Removing timer %p\n", t);
cf2af46e9e2a Changes since SDL 1.2.0 release
Sam Lantinga <slouken@lokigames.com>
parents:
diff changeset
   155
#endif
cf2af46e9e2a Changes since SDL 1.2.0 release
Sam Lantinga <slouken@lokigames.com>
parents:
diff changeset
   156
					if ( prev ) {
cf2af46e9e2a Changes since SDL 1.2.0 release
Sam Lantinga <slouken@lokigames.com>
parents:
diff changeset
   157
						prev->next = next;
cf2af46e9e2a Changes since SDL 1.2.0 release
Sam Lantinga <slouken@lokigames.com>
parents:
diff changeset
   158
					} else {
cf2af46e9e2a Changes since SDL 1.2.0 release
Sam Lantinga <slouken@lokigames.com>
parents:
diff changeset
   159
						SDL_timers = next;
cf2af46e9e2a Changes since SDL 1.2.0 release
Sam Lantinga <slouken@lokigames.com>
parents:
diff changeset
   160
					}
cf2af46e9e2a Changes since SDL 1.2.0 release
Sam Lantinga <slouken@lokigames.com>
parents:
diff changeset
   161
					free(t);
cf2af46e9e2a Changes since SDL 1.2.0 release
Sam Lantinga <slouken@lokigames.com>
parents:
diff changeset
   162
					-- num_timers;
cf2af46e9e2a Changes since SDL 1.2.0 release
Sam Lantinga <slouken@lokigames.com>
parents:
diff changeset
   163
					removed = 1;
cf2af46e9e2a Changes since SDL 1.2.0 release
Sam Lantinga <slouken@lokigames.com>
parents:
diff changeset
   164
				}
cf2af46e9e2a Changes since SDL 1.2.0 release
Sam Lantinga <slouken@lokigames.com>
parents:
diff changeset
   165
			}
cf2af46e9e2a Changes since SDL 1.2.0 release
Sam Lantinga <slouken@lokigames.com>
parents:
diff changeset
   166
		}
cf2af46e9e2a Changes since SDL 1.2.0 release
Sam Lantinga <slouken@lokigames.com>
parents:
diff changeset
   167
		/* Don't update prev if the timer has disappeared */
cf2af46e9e2a Changes since SDL 1.2.0 release
Sam Lantinga <slouken@lokigames.com>
parents:
diff changeset
   168
		if ( ! removed ) {
cf2af46e9e2a Changes since SDL 1.2.0 release
Sam Lantinga <slouken@lokigames.com>
parents:
diff changeset
   169
			prev = t;
cf2af46e9e2a Changes since SDL 1.2.0 release
Sam Lantinga <slouken@lokigames.com>
parents:
diff changeset
   170
		}
cf2af46e9e2a Changes since SDL 1.2.0 release
Sam Lantinga <slouken@lokigames.com>
parents:
diff changeset
   171
	}
cf2af46e9e2a Changes since SDL 1.2.0 release
Sam Lantinga <slouken@lokigames.com>
parents:
diff changeset
   172
	SDL_mutexV(SDL_timer_mutex);
cf2af46e9e2a Changes since SDL 1.2.0 release
Sam Lantinga <slouken@lokigames.com>
parents:
diff changeset
   173
}
cf2af46e9e2a Changes since SDL 1.2.0 release
Sam Lantinga <slouken@lokigames.com>
parents:
diff changeset
   174
cf2af46e9e2a Changes since SDL 1.2.0 release
Sam Lantinga <slouken@lokigames.com>
parents:
diff changeset
   175
SDL_TimerID SDL_AddTimer(Uint32 interval, SDL_NewTimerCallback callback, void *param)
cf2af46e9e2a Changes since SDL 1.2.0 release
Sam Lantinga <slouken@lokigames.com>
parents:
diff changeset
   176
{
cf2af46e9e2a Changes since SDL 1.2.0 release
Sam Lantinga <slouken@lokigames.com>
parents:
diff changeset
   177
	SDL_TimerID t;
cf2af46e9e2a Changes since SDL 1.2.0 release
Sam Lantinga <slouken@lokigames.com>
parents:
diff changeset
   178
	if ( ! SDL_timer_mutex ) {
cf2af46e9e2a Changes since SDL 1.2.0 release
Sam Lantinga <slouken@lokigames.com>
parents:
diff changeset
   179
		if ( SDL_timer_started ) {
cf2af46e9e2a Changes since SDL 1.2.0 release
Sam Lantinga <slouken@lokigames.com>
parents:
diff changeset
   180
			SDL_SetError("This platform doesn't support multiple timers");
cf2af46e9e2a Changes since SDL 1.2.0 release
Sam Lantinga <slouken@lokigames.com>
parents:
diff changeset
   181
		} else {
cf2af46e9e2a Changes since SDL 1.2.0 release
Sam Lantinga <slouken@lokigames.com>
parents:
diff changeset
   182
			SDL_SetError("You must call SDL_Init(SDL_INIT_TIMER) first");
cf2af46e9e2a Changes since SDL 1.2.0 release
Sam Lantinga <slouken@lokigames.com>
parents:
diff changeset
   183
		}
cf2af46e9e2a Changes since SDL 1.2.0 release
Sam Lantinga <slouken@lokigames.com>
parents:
diff changeset
   184
		return NULL;
cf2af46e9e2a Changes since SDL 1.2.0 release
Sam Lantinga <slouken@lokigames.com>
parents:
diff changeset
   185
	}
cf2af46e9e2a Changes since SDL 1.2.0 release
Sam Lantinga <slouken@lokigames.com>
parents:
diff changeset
   186
	if ( ! SDL_timer_threaded ) {
cf2af46e9e2a Changes since SDL 1.2.0 release
Sam Lantinga <slouken@lokigames.com>
parents:
diff changeset
   187
		SDL_SetError("Multiple timers require threaded events!");
cf2af46e9e2a Changes since SDL 1.2.0 release
Sam Lantinga <slouken@lokigames.com>
parents:
diff changeset
   188
		return NULL;
cf2af46e9e2a Changes since SDL 1.2.0 release
Sam Lantinga <slouken@lokigames.com>
parents:
diff changeset
   189
	}
cf2af46e9e2a Changes since SDL 1.2.0 release
Sam Lantinga <slouken@lokigames.com>
parents:
diff changeset
   190
	SDL_mutexP(SDL_timer_mutex);
cf2af46e9e2a Changes since SDL 1.2.0 release
Sam Lantinga <slouken@lokigames.com>
parents:
diff changeset
   191
	t = (SDL_TimerID) malloc(sizeof(struct _SDL_TimerID));
cf2af46e9e2a Changes since SDL 1.2.0 release
Sam Lantinga <slouken@lokigames.com>
parents:
diff changeset
   192
	if ( t ) {
cf2af46e9e2a Changes since SDL 1.2.0 release
Sam Lantinga <slouken@lokigames.com>
parents:
diff changeset
   193
		t->interval = ROUND_RESOLUTION(interval);
cf2af46e9e2a Changes since SDL 1.2.0 release
Sam Lantinga <slouken@lokigames.com>
parents:
diff changeset
   194
		t->cb = callback;
cf2af46e9e2a Changes since SDL 1.2.0 release
Sam Lantinga <slouken@lokigames.com>
parents:
diff changeset
   195
		t->param = param;
cf2af46e9e2a Changes since SDL 1.2.0 release
Sam Lantinga <slouken@lokigames.com>
parents:
diff changeset
   196
		t->last_alarm = SDL_GetTicks();
cf2af46e9e2a Changes since SDL 1.2.0 release
Sam Lantinga <slouken@lokigames.com>
parents:
diff changeset
   197
		t->next = SDL_timers;
cf2af46e9e2a Changes since SDL 1.2.0 release
Sam Lantinga <slouken@lokigames.com>
parents:
diff changeset
   198
		SDL_timers = t;
cf2af46e9e2a Changes since SDL 1.2.0 release
Sam Lantinga <slouken@lokigames.com>
parents:
diff changeset
   199
		++ num_timers;
cf2af46e9e2a Changes since SDL 1.2.0 release
Sam Lantinga <slouken@lokigames.com>
parents:
diff changeset
   200
		list_changed = SDL_TRUE;
cf2af46e9e2a Changes since SDL 1.2.0 release
Sam Lantinga <slouken@lokigames.com>
parents:
diff changeset
   201
		SDL_timer_running = 1;
cf2af46e9e2a Changes since SDL 1.2.0 release
Sam Lantinga <slouken@lokigames.com>
parents:
diff changeset
   202
	}
cf2af46e9e2a Changes since SDL 1.2.0 release
Sam Lantinga <slouken@lokigames.com>
parents:
diff changeset
   203
#ifdef DEBUG_TIMERS
cf2af46e9e2a Changes since SDL 1.2.0 release
Sam Lantinga <slouken@lokigames.com>
parents:
diff changeset
   204
	printf("SDL_AddTimer(%d) = %08x num_timers = %d\n", interval, (Uint32)t, num_timers);
cf2af46e9e2a Changes since SDL 1.2.0 release
Sam Lantinga <slouken@lokigames.com>
parents:
diff changeset
   205
#endif
cf2af46e9e2a Changes since SDL 1.2.0 release
Sam Lantinga <slouken@lokigames.com>
parents:
diff changeset
   206
	SDL_mutexV(SDL_timer_mutex);
cf2af46e9e2a Changes since SDL 1.2.0 release
Sam Lantinga <slouken@lokigames.com>
parents:
diff changeset
   207
	return t;
cf2af46e9e2a Changes since SDL 1.2.0 release
Sam Lantinga <slouken@lokigames.com>
parents:
diff changeset
   208
}
cf2af46e9e2a Changes since SDL 1.2.0 release
Sam Lantinga <slouken@lokigames.com>
parents:
diff changeset
   209
cf2af46e9e2a Changes since SDL 1.2.0 release
Sam Lantinga <slouken@lokigames.com>
parents:
diff changeset
   210
SDL_bool SDL_RemoveTimer(SDL_TimerID id)
cf2af46e9e2a Changes since SDL 1.2.0 release
Sam Lantinga <slouken@lokigames.com>
parents:
diff changeset
   211
{
cf2af46e9e2a Changes since SDL 1.2.0 release
Sam Lantinga <slouken@lokigames.com>
parents:
diff changeset
   212
	SDL_TimerID t, prev = NULL;
cf2af46e9e2a Changes since SDL 1.2.0 release
Sam Lantinga <slouken@lokigames.com>
parents:
diff changeset
   213
	SDL_bool removed;
cf2af46e9e2a Changes since SDL 1.2.0 release
Sam Lantinga <slouken@lokigames.com>
parents:
diff changeset
   214
cf2af46e9e2a Changes since SDL 1.2.0 release
Sam Lantinga <slouken@lokigames.com>
parents:
diff changeset
   215
	removed = SDL_FALSE;
cf2af46e9e2a Changes since SDL 1.2.0 release
Sam Lantinga <slouken@lokigames.com>
parents:
diff changeset
   216
	SDL_mutexP(SDL_timer_mutex);
cf2af46e9e2a Changes since SDL 1.2.0 release
Sam Lantinga <slouken@lokigames.com>
parents:
diff changeset
   217
	/* Look for id in the linked list of timers */
cf2af46e9e2a Changes since SDL 1.2.0 release
Sam Lantinga <slouken@lokigames.com>
parents:
diff changeset
   218
	for (t = SDL_timers; t; prev=t, t = t->next ) {
cf2af46e9e2a Changes since SDL 1.2.0 release
Sam Lantinga <slouken@lokigames.com>
parents:
diff changeset
   219
		if ( t == id ) {
cf2af46e9e2a Changes since SDL 1.2.0 release
Sam Lantinga <slouken@lokigames.com>
parents:
diff changeset
   220
			if(prev) {
cf2af46e9e2a Changes since SDL 1.2.0 release
Sam Lantinga <slouken@lokigames.com>
parents:
diff changeset
   221
				prev->next = t->next;
cf2af46e9e2a Changes since SDL 1.2.0 release
Sam Lantinga <slouken@lokigames.com>
parents:
diff changeset
   222
			} else {
cf2af46e9e2a Changes since SDL 1.2.0 release
Sam Lantinga <slouken@lokigames.com>
parents:
diff changeset
   223
				SDL_timers = t->next;
cf2af46e9e2a Changes since SDL 1.2.0 release
Sam Lantinga <slouken@lokigames.com>
parents:
diff changeset
   224
			}
cf2af46e9e2a Changes since SDL 1.2.0 release
Sam Lantinga <slouken@lokigames.com>
parents:
diff changeset
   225
			free(t);
cf2af46e9e2a Changes since SDL 1.2.0 release
Sam Lantinga <slouken@lokigames.com>
parents:
diff changeset
   226
			-- num_timers;
cf2af46e9e2a Changes since SDL 1.2.0 release
Sam Lantinga <slouken@lokigames.com>
parents:
diff changeset
   227
			removed = SDL_TRUE;
cf2af46e9e2a Changes since SDL 1.2.0 release
Sam Lantinga <slouken@lokigames.com>
parents:
diff changeset
   228
			list_changed = SDL_TRUE;
cf2af46e9e2a Changes since SDL 1.2.0 release
Sam Lantinga <slouken@lokigames.com>
parents:
diff changeset
   229
			break;
cf2af46e9e2a Changes since SDL 1.2.0 release
Sam Lantinga <slouken@lokigames.com>
parents:
diff changeset
   230
		}
cf2af46e9e2a Changes since SDL 1.2.0 release
Sam Lantinga <slouken@lokigames.com>
parents:
diff changeset
   231
	}
cf2af46e9e2a Changes since SDL 1.2.0 release
Sam Lantinga <slouken@lokigames.com>
parents:
diff changeset
   232
#ifdef DEBUG_TIMERS
cf2af46e9e2a Changes since SDL 1.2.0 release
Sam Lantinga <slouken@lokigames.com>
parents:
diff changeset
   233
	printf("SDL_RemoveTimer(%08x) = %d num_timers = %d thread = %d\n", (Uint32)id, removed, num_timers, SDL_ThreadID());
cf2af46e9e2a Changes since SDL 1.2.0 release
Sam Lantinga <slouken@lokigames.com>
parents:
diff changeset
   234
#endif
cf2af46e9e2a Changes since SDL 1.2.0 release
Sam Lantinga <slouken@lokigames.com>
parents:
diff changeset
   235
	SDL_mutexV(SDL_timer_mutex);
cf2af46e9e2a Changes since SDL 1.2.0 release
Sam Lantinga <slouken@lokigames.com>
parents:
diff changeset
   236
	return removed;
cf2af46e9e2a Changes since SDL 1.2.0 release
Sam Lantinga <slouken@lokigames.com>
parents:
diff changeset
   237
}
cf2af46e9e2a Changes since SDL 1.2.0 release
Sam Lantinga <slouken@lokigames.com>
parents:
diff changeset
   238
cf2af46e9e2a Changes since SDL 1.2.0 release
Sam Lantinga <slouken@lokigames.com>
parents:
diff changeset
   239
static void SDL_RemoveAllTimers(SDL_TimerID t)
cf2af46e9e2a Changes since SDL 1.2.0 release
Sam Lantinga <slouken@lokigames.com>
parents:
diff changeset
   240
{
cf2af46e9e2a Changes since SDL 1.2.0 release
Sam Lantinga <slouken@lokigames.com>
parents:
diff changeset
   241
	SDL_TimerID freeme;
cf2af46e9e2a Changes since SDL 1.2.0 release
Sam Lantinga <slouken@lokigames.com>
parents:
diff changeset
   242
cf2af46e9e2a Changes since SDL 1.2.0 release
Sam Lantinga <slouken@lokigames.com>
parents:
diff changeset
   243
	/* Changed to non-recursive implementation.
cf2af46e9e2a Changes since SDL 1.2.0 release
Sam Lantinga <slouken@lokigames.com>
parents:
diff changeset
   244
	   The recursive implementation is elegant, but subject to 
cf2af46e9e2a Changes since SDL 1.2.0 release
Sam Lantinga <slouken@lokigames.com>
parents:
diff changeset
   245
	   stack overflow if there are lots and lots of timers.
cf2af46e9e2a Changes since SDL 1.2.0 release
Sam Lantinga <slouken@lokigames.com>
parents:
diff changeset
   246
	 */
cf2af46e9e2a Changes since SDL 1.2.0 release
Sam Lantinga <slouken@lokigames.com>
parents:
diff changeset
   247
	while ( t ) {
cf2af46e9e2a Changes since SDL 1.2.0 release
Sam Lantinga <slouken@lokigames.com>
parents:
diff changeset
   248
		freeme = t;
cf2af46e9e2a Changes since SDL 1.2.0 release
Sam Lantinga <slouken@lokigames.com>
parents:
diff changeset
   249
		t = t->next;
cf2af46e9e2a Changes since SDL 1.2.0 release
Sam Lantinga <slouken@lokigames.com>
parents:
diff changeset
   250
		free(freeme);
cf2af46e9e2a Changes since SDL 1.2.0 release
Sam Lantinga <slouken@lokigames.com>
parents:
diff changeset
   251
	}
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
cf2af46e9e2a Changes since SDL 1.2.0 release
Sam Lantinga <slouken@lokigames.com>
parents:
diff changeset
   254
/* Old style callback functions are wrapped through this */
cf2af46e9e2a Changes since SDL 1.2.0 release
Sam Lantinga <slouken@lokigames.com>
parents:
diff changeset
   255
static Uint32 callback_wrapper(Uint32 ms, void *param)
cf2af46e9e2a Changes since SDL 1.2.0 release
Sam Lantinga <slouken@lokigames.com>
parents:
diff changeset
   256
{
cf2af46e9e2a Changes since SDL 1.2.0 release
Sam Lantinga <slouken@lokigames.com>
parents:
diff changeset
   257
	SDL_TimerCallback func = (SDL_TimerCallback) param;
cf2af46e9e2a Changes since SDL 1.2.0 release
Sam Lantinga <slouken@lokigames.com>
parents:
diff changeset
   258
	return (*func)(ms);
cf2af46e9e2a Changes since SDL 1.2.0 release
Sam Lantinga <slouken@lokigames.com>
parents:
diff changeset
   259
}
cf2af46e9e2a Changes since SDL 1.2.0 release
Sam Lantinga <slouken@lokigames.com>
parents:
diff changeset
   260
cf2af46e9e2a Changes since SDL 1.2.0 release
Sam Lantinga <slouken@lokigames.com>
parents:
diff changeset
   261
int SDL_SetTimer(Uint32 ms, SDL_TimerCallback callback)
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
	int retval;
cf2af46e9e2a Changes since SDL 1.2.0 release
Sam Lantinga <slouken@lokigames.com>
parents:
diff changeset
   264
cf2af46e9e2a Changes since SDL 1.2.0 release
Sam Lantinga <slouken@lokigames.com>
parents:
diff changeset
   265
#ifdef DEBUG_TIMERS
cf2af46e9e2a Changes since SDL 1.2.0 release
Sam Lantinga <slouken@lokigames.com>
parents:
diff changeset
   266
	printf("SDL_SetTimer(%d)\n", ms);
cf2af46e9e2a Changes since SDL 1.2.0 release
Sam Lantinga <slouken@lokigames.com>
parents:
diff changeset
   267
#endif
cf2af46e9e2a Changes since SDL 1.2.0 release
Sam Lantinga <slouken@lokigames.com>
parents:
diff changeset
   268
	retval = 0;
cf2af46e9e2a Changes since SDL 1.2.0 release
Sam Lantinga <slouken@lokigames.com>
parents:
diff changeset
   269
	if ( SDL_timer_running ) {	/* Stop any currently running timer */
cf2af46e9e2a Changes since SDL 1.2.0 release
Sam Lantinga <slouken@lokigames.com>
parents:
diff changeset
   270
		SDL_timer_running = 0;
cf2af46e9e2a Changes since SDL 1.2.0 release
Sam Lantinga <slouken@lokigames.com>
parents:
diff changeset
   271
		if ( SDL_timer_threaded ) {
cf2af46e9e2a Changes since SDL 1.2.0 release
Sam Lantinga <slouken@lokigames.com>
parents:
diff changeset
   272
			SDL_mutexP(SDL_timer_mutex);
cf2af46e9e2a Changes since SDL 1.2.0 release
Sam Lantinga <slouken@lokigames.com>
parents:
diff changeset
   273
			SDL_RemoveAllTimers(SDL_timers);
cf2af46e9e2a Changes since SDL 1.2.0 release
Sam Lantinga <slouken@lokigames.com>
parents:
diff changeset
   274
			SDL_timers = NULL;
cf2af46e9e2a Changes since SDL 1.2.0 release
Sam Lantinga <slouken@lokigames.com>
parents:
diff changeset
   275
			SDL_mutexV(SDL_timer_mutex);
cf2af46e9e2a Changes since SDL 1.2.0 release
Sam Lantinga <slouken@lokigames.com>
parents:
diff changeset
   276
		} else {
cf2af46e9e2a Changes since SDL 1.2.0 release
Sam Lantinga <slouken@lokigames.com>
parents:
diff changeset
   277
			SDL_SYS_StopTimer();
cf2af46e9e2a Changes since SDL 1.2.0 release
Sam Lantinga <slouken@lokigames.com>
parents:
diff changeset
   278
		}
cf2af46e9e2a Changes since SDL 1.2.0 release
Sam Lantinga <slouken@lokigames.com>
parents:
diff changeset
   279
	}
cf2af46e9e2a Changes since SDL 1.2.0 release
Sam Lantinga <slouken@lokigames.com>
parents:
diff changeset
   280
	if ( ms ) {
cf2af46e9e2a Changes since SDL 1.2.0 release
Sam Lantinga <slouken@lokigames.com>
parents:
diff changeset
   281
		if ( SDL_timer_threaded ) {
cf2af46e9e2a Changes since SDL 1.2.0 release
Sam Lantinga <slouken@lokigames.com>
parents:
diff changeset
   282
			retval = (SDL_AddTimer(ms, callback_wrapper,
cf2af46e9e2a Changes since SDL 1.2.0 release
Sam Lantinga <slouken@lokigames.com>
parents:
diff changeset
   283
					       (void *)callback) != NULL);
cf2af46e9e2a Changes since SDL 1.2.0 release
Sam Lantinga <slouken@lokigames.com>
parents:
diff changeset
   284
		} else {
cf2af46e9e2a Changes since SDL 1.2.0 release
Sam Lantinga <slouken@lokigames.com>
parents:
diff changeset
   285
			SDL_timer_running = 1;
cf2af46e9e2a Changes since SDL 1.2.0 release
Sam Lantinga <slouken@lokigames.com>
parents:
diff changeset
   286
			SDL_alarm_interval = ms;
cf2af46e9e2a Changes since SDL 1.2.0 release
Sam Lantinga <slouken@lokigames.com>
parents:
diff changeset
   287
			SDL_alarm_callback = callback;
cf2af46e9e2a Changes since SDL 1.2.0 release
Sam Lantinga <slouken@lokigames.com>
parents:
diff changeset
   288
			retval = SDL_SYS_StartTimer();
cf2af46e9e2a Changes since SDL 1.2.0 release
Sam Lantinga <slouken@lokigames.com>
parents:
diff changeset
   289
		}
cf2af46e9e2a Changes since SDL 1.2.0 release
Sam Lantinga <slouken@lokigames.com>
parents:
diff changeset
   290
	}
cf2af46e9e2a Changes since SDL 1.2.0 release
Sam Lantinga <slouken@lokigames.com>
parents:
diff changeset
   291
	return retval;
cf2af46e9e2a Changes since SDL 1.2.0 release
Sam Lantinga <slouken@lokigames.com>
parents:
diff changeset
   292
}