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  * This file is part of FFmpeg.
3  *
4  * FFmpeg 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  * FFmpeg 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 FFmpeg; if not, write to the Free Software
16  * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
17  */
18 
19 /**
20  * @file
21  * Libavfilter multithreading support
22  */
23 
24 #include "config.h"
25 
26 #include "libavutil/common.h"
27 #include "libavutil/cpu.h"
28 #include "libavutil/mem.h"
29 #include "libavutil/thread.h"
30 #include "libavutil/slicethread.h"
31 
32 #include "avfilter.h"
33 #include "internal.h"
34 #include "thread.h"
35 
36 typedef struct ThreadContext {
40 
41  /* per-execute parameters */
43  void *arg;
44  int *rets;
46 
47 static void worker_func(void *priv, int jobnr, int threadnr, int nb_jobs, int nb_threads)
48 {
49  ThreadContext *c = priv;
50  int ret = c->func(c->ctx, c->arg, jobnr, nb_jobs);
51  if (c->rets)
52  c->rets[jobnr] = ret;
53 }
54 
56 {
58 }
59 
61  void *arg, int *ret, int nb_jobs)
62 {
64 
65  if (nb_jobs <= 0)
66  return 0;
67  c->ctx = ctx;
68  c->arg = arg;
69  c->func = func;
70  c->rets = ret;
71 
72  avpriv_slicethread_execute(c->thread, nb_jobs, 0);
73  return 0;
74 }
75 
76 static int thread_init_internal(ThreadContext *c, int nb_threads)
77 {
78  nb_threads = avpriv_slicethread_create(&c->thread, c, worker_func, NULL, nb_threads);
79  if (nb_threads <= 1)
81  return FFMAX(nb_threads, 1);
82 }
83 
85 {
86  int ret;
87 
88 #if HAVE_W32THREADS
90 #endif
91 
92  if (graph->nb_threads == 1) {
93  graph->thread_type = 0;
94  return 0;
95  }
96 
97  graph->internal->thread = av_mallocz(sizeof(ThreadContext));
98  if (!graph->internal->thread)
99  return AVERROR(ENOMEM);
100 
101  ret = thread_init_internal(graph->internal->thread, graph->nb_threads);
102  if (ret <= 1) {
103  av_freep(&graph->internal->thread);
104  graph->thread_type = 0;
105  graph->nb_threads = 1;
106  return (ret < 0) ? ret : 0;
107  }
108  graph->nb_threads = ret;
109 
111 
112  return 0;
113 }
114 
116 {
117  if (graph->internal->thread)
119  av_freep(&graph->internal->thread);
120 }
static av_unused void w32thread_init(void)
Definition: w32pthreads.h:397
#define NULL
Definition: coverity.c:32
int thread_type
Type of multithreading allowed for filters in this graph.
Definition: avfilter.h:825
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:60
avfilter_action_func * func
Definition: pthread.c:39
void * av_mallocz(size_t size)
Allocate a memory block with alignment suitable for all memory accesses (including vectors if availab...
Definition: mem.c:236
struct AVFilterGraph * graph
filtergraph this filter belongs to
Definition: avfilter.h:355
struct AVSliceThread AVSliceThread
Definition: slicethread.h:22
int nb_threads
Maximum number of threads used by filters in this graph.
Definition: avfilter.h:832
static void worker_func(void *priv, int jobnr, int threadnr, int nb_jobs, int nb_threads)
Definition: pthread.c:47
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:786
#define AVERROR(e)
Definition: error.h:43
const char * arg
Definition: jacosubdec.c:66
#define FFMAX(a, b)
Definition: common.h:94
AVSliceThread * thread
Definition: pthread.c:38
void * arg
Definition: pthread.c:43
AVFilterGraph * graph
Definition: pthread.c:37
void ff_graph_thread_free(AVFilterGraph *graph)
Definition: pthread.c:115
void avpriv_slicethread_free(AVSliceThread **pctx)
Destroy slice threading context.
Definition: slicethread.c:254
AVFormatContext * ctx
Definition: movenc.c:48
AVFilterGraphInternal * internal
Opaque object for libavfilter internal use.
Definition: avfilter.h:837
int avpriv_slicethread_create(AVSliceThread **pctx, void *priv, void(*worker_func)(void *priv, int jobnr, int threadnr, int nb_jobs, int nb_threads), void(*main_func)(void *priv), int nb_threads)
Create slice threading context.
Definition: slicethread.c:240
static void slice_thread_uninit(ThreadContext *c)
Definition: pthread.c:55
void avpriv_slicethread_execute(AVSliceThread *ctx, int nb_jobs, int execute_main)
Execute slice threading.
Definition: slicethread.c:249
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:76
common internal and external API header
static double c[64]
avfilter_execute_func * thread_execute
Definition: internal.h:150
int ff_graph_thread_init(AVFilterGraph *graph)
Definition: pthread.c:84
An instance of a filter.
Definition: avfilter.h:338
int * rets
Definition: pthread.c:44
#define av_freep(p)
internal API functions
AVFilterContext * ctx
Definition: pthread.c:42