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