src/timer/riscos/SDL_systimer.c
branchSDL-1.3
changeset 1668 4da1ee79c9af
parent 1662 782fd950bd46
equal deleted inserted replaced
1667:1fddae038bc8 1668:4da1ee79c9af
    35 
    35 
    36 #if SDL_THREADS_DISABLED
    36 #if SDL_THREADS_DISABLED
    37 /* Timer  SDL_arraysize(Timer ),start/reset time */
    37 /* Timer  SDL_arraysize(Timer ),start/reset time */
    38 static Uint32 timerStart;
    38 static Uint32 timerStart;
    39 /* Timer running function */
    39 /* Timer running function */
    40 void RISCOS_CheckTimer ();
    40 void RISCOS_CheckTimer();
    41 #else
    41 #else
    42 #include <pthread.h>
    42 #include <pthread.h>
    43 extern Uint32 riscos_main_thread;
    43 extern Uint32 riscos_main_thread;
    44 extern int riscos_using_threads;
    44 extern int riscos_using_threads;
    45 extern Uint32 SDL_ThreadID ();
    45 extern Uint32 SDL_ThreadID();
    46 extern Uint32 SDL_EventThreadID (void);
    46 extern Uint32 SDL_EventThreadID(void);
    47 #endif
    47 #endif
    48 
    48 
    49 
    49 
    50 extern void RISCOS_BackgroundTasks (void);
    50 extern void RISCOS_BackgroundTasks(void);
    51 
    51 
    52 /* The first ticks value of the application */
    52 /* The first ticks value of the application */
    53 clock_t start;
    53 clock_t start;
    54 
    54 
    55 void
    55 void
    56 SDL_StartTicks (void)
    56 SDL_StartTicks(void)
    57 {
    57 {
    58     /* Set first ticks value */
    58     /* Set first ticks value */
    59     start = clock ();
    59     start = clock();
    60 }
    60 }
    61 
    61 
    62 Uint32
    62 Uint32
    63 SDL_GetTicks (void)
    63 SDL_GetTicks(void)
    64 {
    64 {
    65     clock_t ticks;
    65     clock_t ticks;
    66 
    66 
    67     ticks = clock () - start;
    67     ticks = clock() - start;
    68 
    68 
    69 
    69 
    70 #if CLOCKS_PER_SEC == 1000
    70 #if CLOCKS_PER_SEC == 1000
    71 
    71 
    72     return (ticks);
    72     return (ticks);
    82 #endif
    82 #endif
    83 
    83 
    84 }
    84 }
    85 
    85 
    86 void
    86 void
    87 SDL_Delay (Uint32 ms)
    87 SDL_Delay(Uint32 ms)
    88 {
    88 {
    89     Uint32 now, then, elapsed;
    89     Uint32 now, then, elapsed;
    90 #if !SDL_THREADS_DISABLED
    90 #if !SDL_THREADS_DISABLED
    91     int is_event_thread;
    91     int is_event_thread;
    92     if (riscos_using_threads) {
    92     if (riscos_using_threads) {
    93         is_event_thread = 0;
    93         is_event_thread = 0;
    94         if (SDL_EventThreadID ()) {
    94         if (SDL_EventThreadID()) {
    95             if (SDL_EventThreadID () == SDL_ThreadID ())
    95             if (SDL_EventThreadID() == SDL_ThreadID())
    96                 is_event_thread = 1;
    96                 is_event_thread = 1;
    97         } else if (SDL_ThreadID () == riscos_main_thread)
    97         } else if (SDL_ThreadID() == riscos_main_thread)
    98             is_event_thread = 1;
    98             is_event_thread = 1;
    99     } else
    99     } else
   100         is_event_thread = 1;
   100         is_event_thread = 1;
   101 #endif
   101 #endif
   102 
   102 
   103     /*TODO: Next version of Unixlib may allow us to use usleep here */
   103     /*TODO: Next version of Unixlib may allow us to use usleep here */
   104     /*      for non event threads */
   104     /*      for non event threads */
   105 
   105 
   106     /* Set the timeout interval - Linux only needs to do this once */
   106     /* Set the timeout interval - Linux only needs to do this once */
   107     then = SDL_GetTicks ();
   107     then = SDL_GetTicks();
   108 
   108 
   109     do {
   109     do {
   110         /* Do background tasks required while sleeping as we are not multithreaded */
   110         /* Do background tasks required while sleeping as we are not multithreaded */
   111 #if SDL_THREADS_DISABLED
   111 #if SDL_THREADS_DISABLED
   112         RISCOS_BackgroundTasks ();
   112         RISCOS_BackgroundTasks();
   113 #else
   113 #else
   114         /* For threaded build only run background tasks in event thread */
   114         /* For threaded build only run background tasks in event thread */
   115         if (is_event_thread)
   115         if (is_event_thread)
   116             RISCOS_BackgroundTasks ();
   116             RISCOS_BackgroundTasks();
   117 #endif
   117 #endif
   118 
   118 
   119         /* Calculate the time interval left (in case of interrupt) */
   119         /* Calculate the time interval left (in case of interrupt) */
   120         now = SDL_GetTicks ();
   120         now = SDL_GetTicks();
   121         elapsed = (now - then);
   121         elapsed = (now - then);
   122         then = now;
   122         then = now;
   123         if (elapsed >= ms) {
   123         if (elapsed >= ms) {
   124             break;
   124             break;
   125         }
   125         }
   126         ms -= elapsed;
   126         ms -= elapsed;
   127 #if !SDL_THREADS_DISABLED
   127 #if !SDL_THREADS_DISABLED
   128         /* Need to yield to let other threads have a go */
   128         /* Need to yield to let other threads have a go */
   129         if (riscos_using_threads)
   129         if (riscos_using_threads)
   130             pthread_yield ();
   130             pthread_yield();
   131 #endif
   131 #endif
   132 
   132 
   133     }
   133     }
   134     while (1);
   134     while (1);
   135 }
   135 }
   137 #if SDL_THREADS_DISABLED
   137 #if SDL_THREADS_DISABLED
   138 
   138 
   139 /* Non-threaded version of timer */
   139 /* Non-threaded version of timer */
   140 
   140 
   141 int
   141 int
   142 SDL_SYS_TimerInit (void)
   142 SDL_SYS_TimerInit(void)
   143 {
   143 {
   144     return (0);
   144     return (0);
   145 }
   145 }
   146 
   146 
   147 void
   147 void
   148 SDL_SYS_TimerQuit (void)
   148 SDL_SYS_TimerQuit(void)
   149 {
   149 {
   150     SDL_SetTimer (0, NULL);
   150     SDL_SetTimer(0, NULL);
   151 }
   151 }
   152 
   152 
   153 int
   153 int
   154 SDL_SYS_StartTimer (void)
   154 SDL_SYS_StartTimer(void)
   155 {
   155 {
   156     timerStart = SDL_GetTicks ();
   156     timerStart = SDL_GetTicks();
   157 
   157 
   158     return (0);
   158     return (0);
   159 }
   159 }
   160 
   160 
   161 void
   161 void
   162 SDL_SYS_StopTimer (void)
   162 SDL_SYS_StopTimer(void)
   163 {
   163 {
   164     /* Don't need to do anything as we use SDL_timer_running
   164     /* Don't need to do anything as we use SDL_timer_running
   165        to detect if we need to check the timer */
   165        to detect if we need to check the timer */
   166 }
   166 }
   167 
   167 
   168 
   168 
   169 void
   169 void
   170 RISCOS_CheckTimer ()
   170 RISCOS_CheckTimer()
   171 {
   171 {
   172     if (SDL_timer_running
   172     if (SDL_timer_running
   173         && SDL_GetTicks () - timerStart >= SDL_alarm_interval) {
   173         && SDL_GetTicks() - timerStart >= SDL_alarm_interval) {
   174         Uint32 ms;
   174         Uint32 ms;
   175 
   175 
   176         ms = SDL_alarm_callback (SDL_alarm_interval);
   176         ms = SDL_alarm_callback(SDL_alarm_interval);
   177         if (ms != SDL_alarm_interval) {
   177         if (ms != SDL_alarm_interval) {
   178             if (ms) {
   178             if (ms) {
   179                 SDL_alarm_interval = ROUND_RESOLUTION (ms);
   179                 SDL_alarm_interval = ROUND_RESOLUTION(ms);
   180             } else {
   180             } else {
   181                 SDL_alarm_interval = 0;
   181                 SDL_alarm_interval = 0;
   182                 SDL_timer_running = 0;
   182                 SDL_timer_running = 0;
   183             }
   183             }
   184         }
   184         }
   185         if (SDL_alarm_interval)
   185         if (SDL_alarm_interval)
   186             timerStart = SDL_GetTicks ();
   186             timerStart = SDL_GetTicks();
   187     }
   187     }
   188 }
   188 }
   189 
   189 
   190 #else
   190 #else
   191 
   191 
   196 /* Data to handle a single periodic alarm */
   196 /* Data to handle a single periodic alarm */
   197 static int timer_alive = 0;
   197 static int timer_alive = 0;
   198 static SDL_Thread *timer = NULL;
   198 static SDL_Thread *timer = NULL;
   199 
   199 
   200 static int
   200 static int
   201 RunTimer (void *unused)
   201 RunTimer(void *unused)
   202 {
   202 {
   203     while (timer_alive) {
   203     while (timer_alive) {
   204         if (SDL_timer_running) {
   204         if (SDL_timer_running) {
   205             SDL_ThreadedTimerCheck ();
   205             SDL_ThreadedTimerCheck();
   206         }
   206         }
   207         SDL_Delay (1);
   207         SDL_Delay(1);
   208     }
   208     }
   209     return (0);
   209     return (0);
   210 }
   210 }
   211 
   211 
   212 /* This is only called if the event thread is not running */
   212 /* This is only called if the event thread is not running */
   213 int
   213 int
   214 SDL_SYS_TimerInit (void)
   214 SDL_SYS_TimerInit(void)
   215 {
   215 {
   216     timer_alive = 1;
   216     timer_alive = 1;
   217     timer = SDL_CreateThread (RunTimer, NULL);
   217     timer = SDL_CreateThread(RunTimer, NULL);
   218     if (timer == NULL)
   218     if (timer == NULL)
   219         return (-1);
   219         return (-1);
   220     return (SDL_SetTimerThreaded (1));
   220     return (SDL_SetTimerThreaded(1));
   221 }
   221 }
   222 
   222 
   223 void
   223 void
   224 SDL_SYS_TimerQuit (void)
   224 SDL_SYS_TimerQuit(void)
   225 {
   225 {
   226     timer_alive = 0;
   226     timer_alive = 0;
   227     if (timer) {
   227     if (timer) {
   228         SDL_WaitThread (timer, NULL);
   228         SDL_WaitThread(timer, NULL);
   229         timer = NULL;
   229         timer = NULL;
   230     }
   230     }
   231 }
   231 }
   232 
   232 
   233 int
   233 int
   234 SDL_SYS_StartTimer (void)
   234 SDL_SYS_StartTimer(void)
   235 {
   235 {
   236     SDL_SetError ("Internal logic error: RISC OS uses threaded timer");
   236     SDL_SetError("Internal logic error: RISC OS uses threaded timer");
   237     return (-1);
   237     return (-1);
   238 }
   238 }
   239 
   239 
   240 void
   240 void
   241 SDL_SYS_StopTimer (void)
   241 SDL_SYS_StopTimer(void)
   242 {
   242 {
   243     return;
   243     return;
   244 }
   244 }
   245 
   245 
   246 #endif /* SDL_THREADS_DISABLED */
   246 #endif /* SDL_THREADS_DISABLED */