/src/libwebp/src/utils/thread_utils.c
Line | Count | Source |
1 | | // Copyright 2011 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 | | // Author: Skal (pascal.massimino@gmail.com) |
13 | | |
14 | | #include "src/utils/thread_utils.h" |
15 | | |
16 | | #include <assert.h> |
17 | | #include <string.h> // for memset() |
18 | | |
19 | | #include "src/utils/bounds_safety.h" |
20 | | #include "src/utils/utils.h" |
21 | | |
22 | | WEBP_ASSUME_UNSAFE_INDEXABLE_ABI |
23 | | |
24 | | #ifdef WEBP_USE_THREAD |
25 | | |
26 | | #if defined(_WIN32) |
27 | | |
28 | | #include <windows.h> |
29 | | typedef HANDLE pthread_t; |
30 | | |
31 | | #if _WIN32_WINNT < 0x0600 |
32 | | #error _WIN32_WINNT must target Windows Vista / Server 2008 or newer. |
33 | | #endif |
34 | | typedef SRWLOCK pthread_mutex_t; |
35 | | typedef CONDITION_VARIABLE pthread_cond_t; |
36 | | |
37 | | #ifndef WINAPI_FAMILY_PARTITION |
38 | | #define WINAPI_PARTITION_DESKTOP 1 |
39 | | #define WINAPI_FAMILY_PARTITION(x) x |
40 | | #endif |
41 | | |
42 | | #if !WINAPI_FAMILY_PARTITION(WINAPI_PARTITION_DESKTOP) |
43 | | #define USE_CREATE_THREAD |
44 | | #endif |
45 | | |
46 | | #else // !_WIN32 |
47 | | |
48 | | #include <pthread.h> |
49 | | |
50 | | #endif // _WIN32 |
51 | | |
52 | | typedef struct { |
53 | | pthread_mutex_t mutex; |
54 | | pthread_cond_t condition; |
55 | | pthread_t thread; |
56 | | } WebPWorkerImpl; |
57 | | |
58 | | #if defined(_WIN32) |
59 | | |
60 | | //------------------------------------------------------------------------------ |
61 | | // simplistic pthread emulation layer |
62 | | |
63 | | #include <process.h> |
64 | | |
65 | | // _beginthreadex requires __stdcall |
66 | | #define THREADFN unsigned int __stdcall |
67 | | #define THREAD_RETURN(val) (unsigned int)((DWORD_PTR)val) |
68 | | |
69 | | static int pthread_create(pthread_t* const thread, const void* attr, |
70 | | unsigned int(__stdcall* start)(void*), void* arg) { |
71 | | (void)attr; |
72 | | #ifdef USE_CREATE_THREAD |
73 | | *thread = CreateThread(/*lpThreadAttributes=*/NULL, |
74 | | /*dwStackSize=*/0, start, arg, /*dwStackSize=*/0, |
75 | | /*lpThreadId=*/NULL); |
76 | | #else |
77 | | *thread = |
78 | | (pthread_t)_beginthreadex(/*security=*/NULL, |
79 | | /*stack_size=*/0, start, arg, /*initflag=*/0, |
80 | | /*thrdaddr=*/NULL); |
81 | | #endif |
82 | | if (*thread == NULL) return 1; |
83 | | SetThreadPriority(*thread, THREAD_PRIORITY_ABOVE_NORMAL); |
84 | | return 0; |
85 | | } |
86 | | |
87 | | static int pthread_join(pthread_t thread, void** value_ptr) { |
88 | | (void)value_ptr; |
89 | | return (WaitForSingleObject(thread, INFINITE) != WAIT_OBJECT_0 || |
90 | | CloseHandle(thread) == 0); |
91 | | } |
92 | | |
93 | | // Mutex |
94 | | static int pthread_mutex_init(pthread_mutex_t* const mutex, void* mutexattr) { |
95 | | (void)mutexattr; |
96 | | InitializeSRWLock(mutex); |
97 | | return 0; |
98 | | } |
99 | | |
100 | | static int pthread_mutex_lock(pthread_mutex_t* const mutex) { |
101 | | AcquireSRWLockExclusive(mutex); |
102 | | return 0; |
103 | | } |
104 | | |
105 | | static int pthread_mutex_unlock(pthread_mutex_t* const mutex) { |
106 | | ReleaseSRWLockExclusive(mutex); |
107 | | return 0; |
108 | | } |
109 | | |
110 | | static int pthread_mutex_destroy(pthread_mutex_t* const mutex) { |
111 | | (void)mutex; |
112 | | return 0; |
113 | | } |
114 | | |
115 | | // Condition |
116 | | static int pthread_cond_destroy(pthread_cond_t* const condition) { |
117 | | (void)condition; |
118 | | return 0; |
119 | | } |
120 | | |
121 | | static int pthread_cond_init(pthread_cond_t* const condition, void* cond_attr) { |
122 | | (void)cond_attr; |
123 | | InitializeConditionVariable(condition); |
124 | | return 0; |
125 | | } |
126 | | |
127 | | static int pthread_cond_signal(pthread_cond_t* const condition) { |
128 | | WakeConditionVariable(condition); |
129 | | return 0; |
130 | | } |
131 | | |
132 | | static int pthread_cond_wait(pthread_cond_t* const condition, |
133 | | pthread_mutex_t* const mutex) { |
134 | | const int ok = SleepConditionVariableSRW(condition, mutex, INFINITE, 0); |
135 | | return !ok; |
136 | | } |
137 | | |
138 | | #else // !_WIN32 |
139 | | #define THREADFN void* |
140 | 0 | #define THREAD_RETURN(val) val |
141 | | #endif // _WIN32 |
142 | | |
143 | | //------------------------------------------------------------------------------ |
144 | | |
145 | 0 | static THREADFN ThreadLoop(void* ptr) { |
146 | 0 | WebPWorker* const worker = (WebPWorker*)ptr; |
147 | 0 | WebPWorkerImpl* const impl = (WebPWorkerImpl*)worker->impl; |
148 | 0 | int done = 0; |
149 | 0 | while (!done) { |
150 | 0 | pthread_mutex_lock(&impl->mutex); |
151 | 0 | while (worker->status == OK) { // wait in idling mode |
152 | 0 | pthread_cond_wait(&impl->condition, &impl->mutex); |
153 | 0 | } |
154 | 0 | if (worker->status == WORK) { |
155 | 0 | WebPGetWorkerInterface()->Execute(worker); |
156 | 0 | worker->status = OK; |
157 | 0 | } else if (worker->status == NOT_OK) { // finish the worker |
158 | 0 | done = 1; |
159 | 0 | } |
160 | | // signal to the main thread that we're done (for Sync()) |
161 | | // Note the associated mutex does not need to be held when signaling the |
162 | | // condition. Unlocking the mutex first may improve performance in some |
163 | | // implementations, avoiding the case where the waiting thread can't |
164 | | // reacquire the mutex when woken. |
165 | 0 | pthread_mutex_unlock(&impl->mutex); |
166 | 0 | pthread_cond_signal(&impl->condition); |
167 | 0 | } |
168 | 0 | return THREAD_RETURN(NULL); // Thread is finished |
169 | 0 | } |
170 | | |
171 | | // main thread state control |
172 | 12.1k | static void ChangeState(WebPWorker* const worker, WebPWorkerStatus new_status) { |
173 | | // No-op when attempting to change state on a thread that didn't come up. |
174 | | // Checking 'status' without acquiring the lock first would result in a data |
175 | | // race. |
176 | 12.1k | WebPWorkerImpl* const impl = (WebPWorkerImpl*)worker->impl; |
177 | 12.1k | if (impl == NULL) return; |
178 | | |
179 | 0 | pthread_mutex_lock(&impl->mutex); |
180 | 0 | if (worker->status >= OK) { |
181 | | // wait for the worker to finish |
182 | 0 | while (worker->status != OK) { |
183 | 0 | pthread_cond_wait(&impl->condition, &impl->mutex); |
184 | 0 | } |
185 | | // assign new status and release the working thread if needed |
186 | 0 | if (new_status != OK) { |
187 | 0 | worker->status = new_status; |
188 | | // Note the associated mutex does not need to be held when signaling the |
189 | | // condition. Unlocking the mutex first may improve performance in some |
190 | | // implementations, avoiding the case where the waiting thread can't |
191 | | // reacquire the mutex when woken. |
192 | 0 | pthread_mutex_unlock(&impl->mutex); |
193 | 0 | pthread_cond_signal(&impl->condition); |
194 | 0 | return; |
195 | 0 | } |
196 | 0 | } |
197 | 0 | pthread_mutex_unlock(&impl->mutex); |
198 | 0 | } |
199 | | |
200 | | #endif // WEBP_USE_THREAD |
201 | | |
202 | | //------------------------------------------------------------------------------ |
203 | | |
204 | 176k | static void Init(WebPWorker* const worker) { |
205 | 176k | WEBP_UNSAFE_MEMSET(worker, 0, sizeof(*worker)); |
206 | 176k | worker->status = NOT_OK; |
207 | 176k | } |
208 | | |
209 | 12.1k | static int Sync(WebPWorker* const worker) { |
210 | 12.1k | #ifdef WEBP_USE_THREAD |
211 | 12.1k | ChangeState(worker, OK); |
212 | 12.1k | #endif |
213 | 12.1k | assert(worker->status <= OK); |
214 | 12.1k | return !worker->had_error; |
215 | 12.1k | } |
216 | | |
217 | 0 | static int Reset(WebPWorker* const worker) { |
218 | 0 | int ok = 1; |
219 | 0 | worker->had_error = 0; |
220 | 0 | if (worker->status < OK) { |
221 | 0 | #ifdef WEBP_USE_THREAD |
222 | 0 | WebPWorkerImpl* const impl = |
223 | 0 | (WebPWorkerImpl*)WebPSafeCalloc(1, sizeof(WebPWorkerImpl)); |
224 | 0 | worker->impl = (void*)impl; |
225 | 0 | if (worker->impl == NULL) { |
226 | 0 | return 0; |
227 | 0 | } |
228 | 0 | if (pthread_mutex_init(&impl->mutex, NULL)) { |
229 | 0 | goto Error; |
230 | 0 | } |
231 | 0 | if (pthread_cond_init(&impl->condition, NULL)) { |
232 | 0 | pthread_mutex_destroy(&impl->mutex); |
233 | 0 | goto Error; |
234 | 0 | } |
235 | 0 | pthread_mutex_lock(&impl->mutex); |
236 | 0 | ok = !pthread_create(&impl->thread, NULL, ThreadLoop, worker); |
237 | 0 | if (ok) worker->status = OK; |
238 | 0 | pthread_mutex_unlock(&impl->mutex); |
239 | 0 | if (!ok) { |
240 | 0 | pthread_mutex_destroy(&impl->mutex); |
241 | 0 | pthread_cond_destroy(&impl->condition); |
242 | 0 | Error: |
243 | 0 | WebPSafeFree(impl); |
244 | 0 | worker->impl = NULL; |
245 | 0 | return 0; |
246 | 0 | } |
247 | | #else |
248 | | worker->status = OK; |
249 | | #endif |
250 | 0 | } else if (worker->status > OK) { |
251 | 0 | ok = Sync(worker); |
252 | 0 | } |
253 | 0 | assert(!ok || (worker->status == OK)); |
254 | 0 | return ok; |
255 | 0 | } |
256 | | |
257 | 12.1k | static void Execute(WebPWorker* const worker) { |
258 | 12.1k | if (worker->hook != NULL) { |
259 | 12.1k | worker->had_error |= !worker->hook(worker->data1, worker->data2); |
260 | 12.1k | } |
261 | 12.1k | } |
262 | | |
263 | 0 | static void Launch(WebPWorker* const worker) { |
264 | 0 | #ifdef WEBP_USE_THREAD |
265 | 0 | ChangeState(worker, WORK); |
266 | | #else |
267 | | Execute(worker); |
268 | | #endif |
269 | 0 | } |
270 | | |
271 | 204k | static void End(WebPWorker* const worker) { |
272 | 204k | #ifdef WEBP_USE_THREAD |
273 | 204k | if (worker->impl != NULL) { |
274 | 0 | WebPWorkerImpl* const impl = (WebPWorkerImpl*)worker->impl; |
275 | 0 | ChangeState(worker, NOT_OK); |
276 | 0 | pthread_join(impl->thread, NULL); |
277 | 0 | pthread_mutex_destroy(&impl->mutex); |
278 | 0 | pthread_cond_destroy(&impl->condition); |
279 | 0 | WebPSafeFree(impl); |
280 | 0 | worker->impl = NULL; |
281 | 0 | } |
282 | | #else |
283 | | worker->status = NOT_OK; |
284 | | assert(worker->impl == NULL); |
285 | | #endif |
286 | 204k | assert(worker->status == NOT_OK); |
287 | 204k | } |
288 | | |
289 | | //------------------------------------------------------------------------------ |
290 | | |
291 | | static WebPWorkerInterface g_worker_interface = {Init, Reset, Sync, |
292 | | Launch, Execute, End}; |
293 | | |
294 | 0 | int WebPSetWorkerInterface(const WebPWorkerInterface* const winterface) { |
295 | 0 | if (winterface == NULL || winterface->Init == NULL || |
296 | 0 | winterface->Reset == NULL || winterface->Sync == NULL || |
297 | 0 | winterface->Launch == NULL || winterface->Execute == NULL || |
298 | 0 | winterface->End == NULL) { |
299 | 0 | return 0; |
300 | 0 | } |
301 | 0 | g_worker_interface = *winterface; |
302 | 0 | return 1; |
303 | 0 | } |
304 | | |
305 | 374k | const WebPWorkerInterface* WebPGetWorkerInterface(void) { |
306 | 374k | return &g_worker_interface; |
307 | 374k | } |
308 | | |
309 | | //------------------------------------------------------------------------------ |