Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
40 changes: 36 additions & 4 deletions src/essos/pxTimerNative.cpp
100755 → 100644
Original file line number Diff line number Diff line change
Expand Up @@ -18,9 +18,12 @@ limitations under the License.

// pxTimerNative.cpp

#if __cplusplus < 201103L

#include "../pxTimer.h"

#include <stdlib.h>
#include <errno.h>

#define USE_CGT

Expand Down Expand Up @@ -69,10 +72,39 @@ double pxMicroseconds()
#endif
}

void pxSleepUS(uint64_t usToSleep)
{
struct timespec res;

res.tv_sec = (usToSleep / 1000000UL);
res.tv_nsec = (usToSleep * 1000UL) % 1000000000UL;

while (true)
{
struct timespec remain;
const int rv = clock_nanosleep(CLOCK_MONOTONIC, 0, &res, &remain);

if (rv == 0)
{
break;
}

if (errno == EINTR)
{
res = remain;
continue;
}

// Theoretically impossible case in our case :-)
// At this point something went wrong but we cannot
// return any error code as pxSleepMS returns void.
abort();
}
}

void pxSleepMS(uint32_t msToSleep)
{
timeval tv;
tv.tv_sec = 0;
tv.tv_usec = 1000 * msToSleep;
select(0, NULL, NULL, NULL, &tv);
pxSleepUS(msToSleep * 1000UL);
}

#endif // __cplusplus < 201103L
40 changes: 36 additions & 4 deletions src/gles/pxTimerNative.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -18,9 +18,12 @@ limitations under the License.

// pxTimerNative.cpp

#if __cplusplus < 201103L

#include "../pxTimer.h"

#include <stdlib.h>
#include <errno.h>

#define USE_CGT

Expand Down Expand Up @@ -69,10 +72,39 @@ double pxMicroseconds()
#endif
}

void pxSleepUS(uint64_t usToSleep)
{
struct timespec res;

res.tv_sec = (usToSleep / 1000000UL);
res.tv_nsec = (usToSleep * 1000UL) % 1000000000UL;

while (true)
{
struct timespec remain;
const int rv = clock_nanosleep(CLOCK_MONOTONIC, 0, &res, &remain);

if (rv == 0)
{
break;
}

if (errno == EINTR)
{
res = remain;
continue;
}

// Theoretically impossible case in our case :-)
// At this point something went wrong but we cannot
// return any error code as pxSleepMS returns void.
abort();
}
}

void pxSleepMS(uint32_t msToSleep)
{
timeval tv;
tv.tv_sec = 0;
tv.tv_usec = 1000 * msToSleep;
select(0, NULL, NULL, NULL, &tv);
pxSleepUS(msToSleep * 1000UL);
}

#endif // __cplusplus < 201103L
91 changes: 56 additions & 35 deletions src/glut/pxTimerNative.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -18,72 +18,93 @@ limitations under the License.

// pxTimerNative.cpp

#include "../pxTimer.h"
#if __cplusplus < 201103L

#include "pxConfigNative.h"
#include "../pxTimer.h"

#include <stdlib.h>
#include <errno.h>

#include "pxConfigNative.h"

#ifndef USE_CGT
#include <sys/time.h>
#else
#include <time.h>
#endif

#ifdef USE_SELECT_FOR_SLEEP
#include <sys/select.h>
#else
#include <unistd.h>
#endif

double pxSeconds()
{
#ifndef USE_CGT
timeval tv;
gettimeofday(&tv, NULL);
return tv.tv_sec + ((double)tv.tv_usec/1000000);
timeval tv;
gettimeofday(&tv, NULL);
return tv.tv_sec + ((double)tv.tv_usec/1000000);
#else
timespec ts;
clock_gettime(CLOCK_MONOTONIC, &ts);
return ts.tv_sec + ((double)ts.tv_nsec/1000000000);
timespec ts;
clock_gettime(CLOCK_MONOTONIC, &ts);
return ts.tv_sec + ((double)ts.tv_nsec/1000000000);
#endif
}

double pxMilliseconds()
{
#ifndef USE_CGT
timeval tv;
gettimeofday(&tv, NULL);
return ((double)(tv.tv_sec * 1000) + ((double)tv.tv_usec/1000));
timeval tv;
gettimeofday(&tv, NULL);
return ((double)(tv.tv_sec * 1000) + ((double)tv.tv_usec/1000));
#else
timespec ts;
clock_gettime(CLOCK_MONOTONIC, &ts);
return ((double)(ts.tv_sec * 1000) + ((double)ts.tv_nsec/1000000));
timespec ts;
clock_gettime(CLOCK_MONOTONIC, &ts);
return ((double)(ts.tv_sec * 1000) + ((double)ts.tv_nsec/1000000));
#endif
}

double pxMicroseconds()
{
#ifndef USE_CGT
timeval tv;
gettimeofday(&tv, NULL);
return (tv.tv_sec * 1000000) + tv.tv_usec;
timeval tv;
gettimeofday(&tv, NULL);
return (tv.tv_sec * 1000000) + tv.tv_usec;
#else
timespec ts;
clock_gettime(CLOCK_MONOTONIC, &ts);
return ((double)(ts.tv_sec * 1000000) + ((double)ts.tv_nsec/1000));
timespec ts;
clock_gettime(CLOCK_MONOTONIC, &ts);
return ((double)(ts.tv_sec * 1000000) + ((double)ts.tv_nsec/1000));
#endif
}

