src/timer/SDL_timer.c
author Ben Avison <bavison@riscosopen.org>
Thu, 31 Oct 2019 14:00:28 +0300
branchSDL-1.2
changeset 13219 4f88e197acad
parent 6137 4720145f848b
permissions -rw-r--r--
ARM: Create configure option --enable-arm-neon to govern assembly optimizations
---
configure.in | 39 +++++++++++++++++++++++++++++++++++++++
include/SDL_config.h.in | 1 +
include/SDL_cpuinfo.h | 3 +++
src/cpuinfo/SDL_cpuinfo.c | 37 +++++++++++++++++++++++++++++++++++++
4 files changed, 80 insertions(+)
slouken@1
     1
/*
slouken@1
     2
    SDL - Simple DirectMedia Layer
slouken@6137
     3
    Copyright (C) 1997-2012 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@1402
    22
#include "SDL_config.h"
slouken@1
    23
slouken@1
    24
#include "SDL_timer.h"
slouken@1
    25
#include "SDL_timer_c.h"
slouken@1
    26
#include "SDL_mutex.h"
slouken@1
    27
#include "SDL_systimer.h"
slouken@1
    28
slouken@1
    29
/* #define DEBUG_TIMERS */
slouken@1
    30
slouken@1
    31
int SDL_timer_started = 0;
slouken@1
    32
int SDL_timer_running = 0;
slouken@1
    33
slouken@1
    34
/* Data to handle a single periodic alarm */
slouken@1
    35
Uint32 SDL_alarm_interval = 0;
slouken@1
    36
SDL_TimerCallback SDL_alarm_callback;
slouken@1
    37
slouken@1
    38
/* Data used for a thread-based timer */
slouken@1
    39
static int SDL_timer_threaded = 0;
slouken@1
    40
slouken@1
    41
struct _SDL_TimerID {
slouken@1
    42
	Uint32 interval;
slouken@1
    43
	SDL_NewTimerCallback cb;
slouken@1
    44
	void *param;
slouken@1
    45
	Uint32 last_alarm;
slouken@1
    46
	struct _SDL_TimerID *next;
slouken@1
    47
};
slouken@1
    48
slouken@1
    49
static SDL_TimerID SDL_timers = NULL;
slouken@1
    50
static SDL_mutex *SDL_timer_mutex;
slouken@1028
    51
static volatile SDL_bool list_changed = SDL_FALSE;
slouken@1
    52
slouken@1
    53
/* Set whether or not the timer should use a thread.
slouken@1
    54
   This should not be called while the timer subsystem is running.
slouken@1
    55
*/
slouken@1
    56
int SDL_SetTimerThreaded(int value)
slouken@1
    57
{
slouken@1
    58
	int retval;
slouken@1
    59
slouken@1
    60
	if ( SDL_timer_started ) {
slouken@1
    61
		SDL_SetError("Timer already initialized");
slouken@1
    62
		retval = -1;
slouken@1
    63
	} else {
slouken@1
    64
		retval = 0;
slouken@1
    65
		SDL_timer_threaded = value;
slouken@1
    66
	}
slouken@1
    67
	return retval;
slouken@1
    68
}
slouken@1
    69
slouken@1
    70
int SDL_TimerInit(void)
slouken@1
    71
{
slouken@1
    72
	int retval;
slouken@1
    73
slouken@1
    74
	retval = 0;
slouken@1028
    75
	if ( SDL_timer_started ) {
slouken@1028
    76
		SDL_TimerQuit();
slouken@1028
    77
	}
slouken@1
    78
	if ( ! SDL_timer_threaded ) {
slouken@1
    79
		retval = SDL_SYS_TimerInit();
slouken@1
    80
	}
slouken@1
    81
	if ( SDL_timer_threaded ) {
slouken@1
    82
		SDL_timer_mutex = SDL_CreateMutex();
slouken@1
    83
	}
slouken@1028
    84
	if ( retval == 0 ) {
slouken@1028
    85
		SDL_timer_started = 1;
slouken@1028
    86
	}
slouken@1
    87
	return(retval);
slouken@1
    88
}
slouken@1
    89
slouken@1
    90
void SDL_TimerQuit(void)
slouken@1
    91
{
slouken@1
    92
	SDL_SetTimer(0, NULL);
slouken@1
    93
	if ( SDL_timer_threaded < 2 ) {
slouken@1
    94
		SDL_SYS_TimerQuit();
slouken@1
    95
	}
slouken@1
    96
	if ( SDL_timer_threaded ) {
slouken@1
    97
		SDL_DestroyMutex(SDL_timer_mutex);
slouken@1788
    98
		SDL_timer_mutex = NULL;
slouken@1
    99
	}
slouken@1
   100
	SDL_timer_started = 0;
slouken@1
   101
	SDL_timer_threaded = 0;
slouken@1
   102
}
slouken@1
   103
slouken@1
   104
