1 /*****************************************************************************
2 * threadpool.c: thread pooling
3 *****************************************************************************
4 * Copyright (C) 2010-2011 x264 project
6 * Authors: Steven Walters <kemuri9@gmail.com>
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.
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.
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.
22 * This program is also available under a commercial proprietary license.
23 * For more information, contact us at licensing@x264.com.
24 *****************************************************************************/
30 void *(*func)(void *);
33 } x264_threadpool_job_t;
35 struct x264_threadpool_t
39 x264_pthread_t *thread_handle;
40 void (*init_func)(void *);
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 */
50 static void x264_threadpool_thread( x264_threadpool_t *pool )
53 pool->init_func( pool->init_arg );
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 )
63 job = (void*)x264_frame_shift( pool->run.list );
66 x264_pthread_mutex_unlock( &pool->run.mutex );
69 job->ret = job->func( job->arg ); /* execute the function */
70 x264_sync_frame_list_push( &pool->done, (void*)job );
74 int x264_threadpool_init( x264_threadpool_t **p_pool, int threads,
75 void (*init_func)(void *), void *init_arg )
80 x264_threadpool_t *pool;
81 CHECKED_MALLOCZERO( pool, sizeof(x264_threadpool_t) );
84 pool->init_func = init_func;
85 pool->init_arg = init_arg;
86 pool->threads = X264_MIN( threads, X264_THREAD_MAX );
88 CHECKED_MALLOC( pool->thread_handle, pool->threads * sizeof(x264_pthread_t) );
90 if( x264_sync_frame_list_init( &pool->uninit, pool->threads ) ||
91 x264_sync_frame_list_init( &pool->run, pool->threads ) ||
92 x264_sync_frame_list_init( &pool->done, pool->threads ) )
95 for( int i = 0; i < pool->threads; i++ )
97 x264_threadpool_job_t *job;
98 CHECKED_MALLOC( job, sizeof(x264_threadpool_job_t) );
99 x264_sync_frame_list_push( &pool->uninit, (void*)job );
101 for( int i = 0; i < pool->threads; i++ )
102 if( x264_pthread_create( pool->thread_handle+i, NULL, (void*)x264_threadpool_thread, pool ) )
110 void x264_threadpool_run( x264_threadpool_t *pool, void *(*func)(void *), void *arg )
112 x264_threadpool_job_t *job = (void*)x264_sync_frame_list_pop( &pool->uninit );
115 x264_sync_frame_list_push( &pool->run, (void*)job );
118 void *x264_threadpool_wait( x264_threadpool_t *pool, void *arg )
120 x264_threadpool_job_t *job = NULL;
122 x264_pthread_mutex_lock( &pool->done.mutex );
125 for( int i = 0; i < pool->done.i_size; i++ )
127 x264_threadpool_job_t *t = (void*)pool->done.list[i];
130 job = (void*)x264_frame_shift( pool->done.list+i );
135 x264_pthread_cond_wait( &pool->done.cv_fill, &pool->done.mutex );
137 x264_pthread_mutex_unlock( &pool->done.mutex );
139 void *ret = job->ret;
140 x264_sync_frame_list_push( &pool->uninit, (void*)job );
144 static void x264_threadpool_list_delete( x264_sync_frame_list_t *slist )
146 for( int i = 0; slist->list[i]; i++ )
148 x264_free( slist->list[i] );
149 slist->list[i] = NULL;
151 x264_sync_frame_list_delete( slist );
154 void x264_threadpool_delete( x264_threadpool_t *pool )
156 x264_pthread_mutex_lock( &pool->run.mutex );
158 x264_pthread_cond_broadcast( &pool->run.cv_fill );
159 x264_pthread_mutex_unlock( &pool->run.mutex );
160 for( int i = 0; i < pool->threads; i++ )
161 x264_pthread_join( pool->thread_handle[i], NULL );
163 x264_threadpool_list_delete( &pool->uninit );
164 x264_threadpool_list_delete( &pool->run );
165 x264_threadpool_list_delete( &pool->done );
166 x264_free( pool->thread_handle );