Coverage Report

Created: 2024-09-06 07:53

/src/libvpx/vpx_util/vpx_thread.c
Line
Count
Source (jump to first uncovered line)
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
#ifdef __APPLE__
43
  if (worker->thread_name != NULL) {
44
    // Apple's version of pthread_setname_np takes one argument and operates on
45
    // the current thread only. The maximum size of the thread_name buffer was
46
    // noted in the Chromium source code and was confirmed by experiments. If
47
    // thread_name is too long, pthread_setname_np returns -1 with errno
48
    // ENAMETOOLONG (63).
49
    char thread_name[64];
50
    strncpy(thread_name, worker->thread_name, sizeof(thread_name) - 1);
51
    thread_name[sizeof(thread_name) - 1] = '\0';
52
    pthread_setname_np(thread_name);
53
  }
54
#elif (defined(__GLIBC__) && !defined(__GNU__)) || defined(__BIONIC__)
55
0
  if (worker->thread_name != NULL) {
56
    // Linux and Android require names (with nul) fit in 16 chars, otherwise
57
    // pthread_setname_np() returns ERANGE (34).
58
0
    char thread_name[16];
59
0
    strncpy(thread_name, worker->thread_name, sizeof(thread_name) - 1);
60
0
    thread_name[sizeof(thread_name) - 1] = '\0';
61
0
    pthread_setname_np(pthread_self(), thread_name);
62
0
  }
63
0
#endif
64
0
  pthread_mutex_lock(&worker->impl_->mutex_);
65
0
  for (;;) {
66
0
    while (worker->status_ == VPX_WORKER_STATUS_OK) {  // wait in idling mode
67
0
      pthread_cond_wait(&worker->impl_->condition_, &worker->impl_->mutex_);
68
0
    }
69
0
    if (worker->status_ == VPX_WORKER_STATUS_WORKING) {
70
      // When worker->status_ is VPX_WORKER_STATUS_WORKING, the main thread
71
      // doesn't change worker->status_ and will wait until the worker changes
72
      // worker->status_ to VPX_WORKER_STATUS_OK. See change_state(). So the
73
      // worker can safely call execute() without holding worker->impl_->mutex_.
74
      // When the worker reacquires worker->impl_->mutex_, worker->status_ must
75
      // still be VPX_WORKER_STATUS_WORKING.
76
0
      pthread_mutex_unlock(&worker->impl_->mutex_);
77
0
      execute(worker);
78
0
      pthread_mutex_lock(&worker->impl_->mutex_);
79
0
      assert(worker->status_ == VPX_WORKER_STATUS_WORKING);
80
0
      worker->status_ = VPX_WORKER_STATUS_OK;
81
      // signal to the main thread that we're done (for sync())
82
0
      pthread_cond_signal(&worker->impl_->condition_);
83
0
    } else {
84
0
      assert(worker->status_ == VPX_WORKER_STATUS_NOT_OK);  // finish the worker
85
0
      break;
86
0
    }
87
0
  }
88
0
  pthread_mutex_unlock(&worker->impl_->mutex_);
89
0
  return THREAD_EXIT_SUCCESS;  // Thread is finished
