src/timer/linux/SDL_systimer.c
author Sam Lantinga <slouken@libsdl.org>
Sat, 23 Mar 2002 20:54:17 +0000
changeset 316 d85fc19bf840
parent 297 f6ffac90895c
child 317 4e8827521296
permissions -rw-r--r--
Added UNIX RDTSC code by Lompak (disabled by default)
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
0
74212992fb08 Initial revision
Sam Lantinga <slouken@lokigames.com>
parents:
diff changeset
     1
/*
74212992fb08 Initial revision
Sam Lantinga <slouken@lokigames.com>
parents:
diff changeset
     2
    SDL - Simple DirectMedia Layer
297
f6ffac90895c Updated copyright information for 2002
Sam Lantinga <slouken@libsdl.org>
parents: 252
diff changeset
     3
    Copyright (C) 1997, 1998, 1999, 2000, 2001, 2002  Sam Lantinga
0
74212992fb08 Initial revision
Sam Lantinga <slouken@lokigames.com>
parents:
diff changeset
     4
74212992fb08 Initial revision
Sam Lantinga <slouken@lokigames.com>
parents:
diff changeset
     5
    This library is free software; you can redistribute it and/or
74212992fb08 Initial revision
Sam Lantinga <slouken@lokigames.com>
parents:
diff changeset
     6
    modify it under the terms of the GNU Library General Public
74212992fb08 Initial revision
Sam Lantinga <slouken@lokigames.com>
parents:
diff changeset
     7
    License as published by the Free Software Foundation; either
74212992fb08 Initial revision
Sam Lantinga <slouken@lokigames.com>
parents:
diff changeset
     8
    version 2 of the License, or (at your option) any later version.
74212992fb08 Initial revision
Sam Lantinga <slouken@lokigames.com>
parents:
diff changeset
     9
74212992fb08 Initial revision
Sam Lantinga <slouken@lokigames.com>
parents:
diff changeset
    10
    This library is distributed in the hope that it will be useful,
74212992fb08 Initial revision
Sam Lantinga <slouken@lokigames.com>
parents:
diff changeset
    11
    but WITHOUT ANY WARRANTY; without even the implied warranty of
74212992fb08 Initial revision
Sam Lantinga <slouken@lokigames.com>
parents:
diff changeset
    12
    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
74212992fb08 Initial revision
Sam Lantinga <slouken@lokigames.com>
parents:
diff changeset
    13
    Library General Public License for more details.
74212992fb08 Initial revision
Sam Lantinga <slouken@lokigames.com>
parents:
diff changeset
    14
74212992fb08 Initial revision
Sam Lantinga <slouken@lokigames.com>
parents:
diff changeset
    15
    You should have received a copy of the GNU Library General Public
74212992fb08 Initial revision
Sam Lantinga <slouken@lokigames.com>
parents:
diff changeset
    16
    License along with this library; if not, write to the Free
74212992fb08 Initial revision
Sam Lantinga <slouken@lokigames.com>
parents:
diff changeset
    17
    Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
74212992fb08 Initial revision
Sam Lantinga <slouken@lokigames.com>
parents:
diff changeset
    18
74212992fb08 Initial revision
Sam Lantinga <slouken@lokigames.com>
parents:
diff changeset
    19
    Sam Lantinga
252
e8157fcb3114 Updated the source with the correct e-mail address
Sam Lantinga <slouken@libsdl.org>
parents: 166
diff changeset
    20
    slouken@libsdl.org
316
d85fc19bf840 Added UNIX RDTSC code by Lompak (disabled by default)
Sam Lantinga <slouken@libsdl.org>
parents: 297
diff changeset
    21
d85fc19bf840 Added UNIX RDTSC code by Lompak (disabled by default)
Sam Lantinga <slouken@libsdl.org>
parents: 297
diff changeset
    22
    RDTSC stuff by lompik (lompik@voila.fr) 20/03/2002 
0
74212992fb08 Initial revision
Sam Lantinga <slouken@lokigames.com>
parents:
diff changeset
    23
*/
74212992fb08 Initial revision
Sam Lantinga <slouken@lokigames.com>
parents:
diff changeset
    24
74212992fb08 Initial revision
Sam Lantinga <slouken@lokigames.com>
parents:
diff changeset
    25
#ifdef SAVE_RCSID
74212992fb08 Initial revision
Sam Lantinga <slouken@lokigames.com>
parents:
diff changeset
    26
static char rcsid =
74212992fb08 Initial revision
Sam Lantinga <slouken@lokigames.com>
parents:
diff changeset
    27
 "@(#) $Id$";
74212992fb08 Initial revision
Sam Lantinga <slouken@lokigames.com>
parents:
diff changeset
    28
#endif
74212992fb08 Initial revision
Sam Lantinga <slouken@lokigames.com>
parents:
diff changeset
    29
74212992fb08 Initial revision
Sam Lantinga <slouken@lokigames.com>
parents:
diff changeset
    30
#include <stdio.h>
74212992fb08 Initial revision
Sam Lantinga <slouken@lokigames.com>
parents:
diff changeset
    31
#include <sys/time.h>
74212992fb08 Initial revision
Sam Lantinga <slouken@lokigames.com>
parents:
diff changeset
    32
#include <signal.h>
74212992fb08 Initial revision
Sam Lantinga <slouken@lokigames.com>
parents:
diff changeset
    33
#include <unistd.h>
74212992fb08 Initial revision
Sam Lantinga <slouken@lokigames.com>
parents:
diff changeset
    34
#include <string.h>
74212992fb08 Initial revision
Sam Lantinga <slouken@lokigames.com>
parents:
diff changeset
    35
#include <errno.h>
74212992fb08 Initial revision
Sam Lantinga <slouken@lokigames.com>
parents:
diff changeset
    36
74212992fb08 Initial revision
Sam Lantinga <slouken@lokigames.com>
parents:
diff changeset
    37
