Coverage Report

Created: 2026-01-17 06:24

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