1 /*****************************************************************************
2 * threadpool.c: thread pooling
3 *****************************************************************************
4 * Copyright (C) 2010-2021 x264 project
5 *
6 * Authors: Steven Walters <kemuri9@gmail.com>
7 *
8 * This program is free software; you can redistribute it and/or modify
9 * it under the terms of the GNU General Public License as published by
10 * the Free Software Foundation; either version 2 of the License, or
11 * (at your option) any later version.
12 *
13 * This program is distributed in the hope that it will be useful,
14 * but WITHOUT ANY WARRANTY; without even the implied warranty of
15 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 * GNU General Public License for more details.
17 *
18 * You should have received a copy of the GNU General Public License
19 * along with this program; if not, write to the Free Software
20 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02111, USA.
21 *
22 * This program is also available under a commercial proprietary license.
23 * For more information, contact us at licensing@x264.com.
24 *****************************************************************************/
25
26 #include "common.h"
27
28 typedef struct
29 {
30 void *(*func)(void *);
31 void *arg;
32 void *ret;
33 } x264_threadpool_job_t;
34
35 struct x264_threadpool_t
36 {
37 volatile int exit;
38 int threads;
39 x264_pthread_t *thread_handle;
40 void (*init_func)(void *);
41 void *init_arg;
42
43 /* requires a synchronized list structure and associated methods,
44 so use what is already implemented for frames */
45 x264_sync_frame_list_t uninit; /* list of jobs that are awaiting use */
46 x264_sync_frame_list_t run; /* list of jobs that are queued for processing by the pool */
47 x264_sync_frame_list_t done; /* list of jobs that have finished processing */
48 };
49
threadpool_thread(x264_threadpool_t * pool)50 REALIGN_STACK static void *threadpool_thread( x264_threadpool_t *pool )
51 {
52 if( pool->init_func )
53 pool->init_func( pool->init_arg );
54
55 while( !pool->exit )
56 {
57 x264_threadpool_job_t *job = NULL;
58 x264_pthread_mutex_lock( &pool->run.mutex );
59 while( !pool->exit && !pool->run.i_size )
60 x264_pthread_cond_wait( &pool->run.cv_fill, &pool->run.mutex );
61 if( pool->run.i_size )
62 {
63 job = (void*)x264_frame_shift( pool->run.list );
64 pool->run.i_size--;
65 }
66 x264_pthread_mutex_unlock( &pool->run.mutex );
67 if( !job )
68 continue;
69 job->ret = job->func( job->arg );
70 x264_sync_frame_list_push( &pool->done, (void*)job );
71 }
72 return NULL;
73 }
74
x264_threadpool_init(x264_threadpool_t ** p_pool,int threads,void (* init_func)(void *),void * init_arg)75 int x264_threadpool_init( x264_threadpool_t **p_pool, int threads,
76 void (*init_func)(void *), void *init_arg )
77 {
78 if( threads <= 0 )
79 return -1;
80
81 if( x264_threading_init() < 0 )
82 return -1;
83
84 x264_threadpool_t *pool;
85 CHECKED_MALLOCZERO( pool, sizeof(x264_threadpool_t) );
86 *p_pool = pool;
87
88 pool->init_func = init_func;
89 pool->init_arg = init_arg;
90 pool->threads = threads;
91
92 CHECKED_MALLOC( pool->thread_handle, pool->threads * sizeof(x264_pthread_t) );
93
94 if( x264_sync_frame_list_init( &pool->uninit, pool->threads ) ||
95 x264_sync_frame_list_init( &pool->run, pool->threads ) ||
96 x264_sync_frame_list_init( &pool->done, pool->threads ) )
97 goto fail;
98
99 for( int i = 0; i < pool->threads; i++ )
100 {
101 x264_threadpool_job_t *job;
102 CHECKED_MALLOC( job, sizeof(x264_threadpool_job_t) );
103 x264_sync_frame_list_push( &pool->uninit, (void*)job );
104 }
105 for( int i = 0; i < pool->threads; i++ )
106 if( x264_pthread_create( pool->thread_handle+i, NULL, (void*)threadpool_thread, pool ) )
107 goto fail;
108
109 return 0;
110 fail:
111 return -1;
112 }
113
x264_threadpool_run(x264_threadpool_t * pool,void * (* func)(void *),void * arg)114 void x264_threadpool_run( x264_threadpool_t *pool, void *(*func)(void *), void *arg )
115 {
116 x264_threadpool_job_t *job = (void*)x264_sync_frame_list_pop( &pool->uninit );
117 job->func = func;
118 job->arg = arg;
119 x264_sync_frame_list_push( &pool->run, (void*)job );
120 }
121
x264_threadpool_wait(x264_threadpool_t * pool,void * arg)122 void *x264_threadpool_wait( x264_threadpool_t *pool, void *arg )
123 {
124 x264_pthread_mutex_lock( &pool->done.mutex );
125 while( 1 )
126 {
127 for( int i = 0; i < pool->done.i_size; i++ )
128 if( ((x264_threadpool_job_t*)pool->done.list[i])->arg == arg )
129 {
130 x264_threadpool_job_t *job = (void*)x264_frame_shift( pool->done.list+i );
131 pool->done.i_size--;
132 x264_pthread_mutex_unlock( &pool->done.mutex );
133
134 void *ret = job->ret;
135 x264_sync_frame_list_push( &pool->uninit, (void*)job );
136 return ret;
137 }
138
139 x264_pthread_cond_wait( &pool->done.cv_fill, &pool->done.mutex );
140 }
141 }
142
threadpool_list_delete(x264_sync_frame_list_t * slist)143 static void threadpool_list_delete( x264_sync_frame_list_t *slist )
144 {
145 for( int i = 0; slist->list[i]; i++ )
146 {
147 x264_free( slist->list[i] );
148 slist->list[i] = NULL;
149 }
150 x264_sync_frame_list_delete( slist );
151 }
152
x264_threadpool_delete(x264_threadpool_t * pool)153 void x264_threadpool_delete( x264_threadpool_t *pool )
154 {
155 x264_pthread_mutex_lock( &pool->run.mutex );
156 pool->exit = 1;
157 x264_pthread_cond_broadcast( &pool->run.cv_fill );
158 x264_pthread_mutex_unlock( &pool->run.mutex );
159 for( int i = 0; i < pool->threads; i++ )
160 x264_pthread_join( pool->thread_handle[i], NULL );
161
162 threadpool_list_delete( &pool->uninit );
163 threadpool_list_delete( &pool->run );
164 threadpool_list_delete( &pool->done );
165 x264_free( pool->thread_handle );
166 x264_free( pool );
167 }
168