#include "SDL_error.h"
74212992fb08 Initial revision
Sam Lantinga <slouken@lokigames.com>
parents:
diff changeset
    38
#include "SDL_timer.h"
74212992fb08 Initial revision
Sam Lantinga <slouken@lokigames.com>
parents:
diff changeset
    39
#include "SDL_timer_c.h"
74212992fb08 Initial revision
Sam Lantinga <slouken@lokigames.com>
parents:
diff changeset
    40
1
cf2af46e9e2a Changes since SDL 1.2.0 release
Sam Lantinga <slouken@lokigames.com>
parents: 0
diff changeset
    41
#if _POSIX_THREAD_SYSCALL_SOFT
cf2af46e9e2a Changes since SDL 1.2.0 release
Sam Lantinga <slouken@lokigames.com>
parents: 0
diff changeset
    42
#include <pthread.h>
cf2af46e9e2a Changes since SDL 1.2.0 release
Sam Lantinga <slouken@lokigames.com>
parents: 0
diff changeset
    43
#endif
cf2af46e9e2a Changes since SDL 1.2.0 release
Sam Lantinga <slouken@lokigames.com>
parents: 0
diff changeset
    44
0
74212992fb08 Initial revision
Sam Lantinga <slouken@lokigames.com>
parents:
diff changeset
    45
#if defined(DISABLE_THREADS) || defined(FORK_HACK)
74212992fb08 Initial revision
Sam Lantinga <slouken@lokigames.com>
parents:
diff changeset
    46
#define USE_ITIMER
74212992fb08 Initial revision
Sam Lantinga <slouken@lokigames.com>
parents:
diff changeset
    47
#endif
74212992fb08 Initial revision
Sam Lantinga <slouken@lokigames.com>
parents:
diff changeset
    48
74212992fb08 Initial revision
Sam Lantinga <slouken@lokigames.com>
parents:
diff changeset
    49
/* The following defines should really be determined at configure time */
74212992fb08 Initial revision
Sam Lantinga <slouken@lokigames.com>
parents:
diff changeset
    50
166
39877400bd1e Fixed Solaris nitpicks (thanks Mattias!)
Sam Lantinga <slouken@libsdl.org>
parents: 1
diff changeset
    51
#if defined(linux)
0
74212992fb08 Initial revision
Sam Lantinga <slouken@lokigames.com>
parents:
diff changeset
    52
/* Linux select() changes its timeout parameter upon return to contain
74212992fb08 Initial revision
Sam Lantinga <slouken@lokigames.com>
parents:
diff changeset
    53
   the remaining time. Most other unixen leave it unchanged or undefined. */
74212992fb08 Initial revision
Sam Lantinga <slouken@lokigames.com>
parents:
diff changeset
    54
#define SELECT_SETS_REMAINING
166
39877400bd1e Fixed Solaris nitpicks (thanks Mattias!)
Sam Lantinga <slouken@libsdl.org>
parents: 1
diff changeset
    55
#elif defined(__bsdi__) || defined(__FreeBSD__) || defined(__sun)
0
74212992fb08 Initial revision
Sam Lantinga <slouken@lokigames.com>
parents:
diff changeset
    56
#define USE_NANOSLEEP
74212992fb08 Initial revision
Sam Lantinga <slouken@lokigames.com>
parents:
diff changeset
    57
#endif
74212992fb08 Initial revision
Sam Lantinga <slouken@lokigames.com>
parents:
diff changeset
    58
316
d85fc19bf840 Added UNIX RDTSC code by Lompak (disabled by default)
Sam Lantinga <slouken@libsdl.org>
parents: 297
diff changeset
    59
#if defined(i386) || defined(__i386__)
d85fc19bf840 Added UNIX RDTSC code by Lompak (disabled by default)
Sam Lantinga <slouken@libsdl.org>
parents: 297
diff changeset
    60
/* Actually, this isn't reliable on multi-cpu systems, so is disabled */
d85fc19bf840 Added UNIX RDTSC code by Lompak (disabled by default)
Sam Lantinga <slouken@libsdl.org>
parents: 297
diff changeset
    61
/*#define USE_RDTSC*/
d85fc19bf840 Added UNIX RDTSC code by Lompak (disabled by default)
Sam Lantinga <slouken@libsdl.org>
parents: 297
diff changeset
    62
#endif
d85fc19bf840 Added UNIX RDTSC code by Lompak (disabled by default)
Sam Lantinga <slouken@libsdl.org>
parents: 297
diff changeset
    63
d85fc19bf840 Added UNIX RDTSC code by Lompak (disabled by default)
Sam Lantinga <slouken@libsdl.org>
parents: 297
diff changeset
    64
d85fc19bf840 Added UNIX RDTSC code by Lompak (disabled by default)
Sam Lantinga <slouken@libsdl.org>
parents: 297
diff changeset
    65
#ifdef USE_RDTSC 
d85fc19bf840 Added UNIX RDTSC code by Lompak (disabled by default)
Sam Lantinga <slouken@libsdl.org>
parents: 297
diff changeset
    66
d85fc19bf840 Added UNIX RDTSC code by Lompak (disabled by default)
Sam Lantinga <slouken@libsdl.org>
parents: 297
diff changeset
    67
/* The first ticks value of the application */
d85fc19bf840 Added UNIX RDTSC code by Lompak (disabled by default)
Sam Lantinga <slouken@libsdl.org>
parents: 297
diff changeset
    68
static unsigned long long start;
d85fc19bf840 Added UNIX RDTSC code by Lompak (disabled by default)
Sam Lantinga <slouken@libsdl.org>
parents: 297
diff changeset
    69
static float cpu_mhz1000 = 0.0f;
d85fc19bf840 Added UNIX RDTSC code by Lompak (disabled by default)
Sam Lantinga <slouken@libsdl.org>
parents: 297
diff changeset
    70
d85fc19bf840 Added UNIX RDTSC code by Lompak (disabled by default)
Sam Lantinga <slouken@libsdl.org>
parents: 297
diff changeset
    71
