src/timer/SDL_timer.c
author Ryan C. Gordon <icculus@icculus.org>
Tue, 27 Sep 2005 12:14:17 +0000
changeset 1147 b580f7201543
parent 1117 95b261f445b1
child 1190 173c063d4f55
permissions -rw-r--r--
From: "Alex Volkov" <avcp-sdlmail@usa.net>
To: "'A list for developers using the SDL library. \(includesSDL-announce\)'" <sdl@libsdl.org>
Date: Mon, 19 Sep 2005 18:59:43 -0400
Subject: [SDL] [patch] Volume multiplier bug in

The volume multiplier in SDL_MixAudio_MMX_S8() is formed from the wrong
register.
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@252
    22
    slouken@libsdl.org
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
/* Data used for a thread-based timer */
slouken@1
    49
static int SDL_timer_threaded = 0;
slouken@1
    50
slouken@1
    51
struct _SDL_TimerID {
slouken@1
    52
	Uint32 interval;
slouken@1
    53
	SDL_NewTimerCallback cb;
slouken@1
    54
	void *param;
slouken@1
    55
	Uint32 last_alarm;
slouken@1
    56
	struct _SDL_TimerID *next;
slouken@1
    57
};
slouken@1
    58
slouken@1
    59
static SDL_TimerID SDL_timers = NULL;
slouken@1
    60
static SDL_mutex *SDL_timer_mutex;
slouken@1028
    61
static volatile SDL_bool list_changed = SDL_FALSE;
slouken@1
    62
slouken@1
    63
/* Set whether or not the timer should use a thread.
slouken@1
    64
   This should not be called while the timer subsystem is running.
slouken@1
    65
*/
slouken@1
    66
int SDL_SetTimerThreaded(int value)
slouken@1
    67
{
slouken@1
    68
	int retval;
slouken@1
    69
slouken@1
    70
	if ( SDL_timer_started ) {
slouken@1
    71
		SDL_SetError("Timer already initialized");
slouken@1
    72
		retval = -1;
slouken@1
    73
	} else {
slouken@1
    74
		retval = 0;
slouken@1
    75
		SDL_timer_threaded = value;
slouken@1
    76
	}
slouken@1
    77
	return retval;
slouken@1
    78
}
slouken@1
    79
slouken@1
    80
int SDL_TimerInit(void)
slouken@1
    81
{
slouken@1
    82
	int retval;
slouken@1
    83
slouken@1
    84
	retval = 0;
slouken@1028
    85
	if ( SDL_timer_started ) {
slouken@1028
    86
		SDL_TimerQuit();
slouken@1028
    87
	}
slouken@1
    88
	if ( ! SDL_timer_threaded ) {
slouken@1
    89
		retval = SDL_SYS_TimerInit();
slouken@1
    90
	}
slouken@1
    91
	if ( SDL_timer_threaded ) {
slouken@1
    92
		SDL_timer_mutex = SDL_CreateMutex();
slouken@1
    93
	}
slouken@1028
    94
	if ( retval == 0 ) {
slouken@1028
    95
		SDL_timer_started = 1;
slouken@1028
    96
	}
slouken@1
    97
	return(retval);
slouken@1
    98
}
slouken@1
    99
slouken@1
   100
void SDL_TimerQuit(void)
slouken@1
   101
{
slouken@1
   102
	SDL_SetTimer(0, NULL);
slouken@1
   103
	if ( SDL_timer_threaded < 2 ) {
slouken@1
   104
		SDL_SYS_TimerQuit();
slouken@1
   105
	}
slouken@1
   106
	if ( SDL_timer_threaded ) {
slouken@1
   107
		SDL_DestroyMutex(SDL_timer_mutex);
slouken@1
   108
	}
slouken@1
   109
	SDL_timer_started = 0;
slouken@1
   110
	SDL_timer_threaded = 0;
slouken@1
   111
}
slouken@1
   112
slouken@1
   113
