Add two new locks
authorErik de Castro Lopo <erikd@mega-nerd.com>
Sat, 11 Jun 2016 07:57:06 +0000 (17:57 +1000)
committerErik de Castro Lopo <erikd@mega-nerd.com>
Sat, 11 Jun 2016 07:57:06 +0000 (17:57 +1000)
Makefile
src/curses/ux_audio.c
src/curses/ux_locks.c [new file with mode: 0644]
src/curses/ux_locks.h [new file with mode: 0644]

index 4df126778fc207598817bf84632c52919b324e3d..330555fd737582a2dfe4b50face9730092732d83 100644 (file)
--- a/Makefile
+++ b/Makefile
@@ -20,7 +20,7 @@ MAN_PREFIX = $(PREFIX)
 CONFIG_DIR = /etc
 #CONFIG_DIR = $(PREFIX)/etc
 
-# Pick your sound support.  The most featureful form of sound support is 
+# Pick your sound support.  The most featureful form of sound support is
 # through libao.  Comment all of these out if you don't want sound.
 #
 #SOUND = none
@@ -30,8 +30,8 @@ SOUND = ao
 
 
 ##########################################################################
-# The configuration options below are intended mainly for older flavors 
-# of Unix.  For Linux, BSD, and Solaris released since 2003, you can 
+# The configuration options below are intended mainly for older flavors
+# of Unix.  For Linux, BSD, and Solaris released since 2003, you can
 # ignore this section.
 ##########################################################################
 
@@ -44,7 +44,7 @@ COLOR = yes
 CURSES = -lncurses
 #CURSES = -lcurses
 
-# Just in case your operating system keeps its user-added header files 
+# Just in case your operating system keeps its user-added header files
 # somewhere unusual...
 #
 #INCL = -I/usr/local/include
@@ -67,8 +67,8 @@ CURSES = -lncurses
 #NO_MEMMOVE = yes
 
 # Default sample rate for sound effects.
-# All modern sound interfaces can be expected to support 44100 Hz sample 
-# rates.  Earlier ones, particularly ones in Sun 4c workstations support 
+# All modern sound interfaces can be expected to support 44100 Hz sample
+# rates.  Earlier ones, particularly ones in Sun 4c workstations support
 # only up to 8000 Hz.
 SAMPLERATE = 44100
 
@@ -123,7 +123,8 @@ CURSES_OBJECT = $(CURSES_DIR)/ux_init.o \
                $(CURSES_DIR)/ux_blorb.o \
                $(CURSES_DIR)/ux_audio.o \
                $(CURSES_DIR)/ux_resource.o \
-               $(CURSES_DIR)/ux_audio_none.o
+               $(CURSES_DIR)/ux_audio_none.o \
+               $(CURSES_DIR)/ux_locks.o
 
 DUMB_DIR = $(SRCDIR)/dumb
 DUMB_TARGET = $(SRCDIR)/frotz_dumb.a
index 3e2f7e9258385bf3abb03b92b069a04b7bc6e391..13d094cb0a12473d07f8bd75f078a227e7b24f9b 100644 (file)
@@ -36,6 +36,7 @@
 #endif
 
 #include "ux_frotz.h"
+#include "ux_locks.h"
 
 #ifndef NO_SOUND
 
@@ -99,7 +100,6 @@ bool music_stop = FALSE;
 
 float  *musicbuffer;
 int    musicsamples;
-int    musicnum;
 
 
 /*
@@ -259,8 +259,8 @@ void os_stop_sample (int number)
        while(pthread_kill(playaiff_id, 0) == 0);
     }
 
-    if (music_playing && (number == musicnum || number == 0)) {
-       music_playing = FALSE;
+    if (get_music_playing() && (number == get_musicnum () || number == 0)) {
+       set_music_playing(false);
        while(pthread_kill(playmusic_id, 0) == 0);
     }
 
@@ -456,7 +456,7 @@ static int mypower(int base, int exp) {
  * handled here.
  *
  * This function should be able to play OGG chunks, but because of a bug
- * or oversight in Libsndfile, that library is incapable of playing OGG 
+ * or oversight in Libsndfile, that library is incapable of playing OGG
  * data which are embedded in a larger file.
  *
  */
@@ -631,7 +631,7 @@ static void *playmod(EFFECT *raw_effect)
 
     EFFECT myeffect = *raw_effect;
 
-    musicnum = myeffect.number;
+    set_musicnum(myeffect.number);
 
     filestart = ftell(myeffect.fp);
     fseek(myeffect.fp, myeffect.result.data.startpos, SEEK_SET);
diff --git a/src/curses/ux_locks.c b/src/curses/ux_locks.c
new file mode 100644 (file)
index 0000000..08f02a4
--- /dev/null
@@ -0,0 +1,60 @@
+
+#include <pthread.h>
+#include <stdbool.h>
+
+#include "ux_locks.h"
+
+static bool    music_playing = false;
+static pthread_mutex_t music_playing_mutex = PTHREAD_MUTEX_INITIALIZER;
+
+static int     musicnum = 0;
+static pthread_mutex_t musicnum_mutex = PTHREAD_MUTEX_INITIALIZER;
+
+bool
+get_music_playing (void)
+{
+       bool value;
+
+       pthread_mutex_lock (&music_playing_mutex);
+    value = music_playing;
+    pthread_mutex_unlock (&music_playing_mutex);
+    return value;
+}
+
+
+bool
+set_music_playing (bool new_value)
+{
+       bool old_value;
+
+       pthread_mutex_lock (&music_playing_mutex);
+    old_value = music_playing;
+    music_playing = new_value;
+    pthread_mutex_unlock (&music_playing_mutex);
+    return old_value;
+}
+
+
+int
+get_musicnum (void)
+{
+       int value;
+
+       pthread_mutex_lock (&musicnum_mutex);
+    value = musicnum;
+    pthread_mutex_unlock (&musicnum_mutex);
+    return value;
+}
+
+
+int
+set_musicnum (int new_value)
+{
+       int old_value;
+
+       pthread_mutex_lock (&musicnum_mutex);
+    old_value = musicnum;
+    musicnum = new_value;
+    pthread_mutex_unlock (&musicnum_mutex);
+    return old_value;
+}
diff --git a/src/curses/ux_locks.h b/src/curses/ux_locks.h
new file mode 100644 (file)
index 0000000..a289469
--- /dev/null
@@ -0,0 +1,5 @@
+bool get_music_playing (void);
+bool set_music_playing (bool new_value);
+
+int get_musicnum (void);
+int set_musicnum (int new_value);