#if 1
d85fc19bf840 Added UNIX RDTSC code by Lompak (disabled by default)
Sam Lantinga <slouken@libsdl.org>
parents: 297
diff changeset
    72
/* This is for old binutils version that don't recognize rdtsc mnemonics.
d85fc19bf840 Added UNIX RDTSC code by Lompak (disabled by default)
Sam Lantinga <slouken@libsdl.org>
parents: 297
diff changeset
    73
   But all binutils version supports this.
d85fc19bf840 Added UNIX RDTSC code by Lompak (disabled by default)
Sam Lantinga <slouken@libsdl.org>
parents: 297
diff changeset
    74
*/
d85fc19bf840 Added UNIX RDTSC code by Lompak (disabled by default)
Sam Lantinga <slouken@libsdl.org>
parents: 297
diff changeset
    75
#define rdtsc(t) asm(".byte 0x0f, 0x31; " : "=A" (t));   
d85fc19bf840 Added UNIX RDTSC code by Lompak (disabled by default)
Sam Lantinga <slouken@libsdl.org>
parents: 297
diff changeset
    76
#else
d85fc19bf840 Added UNIX RDTSC code by Lompak (disabled by default)
Sam Lantinga <slouken@libsdl.org>
parents: 297
diff changeset
    77
#define rdtsc(t) asm("rdtsc" : "=A" (t));
d85fc19bf840 Added UNIX RDTSC code by Lompak (disabled by default)
Sam Lantinga <slouken@libsdl.org>
parents: 297
diff changeset
    78
#endif
d85fc19bf840 Added UNIX RDTSC code by Lompak (disabled by default)
Sam Lantinga <slouken@libsdl.org>
parents: 297
diff changeset
    79
d85fc19bf840 Added UNIX RDTSC code by Lompak (disabled by default)
Sam Lantinga <slouken@libsdl.org>
parents: 297
diff changeset
    80
static float calc_cpu_mhz(void)
d85fc19bf840 Added UNIX RDTSC code by Lompak (disabled by default)
Sam Lantinga <slouken@libsdl.org>
parents: 297
diff changeset
    81
{
d85fc19bf840 Added UNIX RDTSC code by Lompak (disabled by default)
Sam Lantinga <slouken@libsdl.org>
parents: 297
diff changeset
    82
	float cpu_mhz;
d85fc19bf840 Added UNIX RDTSC code by Lompak (disabled by default)
Sam Lantinga <slouken@libsdl.org>
parents: 297
diff changeset
    83
	unsigned long long tsc_start;
d85fc19bf840 Added UNIX RDTSC code by Lompak (disabled by default)
Sam Lantinga <slouken@libsdl.org>
parents: 297
diff changeset
    84
	unsigned long long tsc_end;
d85fc19bf840 Added UNIX RDTSC code by Lompak (disabled by default)
Sam Lantinga <slouken@libsdl.org>
parents: 297
diff changeset
    85
	struct timeval tv_start, tv_end;
d85fc19bf840 Added UNIX RDTSC code by Lompak (disabled by default)
Sam Lantinga <slouken@libsdl.org>
parents: 297
diff changeset
    86
	long usec_delay;
d85fc19bf840 Added UNIX RDTSC code by Lompak (disabled by default)
Sam Lantinga <slouken@libsdl.org>
parents: 297
diff changeset
    87
d85fc19bf840 Added UNIX RDTSC code by Lompak (disabled by default)
Sam Lantinga <slouken@libsdl.org>
parents: 297
diff changeset
    88
	rdtsc(tsc_start);
d85fc19bf840 Added UNIX RDTSC code by Lompak (disabled by default)
Sam Lantinga <slouken@libsdl.org>
parents: 297
diff changeset
    89
	gettimeofday(&tv_start, NULL);
d85fc19bf840 Added UNIX RDTSC code by Lompak (disabled by default)
Sam Lantinga <slouken@libsdl.org>
parents: 297
diff changeset
    90
	sleep(1);
d85fc19bf840 Added UNIX RDTSC code by Lompak (disabled by default)
Sam Lantinga <slouken@libsdl.org>
parents: 297
diff changeset
    91
	rdtsc(tsc_end);
d85fc19bf840 Added UNIX RDTSC code by Lompak (disabled by default)
Sam Lantinga <slouken@libsdl.org>
parents: 297
diff changeset
    92
	gettimeofday(&tv_end, NULL);
d85fc19bf840 Added UNIX RDTSC code by Lompak (disabled by default)
Sam Lantinga <slouken@libsdl.org>
parents: 297
diff changeset
    93
	usec_delay = 1000000L * (tv_end.tv_sec - tv_start.tv_sec) +
d85fc19bf840 Added UNIX RDTSC code by Lompak (disabled by default)
Sam Lantinga <slouken@libsdl.org>
parents: 297
diff changeset
    94
	                        (tv_end.tv_usec - tv_start.tv_usec);
d85fc19bf840 Added UNIX RDTSC code by Lompak (disabled by default)
Sam Lantinga <slouken@libsdl.org>
parents: 297
diff changeset
    95
	cpu_mhz = (float)(tsc_end-tsc_start) / usec_delay;
d85fc19bf840 Added UNIX RDTSC code by Lompak (disabled by default)
Sam Lantinga <slouken@libsdl.org>
parents: 297
diff changeset
    96
#if 0
d85fc19bf840 Added UNIX RDTSC code by Lompak (disabled by default)
Sam Lantinga <slouken@libsdl.org>
parents: 297
diff changeset
    97
	printf("cpu MHz\t\t: %.3f\n", cpu_mhz);
d85fc19bf840 Added UNIX RDTSC code by Lompak (disabled by default)
Sam Lantinga <slouken@libsdl.org>
parents: 297
diff changeset
    98
#endif
d85fc19bf840 Added UNIX RDTSC code by Lompak (disabled by default)
Sam Lantinga <slouken@libsdl.org>
parents: 297
diff changeset
    99
	return cpu_mhz;
d85fc19bf840 Added UNIX RDTSC code by Lompak (disabled by default)
Sam Lantinga <slouken@libsdl.org>
parents: 297
diff changeset
   100
}
d85fc19bf840 Added UNIX RDTSC code by Lompak (disabled by default)
Sam Lantinga <slouken@libsdl.org>
parents: 297
diff changeset
   101
