Skip to content

Instantly share code, notes, and snippets.

@p-i-
Created October 20, 2024 10:13
Show Gist options
  • Save p-i-/2293988758d466bb61b41f036983b45e to your computer and use it in GitHub Desktop.
Save p-i-/2293988758d466bb61b41f036983b45e to your computer and use it in GitHub Desktop.
clither build on macos
diff --git a/CMakeLists.txt b/CMakeLists.txt
index 8ed9bc5..ce7842f 100644
--- a/CMakeLists.txt
+++ b/CMakeLists.txt
@@ -14,7 +14,7 @@ project ("clither"
###############################################################################
include (CMakeDependentOption)
-option (CLITHER_BENCHMARKS "Compile benchmarks (requires C++)" ON)
+option (CLITHER_BENCHMARKS "Compile benchmarks (requires C++)" OFF)
option (CLITHER_DOC "Compile LaTeX documents" ON)
option (CLITHER_GFX "Set to OFF if you only want to build a server (headless) application.")
cmake_dependent_option (CLITHER_GFX_SDL "Enable SDL graphics backend" ON "CLITHER_GFX" OFF)
diff --git a/src/thread_pthread.c b/src/thread_pthread.c
index 7aad8b4..834fece 100644
--- a/src/thread_pthread.c
+++ b/src/thread_pthread.c
@@ -44,9 +44,17 @@ thread_join(struct thread t, int timeout_ms)
}
ts.tv_sec += off.tv_sec;
+#ifdef __APPLE__
+ // On macOS, use a simple pthread_join without timeout.
+ if (pthread_join((pthread_t)t.handle, &ret) == 0)
+ return 0;
+ return -1;
+#else
+ // On Linux, use pthread_timedjoin_np if available.
if (pthread_timedjoin_np((pthread_t)t.handle, &ret, &ts) == 0)
return 0;
return -1;
+#endif
}
/* ------------------------------------------------------------------------- */
diff --git a/src/tick_posix.c b/src/tick_posix.c
index 589ded5..da97a4b 100644
--- a/src/tick_posix.c
+++ b/src/tick_posix.c
@@ -26,7 +26,7 @@ tick_advance(struct tick* t)
if (now > wait_until)
{
int ticks_behind = (now - wait_until) / t->interval;
- if (ticks_behind > 0)
+ if (ticks_behind > 0)
t->last = wait_until;
return ticks_behind;
}
@@ -51,12 +51,28 @@ tick_wait(struct tick* t)
return (now - wait_until) / t->interval;
}
- /* Context switch on linux takes about ~1us */
+ /* Context switch on Linux takes about ~1us */
if (wait_until - now > 2000)
{
ts.tv_sec = wait_until / 1e9;
ts.tv_nsec = wait_until - ts.tv_sec * 1e9;
+
+#ifdef __APPLE__
+ // Use nanosleep with a calculated relative time.
+ struct timespec rel_ts;
+ clock_gettime(CLOCK_REALTIME, &rel_ts);
+ rel_ts.tv_sec = ts.tv_sec - rel_ts.tv_sec;
+ rel_ts.tv_nsec = ts.tv_nsec - rel_ts.tv_nsec;
+ if (rel_ts.tv_nsec < 0)
+ {
+ rel_ts.tv_nsec += 1e9;
+ rel_ts.tv_sec -= 1;
+ }
+ if (nanosleep(&rel_ts, NULL) == 0)
+#else
+ // Use clock_nanosleep with TIMER_ABSTIME if available.
if (clock_nanosleep(CLOCK_MONOTONIC, TIMER_ABSTIME, &ts, NULL) == 0)
+#endif
{
t->last = wait_until;
return 0;
@@ -95,12 +111,26 @@ tick_wait_warp(struct tick* t, int warp, int tps)
return (now - wait_until) / t->interval;
}
- /* Context switch on linux takes about ~1us */
+ /* Context switch on Linux takes about ~1us */
if (wait_until - now > 2000)
{
ts.tv_sec = wait_until / 1e9;
ts.tv_nsec = wait_until - ts.tv_sec * 1e9;
+
+#ifdef __APPLE__
+ struct timespec rel_ts;
+ clock_gettime(CLOCK_REALTIME, &rel_ts);
+ rel_ts.tv_sec = ts.tv_sec - rel_ts.tv_sec;
+ rel_ts.tv_nsec = ts.tv_nsec - rel_ts.tv_nsec;
+ if (rel_ts.tv_nsec < 0)
+ {
+ rel_ts.tv_nsec += 1e9;
+ rel_ts.tv_sec -= 1;
+ }
+ if (nanosleep(&rel_ts, NULL) == 0)
+#else
if (clock_nanosleep(CLOCK_MONOTONIC, TIMER_ABSTIME, &ts, NULL) == 0)
+#endif
{
t->last = wait_until;
return 0;
@p-i-
Copy link
Author

p-i- commented Oct 20, 2024

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment