Commit b6587421 authored by Anton Khirnov's avatar Anton Khirnov Committed by wm4

pthread_frame: use atomics for frame progress

Merges Libav commit 59c70227.
Signed-off-by: 's avatarwm4 <nfxjfg@googlemail.com>
parent 74926269
...@@ -486,9 +486,11 @@ int ff_thread_decode_frame(AVCodecContext *avctx, ...@@ -486,9 +486,11 @@ int ff_thread_decode_frame(AVCodecContext *avctx,
void ff_thread_report_progress(ThreadFrame *f, int n, int field) void ff_thread_report_progress(ThreadFrame *f, int n, int field)
{ {
PerThreadContext *p; PerThreadContext *p;
volatile int *progress = f->progress ? (int*)f->progress->data : NULL; atomic_int *progress = f->progress ? (atomic_int*)f->progress->data : NULL;
if (!progress || progress[field] >= n) return; if (!progress ||
atomic_load_explicit(&progress[field], memory_order_acquire) >= n)
return;
p = f->owner->internal->thread_ctx; p = f->owner->internal->thread_ctx;
...@@ -496,7 +498,9 @@ void ff_thread_report_progress(ThreadFrame *f, int n, int field) ...@@ -496,7 +498,9 @@ void ff_thread_report_progress(ThreadFrame *f, int n, int field)
av_log(f->owner, AV_LOG_DEBUG, "%p finished %d field %d\n", progress, n, field); av_log(f->owner, AV_LOG_DEBUG, "%p finished %d field %d\n", progress, n, field);
pthread_mutex_lock(&p->progress_mutex); pthread_mutex_lock(&p->progress_mutex);
progress[field] = n;
atomic_store(&progress[field], n);
pthread_cond_broadcast(&p->progress_cond); pthread_cond_broadcast(&p->progress_cond);
pthread_mutex_unlock(&p->progress_mutex); pthread_mutex_unlock(&p->progress_mutex);
} }
...@@ -504,9 +508,11 @@ void ff_thread_report_progress(ThreadFrame *f, int n, int field) ...@@ -504,9 +508,11 @@ void ff_thread_report_progress(ThreadFrame *f, int n, int field)
void ff_thread_await_progress(ThreadFrame *f, int n, int field) void ff_thread_await_progress(ThreadFrame *f, int n, int field)
{ {
PerThreadContext *p; PerThreadContext *p;
volatile int *progress = f->progress ? (int*)f->progress->data : NULL; atomic_int *progress = f->progress ? (atomic_int*)f->progress->data : NULL;
if (!progress || progress[field] >= n) return; if (!progress ||
atomic_load_explicit(&progress[field], memory_order_acquire) >= n)
return;
p = f->owner->internal->thread_ctx; p = f->owner->internal->thread_ctx;
...@@ -514,7 +520,7 @@ void ff_thread_await_progress(ThreadFrame *f, int n, int field) ...@@ -514,7 +520,7 @@ void ff_thread_await_progress(ThreadFrame *f, int n, int field)
av_log(f->owner, AV_LOG_DEBUG, "thread awaiting %d field %d from %p\n", n, field, progress); av_log(f->owner, AV_LOG_DEBUG, "thread awaiting %d field %d from %p\n", n, field, progress);
pthread_mutex_lock(&p->progress_mutex); pthread_mutex_lock(&p->progress_mutex);
while (progress[field] < n) while (atomic_load_explicit(&progress[field], memory_order_relaxed) < n)
pthread_cond_wait(&p->progress_cond, &p->progress_mutex); pthread_cond_wait(&p->progress_cond, &p->progress_mutex);
pthread_mutex_unlock(&p->progress_mutex); pthread_mutex_unlock(&p->progress_mutex);
} }
...@@ -789,14 +795,15 @@ static int thread_get_buffer_internal(AVCodecContext *avctx, ThreadFrame *f, int ...@@ -789,14 +795,15 @@ static int thread_get_buffer_internal(AVCodecContext *avctx, ThreadFrame *f, int
} }
if (avctx->internal->allocate_progress) { if (avctx->internal->allocate_progress) {
int *progress; atomic_int *progress;
f->progress = av_buffer_alloc(2 * sizeof(int)); f->progress = av_buffer_alloc(2 * sizeof(*progress));
if (!f->progress) { if (!f->progress) {
return AVERROR(ENOMEM); return AVERROR(ENOMEM);
} }
progress = (int*)f->progress->data; progress = (atomic_int*)f->progress->data;
progress[0] = progress[1] = -1; atomic_store(&progress[0], -1);
atomic_store(&progress[1], -1);
} }
pthread_mutex_lock(&p->parent->buffer_mutex); pthread_mutex_lock(&p->parent->buffer_mutex);
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment