FFmpeg
threadprogress.c
Go to the documentation of this file.
1 /*
2  * Copyright (c) 2022 Andreas Rheinhardt <andreas.rheinhardt@outlook.com>
3  *
4  * This file is part of FFmpeg.
5  *
6  * FFmpeg is free software; you can redistribute it and/or
7  * modify it under the terms of the GNU Lesser General Public
8  * License as published by the Free Software Foundation; either
9  * version 2.1 of the License, or (at your option) any later version.
10  *
11  * FFmpeg is distributed in the hope that it will be useful,
12  * but WITHOUT ANY WARRANTY; without even the implied warranty of
13  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14  * Lesser General Public License for more details.
15  *
16  * You should have received a copy of the GNU Lesser General Public
17  * License along with FFmpeg; if not, write to the Free Software
18  * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
19  */
20 
21 #include <limits.h>
22 #include <stdatomic.h>
23 
24 #include "pthread_internal.h"
25 #include "threadprogress.h"
26 #include "libavutil/attributes.h"
27 #include "libavutil/thread.h"
28 
29 DEFINE_OFFSET_ARRAY(ThreadProgress, thread_progress, init,
30  (offsetof(ThreadProgress, progress_mutex)),
31  (offsetof(ThreadProgress, progress_cond)));
32 
34 {
35  atomic_init(&pro->progress, init_mode ? -1 : INT_MAX);
36 #if HAVE_THREADS
37  if (init_mode)
38  return ff_pthread_init(pro, thread_progress_offsets);
39 #endif
40  pro->init = init_mode;
41  return 0;
42 }
43 
45 {
46 #if HAVE_THREADS
47  ff_pthread_free(pro, thread_progress_offsets);
48 #else
49  pro->init = 0;
50 #endif
51 }
52 
54 {
55  if (atomic_load_explicit(&pro->progress, memory_order_relaxed) >= n)
56  return;
57 
58  atomic_store_explicit(&pro->progress, n, memory_order_release);
59 
63 }
64 
65 void ff_thread_progress_await(const ThreadProgress *pro_c, int n)
66 {
67  /* Casting const away here is safe, because we only read from progress
68  * and will leave pro_c in the same state upon leaving the function
69  * as it had at the beginning. */
70  ThreadProgress *pro = (ThreadProgress*)pro_c;
71 
72  if (atomic_load_explicit(&pro->progress, memory_order_acquire) >= n)
73  return;
74 
76  while (atomic_load_explicit(&pro->progress, memory_order_relaxed) < n)
79 }
ff_thread_progress_report
void ff_thread_progress_report(ThreadProgress *pro, int n)
This function is a no-op in no-op mode; otherwise it notifies other threads that a certain level of p...
Definition: threadprogress.c:53
threadprogress.h
ThreadProgress
ThreadProgress is an API to easily notify other threads about progress of any kind as long as it can ...
Definition: threadprogress.h:43
thread.h
ff_cond_broadcast
static int ff_cond_broadcast(AVCond *cond)
Definition: thread.h:197
ff_pthread_free
av_cold void ff_pthread_free(void *obj, const unsigned offsets[])
Definition: pthread.c:92
ff_mutex_unlock
static int ff_mutex_unlock(AVMutex *mutex)
Definition: thread.h:189
av_cold
#define av_cold
Definition: attributes.h:90
ff_cond_wait
static int ff_cond_wait(AVCond *cond, AVMutex *mutex)
Definition: thread.h:198
limits.h
ThreadProgress::progress_cond
AVCond progress_cond
Definition: threadprogress.h:47
ff_thread_progress_await
void ff_thread_progress_await(const ThreadProgress *pro_c, int n)
This function is a no-op in no-op mode; otherwise it waits until other threads have reached a certain...
Definition: threadprogress.c:65
pthread_internal.h
ThreadProgress::progress_mutex
AVMutex progress_mutex
Definition: threadprogress.h:46
atomic_load_explicit
#define atomic_load_explicit(object, order)
Definition: stdatomic.h:96
init
int(* init)(AVBSFContext *ctx)
Definition: dts2pts.c:368
ThreadProgress::progress
atomic_int progress
Definition: threadprogress.h:44
attributes.h
ff_mutex_lock
static int ff_mutex_lock(AVMutex *mutex)
Definition: thread.h:188
atomic_store_explicit
#define atomic_store_explicit(object, desired, order)
Definition: stdatomic.h:90
ff_thread_progress_init
av_cold int ff_thread_progress_init(ThreadProgress *pro, int init_mode)
Initialize a ThreadProgress.
Definition: threadprogress.c:33
ThreadProgress::init
unsigned init
Definition: threadprogress.h:45
DEFINE_OFFSET_ARRAY
DEFINE_OFFSET_ARRAY(ThreadProgress, thread_progress, init,(0x42),(0x42))
ff_thread_progress_destroy
av_cold void ff_thread_progress_destroy(ThreadProgress *pro)
Destroy a ThreadProgress.
Definition: threadprogress.c:44
ff_pthread_init
av_cold int ff_pthread_init(void *obj, const unsigned offsets[])
Initialize/destroy a list of mutexes/conditions contained in a structure.
Definition: pthread.c:105
atomic_init
#define atomic_init(obj, value)
Definition: stdatomic.h:33