void SDL_ThreadedTimerCheck(void)
slouken@1
   114
{
slouken@1
   115
	Uint32 now, ms;
slouken@1
   116
	SDL_TimerID t, prev, next;
slouken@1028
   117
	SDL_bool removed;
slouken@1
   118
slouken@1
   119
	SDL_mutexP(SDL_timer_mutex);
slouken@1028
   120
	list_changed = SDL_FALSE;
slouken@1028
   121
	now = SDL_GetTicks();
slouken@1
   122
	for ( prev = NULL, t = SDL_timers; t; t = next ) {
slouken@1028
   123
		removed = SDL_FALSE;
slouken@1
   124
		ms = t->interval - SDL_TIMESLICE;
slouken@1
   125
		next = t->next;
slouken@1028
   126
		if ( (int)(now - t->last_alarm) > (int)ms ) {
slouken@1028
   127
			struct _SDL_TimerID timer;
slouken@1028
   128
slouken@1
   129
			if ( (now - t->last_alarm) < t->interval ) {
slouken@1
   130
				t->last_alarm += t->interval;
slouken@1
   131
			} else {
slouken@1
   132
				t->last_alarm = now;
slouken@1
   133
			}
slouken@1
   134
#ifdef DEBUG_TIMERS
slouken@1
   135
			printf("Executing timer %p (thread = %d)\n",
slouken@1028
   136
				t, SDL_ThreadID());
slouken@1
   137
#endif
slouken@1028
   138
			timer = *t;
slouken@1
   139
			SDL_mutexV(SDL_timer_mutex);
slouken@1028
   140
			ms = timer.cb(timer.interval, timer.param);
slouken@1
   141
			SDL_mutexP(SDL_timer_mutex);
slouken@1
   142
			if ( list_changed ) {
slouken@1028
   143
				/* Abort, list of timers modified */
slouken@1028
   144
				/* FIXME: what if ms was changed? */
slouken@1
   145
				break;
slouken@1
   146
			}
slouken@1
   147
			if ( ms != t->interval ) {
slouken@1
   148
				if ( ms ) {
slouken@1
   149
					t->interval = ROUND_RESOLUTION(ms);
slouken@1028
   150
				} else {
slouken@1028
   151
					/* Remove timer from the list */
slouken@1
   152
#ifdef DEBUG_TIMERS
slouken@1
   153
					printf("SDL: Removing timer %p\n", t);
slouken@1
   154
#endif
slouken@1
   155
					if ( prev ) {
slouken@1
   156
						prev->next = next;
slouken@1
   157
					} else {
slouken@1
   158
						SDL_timers = next;
slouken@1
   159
					}
slouken@1
   160
					free(t);
slouken@1028
   161
					--SDL_timer_running;
slouken@1028
   162
					removed = SDL_TRUE;
slouken@1
   163
				}
slouken@1
   164
			}
slouken@1
   165
		}
slouken@1
   166
		/* Don't update prev if the timer has disappeared */
slouken@1
   167
		if ( ! removed ) {
slouken@1
   168
			prev = t;
slouken@1
   169
		}
slouken@1
   170
	}
slouken@1
   171
	SDL_mutexV(SDL_timer_mutex);
slouken@1
   172
}
slouken@1
   173
slouken@1028
   174
static SDL_TimerID SDL_AddTimerInternal(Uint32 interval, SDL_NewTimerCallback callback, void *param)
slouken@1028
   175
{
slouken@1028
   176
	SDL_TimerID t;
slouken@1028
   177
	t = (SDL_TimerID) malloc(sizeof(struct _SDL_TimerID));
slouken@1028
   178
	if ( t ) {
slouken@1028
   179
		t->interval = ROUND_RESOLUTION(interval);
slouken@1028
   180
		t->cb = callback;
slouken@1028
   181
		t->param = param;
slouken@1028
   182
		t->last_alarm = SDL_GetTicks();
slouken@1028
   183
		t->next = SDL_timers;
slouken@1028
   184
		SDL_timers = t;
slouken@1028
   185
		++SDL_timer_running;
slouken@1028
   186
		list_changed = SDL_TRUE;
slouken@1028
   187
	}
slouken@1028
   188
#ifdef DEBUG_TIMERS
slouken@1028
   189
	printf("SDL_AddTimer(%d) = %08x num_timers = %d\n", interval, (Uint32)t, SDL_timer_running);
slouken@1028
   190
#endif
slouken@1028
   191
	return t;
slouken@1028
   192
}
slouken@1028
   193
slouken@1
   194
SDL_TimerID SDL_AddTimer(Uint32 interval, SDL_NewTimerCallback callback, void *param)
slouken@1
   195
{
slouken@1
   196
	SDL_TimerID t;
slouken@1
   197
	if ( ! SDL_timer_mutex ) {
slouken@1
   198
		if ( SDL_timer_started ) {
slouken@1
   199
			SDL_SetError("This platform doesn't support multiple timers");
slouken@1
   200
		} else {
slouken@1
   201
			SDL_SetError("You must call SDL_Init(SDL_INIT_TIMER) first");
slouken@1
   202
		}
slouken@1
   203
		return NULL;
slouken@1
   204
	}
slouken@1
   205
	if ( ! SDL_timer_threaded ) {
slouken@1
   206
		SDL_SetError("Multiple timers require threaded events!");
slouken@1
   207
		return NULL;
slouken@1
   208
	}
slouken@1
   209
	SDL_mutexP(SDL_timer_mutex);
slouken@1028
   210
	t = SDL_AddTimerInternal(interval, callback, param);
slouken@1
   211
	SDL_mutexV(SDL_timer_mutex);
slouken@1
   212
	return t;
slouken@1
   213
}
slouken@1
   214
