src/timer/SDL_timer.c
author Sam Lantinga <slouken@libsdl.org>
Wed, 01 Feb 2006 06:32:25 +0000
changeset 1312 c9b51268668f
parent 1190 173c063d4f55
child 1330 450721ad5436
permissions -rw-r--r--
Updated copyright information and removed rcs id lines (problematic in branch merges)
I batch edited these files, so please let me know if I've accidentally removed anybody's
credit here.
slouken@1
     1
/*
slouken@1
     2
    SDL - Simple DirectMedia Layer
slouken@1312
     3
    Copyright (C) 1997-2006 Sam Lantinga
slouken@1
     4
slouken@1
     5
    This library is free software; you can redistribute it and/or
slouken@1312
     6
    modify it under the terms of the GNU Lesser General Public
slouken@1
     7
    License as published by the Free Software Foundation; either
slouken@1312
     8
    version 2.1 of the License, or (at your option) any later version.
slouken@1
     9
slouken@1
    10
    This library is distributed in the hope that it will be useful,
slouken@1
    11
    but WITHOUT ANY WARRANTY; without even the implied warranty of
slouken@1
    12
    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
slouken@1312
    13
    Lesser General Public License for more details.
slouken@1
    14
slouken@1312
    15
    You should have received a copy of the GNU Lesser General Public
slouken@1312
    16
    License along with this library; if not, write to the Free Software
slouken@1312
    17
    Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301  USA
slouken@1
    18
slouken@1
    19
    Sam Lantinga
slouken@252
    20
    slouken@libsdl.org
slouken@1
    21
*/
slouken@1
    22
slouken@1
    23
#include <stdlib.h>
slouken@1
    24
#include <stdio.h>			/* For the definition of NULL */
slouken@1
    25
slouken@1
    26
#include "SDL_error.h"
slouken@1
    27
#include "SDL_timer.h"
slouken@1
    28
#include "SDL_timer_c.h"
slouken@1
    29
#include "SDL_mutex.h"
slouken@1
    30
#include "SDL_systimer.h"
slouken@1
    31
slouken@1
    32
/* #define DEBUG_TIMERS */
slouken@1
    33
slouken@1
    34
int SDL_timer_started = 0;
slouken@1
    35
int SDL_timer_running = 0;
slouken@1
    36
slouken@1
    37
/* Data to handle a single periodic alarm */
slouken@1
    38
Uint32 SDL_alarm_interval = 0;
slouken@1
    39
SDL_TimerCallback SDL_alarm_callback;
slouken@1
    40
slouken@1
    41
/* Data used for a thread-based timer */
slouken@1
    42
static int SDL_timer_threaded = 0;
slouken@1
    43
slouken@1
    44
struct _SDL_TimerID {
slouken@1
    45
	Uint32 interval;
slouken@1
    46
	SDL_NewTimerCallback cb;
slouken@1
    47
	void *param;
slouken@1
    48
	Uint32 last_alarm;
slouken@1
    49
	struct _SDL_TimerID *next;
slouken@1
    50
};
slouken@1
    51
slouken@1
    52
static SDL_TimerID SDL_timers = NULL;
slouken@1
    53
static SDL_mutex *SDL_timer_mutex;
slouken@1028
    54
static volatile SDL_bool list_changed = SDL_FALSE;
slouken@1
    55
slouken@1
    56
/* Set whether or not the timer should use a thread.
slouken@1
    57
   This should not be called while the timer subsystem is running.
slouken@1
    58
*/
slouken@1
    59
int SDL_SetTimerThreaded(int value)
slouken@1
    60
{
slouken@1
    61
	int retval;
slouken@1
    62
slouken@1
    63
	if ( SDL_timer_started ) {
slouken@1
    64
		SDL_SetError("Timer already initialized");
slouken@1
    65
		retval = -1;
slouken@1
    66
	} else {
slouken@1
    67
		retval = 0;
slouken@1
    68
		SDL_timer_threaded = value;
slouken@1
    69
	}
slouken@1
    70
	return retval;
slouken@1
    71
}
slouken@1
    72
