Coverage Report

Created: 2026-10-28 06:18

next uncovered line (L), next uncovered region (R), next uncovered branch (B)
/work/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
932
{
50
2.04k
    while( !pool->exit )
51
1.11k
    {
52
1.11k
        x264_threadpool_job_t *job = NULL;
53
1.11k
        x264_pthread_mutex_lock( &pool->run.mutex );
54
2.97k
        while( !pool->exit && !pool->run.i_size )
55
1.86k
            x264_pthread_cond_wait( &pool->run.cv_fill, &pool->run.mutex );
56
1.11k
        if( pool->run.i_size )
57
180
        {
58
180
            job = (void*)x264_frame_shift( pool->run.list );
59
180
            pool->run.i_size--;
60
180
        }
61
1.11k
        x264_pthread_mutex_unlock( &pool->run.mutex );
62
1.11k
        if( !job )
63
933
            continue;
64
180
        job->ret = job->func( job->arg );
65
180
        x264_sync_frame_list_push( &pool->done, (void*)job );
66
180
    }
67
932
    return NULL;
68
932
}
69
70
int x264_threadpool_init( x264_threadpool_t **p_pool, int threads )
71
180
{
72
180
    if( threads <= 0 )
73
0
        return -1;
74
75
180
    if( x264_threading_init() < 0 )
76
0
        return -1;
77
78
180
    x264_threadpool_t *pool;
79
180
    CHECKED_MALLOCZERO( pool, sizeof(x264_threadpool_t) );
80
180
    *p_pool = pool;
81
82
180
    pool->threads   = threads;
83
84
180
    CHECKED_MALLOC( pool->thread_handle, pool->threads * sizeof(x264_pthread_t) );
85
86
180
    if( x264_sync_frame_list_init( &pool->uninit, pool->threads ) ||
87
180
        x264_sync_frame_list_init( &pool->run, pool->threads ) ||
88
180
        x264_sync_frame_list_init( &pool->done, pool->threads ) )
89
0
        goto fail;
90
91
1.11k
    for( int i = 0; i < pool->threads; i++ )
92
933
    {
93
933
       x264_threadpool_job_t *job;
94
933
       CHECKED_MALLOC( job, sizeof(x264_threadpool_job_t) );
95
933
       x264_sync_frame_list_push( &pool->uninit, (void*)job );
96
933
    }
97
1.11k
    for( int i = 0; i < pool->threads; i++ )
98
933
        if( x264_pthread_create( pool->thread_handle+i, NULL, (void*)threadpool_thread, pool ) )
99
0
            goto fail;
100
101
180
    return 0;
102
0
fail:
103
0
    return -1;
104
180
}
x264_8_threadpool_init
Line
Count
Source
71
180
{
72
180
    if( threads <= 0 )
73
0
        return -1;
74
75
180
    if( x264_threading_init() < 0 )
76
0
        return -1;
77
78
180
    x264_threadpool_t *pool;
79
180
    CHECKED_MALLOCZERO( pool, sizeof(x264_threadpool_t) );
80
180
    *p_pool = pool;
81
82
180
    pool->threads   = threads;
83
84
180
    CHECKED_MALLOC( pool->thread_handle, pool->threads * sizeof(x264_pthread_t) );
85
86
180
    if( x264_sync_frame_list_init( &pool->uninit, pool->threads ) ||
87
180
        x264_sync_frame_list_init( &pool->run, pool->threads ) ||
88
180
        x264_sync_frame_list_init( &pool->done, pool->threads ) )
89
0
        goto fail;
90
91
1.11k
    for( int i = 0; i < pool->threads; i++ )
92
933
    {
93
933
       x264_threadpool_job_t *job;
94
933
       CHECKED_MALLOC( job, sizeof(x264_threadpool_job_t) );
95
933
       x264_sync_frame_list_push( &pool->uninit, (void*)job );
96
933
    }
97
1.11k
    for( int i = 0; i < pool->threads; i++ )
98
933
        if( x264_pthread_create( pool->thread_handle+i, NULL, (void*)threadpool_thread, pool ) )
99
0
            goto fail;
100
101
180
    return 0;
102
0
fail:
103
0
    return -1;
104
180
}
Unexecuted instantiation: x264_10_threadpool_init
105
106
void x264_threadpool_run( x264_threadpool_t *pool, void *(*func)(void *), void *arg )
107
180
{
108
180
    x264_threadpool_job_t *job = (void*)x264_sync_frame_list_pop( &pool->uninit );
109
180
    job->func = func;
110
180
    job->arg  = arg;
111
180
    x264_sync_frame_list_push( &pool->run, (void*)job );
112
180
}
x264_8_threadpool_run
Line
Count
Source
107
180
{
108
180
    x264_threadpool_job_t *job = (void*)x264_sync_frame_list_pop( &pool->uninit );
109
180
    job->func = func;
110
180
    job->arg  = arg;
111
180
    x264_sync_frame_list_push( &pool->run, (void*)job );
112
180
}
Unexecuted instantiation: x264_10_threadpool_run
113
114
void *x264_threadpool_wait( x264_threadpool_t *pool, void *arg )
115
180
{
116
180
    x264_pthread_mutex_lock( &pool->done.mutex );
117
360
    while( 1 )
118
360
    {
119
360
        for( int i = 0; i < pool->done.i_size; i++ )
120
180
            if( ((x264_threadpool_job_t*)pool->done.list[i])->arg == arg )
121
180
            {
122
180
                x264_threadpool_job_t *job = (void*)x264_frame_shift( pool->done.list+i );
123
180
                pool->done.i_size--;
124
180
                x264_pthread_mutex_unlock( &pool->done.mutex );
125
126
180
                void *ret = job->ret;
127
180
                x264_sync_frame_list_push( &pool->uninit, (void*)job );
128
180
                return ret;
129
180
            }
130
131
180
        x264_pthread_cond_wait( &pool->done.cv_fill, &pool->done.mutex );
132
180
    }
133
180
}
x264_8_threadpool_wait
Line
Count
Source
115
180
{
116
180
    x264_pthread_mutex_lock( &pool->done.mutex );
117
360
    while( 1 )
118
360
    {
119
360
        for( int i = 0; i < pool->done.i_size; i++ )
120
180
            if( ((x264_threadpool_job_t*)pool->done.list[i])->arg == arg )
121
180
            {
122
180
                x264_threadpool_job_t *job = (void*)x264_frame_shift( pool->done.list+i );
123
180
                pool->done.i_size--;
124
180
                x264_pthread_mutex_unlock( &pool->done.mutex );
125
126
180
                void *ret = job->ret;
127
180
                x264_sync_frame_list_push( &pool->uninit, (void*)job );
128
180
                return ret;
129
180
            }
130
131
180
        x264_pthread_cond_wait( &pool->done.cv_fill, &pool->done.mutex );
132
180
    }
133
180
}
Unexecuted instantiation: x264_10_threadpool_wait
134
135
static void threadpool_list_delete( x264_sync_frame_list_t *slist )
136
540
{
137
1.47k
    for( int i = 0; slist->list[i]; i++ )
138
933
    {
139
933
        x264_free( slist->list[i] );
140
933
        slist->list[i] = NULL;
141
933
    }
142
540
    x264_sync_frame_list_delete( slist );
143
540
}
144
145
void x264_threadpool_delete( x264_threadpool_t *pool )
146
180
{
147
180
    x264_pthread_mutex_lock( &pool->run.mutex );
148
180
    pool->exit = 1;
149
180
    x264_pthread_cond_broadcast( &pool->run.cv_fill );
150
180
    x264_pthread_mutex_unlock( &pool->run.mutex );
151
1.11k
    for( int i = 0; i < pool->threads; i++ )
152
933
        x264_pthread_join( pool->thread_handle[i], NULL );
153
154
180
    threadpool_list_delete( &pool->uninit );
155
180
    threadpool_list_delete( &pool->run );
156
180
    threadpool_list_delete( &pool->done );
157
180
    x264_free( pool->thread_handle );
158
180
    x264_free( pool );
159
180
}
x264_8_threadpool_delete
Line
Count
Source
146
180
{
147
180
    x264_pthread_mutex_lock( &pool->run.mutex );
148
180
    pool->exit = 1;
149
180
    x264_pthread_cond_broadcast( &pool->run.cv_fill );
150
180
    x264_pthread_mutex_unlock( &pool->run.mutex );
151
1.11k
    for( int i = 0; i < pool->threads; i++ )
152
933
        x264_pthread_join( pool->thread_handle[i], NULL );
153
154
180
    threadpool_list_delete( &pool->uninit );
155
180
    threadpool_list_delete( &pool->run );
156
180
    threadpool_list_delete( &pool->done );
157
180
    x264_free( pool->thread_handle );
158
180
    x264_free( pool );
159
180
}
Unexecuted instantiation: x264_10_threadpool_delete