/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 "config/aom_config.h" |
27 | | |
28 | | #include "aom_mem/aom_mem.h" |
29 | | #include "aom_ports/sanitizer.h" |
30 | | #include "aom_util/aom_pthread.h" |
31 | | #include "aom_util/aom_thread.h" |
32 | | |
33 | | #if CONFIG_MULTITHREAD |
34 | | |
35 | | struct AVxWorkerImpl { |
36 | | pthread_mutex_t mutex_; |
37 | | pthread_cond_t condition_; |
38 | | pthread_t thread_; |
39 | | }; |
40 | | |
41 | | //------------------------------------------------------------------------------ |
42 | | |
43 | | static void execute(AVxWorker *const worker); // Forward declaration. |
44 | | |
45 | 125k | static THREADFN thread_loop(void *ptr) { |
46 | 125k | AVxWorker *const worker = (AVxWorker *)ptr; |
47 | | #ifdef __APPLE__ |
48 | | if (worker->thread_name != NULL) { |
49 | | // Apple's version of pthread_setname_np takes one argument and operates on |
50 | | // the current thread only. The maximum size of the thread_name buffer was |
51 | | // noted in the Chromium source code and was confirmed by experiments. If |
52 | | // thread_name is too long, pthread_setname_np returns -1 with errno |
53 | | // ENAMETOOLONG (63). |
54 | | char thread_name[64]; |
55 | | strncpy(thread_name, worker->thread_name, sizeof(thread_name) - 1); |
56 | | thread_name[sizeof(thread_name) - 1] = '\0'; |
57 | | pthread_setname_np(thread_name); |
58 | | } |
59 | | #elif (defined(__GLIBC__) && !defined(__GNU__)) || defined(__BIONIC__) |
60 | 125k | if (worker->thread_name != NULL) { |
61 | | // Linux and Android require names (with nul) fit in 16 chars, otherwise |
62 | | // pthread_setname_np() returns ERANGE (34). |
63 | 125k | char thread_name[16]; |
64 | 125k | strncpy(thread_name, worker->thread_name, sizeof(thread_name) - 1); |
65 | 125k | thread_name[sizeof(thread_name) - 1] = '\0'; |
66 | 125k | pthread_setname_np(pthread_self(), thread_name); |
67 | 125k | } |
68 | 125k | #endif |
69 | 125k | pthread_mutex_lock(&worker->impl_->mutex_); |
70 | 2.44M | for (;;) { |
71 | 4.89M | while (worker->status_ == AVX_WORKER_STATUS_OK) { // wait in idling mode |
72 | 2.44M | pthread_cond_wait(&worker->impl_->condition_, &worker->impl_->mutex_); |
73 | 2.44M | } |
74 | 2.44M | if (worker->status_ == AVX_WORKER_STATUS_WORKING) { |
75 | | // When worker->status_ is AVX_WORKER_STATUS_WORKING, the main thread |
76 | | // doesn't change worker->status_ and will wait until the worker changes |
77 | | // worker->status_ to AVX_WORKER_STATUS_OK. See change_state(). So the |
78 | | // worker can safely call execute() without holding worker->impl_->mutex_. |
79 | | // When the worker reacquires worker->impl_->mutex_, worker->status_ must |
80 | | // still be AVX_WORKER_STATUS_WORKING. |
81 | 2.32M | pthread_mutex_unlock(&worker->impl_->mutex_); |
82 | 2.32M | execute(worker); |
83 | 2.32M | pthread_mutex_lock(&worker->impl_->mutex_); |
84 | 2.32M | assert(worker->status_ == AVX_WORKER_STATUS_WORKING); |
85 | 2.32M | worker->status_ = AVX_WORKER_STATUS_OK; |
86 | | // signal to the main thread that we're done (for sync()) |
87 | 2.32M | pthread_cond_signal(&worker->impl_->condition_); |
88 | 2.32M | } else { |
89 | 125k | assert(worker->status_ == AVX_WORKER_STATUS_NOT_OK); // finish the worker |
90 | 125k | break; |
91 | 125k | } |
92 | 2.44M | } |
93 | 127k | pthread_mutex_unlock(&worker->impl_->mutex_); |
94 | 127k | return THREAD_EXIT_SUCCESS; // Thread is finished |
95 | 125k | } |
96 | | |
97 | | // main thread state control |
98 | 7.15M | static void change_state(AVxWorker *const worker, AVxWorkerStatus new_status) { |
99 | | // No-op when attempting to change state on a thread that didn't come up. |
100 | | // Checking status_ without acquiring the lock first would result in a data |
101 | | // race. |
102 | 7.15M | if (worker->impl_ == NULL) return; |
103 | | |
104 | 6.47M | pthread_mutex_lock(&worker->impl_->mutex_); |
105 | 6.47M | if (worker->status_ >= AVX_WORKER_STATUS_OK) { |
106 | | // wait for the worker to finish |
107 | 6.57M | while (worker->status_ != AVX_WORKER_STATUS_OK) { |
108 | 98.7k | pthread_cond_wait(&worker->impl_->condition_, &worker->impl_->mutex_); |
109 | 98.7k | } |
110 | | // assign new status and release the working thread if needed |
111 | 6.47M | if (new_status != AVX_WORKER_STATUS_OK) { |
112 | 2.44M | worker->status_ = new_status; |
113 | 2.44M | pthread_cond_signal(&worker->impl_->condition_); |
114 | 2.44M | } |
115 | 6.47M | } |
116 | 6.47M | pthread_mutex_unlock(&worker->impl_->mutex_); |
117 | 6.47M | } |
118 | | |
119 | | #endif // CONFIG_MULTITHREAD |
120 | | |
121 | | //------------------------------------------------------------------------------ |
122 | | |
123 | 161k | static void init(AVxWorker *const worker) { |
124 | 161k | memset(worker, 0, sizeof(*worker)); |
125 | 161k | worker->status_ = AVX_WORKER_STATUS_NOT_OK; |
126 | 161k | } |
127 | | |
128 | 4.71M | static int sync(AVxWorker *const worker) { |
129 | 4.71M | #if CONFIG_MULTITHREAD |
130 | 4.71M | change_state(worker, AVX_WORKER_STATUS_OK); |
131 | 4.71M | #endif |
132 | 4.71M | assert(worker->status_ <= AVX_WORKER_STATUS_OK); |
133 | 4.71M | return !worker->had_error; |
134 | 4.71M | } |
135 | | |
136 | 125k | static int reset(AVxWorker *const worker) { |
137 | 125k | int ok = 1; |
138 | 125k | worker->had_error = 0; |
139 | 125k | if (worker->status_ < AVX_WORKER_STATUS_OK) { |
140 | 125k | #if CONFIG_MULTITHREAD |
141 | 125k | worker->impl_ = (AVxWorkerImpl *)aom_calloc(1, sizeof(*worker->impl_)); |
142 | 125k | if (worker->impl_ == NULL) { |
143 | 0 | return 0; |
144 | 0 | } |
145 | 125k | if (pthread_mutex_init(&worker->impl_->mutex_, NULL)) { |
146 | 0 | goto Error; |
147 | 0 | } |
148 | 125k | if (pthread_cond_init(&worker->impl_->condition_, NULL)) { |
149 | 0 | pthread_mutex_destroy(&worker->impl_->mutex_); |
150 | 0 | goto Error; |
151 | 0 | } |
152 | 125k | pthread_attr_t attr; |
153 | 125k | if (pthread_attr_init(&attr)) goto Error2; |
154 | | // Debug ASan builds require at least ~1MiB of stack; prevents |
155 | | // failures on macOS arm64 where the default is 512KiB. |
156 | | // See: https://crbug.com/aomedia/3379 |
157 | | #if defined(AOM_ADDRESS_SANITIZER) && defined(__APPLE__) && AOM_ARCH_ARM && \ |
158 | | !defined(NDEBUG) |
159 | | const size_t kMinStackSize = 1024 * 1024; |
160 | | #else |
161 | 125k | const size_t kMinStackSize = 256 * 1024; |
162 | 125k | #endif |
163 | 125k | size_t stacksize; |
164 | 125k | if (!pthread_attr_getstacksize(&attr, &stacksize)) { |
165 | 125k | if (stacksize < kMinStackSize && |
166 | 125k | pthread_attr_setstacksize(&attr, kMinStackSize)) { |
167 | 0 | pthread_attr_destroy(&attr); |
168 | 0 | goto Error2; |
169 | 0 | } |
170 | 125k | } |
171 | 125k | pthread_mutex_lock(&worker->impl_->mutex_); |
172 | 125k | ok = !pthread_create(&worker->impl_->thread_, &attr, thread_loop, worker); |
173 | 125k | if (ok) worker->status_ = AVX_WORKER_STATUS_OK; |
174 | 125k | pthread_mutex_unlock(&worker->impl_->mutex_); |
175 | 125k | pthread_attr_destroy(&attr); |
176 | 125k | if (!ok) { |
177 | 0 | Error2: |
178 | 0 | pthread_mutex_destroy(&worker->impl_->mutex_); |
179 | 0 | pthread_cond_destroy(&worker->impl_->condition_); |
180 | 0 | Error: |
181 | 0 | aom_free(worker->impl_); |
182 | 0 | worker->impl_ = NULL; |
183 | 0 | return 0; |
184 | 0 | } |
185 | | #else |
186 | | worker->status_ = AVX_WORKER_STATUS_OK; |
187 | | #endif |
188 | 125k | } else if (worker->status_ > AVX_WORKER_STATUS_OK) { |
189 | 0 | ok = sync(worker); |
190 | 0 | } |
191 | 125k | assert(!ok || (worker->status_ == AVX_WORKER_STATUS_OK)); |
192 | 125k | return ok; |
193 | 125k | } |
194 | | |
195 | 2.75M | static void execute(AVxWorker *const worker) { |
196 | 2.75M | if (worker->hook != NULL) { |
197 | 2.75M | worker->had_error |= !worker->hook(worker->data1, worker->data2); |
198 | 2.75M | } |
199 | 2.75M | } |
200 | | |
201 | 2.32M | static void launch(AVxWorker *const worker) { |
202 | 2.32M | #if CONFIG_MULTITHREAD |
203 | 2.32M | change_state(worker, AVX_WORKER_STATUS_WORKING); |
204 | | #else |
205 | | execute(worker); |
206 | | #endif |
207 | 2.32M | } |
208 | | |
209 | 161k | static void end(AVxWorker *const worker) { |
210 | 161k | #if CONFIG_MULTITHREAD |
211 | 161k | if (worker->impl_ != NULL) { |
212 | 125k | change_state(worker, AVX_WORKER_STATUS_NOT_OK); |
213 | 125k | pthread_join(worker->impl_->thread_, NULL); |
214 | 125k | pthread_mutex_destroy(&worker->impl_->mutex_); |
215 | 125k | pthread_cond_destroy(&worker->impl_->condition_); |
216 | 125k | aom_free(worker->impl_); |
217 | 125k | worker->impl_ = NULL; |
218 | 125k | } |
219 | | #else |
220 | | worker->status_ = AVX_WORKER_STATUS_NOT_OK; |
221 | | assert(worker->impl_ == NULL); |
222 | | #endif |
223 | 161k | assert(worker->status_ == AVX_WORKER_STATUS_NOT_OK); |
224 | 161k | } |
225 | | |
226 | | //------------------------------------------------------------------------------ |
227 | | |
228 | | static AVxWorkerInterface g_worker_interface = { init, reset, sync, |
229 | | launch, execute, end }; |
230 | | |
231 | 0 | int aom_set_worker_interface(const AVxWorkerInterface *const winterface) { |
232 | 0 | if (winterface == NULL || winterface->init == NULL || |
233 | 0 | winterface->reset == NULL || winterface->sync == NULL || |
234 | 0 | winterface->launch == NULL || winterface->execute == NULL || |
235 | 0 | winterface->end == NULL) { |
236 | 0 | return 0; |
237 | 0 | } |
238 | 0 | g_worker_interface = *winterface; |
239 | 0 | return 1; |
240 | 0 | } |
241 | | |
242 | 1.40M | const AVxWorkerInterface *aom_get_worker_interface(void) { |
243 | 1.40M | return &g_worker_interface; |
244 | 1.40M | } |
245 | | |
246 | | //------------------------------------------------------------------------------ |