slouken@1
    73
int SDL_TimerInit(void)
slouken@1
    74
{
slouken@1
    75
	int retval;
slouken@1
    76
slouken@1
    77
	retval = 0;
slouken@1028
    78
	if ( SDL_timer_started ) {
slouken@1028
    79
		SDL_TimerQuit();
slouken@1028
    80
	}
slouken@1
    81
	if ( ! SDL_timer_threaded ) {
slouken@1
    82
		retval = SDL_SYS_TimerInit();
slouken@1
    83
	}
slouken@1
    84
	if ( SDL_timer_threaded ) {
slouken@1
    85
		SDL_timer_mutex = SDL_CreateMutex();
slouken@1
    86
	}
slouken@1028
    87
	if ( retval == 0 ) {
slouken@1028
    88
		SDL_timer_started = 1;
slouken@1028
    89
	}
slouken@1
    90
	return(retval);
slouken@1
    91
}
slouken@1
    92
slouken@1
    93
void SDL_TimerQuit(void)
slouken@1
    94
{
slouken@1
    95
	SDL_SetTimer(0, NULL);
slouken@1
    96
	if ( SDL_timer_threaded < 2 ) {
slouken@1
    97
		SDL_SYS_TimerQuit();
slouken@1
    98
	}
slouken@1
    99
	if ( SDL_timer_threaded ) {
slouken@1
   100
		SDL_DestroyMutex(SDL_timer_mutex);
slouken@1
   101
	}
slouken@1
   102
	SDL_timer_started = 0;
slouken@1
   103
	SDL_timer_threaded = 0;
slouken@1
   104
}
slouken@1
   105
slouken@1
   106
void SDL_ThreadedTimerCheck(void)
slouken@1
   107
{
slouken@1
   108
	Uint32 now, ms;
slouken@1
   109
	SDL_TimerID t, prev, next;
slouken@1028
   110
	SDL_bool removed;
slouken@1
   111
slouken@1
   112
	SDL_mutexP(SDL_timer_mutex);
slouken@1028
   113
	list_changed = SDL_FALSE;
slouken@1028
   114
	now = SDL_GetTicks();
slouken@1
   115
	for ( prev = NULL, t = SDL_timers; t; t = next ) {
slouken@1028
   116
		removed = SDL_FALSE;
slouken@1
   117
		ms = t->interval - SDL_TIMESLICE;
slouken@1
   118
		next = t->next;
slouken@1028
   119
		if ( (int)(now - t->last_alarm) > (int)ms ) {
slouken@1028
   120
			struct _SDL_TimerID timer;
slouken@1028
   121
slouken@1
   122
			if ( (now - t->last_alarm) < t->interval ) {
slouken@1
   123
				t->last_alarm += t->interval;
slouken@1
   124
			} else {
slouken@1
   125
				t->last_alarm = now;
slouken@1
   126
			}
slouken@1
   127
#ifdef DEBUG_TIMERS
slouken@1
   128
			printf("Executing timer %p (thread = %d)\n",
slouken@1028
   129
				t, SDL_ThreadID());
slouken@1
   130
#endif
slouken@1028
   131
			timer = *t;
slouken@1
   132
			SDL_mutexV(SDL_timer_mutex);
slouken@1028
   133
			ms = timer.cb(timer.interval, timer.param);
slouken@1
   134
			SDL_mutexP(SDL_timer_mutex);
slouken@1
   135
			if ( list_changed ) {
slouken@1028
   136
				/* Abort, list of timers modified */
slouken@1028
   137
				/* FIXME: what if ms was changed? */
slouken@1
   138
				break;
slouken@1
   139
			}
slouken@1
   140
			if ( ms != t->interval ) {
slouken@1
   141
				if ( ms ) {
slouken@1
   142
					t->interval = ROUND_RESOLUTION(ms);
slouken@1028
   143
				} else {
slouken@1028
   144
					/* Remove timer from the list */
slouken@1
   145
#ifdef DEBUG_TIMERS
slouken@1
   146
					printf("SDL: Removing timer %p\n", t);
slouken@1
   147
#endif
slouken@1
   148
					if ( prev ) {
slouken@1
   149
						prev->next = next;
slouken@1
   150
					} else {
slouken@1
   151
						SDL_timers = next;
slouken@1
   152
					}
slouken@1
   153
					free(t);
slouken@1028
   154
					--SDL_timer_running;
slouken@1028
   155
					removed = SDL_TRUE;
slouken@1
   156
				}
slouken@1
   157
			}
slouken@1
   158
		}
slouken@1
   159
		/* Don't update prev if the timer has disappeared */
slouken@1
   160
		if ( ! removed ) {
slouken@1
   161
			prev = t;
slouken@1
   162
		}
slouken@1
   163
	}
slouken@1
   164
	SDL_mutexV(SDL_timer_mutex);
slouken@1
   165
}
slouken@1
   166
