Added initial support for CLOCK_MONOTONIC.

This commit is contained in:
Camilla Berglund 2011-09-20 00:55:20 +02:00
parent df75a2dc2f
commit eede75fe5e
4 changed files with 51 additions and 26 deletions

View File

@ -309,6 +309,7 @@ version of GLFW.</p>
<li>[Cocoa] Bugfix: The loop condition for saving video modes used the wrong index variable</li> <li>[Cocoa] Bugfix: The loop condition for saving video modes used the wrong index variable</li>
<li>[Cocoa] Bugfix: The OpenGL framework was not retrieved, making glfwGetProcAddress crash</li> <li>[Cocoa] Bugfix: The OpenGL framework was not retrieved, making glfwGetProcAddress crash</li>
<li>[X11] Added support for the <code>GLX_EXT_swap_control</code> extension as an alternative to <code>GLX_SGI_swap_control</code></li> <li>[X11] Added support for the <code>GLX_EXT_swap_control</code> extension as an alternative to <code>GLX_SGI_swap_control</code></li>
<li>[X11] Added the POSIX <code>CLOCK_MONOTONIC</code> time source as the preferred method</li>
<li>[X11] Bugfix: Calling <code>glXCreateContextAttribsARB</code> with an unavailable OpenGL version caused the application to terminate with a <code>BadMatch</code> Xlib error</li> <li>[X11] Bugfix: Calling <code>glXCreateContextAttribsARB</code> with an unavailable OpenGL version caused the application to terminate with a <code>BadMatch</code> Xlib error</li>
<li>[Win32] Removed explicit support for versions of Windows older than Windows XP</li> <li>[Win32] Removed explicit support for versions of Windows older than Windows XP</li>
<li>[Win32] Bugfix: Window activation and iconification did not work as expected</li> <li>[Win32] Bugfix: Window activation and iconification did not work as expected</li>

View File

@ -639,6 +639,9 @@ const char* _glfwPlatformGetVersionString(void)
#else #else
" no-extension-support" " no-extension-support"
#endif #endif
#if defined(_POSIX_TIMERS) && defined(_POSIX_MONOTONIC_CLOCK)
" clock_gettime"
#endif
#if defined(_GLFW_USE_LINUX_JOYSTICKS) #if defined(_GLFW_USE_LINUX_JOYSTICKS)
" Linux-joystick-API" " Linux-joystick-API"
#else #else

View File

@ -31,9 +31,9 @@
#ifndef _platform_h_ #ifndef _platform_h_
#define _platform_h_ #define _platform_h_
#include <sys/time.h>
#include <unistd.h> #include <unistd.h>
#include <signal.h> #include <signal.h>
#include <stdint.h>
#include <X11/Xlib.h> #include <X11/Xlib.h>
#include <X11/keysym.h> #include <X11/keysym.h>
#include <X11/Xatom.h> #include <X11/Xatom.h>
@ -220,8 +220,9 @@ typedef struct _GLFWlibraryX11
// Timer data // Timer data
struct { struct {
GLboolean monotonic;
double resolution; double resolution;
long long t0; uint64_t t0;
} timer; } timer;
#if defined(_GLFW_DLOPEN_LIBGL) #if defined(_GLFW_DLOPEN_LIBGL)

View File

@ -30,6 +30,33 @@
#include "internal.h" #include "internal.h"
#include <time.h>
//========================================================================
// Return raw time
//========================================================================
static uint64_t getRawTime(void)
{
#if defined(_POSIX_TIMERS) && defined(_POSIX_MONOTONIC_CLOCK)
if (_glfwLibrary.X11.timer.monotonic)
{
struct timespec ts;
clock_gettime(CLOCK_MONOTONIC, &ts);
return (uint64_t) ts.tv_sec * (uint64_t) 1000000000 + (uint64_t) ts.tv_nsec;
}
else
#endif
{
struct timeval tv;
gettimeofday(&tv, NULL);
return (uint64_t) tv.tv_sec * (uint64_t) 1000000 + (uint64_t) tv.tv_usec;
}
}
//======================================================================== //========================================================================
// Initialise timer // Initialise timer
@ -37,15 +64,21 @@
void _glfwInitTimer(void) void _glfwInitTimer(void)
{ {
struct timeval tv; #if defined(_POSIX_TIMERS) && defined(_POSIX_MONOTONIC_CLOCK)
struct timespec ts;
// "Resolution" is 1 us if (clock_gettime(CLOCK_MONOTONIC, &ts) == 0)
{
_glfwLibrary.X11.timer.monotonic = GL_TRUE;
_glfwLibrary.X11.timer.resolution = 1e-9;
}
else
#endif
{
_glfwLibrary.X11.timer.resolution = 1e-6; _glfwLibrary.X11.timer.resolution = 1e-6;
}
// Set start-time for timer _glfwLibrary.X11.timer.t0 = getRawTime();
gettimeofday(&tv, NULL);
_glfwLibrary.X11.timer.t0 = (long long) tv.tv_sec * (long long) 1000000 +
(long long) tv.tv_usec;
} }
@ -59,14 +92,8 @@ void _glfwInitTimer(void)
double _glfwPlatformGetTime(void) double _glfwPlatformGetTime(void)
{ {
long long t; return (double) (getRawTime() - _glfwLibrary.X11.timer.t0) *
struct timeval tv; _glfwLibrary.X11.timer.resolution;
gettimeofday(&tv, NULL);
t = (long long) tv.tv_sec * (long long) 1000000 +
(long long) tv.tv_usec;
return (double)(t - _glfwLibrary.X11.timer.t0) * _glfwLibrary.X11.timer.resolution;
} }
@ -76,14 +103,7 @@ double _glfwPlatformGetTime(void)
void _glfwPlatformSetTime(double t) void _glfwPlatformSetTime(double t)
{ {
long long t0; _glfwLibrary.X11.timer.t0 = getRawTime() -
struct timeval tv; (uint64_t) (t / _glfwLibrary.X11.timer.resolution);
gettimeofday(&tv, NULL);
t0 = (long long) tv.tv_sec * (long long) 1000000 +
(long long) tv.tv_usec;
// Calulate new starting time
_glfwLibrary.X11.timer.t0 = t0 - (long long)(t / _glfwLibrary.X11.timer.resolution);
} }