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