slouken@1028
   167
static SDL_TimerID SDL_AddTimerInternal(Uint32 interval, SDL_NewTimerCallback callback, void *param)
slouken@1028
   168
{
slouken@1028
   169
	SDL_TimerID t;
slouken@1028
   170
	t = (SDL_TimerID) malloc(sizeof(struct _SDL_TimerID));
slouken@1028
   171
	if ( t ) {
slouken@1028
   172
		t->interval = ROUND_RESOLUTION(interval);
slouken@1028
   173
		t->cb = callback;
slouken@1028
   174
		t->param = param;
slouken@1028
   175
		t->last_alarm = SDL_GetTicks();
slouken@1028
   176
		t->next = SDL_timers;
slouken@1028
   177
		SDL_timers = t;
slouken@1028
   178
		++SDL_timer_running;
slouken@1028
   179
		list_changed = SDL_TRUE;
slouken@1028
   180
	}
slouken@1028
   181
#ifdef DEBUG_TIMERS
slouken@1028
   182
	printf("SDL_AddTimer(%d) = %08x num_timers = %d\n", interval, (Uint32)t, SDL_timer_running);
slouken@1028
   183
#endif
slouken@1028
   184
	return t;
slouken@1028
   185
}
slouken@1028
   186
slouken@1
   187
SDL_TimerID SDL_AddTimer(Uint32 interval, SDL_NewTimerCallback callback, void *param)
slouken@1
   188
{
slouken@1
   189
	SDL_TimerID t;
slouken@1
   190
	if ( ! SDL_timer_mutex ) {
slouken@1
   191
		if ( SDL_timer_started ) {
slouken@1
   192
			SDL_SetError("This platform doesn't support multiple timers");
slouken@1
   193
		} else {
slouken@1
   194
			SDL_SetError("You must call SDL_Init(SDL_INIT_TIMER) first");
slouken@1
   195
		}
slouken@1
   196
		return NULL;
slouken@1
   197
	}
slouken@1
   198
	if ( ! SDL_timer_threaded ) {
slouken@1
   199
		SDL_SetError("Multiple timers require threaded events!");
slouken@1
   200
		return NULL;
slouken@1
   201
	}
slouken@1
   202
	SDL_mutexP(SDL_timer_mutex);
slouken@1028
   203
	t = SDL_AddTimerInternal(interval, callback, param);
slouken@1
   204
	SDL_mutexV(SDL_timer_mutex);
slouken@1
   205
	return t;
slouken@1
   206
}
slouken@1
   207
slouken@1
   208
