include/SDL_timer.h
author Sam Lantinga <slouken@libsdl.org>
Thu, 27 Jan 2011 14:45:06 -0800
changeset 5111 481dabb098ef
parent 3697 f7b03b6838cb
child 5262 b530ef003506
permissions -rw-r--r--
Improved timer implementation

The new timer model is formalized as using a separate thread to handle timer callbacks. This was the case on almost every platform before, but it's now a requirement, and simplifies the implementation and makes it perform consistently across platforms.

Goals:
* Minimize timer thread blocking
* Dispatch timers as accurately as possible
* SDL_AddTimer() and SDL_RemoveTimer() are completely threadsafe
* SDL_RemoveTimer() doesn't crash with a timer that's expired or removed
     1 /*
     2     SDL - Simple DirectMedia Layer
     3     Copyright (C) 1997-2010 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 
    23 #ifndef _SDL_timer_h
    24 #define _SDL_timer_h
    25 
    26 /**
    27  *  \file SDL_timer.h
    28  *  
    29  *  Header for the SDL time management routines.
    30  */
    31 
    32 #include "SDL_stdinc.h"
    33 #include "SDL_error.h"
    34 
    35 #include "begin_code.h"
    36 /* Set up for C function definitions, even when using C++ */
    37 #ifdef __cplusplus
    38 /* *INDENT-OFF* */
    39 extern "C" {
    40 /* *INDENT-ON* */
    41 #endif
    42 
    43 /**
    44  * \brief Get the number of milliseconds since the SDL library initialization.
    45  *  
    46  * \note This value wraps if the program runs for more than ~49 days.
    47  */
    48 extern DECLSPEC Uint32 SDLCALL SDL_GetTicks(void);
    49 
    50 /**
    51  * \brief Wait a specified number of milliseconds before returning.
    52  */
    53 extern DECLSPEC void SDLCALL SDL_Delay(Uint32 ms);
    54 
    55 /**
    56  *  Function prototype for the timer callback function.
    57  *  
    58  *  The callback function is passed the current timer interval and returns
    59  *  the next timer interval.  If the returned value is the same as the one
    60  *  passed in, the periodic alarm continues, otherwise a new alarm is
    61  *  scheduled.  If the callback returns 0, the periodic alarm is cancelled.
    62  */
    63 typedef Uint32 (SDLCALL * SDL_TimerCallback) (Uint32 interval, void *param);
    64 
    65 /**
    66  * Definition of the timer ID type.
    67  */
    68 typedef int SDL_TimerID;
    69 
    70 /**
    71  * \brief Add a new timer to the pool of timers already running.
    72  *
    73  * \return A timer ID, or NULL when an error occurs.
    74  */
    75 extern DECLSPEC SDL_TimerID SDLCALL SDL_AddTimer(Uint32 interval,
    76                                                  SDL_TimerCallback callback,
    77                                                  void *param);
    78 
    79 /**
    80  * \brief Remove a timer knowing its ID.
    81  *
    82  * \return A boolean value indicating success or failure.
    83  *
    84  * \warning It is not safe to remove a timer multiple times.
    85  */
    86 extern DECLSPEC SDL_bool SDLCALL SDL_RemoveTimer(SDL_TimerID t);
    87 
    88 
    89 /* Ends C function definitions when using C++ */
    90 #ifdef __cplusplus
    91 /* *INDENT-OFF* */
    92 }
    93 /* *INDENT-ON* */
    94 #endif
    95 #include "close_code.h"
    96 
    97 #endif /* _SDL_timer_h */
    98 
    99 /* vi: set ts=4 sw=4 expandtab: */