slouken@1
   215
SDL_bool SDL_RemoveTimer(SDL_TimerID id)
slouken@1
   216
{
slouken@1
   217
	SDL_TimerID t, prev = NULL;
slouken@1
   218
	SDL_bool removed;
slouken@1
   219
slouken@1
   220
	removed = SDL_FALSE;
slouken@1
   221
	SDL_mutexP(SDL_timer_mutex);
slouken@1
   222
	/* Look for id in the linked list of timers */
slouken@1
   223
	for (t = SDL_timers; t; prev=t, t = t->next ) {
slouken@1
   224
		if ( t == id ) {
slouken@1
   225
			if(prev) {
slouken@1
   226
				prev->next = t->next;
slouken@1
   227
			} else {
slouken@1
   228
				SDL_timers = t->next;
slouken@1
   229
			}
slouken@1
   230
			free(t);
slouken@1028
   231
			--SDL_timer_running;
slouken@1
   232
			removed = SDL_TRUE;
slouken@1
   233
			list_changed = SDL_TRUE;
slouken@1
   234
			break;
slouken@1
   235
		}
slouken@1
   236
	}
slouken@1
   237
#ifdef DEBUG_TIMERS
slouken@1028
   238
	printf("SDL_RemoveTimer(%08x) = %d num_timers = %d thread = %d\n", (Uint32)id, removed, SDL_timer_running, SDL_ThreadID());
slouken@1
   239
#endif
slouken@1
   240
	SDL_mutexV(SDL_timer_mutex);
slouken@1
   241
	return removed;
slouken@1
   242
}
slouken@1
   243
slouken@1
   244
/* Old style callback functions are wrapped through this */
slouken@1
   245
static Uint32 callback_wrapper(Uint32 ms, void *param)
slouken@1
   246
{
slouken@1
   247
	SDL_TimerCallback func = (SDL_TimerCallback) param;
slouken@1
   248
	return (*func)(ms);
slouken@1
   249
}
slouken@1
   250
slouken@1
   251
int SDL_SetTimer(Uint32 ms, SDL_TimerCallback callback)
slouken@1
   252
{
slouken@1
   253
	int retval;
slouken@1
   254
slouken@1
   255
#ifdef DEBUG_TIMERS
slouken@1
   256
	printf("SDL_SetTimer(%d)\n", ms);
slouken@1
   257
#endif
slouken@1
   258
	retval = 0;
slouken@1028
   259
slouken@1028
   260
	if ( SDL_timer_threaded ) {
slouken@1028
   261
		SDL_mutexP(SDL_timer_mutex);
slouken@1028
   262
	}
slouken@1
   263
	if ( SDL_timer_running ) {	/* Stop any currently running timer */
slouken@1
   264
		if ( SDL_timer_threaded ) {
slouken@1028
   265
			while ( SDL_timers ) {
slouken@1028
   266
				SDL_TimerID freeme = SDL_timers;
slouken@1028
   267
				SDL_timers = SDL_timers->next;
slouken@1028
   268
				free(freeme);
slouken@1028
   269
			}
slouken@1028
   270
			SDL_timer_running = 0;
slouken@1028
   271
			list_changed = SDL_TRUE;
slouken@1
   272
		} else {
slouken@1
   273
			SDL_SYS_StopTimer();
slouken@1028
   274
			SDL_timer_running = 0;
slouken@1
   275
		}
slouken@1
   276
	}
slouken@1
   277
	if ( ms ) {
slouken@1
   278
		if ( SDL_timer_threaded ) {
slouken@1028
   279
			if ( SDL_AddTimerInternal(ms, callback_wrapper, (void *)callback) == NULL ) {
slouken@1028
   280
				retval = -1;
slouken@1028
   281
			}
slouken@1
   282
		} else {
slouken@1
   283
			SDL_timer_running = 1;
slouken@1
   284
			SDL_alarm_interval = ms;
slouken@1
   285
			SDL_alarm_callback = callback;
slouken@1
   286
			retval = SDL_SYS_StartTimer();
slouken@1
   287
		}
slouken@1
   288
	}
slouken@1028
   289
	if ( SDL_timer_threaded ) {
icculus@1117
   290
		SDL_mutexV(SDL_timer_mutex);
slouken@1028
   291
	}
slouken@1028
   292
slouken@1
   293
	return retval;
slouken@1
   294
}