SDL_bool SDL_RemoveTimer(SDL_TimerID id)
slouken@1
   209
{
slouken@1
   210
	SDL_TimerID t, prev = NULL;
slouken@1
   211
	SDL_bool removed;
slouken@1
   212
slouken@1
   213
	removed = SDL_FALSE;
slouken@1
   214
	SDL_mutexP(SDL_timer_mutex);
slouken@1
   215
	/* Look for id in the linked list of timers */
slouken@1
   216
	for (t = SDL_timers; t; prev=t, t = t->next ) {
slouken@1
   217
		if ( t == id ) {
slouken@1
   218
			if(prev) {
slouken@1
   219
				prev->next = t->next;
slouken@1
   220
			} else {
slouken@1
   221
				SDL_timers = t->next;
slouken@1
   222
			}
slouken@1
   223
			free(t);
slouken@1028
   224
			--SDL_timer_running;
slouken@1
   225
			removed = SDL_TRUE;
slouken@1
   226
			list_changed = SDL_TRUE;
slouken@1
   227
			break;
slouken@1
   228
		}
slouken@1
   229
	}
slouken@1
   230
#ifdef DEBUG_TIMERS
slouken@1028
   231
	printf("SDL_RemoveTimer(%08x) = %d num_timers = %d thread = %d\n", (Uint32)id, removed, SDL_timer_running, SDL_ThreadID());
slouken@1
   232
#endif
slouken@1
   233
	SDL_mutexV(SDL_timer_mutex);
slouken@1
   234
	return removed;
slouken@1
   235
}
slouken@1
   236
slouken@1
   237
/* Old style callback functions are wrapped through this */
icculus@1190
   238
static Uint32 SDLCALL callback_wrapper(Uint32 ms, void *param)
slouken@1
   239
{
slouken@1
   240
	SDL_TimerCallback func = (SDL_TimerCallback) param;
slouken@1
   241
	return (*func)(ms);
slouken@1
   242
}
slouken@1
   243
slouken@1
   244
int SDL_SetTimer(Uint32 ms, SDL_TimerCallback callback)
slouken@1
   245
{
slouken@1
   246
	int retval;
slouken@1
   247
slouken@1
   248
#ifdef DEBUG_TIMERS
slouken@1
   249
	printf("SDL_SetTimer(%d)\n", ms);
slouken@1
   250
#endif
slouken@1
   251
	retval = 0;
slouken@1028
   252
slouken@1028
   253
	if ( SDL_timer_threaded ) {
slouken@1028
   254
		SDL_mutexP(SDL_timer_mutex);
slouken@1028
   255
	}
slouken@1
   256
	if ( SDL_timer_running ) {	/* Stop any currently running timer */
slouken@1
   257
		if ( SDL_timer_threaded ) {
slouken@1028
   258
			while ( SDL_timers ) {
slouken@1028
   259
				SDL_TimerID freeme = SDL_timers;
slouken@1028
   260
				SDL_timers = SDL_timers->next;
slouken@1028
   261
				free(freeme);
slouken@1028
   262
			}
slouken@1028
   263
			SDL_timer_running = 0;
slouken@1028
   264
			list_changed = SDL_TRUE;
slouken@1
   265
		} else {
slouken@1
   266
			SDL_SYS_StopTimer();
slouken@1028
   267
			SDL_timer_running = 0;
slouken@1
   268
		}
slouken@1
   269
	}
slouken@1
   270
	if ( ms ) {
slouken@1
   271
		if ( SDL_timer_threaded ) {
slouken@1028
   272
			if ( SDL_AddTimerInternal(ms, callback_wrapper, (void *)callback) == NULL ) {
slouken@1028
   273
				retval = -1;
slouken@1028
   274
			}
slouken@1
   275
		} else {
slouken@1
   276
			SDL_timer_running = 1;
slouken@1
   277
			SDL_alarm_interval = ms;
slouken@1
   278
			SDL_alarm_callback = callback;
slouken@1
   279
			retval = SDL_SYS_StartTimer();
slouken@1
   280
		}
slouken@1
   281
	}
slouken@1028
   282
	if ( SDL_timer_threaded ) {
icculus@1117
   283
		SDL_mutexV(SDL_timer_mutex);
slouken@1028
   284
	}
slouken@1028
   285
slouken@1
   286
	return retval;
slouken@1
   287
}