diff options
author | 2009-07-06 03:01:41 +0200 | |
---|---|---|
committer | 2009-07-06 03:01:41 +0200 | |
commit | d05d431ebad5767f3afa9b6c76639e83233e2d9c (patch) | |
tree | 0ab32f81a39f5ce3fab292892aee97ee1f85e6c5 | |
parent | b7a872069ad3990b65d8ebe6d53de8768841980f (diff) |
fixed inter-thread conflict for codec access
-rw-r--r-- | codec.c | 25 | ||||
-rw-r--r-- | codec.h | 12 | ||||
-rw-r--r-- | main.c | 4 | ||||
-rw-r--r-- | psdl.c | 4 |
4 files changed, 45 insertions, 0 deletions
@@ -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); +} + @@ -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 @@ -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 (); } @@ -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; } |