summaryrefslogtreecommitdiff
path: root/recordmydesktop/src/rmd_threads.c
diff options
context:
space:
mode:
authorVito Caputo <vcaputo@pengaru.com>2020-06-19 14:52:34 -0700
committerVito Caputo <vcaputo@pengaru.com>2020-06-23 18:04:38 -0700
commit001c13da0690b9eb6d7d5642099479e63041fbec (patch)
tree6e6787dbbd62d8e965ab1638d1ffee3087e773d5 /recordmydesktop/src/rmd_threads.c
parent1c4174855664744595541347afecfea1fb919b52 (diff)
*: first attempt to unfuck thread synchronization
I'm already regretting this... I don't think the author understood how condition variables work, that signal/broadcast is purely synchronous with blocked waiters - they don't queue a "signaled" state. So all these cond_waits littered everywhere without any explicit stateful condition being watched, if they didn't happen to be sitting in the wait when the signal occurred, that signal event was lost, and blocking would persist until the next one. Even if the wait only *just* missed the arrival due to scheduling, it was a very racy and broken use of condition variables. Not to mention the possibility of spurious wakeups... You *always* need some shared state to use condition variables properly, it's why they include a mutex; to protect the shared state. If you're not writing a while() loop to wait on a condition variable, you're almost certainly doing it wrong. This stuff will continue to evolve as I get around to it.
Diffstat (limited to 'recordmydesktop/src/rmd_threads.c')
-rw-r--r--recordmydesktop/src/rmd_threads.c14
1 files changed, 9 insertions, 5 deletions
diff --git a/recordmydesktop/src/rmd_threads.c b/recordmydesktop/src/rmd_threads.c
index 7afe792..91823b4 100644
--- a/recordmydesktop/src/rmd_threads.c
+++ b/recordmydesktop/src/rmd_threads.c
@@ -119,11 +119,13 @@ void rmdThreads(ProgData *pdata) {
pthread_join(image_capture_t,NULL);
fprintf(stderr,"Shutting down.");
//if no damage events have been received the thread will get stuck
- while (!pdata->th_enc_thread_waiting && !pdata->th_encoding_clean) {
- usleep(10000);
- pthread_mutex_lock(&pdata->img_buff_ready_mutex);
+ pthread_mutex_lock(&pdata->img_buff_ready_mutex);
+ while (pdata->th_enc_thread_waiting && !pdata->th_encoding_clean) {
+ puts("waiting for th_enc");
+ pdata->th_enc_thread_waiting = 0;
pthread_cond_signal(&pdata->image_buffer_ready);
pthread_mutex_unlock(&pdata->img_buff_ready_mutex);
+ usleep(10000);
}
if (pdata->args.encOnTheFly)
@@ -141,11 +143,13 @@ void rmdThreads(ProgData *pdata) {
pthread_join(sound_capture_t,NULL);
fprintf(stderr,".");
+ pthread_mutex_lock(&pdata->snd_buff_ready_mutex);
while (pdata->v_enc_thread_waiting || !pdata->v_encoding_clean) {
- usleep(10000);
- pthread_mutex_lock(&pdata->snd_buff_ready_mutex);
+ puts("waiting for v_enc");
+ pdata->v_enc_thread_waiting = 0;
pthread_cond_signal(&pdata->sound_data_read);
pthread_mutex_unlock(&pdata->snd_buff_ready_mutex);
+ usleep(10000);
}
if (pdata->args.encOnTheFly)
© All Rights Reserved