Skip to content
Snippets Groups Projects
Commit a13b7713 authored by Deucе's avatar Deucе :ok_hand_tone4:
Browse files

Add missing re-lock in xptone_open_locked()

While we're here, add debug assertions to the pthread stuff.
parent b9bbf9b6
No related branches found
No related tags found
No related merge requests found
Pipeline #7914 passed
...@@ -70,6 +70,16 @@ ...@@ -70,6 +70,16 @@
#ifdef XPDEV_THREAD_SAFE #ifdef XPDEV_THREAD_SAFE
#include "threadwrap.h" #include "threadwrap.h"
#ifndef NDEBUG
#define chk_pthread_mutex_init(a, b) assert(pthread_mutex_init(a, b) == 0)
#define chk_pthread_mutex_lock(a) assert(pthread_mutex_lock(a) == 0)
#define chk_pthread_mutex_unlock(a) assert(pthread_mutex_unlock(a) == 0)
#else
#define chk_pthread_mutex_init(a, b) pthread_mutex_init(a, b)
#define chk_pthread_mutex_lock(a) pthread_mutex_lock(a)
#define chk_pthread_mutex_unlock(a) pthread_mutex_unlock(a)
#endif
static bool sample_thread_running = false; static bool sample_thread_running = false;
static sem_t sample_pending_sem; static sem_t sample_pending_sem;
static sem_t sample_complete_sem; static sem_t sample_complete_sem;
...@@ -433,11 +443,12 @@ xptone_open_locked(void) ...@@ -433,11 +443,12 @@ xptone_open_locked(void)
handle_type = SOUND_DEVICE_PULSEAUDIO; handle_type = SOUND_DEVICE_PULSEAUDIO;
handle_rc++; handle_rc++;
#ifdef XPDEV_THREAD_SAFE #ifdef XPDEV_THREAD_SAFE
pthread_mutex_unlock(&handle_mutex); chk_pthread_mutex_unlock(&handle_mutex);
pthread_mutex_lock(&sample_mutex); chk_pthread_mutex_lock(&sample_mutex);
if (samples_posted == 0) if (samples_posted == 0)
xp_play_sample_locked((unsigned char *)"\x80", 1, false); xp_play_sample_locked((unsigned char *)"\x80", 1, false);
pthread_mutex_unlock(&sample_mutex); chk_pthread_mutex_unlock(&sample_mutex);
chk_pthread_mutex_lock(&handle_mutex);
#else #else
xptone(0, 1, WAVE_SHAPE_SQUARE); xptone(0, 1, WAVE_SHAPE_SQUARE);
#endif #endif
...@@ -681,11 +692,11 @@ xptone_open(void) ...@@ -681,11 +692,11 @@ xptone_open(void)
bool ret; bool ret;
#ifdef XPDEV_THREAD_SAFE #ifdef XPDEV_THREAD_SAFE
pthread_once(&sample_initialized_pto, init_sample); pthread_once(&sample_initialized_pto, init_sample);
pthread_mutex_lock(&handle_mutex); chk_pthread_mutex_lock(&handle_mutex);
#endif #endif
ret = xptone_open_locked(); ret = xptone_open_locked();
#ifdef XPDEV_THREAD_SAFE #ifdef XPDEV_THREAD_SAFE
pthread_mutex_unlock(&handle_mutex); chk_pthread_mutex_unlock(&handle_mutex);
#endif #endif
return ret; return ret;
} }
...@@ -755,12 +766,12 @@ void ...@@ -755,12 +766,12 @@ void
xptone_complete(void) xptone_complete(void)
{ {
#ifdef XPDEV_THREAD_SAFE #ifdef XPDEV_THREAD_SAFE
pthread_mutex_lock(&handle_mutex); chk_pthread_mutex_lock(&handle_mutex);
#endif #endif
// coverity[sleep] // coverity[sleep]
xptone_complete_locked(); xptone_complete_locked();
#ifdef XPDEV_THREAD_SAFE #ifdef XPDEV_THREAD_SAFE
pthread_mutex_unlock(&handle_mutex); chk_pthread_mutex_unlock(&handle_mutex);
#endif #endif
} }
...@@ -836,12 +847,12 @@ xptone_close(void) ...@@ -836,12 +847,12 @@ xptone_close(void)
bool ret; bool ret;
#ifdef XPDEV_THREAD_SAFE #ifdef XPDEV_THREAD_SAFE
pthread_mutex_lock(&handle_mutex); chk_pthread_mutex_lock(&handle_mutex);
#endif #endif
// coverity[sleep] // coverity[sleep]
ret = xptone_close_locked(); ret = xptone_close_locked();
#ifdef XPDEV_THREAD_SAFE #ifdef XPDEV_THREAD_SAFE
pthread_mutex_unlock(&handle_mutex); chk_pthread_mutex_unlock(&handle_mutex);
#endif #endif
return ret; return ret;
} }
...@@ -1033,19 +1044,19 @@ void xp_play_sample_thread(void *data) ...@@ -1033,19 +1044,19 @@ void xp_play_sample_thread(void *data)
else else
waited = false; waited = false;
posted_last = false; posted_last = false;
pthread_mutex_lock(&handle_mutex); chk_pthread_mutex_lock(&handle_mutex);
if (handle_type == SOUND_DEVICE_CLOSED) { if (handle_type == SOUND_DEVICE_CLOSED) {
must_close = true; must_close = true;
if (!xptone_open_locked()) { if (!xptone_open_locked()) {
sem_post(&sample_complete_sem); sem_post(&sample_complete_sem);
pthread_mutex_unlock(&handle_mutex); chk_pthread_mutex_unlock(&handle_mutex);
continue; continue;
} }
} }
if (pthread_mutex_lock(&sample_mutex) != 0) { if (pthread_mutex_lock(&sample_mutex) != 0) {
pthread_mutex_unlock(&handle_mutex); chk_pthread_mutex_unlock(&handle_mutex);
goto error_return; goto error_return;
} }
this_sample_size = sample_size; this_sample_size = sample_size;
...@@ -1054,12 +1065,12 @@ void xp_play_sample_thread(void *data) ...@@ -1054,12 +1065,12 @@ void xp_play_sample_thread(void *data)
sample = (unsigned char *)malloc(sample_size); sample = (unsigned char *)malloc(sample_size);
if (sample == NULL) { if (sample == NULL) {
sem_post(&sample_complete_sem); sem_post(&sample_complete_sem);
pthread_mutex_unlock(&sample_mutex); chk_pthread_mutex_unlock(&sample_mutex);
pthread_mutex_unlock(&handle_mutex); chk_pthread_mutex_unlock(&handle_mutex);
continue; continue;
} }
memcpy(sample, sample_buffer, this_sample_size); memcpy(sample, sample_buffer, this_sample_size);
pthread_mutex_unlock(&sample_mutex); chk_pthread_mutex_unlock(&sample_mutex);
do_xp_play_sample(sample, this_sample_size, &freed); do_xp_play_sample(sample, this_sample_size, &freed);
if (freed) if (freed)
...@@ -1075,12 +1086,12 @@ void xp_play_sample_thread(void *data) ...@@ -1075,12 +1086,12 @@ void xp_play_sample_thread(void *data)
xptone_close_locked(); xptone_close_locked();
} }
} }
pthread_mutex_unlock(&handle_mutex); chk_pthread_mutex_unlock(&handle_mutex);
} }
error_return: error_return:
#ifdef _WIN32 #ifdef _WIN32
pthread_mutex_lock(&handle_mutex); chk_pthread_mutex_lock(&handle_mutex);
if (handle_type == SOUND_DEVICE_WIN32) { if (handle_type == SOUND_DEVICE_WIN32) {
if (wh[curr_wh].dwFlags & WHDR_PREPARED) { if (wh[curr_wh].dwFlags & WHDR_PREPARED) {
while (waveOutUnprepareHeader(waveOut, &wh[curr_wh], sizeof(wh[curr_wh])) == WAVERR_STILLPLAYING) while (waveOutUnprepareHeader(waveOut, &wh[curr_wh], sizeof(wh[curr_wh])) == WAVERR_STILLPLAYING)
...@@ -1088,7 +1099,7 @@ error_return: ...@@ -1088,7 +1099,7 @@ error_return:
} }
FREE_AND_NULL(wh[curr_wh].lpData); FREE_AND_NULL(wh[curr_wh].lpData);
} }
pthread_mutex_unlock(&handle_mutex); chk_pthread_mutex_unlock(&handle_mutex);
#endif #endif
FREE_AND_NULL(sample); FREE_AND_NULL(sample);
...@@ -1102,8 +1113,8 @@ error_return: ...@@ -1102,8 +1113,8 @@ error_return:
static void static void
init_sample(void) init_sample(void)
{ {
pthread_mutex_init(&sample_mutex, NULL); chk_pthread_mutex_init(&sample_mutex, NULL);
pthread_mutex_init(&handle_mutex, NULL); chk_pthread_mutex_init(&handle_mutex, NULL);
sem_init(&sample_pending_sem, 0, 0); sem_init(&sample_pending_sem, 0, 0);
sem_init(&sample_complete_sem, 0, 0); sem_init(&sample_complete_sem, 0, 0);
} }
...@@ -1116,9 +1127,9 @@ static bool xp_play_sample_locked(unsigned char *sample, size_t size, bool backg ...@@ -1116,9 +1127,9 @@ static bool xp_play_sample_locked(unsigned char *sample, size_t size, bool backg
} }
while (samples_posted > 0) { while (samples_posted > 0) {
pthread_mutex_unlock(&sample_mutex); chk_pthread_mutex_unlock(&sample_mutex);
sem_wait(&sample_complete_sem); sem_wait(&sample_complete_sem);
pthread_mutex_lock(&sample_mutex); chk_pthread_mutex_lock(&sample_mutex);
samples_posted--; samples_posted--;
} }
sample_buffer = sample; sample_buffer = sample;
...@@ -1127,9 +1138,9 @@ static bool xp_play_sample_locked(unsigned char *sample, size_t size, bool backg ...@@ -1127,9 +1138,9 @@ static bool xp_play_sample_locked(unsigned char *sample, size_t size, bool backg
sem_post(&sample_pending_sem); sem_post(&sample_pending_sem);
if (!background) { if (!background) {
while (samples_posted > 0) { while (samples_posted > 0) {
pthread_mutex_unlock(&sample_mutex); chk_pthread_mutex_unlock(&sample_mutex);
sem_wait(&sample_complete_sem); sem_wait(&sample_complete_sem);
pthread_mutex_lock(&sample_mutex); chk_pthread_mutex_lock(&sample_mutex);
samples_posted--; samples_posted--;
} }
} }
...@@ -1145,9 +1156,9 @@ bool xp_play_sample(unsigned char *sample, size_t size, bool background) ...@@ -1145,9 +1156,9 @@ bool xp_play_sample(unsigned char *sample, size_t size, bool background)
bool ret; bool ret;
pthread_once(&sample_initialized_pto, init_sample); pthread_once(&sample_initialized_pto, init_sample);
pthread_mutex_lock(&sample_mutex); chk_pthread_mutex_lock(&sample_mutex);
ret = xp_play_sample_locked(sample, size, background); ret = xp_play_sample_locked(sample, size, background);
pthread_mutex_unlock(&sample_mutex); chk_pthread_mutex_unlock(&sample_mutex);
return ret; return ret;
} }
#else #else
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment