From a221e3e4cd8b5f252a7349cd68489de6436b56e2 Mon Sep 17 00:00:00 2001
From: Themaister <maister@archlinux.us>
Date: Wed, 30 Nov 2011 17:46:58 +0100
Subject: [PATCH] Enable/disable threading support.

---
 Makefile            |  9 +++++++--
 Makefile.win        |  8 +++++++-
 config.features.h   |  6 ++++++
 netplay.c           |  4 ++++
 qb/config.libs.sh   | 40 +++++++++++++++++++++++-----------------
 qb/config.params.sh |  1 +
 ssnes.c             |  9 +++++++++
 7 files changed, 57 insertions(+), 20 deletions(-)

diff --git a/Makefile b/Makefile
index 01dfd10ac6..e45dbaa2bd 100644
--- a/Makefile
+++ b/Makefile
@@ -2,11 +2,11 @@ include config.mk
 
 TARGET = ssnes tools/ssnes-joyconfig
 
-OBJ = ssnes.o file.o driver.o settings.o dynamic.o message.o rewind.o movie.o autosave.o gfx/gfx_common.o ups.o bps.o strl.o getopt.o screenshot.o thread.o audio/hermite.o
+OBJ = ssnes.o file.o driver.o settings.o dynamic.o message.o rewind.o movie.o gfx/gfx_common.o ups.o bps.o strl.o getopt.o screenshot.o thread.o audio/hermite.o
 JOYCONFIG_OBJ = tools/ssnes-joyconfig.o conf/config_file.o strl.o
 HEADERS = $(wildcard */*.h) $(wildcard *.h)
 
-LIBS = -lm -lpthread
+LIBS = -lm
 DEFINES = -DHAVE_CONFIG_H
 
 ifneq ($(findstring Darwin,$(OS)),)
@@ -27,6 +27,11 @@ ifneq ($(findstring Linux,$(OS)),)
    LIBS += -lrt
 endif
 
+ifeq ($(HAVE_THREADS), 1)
+   OBJ += autosave.o
+   LIBS += -lpthread
+endif
+
 ifeq ($(HAVE_CONFIGFILE), 1)
    OBJ += conf/config_file.o
 endif
diff --git a/Makefile.win b/Makefile.win
index 69453bd3b5..7f3e0be974 100644
--- a/Makefile.win
+++ b/Makefile.win
@@ -1,6 +1,6 @@
 TARGET = ssnes.exe
 JTARGET = ssnes-joyconfig.exe
-OBJ = ssnes.o file.o driver.o conf/config_file.o settings.o dynamic.o message.o rewind.o movie.o autosave.o gfx/gfx_common.o bps.o ups.o strl.o screenshot.o audio/hermite.o thread.o getopt.o
+OBJ = ssnes.o file.o driver.o conf/config_file.o settings.o dynamic.o message.o rewind.o movie.o gfx/gfx_common.o bps.o ups.o strl.o screenshot.o audio/hermite.o thread.o getopt.o
 JOBJ = conf/config_file.o tools/main-stub.o tools/ssnes-joyconfig.o strl.o
 
 CC = gcc
@@ -13,6 +13,7 @@ HAVE_SDL = 1
 HAVE_OPENGL = 1
 HAVE_DYLIB = 1
 HAVE_NETPLAY = 1
+HAVE_THREADS = 1
 DYNAMIC = 1
 
 ifeq ($(SLIM),)
@@ -39,6 +40,11 @@ ifeq ($(HAVE_SDL), 1)
    DEFINES += -ISDL -DHAVE_SDL
 endif
 
+ifeq ($(HAVE_THREADS), 1)
+   OBJ += autosave.o
+   DEFINES += -DHAVE_THREADS
+endif
+
 ifeq ($(HAVE_OPENGL), 1)
    DEFINES += -DHAVE_OPENGL
    LIBS += -lopengl32
diff --git a/config.features.h b/config.features.h
index 4e056332a6..117acb7767 100644
--- a/config.features.h
+++ b/config.features.h
@@ -14,6 +14,12 @@ static const bool _sdl_supp = true;
 static const bool _sdl_supp = false;
 #endif
 
+#ifdef HAVE_THREADS
+static const bool _thread_supp = true;
+#else
+static const bool _thread_supp = false;
+#endif
+
 #ifdef HAVE_OPENGL
 static const bool _opengl_supp = true;
 #else
diff --git a/netplay.c b/netplay.c
index 4fe2139d15..f9b28c84ae 100644
--- a/netplay.c
+++ b/netplay.c
@@ -735,9 +735,13 @@ void netplay_post_frame(netplay_t *handle)
       while (first || (handle->tmp_ptr != handle->self_ptr))
       {
          psnes_serialize(handle->buffer[handle->tmp_ptr].state, handle->state_size);
+#ifdef HAVE_THREADS
          lock_autosave();
+#endif
          psnes_run();
+#ifdef HAVE_THREADS
          unlock_autosave();
+#endif
          handle->tmp_ptr = NEXT_PTR(handle->tmp_ptr);
          first = false;
       }
diff --git a/qb/config.libs.sh b/qb/config.libs.sh
index b6e64575c6..9cc90e407c 100644
--- a/qb/config.libs.sh
+++ b/qb/config.libs.sh
@@ -31,6 +31,7 @@ if [ $HAVE_DYNAMIC != yes ]; then
    add_define_make libsnes $LIBSNES
 fi
 
+check_lib THREADS -lpthread pthread_create
 check_lib DYLIB $DYLIB dlopen
 check_lib NETPLAY -lc socket
 check_lib GETOPT_LONG -lc getopt_long
@@ -78,25 +79,30 @@ fi
 check_pkgconf XML libxml-2.0
 check_pkgconf SDL_IMAGE SDL_image
 
-if [ $HAVE_FFMPEG != no ]; then
-   check_pkgconf AVCODEC libavcodec
-   check_pkgconf AVFORMAT libavformat
-   check_pkgconf AVUTIL libavutil
-   check_pkgconf SWSCALE libswscale
+if [ $HAVE_THREADS != no ]; then
+   if [ $HAVE_FFMPEG != no ]; then
+      check_pkgconf AVCODEC libavcodec
+      check_pkgconf AVFORMAT libavformat
+      check_pkgconf AVUTIL libavutil
+      check_pkgconf SWSCALE libswscale
 
-   ( [ $HAVE_FFMPEG = auto ] && ( [ $HAVE_AVCODEC = no ] || [ $HAVE_AVFORMAT = no ] || [ $HAVE_AVUTIL = no ] || [ $HAVE_SWSCALE = no ] ) && HAVE_FFMPEG=no ) || HAVE_FFMPEG=yes
-fi
+      ( [ $HAVE_FFMPEG = auto ] && ( [ $HAVE_AVCODEC = no ] || [ $HAVE_AVFORMAT = no ] || [ $HAVE_AVUTIL = no ] || [ $HAVE_SWSCALE = no ] ) && HAVE_FFMPEG=no ) || HAVE_FFMPEG=yes
+   fi
 
-if [ $HAVE_FFMPEG = yes ]; then
-   check_lib FFMPEG_ALLOC_CONTEXT3 "$AVCODEC_LIBS" avcodec_alloc_context3
-   check_lib FFMPEG_AVCODEC_OPEN2 "$AVCODEC_LIBS" avcodec_open2
-   check_lib FFMPEG_AVIO_OPEN "$AVFORMAT_LIBS" avio_open
-   check_lib FFMPEG_AVFORMAT_WRITE_HEADER "$AVFORMAT_LIBS" avformat_write_header
-   check_lib FFMPEG_AVFORMAT_NEW_STREAM "$AVFORMAT_LIBS" avformat_new_stream
-fi
+   if [ $HAVE_FFMPEG = yes ]; then
+      check_lib FFMPEG_ALLOC_CONTEXT3 "$AVCODEC_LIBS" avcodec_alloc_context3
+      check_lib FFMPEG_AVCODEC_OPEN2 "$AVCODEC_LIBS" avcodec_open2
+      check_lib FFMPEG_AVIO_OPEN "$AVFORMAT_LIBS" avio_open
+      check_lib FFMPEG_AVFORMAT_WRITE_HEADER "$AVFORMAT_LIBS" avformat_write_header
+      check_lib FFMPEG_AVFORMAT_NEW_STREAM "$AVFORMAT_LIBS" avformat_new_stream
+   fi
 
-if [ $HAVE_FFMPEG = no ] && [ $HAVE_X264RGB = yes ]; then
-   echo "x264 RGB recording is enabled, but FFmpeg is not. --enable-x264rgb will not have any effect."
+   if [ $HAVE_FFMPEG = no ] && [ $HAVE_X264RGB = yes ]; then
+      echo "x264 RGB recording is enabled, but FFmpeg is not. --enable-x264rgb will not have any effect."
+   fi
+else
+   echo "Not building with threading support. Will skip FFmpeg."
+   HAVE_FFMPEG=no
 fi
 
 check_lib DYNAMIC $DYLIB dlopen
@@ -118,7 +124,7 @@ check_pkgconf PYTHON python3
 add_define_make OS $OS
 
 # Creates config.mk and config.h.
-VARS="ALSA OSS OSS_BSD OSS_LIB AL RSOUND ROAR JACK COREAUDIO PULSE SDL OPENGL DYLIB GETOPT_LONG CG XML SDL_IMAGE DYNAMIC FFMPEG AVCODEC AVFORMAT AVUTIL SWSCALE CONFIGFILE FREETYPE XVIDEO X11 XEXT NETPLAY FBO STRL PYTHON FFMPEG_ALLOC_CONTEXT3 FFMPEG_AVCODEC_OPEN2 FFMPEG_AVIO_OPEN FFMPEG_AVFORMAT_WRITE_HEADER FFMPEG_AVFORMAT_NEW_STREAM X264RGB"
+VARS="ALSA OSS OSS_BSD OSS_LIB AL RSOUND ROAR JACK COREAUDIO PULSE SDL OPENGL DYLIB GETOPT_LONG THREADS CG XML SDL_IMAGE DYNAMIC FFMPEG AVCODEC AVFORMAT AVUTIL SWSCALE CONFIGFILE FREETYPE XVIDEO X11 XEXT NETPLAY FBO STRL PYTHON FFMPEG_ALLOC_CONTEXT3 FFMPEG_AVCODEC_OPEN2 FFMPEG_AVIO_OPEN FFMPEG_AVFORMAT_WRITE_HEADER FFMPEG_AVFORMAT_NEW_STREAM X264RGB"
 create_config_make config.mk $VARS
 create_config_header config.h $VARS
 
diff --git a/qb/config.params.sh b/qb/config.params.sh
index 93faf751c3..f7e43e5e59 100644
--- a/qb/config.params.sh
+++ b/qb/config.params.sh
@@ -9,6 +9,7 @@ PACKAGE_VERSION=0.9.2
 # $3: Default arg. auto implies that HAVE_ALSA will be set according to library checks later on.
 add_command_line_enable DYNAMIC "Disable dynamic loading of libsnes library" yes
 add_command_line_string LIBSNES "libsnes library used" ""
+add_command_line_enable THREADS "Threading support" auto
 add_command_line_enable FFMPEG "Enable FFmpeg recording support" auto
 add_command_line_enable X264RGB "Enable lossless X264 RGB recording" no
 add_command_line_enable DYLIB "Enable dynamic loading support" auto
diff --git a/ssnes.c b/ssnes.c
index 2ead93c9ae..b3bb9fa680 100644
--- a/ssnes.c
+++ b/ssnes.c
@@ -406,6 +406,7 @@ static void print_features(void)
    puts("");
    puts("Features:");
    _PSUPP(sdl, "SDL", "SDL drivers");
+   _PSUPP(thread, "Threads", "Threading support");
    _PSUPP(opengl, "OpenGL", "OpenGL driver");
    _PSUPP(xvideo, "XVideo", "XVideo output");
    _PSUPP(alsa, "ALSA", "audio driver");
@@ -1212,6 +1213,7 @@ static void deinit_netplay(void)
 
 static void init_autosave(void)
 {
+#ifdef HAVE_THREADS
    int ram_types[2] = {-1, -1};
    const char *ram_paths[2] = {NULL, NULL};
 
@@ -1261,15 +1263,18 @@ static void init_autosave(void)
          }
       }
    }
+#endif
 }
 
 static void deinit_autosave(void)
 {
+#ifdef HAVE_THREADS
    for (unsigned i = 0; i < sizeof(g_extern.autosave)/sizeof(g_extern.autosave[0]); i++)
    {
       if (g_extern.autosave[i])
          autosave_free(g_extern.autosave[i]);
    }
+#endif
 }
 
 static void set_savestate_auto_index(void)
@@ -2032,7 +2037,9 @@ int main(int argc, char *argv[])
       // Run libsnes for one frame.
       if (!g_extern.is_paused || g_extern.is_oneshot)
       {
+#ifdef HAVE_THREADS
          lock_autosave();
+#endif
 
 #ifdef HAVE_NETPLAY
          if (g_extern.netplay)
@@ -2050,7 +2057,9 @@ int main(int argc, char *argv[])
             netplay_post_frame(g_extern.netplay);
 #endif
 
+#ifdef HAVE_THREADS
          unlock_autosave();
+#endif
       }
       else
       {