Coverage Report

Created: 2026-02-14 07:00

next uncovered line (L), next uncovered region (R), next uncovered branch (B)
/src/aom/aom_util/aom_thread.c
Line
Count
Source
1
/*
2
 * Copyright (c) 2016, Alliance for Open Media. All rights reserved.
3
 *
4
 * This source code is subject to the terms of the BSD 2 Clause License and
5
 * the Alliance for Open Media Patent License 1.0. If the BSD 2 Clause License
6
 * was not distributed with this source code in the LICENSE file, you can
7
 * obtain it at www.aomedia.org/license/software. If the Alliance for Open
8
 * Media Patent License 1.0 was not distributed with this source code in the
9
 * PATENTS file, you can obtain it at www.aomedia.org/license/patent.
10
 */
11
//
12
// Multi-threaded worker
13
//
14
// Original source:
15
//  https://chromium.googlesource.com/webm/libwebp
16
17
// Enable GNU extensions in glibc so that we can call pthread_setname_np().
18
// This must be before any #include statements.
19
#ifndef _GNU_SOURCE
20
#define _GNU_SOURCE
21
#endif
22
23
#include <assert.h>
24
#include <string.h>  // for memset()
25
26
#include "config/aom_config.h"
27
28
#include "aom_mem/aom_mem.h"
29
#include "aom_ports/sanitizer.h"
30
#include "aom_util/aom_pthread.h"
31
#include "aom_util/aom_thread.h"
32
33
#if CONFIG_MULTITHREAD
34
35
struct AVxWorkerImpl {
36
  pthread_mutex_t mutex_;
37
  pthread_cond_t condition_;
38
  pthread_t thread_;
39
};
40
41
//------------------------------------------------------------------------------
42
43
static void execute(AVxWorker *const worker);  // Forward declaration.
44
45
83.1k
static THREADFN thread_loop(void *ptr) {
46
83.1k
  AVxWorker *const worker = (AVxWorker *)ptr;
47
83.1k
#ifdef HAVE_PTHREAD_SETNAME_NP
48
#ifdef __APPLE__
49
  if (worker->thread_name != NULL) {
50
    // Apple's version of pthread_setname_np takes one argument and operates on
51
    // the current thread only. The maximum size of the thread_name buffer was
52
    // noted in the Chromium source code and was confirmed by experiments. If
53
    // thread_name is too long, pthread_setname_np returns -1 with errno
54
    // ENAMETOOLONG (63).
55
    char thread_name[64];
56
    strncpy(thread_name, worker->thread_name, sizeof(thread_name) - 1);
57
    thread_name[sizeof(thread_name) - 1] = '\0';
58
    pthread_setname_np(thread_name);
59
  }
60
#elif (defined(__GLIBC__) && !defined(__GNU__)) || defined(__BIONIC__)
61
83.1k
  if (worker->thread_name != NULL) {
62
    // Linux and Android require names (with nul) fit in 16 chars, otherwise
63
    // pthread_setname_np() returns ERANGE (34).
64
83.1k
    char thread_name[16];
65
83.1k
    strncpy(thread_name, worker->thread_name, sizeof(thread_name) - 1);
66
83.1k
    thread_name[sizeof(thread_name) - 1] = '\0';
67
83.1k
    pthread_setname_np(pthread_self(), thread_name);
68
83.1k
  }
69
83.1k
#endif
70
83.1k
#endif
71
83.1k
  pthread_mutex_lock(&worker->impl_->mutex_);
72
1.61M
  for (;;) {
73
3.22M
    while (worker->status_ == AVX_WORKER_STATUS_OK) {  // wait in idling mode
74
1.61M
      pthread_cond_wait(&worker->impl_->condition_, &worker->impl_->mutex_);
75
1.61M
    }
76
1.61M
    if (worker->status_ == AVX_WORKER_STATUS_WORKING) {
77
      // When worker->status_ is AVX_WORKER_STATUS_WORKING, the main thread
78
      // doesn't change worker->status_ and will wait until the worker changes
79
      // worker->status_ to AVX_WORKER_STATUS_OK. See change_state(). So the
80
      // worker can safely call execute() without holding worker->impl_->mutex_.
81
      // When the worker reacquires worker->impl_->mutex_, worker->status_ must
82
      // still be AVX_WORKER_STATUS_WORKING.
83
1.53M
      pthread_mutex_unlock(&worker->impl_->mutex_);
84
1.53M
      execute(worker);
85
1.53M
      pthread_mutex_lock(&worker->impl_->mutex_);
86
1.53M
      assert(worker->status_ == AVX_WORKER_STATUS_WORKING);
87
1.53M
      worker->status_ = AVX_WORKER_STATUS_OK;
88
      // signal to the main thread that we're done (for sync())
89
1.53M
      pthread_cond_signal(&worker->impl_->condition_);
90
1.53M
    } else {
91
82.9k
      assert(worker->status_ == AVX_WORKER_STATUS_NOT_OK);  // finish the worker
92
83.1k
      break;
93
82.9k
    }
94
1.61M
  }
95
84.0k
  pthread_mutex_unlock(&worker->impl_->mutex_);
96
84.0k
  return THREAD_EXIT_SUCCESS;  // Thread is finished
97
83.1k
}
98
99
// main thread state control
100
4.93M
static void change_state(AVxWorker *const worker, AVxWorkerStatus new_status) {
101
  // No-op when attempting to change state on a thread that didn't come up.
102
  // Checking status_ without acquiring the lock first would result in a data
103
  // race.
104
4.93M
  if (worker->impl_ == NULL) return;
105
106
4.26M
  pthread_mutex_lock(&worker->impl_->mutex_);
107
4.26M
  if (worker->status_ >= AVX_WORKER_STATUS_OK) {
108
    // wait for the worker to finish
109
4.33M
    while (worker->status_ != AVX_WORKER_STATUS_OK) {
110
67.6k
      pthread_cond_wait(&worker->impl_->condition_, &worker->impl_->mutex_);
111
67.6k
    }
112
    // assign new status and release the working thread if needed
113
4.26M
    if (new_status != AVX_WORKER_STATUS_OK) {
114
1.61M
      worker->status_ = new_status;
115
1.61M
      pthread_cond_signal(&worker->impl_->condition_);
116
1.61M
    }
117
4.26M
  }
118
4.26M
  pthread_mutex_unlock(&worker->impl_->mutex_);
119
4.26M
}
120
121
#endif  // CONFIG_MULTITHREAD
122
123
//------------------------------------------------------------------------------
124
125
106k
static void init(AVxWorker *const worker) {
126
106k
  memset(worker, 0, sizeof(*worker));
127
106k
  worker->status_ = AVX_WORKER_STATUS_NOT_OK;
128
106k
}
129
130
3.32M
static int sync(AVxWorker *const worker) {
131
3.32M
#if CONFIG_MULTITHREAD
132
3.32M
  change_state(worker, AVX_WORKER_STATUS_OK);
133
3.32M
#endif
134
3.32M
  assert(worker->status_ <= AVX_WORKER_STATUS_OK);
135
3.32M
  return !worker->had_error;
136
3.32M
}
137
138
83.1k
static int reset(AVxWorker *const worker) {
139
83.1k
  int ok = 1;
140
83.1k
  worker->had_error = 0;
141
83.1k
  if (worker->status_ < AVX_WORKER_STATUS_OK) {
142
83.1k
#if CONFIG_MULTITHREAD
143
83.1k
    worker->impl_ = (AVxWorkerImpl *)aom_calloc(1, sizeof(*worker->impl_));
144
83.1k
    if (worker->impl_ == NULL) {
145
0
      return 0;
146
0
    }
147
83.1k
    if (pthread_mutex_init(&worker->impl_->mutex_, NULL)) {
148
0
      goto Error;
149
0
    }
150
83.1k
    if (pthread_cond_init(&worker->impl_->condition_, NULL)) {
151
0
      pthread_mutex_destroy(&worker->impl_->mutex_);
152
0
      goto Error;
153
0
    }
154
83.1k
    pthread_attr_t attr;
155
83.1k
    if (pthread_attr_init(&attr)) goto Error2;
156
    // Debug ASan builds require at least ~1MiB of stack; prevents
157
    // failures on macOS arm64 where the default is 512KiB.
158
    // See: https://crbug.com/aomedia/3379
159
#if defined(AOM_ADDRESS_SANITIZER) && defined(__APPLE__) && AOM_ARCH_ARM && \
160
    !defined(NDEBUG)
161
    const size_t kMinStackSize = 1024 * 1024;
162
#else
163
83.1k
    const size_t kMinStackSize = 256 * 1024;
164
83.1k
#endif
165
83.1k
    size_t stacksize;
166
83.1k
    if (!pthread_attr_getstacksize(&attr, &stacksize)) {
167
83.1k
      if (stacksize < kMinStackSize &&
168
0
          pthread_attr_setstacksize(&attr, kMinStackSize)) {
169
0
        pthread_attr_destroy(&attr);
170
0
        goto Error2;
171
0
      }
172
83.1k
    }
173
83.1k
    pthread_mutex_lock(&worker->impl_->mutex_);
174
83.1k
    ok = !pthread_create(&worker->impl_->thread_, &attr, thread_loop, worker);
175
83.1k
    if (ok) worker->status_ = AVX_WORKER_STATUS_OK;
176
83.1k
    pthread_mutex_unlock(&worker->impl_->mutex_);
177
83.1k
    pthread_attr_destroy(&attr);
178
83.1k
    if (!ok) {
179
0
    Error2:
180
0
      pthread_mutex_destroy(&worker->impl_->mutex_);
181
0
      pthread_cond_destroy(&worker->impl_->condition_);
182
0
    Error:
183
0
      aom_free(worker->impl_);
184
0
      worker->impl_ = NULL;
185
0
      return 0;
186
0
    }
187
#else
188
    worker->status_ = AVX_WORKER_STATUS_OK;
189
#endif
190
83.1k
  } else if (worker->status_ > AVX_WORKER_STATUS_OK) {
191
0
    ok = sync(worker);
192
0
  }
193
83.1k
  assert(!ok || (worker->status_ == AVX_WORKER_STATUS_OK));
194
83.1k
  return ok;
195
83.1k
}
196
197
1.86M
static void execute(AVxWorker *const worker) {
198
1.86M
  if (worker->hook != NULL) {
199
1.86M
    worker->had_error |= !worker->hook(worker->data1, worker->data2);
200
1.86M
  }
201
1.86M
}
202
203
1.53M
static void launch(AVxWorker *const worker) {
204
1.53M
#if CONFIG_MULTITHREAD
205
1.53M
  change_state(worker, AVX_WORKER_STATUS_WORKING);
206
#else
207
  execute(worker);
208
#endif
209
1.53M
}
210
211
106k
static void end(AVxWorker *const worker) {
212
106k
#if CONFIG_MULTITHREAD
213
106k
  if (worker->impl_ != NULL) {
214
83.1k
    change_state(worker, AVX_WORKER_STATUS_NOT_OK);
215
83.1k
    pthread_join(worker->impl_->thread_, NULL);
216
83.1k
    pthread_mutex_destroy(&worker->impl_->mutex_);
217
83.1k
    pthread_cond_destroy(&worker->impl_->condition_);
218
83.1k
    aom_free(worker->impl_);
219
83.1k
    worker->impl_ = NULL;
220
83.1k
  }
221
#else
222
  worker->status_ = AVX_WORKER_STATUS_NOT_OK;
223
  assert(worker->impl_ == NULL);
224
#endif
225
106k
  assert(worker->status_ == AVX_WORKER_STATUS_NOT_OK);
226
106k
}
227
228
//------------------------------------------------------------------------------
229
230
static AVxWorkerInterface g_worker_interface = { init,   reset,   sync,
231
                                                 launch, execute, end };
232
233
0
int aom_set_worker_interface(const AVxWorkerInterface *const winterface) {
234
0
  if (winterface == NULL || winterface->init == NULL ||
235
0
      winterface->reset == NULL || winterface->sync == NULL ||
236
0
      winterface->launch == NULL || winterface->execute == NULL ||
237
0
      winterface->end == NULL) {
238
0
    return 0;
239
0
  }
240
0
  g_worker_interface = *winterface;
241
0
  return 1;
242
0
}
243
244
1.20M
const AVxWorkerInterface *aom_get_worker_interface(void) {
245
1.20M
  return &g_worker_interface;
246
1.20M
}
247
248
//------------------------------------------------------------------------------