/src/x264/common/threadpool.c
Line | Count | Source |
1 | | /***************************************************************************** |
2 | | * threadpool.c: thread pooling |
3 | | ***************************************************************************** |
4 | | * Copyright (C) 2010-2025 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 | | |
41 | | /* requires a synchronized list structure and associated methods, |
42 | | so use what is already implemented for frames */ |
43 | | x264_sync_frame_list_t uninit; /* list of jobs that are awaiting use */ |
44 | | x264_sync_frame_list_t run; /* list of jobs that are queued for processing by the pool */ |
45 | | x264_sync_frame_list_t done; /* list of jobs that have finished processing */ |
46 | | }; |
47 | | |
48 | | REALIGN_STACK static void *threadpool_thread( x264_threadpool_t *pool ) |
49 | 0 | { |
50 | 0 | while( !pool->exit ) |
51 | 0 | { |
52 | 0 | x264_threadpool_job_t *job = NULL; |
53 | 0 | x264_pthread_mutex_lock( &pool->run.mutex ); |
54 | 0 | while( !pool->exit && !pool->run.i_size ) |
55 | 0 | x264_pthread_cond_wait( &pool->run.cv_fill, &pool->run.mutex ); |
56 | 0 | if( pool->run.i_size ) |
57 | 0 | { |
58 | 0 | job = (void*)x264_frame_shift( pool->run.list ); |
59 | 0 | pool->run.i_size--; |
60 | 0 | } |
61 | 0 | x264_pthread_mutex_unlock( &pool->run.mutex ); |
62 | 0 | if( !job ) |
63 | 0 | continue; |
64 | 0 | job->ret = job->func( job->arg ); |
65 | 0 | x264_sync_frame_list_push( &pool->done, (void*)job ); |
66 | 0 | } |
67 | 0 | return NULL; |
68 | 0 | } |
69 | | |
70 | | int x264_threadpool_init( x264_threadpool_t **p_pool, int threads ) |
71 | 0 | { |
72 | 0 | if( threads <= 0 ) |
73 | 0 | return -1; |
74 | | |
75 | 0 | if( x264_threading_init() < 0 ) |
76 | 0 | return -1; |
77 | | |
78 | 0 | x264_threadpool_t *pool; |
79 | 0 | CHECKED_MALLOCZERO( pool, sizeof(x264_threadpool_t) ); |
80 | 0 | *p_pool = pool; |
81 | |
|
82 | 0 | pool->threads = threads; |
83 | |
|
84 | 0 | CHECKED_MALLOC( pool->thread_handle, pool->threads * sizeof(x264_pthread_t) ); |
85 | | |
86 | 0 | if( x264_sync_frame_list_init( &pool->uninit, pool->threads ) || |
87 | 0 | x264_sync_frame_list_init( &pool->run, pool->threads ) || |
88 | 0 | x264_sync_frame_list_init( &pool->done, pool->threads ) ) |
89 | 0 | goto fail; |
90 | | |
91 | 0 | for( int i = 0; i < pool->threads; i++ ) |
92 | 0 | { |
93 | 0 | x264_threadpool_job_t *job; |
94 | 0 | CHECKED_MALLOC( job, sizeof(x264_threadpool_job_t) ); |
95 | 0 | x264_sync_frame_list_push( &pool->uninit, (void*)job ); |
96 | 0 | } |
97 | 0 | for( int i = 0; i < pool->threads; i++ ) |
98 | 0 | if( x264_pthread_create( pool->thread_handle+i, NULL, (void*)threadpool_thread, pool ) ) |
99 | 0 | goto fail; |
100 | | |
101 | 0 | return 0; |
102 | 0 | fail: |
103 | 0 | return -1; |
104 | 0 | } Unexecuted instantiation: x264_8_threadpool_init Unexecuted instantiation: x264_10_threadpool_init |
105 | | |
106 | | void x264_threadpool_run( x264_threadpool_t *pool, void *(*func)(void *), void *arg ) |
107 | 0 | { |
108 | 0 | x264_threadpool_job_t *job = (void*)x264_sync_frame_list_pop( &pool->uninit ); |
109 | 0 | job->func = func; |
110 | 0 | job->arg = arg; |
111 | 0 | x264_sync_frame_list_push( &pool->run, (void*)job ); |
112 | 0 | } Unexecuted instantiation: x264_8_threadpool_run Unexecuted instantiation: x264_10_threadpool_run |
113 | | |
114 | | void *x264_threadpool_wait( x264_threadpool_t *pool, void *arg ) |
115 | 0 | { |
116 | 0 | x264_pthread_mutex_lock( &pool->done.mutex ); |
117 | 0 | while( 1 ) |
118 | 0 | { |
119 | 0 | for( int i = 0; i < pool->done.i_size; i++ ) |
120 | 0 | if( ((x264_threadpool_job_t*)pool->done.list[i])->arg == arg ) |
121 | 0 | { |
122 | 0 | x264_threadpool_job_t *job = (void*)x264_frame_shift( pool->done.list+i ); |
123 | 0 | pool->done.i_size--; |
124 | 0 | x264_pthread_mutex_unlock( &pool->done.mutex ); |
125 | |
|
126 | 0 | void *ret = job->ret; |
127 | 0 | x264_sync_frame_list_push( &pool->uninit, (void*)job ); |
128 | 0 | return ret; |
129 | 0 | } |
130 | | |
131 | 0 | x264_pthread_cond_wait( &pool->done.cv_fill, &pool->done.mutex ); |
132 | 0 | } |
133 | 0 | } Unexecuted instantiation: x264_8_threadpool_wait Unexecuted instantiation: x264_10_threadpool_wait |
134 | | |
135 | | static void threadpool_list_delete( x264_sync_frame_list_t *slist ) |
136 | 0 | { |
137 | 0 | for( int i = 0; slist->list[i]; i++ ) |
138 | 0 | { |
139 | 0 | x264_free( slist->list[i] ); |
140 | 0 | slist->list[i] = NULL; |
141 | 0 | } |
142 | 0 | x264_sync_frame_list_delete( slist ); |
143 | 0 | } |
144 | | |
145 | | void x264_threadpool_delete( x264_threadpool_t *pool ) |
146 | 0 | { |
147 | 0 | x264_pthread_mutex_lock( &pool->run.mutex ); |
148 | 0 | pool->exit = 1; |
149 | 0 | x264_pthread_cond_broadcast( &pool->run.cv_fill ); |
150 | 0 | x264_pthread_mutex_unlock( &pool->run.mutex ); |
151 | 0 | for( int i = 0; i < pool->threads; i++ ) |
152 | 0 | x264_pthread_join( pool->thread_handle[i], NULL ); |
153 | |
|
154 | 0 | threadpool_list_delete( &pool->uninit ); |
155 | 0 | threadpool_list_delete( &pool->run ); |
156 | 0 | threadpool_list_delete( &pool->done ); |
157 | 0 | x264_free( pool->thread_handle ); |
158 | 0 | x264_free( pool ); |
159 | 0 | } Unexecuted instantiation: x264_8_threadpool_delete Unexecuted instantiation: x264_10_threadpool_delete |