90
0
}
91
92
// main thread state control
93
0
static void change_state(VPxWorker *const worker, VPxWorkerStatus new_status) {
94
  // No-op when attempting to change state on a thread that didn't come up.
95
  // Checking status_ without acquiring the lock first would result in a data
96
  // race.
97
0
  if (worker->impl_ == NULL) return;
98
99
0
  pthread_mutex_lock(&worker->impl_->mutex_);
100
0
  if (worker->status_ >= VPX_WORKER_STATUS_OK) {
101
    // wait for the worker to finish
102
0
    while (worker->status_ != VPX_WORKER_STATUS_OK) {
103
0
      pthread_cond_wait(&worker->impl_->condition_, &worker->impl_->mutex_);
104
0
    }
105
    // assign new status and release the working thread if needed
106
0
    if (new_status != VPX_WORKER_STATUS_OK) {
107
0
      worker->status_ = new_status;
108
0
      pthread_cond_signal(&worker->impl_->condition_);
109
0
    }
110
0
  }
111
0
  pthread_mutex_unlock(&worker->impl_->mutex_);
112
0
}
113
114
#endif  // CONFIG_MULTITHREAD
115
116
//------------------------------------------------------------------------------
117
118
0
static void init(VPxWorker *const worker) {
119
0
  memset(worker, 0, sizeof(*worker));
120
0
  worker->status_ = VPX_WORKER_STATUS_NOT_OK;
121
0
}
122
123
0
static int sync(VPxWorker *const worker) {
124
0
#if CONFIG_MULTITHREAD
125
0
  change_state(worker, VPX_WORKER_STATUS_OK);
126
0
#endif
127
0
  assert(worker->status_ <= VPX_WORKER_STATUS_OK);
128
0
  return !worker->had_error;
129
0
}
130
131
0
static int reset(VPxWorker *const worker) {
132
0
  int ok = 1;
133
0
  worker->had_error = 0;
134
0
  if (worker->status_ < VPX_WORKER_STATUS_OK) {
135
0
#if CONFIG_MULTITHREAD
136
0
    worker->impl_ = (VPxWorkerImpl *)vpx_calloc(1, sizeof(*worker->impl_));
137
0
    if (worker->impl_ == NULL) {
138
0
      return 0;
139
0
    }
140
0
    if (pthread_mutex_init(&worker->impl_->mutex_, NULL)) {
141
0
      goto Error;
142
0
    }
143
0
    if (pthread_cond_init(&worker->impl_->condition_, NULL)) {
144
0
      pthread_mutex_destroy(&worker->impl_->mutex_);
145
0
      goto Error;
146
0
    }
147
0
    pthread_mutex_lock(&worker->impl_->mutex_);
148
0
    ok = !pthread_create(&worker->impl_->thread_, NULL, thread_loop, worker);
149
0
    if (ok) worker->status_ = VPX_WORKER_STATUS_OK;
150
0
    pthread_mutex_unlock(&worker->impl_->mutex_);
151
0
    if (!ok) {
152
0
      pthread_mutex_destroy(&worker->impl_->mutex_);
153
0
      pthread_cond_destroy(&worker->impl_->condition_);
154
0
    Error:
155
0
      vpx_free(worker->impl_);
156
0
      worker->impl_ = NULL;
157
0
      return 0;
158
0
    }
159
#else
160
    worker->status_ = VPX_WORKER_STATUS_OK;
161
#endif
162
0
  } else if (worker->status_ > VPX_WORKER_STATUS_OK) {
163
0
    ok = sync(worker);
164
0
  }
165
0
  assert(!ok || (worker->status_ == VPX_WORKER_STATUS_OK));
166
0
  return ok;
167
0
}
168
169
0
static void execute(VPxWorker *const worker) {
170
0
  if (worker->hook != NULL) {
171
0
    worker->had_error |= !worker->hook(worker->data1, worker->data2);
172
0
  }
173
0
}
174
175
0
static void launch(VPxWorker *const worker) {
176
0
#if CONFIG_MULTITHREAD
177
0
  change_state(worker, VPX_WORKER_STATUS_WORKING);
178
#else
179
  execute(worker);
180
#endif
181
0
}
182
183
0
static void end(VPxWorker *const worker) {
184
0
#if CONFIG_MULTITHREAD
185
0
  if (worker->impl_ != NULL) {
186
0
    change_state(worker, VPX_WORKER_STATUS_NOT_OK);
187
0
    pthread_join(worker->impl_->thread_, NULL);
188
0
    pthread_mutex_destroy(&worker->impl_->mutex_);
189
0
    pthread_cond_destroy(&worker->impl_->condition_);
190
0
    vpx_free(worker->impl_);
191
0
    worker->impl_ = NULL;
192
0
  }
193
#else
194
  worker->status_ = VPX_WORKER_STATUS_NOT_OK;
195
  assert(worker->impl_ == NULL);
196
#endif
197
0
  assert(worker->status_ == VPX_WORKER_STATUS_NOT_OK);
198
0
}
199
200
//------------------------------------------------------------------------------
201
202
static VPxWorkerInterface g_worker_interface = { init,   reset,   sync,
203
                                                 launch, execute, end };
204
205
0
int vpx_set_worker_interface(const VPxWorkerInterface *const winterface) {
206
0
  if (winterface == NULL || winterface->init == NULL ||
207
0
      winterface->reset == NULL || winterface->sync == NULL ||
208
0
      winterface->launch == NULL || winterface->execute == NULL ||
209
0
      winterface->end == NULL) {
210
0
    return 0;
211
0
  }
212
0
  g_worker_interface = *winterface;
213
0
  return 1;
214
0
}
215
216
0
const VPxWorkerInterface *vpx_get_worker_interface(void) {
217
0
  return &g_worker_interface;
218
0
}
219
220
//------------------------------------------------------------------------------