src/timer/riscos/SDL_systimer.c
author Sam Lantinga <slouken@libsdl.org>
Mon, 29 May 2006 04:04:35 +0000
branchSDL-1.3
changeset 1668 4da1ee79c9af
parent 1662 782fd950bd46
permissions -rw-r--r--
more tweaking indent options
     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 #include "SDL_config.h"
    23 
    24 #ifdef SDL_TIMER_RISCOS
    25 
    26 #include <stdio.h>
    27 #include <time.h>
    28 #include <sys/time.h>
    29 #include <unistd.h>
    30 #include <string.h>
    31 #include <errno.h>
    32 
    33 #include "SDL_timer.h"
    34 #include "../SDL_timer_c.h"
    35 
    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();
    41 #else
    42 #include <pthread.h>
    43 extern Uint32 riscos_main_thread;
    44 extern int riscos_using_threads;
    45 extern Uint32 SDL_ThreadID();
    46 extern Uint32 SDL_EventThreadID(void);
    47 #endif
    48 
    49 
    50 extern void RISCOS_BackgroundTasks(void);
    51 
    52 /* The first ticks value of the application */
    53 clock_t start;
    54 
    55 void
    56 SDL_StartTicks(void)
    57 {
    58     /* Set first ticks value */
    59     start = clock();
    60 }
    61 
    62 Uint32
    63 SDL_GetTicks(void)
    64 {
    65     clock_t ticks;
    66 
    67     ticks = clock() - start;
    68 
    69 
    70 #if CLOCKS_PER_SEC == 1000
    71 
    72     return (ticks);
    73 
    74 #elif CLOCKS_PER_SEC == 100
    75 
    76     return (ticks * 10);
    77 
    78 #else
    79 
    80     return ticks * (1000 / CLOCKS_PER_SEC);
    81 
    82 #endif
    83 
    84 }
    85 
    86 void
    87 SDL_Delay(Uint32 ms)
    88 {
    89     Uint32 now, then, elapsed;
    90 #if !SDL_THREADS_DISABLED
    91     int is_event_thread;
    92     if (riscos_using_threads) {
    93         is_event_thread = 0;
    94         if (SDL_EventThreadID()) {
    95             if (SDL_EventThreadID() == SDL_ThreadID())
    96                 is_event_thread = 1;
    97         } else if (SDL_ThreadID() == riscos_main_thread)
    98             is_event_thread = 1;
    99     } else
   100         is_event_thread = 1;
   101 #endif
   102 
   103     /*TODO: Next version of Unixlib may allow us to use usleep here */
   104     /*      for non event threads */
   105 
   106     /* Set the timeout interval - Linux only needs to do this once */
   107     then = SDL_GetTicks();
   108 
   109     do {
   110         /* Do background tasks required while sleeping as we are not multithreaded */
   111 #if SDL_THREADS_DISABLED
   112         RISCOS_BackgroundTasks();
   113 #else
   114         /* For threaded build only run background tasks in event thread */
   115         if (is_event_thread)
   116             RISCOS_BackgroundTasks();
   117 #endif
   118 
   119         /* Calculate the time interval left (in case of interrupt) */
   120         now = SDL_GetTicks();
   121         elapsed = (now - then);
   122         then = now;
   123         if (elapsed >= ms) {
   124             break;
   125         }
   126         ms -= elapsed;
   127 #if !SDL_THREADS_DISABLED
   128         /* Need to yield to let other threads have a go */
   129         if (riscos_using_threads)
   130             pthread_yield();
   131 #endif
   132 
   133     }
   134     while (1);
   135 }
   136 
   137 #if SDL_THREADS_DISABLED
   138 
   139 /* Non-threaded version of timer */
   140 
   141 int
   142 SDL_SYS_TimerInit(void)
   143 {
   144     return (0);
   145 }
   146 
   147 void
   148 SDL_SYS_TimerQuit(void)
   149 {
   150     SDL_SetTimer(0, NULL);
   151 }
   152 
   153 int
   154 SDL_SYS_StartTimer(void)
   155 {
   156     timerStart = SDL_GetTicks();
   157 
   158     return (0);
   159 }
   160 
   161 void
   162 SDL_SYS_StopTimer(void)
   163 {
   164     /* Don't need to do anything as we use SDL_timer_running
   165        to detect if we need to check the timer */
   166 }
   167 
   168 
   169 void
   170 RISCOS_CheckTimer()
   171 {
   172     if (SDL_timer_running
   173         && SDL_GetTicks() - timerStart >= SDL_alarm_interval) {
   174         Uint32 ms;
   175 
   176         ms = SDL_alarm_callback(SDL_alarm_interval);
   177         if (ms != SDL_alarm_interval) {
   178             if (ms) {
   179                 SDL_alarm_interval = ROUND_RESOLUTION(ms);
   180             } else {
   181                 SDL_alarm_interval = 0;
   182                 SDL_timer_running = 0;
   183             }
   184         }
   185         if (SDL_alarm_interval)
   186             timerStart = SDL_GetTicks();
   187     }
   188 }
   189 
   190 #else
   191 
   192 /* Threaded version of timer - based on code for linux */
   193 
   194 #include "SDL_thread.h"
   195 
   196 /* Data to handle a single periodic alarm */
   197 static int timer_alive = 0;
   198 static SDL_Thread *timer = NULL;
   199 
   200 static int
   201 RunTimer(void *unused)
   202 {
   203     while (timer_alive) {
   204         if (SDL_timer_running) {
   205             SDL_ThreadedTimerCheck();
   206         }
   207         SDL_Delay(1);
   208     }
   209     return (0);
   210 }
   211 
   212 /* This is only called if the event thread is not running */
   213 int
   214 SDL_SYS_TimerInit(void)
   215 {
   216     timer_alive = 1;
   217     timer = SDL_CreateThread(RunTimer, NULL);
   218     if (timer == NULL)
   219         return (-1);
   220     return (SDL_SetTimerThreaded(1));
   221 }
   222 
   223 void
   224 SDL_SYS_TimerQuit(void)
   225 {
   226     timer_alive = 0;
   227     if (timer) {
   228         SDL_WaitThread(timer, NULL);
   229         timer = NULL;
   230     }
   231 }
   232 
   233 int
   234 SDL_SYS_StartTimer(void)
   235 {
   236     SDL_SetError("Internal logic error: RISC OS uses threaded timer");
   237     return (-1);
   238 }
   239 
   240 void
   241 SDL_SYS_StopTimer(void)
   242 {
   243     return;
   244 }
   245 
   246 #endif /* SDL_THREADS_DISABLED */
   247 
   248 #endif /* SDL_TIMER_RISCOS */
   249 /* vi: set ts=4 sw=4 expandtab: */