Coverage Report

Created: 2026-03-08 06:41

next uncovered line (L), next uncovered region (R), next uncovered branch (B)
/work/x265/source/common/threading.cpp
Line
Count
Source
1
/*****************************************************************************
2
 * Copyright (C) 2013-2020 MulticoreWare, Inc
3
 *
4
 * Authors: Steve Borho <steve@borho.org>
5
 *
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.
10
 *
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.
15
 *
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
 *
20
 * This program is also available under a commercial proprietary license.
21
 * For more information, contact us at license @ x265.com
22
 *****************************************************************************/
23
24
#include "common.h"
25
#include "threading.h"
26
#include "cpu.h"
27
28
namespace X265_NS {
29
// x265 private namespace
30
31
#if X265_ARCH_X86 && !defined(X86_64) && ENABLE_ASSEMBLY && defined(__GNUC__)
32
extern "C" intptr_t PFX(stack_align)(void (*func)(), ...);
33
#define STACK_ALIGN(func, ...) PFX(stack_align)((void (*)())func, __VA_ARGS__)
34
#else
35
23.8k
#define STACK_ALIGN(func, ...) func(__VA_ARGS__)
36
#endif
37
38
#if NO_ATOMICS
39
pthread_mutex_t g_mutex = PTHREAD_MUTEX_INITIALIZER;
40
41
int no_atomic_or(int* ptr, int mask)
42
{ 
43
    pthread_mutex_lock(&g_mutex);
44
    int ret = *ptr;
45
    *ptr |= mask;
46
    pthread_mutex_unlock(&g_mutex);
47
    return ret;
48
}
49
50
int no_atomic_and(int* ptr, int mask)
51
{
52
    pthread_mutex_lock(&g_mutex);
53
    int ret = *ptr;
54
    *ptr &= mask;
55
    pthread_mutex_unlock(&g_mutex);
56
    return ret;
57
}
58
59
int no_atomic_inc(int* ptr)
60
{
61
    pthread_mutex_lock(&g_mutex);
62
    *ptr += 1;
63
    int ret = *ptr;
64
    pthread_mutex_unlock(&g_mutex);
65
    return ret;
66
}
67
68
int no_atomic_dec(int* ptr)
69
{
70
    pthread_mutex_lock(&g_mutex);
71
    *ptr -= 1;
72
    int ret = *ptr;
73
    pthread_mutex_unlock(&g_mutex);
74
    return ret;
75
}
76
77
int no_atomic_add(int* ptr, int val)
78
{
79
    pthread_mutex_lock(&g_mutex);
80
    *ptr += val;
81
    int ret = *ptr;
82
    pthread_mutex_unlock(&g_mutex);
83
    return ret;
84
}
85
86
int64_t no_atomic_add64(int64_t* ptr, int64_t val)
87
{
88
    pthread_mutex_lock(&g_mutex);
89
    *ptr += val;
90
    int64_t ret = *ptr;
91
    pthread_mutex_unlock(&g_mutex);
92
    return ret;
93
}
94
#endif
95
96
/* C shim for forced stack alignment */
97
static void stackAlignMain(Thread *instance)
98
23.8k
{
99
    // defer processing to the virtual function implemented in the derived class
100
23.8k
    instance->threadMain();
101
23.8k
}
102
103
#if _WIN32
104
105
static DWORD WINAPI ThreadShim(Thread *instance)
106
{
107
    STACK_ALIGN(stackAlignMain, instance);
108
109
    return 0;
110
}
111
112
bool Thread::start()
113
{
114
    DWORD threadId;
115
116
    thread = CreateThread(NULL, 0, (LPTHREAD_START_ROUTINE)ThreadShim, this, 0, &threadId);
117
118
    return threadId > 0;
119
}
120
121
void Thread::stop()
122
{
123
    if (thread)
124
        WaitForSingleObject(thread, INFINITE);
125
}
126
127
Thread::~Thread()
128
{
129
    if (thread)
130
        CloseHandle(thread);
131
}
132
133
#else /* POSIX / pthreads */
134
135
static void *ThreadShim(void *opaque)
136
23.8k
{
137
    // defer processing to the virtual function implemented in the derived class
138
23.8k
    Thread *instance = reinterpret_cast<Thread *>(opaque);
139
140
23.8k
    STACK_ALIGN(stackAlignMain, instance);
141
142
23.8k
    return NULL;
143
23.8k
}
144
145
bool Thread::start()
146
23.8k
{
147
23.8k
    if (pthread_create(&thread, NULL, ThreadShim, this))
148
0
    {
149
0
        thread = 0;
150
0
        return false;
151
0
    }
152
153
23.8k
    return true;
154
23.8k
}
155
156
void Thread::stop()
157
47.6k
{
158
47.6k
    if (thread){
159
23.8k
        pthread_join(thread, NULL);
160
23.8k
        thread = 0;
161
23.8k
    }
162
47.6k
}
163
164
23.8k
Thread::~Thread() { stop(); }
165
166
#endif // if _WIN32
167
168
Thread::Thread()
169
23.8k
{
170
23.8k
    thread = 0;
171
23.8k
}
172
173
}