Coverage Report

Created: 2026-05-16 06:31

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