void SDL_ThreadedTimerCheck(void)
slouken@1
   105
{
slouken@1
   106
	Uint32 now, ms;
slouken@1
   107
	SDL_TimerID t, prev, next;
slouken@1028
   108
	SDL_bool removed;
slouken@1
   109
slouken@1
   110
	SDL_mutexP(SDL_timer_mutex);
slouken@1028
   111
	list_changed = SDL_FALSE;
slouken@1028
   112
	now = SDL_GetTicks();
slouken@1
   113
	for ( prev = NULL, t = SDL_timers; t; t = next ) {
slouken@1028
   114
		removed = SDL_FALSE;
slouken@1
   115
		ms = t->interval - SDL_TIMESLICE;
slouken@1
   116
		next = t->next;
slouken@1028
   117
		if ( (int)(now - t->last_alarm) > (int)ms ) {
slouken@1028
   118
			struct _SDL_TimerID timer;
slouken@1028
   119
slouken@1
   120
			if ( (now - t->last_alarm) < t->interval ) {
slouken@1
   121
				t->last_alarm += t->interval;
slouken@1
   122
			} else {
slouken@1
   123
				t->last_alarm = now;
slouken@1
   124
			}
slouken@1
   125
#ifdef DEBUG_TIMERS
slouken@1
   126
			printf("Executing timer %p (thread = %d)\n",
slouken@1028
   127
				t, SDL_ThreadID());
slouken@1
   128
#endif
slouken@1028
   129
			timer = *t;
slouken@1
   130
			SDL_mutexV(SDL_timer_mutex);
slouken@1028
   131
			ms = timer.cb(timer.interval, timer.param);
slouken@1
   132
			SDL_mutexP(SDL_timer_mutex);
slouken@1
   133
			if ( list_changed ) {
slouken@1028
   134
				/* Abort, list of timers modified */
slouken@1028
   135
				/* FIXME: what if ms was changed? */
slouken@1
   136
				break;
slouken@1
   137
			}
slouken@1
   138
			if ( ms != t->interval ) {
slouken@1
   139
				if ( ms ) {
slouken@1
   140
					t->interval = ROUND_RESOLUTION(ms);
slouken@1028
   141
				} else {
slouken@1028
   142
					/* Remove timer from the list */
slouken@1
   143
#ifdef DEBUG_TIMERS
slouken@1
   144
					printf("SDL: Removing timer %p\n", t);
slouken@1
   145
#endif
slouken@1
   146
					if ( prev ) {
slouken@1
   147
						prev->next = next;
slouken@1
   148
					} else {
slouken@1
   149
						SDL_timers = next;
slouken@1
   150
					}
slouken@1336
   151
					SDL_free(t);
slouken@1028
   152
					--SDL_timer_running;
slouken@1028
   153
					removed = SDL_TRUE;
slouken@1
   154
				}
slouken@1
   155
			}
slouken@1
   156
		}
slouken@1
   157
		/* Don't update prev if the timer has disappeared */
slouken@1
   158
		if ( ! removed ) {
slouken@1
   159
			prev = t;
slouken@1
   160
		}
slouken@1
   161
	}
slouken@1
   162
	SDL_mutexV(SDL_timer_mutex);
slouken@1
   163
}
slouken@1
   164
slouken@1028
   165
static SDL_TimerID SDL_AddTimerInternal(Uint32 interval, SDL_NewTimerCallback callback, void *param)
slouken@1028
   166
{
slouken@1028
   167
	SDL_TimerID t;
slouken@1336
   168
	t = (SDL_TimerID) SDL_malloc(sizeof(struct _SDL_TimerID));
slouken@1028
   169
	if ( t ) {
slouken@1028
   170
		t->interval = ROUND_RESOLUTION(interval);
slouken@1028
   171
		t->cb = callback;
slouken@1028
   172
		t->param = param;
slouken@1028
   173
		t->last_alarm = SDL_GetTicks();
slouken@1028
   174
		t->next = SDL_timers;
slouken@1028
   175
		SDL_timers = t;
slouken@1028
   176
		++SDL_timer_running;
slouken@1028
   177
		list_changed = SDL_TRUE;
slouken@1028
   178
	}
slouken@1028
   179
#ifdef DEBUG_TIMERS
slouken@1028
   180
	printf("SDL_AddTimer(%d) = %08x num_timers = %d\n", interval, (Uint32)t, SDL_timer_running);
slouken@1028
   181
#endif
slouken@1028
   182
	return t;
slouken@1028
   183
}
slouken@1028
   184
slouken@1
   185
