Libav
pthread.c
Go to the documentation of this file.
1 /*
2  *
3  * This file is part of Libav.
4  *
5  * Libav 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  * Libav 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 Libav; if not, write to the Free Software
17  * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
18  */
19 
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_W32THREADS
38 #include "compat/w32pthreads.h"
39 #endif
40 
41 typedef struct ThreadContext {
43 
47 
48  /* per-execute perameters */
50  void *arg;
51  int *rets;
52  int nb_rets;
53  int nb_jobs;
54 
59  unsigned int current_execute;
60  int done;
62 
63 static void* attribute_align_arg worker(void *v)
64 {
65  ThreadContext *c = v;
66  int our_job = c->nb_jobs;
67  int nb_threads = c->nb_threads;
68  unsigned int last_execute = 0;
69  int self_id;
70 
72  self_id = c->current_job++;
73  for (;;) {
74  while (our_job >= c->nb_jobs) {
75  if (c->current_job == nb_threads + c->nb_jobs)
77 
78  while (last_execute == c->current_execute && !c->done)
80  last_execute = c->current_execute;
81  our_job = self_id;
82 
83  if (c->done) {
85  return NULL;
86  }
87  }
89 
90  c->rets[our_job % c->nb_rets] = c->func(c->ctx, c->arg, our_job, c->nb_jobs);
91 
93  our_job = c->current_job++;
94  }
95 }
96 
98 {
99  int i;
100 
102  c->done = 1;
105 
106  for (i = 0; i < c->nb_threads; i++)
107  pthread_join(c->workers[i], NULL);
108 
112  av_freep(&c->workers);
113 }
114 
116 {
117  while (c->current_job != c->nb_threads + c->nb_jobs)
120 }
121 
123  void *arg, int *ret, int nb_jobs)
124 {
125  ThreadContext *c = ctx->graph->internal->thread;
126  int dummy_ret;
127 
128  if (nb_jobs <= 0)
129  return 0;
130 
132 
133  c->current_job = c->nb_threads;
134  c->nb_jobs = nb_jobs;
135  c->ctx = ctx;
136  c->arg = arg;
137  c->func = func;
138  if (ret) {
139  c->rets = ret;
140  c->nb_rets = nb_jobs;
141  } else {
142  c->rets = &dummy_ret;
143  c->nb_rets = 1;
144  }
145  c->current_execute++;
146 
148 
150 
151  return 0;
152 }
153 
155 {
156  int i, ret;
157 
158  if (!nb_threads) {
159  int nb_cpus = av_cpu_count();
160  av_log(c->graph, AV_LOG_DEBUG, "Detected %d logical cores.\n", nb_cpus);
161  // use number of cores + 1 as thread count if there is more than one
162  if (nb_cpus > 1)
163  nb_threads = nb_cpus + 1;
164  else
165  nb_threads = 1;
166  }
167 
168  if (nb_threads <= 1)
169  return 1;
170 
171  c->nb_threads = nb_threads;
172  c->workers = av_mallocz(sizeof(*c->workers) * nb_threads);
173  if (!c->workers)
174  return AVERROR(ENOMEM);
175 
176  c->current_job = 0;
177  c->nb_jobs = 0;
178  c->done = 0;
179 
182 
185  for (i = 0; i < nb_threads; i++) {
186  ret = pthread_create(&c->workers[i], NULL, worker, c);
187  if (ret) {
189  c->nb_threads = i;
191  return AVERROR(ret);
192  }
193  }
194 
196 
197  return c->nb_threads;
198 }
199 
201 {
202  int ret;
203 
204 #if HAVE_W32THREADS
205  w32thread_init();
206 #endif
207 
208  if (graph->nb_threads == 1) {
209  graph->thread_type = 0;
210  return 0;
211  }
212 
213  graph->internal->thread = av_mallocz(sizeof(ThreadContext));
214  if (!graph->internal->thread)
215  return AVERROR(ENOMEM);
216 
217  ret = thread_init_internal(graph->internal->thread, graph->nb_threads);
218  if (ret <= 1) {
219  av_freep(&graph->internal->thread);
220  graph->thread_type = 0;
221  graph->nb_threads = 1;
222  return (ret < 0) ? ret : 0;
223  }
224  graph->nb_threads = ret;
225 
227 
228  return 0;
229 }
230 
232 {
233  if (graph->internal->thread)
235  av_freep(&graph->internal->thread);
236 }
static av_unused void pthread_cond_signal(pthread_cond_t *cond)
Definition: w32pthreads.h:252
static av_unused void w32thread_init(void)
Definition: w32pthreads.h:277
int thread_type
Type of multithreading allowed for filters in this graph.
Definition: avfilter.h:969
pthread_t * workers
Definition: pthread.c:45
int av_cpu_count(void)
Definition: cpu.c:151
static av_unused void pthread_cond_init(pthread_cond_t *cond, const void *unused_attr)
Definition: w32pthreads.h:149
Main libavfilter public API header.
memory handling functions
static av_unused void pthread_cond_broadcast(pthread_cond_t *cond)
Definition: w32pthreads.h:189
static int thread_execute(AVFilterContext *ctx, avfilter_action_func *func, void *arg, int *ret, int nb_jobs)
Definition: pthread.c:122
avfilter_action_func * func
Definition: pthread.c:46
static void slice_thread_park_workers(ThreadContext *c)
Definition: pthread.c:115
pthread_cond_t last_job_cond
Definition: pthread.c:55
void av_freep(void *arg)
Free a memory block which has been allocated with av_malloc(z)() or av_realloc() and set the pointer ...
Definition: mem.c:198
struct AVFilterGraph * graph
filtergraph this filter belongs to
Definition: avfilter.h:586
CRITICAL_SECTION pthread_mutex_t
Definition: w32pthreads.h:55
static av_unused int pthread_create(pthread_t *thread, const void *unused_attr, void *(*start_routine)(void *), void *arg)
Definition: w32pthreads.h:97
int nb_threads
Maximum number of threads used by filters in this graph.
Definition: avfilter.h:976
#define AVERROR(e)
Definition: error.h:43
static int pthread_mutex_init(pthread_mutex_t *m, void *attr)
Definition: w32pthreads.h:117
static int pthread_mutex_unlock(pthread_mutex_t *m)
Definition: w32pthreads.h:132
#define AV_LOG_DEBUG
Stuff which is only useful for libav* developers.
Definition: log.h:144
int current_job
Definition: pthread.c:58
void av_log(void *avcl, int level, const char *fmt,...)
Definition: log.c:169
int nb_threads
Definition: pthread.c:44
static av_unused void pthread_join(pthread_t thread, void **value_ptr)
Definition: w32pthreads.h:107
void * arg
Definition: pthread.c:50
AVFilterGraph * graph
Definition: pthread.c:42
void ff_graph_thread_free(AVFilterGraph *graph)
Definition: pthread.c:231
static int pthread_mutex_destroy(pthread_mutex_t *m)
Definition: w32pthreads.h:122
static av_unused void pthread_cond_destroy(pthread_cond_t *cond)
Definition: w32pthreads.h:173
static av_unused int pthread_cond_wait(pthread_cond_t *cond, pthread_mutex_t *mutex)
Definition: w32pthreads.h:220
pthread_mutex_t current_job_lock
Definition: pthread.c:57
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:923
pthread_cond_t current_job_cond
Definition: pthread.c:56
#define attribute_align_arg
Definition: internal.h:54
NULL
Definition: eval.c:55
static int pthread_mutex_lock(pthread_mutex_t *m)
Definition: w32pthreads.h:127
AVFilterGraphInternal * internal
Opaque object for libavfilter internal use.
Definition: avfilter.h:981
static void slice_thread_uninit(ThreadContext *c)
Definition: pthread.c:97
static int thread_init_internal(ThreadContext *c, int nb_threads)
Definition: pthread.c:154
static void *attribute_align_arg worker(void *v)
Definition: pthread.c:63
common internal and external API header
unsigned int current_execute
Definition: pthread.c:59
int nb_rets
Definition: pthread.c:52
int nb_jobs
Definition: pthread.c:53
avfilter_execute_func * thread_execute
Definition: internal.h:133
int ff_graph_thread_init(AVFilterGraph *graph)
Definition: pthread.c:200
w32threads to pthreads wrapper
An instance of a filter.
Definition: avfilter.h:563
int * rets
Definition: pthread.c:51
internal API functions
AVFilterContext * ctx
Definition: pthread.c:49
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:205