src/timer/os2/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
icculus@1190
     1
/*
icculus@1190
     2
    SDL - Simple DirectMedia Layer
slouken@1312
     3
    Copyright (C) 1997-2006 Sam Lantinga
icculus@1190
     4
icculus@1190
     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
icculus@1190
     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.
icculus@1190
     9
icculus@1190
    10
    This library is distributed in the hope that it will be useful,
icculus@1190
    11
    but WITHOUT ANY WARRANTY; without even the implied warranty of
icculus@1190
    12
    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
slouken@1312
    13
    Lesser General Public License for more details.
icculus@1190
    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
icculus@1190
    18
icculus@1190
    19
    Sam Lantinga
icculus@1190
    20
    slouken@libsdl.org
icculus@1190
    21
*/
slouken@1402
    22
#include "SDL_config.h"
icculus@1190
    23
slouken@1635
    24
#ifdef SDL_TIMER_OS2
slouken@1635
    25
icculus@1190
    26
#define INCL_DOSMISC
icculus@1190
    27
#define INCL_DOSERRORS
icculus@1190
    28
#define INCL_DOSSEMAPHORES
icculus@1190
    29
#define INCL_DOSDATETIME
icculus@1190
    30
#define INCL_DOSPROCESS
icculus@1190
    31
#define INCL_DOSPROFILE
icculus@1190
    32
#define INCL_DOSEXCEPTIONS
icculus@1190
    33
#include <os2.h>
icculus@1190
    34
icculus@1190
    35
#include "SDL_thread.h"
icculus@1190
    36
#include "SDL_timer.h"
slouken@1361
    37
#include "../SDL_timer_c.h"
icculus@1190
    38
icculus@1190
    39
icculus@1190
    40
#define TIME_WRAP_VALUE (~(DWORD)0)
icculus@1190
    41
icculus@1190
    42
/* The first high-resolution ticks value of the application */
icculus@1190
    43
static long long hires_start_ticks;
icculus@1190
    44
/* The number of ticks per second of the high-resolution performance counter */
icculus@1190
    45
static ULONG hires_ticks_per_second;
icculus@1190
    46
slouken@1662
    47
void
slouken@1668
    48
SDL_StartTicks(void)
icculus@1190
    49
{
slouken@1668
    50
    DosTmrQueryFreq(&hires_ticks_per_second);
slouken@1668
    51
    DosTmrQueryTime((PQWORD) & hires_start_ticks);
icculus@1190
    52
}
icculus@1190
    53
slouken@1662
    54
DECLSPEC Uint32 SDLCALL
slouken@1668
    55
SDL_GetTicks(void)
icculus@1190
    56
{
slouken@1662
    57
    long long hires_now;
slouken@1662
    58
    ULONG ticks = ticks;
icculus@1190
    59
slouken@1668
    60
    DosTmrQueryTime((PQWORD) & hires_now);
icculus@1190
    61
/*
icculus@1190
    62
        hires_now -= hires_start_ticks;
icculus@1190
    63
        hires_now *= 1000;
icculus@1190
    64
        hires_now /= hires_ticks_per_second;
icculus@1190
    65
*/
slouken@1662
    66
    /* inline asm to avoid runtime inclusion */
slouken@1662
    67
    _asm {
slouken@1662
    68
    push edx
slouken@1662
    69
            push eax
slouken@1662
    70
            mov eax, dword ptr hires_now
slouken@1662
    71
            mov edx, dword ptr hires_now + 4
slouken@1662
    72
            sub eax, dword ptr hires_start_ticks
slouken@1662
    73
            sbb edx, dword ptr hires_start_ticks + 4
slouken@1662
    74
            mov ebx, 1000
slouken@1662
    75
            mov ecx, edx
slouken@1662
    76
            mul ebx
slouken@1662
    77
            push eax
slouken@1662
    78
            push edx
slouken@1662
    79
            mov eax, ecx
slouken@1662
    80
            mul ebx
slouken@1662
    81
            pop eax
slouken@1662
    82
            add edx, eax
slouken@1662
    83
            pop eax
slouken@1662
    84
            mov ebx, dword ptr hires_ticks_per_second
slouken@1662
    85
            div ebx mov dword ptr ticks, eax pop edx pop eax}
icculus@1190
    86
slouken@1662
    87
    return ticks;
icculus@1190
    88
icculus@1190
    89
}
icculus@1190
    90
