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  *
3  * This file is part of FFmpeg.
4  *
5  * FFmpeg is free software; you can redistribute it and/or
6  * modify it under the terms of the GNU Lesser General Public
7  * License as published by the Free Software Foundation; either
8  * version 2.1 of the License, or (at your option) any later version.
9  *
10  * FFmpeg is distributed in the hope that it will be useful,
11  * but WITHOUT ANY WARRANTY; without even the implied warranty of
12  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13  * Lesser General Public License for more details.
14  *
15  * You should have received a copy of the GNU Lesser General Public
16  * License along with FFmpeg; if not, write to the Free Software
17  * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
18  */
19 
20 /**
21  * @file
22  * Libavfilter multithreading support
23  */
24 
25 #include "config.h"
26 
27 #include "libavutil/common.h"
28 #include "libavutil/cpu.h"
29 #include "libavutil/mem.h"
30 #include "libavutil/thread.h"
31 
32 #include "avfilter.h"
33 #include "internal.h"
34 #include "thread.h"
35 
36 typedef struct ThreadContext {
38 
42 
43  /* per-execute parameters */
45  void *arg;
46  int *rets;
47  int nb_rets;
48  int nb_jobs;
49 
54  unsigned int current_execute;
55  int done;
57 
58 static void* attribute_align_arg worker(void *v)
59 {
60  ThreadContext *c = v;
61  int our_job = c->nb_jobs;
62  int nb_threads = c->nb_threads;
63  unsigned int last_execute = 0;
64  int self_id;
65 
67  self_id = c->current_job++;
68  for (;;) {
69  while (our_job >= c->nb_jobs) {
70  if (c->current_job == nb_threads + c->nb_jobs)
72 
73  while (last_execute == c->current_execute && !c->done)
75  last_execute = c->current_execute;
76  our_job = self_id;
77 
78  if (c->done) {
80  return NULL;
81  }
82  }
84 
85  c->rets[our_job % c->nb_rets] = c->func(c->ctx, c->arg, our_job, c->nb_jobs);
86 
88  our_job = c->current_job++;
89  }
90 }
91 
93 {
94  int i;
95 
97  c->done = 1;
100 
101  for (i = 0; i < c->nb_threads; i++)
102  pthread_join(c->workers[i], NULL);
103 
107  av_freep(&c->workers);
108 }
109 
111 {
112  while (c->current_job != c->nb_threads + c->nb_jobs)
115 }
116 
118  void *arg, int *ret, int nb_jobs)
119 {
120  ThreadContext *c = ctx->graph->internal->thread;
121  int dummy_ret;
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  if (ret) {
134  c->rets = ret;
135  c->nb_rets = nb_jobs;
136  } else {
137  c->rets = &dummy_ret;
138  c->nb_rets = 1;
139  }
140  c->current_execute++;
141 
143 
145 
146  return 0;
147 }
148 
149 static int thread_init_internal(ThreadContext *c, int nb_threads)
150 {
151  int i, ret;
152 
153  if (!nb_threads) {
154  int nb_cpus = av_cpu_count();
155  // use number of cores + 1 as thread count if there is more than one
156  if (nb_cpus > 1)
157  nb_threads = nb_cpus + 1;
158  else
159  nb_threads = 1;
160  }
161 
162  if (nb_threads <= 1)
163  return 1;
164 
165  c->nb_threads = nb_threads;
166  c->workers = av_mallocz_array(sizeof(*c->workers), nb_threads);
167  if (!c->workers)
168  return AVERROR(ENOMEM);
169 
170  c->current_job = 0;
171  c->nb_jobs = 0;
172  c->done = 0;
173 
176 
179  for (i = 0; i < nb_threads; i++) {
180  ret = pthread_create(&c->workers[i], NULL, worker, c);
181  if (ret) {
183  c->nb_threads = i;
185  return AVERROR(ret);
186  }
187  }
188 
190 
191  return c->nb_threads;
192 }
193 
195 {
196  int ret;
197 
198 #if HAVE_W32THREADS
199  w32thread_init();
200 #endif
201 
202  if (graph->nb_threads == 1) {
203  graph->thread_type = 0;
204  return 0;
205  }
206 
207  graph->internal->thread = av_mallocz(sizeof(ThreadContext));
208  if (!graph->internal->thread)
209  return AVERROR(ENOMEM);
210 
211  ret = thread_init_internal(graph->internal->thread, graph->nb_threads);
212  if (ret <= 1) {
213  av_freep(&graph->internal->thread);
214  graph->thread_type = 0;
215  graph->nb_threads = 1;
216  return (ret < 0) ? ret : 0;
217  }
218  graph->nb_threads = ret;
219 
221 
222  return 0;
223 }
224 
226 {
227  if (graph->internal->thread)
229  av_freep(&graph->internal->thread);
230 }
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:781
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:40
int av_cpu_count(void)
Definition: cpu.c:256
AVFormatContext * ctx
Definition: movenc-test.c:48
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:117
avfilter_action_func * func
Definition: pthread.c:41
static void slice_thread_park_workers(ThreadContext *c)
Definition: pthread.c:110
pthread_cond_t last_job_cond
Definition: pthread.c:50
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:321
HMTX pthread_mutex_t
Definition: os2threads.h:49
int nb_threads
Maximum number of threads used by filters in this graph.
Definition: avfilter.h:788
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:744
#define AVERROR(e)
Definition: error.h:43
int current_job
Definition: pthread.c:53
const char * arg
Definition: jacosubdec.c:66
int nb_threads
Definition: pthread.c:39
void * arg
Definition: pthread.c:45
AVFilterGraph * graph
Definition: pthread.c:37
void ff_graph_thread_free(AVFilterGraph *graph)
Definition: pthread.c:225
pthread_mutex_t current_job_lock
Definition: pthread.c:52
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
pthread_cond_t current_job_cond
Definition: pthread.c:51
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:793
static void slice_thread_uninit(ThreadContext *c)
Definition: pthread.c:92
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:149
static void *attribute_align_arg worker(void *v)
Definition: pthread.c:58
common internal and external API header
unsigned int current_execute
Definition: pthread.c:54
int nb_rets
Definition: pthread.c:47
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:48
avfilter_execute_func * thread_execute
Definition: internal.h:149
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:194
static av_always_inline int pthread_mutex_unlock(pthread_mutex_t *mutex)
Definition: os2threads.h:120
An instance of a filter.
Definition: avfilter.h:304
static void * av_mallocz_array(size_t nmemb, size_t size)
Definition: mem.h:229
int * rets
Definition: pthread.c:46
#define av_freep(p)
static av_always_inline int pthread_mutex_lock(pthread_mutex_t *mutex)
Definition: os2threads.h:113
internal API functions
AVFilterContext * ctx
Definition: pthread.c:44
void * av_mallocz(size_t size)
Allocate a block of size bytes with alignment suitable for all memory accesses (including vectors if ...
Definition: mem.c:252