d85fc19bf840 Added UNIX RDTSC code by Lompak (disabled by default)
Sam Lantinga <slouken@libsdl.org>
parents: 297
diff changeset
   102
#else
0
74212992fb08 Initial revision
Sam Lantinga <slouken@lokigames.com>
parents:
diff changeset
   103
74212992fb08 Initial revision
Sam Lantinga <slouken@lokigames.com>
parents:
diff changeset
   104
/* The first ticks value of the application */
74212992fb08 Initial revision
Sam Lantinga <slouken@lokigames.com>
parents:
diff changeset
   105
static struct timeval start;
74212992fb08 Initial revision
Sam Lantinga <slouken@lokigames.com>
parents:
diff changeset
   106
316
d85fc19bf840 Added UNIX RDTSC code by Lompak (disabled by default)
Sam Lantinga <slouken@libsdl.org>
parents: 297
diff changeset
   107
#endif  /* USE_RDTSC */
d85fc19bf840 Added UNIX RDTSC code by Lompak (disabled by default)
Sam Lantinga <slouken@libsdl.org>
parents: 297
diff changeset
   108
d85fc19bf840 Added UNIX RDTSC code by Lompak (disabled by default)
Sam Lantinga <slouken@libsdl.org>
parents: 297
diff changeset
   109
0
74212992fb08 Initial revision
Sam Lantinga <slouken@lokigames.com>
parents:
diff changeset
   110
void SDL_StartTicks(void)
74212992fb08 Initial revision
Sam Lantinga <slouken@lokigames.com>
parents:
diff changeset
   111
{
74212992fb08 Initial revision
Sam Lantinga <slouken@lokigames.com>
parents:
diff changeset
   112
	/* Set first ticks value */
316
d85fc19bf840 Added UNIX RDTSC code by Lompak (disabled by default)
Sam Lantinga <slouken@libsdl.org>
parents: 297
diff changeset
   113
#ifdef USE_RDTSC
d85fc19bf840 Added UNIX RDTSC code by Lompak (disabled by default)
Sam Lantinga <slouken@libsdl.org>
parents: 297
diff changeset
   114
	if ( ! cpu_mhz1000 ) {
d85fc19bf840 Added UNIX RDTSC code by Lompak (disabled by default)
Sam Lantinga <slouken@libsdl.org>
parents: 297
diff changeset
   115
		cpu_mhz1000 = calc_cpu_mhz() * 1000.0f;
d85fc19bf840 Added UNIX RDTSC code by Lompak (disabled by default)
Sam Lantinga <slouken@libsdl.org>
parents: 297
diff changeset
   116
	}
d85fc19bf840 Added UNIX RDTSC code by Lompak (disabled by default)
Sam Lantinga <slouken@libsdl.org>
parents: 297
diff changeset
   117
	rdtsc(start);
d85fc19bf840 Added UNIX RDTSC code by Lompak (disabled by default)
Sam Lantinga <slouken@libsdl.org>
parents: 297
diff changeset
   118
#else
0
74212992fb08 Initial revision
Sam Lantinga <slouken@lokigames.com>
parents:
diff changeset
   119
	gettimeofday(&start, NULL);
316
d85fc19bf840 Added UNIX RDTSC code by Lompak (disabled by default)
Sam Lantinga <slouken@libsdl.org>
parents: 297
diff changeset
   120
#endif /* USE_RDTSC */
0
74212992fb08 Initial revision
Sam Lantinga <slouken@lokigames.com>
parents:
diff changeset
   121
}
74212992fb08 Initial revision
Sam Lantinga <slouken@lokigames.com>
parents:
diff changeset
   122
74212992fb08 Initial revision
Sam Lantinga <slouken@lokigames.com>
parents:
diff changeset
   123
Uint32 SDL_GetTicks (void)
74212992fb08 Initial revision
Sam Lantinga <slouken@lokigames.com>
parents:
diff changeset
   124
{
316
d85fc19bf840 Added UNIX RDTSC code by Lompak (disabled by default)
Sam Lantinga <slouken@libsdl.org>
parents: 297
diff changeset
   125
#ifdef USE_RDTSC 
d85fc19bf840 Added UNIX RDTSC code by Lompak (disabled by default)
Sam Lantinga <slouken@libsdl.org>
parents: 297
diff changeset
   126
	unsigned long long now;
d85fc19bf840 Added UNIX RDTSC code by Lompak (disabled by default)
Sam Lantinga <slouken@libsdl.org>
parents: 297
diff changeset
   127
	if ( ! cpu_mhz1000 ) {
d85fc19bf840 Added UNIX RDTSC code by Lompak (disabled by default)
Sam Lantinga <slouken@libsdl.org>
parents: 297
diff changeset
   128
		return 0; /* Shouldn't happen. BUG!! */
d85fc19bf840 Added UNIX RDTSC code by Lompak (disabled by default)
Sam Lantinga <slouken@libsdl.org>
parents: 297
diff changeset
   129
	}
d85fc19bf840 Added UNIX RDTSC code by Lompak (disabled by default)
Sam Lantinga <slouken@libsdl.org>
parents: 297
diff changeset
   130
	rdtsc(now);
d85fc19bf840 Added UNIX RDTSC code by Lompak (disabled by default)
Sam Lantinga <slouken@libsdl.org>
parents: 297
diff changeset
   131
	return (Uint32)((now-start)/cpu_mhz1000);
d85fc19bf840 Added UNIX RDTSC code by Lompak (disabled by default)
Sam Lantinga <slouken@libsdl.org>
parents: 297
diff changeset
   132
#else
0
74212992fb08 Initial revision
Sam Lantinga <slouken@lokigames.com>
parents:
diff changeset
   133
	struct timeval now;
74212992fb08 Initial revision
Sam Lantinga <slouken@lokigames.com>
parents:
diff changeset
   134
	Uint32 ticks;
74212992fb08 Initial revision
Sam Lantinga <slouken@lokigames.com>
parents:
diff changeset
   135
74212992fb08 Initial revision
Sam Lantinga <slouken@lokigames.com>
parents:
diff changeset
   136
	gettimeofday(&now, NULL);
74212992fb08 Initial revision
Sam Lantinga <slouken@lokigames.com>
parents:
diff changeset
   137
	ticks=(now.tv_sec-start.tv_sec)*1000+(now.tv_usec-start.tv_usec)/1000;
74212992fb08 Initial revision
Sam Lantinga <slouken@lokigames.com>
parents:
diff changeset
   138
	return(ticks);
316
d85fc19bf840 Added UNIX RDTSC code by Lompak (disabled by default)
Sam Lantinga <slouken@libsdl.org>
parents: 297
diff changeset
   139
#endif /* USE_RDTSC */
0
74212992fb08 Initial revision
Sam Lantinga <slouken@lokigames.com>
parents:
diff changeset
   140
}
74212992fb08 Initial revision
Sam Lantinga <slouken@lokigames.com>
parents:
diff changeset
   141
