From f28d12a72998b0a6018b41af8f86a24b10ab506f Mon Sep 17 00:00:00 2001
From: Paul Cercueil <paul@crapouillou.net>
Date: Sat, 11 Feb 2023 14:22:21 +0000
Subject: [PATCH] Support compiling without pthreads

Add NO_PTHREAD option in the Makefile, which can be used on platforms
that don't support pthreads.

Since dfsound/spu.c disables threading support on Win32 and platform
defining NO_OS (Switch, 3DS, PSP Vita), mark them as not having
pthreads.

Signed-off-by: Paul Cercueil <paul@crapouillou.net>
---
 Makefile                         |  3 ++-
 frontend/libretro.c              | 42 ++++++++++++++++----------------
 frontend/libretro_core_options.h |  4 +--
 libpcsxcore/cdriso.c             |  3 +--
 plugins/dfsound/spu.c            | 11 +++------
 5 files changed, 30 insertions(+), 33 deletions(-)

diff --git a/Makefile b/Makefile
index 7b7ce5ce..6e9d10ca 100644
--- a/Makefile
+++ b/Makefile
@@ -6,7 +6,8 @@ CFLAGS += -Wall -ggdb -Iinclude -ffast-math
 ifndef DEBUG
 CFLAGS += -O2 -DNDEBUG
 endif
-CFLAGS += -DHAVE_MMAP=$(if $(NO_MMAP),0,1)
+CFLAGS += -DHAVE_MMAP=$(if $(NO_MMAP),0,1) \
+	  -DHAVE_PTHREAD=$(if $(NO_PTHREAD),0,1)
 CXXFLAGS += $(CFLAGS)
 #DRC_DBG = 1
 #PCNT = 1
diff --git a/frontend/libretro.c b/frontend/libretro.c
index 32d0bec3..e5b21d52 100644
--- a/frontend/libretro.c
+++ b/frontend/libretro.c
@@ -2098,28 +2098,28 @@ static void update_variables(bool in_flight)
          spu_config.iUseThread = 0;
    }
 
-#ifndef _WIN32
-   var.value = NULL;
-   var.key = "pcsx_rearmed_async_cd";
-   if (environ_cb(RETRO_ENVIRONMENT_GET_VARIABLE, &var) && var.value)
-   {
-      if (strcmp(var.value, "async") == 0)
-      {
-         Config.AsyncCD = 1;
-         Config.CHD_Precache = 0;
-      }
-      else if (strcmp(var.value, "sync") == 0)
-      {
-         Config.AsyncCD = 0;
-         Config.CHD_Precache = 0;
-      }
-      else if (strcmp(var.value, "precache") == 0)
-      {
-         Config.AsyncCD = 0;
-         Config.CHD_Precache = 1;
-      }
+   if (HAVE_PTHREAD) {
+	   var.value = NULL;
+	   var.key = "pcsx_rearmed_async_cd";
+	   if (environ_cb(RETRO_ENVIRONMENT_GET_VARIABLE, &var) && var.value)
+	   {
+		  if (strcmp(var.value, "async") == 0)
+		  {
+			 Config.AsyncCD = 1;
+			 Config.CHD_Precache = 0;
+		  }
+		  else if (strcmp(var.value, "sync") == 0)
+		  {
+			 Config.AsyncCD = 0;
+			 Config.CHD_Precache = 0;
+		  }
+		  else if (strcmp(var.value, "precache") == 0)
+		  {
+			 Config.AsyncCD = 0;
+			 Config.CHD_Precache = 1;
+		  }
+       }
    }
-#endif
 
    var.value = NULL;
    var.key = "pcsx_rearmed_noxadecoding";
diff --git a/frontend/libretro_core_options.h b/frontend/libretro_core_options.h
index 781c514b..b9eaf77b 100644
--- a/frontend/libretro_core_options.h
+++ b/frontend/libretro_core_options.h
@@ -750,7 +750,7 @@ struct retro_core_option_v2_definition option_defs_us[] = {
       },
       "enabled",
    },
-#if !defined(THREAD_ENABLED) && !defined(_WIN32) && !defined(NO_OS)
+#if HAVE_PTHREAD
    {
       "pcsx_rearmed_spu_thread",
       "Threaded SPU",
@@ -765,7 +765,7 @@ struct retro_core_option_v2_definition option_defs_us[] = {
       },
       "disabled",
    },
-#endif // THREAD_ENABLED
+#endif // HAVE_PTHREAD
    {
       "pcsx_rearmed_show_input_settings",
       "Show Input Settings",
diff --git a/libpcsxcore/cdriso.c b/libpcsxcore/cdriso.c
index f47fcfdd..d0440e20 100644
--- a/libpcsxcore/cdriso.c
+++ b/libpcsxcore/cdriso.c
@@ -30,8 +30,7 @@
 #include <process.h>
 #include <windows.h>
 #define strcasecmp _stricmp
-#define usleep(x) Sleep((x) / 1000)
-#else
+#elif HAVE_PTHREAD
 #include <pthread.h>
 #include <sys/time.h>
 #include <unistd.h>
diff --git a/plugins/dfsound/spu.c b/plugins/dfsound/spu.c
index ebebd2a2..f5e8de50 100644
--- a/plugins/dfsound/spu.c
+++ b/plugins/dfsound/spu.c
@@ -18,9 +18,6 @@
  *                                                                         *
  ***************************************************************************/
 
-#if !defined(THREAD_ENABLED) && !defined(_WIN32) && !defined(NO_OS)
-#define THREAD_ENABLED 1
-#endif
 #include "stdafx.h"
 
 #define _IN_SPU
@@ -832,7 +829,7 @@ static void do_samples_finish(int *SSumLR, int ns_to,
 
 // optional worker thread handling
 
-#if defined(THREAD_ENABLED) || defined(WANT_THREAD_CODE)
+#if HAVE_PTHREAD || defined(WANT_THREAD_CODE)
 
 // worker thread state
 static struct spu_worker {
@@ -1087,7 +1084,7 @@ static void sync_worker_thread(int force) {}
 
 static const void * const worker = NULL;
 
-#endif // THREAD_ENABLED
+#endif // HAVE_PTHREAD || defined(WANT_THREAD_CODE)
 
 ////////////////////////////////////////////////////////////////////////
 // MAIN SPU FUNCTION
@@ -1368,7 +1365,7 @@ static void RemoveStreams(void)
 /* special code for TI C64x DSP */
 #include "spu_c64x.c"
 
-#elif defined(THREAD_ENABLED)
+#elif HAVE_PTHREAD
 
 #include <pthread.h>
 #include <semaphore.h>
@@ -1467,7 +1464,7 @@ static void exit_spu_thread(void)
  worker = NULL;
 }
 
-#else // if !THREAD_ENABLED
+#else // if !HAVE_PTHREAD
 
 static void init_spu_thread(void)
 {
-- 
2.39.5