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 
31 #include "avfilter.h"
32 #include "internal.h"
33 #include "thread.h"
34 
35 #if HAVE_PTHREADS
36 #include <pthread.h>
37 #elif HAVE_OS2THREADS
38 #include "compat/os2threads.h"
39 #elif HAVE_W32THREADS
40 #include "compat/w32pthreads.h"
41 #endif
42 
43 typedef struct ThreadContext {
45 
49 
50  /* per-execute perameters */
52  void *arg;
53  int *rets;
54  int nb_rets;
55  int nb_jobs;
56 
61  unsigned int current_execute;
62  int done;
64 
65 static void* attribute_align_arg worker(void *v)
66 {
67  ThreadContext *c = v;
68  int our_job = c->nb_jobs;
69  int nb_threads = c->nb_threads;
70  unsigned int last_execute = 0;
71  int self_id;
72 
74  self_id = c->current_job++;
75  for (;;) {
76  while (our_job >= c->nb_jobs) {
77  if (c->current_job == nb_threads + c->nb_jobs)
79 
80  while (last_execute == c->current_execute && !c->done)
82  last_execute = c->current_execute;
83  our_job = self_id;
84 
85  if (c->done) {
87  return NULL;
88  }
89  }
91 
92  c->rets[our_job % c->nb_rets] = c->func(c->ctx, c->arg, our_job, c->nb_jobs);
93 
95  our_job = c->current_job++;
96  }
97 }
98 
100 {
101  int i;
102 
104  c->done = 1;
107 
108  for (i = 0; i < c->nb_threads; i++)
109  pthread_join(c->workers[i], NULL);
110 
114  av_freep(&c->workers);
115 }
116 
118 {
119  while (c->current_job != c->nb_threads + c->nb_jobs)
122 }
123 
125  void *arg, int *ret, int nb_jobs)
126 {
127  ThreadContext *c = ctx->graph->internal->thread;
128  int dummy_ret;
129 
130  if (nb_jobs <= 0)
131  return 0;
132 
134 
135  c->current_job = c->nb_threads;
136  c->nb_jobs = nb_jobs;
137  c->ctx = ctx;
138  c->arg = arg;
139  c->func = func;
140  if (ret) {
141  c->rets = ret;
142  c->nb_rets = nb_jobs;
143  } else {
144  c->rets = &dummy_ret;
145  c->nb_rets = 1;
146  }
147  c->current_execute++;
148 
150 
152 
153  return 0;
154 }
155 
156 static int thread_init_internal(ThreadContext *c, int nb_threads)
157 {
158  int i, ret;
159 
160  if (!nb_threads) {
161  int nb_cpus = av_cpu_count();
162  // use number of cores + 1 as thread count if there is more than one
163  if (nb_cpus > 1)
164  nb_threads = nb_cpus + 1;
165  else
166  nb_threads = 1;
167  }
168 
169  if (nb_threads <= 1)
170  return 1;
171 
172  c->nb_threads = nb_threads;
173  c->workers = av_mallocz_array(sizeof(*c->workers), nb_threads);
174  if (!c->workers)
175  return AVERROR(ENOMEM);
176 
177  c->current_job = 0;
178  c->nb_jobs = 0;
179  c->done = 0;
180 
183 
186  for (i = 0; i < nb_threads; i++) {
187  ret = pthread_create(&c->workers[i], NULL, worker, c);
188  if (ret) {
190  c->nb_threads = i;
192  return AVERROR(ret);
193  }
194  }
195 
197 
198  return c->nb_threads;
199 }
200 
202 {
203  int ret;
204 
205 #if HAVE_W32THREADS
206  w32thread_init();
207 #endif
208 
209  if (graph->nb_threads == 1) {
210  graph->thread_type = 0;
211  return 0;
212  }
213 
214  graph->internal->thread = av_mallocz(sizeof(ThreadContext));
215  if (!graph->internal->thread)
216  return AVERROR(ENOMEM);
217 
218  ret = thread_init_internal(graph->internal->thread, graph->nb_threads);
219  if (ret <= 1) {
220  av_freep(&graph->internal->thread);
221  graph->thread_type = 0;
222  graph->nb_threads = 1;
223  return (ret < 0) ? ret : 0;
224  }
225  graph->nb_threads = ret;
226 
228 
229  return 0;
230 }
231 
233 {
234  if (graph->internal->thread)
236  av_freep(&graph->internal->thread);
237 }
static av_unused void w32thread_init(void)
Definition: w32pthreads.h:299
#define NULL
Definition: coverity.c:32
float v
static av_always_inline int pthread_mutex_destroy(pthread_mutex_t *mutex)
Definition: os2threads.h:94
int thread_type
Type of multithreading allowed for filters in this graph.
Definition: avfilter.h:1196
static av_always_inline int pthread_cond_wait(pthread_cond_t *cond, pthread_mutex_t *mutex)
Definition: os2threads.h:153
pthread_t * workers
Definition: pthread.c:47
int av_cpu_count(void)
Definition: cpu.c:251
Main libavfilter public API header.
memory handling functions
os2threads to pthreads wrapper
static int thread_execute(AVFilterContext *ctx, avfilter_action_func *func, void *arg, int *ret, int nb_jobs)
Definition: pthread.c:124
avfilter_action_func * func
Definition: pthread.c:48
static void slice_thread_park_workers(ThreadContext *c)
Definition: pthread.c:117
pthread_cond_t last_job_cond
Definition: pthread.c:57
static av_always_inline int pthread_cond_destroy(pthread_cond_t *cond)
Definition: os2threads.h:124
struct AVFilterGraph * graph
filtergraph this filter belongs to
Definition: avfilter.h:656
HMTX pthread_mutex_t
Definition: os2threads.h:40
int nb_threads
Maximum number of threads used by filters in this graph.
Definition: avfilter.h:1203
static av_always_inline int pthread_cond_signal(pthread_cond_t *cond)
Definition: os2threads.h:131
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:1150
#define AVERROR(e)
Definition: error.h:43
int current_job
Definition: pthread.c:60
const char * arg
Definition: jacosubdec.c:66
int nb_threads
Definition: pthread.c:46
void * arg
Definition: pthread.c:52
AVFilterGraph * graph
Definition: pthread.c:44
void ff_graph_thread_free(AVFilterGraph *graph)
Definition: pthread.c:232
ret
Definition: avfilter.c:974
pthread_mutex_t current_job_lock
Definition: pthread.c:59
static av_always_inline int pthread_join(pthread_t thread, void **value_ptr)
Definition: os2threads.h:80
static av_always_inline int pthread_mutex_init(pthread_mutex_t *mutex, const pthread_mutexattr_t *attr)
Definition: os2threads.h:87
pthread_cond_t current_job_cond
Definition: pthread.c:58
static av_always_inline int pthread_create(pthread_t *thread, const pthread_attr_t *attr, void *(*start_routine)(void *), void *arg)
Definition: os2threads.h:64
AVFilterGraphInternal * internal
Opaque object for libavfilter internal use.
Definition: avfilter.h:1208
static void slice_thread_uninit(ThreadContext *c)
Definition: pthread.c:99
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:156
static void *attribute_align_arg worker(void *v)
Definition: pthread.c:65
common internal and external API header
unsigned int current_execute
Definition: pthread.c:61
int nb_rets
Definition: pthread.c:54
static double c[64]
static av_always_inline int pthread_cond_init(pthread_cond_t *cond, const pthread_condattr_t *attr)
Definition: os2threads.h:115
int nb_jobs
Definition: pthread.c:55
avfilter_execute_func * thread_execute
Definition: internal.h:158
static av_always_inline int pthread_cond_broadcast(pthread_cond_t *cond)
Definition: os2threads.h:142
int ff_graph_thread_init(AVFilterGraph *graph)
Definition: pthread.c:201
static av_always_inline int pthread_mutex_unlock(pthread_mutex_t *mutex)
Definition: os2threads.h:108
w32threads to pthreads wrapper
An instance of a filter.
Definition: avfilter.h:633
static void * av_mallocz_array(size_t nmemb, size_t size)
Definition: mem.h:228
int * rets
Definition: pthread.c:53
#define av_freep(p)
static av_always_inline int pthread_mutex_lock(pthread_mutex_t *mutex)
Definition: os2threads.h:101
internal API functions
AVFilterContext * ctx
Definition: pthread.c:51
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:250