Coverage Report

Created: 2026-10-17 06:45

next uncovered line (L), next uncovered region (R), next uncovered branch (B)
/src/libvpx/vpx_util/vpx_thread.c
Line
Count
Source
1
// Copyright 2013 Google Inc. All Rights Reserved.
2
//
3
// Use of this source code is governed by a BSD-style license
4
// that can be found in the COPYING file in the root of the source
5
// tree. An additional intellectual property rights grant can be found
6
// in the file PATENTS. All contributing project authors may
7
// be found in the AUTHORS file in the root of the source tree.
8
// -----------------------------------------------------------------------------
9
//
10
// Multi-threaded worker
11
//
12
// Original source:
13
//  https://chromium.googlesource.com/webm/libwebp
14
15
// Enable GNU extensions in glibc so that we can call pthread_setname_np().
16
// This must be before any #include statements.
17
#ifndef _GNU_SOURCE
18
#define _GNU_SOURCE
19
#endif
20
21
#include <assert.h>
22
#include <string.h>  // for memset()
23
#include "./vpx_config.h"
24
#include "./vpx_thread.h"
25
#include "vpx_mem/vpx_mem.h"
26
#include "vpx_util/vpx_pthread.h"
27
28
#if CONFIG_MULTITHREAD
29
30
struct VPxWorkerImpl {
31
  pthread_mutex_t mutex_;
32
  pthread_cond_t condition_;
33
  pthread_t thread_;
34
};
35
36
//------------------------------------------------------------------------------
37
38
static void execute(VPxWorker *const worker);  // Forward declaration.
39
40
84.9k
static THREADFN thread_loop(void *ptr) {
41
84.9k
  VPxWorker *const worker = (VPxWorker *)ptr;
42
84.9k
#ifdef HAVE_PTHREAD_SETNAME_NP
43
#ifdef __APPLE__
44
  if (worker->thread_name != NULL) {
45
    // Apple's version of pthread_setname_np takes one argument and operates on
46
    // the current thread only. The maximum size of the thread_name buffer was
47
    // noted in the Chromium source code and was confirmed by experiments. If
48
    // thread_name is too long, pthread_setname_np returns -1 with errno
49
    // ENAMETOOLONG (63).
50
    char thread_name[64];
51
    strncpy(thread_name, worker->thread_name, sizeof(thread_name) - 1);
52
    thread_name[sizeof(thread_name) - 1] = '\0';
53
    pthread_setname_np(thread_name);
54
  }
55
#elif (defined(__GLIBC__) && !defined(__GNU__)) || defined(__BIONIC__)
56
84.9k
  if (worker->thread_name != NULL) {
57
    // Linux and Android require names (with nul) fit in 16 chars, otherwise
58
    // pthread_setname_np() returns ERANGE (34).
59
84.9k
    char thread_name[16];
60
84.9k
    strncpy(thread_name, worker->thread_name, sizeof(thread_name) - 1);
61
84.9k
    thread_name[sizeof(thread_name) - 1] = '\0';
62
84.9k
    pthread_setname_np(pthread_self(), thread_name);
63
84.9k
  }
64
84.9k
#endif
65
84.9k
#endif
66
84.9k
  pthread_mutex_lock(&worker->impl_->mutex_);
67
481k
  for (;;) {
68
956k
    while (worker->status_ == VPX_WORKER_STATUS_OK) {  // wait in idling mode
69
475k
      pthread_cond_wait(&worker->impl_->condition_, &worker->impl_->mutex_);
70
475k
    }
71
481k
    if (worker->status_ == VPX_WORKER_STATUS_WORKING) {
72
      // When worker->status_ is VPX_WORKER_STATUS_WORKING, the main thread
73
      // doesn't change worker->status_ and will wait until the worker changes
74
      // worker->status_ to VPX_WORKER_STATUS_OK. See change_state(). So the
75
      // worker can safely call execute() without holding worker->impl_->mutex_.
76
      // When the worker reacquires worker->impl_->mutex_, worker->status_ must
77
      // still be VPX_WORKER_STATUS_WORKING.
78
396k
      pthread_mutex_unlock(&worker->impl_->mutex_);
79
396k
      execute(worker);
80
396k
      pthread_mutex_lock(&worker->impl_->mutex_);
81
396k
      assert(worker->status_ == VPX_WORKER_STATUS_WORKING);
82
396k
      worker->status_ = VPX_WORKER_STATUS_OK;
83
      // signal to the main thread that we're done (for sync())
84
396k
      pthread_cond_signal(&worker->impl_->condition_);
85
396k
    } else {
86
84.9k
      assert(worker->status_ == VPX_WORKER_STATUS_NOT_OK);  // finish the worker
87
84.9k
      break;
88
84.9k
    }
89
481k
  }
90
85.0k
  pthread_mutex_unlock(&worker->impl_->mutex_);
91
85.0k
  return THREAD_EXIT_SUCCESS;  // Thread is finished
92
84.9k
}
93
94
// main thread state control
95
1.87M
static void change_state(VPxWorker *const worker, VPxWorkerStatus new_status) {
96
  // No-op when attempting to change state on a thread that didn't come up.
97
  // Checking status_ without acquiring the lock first would result in a data
98
  // race.
99
1.87M
  if (worker->impl_ == NULL) return;
100
101
1.46M
  pthread_mutex_lock(&worker->impl_->mutex_);
102
1.46M
  if (worker->status_ >= VPX_WORKER_STATUS_OK) {
103
    // wait for the worker to finish
104
1.66M
    while (worker->status_ != VPX_WORKER_STATUS_OK) {
105
191k
      pthread_cond_wait(&worker->impl_->condition_, &worker->impl_->mutex_);
106
191k
    }
107
    // assign new status and release the working thread if needed
108
1.46M
    if (new_status != VPX_WORKER_STATUS_OK) {
109
481k
      worker->status_ = new_status;
110
481k
      pthread_cond_signal(&worker->impl_->condition_);
111
481k
    }
112
1.46M
  }
113
1.46M
  pthread_mutex_unlock(&worker->impl_->mutex_);
114
1.46M
}
115
116
#endif  // CONFIG_MULTITHREAD
117
118
//------------------------------------------------------------------------------
119
120
103k
static void init(VPxWorker *const worker) {
121
103k
  memset(worker, 0, sizeof(*worker));
122
103k
  worker->status_ = VPX_WORKER_STATUS_NOT_OK;
123
103k
}
124
125
1.39M
static int sync(VPxWorker *const worker) {
126
1.39M
#if CONFIG_MULTITHREAD
127
1.39M
  change_state(worker, VPX_WORKER_STATUS_OK);
128
1.39M
#endif
129
1.39M
  assert(worker->status_ <= VPX_WORKER_STATUS_OK);
130
1.39M
  return !worker->had_error;
131
1.39M
}
132
133
84.9k
static int reset(VPxWorker *const worker) {
134
84.9k
  int ok = 1;
135
84.9k
  worker->had_error = 0;
136
84.9k
  if (worker->status_ < VPX_WORKER_STATUS_OK) {
137
84.9k
#if CONFIG_MULTITHREAD
138
84.9k
    worker->impl_ = (VPxWorkerImpl *)vpx_calloc(1, sizeof(*worker->impl_));
139
84.9k
    if (worker->impl_ == NULL) {
140
0
      return 0;
141
0
    }
142
84.9k
    if (pthread_mutex_init(&worker->impl_->mutex_, NULL)) {
143
0
      goto Error;
144
0
    }
145
84.9k
    if (pthread_cond_init(&worker->impl_->condition_, NULL)) {
146
0
      pthread_mutex_destroy(&worker->impl_->mutex_);
147
0
      goto Error;
148
0
    }
149
84.9k
    pthread_mutex_lock(&worker->impl_->mutex_);
150
84.9k
    ok = !pthread_create(&worker->impl_->thread_, NULL, thread_loop, worker);
151
84.9k
    if (ok) worker->status_ = VPX_WORKER_STATUS_OK;
152
84.9k
    pthread_mutex_unlock(&worker->impl_->mutex_);
153
84.9k
    if (!ok) {
154
0
      pthread_mutex_destroy(&worker->impl_->mutex_);
155
0
      pthread_cond_destroy(&worker->impl_->condition_);
156
0
    Error:
157
0
      vpx_free(worker->impl_);
158
0
      worker->impl_ = NULL;
159
0
      return 0;
160
0
    }
161
#else
162
    worker->status_ = VPX_WORKER_STATUS_OK;
163
#endif
164
84.9k
  } else if (worker->status_ > VPX_WORKER_STATUS_OK) {
165
0
    ok = sync(worker);
166
0
  }
167
84.9k
  assert(!ok || (worker->status_ == VPX_WORKER_STATUS_OK));
168
84.9k
  return ok;
169
84.9k
}
170
171
684k
static void execute(VPxWorker *const worker) {
172
684k
  if (worker->hook != NULL) {
173
684k
    worker->had_error |= !worker->hook(worker->data1, worker->data2);
174
684k
  }
175
684k
}
176
177
396k
static void launch(VPxWorker *const worker) {
178
396k
#if CONFIG_MULTITHREAD
179
396k
  change_state(worker, VPX_WORKER_STATUS_WORKING);
180
#else
181
  execute(worker);
182
#endif
183
396k
}
184
185
103k
static void end(VPxWorker *const worker) {
186
103k
#if CONFIG_MULTITHREAD
187
103k
  if (worker->impl_ != NULL) {
188
84.9k
    change_state(worker, VPX_WORKER_STATUS_NOT_OK);
189
84.9k
    pthread_join(worker->impl_->thread_, NULL);
190
84.9k
    pthread_mutex_destroy(&worker->impl_->mutex_);
191
84.9k
    pthread_cond_destroy(&worker->impl_->condition_);
192
84.9k
    vpx_free(worker->impl_);
193
84.9k
    worker->impl_ = NULL;
194
84.9k
  }
195
#else
196
  worker->status_ = VPX_WORKER_STATUS_NOT_OK;
197
  assert(worker->impl_ == NULL);
198
#endif
199
103k
  assert(worker->status_ == VPX_WORKER_STATUS_NOT_OK);
200
103k
}
201
202
//------------------------------------------------------------------------------
203
204
static VPxWorkerInterface g_worker_interface = { init,   reset,   sync,
205
                                                 launch, execute, end };
206
207
0
int vpx_set_worker_interface(const VPxWorkerInterface *const winterface) {
208
0
  if (winterface == NULL || winterface->init == NULL ||
209
0
      winterface->reset == NULL || winterface->sync == NULL ||
210
0
      winterface->launch == NULL || winterface->execute == NULL ||
211
0
      winterface->end == NULL) {
212
0
    return 0;
213
0
  }
214
0
  g_worker_interface = *winterface;
215
0
  return 1;
216
0
}
217
218
617k
const VPxWorkerInterface *vpx_get_worker_interface(void) {
219
617k
  return &g_worker_interface;
220
617k
}
221
222
//------------------------------------------------------------------------------