SDL_TimerID SDL_AddTimer(Uint32 interval, SDL_NewTimerCallback callback, void *param)
slouken@1
   186
{
slouken@1
   187
	SDL_TimerID t;
slouken@1
   188
	if ( ! SDL_timer_mutex ) {
slouken@1
   189
		if ( SDL_timer_started ) {
slouken@1
   190
			SDL_SetError("This platform doesn't support multiple timers");
slouken@1
   191
		} else {
slouken@1
   192
			SDL_SetError("You must call SDL_Init(SDL_INIT_TIMER) first");
slouken@1
   193
		}
slouken@1
   194
		return NULL;
slouken@1
   195
	}
slouken@1
   196
	if ( ! SDL_timer_threaded ) {
slouken@1
   197
		SDL_SetError("Multiple timers require threaded events!");
slouken@1
   198
		return NULL;
slouken@1
   199
	}
slouken@1
   200
	SDL_mutexP(SDL_timer_mutex);
slouken@1028
   201
	t = SDL_AddTimerInternal(interval, callback, param);
slouken@1
   202
	SDL_mutexV(SDL_timer_mutex);
slouken@1
   203
	return t;
slouken@1
   204
}
slouken@1
   205
slouken@1
   206
SDL_bool SDL_RemoveTimer(SDL_TimerID id)
slouken@1
   207
{
slouken@1
   208
	SDL_TimerID t, prev = NULL;
slouken@1
   209
	SDL_bool removed;
slouken@1
   210
slouken@1
   211
	removed = SDL_FALSE;
slouken@1
   212
	SDL_mutexP(SDL_timer_mutex);
slouken@1
   213
	/* Look for id in the linked list of timers */
slouken@1
   214
	for (t = SDL_timers; t; prev=t, t = t->next ) {
slouken@1
   215
		if ( t == id ) {
slouken@1
   216
			if(prev) {
slouken@1
   217
				prev->next = t->next;
slouken@1
   218
			} else {
slouken@1
   219
				SDL_timers = t->next;
slouken@1
   220
			}
slouken@1336
   221
			SDL_free(t);
slouken@1028
   222
			--SDL_timer_running;
slouken@1
   223
			removed = SDL_TRUE;
slouken@1
   224
			list_changed = SDL_TRUE;
slouken@1
   225
			break;
slouken@1
   226
		}
slouken@1
   227
	}
slouken@1
   228
#ifdef DEBUG_TIMERS
slouken@1028
   229
	printf("SDL_RemoveTimer(%08x) = %d num_timers = %d thread = %d\n", (Uint32)id, removed, SDL_timer_running, SDL_ThreadID());
slouken@1
   230
#endif
slouken@1
   231
	SDL_mutexV(SDL_timer_mutex);
slouken@1
   232
	return removed;
slouken@1
   233
}
slouken@1
   234
slouken@1
   235
/* Old style callback functions are wrapped through this */
icculus@1190
   236
static Uint32 SDLCALL callback_wrapper(Uint32 ms, void *param)
slouken@1
   237
{
slouken@1
   238
	SDL_TimerCallback func = (SDL_TimerCallback) param;
slouken@1
   239
	return (*func)(ms);
slouken@1
   240
}
slouken@1
   241
slouken@1
   242
int SDL_SetTimer(Uint32 ms, SDL_TimerCallback callback)
slouken@1
   243
{
slouken@1
   244
	int retval;
slouken@1
   245
slouken@1
   246
#ifdef DEBUG_TIMERS
slouken@1
   247
	printf("SDL_SetTimer(%d)\n", ms);
slouken@1
   248
#endif
slouken@1
   249
	retval = 0;
slouken@1028
   250
slouken@1028
   251
	if ( SDL_timer_threaded ) {
slouken@1028
   252
		SDL_mutexP(SDL_timer_mutex);
slouken@1028
   253
	}
slouken@1
   254
	if ( SDL_timer_running ) {	/* Stop any currently running timer */
slouken@1
   255
		if ( SDL_timer_threaded ) {
slouken@1028
   256
			while ( SDL_timers ) {
slouken@1028
   257
				SDL_TimerID freeme = SDL_timers;
slouken@1028
   258
				SDL_timers = SDL_timers->next;
slouken@1336
   259
				SDL_free(freeme);
slouken@1028
   260
			}
slouken@1028
   261
			SDL_timer_running = 0;
slouken@1028
   262
			list_changed = SDL_TRUE;
slouken@1
   263
		} else {
slouken@1
   264
			SDL_SYS_StopTimer();
slouken@1028
   265
			SDL_timer_running = 0;
slouken@1
   266
		}
slouken@1
   267
	}
slouken@1
   268
	if ( ms ) {
slouken@1
   269
		if ( SDL_timer_threaded ) {
slouken@1028
   270
			if ( SDL_AddTimerInternal(ms, callback_wrapper, (void *)callback) == NULL ) {
slouken@1028
   271
				retval = -1;
slouken@1028
   272
			}
slouken@1
   273
		} else {
slouken@1
   274
			SDL_timer_running = 1;
slouken@1
   275
			SDL_alarm_interval = ms;
slouken@1
   276
			SDL_alarm_callback = callback;
slouken@1
   277
			retval = SDL_SYS_StartTimer();
slouken@1
   278
		}
slouken@1
   279
	}
slouken@1028
   280
	if ( SDL_timer_threaded ) {
icculus@1117
   281
		SDL_mutexV(SDL_timer_mutex);
slouken@1028
   282
	}
slouken@1028
   283
slouken@1
   284
	return retval;
slouken@1
   285
}