Coverage Report

Created: 2026-12-01 06:52

next uncovered line (L), next uncovered region (R), next uncovered branch (B)
/work/dav1d/src/thread.h
Line
Count
Source
1
/*
2
 * Copyright © 2018-2021, VideoLAN and dav1d authors
3
 * Copyright © 2018, Two Orioles, LLC
4
 * All rights reserved.
5
 *
6
 * Redistribution and use in source and binary forms, with or without
7
 * modification, are permitted provided that the following conditions are met:
8
 *
9
 * 1. Redistributions of source code must retain the above copyright notice, this
10
 *    list of conditions and the following disclaimer.
11
 *
12
 * 2. Redistributions in binary form must reproduce the above copyright notice,
13
 *    this list of conditions and the following disclaimer in the documentation
14
 *    and/or other materials provided with the distribution.
15
 *
16
 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND
17
 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
18
 * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
19
 * DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR
20
 * ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
21
 * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
22
 * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
23
 * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
24
 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
25
 * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
26
 */
27
28
#ifndef DAV1D_SRC_THREAD_H
29
#define DAV1D_SRC_THREAD_H
30
31
#if defined(_WIN32)
32
33
#include <limits.h>
34
#include <windows.h>
35
36
#define PTHREAD_MUTEX_INITIALIZER SRWLOCK_INIT
37
#define PTHREAD_ONCE_INIT INIT_ONCE_STATIC_INIT
38
39
typedef struct {
40
    HANDLE h;
41
    void *(*func)(void*);
42
    void *arg;
43
} pthread_t;
44
45
typedef struct {
46
    unsigned stack_size;
47
} pthread_attr_t;
48
49
typedef SRWLOCK pthread_mutex_t;
50
typedef CONDITION_VARIABLE pthread_cond_t;
51
typedef INIT_ONCE pthread_once_t;
52
53
void dav1d_init_thread(void);
54
void dav1d_set_thread_name(const wchar_t *name);
55
#define dav1d_set_thread_name(name) dav1d_set_thread_name(L##name)
56
57
int dav1d_pthread_create(pthread_t *thread, const pthread_attr_t *attr,
58
                         void *(*func)(void*), void *arg);
59
int dav1d_pthread_join(pthread_t *thread, void **res);
60
int dav1d_pthread_once(pthread_once_t *once_control,
61
                       void (*init_routine)(void));
62
63
#define pthread_create dav1d_pthread_create
64
#define pthread_join(thread, res) dav1d_pthread_join(&(thread), res)
65
#define pthread_once   dav1d_pthread_once
66
67
static inline int pthread_attr_init(pthread_attr_t *const attr) {
68
    attr->stack_size = 0;
69
    return 0;
70
}
71
72
static inline int pthread_attr_destroy(pthread_attr_t *const attr) {
73
    return 0;
74
}
75
76
static inline int pthread_attr_setstacksize(pthread_attr_t *const attr,
77
                                            const size_t stack_size)
78
{
79
    if (stack_size > UINT_MAX) return 1;
80
    attr->stack_size = (unsigned) stack_size;
81
    return 0;
82
}
83
84
static inline int pthread_mutex_init(pthread_mutex_t *const mutex,
85
                                     const void *const attr)
86
{
87
    InitializeSRWLock(mutex);
88
    return 0;
89
}
90
91
static inline int pthread_mutex_destroy(pthread_mutex_t *const mutex) {
92
    return 0;
93
}
94
95
static inline int pthread_mutex_lock(pthread_mutex_t *const mutex) {
96
    AcquireSRWLockExclusive(mutex);
97
    return 0;
98
}
99
100
static inline int pthread_mutex_trylock(pthread_mutex_t *const mutex) {
101
    return !TryAcquireSRWLockExclusive(mutex);
102
}
103
104
static inline int pthread_mutex_unlock(pthread_mutex_t *const mutex) {
105
    ReleaseSRWLockExclusive(mutex);
106
    return 0;
107
}
108
109
static inline int pthread_cond_init(pthread_cond_t *const cond,
110
                                    const void *const attr)
