summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorGravatar waker <wakeroid@gmail.com>2009-07-06 03:01:41 +0200
committerGravatar waker <wakeroid@gmail.com>2009-07-06 03:01:41 +0200
commitd05d431ebad5767f3afa9b6c76639e83233e2d9c (patch)
tree0ab32f81a39f5ce3fab292892aee97ee1f85e6c5
parentb7a872069ad3990b65d8ebe6d53de8768841980f (diff)
fixed inter-thread conflict for codec access
-rw-r--r--codec.c25
-rw-r--r--codec.h12
-rw-r--r--main.c4
-rw-r--r--psdl.c4
4 files changed, 45 insertions, 0 deletions
diff --git a/codec.c b/codec.c
index e69de29b..5beaa43b 100644
--- a/codec.c
+++ b/codec.c
@@ -0,0 +1,25 @@
+#include "codec.h"
+#include "threading.h"
+
+static uintptr_t mutex;
+
+void
+codec_init_locking (void) {
+ mutex = mutex_create ();
+}
+
+void
+codec_free_locking (void) {
+ mutex_free (mutex);
+}
+
+void
+codec_lock (void) {
+ mutex_lock (mutex);
+}
+
+void
+codec_unlock (void) {
+ mutex_unlock (mutex);
+}
+
diff --git a/codec.h b/codec.h
index 5a33590a..56033e94 100644
--- a/codec.h
+++ b/codec.h
@@ -22,4 +22,16 @@ typedef struct codec_s {
codec_t *get_codec_for_file (const char *fname);
+void
+codec_init_locking (void);
+
+void
+codec_free_locking (void);
+
+void
+codec_lock (void);
+
+void
+codec_unlock (void);
+
#endif // __CODEC_H
diff --git a/main.c b/main.c
index fd097d5c..985da9f1 100644
--- a/main.c
+++ b/main.c
@@ -18,6 +18,7 @@ int psdl_terminate = 0;
void
psdl_thread (uintptr_t ctx) {
+ codec_init_locking ();
psdl_init ();
while (!psdl_terminate) {
uint32_t msg;
@@ -65,7 +66,9 @@ psdl_thread (uintptr_t ctx) {
case M_SONGSEEK:
if (playlist_current && playlist_current->codec) {
psdl_pause ();
+ codec_lock ();
playlist_current->codec->seek (p1 / 1000.f);
+ codec_unlock ();
psdl_unpause ();
}
break;
@@ -75,6 +78,7 @@ psdl_thread (uintptr_t ctx) {
// handle message pump here
}
psdl_free ();
+ codec_free_locking ();
ps_free ();
}
diff --git a/psdl.c b/psdl.c
index 3f4c92b9..3bba6916 100644
--- a/psdl.c
+++ b/psdl.c
@@ -139,7 +139,9 @@ psdl_callback (void* userdata, Uint8 *stream, int len) {
memset (stream, 0, len);
}
else if (codec->info.samplesPerSecond == sdl_player_freq) {
+ codec_lock ();
codecret = codec->read (stream, len);
+ codec_unlock ();
}
else {
int nsamples = len/4;
@@ -147,6 +149,7 @@ psdl_callback (void* userdata, Uint8 *stream, int len) {
// add 5 extra samples for SRC
nsamples = nsamples * codec->info.samplesPerSecond / sdl_player_freq + 5;
// read data at source samplerate (with some room for SRC)
+ codec_lock ();
codecret = codec->read (sdl_buffer, (nsamples - codecleft) * 2 * codec->info.channels);
// convert to float, and apply soft volume
int i;
@@ -178,6 +181,7 @@ psdl_callback (void* userdata, Uint8 *stream, int len) {
codecleft = nsamples - srcdata.input_frames_used;
// copy spare samples for next update
memmove (sdl_fbuffer, &sdl_fbuffer[srcdata.input_frames_used*2], codecleft * 8);
+ codec_unlock ();
}
sdl_eof = codecret == -1 ? 1 : 0;
}