icculus@1190
    91
/* High resolution sleep, originally made by Ilya Zakharevich */
slouken@1662
    92
DECLSPEC void SDLCALL
slouken@1668
    93
SDL_Delay(Uint32 ms)
icculus@1190
    94
{
slouken@1662
    95
    /* This is similar to DosSleep(), but has 8ms granularity in time-critical
slouken@1662
    96
       threads even on Warp3. */
slouken@1662
    97
    HEV hevEvent1 = 0;          /* Event semaphore handle    */
slouken@1662
    98
    HTIMER htimerEvent1 = 0;    /* Timer handle              */
slouken@1662
    99
    APIRET rc = NO_ERROR;       /* Return code               */
slouken@1662
   100
    int ret = 1;
slouken@1662
   101
    ULONG priority = 0, nesting;        /* Shut down the warnings */
slouken@1662
   102
    PPIB pib;
slouken@1662
   103
    PTIB tib;
slouken@1662
   104
    char *e = NULL;
slouken@1662
   105
    APIRET badrc;
slouken@1662
   106
    int switch_priority = 50;
icculus@1190
   107
slouken@1668
   108
    DosCreateEventSem(NULL,     /* Unnamed */
slouken@1668
   109
                      &hevEvent1,       /* Handle of semaphore returned */
slouken@1668
   110
                      DC_SEM_SHARED,    /* Shared needed for DosAsyncTimer */
slouken@1668
   111
                      FALSE);   /* Semaphore is in RESET state  */
icculus@1190
   112
slouken@1662
   113
    if (ms >= switch_priority)
slouken@1662
   114
        switch_priority = 0;
slouken@1662
   115
    if (switch_priority) {
slouken@1668
   116
        if (DosGetInfoBlocks(&tib, &pib) != NO_ERROR)
slouken@1662
   117
            switch_priority = 0;
slouken@1662
   118
        else {
slouken@1662
   119
            /* In Warp3, to switch scheduling to 8ms step, one needs to do 
slouken@1662
   120
               DosAsyncTimer() in time-critical thread.  On laters versions,
slouken@1662
   121
               more and more cases of wait-for-something are covered.
icculus@1190
   122
slouken@1662
   123
               It turns out that on Warp3fp42 it is the priority at the time
slouken@1662
   124
               of DosAsyncTimer() which matters.  Let's hope that this works
slouken@1662
   125
               with later versions too...  XXXX
slouken@1662
   126
             */
slouken@1662
   127
            priority = (tib->tib_ptib2->tib2_ulpri);
slouken@1662
   128
            if ((priority & 0xFF00) == 0x0300)  /* already time-critical */
slouken@1662
   129
                switch_priority = 0;
slouken@1662
   130
            /* Make us time-critical.  Just modifying TIB is not enough... */
slouken@1662
   131
            /* tib->tib_ptib2->tib2_ulpri = 0x0300; */
slouken@1662
   132
            /* We do not want to run at high priority if a signal causes us
slouken@1662
   133
               to longjmp() out of this section... */
slouken@1668
   134
            if (DosEnterMustComplete(&nesting))
slouken@1662
   135
                switch_priority = 0;
slouken@1662
   136
            else
slouken@1668
   137
                DosSetPriority(PRTYS_THREAD, PRTYC_TIMECRITICAL, 0, 0);
slouken@1662
   138
        }
icculus@1190
   139
    }
icculus@1190
   140
slouken@1668
   141
    if ((badrc = DosAsyncTimer(ms, (HSEM) hevEvent1,    /* Semaphore to post        */
slouken@1668
   142
                               &htimerEvent1))) /* Timer handler (returned) */
slouken@1662
   143
        e = "DosAsyncTimer";
icculus@1190
   144
slouken@1662
   145
    if (switch_priority && tib->tib_ptib2->tib2_ulpri == 0x0300) {
slouken@1662
   146
        /* Nobody switched priority while we slept...  Ignore errors... */
slouken@1662
   147
        /* tib->tib_ptib2->tib2_ulpri = priority; *//* Get back... */
slouken@1662
   148
        if (!
slouken@1668
   149
            (rc = DosSetPriority(PRTYS_THREAD, (priority >> 8) & 0xFF, 0, 0)))
slouken@1668
   150
            rc = DosSetPriority(PRTYS_THREAD, 0, priority & 0xFF, 0);
slouken@1662
   151
    }
slouken@1662
   152
    if (switch_priority)
slouken@1668
   153
        rc = DosExitMustComplete(&nesting);     /* Ignore errors */
icculus@1190
   154
slouken@1662
   155
    /* The actual blocking call is made with "normal" priority.  This way we
slouken@1662
   156
       should not bother with DosSleep(0) etc. to compensate for us interrupting
slouken@1662
   157
       higher-priority threads.  The goal is to prohibit the system spending too
slouken@1662
   158
       much time halt()ing, not to run us "no matter what". */
slouken@1662
   159
    if (!e)                     /* Wait for AsyncTimer event */
slouken@1668
   160
        badrc = DosWaitEventSem(hevEvent1, SEM_INDEFINITE_WAIT);
icculus@1190
   161
slouken@1662
   162
    if (e);                     /* Do nothing */
slouken@1662
   163
    else if (badrc == ERROR_INTERRUPT)
slouken@1662
   164
        ret = 0;
slouken@1662
   165
    else if (badrc)
slouken@1662
   166
        e = "DosWaitEventSem";
slouken@1668
   167
    if ((rc = DosCloseEventSem(hevEvent1)) && !e) {     /* Get rid of semaphore */
slouken@1662
   168
        e = "DosCloseEventSem";
slouken@1662
   169
        badrc = rc;
slouken@1662
   170
    }
slouken@1662
   171
    if (e) {
slouken@1668
   172
        SDL_SetError("[SDL_Delay] : Had error in %s(), rc is 0x%x\n", e,
slouken@1668
   173
                     badrc);
slouken@1662
   174
    }
icculus@1190
   175
}
icculus@1190
   176
