19 #include <stdatomic.h>
25 #if HAVE_PTHREADS || HAVE_W32THREADS || HAVE_OS2THREADS
27 typedef struct WorkerContext {
36 WorkerContext *workers;
38 int nb_active_threads;
49 void (*
worker_func)(
void *priv,
int jobnr,
int threadnr,
int nb_jobs,
int nb_threads);
55 unsigned nb_jobs = ctx->nb_jobs;
56 unsigned nb_active_threads = ctx->nb_active_threads;
58 unsigned current_job = first_job;
61 ctx->worker_func(ctx->priv, current_job, first_job, nb_jobs, nb_active_threads);
64 return current_job == nb_jobs + nb_active_threads - 1;
95 void (*
worker_func)(
void *priv,
int jobnr,
int threadnr,
int nb_jobs,
int nb_threads),
110 nb_threads = nb_cpus + 1;
115 nb_workers = nb_threads;
123 if (nb_workers && !(ctx->workers =
av_calloc(nb_workers,
sizeof(*ctx->workers)))) {
131 ctx->nb_threads = nb_threads;
132 ctx->nb_active_threads = 0;
142 for (i = 0; i < nb_workers; i++) {
143 WorkerContext *w = &ctx->workers[i];
170 int nb_workers, i, is_last = 0;
173 ctx->nb_jobs = nb_jobs;
174 ctx->nb_active_threads =
FFMIN(nb_jobs, ctx->nb_threads);
177 nb_workers = ctx->nb_active_threads;
178 if (!ctx->main_func || !execute_main)
181 for (i = 0; i < nb_workers; i++) {
182 WorkerContext *w = &ctx->workers[i];
189 if (ctx->main_func && execute_main)
190 ctx->main_func(ctx->priv);
192 is_last = run_jobs(ctx);
212 nb_workers = ctx->nb_threads;
217 for (i = 0; i < nb_workers; i++) {
218 WorkerContext *w = &ctx->workers[i];
225 for (i = 0; i < nb_workers; i++) {
226 WorkerContext *w = &ctx->workers[i];
241 void (*
worker_func)(
void *priv,
int jobnr,
int threadnr,
int nb_jobs,
int nb_threads),
static av_unused void w32thread_init(void)
static av_always_inline int pthread_mutex_destroy(pthread_mutex_t *mutex)
#define pthread_mutex_lock(a)
static av_always_inline int pthread_cond_wait(pthread_cond_t *cond, pthread_mutex_t *mutex)
Memory handling functions.
static void worker_func(void *priv, int jobnr, int threadnr, int nb_jobs, int nb_threads)
void * av_mallocz(size_t size)
Allocate a memory block with alignment suitable for all memory accesses (including vectors if availab...
static av_always_inline int pthread_cond_destroy(pthread_cond_t *cond)
void * av_calloc(size_t nmemb, size_t size)
Non-inlined equivalent of av_mallocz_array().
int(* cond)(enum AVPixelFormat pix_fmt)
#define av_assert0(cond)
assert() equivalent, that is always enabled.
struct AVSliceThread AVSliceThread
static av_always_inline int pthread_cond_signal(pthread_cond_t *cond)
simple assert() macros that are a bit more flexible than ISO C assert().
void avpriv_slicethread_free(AVSliceThread **pctx)
Destroy slice threading context.
typedef void(APIENTRY *FF_PFNGLACTIVETEXTUREPROC)(GLenum texture)
static av_always_inline int pthread_join(pthread_t thread, void **value_ptr)
static av_always_inline int pthread_mutex_init(pthread_mutex_t *mutex, const pthread_mutexattr_t *attr)
#define pthread_mutex_unlock(a)
static pthread_mutex_t * mutex
#define atomic_fetch_add_explicit(object, operand, order)
#define attribute_align_arg
static av_always_inline int pthread_create(pthread_t *thread, const pthread_attr_t *attr, void *(*start_routine)(void *), void *arg)
int avpriv_slicethread_create(AVSliceThread **pctx, void *priv, void(*worker_func)(void *priv, int jobnr, int threadnr, int nb_jobs, int nb_threads), void(*main_func)(void *priv), int nb_threads)
Create slice threading context.
void avpriv_slicethread_execute(AVSliceThread *ctx, int nb_jobs, int execute_main)
Execute slice threading.
#define atomic_store_explicit(object, desired, order)
int( main_func)(AVCodecContext *c)
static av_always_inline int pthread_cond_init(pthread_cond_t *cond, const pthread_condattr_t *attr)
#define atomic_init(obj, value)