74212992fb08 Initial revision
Sam Lantinga <slouken@lokigames.com>
parents:
diff changeset
   142
void SDL_Delay (Uint32 ms)
74212992fb08 Initial revision
Sam Lantinga <slouken@lokigames.com>
parents:
diff changeset
   143
{
74212992fb08 Initial revision
Sam Lantinga <slouken@lokigames.com>
parents:
diff changeset
   144
	int was_error;
74212992fb08 Initial revision
Sam Lantinga <slouken@lokigames.com>
parents:
diff changeset
   145
74212992fb08 Initial revision
Sam Lantinga <slouken@lokigames.com>
parents:
diff changeset
   146
#ifdef USE_NANOSLEEP
74212992fb08 Initial revision
Sam Lantinga <slouken@lokigames.com>
parents:
diff changeset
   147
	struct timespec elapsed, tv;
74212992fb08 Initial revision
Sam Lantinga <slouken@lokigames.com>
parents:
diff changeset
   148
#else
74212992fb08 Initial revision
Sam Lantinga <slouken@lokigames.com>
parents:
diff changeset
   149
	struct timeval tv;
74212992fb08 Initial revision
Sam Lantinga <slouken@lokigames.com>
parents:
diff changeset
   150
#ifndef SELECT_SETS_REMAINING
74212992fb08 Initial revision
Sam Lantinga <slouken@lokigames.com>
parents:
diff changeset
   151
	Uint32 then, now, elapsed;
74212992fb08 Initial revision
Sam Lantinga <slouken@lokigames.com>
parents:
diff changeset
   152
#endif
74212992fb08 Initial revision
Sam Lantinga <slouken@lokigames.com>
parents:
diff changeset
   153
#endif
74212992fb08 Initial revision
Sam Lantinga <slouken@lokigames.com>
parents:
diff changeset
   154
74212992fb08 Initial revision
Sam Lantinga <slouken@lokigames.com>
parents:
diff changeset
   155
	/* Set the timeout interval - Linux only needs to do this once */
74212992fb08 Initial revision
Sam Lantinga <slouken@lokigames.com>
parents:
diff changeset
   156
#ifdef SELECT_SETS_REMAINING
74212992fb08 Initial revision
Sam Lantinga <slouken@lokigames.com>
parents:
diff changeset
   157
	tv.tv_sec = ms/1000;
74212992fb08 Initial revision
Sam Lantinga <slouken@lokigames.com>
parents:
diff changeset
   158
	tv.tv_usec = (ms%1000)*1000;
74212992fb08 Initial revision
Sam Lantinga <slouken@lokigames.com>
parents:
diff changeset
   159
#elif defined(USE_NANOSLEEP)
74212992fb08 Initial revision
Sam Lantinga <slouken@lokigames.com>
parents:
diff changeset
   160
	elapsed.tv_sec = ms/1000;
74212992fb08 Initial revision
Sam Lantinga <slouken@lokigames.com>
parents:
diff changeset
   161
	elapsed.tv_nsec = (ms%1000)*1000000;
74212992fb08 Initial revision
Sam Lantinga <slouken@lokigames.com>
parents:
diff changeset
   162
#else
74212992fb08 Initial revision
Sam Lantinga <slouken@lokigames.com>
parents:
diff changeset
   163
	then = SDL_GetTicks();
74212992fb08 Initial revision
Sam Lantinga <slouken@lokigames.com>
parents:
diff changeset
   164
#endif
74212992fb08 Initial revision
Sam Lantinga <slouken@lokigames.com>
parents:
diff changeset
   165
	do {
74212992fb08 Initial revision
Sam Lantinga <slouken@lokigames.com>
parents:
diff changeset
   166
		errno = 0;
74212992fb08 Initial revision
Sam Lantinga <slouken@lokigames.com>
parents:
diff changeset
   167
1
cf2af46e9e2a Changes since SDL 1.2.0 release
Sam Lantinga <slouken@lokigames.com>
parents: 0
diff changeset
   168
#if _POSIX_THREAD_SYSCALL_SOFT
cf2af46e9e2a Changes since SDL 1.2.0 release
Sam Lantinga <slouken@lokigames.com>
parents: 0
diff changeset
   169
		pthread_yield_np();
cf2af46e9e2a Changes since SDL 1.2.0 release
Sam Lantinga <slouken@lokigames.com>
parents: 0
diff changeset
   170
#endif
0
74212992fb08 Initial revision
Sam Lantinga <slouken@lokigames.com>
parents:
diff changeset
   171
#ifdef USE_NANOSLEEP
74212992fb08 Initial revision
Sam Lantinga <slouken@lokigames.com>
parents:
diff changeset
   172
		tv.tv_sec = elapsed.tv_sec;
74212992fb08 Initial revision
Sam Lantinga <slouken@lokigames.com>
parents:
diff changeset
   173
		tv.tv_nsec = elapsed.tv_nsec;
74212992fb08 Initial revision
Sam Lantinga <slouken@lokigames.com>
parents:
diff changeset
   174
		was_error = nanosleep(&tv, &elapsed);
74212992fb08 Initial revision
Sam Lantinga <slouken@lokigames.com>
parents:
diff changeset
   175
#else
74212992fb08 Initial revision
Sam Lantinga <slouken@lokigames.com>
parents:
diff changeset
   176
#ifndef SELECT_SETS_REMAINING
74212992fb08 Initial revision
Sam Lantinga <slouken@lokigames.com>
parents:
diff changeset
   177
		/* Calculate the time interval left (in case of interrupt) */
74212992fb08 Initial revision
Sam Lantinga <slouken@lokigames.com>
parents:
diff changeset
   178
		now = SDL_GetTicks();
74212992fb08 Initial revision
Sam Lantinga <slouken@lokigames.com>
parents:
diff changeset
   179
		elapsed = (now-then);
74212992fb08 Initial revision
Sam Lantinga <slouken@lokigames.com>
parents:
diff changeset
   180
		then = now;
74212992fb08 Initial revision
Sam Lantinga <slouken@lokigames.com>
parents:
diff changeset
   181
		if ( elapsed >= ms ) {
74212992fb08 Initial revision
Sam Lantinga <slouken@lokigames.com>
parents:
diff changeset
   182
			break;
74212992fb08 Initial revision
Sam Lantinga <slouken@lokigames.com>
parents:
diff changeset
   183
		}
74212992fb08 Initial revision
Sam Lantinga <slouken@lokigames.com>
parents:
diff changeset
   184
		ms -= elapsed;
74212992fb08 Initial revision
Sam Lantinga <slouken@lokigames.com>
parents:
diff changeset
   185
		tv.tv_sec = ms/1000;
74212992fb08 Initial revision
Sam Lantinga <slouken@lokigames.com>
parents:
diff changeset
   186
		tv.tv_usec = (ms%1000)*1000;
74212992fb08 Initial revision
Sam Lantinga <slouken@lokigames.com>
parents:
diff changeset
   187
#endif
74212992fb08 Initial revision
Sam Lantinga <slouken@lokigames.com>
parents:
diff changeset
   188
		was_error = select(0, NULL, NULL, NULL, &tv);
74212992fb08 Initial revision
Sam Lantinga <slouken@lokigames.com>
parents:
diff changeset
   189
#endif /* USE_NANOSLEEP */
74212992fb08 Initial revision
Sam Lantinga <slouken@lokigames.com>
parents:
diff changeset
   190
	} while ( was_error && (errno == EINTR) );
74212992fb08 Initial revision
Sam Lantinga <slouken@lokigames.com>
parents:
diff changeset
   191
}
74212992fb08 Initial revision
Sam Lantinga <slouken@lokigames.com>
parents:
diff changeset
   192
