FFmpeg
 All Data Structures Namespaces Files Functions Variables Typedefs Enumerations Enumerator Macros Groups Pages
pthread.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 /**
20  * @file
21  * Libavfilter multithreading support
22  */
23 
24 #include "config.h"
25 
26 #include "libavutil/common.h"
27 #include "libavutil/cpu.h"
28 #include "libavutil/mem.h"
29 #include "libavutil/thread.h"
30 
31 #include "avfilter.h"
32 #include "internal.h"
33 #include "thread.h"
34 
35 typedef struct ThreadContext {
37 
41 
42  /* per-execute parameters */
44  void *arg;
45  int *rets;
46  int nb_jobs;
47 
52  unsigned int current_execute;
53  int done;
55 
56 static void* attribute_align_arg worker(void *v)
57 {
58  ThreadContext *c = v;
59  int our_job = c->nb_jobs;
60  int nb_threads = c->nb_threads;
61  unsigned int last_execute = 0;
62  int ret, self_id;
63 
65  self_id = c->current_job++;
66 
67  for (;;) {
68  while (our_job >= c->nb_jobs) {
69  if (c->current_job == nb_threads + c->nb_jobs)
71 
72  while (last_execute == c->current_execute && !c->done)
74  last_execute = c->current_execute;
75  our_job = self_id;
76 
77  if (c->done) {
79  return NULL;
80  }
81  }
83 
84  ret = c->func(c->ctx, c->arg, our_job, c->nb_jobs);
85  if (c->rets)
86  c->rets[our_job % c->nb_jobs] = ret;
87 
89  our_job = c->current_job++;
90  }
91 }
92 
94 {
95  int i;
96 
98  c->done = 1;
101 
102  for (i = 0; i < c->nb_threads; i++)
103  pthread_join(c->workers[i], NULL);
104 
108  av_freep(&c->workers);
109 }
110 
112 {
113  while (c->current_job != c->nb_threads + c->nb_jobs)
116 }
117 
119  void *arg, int *ret, int nb_jobs)
120 {
121  ThreadContext *c = ctx->graph->internal->thread;
122 
123  if (nb_jobs <= 0)
124  return 0;
125 
127 
128  c->current_job = c->nb_threads;
129  c->nb_jobs = nb_jobs;
130  c->ctx = ctx;
131  c->arg = arg;
132  c->func = func;
133  c->rets = ret;
134  c->current_execute++;
135 
137 
139 
140  return 0;
141 }
142 
143 static int thread_init_internal(ThreadContext *c, int nb_threads)
144 {
145  int i, ret;
146 
147  if (!nb_threads) {
148  int nb_cpus = av_cpu_count();
149  // use number of cores + 1 as thread count if there is more than one
150  if (nb_cpus > 1)
151  nb_threads = nb_cpus + 1;
152  else
153  nb_threads = 1;
154  }
155 
156  if (nb_threads <= 1)
157  return 1;
158 
159  c->nb_threads = nb_threads;
160  c->workers = av_mallocz_array(sizeof(*c->workers), nb_threads);
161  if (!c->workers)
162  return AVERROR(ENOMEM);
163 
164  c->current_job = 0;
165  c->nb_jobs = 0;
166  c->done = 0;
167 
170 
173  for (i = 0; i < nb_threads; i++) {
174  ret = pthread_create(&c->workers[i], NULL, worker, c);
175  if (ret) {
177  c->nb_threads = i;
179  return AVERROR(ret);
180  }
181  }
182 
184 
185  return c->nb_threads;
186 }
187 
189 {
190  int ret;
191 
192 #if HAVE_W32THREADS
193  w32thread_init();
194 #endif
195 
196  if (graph->nb_threads == 1) {
197  graph->thread_type = 0;
198  return 0;
199  }
200 
201  graph->internal->thread = av_mallocz(sizeof(ThreadContext));
202  if (!graph->internal->thread)
203  return AVERROR(ENOMEM);
204 
205  ret = thread_init_internal(graph->internal->thread, graph->nb_threads);
206  if (ret <= 1) {
207  av_freep(&graph->internal->thread);
208  graph->thread_type = 0;
209  graph->nb_threads = 1;
210  return (ret < 0) ? ret : 0;
211  }
212  graph->nb_threads = ret;
213 
215 
216  return 0;
217 }
218 
220 {
221  if (graph->internal->thread)
223  av_freep(&graph->internal->thread);
224 }
static av_unused void w32thread_init(void)
Definition: w32pthreads.h:397
#define NULL
Definition: coverity.c:32
static av_always_inline int pthread_mutex_destroy(pthread_mutex_t *mutex)
Definition: os2threads.h:106
int thread_type
Type of multithreading allowed for filters in this graph.
Definition: avfilter.h:862
#define pthread_mutex_lock(a)
Definition: ffprobe.c:60
static av_always_inline int pthread_cond_wait(pthread_cond_t *cond, pthread_mutex_t *mutex)
Definition: os2threads.h:164
pthread_t * workers
Definition: pthread.c:39
int av_cpu_count(void)
Definition: cpu.c:260
Main libavfilter public API header.
Memory handling functions.
static int thread_execute(AVFilterContext *ctx, avfilter_action_func *func, void *arg, int *ret, int nb_jobs)
Definition: pthread.c:118
avfilter_action_func * func
Definition: pthread.c:40
void * av_mallocz(size_t size)
Allocate a memory block with alignment suitable for all memory accesses (including vectors if availab...
Definition: mem.c:222
static void slice_thread_park_workers(ThreadContext *c)
Definition: pthread.c:111
pthread_cond_t last_job_cond
Definition: pthread.c:48
static av_always_inline int pthread_cond_destroy(pthread_cond_t *cond)
Definition: os2threads.h:138
struct AVFilterGraph * graph
filtergraph this filter belongs to
Definition: avfilter.h:340
HMTX pthread_mutex_t
Definition: os2threads.h:49
int nb_threads
Maximum number of threads used by filters in this graph.
Definition: avfilter.h:869
static av_always_inline int pthread_cond_signal(pthread_cond_t *cond)
Definition: os2threads.h:146
int( avfilter_action_func)(AVFilterContext *ctx, void *arg, int jobnr, int nb_jobs)
A function pointer passed to the AVFilterGraph::execute callback to be executed multiple times...
Definition: avfilter.h:823
#define AVERROR(e)
Definition: error.h:43
int current_job
Definition: pthread.c:51
const char * arg
Definition: jacosubdec.c:66
int nb_threads
Definition: pthread.c:38
static void * av_mallocz_array(size_t nmemb, size_t size)
Definition: mem.h:229
void * arg
Definition: pthread.c:44
AVFilterGraph * graph
Definition: pthread.c:36
void ff_graph_thread_free(AVFilterGraph *graph)
Definition: pthread.c:219
pthread_mutex_t current_job_lock
Definition: pthread.c:50
AVFormatContext * ctx
Definition: movenc.c:48
static av_always_inline int pthread_join(pthread_t thread, void **value_ptr)
Definition: os2threads.h:88
static av_always_inline int pthread_mutex_init(pthread_mutex_t *mutex, const pthread_mutexattr_t *attr)
Definition: os2threads.h:98
#define pthread_mutex_unlock(a)
Definition: ffprobe.c:64
pthread_cond_t current_job_cond
Definition: pthread.c:49
static av_always_inline int pthread_create(pthread_t *thread, const pthread_attr_t *attr, void *(*start_routine)(void *), void *arg)
Definition: os2threads.h:74
AVFilterGraphInternal * internal
Opaque object for libavfilter internal use.
Definition: avfilter.h:874
static void slice_thread_uninit(ThreadContext *c)
Definition: pthread.c:93
int(* func)(AVBPrint *dst, const char *in, const char *arg)
Definition: jacosubdec.c:67
static int thread_init_internal(ThreadContext *c, int nb_threads)
Definition: pthread.c:143
static void *attribute_align_arg worker(void *v)
Definition: pthread.c:56
common internal and external API header
unsigned int current_execute
Definition: pthread.c:52
static double c[64]
static av_always_inline int pthread_cond_init(pthread_cond_t *cond, const pthread_condattr_t *attr)
Definition: os2threads.h:127
int nb_jobs
Definition: pthread.c:46
avfilter_execute_func * thread_execute
Definition: internal.h:150
static av_always_inline int pthread_cond_broadcast(pthread_cond_t *cond)
Definition: os2threads.h:156
int ff_graph_thread_init(AVFilterGraph *graph)
Definition: pthread.c:188
An instance of a filter.
Definition: avfilter.h:323
int * rets
Definition: pthread.c:45
#define av_freep(p)
internal API functions
AVFilterContext * ctx
Definition: pthread.c:43