[common/client] platform: make timing waits portable

This commit is contained in:
Geoffrey McRae
2026-09-12 08:13:16 +10:00
parent d045ac6ba7
commit 9c8343df96
18 changed files with 351 additions and 161 deletions

View File

@@ -27,6 +27,7 @@
#include "common/debug.h"
#include "common/time.h"
#include "common/util.h"
#define PULSEAUDIO_ERROR_REPORT_INTERVAL_NS INT64_C(1000000000)
#define PULSEAUDIO_OPERATION_TIMEOUT_US UINT64_C(250000)

View File

@@ -22,8 +22,9 @@
#include "wayland.h"
#include <stdbool.h>
#include <string.h>
#include <errno.h>
#include <string.h>
#include <time.h>
#include <wayland-client.h>
@@ -68,11 +69,26 @@ static bool presentationOrdered(const struct timespec * left,
(left->tv_sec == right->tv_sec && left->tv_nsec >= right->tv_nsec);
}
static struct timespec presentationTimespecDiff(
const struct timespec * left, const struct timespec * right)
{
struct timespec result =
{
.tv_sec = left->tv_sec - right->tv_sec,
.tv_nsec = left->tv_nsec - right->tv_nsec,
};
if (result.tv_nsec < 0)
{
--result.tv_sec;
result.tv_nsec += 1000000000;
}
return result;
}
static uint64_t presentationDelta(const struct timespec * left,
const struct timespec * right)
{
struct timespec delta;
tsDiff(&delta, left, right);
const struct timespec delta = presentationTimespecDiff(left, right);
return (uint64_t)delta.tv_sec * 1000000000ULL + delta.tv_nsec;
}
@@ -132,8 +148,8 @@ static void presentationFeedbackPresented(void * opaque,
if (tvNsec < 1000000000)
{
struct timespec delta;
tsDiff(&delta, &present, &frame->sent);
const struct timespec delta =
presentationTimespecDiff(&present, &frame->sent);
ringbuffer_push(wlWm.photonTimings,
&(float){ delta.tv_sec * 1e3f + delta.tv_nsec * 1e-6f });
}

View File

@@ -30,6 +30,7 @@
#include "resources/no-input-cursor/32.xcur.h"
#include <string.h>
#include <time.h>
#include <unistd.h>
#include <sys/epoll.h>
#include <errno.h>

View File

@@ -893,10 +893,7 @@ static int cursorRepaintThread(void * unused)
if (!waitTime)
break;
struct timespec deadlineTime;
clock_gettime(CLOCK_MONOTONIC, &deadlineTime);
tsAdd(&deadlineTime, waitTime);
lgWaitEventAbs(e_cursorRepaint, &deadlineTime);
lgWaitEventDeadline(e_cursorRepaint, deadline);
}
}
@@ -941,8 +938,7 @@ static int renderThread(void * unused)
/* signal to other threads that the renderer is ready */
lgSignalEvent(e_startup);
struct timespec time;
clock_gettime(CLOCK_MONOTONIC, &time);
uint64_t renderDeadline = nanotime();
while(likely(app_getState() != APP_STATE_SHUTDOWN))
{
@@ -994,7 +990,7 @@ static int renderThread(void * unused)
else if (g_params.fpsMin != 0)
{
app_handleRenderEvent(microtime());
lgWaitEventAbs(g_state.frameEvent, &time);
lgWaitEventDeadline(g_state.frameEvent, renderDeadline);
}
bool cursorWake;
@@ -1112,8 +1108,7 @@ static int renderThread(void * unused)
if (!g_state.jitRender && g_params.fpsMin != 0)
{
/* A frame-driven swap also satisfies the minimum render rate. */
clock_gettime(CLOCK_MONOTONIC, &time);
tsAdd(&time, app_overlayNeedsRender() ?
renderDeadline = nanotime() + (app_overlayNeedsRender() ?
g_state.overlayFrameTime : g_state.frameTime);
}

View File

@@ -29,6 +29,7 @@
#include "common/debug.h"
#include "common/stringutils.h"
#include "common/time.h"
#include "common/util.h"
#include "overlay_utils.h"
#include <math.h>

View File

@@ -28,6 +28,7 @@
#include "common/debug.h"
#include "common/locking.h"
#include "common/util.h"
#include "main.h"
typedef struct RenderCommand

View File

@@ -24,6 +24,7 @@
#include "common/event.h"
#include "common/ringbuffer.h"
#include "common/time.h"
#include "common/util.h"
#include <usbredirparser.h>

View File

@@ -1,5 +1,19 @@
cmake_minimum_required(VERSION 3.10)
add_executable(common-platform-tests
common_platform_test.c
)
target_link_libraries(common-platform-tests
${EXE_FLAGS}
lg_common
)
add_test(NAME common-platform-tests
COMMAND common-platform-tests
)
set_tests_properties(common-platform-tests PROPERTIES
TIMEOUT 10
)
add_executable(font-tests
font_test.c
../src/font.c

View File

@@ -0,0 +1,117 @@
/**
* Looking Glass
* Copyright © 2017-2026 The Looking Glass Authors
* https://looking-glass.io
*
* This program is free software; you can redistribute it and/or modify it
* under the terms of the GNU General Public License as published by the Free
* Software Foundation; either version 2 of the License, or (at your option)
* any later version.
*
* This program is distributed in the hope that it will be useful, but WITHOUT
* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for
* more details.
*
* You should have received a copy of the GNU General Public License along
* with this program; if not, write to the Free Software Foundation, Inc., 59
* Temple Place, Suite 330, Boston, MA 02111-1307 USA
*/
#include "test.h"
#include "common/event.h"
#include "common/thread.h"
#include "common/time.h"
#include <stdatomic.h>
#include <stdint.h>
#define TEST_WAIT_NS UINT64_C(1000000000)
struct SignalState
{
LGEvent * event;
};
struct TimerState
{
LGEvent * event;
atomic_uint calls;
};
static int signalThread(void * opaque)
{
const struct SignalState * state = opaque;
nsleep(UINT64_C(2000000));
CHECK(lgSignalEvent(state->event));
return 0;
}
static bool timerCallback(void * opaque)
{
struct TimerState * state = opaque;
const unsigned int calls = atomic_fetch_add_explicit(
&state->calls, 1, memory_order_relaxed) + 1;
if (calls < 2)
return true;
CHECK(lgSignalEvent(state->event));
return false;
}
static void testEventWaits(void)
{
LGEvent * event = lgCreateEvent(true, 0);
CHECK(event);
const uint64_t timeoutStart = nanotime();
CHECK(!lgWaitEventNS(event, UINT64_C(1000000)));
CHECK(nanotime() >= timeoutStart);
const struct SignalState state = { .event = event };
LGThread * thread;
CHECK(lgCreateThread("eventSignal", signalThread, (void *)&state, &thread));
CHECK(lgWaitEventDeadline(event, nanotime() + TEST_WAIT_NS));
CHECK(lgJoinThread(thread, NULL));
CHECK(!lgWaitEvent(event, 0));
lgFreeEvent(event);
}
static void testManualReset(void)
{
LGEvent * event = lgCreateEvent(false, 0);
CHECK(event);
CHECK(lgSignalEvent(event));
CHECK(lgWaitEvent(event, 0));
CHECK(lgWaitEvent(event, 0));
CHECK(lgResetEvent(event));
CHECK(!lgWaitEvent(event, 0));
lgFreeEvent(event);
}
static void testTimer(void)
{
LGEvent * event = lgCreateEvent(true, 0);
CHECK(event);
struct TimerState state = { .event = event };
atomic_init(&state.calls, 0);
LGTimer * timer;
CHECK(lgCreateTimer(1, timerCallback, &state, &timer));
CHECK(lgWaitEventNS(event, TEST_WAIT_NS));
lgTimerDestroy(timer);
CHECK(atomic_load_explicit(&state.calls, memory_order_relaxed) == 2);
lgFreeEvent(event);
}
int main(void)
{
lgTimeInit();
testEventWaits();
testManualReset();
testTimer();
return 0;
}

View File

@@ -22,6 +22,7 @@
#include "common/debug.h"
#include "common/time.h"
#include "common/util.h"
#include <purespice.h>