Coverage Report

Created: 2026-02-14 06:59

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
0
static THREADFN thread_loop(void *ptr) {
41
0
  VPxWorker *const worker = (VPxWorker *)ptr;
42
0
#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
0
  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
0
    char thread_name[16];
60
0
    strncpy(thread_name, worker->thread_name, sizeof(thread_name) - 1);
61
0
    thread_name[sizeof(thread_name) - 1] = '\0';
62
0
    pthread_setname_np(pthread_self(), thread_name);
63
0
  }
64
0
#endif
65
0
#endif
66
0
  pthread_mutex_lock(&worker->impl_->mutex_);
67
0
  for (;;) {
68
0
    while (worker->status_ == VPX_WORKER_STATUS_OK) {  // wait in idling mode
69
0
      pthread_cond_wait(&worker->impl_->condition_, &worker->impl_->mutex_);
70
0
    }
71
0
    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
0
      pthread_mutex_unlock(&worker->impl_->mutex_);
79
0
      execute(worker);
80
0
      pthread_mutex_lock(&worker->impl_->mutex_);
81
0
      assert(worker->status_ == VPX_WORKER_STATUS_WORKING);
82
0
      worker->status_ = VPX_WORKER_STATUS_OK;
83
      // signal to the main thread that we're done (for sync())
84
0
      pthread_cond_signal(&worker->impl_->condition_);
85
0
    } else {
86
0
      assert(worker->status_ == VPX_WORKER_STATUS_NOT_OK);  // finish the worker
87
0
      break;
88
0
    }
89
0
  }
90
0
  pthread_mutex_unlock(&worker->impl_->mutex_);
91
0
  return THREAD_EXIT_SUCCESS;  // Thread is finished
92
0
}
93
94
// main thread state control
95
996k
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
996k
  if (worker->impl_ == NULL) return;
100
101
0
  pthread_mutex_lock(&worker->impl_->mutex_);
102
0
  if (worker->status_ >= VPX_WORKER_STATUS_OK) {
103
    // wait for the worker to finish
104
0
    while (worker->status_ != VPX_WORKER_STATUS_OK) {
105
0
      pthread_cond_wait(&worker->impl_->condition_, &worker->impl_->mutex_);
106
0
    }
107
    // assign new status and release the working thread if needed
108
0
    if (new_status != VPX_WORKER_STATUS_OK) {
109
0
      worker->status_ = new_status;
110
0
      pthread_cond_signal(&worker->impl_->condition_);
111
0
    }
112
0
  }
113
0
  pthread_mutex_unlock(&worker->impl_->mutex_);
114
0
}
115
116
#endif  // CONFIG_MULTITHREAD
117
118
//------------------------------------------------------------------------------
119
120
14.5k
static void init(VPxWorker *const worker) {
121
14.5k
  memset(worker, 0, sizeof(*worker));
122
14.5k
  worker->status_ = VPX_WORKER_STATUS_NOT_OK;
123
14.5k
}
124
125
996k
static int sync(VPxWorker *const worker) {
126
996k
#if CONFIG_MULTITHREAD
127
996k
  change_state(worker, VPX_WORKER_STATUS_OK);
128
996k
#endif
129
996k
  assert(worker->status_ <= VPX_WORKER_STATUS_OK);
130
996k
  return !worker->had_error;
131
996k
}
132
133
0
static int reset(VPxWorker *const worker) {
134
0
  int ok = 1;
135
0
  worker->had_error = 0;
136
0
  if (worker->status_ < VPX_WORKER_STATUS_OK) {
137
0
#if CONFIG_MULTITHREAD
138
0
    worker->impl_ = (VPxWorkerImpl *)vpx_calloc(1, sizeof(*worker->impl_));
139
0
    if (worker->impl_ == NULL) {
140
0
      return 0;
141
0
    }
142
0
    if (pthread_mutex_init(&worker->impl_->mutex_, NULL)) {
143
0
      goto Error;
144
0
    }
145
0
    if (pthread_cond_init(&worker->impl_->condition_, NULL)) {
146
0
      pthread_mutex_destroy(&worker->impl_->mutex_);
147
0
      goto Error;
148
0
    }
149
0
    pthread_mutex_lock(&worker->impl_->mutex_);
150
0
    ok = !pthread_create(&worker->impl_->thread_, NULL, thread_loop, worker);
151
0
    if (ok) worker->status_ = VPX_WORKER_STATUS_OK;
152
0
    pthread_mutex_unlock(&worker->impl_->mutex_);
153
0
    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
0
  } else if (worker->status_ > VPX_WORKER_STATUS_OK) {
165
0
    ok = sync(worker);
166
0
  }
167
0
  assert(!ok || (worker->status_ == VPX_WORKER_STATUS_OK));
168
0
  return ok;
169
0
}
170
171
604k
static void execute(VPxWorker *const worker) {
172
604k
  if (worker->hook != NULL) {
173
604k
    worker->had_error |= !worker->hook(worker->data1, worker->data2);
174
604k
  }
175
604k
}
176
177
0
static void launch(VPxWorker *const worker) {
178
0
#if CONFIG_MULTITHREAD
179
0
  change_state(worker, VPX_WORKER_STATUS_WORKING);
180
#else
181
  execute(worker);
182
#endif
183
0
}
184
185
14.5k
static void end(VPxWorker *const worker) {
186
14.5k
#if CONFIG_MULTITHREAD
187
14.5k
  if (worker->impl_ != NULL) {
188
0
    change_state(worker, VPX_WORKER_STATUS_NOT_OK);
189
0
    pthread_join(worker->impl_->thread_, NULL);
190
0
    pthread_mutex_destroy(&worker->impl_->mutex_);
191
0
    pthread_cond_destroy(&worker->impl_->condition_);
192
0
    vpx_free(worker->impl_);
193
0
    worker->impl_ = NULL;
194
0
  }
195
#else
196
  worker->status_ = VPX_WORKER_STATUS_NOT_OK;
197
  assert(worker->impl_ == NULL);
198
#endif
199
14.5k
  assert(worker->status_ == VPX_WORKER_STATUS_NOT_OK);
200
14.5k
}
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
444k
const VPxWorkerInterface *vpx_get_worker_interface(void) {
219
444k
  return &g_worker_interface;
220
444k
}
221
222
//------------------------------------------------------------------------------