Libav
pthread_slice.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 
25 #include "config.h"
26 
27 #if HAVE_PTHREADS
28 #include <pthread.h>
29 #elif HAVE_W32THREADS
30 #include "compat/w32pthreads.h"
31 #endif
32 
33 #include "avcodec.h"
34 #include "internal.h"
35 #include "pthread_internal.h"
36 #include "thread.h"
37 
38 #include "libavutil/common.h"
39 #include "libavutil/cpu.h"
40 #include "libavutil/mem.h"
41 
42 typedef int (action_func)(AVCodecContext *c, void *arg);
43 typedef int (action_func2)(AVCodecContext *c, void *arg, int jobnr, int threadnr);
44 
45 typedef struct SliceThreadContext {
49  void *args;
50  int *rets;
52  int job_count;
53  int job_size;
54 
58  unsigned current_execute;
60  int done;
62 
63 static void* attribute_align_arg worker(void *v)
64 {
65  AVCodecContext *avctx = v;
67  unsigned last_execute = 0;
68  int our_job = c->job_count;
69  int thread_count = avctx->thread_count;
70  int self_id;
71 
73  self_id = c->current_job++;
74  for (;;){
75  while (our_job >= c->job_count) {
76  if (c->current_job == thread_count + c->job_count)
78 
79  while (last_execute == c->current_execute && !c->done)
81  last_execute = c->current_execute;
82  our_job = self_id;
83 
84  if (c->done) {
86  return NULL;
87  }
88  }
90 
91  c->rets[our_job%c->rets_count] = c->func ? c->func(avctx, (char*)c->args + our_job*c->job_size):
92  c->func2(avctx, c->args, our_job, self_id);
93 
95  our_job = c->current_job++;
96  }
97 }
98 
100 {
102  int i;
103 
105  c->done = 1;
108 
109  for (i=0; i<avctx->thread_count; i++)
110  pthread_join(c->workers[i], NULL);
111 
115  av_free(c->workers);
116  av_freep(&avctx->internal->thread_ctx);
117 }
118 
119 static av_always_inline void thread_park_workers(SliceThreadContext *c, int thread_count)
120 {
121  while (c->current_job != thread_count + c->job_count)
124 }
125 
126 static int thread_execute(AVCodecContext *avctx, action_func* func, void *arg, int *ret, int job_count, int job_size)
127 {
129  int dummy_ret;
130 
131  if (!(avctx->active_thread_type&FF_THREAD_SLICE) || avctx->thread_count <= 1)
132  return avcodec_default_execute(avctx, func, arg, ret, job_count, job_size);
133 
134  if (job_count <= 0)
135  return 0;
136 
138 
139  c->current_job = avctx->thread_count;
140  c->job_count = job_count;
141  c->job_size = job_size;
142  c->args = arg;
143  c->func = func;
144  if (ret) {
145  c->rets = ret;
146  c->rets_count = job_count;
147  } else {
148  c->rets = &dummy_ret;
149  c->rets_count = 1;
150  }
151  c->current_execute++;
153 
155 
156  return 0;
157 }
158 
159 static int thread_execute2(AVCodecContext *avctx, action_func2* func2, void *arg, int *ret, int job_count)
160 {
162  c->func2 = func2;
163  return thread_execute(avctx, NULL, arg, ret, job_count, 0);
164 }
165 
167 {
168  int i;
170  int thread_count = avctx->thread_count;
171 
172 #if HAVE_W32THREADS
173  w32thread_init();
174 #endif
175 
176  if (!thread_count) {
177  int nb_cpus = av_cpu_count();
178  av_log(avctx, AV_LOG_DEBUG, "detected %d logical cores\n", nb_cpus);
179  // use number of cores + 1 as thread count if there is more than one
180  if (nb_cpus > 1)
181  thread_count = avctx->thread_count = FFMIN(nb_cpus + 1, MAX_AUTO_THREADS);
182  else
183  thread_count = avctx->thread_count = 1;
184  }
185 
186  if (thread_count <= 1) {
187  avctx->active_thread_type = 0;
188  return 0;
189  }
190 
191  c = av_mallocz(sizeof(SliceThreadContext));
192  if (!c)
193  return -1;
194 
195  c->workers = av_mallocz(sizeof(pthread_t)*thread_count);
196  if (!c->workers) {
197  av_free(c);
198  return -1;
199  }
200 
201  avctx->internal->thread_ctx = c;
202  c->current_job = 0;
203  c->job_count = 0;
204  c->job_size = 0;
205  c->done = 0;
210  for (i=0; i<thread_count; i++) {
211  if(pthread_create(&c->workers[i], NULL, worker, avctx)) {
212  avctx->thread_count = i;
214  ff_thread_free(avctx);
215  return -1;
216  }
217  }
218 
219  thread_park_workers(c, thread_count);
220 
221  avctx->execute = thread_execute;
222  avctx->execute2 = thread_execute2;
223  return 0;
224 }
int ff_slice_thread_init(AVCodecContext *avctx)
static int pthread_cond_wait(pthread_cond_t *cond, pthread_mutex_t *mutex)
Definition: w32pthreads.h:207
static void pthread_join(pthread_t thread, void **value_ptr)
Definition: w32pthreads.h:94
int av_cpu_count(void)
Definition: cpu.c:144
memory handling functions
int( action_func)(AVCodecContext *c, void *arg)
Definition: pthread_slice.c:42
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
pthread_cond_t current_job_cond
Definition: pthread_slice.c:56
static void pthread_cond_init(pthread_cond_t *cond, const void *unused_attr)
Definition: w32pthreads.h:136
void * thread_ctx
Definition: internal.h:91
Multithreading support functions.
CRITICAL_SECTION pthread_mutex_t
Definition: w32pthreads.h:54
static int pthread_create(pthread_t *thread, const void *unused_attr, void *(*start_routine)(void *), void *arg)
Definition: w32pthreads.h:84
static void pthread_cond_signal(pthread_cond_t *cond)
Definition: w32pthreads.h:239
void av_free(void *ptr)
Free a memory block which has been allocated with av_malloc(z)() or av_realloc(). ...
Definition: mem.c:186
static void *attribute_align_arg worker(void *v)
Definition: pthread_slice.c:63
pthread_mutex_t current_job_lock
Definition: pthread_slice.c:57
static int pthread_mutex_init(pthread_mutex_t *m, void *attr)
Definition: w32pthreads.h:104
int active_thread_type
Which multithreading methods are in use by the codec.
Definition: avcodec.h:2533
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:144
action_func2 * func2
Definition: pthread_slice.c:48
void av_log(void *avcl, int level, const char *fmt,...)
Definition: log.c:148
void ff_thread_free(AVCodecContext *avctx)
Definition: pthread.c:82
action_func * func
Definition: pthread_slice.c:47
static int thread_execute2(AVCodecContext *avctx, action_func2 *func2, void *arg, int *ret, int job_count)
static int pthread_mutex_destroy(pthread_mutex_t *m)
Definition: w32pthreads.h:109
#define FFMIN(a, b)
Definition: common.h:57
void ff_slice_thread_free(AVCodecContext *avctx)
Definition: pthread_slice.c:99
#define FF_THREAD_SLICE
Decode more than one part of a single frame at once.
Definition: avcodec.h:2526
int thread_count
thread count is used to decide how many independent tasks should be passed to execute() ...
Definition: avcodec.h:2514
#define attribute_align_arg
Definition: internal.h:53
NULL
Definition: eval.c:55
static int pthread_mutex_lock(pthread_mutex_t *m)
Definition: w32pthreads.h:114
unsigned current_execute
Definition: pthread_slice.c:58
Libavcodec external API header.
static void pthread_cond_destroy(pthread_cond_t *cond)
Definition: w32pthreads.h:160
main external API structure.
Definition: avcodec.h:1054
static av_always_inline void thread_park_workers(SliceThreadContext *c, int thread_count)
#define MAX_AUTO_THREADS
static void pthread_cond_broadcast(pthread_cond_t *cond)
Definition: w32pthreads.h:176
common internal api header.
common internal and external API header
int(* execute)(struct AVCodecContext *c, int(*func)(struct AVCodecContext *c2, void *arg), void *arg2, int *ret, int count, int size)
The codec may call this to execute several independent things.
Definition: avcodec.h:2554
static void w32thread_init(void)
Definition: w32pthreads.h:264
int(* execute2)(struct AVCodecContext *c, int(*func)(struct AVCodecContext *c2, void *arg, int jobnr, int threadnr), void *arg2, int *ret, int count)
The codec may call this to execute several independent things.
Definition: avcodec.h:2574
struct AVCodecInternal * internal
Private context used for internal data.
Definition: avcodec.h:1098
pthread_t * workers
Definition: pthread_slice.c:46
w32threads to pthreads wrapper
#define av_always_inline
Definition: attributes.h:40
int( action_func2)(AVCodecContext *c, void *arg, int jobnr, int threadnr)
Definition: pthread_slice.c:43
static int thread_execute(AVCodecContext *avctx, action_func *func, void *arg, int *ret, int job_count, int job_size)
pthread_cond_t last_job_cond
Definition: pthread_slice.c:55
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
int avcodec_default_execute(AVCodecContext *c, int(*func)(AVCodecContext *c2, void *arg2), void *arg, int *ret, int count, int size)
Definition: utils.c:800