Coverage Report

Created: 2023-06-07 06:31

/src/aom/aom_util/aom_thread.c
Line
Count
Source (jump to first uncovered line)
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 "aom_mem/aom_mem.h"
27
#include "aom_util/aom_thread.h"
28
29
#if CONFIG_MULTITHREAD
30
31
struct AVxWorkerImpl {
32
  pthread_mutex_t mutex_;
33
  pthread_cond_t condition_;
34
  pthread_t thread_;
35
};
36
37
//------------------------------------------------------------------------------
38
39
static void execute(AVxWorker *const worker);  // Forward declaration.
40
41
108k
static THREADFN thread_loop(void *ptr) {
42
108k
  AVxWorker *const worker = (AVxWorker *)ptr;
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
108k
  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
108k
    char thread_name[16];
60
108k
    strncpy(thread_name, worker->thread_name, sizeof(thread_name) - 1);
61
108k
    thread_name[sizeof(thread_name) - 1] = '\0';
62
108k
    pthread_setname_np(pthread_self(), thread_name);
63
108k
  }
64
108k
#endif
65
108k
  pthread_mutex_lock(&worker->impl_->mutex_);
66
1.26M
  for (;;) {
67
2.52M
    while (worker->status_ == OK) {  // wait in idling mode
68
1.26M
      pthread_cond_wait(&worker->impl_->condition_, &worker->impl_->mutex_);
69
1.26M
    }
70
1.26M
    if (worker->status_ == WORK) {
71
      // When worker->status_ is WORK, the main thread doesn't change
72
      // worker->status_ and will wait until the worker changes worker->status_
73
      // to OK. See change_state(). So the worker can safely call execute()
74
      // without holding worker->impl_->mutex_. When the worker reacquires
75
      // worker->impl_->mutex_, worker->status_ must still be WORK.
76
1.15M
      pthread_mutex_unlock(&worker->impl_->mutex_);
77
1.15M
      execute(worker);
78
1.15M
      pthread_mutex_lock(&worker->impl_->mutex_);
79
1.15M
      assert(worker->status_ == WORK);
80
0
      worker->status_ = OK;
81
      // signal to the main thread that we're done (for sync())
82
1.15M
      pthread_cond_signal(&worker->impl_->condition_);
83
1.15M
    } else {
84
110k
      assert(worker->status_ == NOT_OK);  // finish the worker
85
0
      break;
86
110k
    }
87
1.26M
  }
88
108k
  pthread_mutex_unlock(&worker->impl_->mutex_);
89
108k
  return THREAD_RETURN(NULL);  // Thread is finished
90
108k
}
91
92
// main thread state control
93
5.66M
static void change_state(AVxWorker *const worker, AVxWorkerStatus 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
5.66M
  if (worker->impl_ == NULL) return;
98
99
4.72M
  pthread_mutex_lock(&worker->impl_->mutex_);
100
4.72M
  if (worker->status_ >= OK) {
101
    // wait for the worker to finish
102
4.79M
    while (worker->status_ != OK) {
103
69.1k
      pthread_cond_wait(&worker->impl_->condition_, &worker->impl_->mutex_);
104
69.1k
    }
105
    // assign new status and release the working thread if needed
106
4.72M
    if (new_status != OK) {
107
1.26M
      worker->status_ = new_status;
108
1.26M
      pthread_cond_signal(&worker->impl_->condition_);
109
1.26M
    }
110
4.72M
  }
111
4.72M
  pthread_mutex_unlock(&worker->impl_->mutex_);
112
4.72M
}
113
114
#endif  // CONFIG_MULTITHREAD
115
116
//------------------------------------------------------------------------------
117
118
141k
static void init(AVxWorker *const worker) {
119
141k
  memset(worker, 0, sizeof(*worker));
120
141k
  worker->status_ = NOT_OK;
121
141k
}
122
123
4.40M
static int sync(AVxWorker *const worker) {
124
4.40M
#if CONFIG_MULTITHREAD
125
4.40M
  change_state(worker, OK);
126
4.40M
#endif
127
4.40M
  assert(worker->status_ <= OK);
128
0
  return !worker->had_error;
129
4.40M
}
130
131
108k
static int reset(AVxWorker *const worker) {
132
108k
  int ok = 1;
133
108k
  worker->had_error = 0;
134
108k
  if (worker->status_ < OK) {
135
108k
#if CONFIG_MULTITHREAD
136
108k
    worker->impl_ = (AVxWorkerImpl *)aom_calloc(1, sizeof(*worker->impl_));
137
108k
    if (worker->impl_ == NULL) {
138
0
      return 0;
139
0
    }
140
108k
    if (pthread_mutex_init(&worker->impl_->mutex_, NULL)) {
141
0
      goto Error;
142
0
    }
143
108k
    if (pthread_cond_init(&worker->impl_->condition_, NULL)) {
144
0
      pthread_mutex_destroy(&worker->impl_->mutex_);
145
0
      goto Error;
146
0
    }
147
108k
    pthread_mutex_lock(&worker->impl_->mutex_);
148
108k
    ok = !pthread_create(&worker->impl_->thread_, NULL, thread_loop, worker);
149
108k
    if (ok) worker->status_ = OK;
150
108k
    pthread_mutex_unlock(&worker->impl_->mutex_);
151
108k
    if (!ok) {
152
0
      pthread_mutex_destroy(&worker->impl_->mutex_);
153
0
      pthread_cond_destroy(&worker->impl_->condition_);
154
0
    Error:
155
0
      aom_free(worker->impl_);
156
0
      worker->impl_ = NULL;
157
0
      return 0;
158
0
    }
159
#else
160
    worker->status_ = OK;
161
#endif
162
108k
  } else if (worker->status_ > OK) {
163
0
    ok = sync(worker);
164
0
  }
165
108k
  assert(!ok || (worker->status_ == OK));
166
0
  return ok;
167
108k
}
168
169
1.52M
static void execute(AVxWorker *const worker) {
170
1.52M
  if (worker->hook != NULL) {
171
1.52M
    worker->had_error |= !worker->hook(worker->data1, worker->data2);
172
1.52M
  }
173
1.52M
}
174
175
1.15M
static void launch(AVxWorker *const worker) {
176
1.15M
#if CONFIG_MULTITHREAD
177
1.15M
  change_state(worker, WORK);
178
#else
179
  execute(worker);
180
#endif
181
1.15M
}
182
183
141k
static void end(AVxWorker *const worker) {
184
141k
#if CONFIG_MULTITHREAD
185
141k
  if (worker->impl_ != NULL) {
186
108k
    change_state(worker, NOT_OK);
187
108k
    pthread_join(worker->impl_->thread_, NULL);
188
108k
    pthread_mutex_destroy(&worker->impl_->mutex_);
189
108k
    pthread_cond_destroy(&worker->impl_->condition_);
190
108k
    aom_free(worker->impl_);
191
108k
    worker->impl_ = NULL;
192
108k
  }
193
#else
194
  worker->status_ = NOT_OK;
195
  assert(worker->impl_ == NULL);
196
#endif
197
141k
  assert(worker->status_ == NOT_OK);
198
141k
}
199
200
//------------------------------------------------------------------------------
201
202
static AVxWorkerInterface g_worker_interface = { init,   reset,   sync,
203
                                                 launch, execute, end };
204
205
0
int aom_set_worker_interface(const AVxWorkerInterface *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
1.52M
const AVxWorkerInterface *aom_get_worker_interface(void) {
217
1.52M
  return &g_worker_interface;
218
1.52M
}
219
220
//------------------------------------------------------------------------------