void pxSleepUS(uint64_t usToSleep)
{
struct timespec res;

res.tv_sec = (usToSleep / 1000000UL);
res.tv_nsec = (usToSleep * 1000UL) % 1000000000UL;

while (true)
{
struct timespec remain;
const int rv = clock_nanosleep(CLOCK_MONOTONIC, 0, &res, &remain);

if (rv == 0)
{
break;
}

if (errno == EINTR)
{
res = remain;
continue;
}

// Theoretically impossible case in our case :-)
// At this point something went wrong but we cannot
// return any error code as pxSleepMS returns void.
abort();
}
}

void pxSleepMS(uint32_t msToSleep)
{
#ifdef USE_SELECT_FOR_SLEEP
timeval tv;
tv.tv_sec = 0;
tv.tv_usec = 1000 * msToSleep;
select(0, NULL, NULL, NULL, &tv);
#else
useconds_t s = (useconds_t)msToSleep*1000;
usleep(s);
#endif
pxSleepUS(msToSleep * 1000UL);
}

#endif // __cplusplus < 201103L
13 changes: 11 additions & 2 deletions src/mac/pxTimerNative.mm
Original file line number Diff line number Diff line change
@@ -1,7 +1,9 @@
// pxCore CopyRight 2005-2007 John Robinson
// pxCore CopyRight 2005-2018 John Robinson
// Portable Framebuffer and Windowing Library
// pxTimerNative.cpp

#if __cplusplus < 201103L

#include "pxTimer.h"

#if 0
Expand Down Expand Up @@ -60,7 +62,14 @@
#endif
}

void pxSleepUS(uint64_t usToSleep)
{
usleep(usToSleep);
}

void pxSleepMS(uint32_t msToSleep)
{
usleep(msToSleep*1000);
pxSleepUS(msToSleep*(uint64_t)1000);
}

#endif // __cplusplus < 201103L
47 changes: 42 additions & 5 deletions src/pxTimer.h
Original file line number Diff line number Diff line change
Expand Up @@ -23,10 +23,47 @@

#include <inttypes.h>

double pxSeconds();
double pxMilliseconds();
double pxMicroseconds();
#if __cplusplus >= 201103L // {
// Common implementation for all modern platforms supporting >= c++11

void pxSleepMS(uint32_t msToSleep);
# include <chrono>
# include <ratio>
# include <thread>

#endif
inline double pxMicroseconds()
{
const auto t_now = std::chrono::steady_clock::now();
const auto t_us = std::chrono::time_point_cast<std::chrono::microseconds>(t_now).time_since_epoch();
return t_us.count();
}

inline double pxMilliseconds()
{
return pxMicroseconds() / 1000.0;
}

inline double pxSeconds()
{
return pxMilliseconds() / 1000.0;
}

inline void pxSleepUS(const uint64_t usToSleep)
{
std::this_thread::sleep_for(std::chrono::microseconds(usToSleep));
}

inline void pxSleepMS(const uint32_t msToSleep)
{
pxSleepUS(msToSleep * 1000UL);
}
#else // }{
// Legacy, platform specific, implementation for old platforms not supporting c++11
double pxMicroseconds();
double pxMilliseconds();
double pxSeconds();

void pxSleepMS(uint32_t msToSleep);
void pxSleepUS(uint64_t usToSleep);
#endif // } __cplusplus >= 201103L

#endif // PX_TIMER_H
40 changes: 36 additions & 4 deletions src/wayland/pxTimerNative.cpp
100755 → 100644
Original file line number Diff line number Diff line change
Expand Up @@ -18,9 +18,12 @@ limitations under the License.

// pxTimerNative.cpp

#if __cplusplus < 201103L

#include "../pxTimer.h"

#include <stdlib.h>
#include <errno.h>

#define USE_CGT

Expand Down Expand Up @@ -69,10 +72,39 @@ double pxMicroseconds()
#endif
}

void pxSleepUS(uint64_t usToSleep)
{
struct timespec res;

res.tv_sec = (usToSleep / 1000000UL);
res.tv_nsec = (usToSleep * 1000UL) % 1000000000UL;

while (true)
{
struct timespec remain;
const int rv = clock_nanosleep(CLOCK_MONOTONIC, 0, &res, &remain);

if (rv == 0)
{
break;
}

if (errno == EINTR)
{
res = remain;
continue;
}

// Theoretically impossible case in our case :-)
// At this point something went wrong but we cannot
// return any error code as pxSleepMS returns void.
abort();
}
}

void pxSleepMS(uint32_t msToSleep)
{
timeval tv;
tv.tv_sec = 0;
tv.tv_usec = 1000 * msToSleep;
select(0, NULL, NULL, NULL, &tv);
pxSleepUS(msToSleep * 1000UL);
}

#endif // __cplusplus < 201103L
Loading