Coverage Report

Created: 2026-05-30 06:10

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