74212992fb08 Initial revision
Sam Lantinga <slouken@lokigames.com>
parents:
diff changeset
   193
#ifdef USE_ITIMER
74212992fb08 Initial revision
Sam Lantinga <slouken@lokigames.com>
parents:
diff changeset
   194
74212992fb08 Initial revision
Sam Lantinga <slouken@lokigames.com>
parents:
diff changeset
   195
static void HandleAlarm(int sig)
74212992fb08 Initial revision
Sam Lantinga <slouken@lokigames.com>
parents:
diff changeset
   196
{
74212992fb08 Initial revision
Sam Lantinga <slouken@lokigames.com>
parents:
diff changeset
   197
	Uint32 ms;
74212992fb08 Initial revision
Sam Lantinga <slouken@lokigames.com>
parents:
diff changeset
   198
74212992fb08 Initial revision
Sam Lantinga <slouken@lokigames.com>
parents:
diff changeset
   199
	if ( SDL_alarm_callback ) {
74212992fb08 Initial revision
Sam Lantinga <slouken@lokigames.com>
parents:
diff changeset
   200
		ms = (*SDL_alarm_callback)(SDL_alarm_interval);
74212992fb08 Initial revision
Sam Lantinga <slouken@lokigames.com>
parents:
diff changeset
   201
		if ( ms != SDL_alarm_interval ) {
74212992fb08 Initial revision
Sam Lantinga <slouken@lokigames.com>
parents:
diff changeset
   202
			SDL_SetTimer(ms, SDL_alarm_callback);
74212992fb08 Initial revision
Sam Lantinga <slouken@lokigames.com>
parents:
diff changeset
   203
		}
74212992fb08 Initial revision
Sam Lantinga <slouken@lokigames.com>
parents:
diff changeset
   204
	}
74212992fb08 Initial revision
Sam Lantinga <slouken@lokigames.com>
parents:
diff changeset
   205
}
74212992fb08 Initial revision
Sam Lantinga <slouken@lokigames.com>
parents:
diff changeset
   206
74212992fb08 Initial revision
Sam Lantinga <slouken@lokigames.com>
parents:
diff changeset
   207
