mirror of
https://github.com/gnif/LookingGlass.git
synced 2025-05-17 20:11:17 +00:00
[all] moved time and locking methods to the common library
This commit is contained in:
parent
278d851c7c
commit
4746c89227
@ -19,81 +19,9 @@ Place, Suite 330, Boston, MA 02111-1307 USA
|
|||||||
|
|
||||||
#pragma once
|
#pragma once
|
||||||
|
|
||||||
#include <time.h>
|
#include <stdlib.h>
|
||||||
#include <stdint.h>
|
|
||||||
#include <stdbool.h>
|
#include <stdbool.h>
|
||||||
|
|
||||||
static inline uint64_t microtime()
|
|
||||||
{
|
|
||||||
struct timespec time;
|
|
||||||
clock_gettime(CLOCK_MONOTONIC_RAW, &time);
|
|
||||||
return ((uint64_t)time.tv_sec * 1000000) + (time.tv_nsec / 1000);
|
|
||||||
}
|
|
||||||
|
|
||||||
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);
|
|
||||||
}
|
|
||||||
|
|
||||||
#ifdef ATOMIC_LOCKING
|
|
||||||
#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)
|
|
||||||
#else
|
|
||||||
#include <SDL2/SDL.h>
|
|
||||||
#define LG_LOCK_MODE "Mutex"
|
|
||||||
typedef SDL_mutex * LG_Lock;
|
|
||||||
#define LG_LOCK_INIT(x) (x = SDL_CreateMutex())
|
|
||||||
#define LG_LOCK(x) SDL_LockMutex(x)
|
|
||||||
#define LG_UNLOCK(x) SDL_UnlockMutex(x)
|
|
||||||
#define LG_LOCK_FREE(x) SDL_DestroyMutex(x)
|
|
||||||
#endif
|
|
||||||
|
|
||||||
static inline uint32_t get_bit(const uint8_t * const base, size_t * const offset)
|
|
||||||
{
|
|
||||||
uint32_t out = ((*(base + (*offset >> 0x3))) >> (0x7 - (*offset & 0x7))) & 0x1;
|
|
||||||
++*offset;
|
|
||||||
return out;
|
|
||||||
}
|
|
||||||
|
|
||||||
static inline uint32_t get_bits(const uint8_t * const base, size_t * const offset, const uint8_t bits)
|
|
||||||
{
|
|
||||||
uint32_t value = 0;
|
|
||||||
for (int i = 0; i < bits; ++i)
|
|
||||||
value |= (get_bit(base, offset) ? 1 : 0) << (bits - i - 1);
|
|
||||||
return value;
|
|
||||||
}
|
|
||||||
|
|
||||||
static inline uint32_t decode_u_golomb(const uint8_t * const base, size_t * const offset)
|
|
||||||
{
|
|
||||||
uint32_t i = 0;
|
|
||||||
while(get_bit(base, offset) == 0)
|
|
||||||
++i;
|
|
||||||
|
|
||||||
return ((1 << i) - 1 + get_bits(base, offset, i));
|
|
||||||
}
|
|
||||||
|
|
||||||
static inline int32_t decode_s_golomb(const uint8_t * const base, size_t * const offset)
|
|
||||||
{
|
|
||||||
const uint32_t g = decode_u_golomb(base, offset);
|
|
||||||
return (g & 0x1) ? (g + 1) / 2 : -(g / 2);
|
|
||||||
}
|
|
||||||
|
|
||||||
// reads the specified file into a new buffer
|
// reads the specified file into a new buffer
|
||||||
// the callee must free the buffer
|
// the callee must free the buffer
|
||||||
bool file_get_contents(const char * filename, char ** buffer, size_t * length);
|
bool file_get_contents(const char * filename, char ** buffer, size_t * length);
|
||||||
|
@ -19,7 +19,7 @@ Place, Suite 330, Boston, MA 02111-1307 USA
|
|||||||
|
|
||||||
#include "alert.h"
|
#include "alert.h"
|
||||||
#include "common/debug.h"
|
#include "common/debug.h"
|
||||||
#include "utils.h"
|
#include "common/locking.h"
|
||||||
|
|
||||||
#include "texture.h"
|
#include "texture.h"
|
||||||
#include "shader.h"
|
#include "shader.h"
|
||||||
@ -215,4 +215,4 @@ void egl_alert_render(EGL_Alert * alert, const float scaleX, const float scaleY)
|
|||||||
egl_model_render(alert->model);
|
egl_model_render(alert->model);
|
||||||
|
|
||||||
glDisable(GL_BLEND);
|
glDisable(GL_BLEND);
|
||||||
}
|
}
|
||||||
|
@ -19,7 +19,7 @@ Place, Suite 330, Boston, MA 02111-1307 USA
|
|||||||
|
|
||||||
#include "cursor.h"
|
#include "cursor.h"
|
||||||
#include "common/debug.h"
|
#include "common/debug.h"
|
||||||
#include "utils.h"
|
#include "common/locking.h"
|
||||||
|
|
||||||
#include "texture.h"
|
#include "texture.h"
|
||||||
#include "shader.h"
|
#include "shader.h"
|
||||||
@ -286,4 +286,4 @@ void egl_cursor_render(EGL_Cursor * cursor)
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
glDisable(GL_BLEND);
|
glDisable(GL_BLEND);
|
||||||
}
|
}
|
||||||
|
@ -20,7 +20,7 @@ Place, Suite 330, Boston, MA 02111-1307 USA
|
|||||||
#include "desktop.h"
|
#include "desktop.h"
|
||||||
#include "common/debug.h"
|
#include "common/debug.h"
|
||||||
#include "common/option.h"
|
#include "common/option.h"
|
||||||
#include "utils.h"
|
#include "common/locking.h"
|
||||||
|
|
||||||
#include "texture.h"
|
#include "texture.h"
|
||||||
#include "shader.h"
|
#include "shader.h"
|
||||||
@ -281,4 +281,4 @@ bool egl_desktop_render(EGL_Desktop * desktop, const float x, const float y, con
|
|||||||
|
|
||||||
egl_model_render(desktop->model);
|
egl_model_render(desktop->model);
|
||||||
return true;
|
return true;
|
||||||
}
|
}
|
||||||
|
@ -22,6 +22,7 @@ Place, Suite 330, Boston, MA 02111-1307 USA
|
|||||||
#include "common/debug.h"
|
#include "common/debug.h"
|
||||||
#include "common/option.h"
|
#include "common/option.h"
|
||||||
#include "common/sysinfo.h"
|
#include "common/sysinfo.h"
|
||||||
|
#include "common/time.h"
|
||||||
#include "utils.h"
|
#include "utils.h"
|
||||||
#include "dynamic/fonts.h"
|
#include "dynamic/fonts.h"
|
||||||
|
|
||||||
|
@ -33,7 +33,7 @@ Place, Suite 330, Boston, MA 02111-1307 USA
|
|||||||
#include "common/debug.h"
|
#include "common/debug.h"
|
||||||
#include "common/option.h"
|
#include "common/option.h"
|
||||||
#include "common/framebuffer.h"
|
#include "common/framebuffer.h"
|
||||||
#include "utils.h"
|
#include "common/locking.h"
|
||||||
#include "dynamic/fonts.h"
|
#include "dynamic/fonts.h"
|
||||||
#include "ll.h"
|
#include "ll.h"
|
||||||
|
|
||||||
@ -1344,4 +1344,4 @@ static void draw_mouse(struct Inst * this)
|
|||||||
glTranslatef(this->mousePos.x, this->mousePos.y, 0.0f);
|
glTranslatef(this->mousePos.x, this->mousePos.y, 0.0f);
|
||||||
glCallList(this->mouseList);
|
glCallList(this->mouseList);
|
||||||
glPopMatrix();
|
glPopMatrix();
|
||||||
}
|
}
|
||||||
|
@ -18,8 +18,8 @@ Place, Suite 330, Boston, MA 02111-1307 USA
|
|||||||
*/
|
*/
|
||||||
|
|
||||||
#include "spice/spice.h"
|
#include "spice/spice.h"
|
||||||
#include "utils.h"
|
|
||||||
#include "common/debug.h"
|
#include "common/debug.h"
|
||||||
|
#include "common/locking.h"
|
||||||
|
|
||||||
#include <string.h>
|
#include <string.h>
|
||||||
#include <unistd.h>
|
#include <unistd.h>
|
||||||
@ -1665,4 +1665,4 @@ bool spice_clipboard_data(SpiceDataType type, uint8_t * data, size_t size)
|
|||||||
|
|
||||||
free(buffer);
|
free(buffer);
|
||||||
return true;
|
return true;
|
||||||
}
|
}
|
||||||
|
@ -19,7 +19,7 @@ Place, Suite 330, Boston, MA 02111-1307 USA
|
|||||||
|
|
||||||
#include "ll.h"
|
#include "ll.h"
|
||||||
|
|
||||||
#include "utils.h"
|
#include "common/locking.h"
|
||||||
#include <stdlib.h>
|
#include <stdlib.h>
|
||||||
#include <assert.h>
|
#include <assert.h>
|
||||||
|
|
||||||
@ -157,4 +157,4 @@ bool ll_walk(struct ll * list, void ** data)
|
|||||||
LG_UNLOCK(list->lock);
|
LG_UNLOCK(list->lock);
|
||||||
|
|
||||||
return true;
|
return true;
|
||||||
}
|
}
|
||||||
|
@ -45,6 +45,7 @@ Place, Suite 330, Boston, MA 02111-1307 USA
|
|||||||
#include "common/KVMFR.h"
|
#include "common/KVMFR.h"
|
||||||
#include "common/stringutils.h"
|
#include "common/stringutils.h"
|
||||||
#include "common/thread.h"
|
#include "common/thread.h"
|
||||||
|
#include "common/locking.h"
|
||||||
#include "common/event.h"
|
#include "common/event.h"
|
||||||
#include "common/ivshmem.h"
|
#include "common/ivshmem.h"
|
||||||
#include "common/time.h"
|
#include "common/time.h"
|
||||||
@ -112,7 +113,7 @@ static void updatePositionInfo()
|
|||||||
|
|
||||||
if (force && params.forceAspect)
|
if (force && params.forceAspect)
|
||||||
{
|
{
|
||||||
state.resizeTimeout = getMicrotime() + RESIZE_TIMEOUT;
|
state.resizeTimeout = microtime() + RESIZE_TIMEOUT;
|
||||||
state.resizeDone = false;
|
state.resizeDone = false;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@ -219,7 +220,7 @@ static int renderThread(void * unused)
|
|||||||
|
|
||||||
clock_nanosleep(CLOCK_MONOTONIC, TIMER_ABSTIME, &time, NULL);
|
clock_nanosleep(CLOCK_MONOTONIC, TIMER_ABSTIME, &time, NULL);
|
||||||
|
|
||||||
if (!state.resizeDone && state.resizeTimeout < getMicrotime())
|
if (!state.resizeDone && state.resizeTimeout < microtime())
|
||||||
{
|
{
|
||||||
SDL_SetWindowSize(
|
SDL_SetWindowSize(
|
||||||
state.window,
|
state.window,
|
||||||
@ -1574,4 +1575,4 @@ int main(int argc, char * argv[])
|
|||||||
config_free();
|
config_free();
|
||||||
return ret;
|
return ret;
|
||||||
|
|
||||||
}
|
}
|
||||||
|
@ -18,6 +18,8 @@ Place, Suite 330, Boston, MA 02111-1307 USA
|
|||||||
*/
|
*/
|
||||||
#pragma once
|
#pragma once
|
||||||
|
|
||||||
|
#include "time.h"
|
||||||
|
|
||||||
#define INTERLOCKED_AND8 __sync_fetch_and_and
|
#define INTERLOCKED_AND8 __sync_fetch_and_and
|
||||||
#define INTERLOCKED_OR8 __sync_fetch_and_or
|
#define INTERLOCKED_OR8 __sync_fetch_and_or
|
||||||
#define INTERLOCKED_INC(x) __sync_fetch_and_add((x), 1)
|
#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)); \
|
while(__sync_lock_test_and_set(&(lock), 1)) while((lock)); \
|
||||||
x\
|
x\
|
||||||
__sync_lock_release(&(lock));
|
__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)
|
||||||
|
@ -25,9 +25,10 @@ Place, Suite 330, Boston, MA 02111-1307 USA
|
|||||||
#include <windows.h>
|
#include <windows.h>
|
||||||
#else
|
#else
|
||||||
#include <time.h>
|
#include <time.h>
|
||||||
|
#include <stdint.h>
|
||||||
#endif
|
#endif
|
||||||
|
|
||||||
static inline uint64_t getMicrotime()
|
static inline uint64_t microtime()
|
||||||
{
|
{
|
||||||
#if defined(_WIN32)
|
#if defined(_WIN32)
|
||||||
static LARGE_INTEGER freq = { 0 };
|
static LARGE_INTEGER freq = { 0 };
|
||||||
@ -42,4 +43,24 @@ static inline uint64_t getMicrotime()
|
|||||||
clock_gettime(CLOCK_MONOTONIC, &time);
|
clock_gettime(CLOCK_MONOTONIC, &time);
|
||||||
return (uint64_t)time.tv_sec * 1000000LL + time.tv_nsec / 1000LL;
|
return (uint64_t)time.tv_sec * 1000000LL + time.tv_nsec / 1000LL;
|
||||||
#endif
|
#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
|
||||||
|
@ -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;
|
uint64_t end = now + spinTime * 1000;
|
||||||
while(!event->signaled)
|
while(!event->signaled)
|
||||||
{
|
{
|
||||||
now = getMicrotime();
|
now = microtime();
|
||||||
if (now >= end)
|
if (now >= end)
|
||||||
break;
|
break;
|
||||||
}
|
}
|
||||||
@ -218,4 +218,4 @@ bool lgResetEvent(LGEvent * event)
|
|||||||
{
|
{
|
||||||
event->signaled = false;
|
event->signaled = false;
|
||||||
return ResetEvent(event->handle);
|
return ResetEvent(event->handle);
|
||||||
}
|
}
|
||||||
|
Loading…
x
Reference in New Issue
Block a user