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