icculus@1190
   177
/* Data to handle a single periodic alarm */
icculus@1190
   178
static int timer_alive = 0;
icculus@1190
   179
static SDL_Thread *timer = NULL;
icculus@1190
   180
slouken@1662
   181
static int
slouken@1668
   182
RunTimer(void *unused)
icculus@1190
   183
{
slouken@1668
   184
    DosSetPriority(PRTYS_THREAD, PRTYC_TIMECRITICAL, 0, 0);
slouken@1662
   185
    while (timer_alive) {
slouken@1662
   186
        if (SDL_timer_running) {
slouken@1668
   187
            SDL_ThreadedTimerCheck();
icculus@1190
   188
        }
slouken@1668
   189
        SDL_Delay(10);
slouken@1662
   190
    }
slouken@1662
   191
    return (0);
icculus@1190
   192
}
icculus@1190
   193
icculus@1190
   194
/* This is only called if the event thread is not running */
slouken@1662
   195
int
slouken@1668
   196
SDL_SYS_TimerInit(void)
icculus@1190
   197
{
slouken@1662
   198
    timer_alive = 1;
slouken@1668
   199
    timer = SDL_CreateThread(RunTimer, NULL);
slouken@1662
   200
    if (timer == NULL)
slouken@1662
   201
        return (-1);
slouken@1668
   202
    return (SDL_SetTimerThreaded(1));
icculus@1190
   203
}
icculus@1190
   204
slouken@1662
   205
void
slouken@1668
   206
SDL_SYS_TimerQuit(void)
icculus@1190
   207
{
slouken@1662
   208
    timer_alive = 0;
slouken@1662
   209
    if (timer) {
slouken@1668
   210
        SDL_WaitThread(timer, NULL);
slouken@1662
   211
        timer = NULL;
slouken@1662
   212
    }
icculus@1190
   213
}
icculus@1190
   214
slouken@1662
   215
int
slouken@1668
   216
SDL_SYS_StartTimer(void)
icculus@1190
   217
{
slouken@1668
   218
    SDL_SetError("Internal logic error: OS/2 uses threaded timer");
slouken@1662
   219
    return (-1);
icculus@1190
   220
}
icculus@1190
   221
slouken@1662
   222
void
slouken@1668
   223
SDL_SYS_StopTimer(void)
icculus@1190
   224
{
slouken@1662
   225
    return;
icculus@1190
   226
}
icculus@1190
   227
slouken@1635
   228
#endif /* SDL_TIMER_OS2 */
slouken@1662
   229
/* vi: set ts=4 sw=4 expandtab: */