int SDL_SYS_TimerInit(void)
74212992fb08 Initial revision
Sam Lantinga <slouken@lokigames.com>
parents:
diff changeset
   208
{
74212992fb08 Initial revision
Sam Lantinga <slouken@lokigames.com>
parents:
diff changeset
   209
	struct sigaction action;
74212992fb08 Initial revision
Sam Lantinga <slouken@lokigames.com>
parents:
diff changeset
   210
74212992fb08 Initial revision
Sam Lantinga <slouken@lokigames.com>
parents:
diff changeset
   211
	/* Set the alarm handler (Linux specific) */
74212992fb08 Initial revision
Sam Lantinga <slouken@lokigames.com>
parents:
diff changeset
   212
	memset(&action, 0, sizeof(action));
74212992fb08 Initial revision
Sam Lantinga <slouken@lokigames.com>
parents:
diff changeset
   213
	action.sa_handler = HandleAlarm;
74212992fb08 Initial revision
Sam Lantinga <slouken@lokigames.com>
parents:
diff changeset
   214
	action.sa_flags = SA_RESTART;
74212992fb08 Initial revision
Sam Lantinga <slouken@lokigames.com>
parents:
diff changeset
   215
	sigemptyset(&action.sa_mask);
74212992fb08 Initial revision
Sam Lantinga <slouken@lokigames.com>
parents:
diff changeset
   216
	sigaction(SIGALRM, &action, NULL);
74212992fb08 Initial revision
Sam Lantinga <slouken@lokigames.com>
parents:
diff changeset
   217
	return(0);
74212992fb08 Initial revision
Sam Lantinga <slouken@lokigames.com>
parents:
diff changeset
   218
}
74212992fb08 Initial revision
Sam Lantinga <slouken@lokigames.com>
parents:
diff changeset
   219
74212992fb08 Initial revision
Sam Lantinga <slouken@lokigames.com>
parents:
diff changeset
   220
void SDL_SYS_TimerQuit(void)
74212992fb08 Initial revision
Sam Lantinga <slouken@lokigames.com>
parents:
diff changeset
   221
{
74212992fb08 Initial revision
Sam Lantinga <slouken@lokigames.com>
parents:
diff changeset
   222
	SDL_SetTimer(0, NULL);
74212992fb08 Initial revision
Sam Lantinga <slouken@lokigames.com>
parents:
diff changeset
   223
}
74212992fb08 Initial revision
Sam Lantinga <slouken@lokigames.com>
parents:
diff changeset
   224
74212992fb08 Initial revision
Sam Lantinga <slouken@lokigames.com>
parents:
diff changeset
   225
int SDL_SYS_StartTimer(void)
74212992fb08 Initial revision
Sam Lantinga <slouken@lokigames.com>
parents:
diff changeset
   226
{
74212992fb08 Initial revision
Sam Lantinga <slouken@lokigames.com>
parents:
diff changeset
   227
	struct itimerval timer;
74212992fb08 Initial revision
Sam Lantinga <slouken@lokigames.com>
parents:
diff changeset
   228
74212992fb08 Initial revision
Sam Lantinga <slouken@lokigames.com>
parents:
diff changeset
   229
	timer.it_value.tv_sec = (SDL_alarm_interval/1000);
74212992fb08 Initial revision
Sam Lantinga <slouken@lokigames.com>
parents:
diff changeset
   230
	timer.it_value.tv_usec = (SDL_alarm_interval%1000)*1000;
74212992fb08 Initial revision
Sam Lantinga <slouken@lokigames.com>
parents:
diff changeset
   231
	timer.it_interval.tv_sec = (SDL_alarm_interval/1000);
74212992fb08 Initial revision
Sam Lantinga <slouken@lokigames.com>
parents:
diff changeset
   232
	timer.it_interval.tv_usec = (SDL_alarm_interval%1000)*1000;
74212992fb08 Initial revision
Sam Lantinga <slouken@lokigames.com>
parents:
diff changeset
   233
	setitimer(ITIMER_REAL, &timer, NULL);
74212992fb08 Initial revision
Sam Lantinga <slouken@lokigames.com>
parents:
diff changeset
   234
	return(0);
74212992fb08 Initial revision
Sam Lantinga <slouken@lokigames.com>
parents:
diff changeset
   235
}
74212992fb08 Initial revision
Sam Lantinga <slouken@lokigames.com>
parents:
diff changeset
   236
74212992fb08 Initial revision
Sam Lantinga <slouken@lokigames.com>
parents:
diff changeset
   237
void SDL_SYS_StopTimer(void)
74212992fb08 Initial revision
Sam Lantinga <slouken@lokigames.com>
parents:
diff changeset
   238
{
74212992fb08 Initial revision
Sam Lantinga <slouken@lokigames.com>
parents:
diff changeset
   239
	struct itimerval timer;
74212992fb08 Initial revision
Sam Lantinga <slouken@lokigames.com>
parents:
diff changeset
   240
74212992fb08 Initial revision
Sam Lantinga <slouken@lokigames.com>
parents:
diff changeset
   241
	memset(&timer, 0, (sizeof timer));
74212992fb08 Initial revision
Sam Lantinga <slouken@lokigames.com>
parents:
diff changeset
   242
	setitimer(ITIMER_REAL, &timer, NULL);
74212992fb08 Initial revision
Sam Lantinga <slouken@lokigames.com>
parents:
diff changeset
   243
}
74212992fb08 Initial revision
Sam Lantinga <slouken@lokigames.com>
parents:
diff changeset
   244
74212992fb08 Initial revision
Sam Lantinga <slouken@lokigames.com>
parents:
diff changeset
   245
#else /* USE_ITIMER */
74212992fb08 Initial revision
Sam Lantinga <slouken@lokigames.com>
parents:
diff changeset
   246
74212992fb08 Initial revision
Sam Lantinga <slouken@lokigames.com>
parents:
diff changeset
   247
#include "SDL_thread.h"
74212992fb08 Initial revision
Sam Lantinga <slouken@lokigames.com>
parents:
diff changeset
   248
74212992fb08 Initial revision
Sam Lantinga <slouken@lokigames.com>
parents:
diff changeset
   249
/* Data to handle a single periodic alarm */
74212992fb08 Initial revision
Sam Lantinga <slouken@lokigames.com>
parents:
diff changeset
   250
static int timer_alive = 0;
74212992fb08 Initial revision
Sam Lantinga <slouken@lokigames.com>
parents:
diff changeset
   251
static SDL_Thread *timer = NULL;
74212992fb08 Initial revision
Sam Lantinga <slouken@lokigames.com>
parents:
diff changeset
   252
