Libav
pthread.c
Go to the documentation of this file.
1 /*
2  * This file is part of Libav.
3  *
4  * Libav 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  * Libav 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 Libav; if not, write to the Free Software
16  * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
17  */
18 
24 #include "config.h"
25 
26 #include "libavutil/common.h"
27 #include "libavutil/cpu.h"
28 #include "libavutil/mem.h"
29 
30 #include "avfilter.h"
31 #include "internal.h"
32 #include "thread.h"
33 
34 #if HAVE_PTHREADS
35 #include <pthread.h>
36 #elif HAVE_W32THREADS
37 #include "compat/w32pthreads.h"
38 #endif
39 
40 typedef struct ThreadContext {
42 
46 
47  /* per-execute parameters */
49  void *arg;
50  int *rets;
51  int nb_rets;
52  int nb_jobs;
53 
58  unsigned int current_execute;
59  int done;
61 
62 static void* attribute_align_arg worker(void *v)
63 {
64  ThreadContext *c = v;
65  int our_job = c->nb_jobs;
66  int nb_threads = c->nb_threads;
67  unsigned int last_execute = 0;
68  int self_id;
69 
71  self_id = c->current_job++;
72  for (;;) {
73  while (our_job >= c->nb_jobs) {
74  if (c->current_job == nb_threads + c->nb_jobs)
76 
77  while (last_execute == c->current_execute && !c->done)
79  last_execute = c->current_execute;
80  our_job = self_id;
81 
82  if (c->done) {
84  return NULL;
85  }
86  }
88 
89  c->rets[our_job % c->nb_rets] = c->func(c->ctx, c->arg, our_job, c->nb_jobs);
90 
92  our_job = c->current_job++;
93  }
94 }
95 
97 {
98  int i;
99 
101  c->done = 1;
104 
105  for (i = 0; i < c->nb_threads; i++)
106  pthread_join(c->workers[i], NULL);
107 
111  av_freep(&c->workers);
112 }
113 
115 {
116  while (c->current_job != c->nb_threads + c->nb_jobs)
119 }
120 
122  void *arg, int *ret, int nb_jobs)
123 {
124  ThreadContext *c = ctx->graph->internal->thread;
125  int dummy_ret;
126 
127  if (nb_jobs <= 0)
128  return 0;
129 
131 
132  c->current_job = c->nb_threads;
133  c->nb_jobs = nb_jobs;
134  c->ctx = ctx;
135  c->arg = arg;
136  c->func = func;
137  if (ret) {
138  c->rets = ret;
139  c->nb_rets = nb_jobs;
140  } else {
141  c->rets = &dummy_ret;
142  c->nb_rets = 1;
143  }
144  c->current_execute++;
145 
147 
149 
150  return 0;
151 }
152 
154 {
155  int i, ret;
156 
157  if (!nb_threads) {
158  int nb_cpus = av_cpu_count();
159  av_log(c->graph, AV_LOG_DEBUG, "Detected %d logical cores.\n", nb_cpus);
160  // use number of cores + 1 as thread count if there is more than one
161  if (nb_cpus > 1)
162  nb_threads = nb_cpus + 1;
163  else
164  nb_threads = 1;
165  }
166 
167  if (nb_threads <= 1)
168  return 1;
169 
170  c->nb_threads = nb_threads;
171  c->workers = av_mallocz(sizeof(*c->workers) * nb_threads);
172  if (!c->workers)
173  return AVERROR(ENOMEM);
174 
175  c->current_job = 0;
176  c->nb_jobs = 0;
177  c->done = 0;
178 
181 
184  for (i = 0; i < nb_threads; i++) {
185  ret = pthread_create(&c->workers[i], NULL, worker, c);
186  if (ret) {
188  c->nb_threads = i;
190  return AVERROR(ret);
191  }
192  }
193 
195 
196  return c->nb_threads;
197 }
198 
200 {
201  int ret;
202 
203 #if HAVE_W32THREADS
204  w32thread_init();
205 #endif
206 
207  if (graph->nb_threads == 1) {
208  graph->thread_type = 0;
209  return 0;
210  }
211 
212  graph->internal->thread = av_mallocz(sizeof(ThreadContext));
213  if (!graph->internal->thread)
214  return AVERROR(ENOMEM);
215 
216  ret = thread_init_internal(graph->internal->thread, graph->nb_threads);
217  if (ret <= 1) {
218  av_freep(&graph->internal->thread);
219  graph->thread_type = 0;
220  graph->nb_threads = 1;
221  return (ret < 0) ? ret : 0;
222  }
223  graph->nb_threads = ret;
224 
226 
227  return 0;
228 }
229 
231 {
232  if (graph->internal->thread)
234  av_freep(&graph->internal->thread);
235 }
static av_unused void pthread_cond_signal(pthread_cond_t *cond)
Definition: w32pthreads.h:354
static av_unused void w32thread_init(void)
Definition: w32pthreads.h:380
int thread_type
Type of multithreading allowed for filters in this graph.
Definition: avfilter.h:623
pthread_t * workers
Definition: pthread.c:44
int av_cpu_count(void)
Definition: cpu.c:153
static av_unused void pthread_cond_init(pthread_cond_t *cond, const void *unused_attr)
Definition: w32pthreads.h:248
Main libavfilter public API header.
memory handling functions
static av_unused void pthread_cond_broadcast(pthread_cond_t *cond)
Definition: w32pthreads.h:291
av_log(ac->avr, AV_LOG_TRACE, "%d samples - audio_convert: %s to %s (%s)\, len, av_get_sample_fmt_name(ac->in_fmt), av_get_sample_fmt_name(ac->out_fmt), use_generic ? ac->func_descr_generic :ac->func_descr)
static int thread_execute(AVFilterContext *ctx, avfilter_action_func *func, void *arg, int *ret, int nb_jobs)
Definition: pthread.c:121
avfilter_action_func * func
Definition: pthread.c:45
static void slice_thread_park_workers(ThreadContext *c)
Definition: pthread.c:114
pthread_cond_t last_job_cond
Definition: pthread.c:54
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:202
struct AVFilterGraph * graph
filtergraph this filter belongs to
Definition: avfilter.h:279
CRITICAL_SECTION pthread_mutex_t
Definition: w32pthreads.h:60
static av_unused int pthread_create(pthread_t *thread, const void *unused_attr, void *(*start_routine)(void *), void *arg)
Definition: w32pthreads.h:84
int nb_threads
Maximum number of threads used by filters in this graph.
Definition: avfilter.h:630
#define AVERROR(e)
Definition: error.h:43
static int pthread_mutex_init(pthread_mutex_t *m, void *attr)
Definition: w32pthreads.h:104
static int pthread_mutex_unlock(pthread_mutex_t *m)
Definition: w32pthreads.h:119
#define AV_LOG_DEBUG
Stuff which is only useful for libav* developers.
Definition: log.h:145
int current_job
Definition: pthread.c:57
int nb_threads
Definition: pthread.c:43
static av_unused void pthread_join(pthread_t thread, void **value_ptr)
Definition: w32pthreads.h:94
void * arg
Definition: pthread.c:49
AVFilterGraph * graph
Definition: pthread.c:41
void ff_graph_thread_free(AVFilterGraph *graph)
Definition: pthread.c:230
static int pthread_mutex_destroy(pthread_mutex_t *m)
Definition: w32pthreads.h:109
static av_unused void pthread_cond_destroy(pthread_cond_t *cond)
Definition: w32pthreads.h:275
static av_unused int pthread_cond_wait(pthread_cond_t *cond, pthread_mutex_t *mutex)
Definition: w32pthreads.h:322
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:586
pthread_mutex_t current_job_lock
Definition: pthread.c:56
pthread_cond_t current_job_cond
Definition: pthread.c:55
#define attribute_align_arg
Definition: internal.h:55
NULL
Definition: eval.c:55
static int pthread_mutex_lock(pthread_mutex_t *m)
Definition: w32pthreads.h:114
AVFilterGraphInternal * internal
Opaque object for libavfilter internal use.
Definition: avfilter.h:635
static void slice_thread_uninit(ThreadContext *c)
Definition: pthread.c:96
static int thread_init_internal(ThreadContext *c, int nb_threads)
Definition: pthread.c:153
static void *attribute_align_arg worker(void *v)
Definition: pthread.c:62
common internal and external API header
unsigned int current_execute
Definition: pthread.c:58
int nb_rets
Definition: pthread.c:51
int nb_jobs
Definition: pthread.c:52
avfilter_execute_func * thread_execute
Definition: internal.h:131
int ff_graph_thread_init(AVFilterGraph *graph)
Definition: pthread.c:199
w32threads to pthreads wrapper
An instance of a filter.
Definition: avfilter.h:262
int * rets
Definition: pthread.c:50
internal API functions
AVFilterContext * ctx
Definition: pthread.c:48
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:211