2 SDL - Simple DirectMedia Layer
3 Copyright (C) 1997-2009 Sam Lantinga
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.
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.
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
22 #include "SDL_config.h"
24 #ifdef SDL_TIMER_RISCOS
33 #include "SDL_timer.h"
34 #include "../SDL_timer_c.h"
36 #if SDL_THREADS_DISABLED
37 /* Timer SDL_arraysize(Timer ),start/reset time */
38 static Uint32 timerStart;
39 /* Timer running function */
40 void RISCOS_CheckTimer();
43 extern Uint32 riscos_main_thread;
44 extern int riscos_using_threads;
45 extern Uint32 SDL_ThreadID();
46 extern Uint32 SDL_EventThreadID(void);
50 extern void RISCOS_BackgroundTasks(void);
52 /* The first ticks value of the application */
55 void SDL_StartTicks(void)
57 /* Set first ticks value */
61 Uint32 SDL_GetTicks (void)
68 #if CLOCKS_PER_SEC == 1000
72 #elif CLOCKS_PER_SEC == 100
78 return ticks*(1000/CLOCKS_PER_SEC);
84 void SDL_Delay (Uint32 ms)
86 Uint32 now,then,elapsed;
87 #if !SDL_THREADS_DISABLED
89 if (riscos_using_threads)
92 if (SDL_EventThreadID())
94 if (SDL_EventThreadID() == SDL_ThreadID()) is_event_thread = 1;
95 } else if (SDL_ThreadID() == riscos_main_thread) is_event_thread = 1;
96 } else is_event_thread = 1;
99 /*TODO: Next version of Unixlib may allow us to use usleep here */
100 /* for non event threads */
102 /* Set the timeout interval - Linux only needs to do this once */
103 then = SDL_GetTicks();
106 /* Do background tasks required while sleeping as we are not multithreaded */
107 #if SDL_THREADS_DISABLED
108 RISCOS_BackgroundTasks();
110 /* For threaded build only run background tasks in event thread */
111 if (is_event_thread) RISCOS_BackgroundTasks();
114 /* Calculate the time interval left (in case of interrupt) */
115 now = SDL_GetTicks();
116 elapsed = (now-then);
118 if ( elapsed >= ms ) {
122 #if !SDL_THREADS_DISABLED
123 /* Need to yield to let other threads have a go */
124 if (riscos_using_threads) pthread_yield();
130 #if SDL_THREADS_DISABLED
132 /* Non-threaded version of timer */
134 int SDL_SYS_TimerInit(void)
139 void SDL_SYS_TimerQuit(void)
141 SDL_SetTimer(0, NULL);
144 int SDL_SYS_StartTimer(void)
146 timerStart = SDL_GetTicks();
151 void SDL_SYS_StopTimer(void)
153 /* Don't need to do anything as we use SDL_timer_running
154 to detect if we need to check the timer */
158 void RISCOS_CheckTimer()
160 if (SDL_timer_running && SDL_GetTicks() - timerStart >= SDL_alarm_interval)
164 ms = SDL_alarm_callback(SDL_alarm_interval);
165 if ( ms != SDL_alarm_interval )
169 SDL_alarm_interval = ROUND_RESOLUTION(ms);
172 SDL_alarm_interval = 0;
173 SDL_timer_running = 0;
176 if (SDL_alarm_interval) timerStart = SDL_GetTicks();
182 /* Threaded version of timer - based on code for linux */
184 #include "SDL_thread.h"
186 /* Data to handle a single periodic alarm */
187 static int timer_alive = 0;
188 static SDL_Thread *timer = NULL;
190 static int RunTimer(void *unused)
192 while ( timer_alive ) {
193 if ( SDL_timer_running ) {
194 SDL_ThreadedTimerCheck();
201 /* This is only called if the event thread is not running */
202 int SDL_SYS_TimerInit(void)
205 timer = SDL_CreateThread(RunTimer, NULL);
208 return(SDL_SetTimerThreaded(1));
211 void SDL_SYS_TimerQuit(void)
215 SDL_WaitThread(timer, NULL);
220 int SDL_SYS_StartTimer(void)
222 SDL_SetError("Internal logic error: RISC OS uses threaded timer");
226 void SDL_SYS_StopTimer(void)
231 #endif /* SDL_THREADS_DISABLED */
233 #endif /* SDL_TIMER_RISCOS */