74212992fb08 Initial revision
Sam Lantinga <slouken@lokigames.com>
parents:
diff changeset
   253
static int RunTimer(void *unused)
74212992fb08 Initial revision
Sam Lantinga <slouken@lokigames.com>
parents:
diff changeset
   254
{
74212992fb08 Initial revision
Sam Lantinga <slouken@lokigames.com>
parents:
diff changeset
   255
	while ( timer_alive ) {
74212992fb08 Initial revision
Sam Lantinga <slouken@lokigames.com>
parents:
diff changeset
   256
		if ( SDL_timer_running ) {
74212992fb08 Initial revision
Sam Lantinga <slouken@lokigames.com>
parents:
diff changeset
   257
			SDL_ThreadedTimerCheck();
74212992fb08 Initial revision
Sam Lantinga <slouken@lokigames.com>
parents:
diff changeset
   258
		}
74212992fb08 Initial revision
Sam Lantinga <slouken@lokigames.com>
parents:
diff changeset
   259
		SDL_Delay(1);
74212992fb08 Initial revision
Sam Lantinga <slouken@lokigames.com>
parents:
diff changeset
   260
	}
74212992fb08 Initial revision
Sam Lantinga <slouken@lokigames.com>
parents:
diff changeset
   261
	return(0);
74212992fb08 Initial revision
Sam Lantinga <slouken@lokigames.com>
parents:
diff changeset
   262
}
74212992fb08 Initial revision
Sam Lantinga <slouken@lokigames.com>
parents:
diff changeset
   263
74212992fb08 Initial revision
Sam Lantinga <slouken@lokigames.com>
parents:
diff changeset
   264
/* This is only called if the event thread is not running */
74212992fb08 Initial revision
Sam Lantinga <slouken@lokigames.com>
parents:
diff changeset
   265
int SDL_SYS_TimerInit(void)
74212992fb08 Initial revision
Sam Lantinga <slouken@lokigames.com>
parents:
diff changeset
   266
{
74212992fb08 Initial revision
Sam Lantinga <slouken@lokigames.com>
parents:
diff changeset
   267
	timer_alive = 1;
74212992fb08 Initial revision
Sam Lantinga <slouken@lokigames.com>
parents:
diff changeset
   268
	timer = SDL_CreateThread(RunTimer, NULL);
74212992fb08 Initial revision
Sam Lantinga <slouken@lokigames.com>
parents:
diff changeset
   269
	if ( timer == NULL )
74212992fb08 Initial revision
Sam Lantinga <slouken@lokigames.com>
parents:
diff changeset
   270
		return(-1);
74212992fb08 Initial revision
Sam Lantinga <slouken@lokigames.com>
parents:
diff changeset
   271
	return(SDL_SetTimerThreaded(1));
74212992fb08 Initial revision
Sam Lantinga <slouken@lokigames.com>
parents:
diff changeset
   272
}
74212992fb08 Initial revision
Sam Lantinga <slouken@lokigames.com>
parents:
diff changeset
   273
74212992fb08 Initial revision
Sam Lantinga <slouken@lokigames.com>
parents:
diff changeset
   274
void SDL_SYS_TimerQuit(void)
74212992fb08 Initial revision
Sam Lantinga <slouken@lokigames.com>
parents:
diff changeset
   275
{
74212992fb08 Initial revision
Sam Lantinga <slouken@lokigames.com>
parents:
diff changeset
   276
	timer_alive = 0;
74212992fb08 Initial revision
Sam Lantinga <slouken@lokigames.com>
parents:
diff changeset
   277
	if ( timer ) {
74212992fb08 Initial revision
Sam Lantinga <slouken@lokigames.com>
parents:
diff changeset
   278
		SDL_WaitThread(timer, NULL);
74212992fb08 Initial revision
Sam Lantinga <slouken@lokigames.com>
parents:
diff changeset
   279
		timer = NULL;
74212992fb08 Initial revision
Sam Lantinga <slouken@lokigames.com>
parents:
diff changeset
   280
	}
74212992fb08 Initial revision
Sam Lantinga <slouken@lokigames.com>
parents:
diff changeset
   281
}
74212992fb08 Initial revision
Sam Lantinga <slouken@lokigames.com>
parents:
diff changeset
   282
74212992fb08 Initial revision
Sam Lantinga <slouken@lokigames.com>
parents:
diff changeset
   283
int SDL_SYS_StartTimer(void)
74212992fb08 Initial revision
Sam Lantinga <slouken@lokigames.com>
parents:
diff changeset
   284
{
74212992fb08 Initial revision
Sam Lantinga <slouken@lokigames.com>
parents:
diff changeset
   285
	SDL_SetError("Internal logic error: Linux uses threaded timer");
74212992fb08 Initial revision
Sam Lantinga <slouken@lokigames.com>
parents:
diff changeset
   286
	return(-1);
74212992fb08 Initial revision
Sam Lantinga <slouken@lokigames.com>
parents:
diff changeset
   287
}
74212992fb08 Initial revision
Sam Lantinga <slouken@lokigames.com>
parents:
diff changeset
   288
74212992fb08 Initial revision
Sam Lantinga <slouken@lokigames.com>
parents:
diff changeset
   289
void SDL_SYS_StopTimer(void)
74212992fb08 Initial revision
Sam Lantinga <slouken@lokigames.com>
parents:
diff changeset
   290
{
74212992fb08 Initial revision
Sam Lantinga <slouken@lokigames.com>
parents:
diff changeset
   291
	return;
74212992fb08 Initial revision
Sam Lantinga <slouken@lokigames.com>
parents:
diff changeset
   292
}
74212992fb08 Initial revision
Sam Lantinga <slouken@lokigames.com>
parents:
diff changeset
   293
74212992fb08 Initial revision
Sam Lantinga <slouken@lokigames.com>
parents:
diff changeset
   294
#endif /* USE_ITIMER */