FFmpeg  4.4.9
slicethread.c
Go to the documentation of this file.
1 /*
2  * This file is part of FFmpeg.
3  *
4  * FFmpeg is free software; you can redistribute it and/or
5  * modify it under the terms of the GNU Lesser General Public
6  * License as published by the Free Software Foundation; either
7  * version 2.1 of the License, or (at your option) any later version.
8  *
9  * FFmpeg is distributed in the hope that it will be useful,
10  * but WITHOUT ANY WARRANTY; without even the implied warranty of
11  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
12  * Lesser General Public License for more details.
13  *
14  * You should have received a copy of the GNU Lesser General Public
15  * License along with FFmpeg; if not, write to the Free Software
16  * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
17  */
18 
19 #include <stdatomic.h>
20 #include "slicethread.h"
21 #include "mem.h"
22 #include "thread.h"
23 #include "avassert.h"
24 
25 #define MAX_AUTO_THREADS 16
26 
27 #if HAVE_PTHREADS || HAVE_W32THREADS || HAVE_OS2THREADS
28 
29 typedef struct WorkerContext {
33  pthread_t thread;
34  int done;
35 } WorkerContext;
36 
37 struct AVSliceThread {
38  WorkerContext *workers;
39  int nb_threads;
40  int nb_active_threads;
41  int nb_jobs;
42 
43  atomic_uint first_job;
44  atomic_uint current_job;
45  pthread_mutex_t done_mutex;
46  pthread_cond_t done_cond;
47  int done;
48  int finished;
49 
50  void *priv;
51  void (*worker_func)(void *priv, int jobnr, int threadnr, int nb_jobs, int nb_threads);
52  void (*main_func)(void *priv);
53 };
54 
55 static int run_jobs(AVSliceThread *ctx)
56 {
57  unsigned nb_jobs = ctx->nb_jobs;
58  unsigned nb_active_threads = ctx->nb_active_threads;
59  unsigned first_job = atomic_fetch_add_explicit(&ctx->first_job, 1, memory_order_acq_rel);
60  unsigned current_job = first_job;
61 
62  do {
63  ctx->worker_func(ctx->priv, current_job, first_job, nb_jobs, nb_active_threads);
64  } while ((current_job = atomic_fetch_add_explicit(&ctx->current_job, 1, memory_order_acq_rel)) < nb_jobs);
65 
66  return current_job == nb_jobs + nb_active_threads - 1;
67 }
68 
69 static void *attribute_align_arg thread_worker(void *v)
70 {
71  WorkerContext *w = v;
72  AVSliceThread *ctx = w->ctx;
73 
74  pthread_mutex_lock(&w->mutex);
75  pthread_cond_signal(&w->cond);
76 
77  while (1) {
78  w->done = 1;
79  while (w->done)
80  pthread_cond_wait(&w->cond, &w->mutex);
81 
82  if (ctx->finished) {
83  pthread_mutex_unlock(&w->mutex);
84  return NULL;
85  }
86 
87  if (run_jobs(ctx)) {
88  pthread_mutex_lock(&ctx->done_mutex);
89  ctx->done = 1;
90  pthread_cond_signal(&ctx->done_cond);
91  pthread_mutex_unlock(&ctx->done_mutex);
92  }
93  }
94 }
95 
96 int avpriv_slicethread_create(AVSliceThread **pctx, void *priv,
97  void (*worker_func)(void *priv, int jobnr, int threadnr, int nb_jobs, int nb_threads),
98  void (*main_func)(void *priv),
99  int nb_threads)
100 {
102  int nb_workers, i;
103  int ret;
104 
105  av_assert0(nb_threads >= 0);
106  if (!nb_threads) {
107  int nb_cpus = av_cpu_count();
108  if (nb_cpus > 1)
109  nb_threads = FFMIN(nb_cpus + 1, MAX_AUTO_THREADS);
110  else
111  nb_threads = 1;
112  }
113 
114  nb_workers = nb_threads;
115  if (!main_func)
116  nb_workers--;
117 
118  *pctx = ctx = av_mallocz(sizeof(*ctx));
119  if (!ctx)
120  return AVERROR(ENOMEM);
121 
122  if (nb_workers && !(ctx->workers = av_calloc(nb_workers, sizeof(*ctx->workers)))) {
123  av_freep(pctx);
124  return AVERROR(ENOMEM);
125  }
126 
127  ctx->priv = priv;
128  ctx->worker_func = worker_func;
129  ctx->main_func = main_func;
130  ctx->nb_threads = nb_threads;
131  ctx->nb_active_threads = 0;
132  ctx->nb_jobs = 0;
133  ctx->finished = 0;
134 
135  atomic_init(&ctx->first_job, 0);
136  atomic_init(&ctx->current_job, 0);
137  ret = pthread_mutex_init(&ctx->done_mutex, NULL);
138  if (ret) {
139  av_freep(&ctx->workers);
140  av_freep(pctx);
141  return AVERROR(ret);
142  }
143  ret = pthread_cond_init(&ctx->done_cond, NULL);
144  if (ret) {
145  ctx->nb_threads = main_func ? 0 : 1;
147  return AVERROR(ret);
148  }
149  ctx->done = 0;
150 
151  for (i = 0; i < nb_workers; i++) {
152  WorkerContext *w = &ctx->workers[i];
153  int ret;
154  w->ctx = ctx;
155  ret = pthread_mutex_init(&w->mutex, NULL);
156  if (ret) {
157  ctx->nb_threads = main_func ? i : i + 1;
159  return AVERROR(ret);
160  }
161  ret = pthread_cond_init(&w->cond, NULL);
162  if (ret) {
163  pthread_mutex_destroy(&w->mutex);
164  ctx->nb_threads = main_func ? i : i + 1;
166  return AVERROR(ret);
167  }
168  pthread_mutex_lock(&w->mutex);
169  w->done = 0;
170 
171  if (ret = pthread_create(&w->thread, NULL, thread_worker, w)) {
172  ctx->nb_threads = main_func ? i : i + 1;
173  pthread_mutex_unlock(&w->mutex);
174  pthread_cond_destroy(&w->cond);
175  pthread_mutex_destroy(&w->mutex);
177  return AVERROR(ret);
178  }
179 
180  while (!w->done)
181  pthread_cond_wait(&w->cond, &w->mutex);
182  pthread_mutex_unlock(&w->mutex);
183  }
184 
185  return nb_threads;
186 }
187 
188 void avpriv_slicethread_execute(AVSliceThread *ctx, int nb_jobs, int execute_main)
189 {
190  int nb_workers, i, is_last = 0;
191 
192  av_assert0(nb_jobs > 0);
193  ctx->nb_jobs = nb_jobs;
194  ctx->nb_active_threads = FFMIN(nb_jobs, ctx->nb_threads);
195  atomic_store_explicit(&ctx->first_job, 0, memory_order_relaxed);
196  atomic_store_explicit(&ctx->current_job, ctx->nb_active_threads, memory_order_relaxed);
197  nb_workers = ctx->nb_active_threads;
198  if (!ctx->main_func || !execute_main)
199  nb_workers--;
200 
201  for (i = 0; i < nb_workers; i++) {
202  WorkerContext *w = &ctx->workers[i];
203  pthread_mutex_lock(&w->mutex);
204  w->done = 0;
205  pthread_cond_signal(&w->cond);
206  pthread_mutex_unlock(&w->mutex);
207  }
208 
209  if (ctx->main_func && execute_main)
210  ctx->main_func(ctx->priv);
211  else
212  is_last = run_jobs(ctx);
213 
214  if (!is_last) {
215  pthread_mutex_lock(&ctx->done_mutex);
216  while (!ctx->done)
217  pthread_cond_wait(&ctx->done_cond, &ctx->done_mutex);
218  ctx->done = 0;
219  pthread_mutex_unlock(&ctx->done_mutex);
220  }
221 }
222 
224 {
226  int nb_workers, i;
227 
228  if (!pctx || !*pctx)
229  return;
230 
231  ctx = *pctx;
232  nb_workers = ctx->nb_threads;
233  if (!ctx->main_func)
234  nb_workers--;
235 
236  ctx->finished = 1;
237  for (i = 0; i < nb_workers; i++) {
238  WorkerContext *w = &ctx->workers[i];
239  pthread_mutex_lock(&w->mutex);
240  w->done = 0;
241  pthread_cond_signal(&w->cond);
242  pthread_mutex_unlock(&w->mutex);
243  }
244 
245  for (i = 0; i < nb_workers; i++) {
246  WorkerContext *w = &ctx->workers[i];
247  pthread_join(w->thread, NULL);
248  pthread_cond_destroy(&w->cond);
249  pthread_mutex_destroy(&w->mutex);
250  }
251 
252  pthread_cond_destroy(&ctx->done_cond);
253  pthread_mutex_destroy(&ctx->done_mutex);
254  av_freep(&ctx->workers);
255  av_freep(pctx);
256 }
257 
258 #else /* HAVE_PTHREADS || HAVE_W32THREADS || HAVE_OS32THREADS */
259 
261  void (*worker_func)(void *priv, int jobnr, int threadnr, int nb_jobs, int nb_threads),
262  void (*main_func)(void *priv),
263  int nb_threads)
264 {
265  *pctx = NULL;
266  return AVERROR(EINVAL);
267 }
268 
269 void avpriv_slicethread_execute(AVSliceThread *ctx, int nb_jobs, int execute_main)
270 {
271  av_assert0(0);
272 }
273 
275 {
276  av_assert0(!pctx || !*pctx);
277 }
278 
279 #endif /* HAVE_PTHREADS || HAVE_W32THREADS || HAVE_OS32THREADS */
simple assert() macros that are a bit more flexible than ISO C assert().
#define av_assert0(cond)
assert() equivalent, that is always enabled.
Definition: avassert.h:37
#define FFMIN(a, b)
Definition: common.h:105
#define NULL
Definition: coverity.c:32
int av_cpu_count(void)
Definition: cpu.c:275
#define atomic_fetch_add_explicit(object, operand, order)
Definition: stdatomic.h:149
intptr_t atomic_uint
Definition: stdatomic.h:56
#define atomic_store_explicit(object, desired, order)
Definition: stdatomic.h:90
#define atomic_init(obj, value)
Definition: stdatomic.h:33
#define pthread_mutex_lock(a)
Definition: ffprobe.c:63
#define pthread_mutex_unlock(a)
Definition: ffprobe.c:67
#define AVERROR(e)
Definition: error.h:43
void * av_mallocz(size_t size)
Allocate a memory block with alignment suitable for all memory accesses (including vectors if availab...
Definition: mem.c:237
void * av_calloc(size_t nmemb, size_t size)
Non-inlined equivalent of av_mallocz_array().
Definition: mem.c:245
int i
Definition: input.c:407
#define attribute_align_arg
Definition: internal.h:61
uint8_t w
Definition: llviddspenc.c:39
static AVMutex mutex
Definition: log.c:44
Memory handling functions.
static av_always_inline int pthread_cond_signal(pthread_cond_t *cond)
Definition: os2threads.h:152
static av_always_inline int pthread_cond_destroy(pthread_cond_t *cond)
Definition: os2threads.h:144
static av_always_inline int pthread_mutex_init(pthread_mutex_t *mutex, const pthread_mutexattr_t *attr)
Definition: os2threads.h:104
static av_always_inline int pthread_join(pthread_t thread, void **value_ptr)
Definition: os2threads.h:94
static av_always_inline int pthread_cond_init(pthread_cond_t *cond, const pthread_condattr_t *attr)
Definition: os2threads.h:133
static av_always_inline int pthread_create(pthread_t *thread, const pthread_attr_t *attr, void *(*start_routine)(void *), void *arg)
Definition: os2threads.h:80
_fmutex pthread_mutex_t
Definition: os2threads.h:53
static av_always_inline int pthread_cond_wait(pthread_cond_t *cond, pthread_mutex_t *mutex)
Definition: os2threads.h:192
static av_always_inline int pthread_mutex_destroy(pthread_mutex_t *mutex)
Definition: os2threads.h:112
int(* cond)(enum AVPixelFormat pix_fmt)
Definition: pixdesc_query.c:28
static void worker_func(void *priv, int jobnr, int threadnr, int nb_jobs, int nb_threads)
Definition: pthread_slice.c:65
int() main_func(AVCodecContext *c)
Definition: pthread_slice.c:41
typedef void(RENAME(mix_any_func_type))
void avpriv_slicethread_execute(AVSliceThread *ctx, int nb_jobs, int execute_main)
Execute slice threading.
Definition: slicethread.c:269
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.
Definition: slicethread.c:260
#define MAX_AUTO_THREADS
Definition: slicethread.c:25
void avpriv_slicethread_free(AVSliceThread **pctx)
Destroy slice threading context.
Definition: slicethread.c:274
struct AVSliceThread AVSliceThread
Definition: slicethread.h:22
#define av_freep(p)
AVFormatContext * ctx
Definition: movenc.c:48