src/timer/riscos/SDL_systimer.c
author Sam Lantinga <slouken@libsdl.org>
Fri, 10 Feb 2006 06:48:43 +0000
changeset 1358 c71e05b4dc2e
parent 1312 c9b51268668f
child 1361 19418e4422cb
permissions -rw-r--r--
More header massaging... works great on Windows. ;-)
     1 /*
     2     SDL - Simple DirectMedia Layer
     3     Copyright (C) 1997-2006 Sam Lantinga
     4 
     5     This library is free software; you can redistribute it and/or
     6     modify it under the terms of the GNU Lesser General Public
     7     License as published by the Free Software Foundation; either
     8     version 2.1 of the License, or (at your option) any later version.
     9 
    10     This library is distributed in the hope that it will be useful,
    11     but WITHOUT ANY WARRANTY; without even the implied warranty of
    12     MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
    13     Lesser General Public License for more details.
    14 
    15     You should have received a copy of the GNU Lesser General Public
    16     License along with this library; if not, write to the Free Software
    17     Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301  USA
    18 
    19     Sam Lantinga
    20     slouken@libsdl.org
    21 */
    22 
    23 #include <stdio.h>
    24 #include <time.h>
    25 #include <sys/time.h>
    26 #include <unistd.h>
    27 #include <string.h>
    28 #include <errno.h>
    29 
    30 #include "SDL_timer.h"
    31 #include "SDL_timer_c.h"
    32 
    33 #ifdef DISABLE_THREADS
    34 /* Timer start/reset time */
    35 static Uint32 timerStart;
    36 /* Timer running function */
    37 void RISCOS_CheckTimer();
    38 #else
    39 #include <pthread.h>
    40 extern Uint32 riscos_main_thread;
    41 extern int riscos_using_threads;
    42 extern Uint32 SDL_ThreadID();
    43 extern Uint32 SDL_EventThreadID(void);
    44 #endif
    45 
    46 
    47 extern void RISCOS_BackgroundTasks(void);
    48 
    49 /* The first ticks value of the application */
    50 clock_t start;
    51 
    52 void SDL_StartTicks(void)
    53 {
    54 	/* Set first ticks value */
    55 	start = clock();
    56 }
    57 
    58 Uint32 SDL_GetTicks (void)
    59 {
    60 	clock_t ticks;
    61 
    62 	ticks=clock()-start;
    63 
    64 
    65 #if CLOCKS_PER_SEC == 1000
    66 
    67 	return(ticks);
    68 
    69 #elif CLOCKS_PER_SEC == 100
    70 
    71 	return (ticks * 10);
    72 
    73 #else
    74 
    75 	return ticks*(1000/CLOCKS_PER_SEC);
    76 
    77 #endif
    78 
    79 }
    80 
    81 void SDL_Delay (Uint32 ms)
    82 {
    83     Uint32 now,then,elapsed;
    84 #ifndef DISABLE_THREADS
    85     int is_event_thread;
    86     if (riscos_using_threads)
    87     {
    88        is_event_thread = 0;
    89        if (SDL_EventThreadID())
    90        {
    91           if (SDL_EventThreadID() == SDL_ThreadID()) is_event_thread = 1;
    92        } else if (SDL_ThreadID() == riscos_main_thread) is_event_thread = 1;
    93     } else is_event_thread = 1;
    94 #endif
    95 
    96         /*TODO: Next version of Unixlib may allow us to use usleep here */
    97         /*      for non event threads */
    98 
    99 	/* Set the timeout interval - Linux only needs to do this once */
   100 	then = SDL_GetTicks();
   101 
   102 	do {
   103 		/* Do background tasks required while sleeping as we are not multithreaded */
   104 #ifdef DISABLE_THREADS
   105 		RISCOS_BackgroundTasks();
   106 #else
   107 		/* For threaded build only run background tasks in event thread */
   108 		if (is_event_thread) RISCOS_BackgroundTasks();
   109 #endif
   110 
   111 		/* Calculate the time interval left (in case of interrupt) */
   112 		now = SDL_GetTicks();
   113 		elapsed = (now-then);
   114 		then = now;
   115 		if ( elapsed >= ms ) {
   116 			break;
   117 		}
   118 		ms -= elapsed;
   119 #ifndef DISABLE_THREADS
   120             /* Need to yield to let other threads have a go */
   121             if (riscos_using_threads) pthread_yield();
   122 #endif
   123 
   124 	} while ( 1 );
   125 }
   126 
   127 #ifdef DISABLE_THREADS
   128 
   129 /* Non-threaded version of timer */
   130 
   131 int SDL_SYS_TimerInit(void)
   132 {
   133 	return(0);
   134 }
   135 
   136 void SDL_SYS_TimerQuit(void)
   137 {
   138 	SDL_SetTimer(0, NULL);
   139 }
   140 
   141 int SDL_SYS_StartTimer(void)
   142 {
   143 	timerStart = SDL_GetTicks();
   144 
   145 	return(0);
   146 }
   147 
   148 void SDL_SYS_StopTimer(void)
   149 {
   150 	/* Don't need to do anything as we use SDL_timer_running
   151 	   to detect if we need to check the timer */
   152 }
   153 
   154 
   155 void RISCOS_CheckTimer()
   156 {
   157 	if (SDL_timer_running && SDL_GetTicks() - timerStart >= SDL_alarm_interval)
   158 	{
   159 		Uint32 ms;
   160 
   161 		ms = SDL_alarm_callback(SDL_alarm_interval);
   162 		if ( ms != SDL_alarm_interval )
   163 		{
   164 			if ( ms )
   165 			{
   166 				SDL_alarm_interval = ROUND_RESOLUTION(ms);
   167 			} else
   168 			{
   169 				SDL_alarm_interval = 0;
   170 				SDL_timer_running = 0;
   171 			}
   172 		}
   173 		if (SDL_alarm_interval) timerStart = SDL_GetTicks();
   174 	}
   175 }
   176 
   177 #else
   178 
   179 /* Threaded version of timer - based on code for linux */
   180 
   181 #include "SDL_thread.h"
   182 
   183 /* Data to handle a single periodic alarm */
   184 static int timer_alive = 0;
   185 static SDL_Thread *timer = NULL;
   186 
   187 static int RunTimer(void *unused)
   188 {
   189 	while ( timer_alive ) {
   190 		if ( SDL_timer_running ) {
   191 			SDL_ThreadedTimerCheck();
   192 		}
   193 		SDL_Delay(1);
   194 	}
   195 	return(0);
   196 }
   197 
   198 /* This is only called if the event thread is not running */
   199 int SDL_SYS_TimerInit(void)
   200 {
   201 	timer_alive = 1;
   202 	timer = SDL_CreateThread(RunTimer, NULL);
   203 	if ( timer == NULL )
   204 		return(-1);
   205 	return(SDL_SetTimerThreaded(1));
   206 }
   207 
   208 void SDL_SYS_TimerQuit(void)
   209 {
   210 	timer_alive = 0;
   211 	if ( timer ) {
   212 		SDL_WaitThread(timer, NULL);
   213 		timer = NULL;
   214 	}
   215 }
   216 
   217 int SDL_SYS_StartTimer(void)
   218 {
   219 	SDL_SetError("Internal logic error: RISC OS uses threaded timer");
   220 	return(-1);
   221 }
   222 
   223 void SDL_SYS_StopTimer(void)
   224 {
   225 	return;
   226 }
   227 
   228 #endif /* DISABLE_THREADS */