111
{
112
    InitializeConditionVariable(cond);
113
    return 0;
114
}
115
116
static inline int pthread_cond_destroy(pthread_cond_t *const cond) {
117
    return 0;
118
}
119
120
static inline int pthread_cond_wait(pthread_cond_t *const cond,
121
                                    pthread_mutex_t *const mutex)
122
{
123
    return !SleepConditionVariableSRW(cond, mutex, INFINITE, 0);
124
}
125
126
static inline int pthread_cond_signal(pthread_cond_t *const cond) {
127
    WakeConditionVariable(cond);
128
    return 0;
129
}
130
131
static inline int pthread_cond_broadcast(pthread_cond_t *const cond) {
132
    WakeAllConditionVariable(cond);
133
    return 0;
134
}
135
136
#else
137
138
#include <pthread.h>
139
#if defined(__FreeBSD__)
140
 /* ALIGN from <sys/param.h> conflicts with ALIGN from "common/attributes.h" */
141
#define _SYS_PARAM_H_
142
#include <sys/types.h>
143
#endif
144
#if HAVE_PTHREAD_NP_H
145
#include <pthread_np.h>
146
#endif
147
148
1
#define dav1d_init_thread() do {} while (0)
149
150
/* Thread naming support */
151
152
#ifdef __linux__
153
154
#include <sys/prctl.h>
155
156
854k
static inline void dav1d_set_thread_name(const char *const name) {
157
    prctl(PR_SET_NAME, name);
158
854k
}
Unexecuted instantiation: lib.c:dav1d_set_thread_name
Unexecuted instantiation: log.c:dav1d_set_thread_name
Unexecuted instantiation: mem.c:dav1d_set_thread_name
Unexecuted instantiation: obu.c:dav1d_set_thread_name
Unexecuted instantiation: picture.c:dav1d_set_thread_name
Unexecuted instantiation: ref.c:dav1d_set_thread_name
Unexecuted instantiation: refmvs.c:dav1d_set_thread_name
thread_task.c:dav1d_set_thread_name
Line
Count
Source
156
854k
static inline void dav1d_set_thread_name(const char *const name) {
157
    prctl(PR_SET_NAME, name);
158
854k
}
Unexecuted instantiation: cdf.c:dav1d_set_thread_name
Unexecuted instantiation: data.c:dav1d_set_thread_name
Unexecuted instantiation: decode.c:dav1d_set_thread_name
Unexecuted instantiation: lf_mask.c:dav1d_set_thread_name
Unexecuted instantiation: recon_tmpl.c:dav1d_set_thread_name
Unexecuted instantiation: scan.c:dav1d_set_thread_name
Unexecuted instantiation: cdef_apply_tmpl.c:dav1d_set_thread_name
Unexecuted instantiation: lf_apply_tmpl.c:dav1d_set_thread_name
Unexecuted instantiation: lr_apply_tmpl.c:dav1d_set_thread_name
159
160
#elif HAVE_PTHREAD_SETNAME_NP && defined(__APPLE__)
161
162
static inline void dav1d_set_thread_name(const char *const name) {
163
    pthread_setname_np(name);
164
}
165
166
#elif HAVE_PTHREAD_SETNAME_NP && defined(__NetBSD__)
167
168
static inline void dav1d_set_thread_name(const char *const name) {
169
    pthread_setname_np(pthread_self(), "%s", (void*)name);
170
}
171
172
#elif HAVE_PTHREAD_SETNAME_NP
173
174
static inline void dav1d_set_thread_name(const char *const name) {
175
    pthread_setname_np(pthread_self(), name);
176
}
177
178
#elif HAVE_PTHREAD_SET_NAME_NP
179
180
static inline void dav1d_set_thread_name(const char *const name) {
181
    pthread_set_name_np(pthread_self(), name);
182
}
183
184
#elif defined(__HAIKU__)
185
186
#include <os/kernel/OS.h>
187
188
static inline void dav1d_set_thread_name(const char *const name) {
189
    rename_thread(find_thread(NULL), name);
190
}
191
192
#else
193
194
#define dav1d_set_thread_name(name) do {} while (0)
195
196
#endif
197
198
#endif
199
200
#endif /* DAV1D_SRC_THREAD_H */