cdrom: Support using C11 threads instead of libretro ones
authorPaul Cercueil <paul@crapouillou.net>
Mon, 14 Apr 2025 21:47:35 +0000 (23:47 +0200)
committernotaz <notasas@gmail.com>
Tue, 22 Apr 2025 22:07:51 +0000 (01:07 +0300)
Libretro threads only work on... libretro.
Add support for C11 threads for the non-libretro platforms that still
want the async CD-ROM code.

Signed-off-by: Paul Cercueil <paul@crapouillou.net>
libpcsxcore/cdrom-async.c

index 7658096..7192552 100644 (file)
@@ -41,7 +41,53 @@ static int  rcdrom_isMediaInserted(void *stream) { return 0; }
 
 #endif
 
+#ifdef USE_C11_THREADS
+#include <threads.h>
+
+static int c11_threads_cb_wrapper(void *cb)
+{
+   ((void (*)(void *))cb)(NULL);
+
+   return 0;
+}
+
+#define slock_new() ({ \
+        mtx_t *lock = malloc(sizeof(*lock)); \
+        if (lock) mtx_init(lock, mtx_plain); \
+        lock; \
+})
+
+#define scond_new() ({ \
+        cnd_t *cnd = malloc(sizeof(*cnd)); \
+        if (cnd) cnd_init(cnd); \
+        cnd; \
+})
+
+#define pcsxr_sthread_create(cb, unused) ({ \
+        thrd_t *thd = malloc(sizeof(*thd)); \
+        if (thd) \
+                thrd_create(thd, c11_threads_cb_wrapper, cb); \
+        thd; \
+})
+
+#define sthread_join(thrd) ({ \
+        thrd_join(*thrd, NULL); \
+        free(thrd); \
+})
+
+#define slock_free(lock) free(lock)
+#define slock_lock(lock) mtx_lock(lock)
+#define slock_unlock(lock) mtx_unlock(lock)
+#define scond_free(cond) free(cond)
+#define scond_wait(cond, lock) cnd_wait(cond, lock)
+#define scond_signal(cond) cnd_signal(cond)
+#define slock_t mtx_t
+#define scond_t cnd_t
+#define sthread_t thrd_t
+#else
 #include "../frontend/libretro-rthreads.h"
+#endif
+
 #include "retro_timers.h"
 
 struct cached_buf {