[all] moved time and locking methods to the common library

This commit is contained in:
Geoffrey McRae
2020-01-17 14:35:08 +11:00
parent 278d851c7c
commit 4746c89227
13 changed files with 55 additions and 95 deletions

View File

@@ -18,6 +18,8 @@ Place, Suite 330, Boston, MA 02111-1307 USA
*/
#pragma once
#include "time.h"
#define INTERLOCKED_AND8 __sync_fetch_and_and
#define INTERLOCKED_OR8 __sync_fetch_and_or
#define INTERLOCKED_INC(x) __sync_fetch_and_add((x), 1)
@@ -35,3 +37,10 @@ Place, Suite 330, Boston, MA 02111-1307 USA
while(__sync_lock_test_and_set(&(lock), 1)) while((lock)); \
x\
__sync_lock_release(&(lock));
#define LG_LOCK_MODE "Atomic"
typedef volatile int LG_Lock;
#define LG_LOCK_INIT(x) (x) = 0
#define LG_LOCK(x) while(__sync_lock_test_and_set(&(x), 1)) {nsleep(100);}
#define LG_UNLOCK(x) __sync_lock_release(&x)
#define LG_LOCK_FREE(x)

View File

@@ -25,9 +25,10 @@ Place, Suite 330, Boston, MA 02111-1307 USA
#include <windows.h>
#else
#include <time.h>
#include <stdint.h>
#endif
static inline uint64_t getMicrotime()
static inline uint64_t microtime()
{
#if defined(_WIN32)
static LARGE_INTEGER freq = { 0 };
@@ -42,4 +43,24 @@ static inline uint64_t getMicrotime()
clock_gettime(CLOCK_MONOTONIC, &time);
return (uint64_t)time.tv_sec * 1000000LL + time.tv_nsec / 1000LL;
#endif
}
}
#if !defined(_WIN32)
//FIXME: make win32 versions
static inline uint64_t nanotime()
{
struct timespec time;
clock_gettime(CLOCK_MONOTONIC_RAW, &time);
return ((uint64_t)time.tv_sec * 1e9) + time.tv_nsec;
}
static inline void nsleep(uint64_t ns)
{
const struct timespec ts =
{
.tv_sec = ns / 1e9,
.tv_nsec = ns - ((ns / 1e9) * 1e9)
};
nanosleep(&ts, NULL);
}
#endif

View File

@@ -116,11 +116,11 @@ bool lgWaitEvent(LGEvent * event, unsigned int timeout)
}
}
uint64_t now = getMicrotime();
uint64_t now = microtime();
uint64_t end = now + spinTime * 1000;
while(!event->signaled)
{
now = getMicrotime();
now = microtime();
if (now >= end)
break;
}
@@ -218,4 +218,4 @@ bool lgResetEvent(LGEvent * event)
{
event->signaled = false;
return ResetEvent(event->handle);
}
}