1 /*****************************************************************************
2 * threadpool.c: x264 threadpool module
3 *****************************************************************************
4 * Copyright (C) 2010 Steven Walters <kemuri9@gmail.com>
6 * This program is free software; you can redistribute it and/or modify
7 * it under the terms of the GNU General Public License as published by
8 * the Free Software Foundation; either version 2 of the License, or
9 * (at your option) any later version.
11 * This program is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 * GNU General Public License for more details.
16 * You should have received a copy of the GNU General Public License
17 * along with this program; if not, write to the Free Software
18 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02111, USA.
19 *****************************************************************************/
25 void *(*func)(void *);
28 } x264_threadpool_job_t;
30 struct x264_threadpool_t
34 x264_pthread_t *thread_handle;
35 void (*init_func)(void *);
38 /* requires a synchronized list structure and associated methods,
39 so use what is already implemented for frames */
40 x264_sync_frame_list_t uninit; /* list of jobs that are awaiting use */
41 x264_sync_frame_list_t run; /* list of jobs that are queued for processing by the pool */
42 x264_sync_frame_list_t done; /* list of jobs that have finished processing */
45 static void x264_threadpool_thread( x264_threadpool_t *pool )
48 pool->init_func( pool->init_arg );
52 x264_threadpool_job_t *job = NULL;
53 x264_pthread_mutex_lock( &pool->run.mutex );
54 while( !pool->exit && !pool->run.i_size )
55 x264_pthread_cond_wait( &pool->run.cv_fill, &pool->run.mutex );
56 if( pool->run.i_size )
58 job = (void*)x264_frame_shift( pool->run.list );
61 x264_pthread_mutex_unlock( &pool->run.mutex );
64 job->ret = job->func( job->arg ); /* execute the function */
65 x264_sync_frame_list_push( &pool->done, (void*)job );
69 int x264_threadpool_init( x264_threadpool_t **p_pool, int threads,
70 void (*init_func)(void *), void *init_arg )
75 x264_threadpool_t *pool;
76 CHECKED_MALLOCZERO( pool, sizeof(x264_threadpool_t) );
79 pool->init_func = init_func;
80 pool->init_arg = init_arg;
81 pool->threads = X264_MIN( threads, X264_THREAD_MAX );
83 CHECKED_MALLOC( pool->thread_handle, pool->threads * sizeof(x264_pthread_t) );
85 if( x264_sync_frame_list_init( &pool->uninit, pool->threads ) ||
86 x264_sync_frame_list_init( &pool->run, pool->threads ) ||
87 x264_sync_frame_list_init( &pool->done, pool->threads ) )
90 for( int i = 0; i < pool->threads; i++ )
92 x264_threadpool_job_t *job;
93 CHECKED_MALLOC( job, sizeof(x264_threadpool_job_t) );
94 x264_sync_frame_list_push( &pool->uninit, (void*)job );
96 for( int i = 0; i < pool->threads; i++ )
97 if( x264_pthread_create( pool->thread_handle+i, NULL, (void*)x264_threadpool_thread, pool ) )
105 void x264_threadpool_run( x264_threadpool_t *pool, void *(*func)(void *), void *arg )
107 x264_threadpool_job_t *job = (void*)x264_sync_frame_list_pop( &pool->uninit );
110 x264_sync_frame_list_push( &pool->run, (void*)job );
113 void *x264_threadpool_wait( x264_threadpool_t *pool, void *arg )
115 x264_threadpool_job_t *job = NULL;
117 x264_pthread_mutex_lock( &pool->done.mutex );
120 for( int i = 0; i < pool->done.i_size; i++ )
122 x264_threadpool_job_t *t = (void*)pool->done.list[i];
125 job = (void*)x264_frame_shift( pool->done.list+i );
130 x264_pthread_cond_wait( &pool->done.cv_fill, &pool->done.mutex );
132 x264_pthread_mutex_unlock( &pool->done.mutex );
134 void *ret = job->ret;
135 x264_sync_frame_list_push( &pool->uninit, (void*)job );
139 static void x264_threadpool_list_delete( x264_sync_frame_list_t *slist )
141 for( int i = 0; slist->list[i]; i++ )
143 x264_free( slist->list[i] );
144 slist->list[i] = NULL;
146 x264_sync_frame_list_delete( slist );
149 void x264_threadpool_delete( x264_threadpool_t *pool )
151 x264_pthread_mutex_lock( &pool->run.mutex );
153 x264_pthread_cond_broadcast( &pool->run.cv_fill );
154 x264_pthread_mutex_unlock( &pool->run.mutex );
155 for( int i = 0; i < pool->threads; i++ )
156 x264_pthread_join( pool->thread_handle[i], NULL );
158 x264_threadpool_list_delete( &pool->uninit );
159 x264_threadpool_list_delete( &pool->run );
160 x264_threadpool_list_delete( &pool->done );
161 x264_free( pool->thread_handle );