History

pthread: Avoid crashes/odd behavior caused by spurious wakeups

This is similar to 5152196b2b47d446d9a509b9bfb318daa26a1f2b.

Derek Buitenhuis committed 13 years ago

Commit 3511d4f1 parent 157dc0b1 file · +8 −2Browse files

Unified · Split

libavfilter/pthread.c+8−2
@@ -56,6 +56,7 @@ typedef struct ThreadContext { · 55 unmodified lines
5656 pthread_cond_t current_job_cond;
5757 pthread_mutex_t current_job_lock;
5858 int current_job;
59+ unsigned int current_execute;
5960 int done;
6061 } ThreadContext;
6162
@@ -64,6 +65,7 @@ static void* attribute_align_arg worker(void *v) · 2 unmodified lines
6465 ThreadContext *c = v;
6566 int our_job = c->nb_jobs;
6667 int nb_threads = c->nb_threads;
68+ unsigned int last_execute = 0;
6769 int self_id;
6870
6971 pthread_mutex_lock(&c->current_job_lock);
@@ -73,8 +75,9 @@ static void* attribute_align_arg worker(void *v) · 3 unmodified lines
7375 if (c->current_job == nb_threads + c->nb_jobs)
7476 pthread_cond_signal(&c->last_job_cond);
7577
76− if (!c->done)
78+ while (last_execute == c->current_execute && !c->done)
7779 pthread_cond_wait(&c->current_job_cond, &c->current_job_lock);
80+ last_execute = c->current_execute;
7881 our_job = self_id;
7982
8083 if (c->done) {
@@ -111,7 +114,8 @@ static void slice_thread_uninit(ThreadContext *c) · 30 unmodified lines
111114
112115 static void slice_thread_park_workers(ThreadContext *c)
113116 {
114− pthread_cond_wait(&c->last_job_cond, &c->current_job_lock);
117+ while (c->current_job != c->nb_threads + c->nb_jobs)
118+ pthread_cond_wait(&c->last_job_cond, &c->current_job_lock);
115119 pthread_mutex_unlock(&c->current_job_lock);
116120 }
117121
@@ -138,6 +142,8 @@ static int thread_execute(AVFilterContext *ctx, avfilter_action_func *func, · 20 unmodified lines
138142 c->rets = &dummy_ret;
139143 c->nb_rets = 1;
140144 }
145+ c->current_execute++;
146+
141147 pthread_cond_broadcast(&c->current_job_cond);
142148
143149 slice_thread_park_workers(c);

Keyboard shortcuts

General

Show keyboard shortcuts
?
Focus the current search
/

Issues and pull requests

Open filters
f

Issues, pull requests and Inbox

Select next item
j↓
Select previous item
k↑
Open selected item
Enter
Dismiss filters or clear selection
Esc

Repository code

Go to file
t