src/timer/riscos/SDL_systimer.c
author Sam Lantinga <slouken@libsdl.org>
Wed, 16 Dec 2009 04:48:11 +0000
changeset 3578 0d1b16ee0bca
parent 2859 99210400e8b9
child 3697 f7b03b6838cb
permissions -rw-r--r--
Fixed bug #741

The thread ID is an unsigned long so it can hold pthread_t so people can do naughty things with it.

I'm going to be adding additional useful thread API functions, but this should prevent crashes in people's existing code on 64-bit architectures.
     1 /*
     2     SDL - Simple DirectMedia Layer
     3     Copyright (C) 1997-2009 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 SDL_threadID riscos_main_thread;
    44 extern int riscos_using_threads;
    45 extern SDL_threadID SDL_ThreadID();
    46 extern SDL_threadID 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     } while (1);
   134 }
   135 
   136 #if SDL_THREADS_DISABLED
   137 
   138 /* Non-threaded version of timer */
   139 
   140 int
   141 SDL_SYS_TimerInit(void)
   142 {
   143     return (0);
   144 }
   145 
   146 void
   147 SDL_SYS_TimerQuit(void)
   148 {
   149     SDL_SetTimer(0, NULL);
   150 }
   151 
   152 int
   153 SDL_SYS_StartTimer(void)
   154 {
   155     timerStart = SDL_GetTicks();
   156 
   157     return (0);
   158 }
   159 
   160 void
   161 SDL_SYS_StopTimer(void)
   162 {
   163     /* Don't need to do anything as we use SDL_timer_running
   164        to detect if we need to check the timer */
   165 }
   166 
   167 
   168 void
   169 RISCOS_CheckTimer()
   170 {
   171     if (SDL_timer_running
   172         && SDL_GetTicks() - timerStart >= SDL_alarm_interval) {
   173         Uint32 ms;
   174 
   175         ms = SDL_alarm_callback(SDL_alarm_interval);
   176         if (ms != SDL_alarm_interval) {
   177             if (ms) {
   178                 SDL_alarm_interval = ROUND_RESOLUTION(ms);
   179             } else {
   180                 SDL_alarm_interval = 0;
   181                 SDL_timer_running = 0;
   182             }
   183         }
   184         if (SDL_alarm_interval)
   185             timerStart = SDL_GetTicks();
   186     }
   187 }
   188 
   189 #else
   190 
   191 /* Threaded version of timer - based on code for linux */
   192 
   193 #include "SDL_thread.h"
   194 
   195 /* Data to handle a single periodic alarm */
   196 static int timer_alive = 0;
   197 static SDL_Thread *timer = NULL;
   198 
   199 static int
   200 RunTimer(void *unused)
   201 {
   202     while (timer_alive) {
   203         if (SDL_timer_running) {
   204             SDL_ThreadedTimerCheck();
   205         }
   206         SDL_Delay(1);
   207     }
   208     return (0);
   209 }
   210 
   211 /* This is only called if the event thread is not running */
   212 int
   213 SDL_SYS_TimerInit(void)
   214 {
   215     timer_alive = 1;
   216     timer = SDL_CreateThread(RunTimer, NULL);
   217     if (timer == NULL)
   218         return (-1);
   219     return (SDL_SetTimerThreaded(1));
   220 }
   221 
   222 void
   223 SDL_SYS_TimerQuit(void)
   224 {
   225     timer_alive = 0;
   226     if (timer) {
   227         SDL_WaitThread(timer, NULL);
   228         timer = NULL;
   229     }
   230 }
   231 
   232 int
   233 SDL_SYS_StartTimer(void)
   234 {
   235     SDL_SetError("Internal logic error: RISC OS uses threaded timer");
   236     return (-1);
   237 }
   238 
   239 void
   240 SDL_SYS_StopTimer(void)
   241 {
   242     return;
   243 }
   244 
   245 #endif /* SDL_THREADS_DISABLED */
   246 
   247 #endif /* SDL_TIMER_RISCOS */
   248 /* vi: set ts=4 sw=4 expandtab: */