Backed out changeset 440770be9e5e (bug 1870687) for causing bc failures on browser_se...
[gecko.git] / third_party / aom / aom_util / aom_pthread.h
blob425a6b00f1ef51b3317ba51ce8219314d0af1b24
1 /*
2 * Copyright (c) 2024, Alliance for Open Media. All rights reserved
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.
12 // pthread.h wrapper
14 #ifndef AOM_AOM_UTIL_AOM_PTHREAD_H_
15 #define AOM_AOM_UTIL_AOM_PTHREAD_H_
17 #include "config/aom_config.h"
19 #if CONFIG_MULTITHREAD
21 #ifdef __cplusplus
22 extern "C" {
23 #endif
25 #if defined(_WIN32) && !HAVE_PTHREAD_H
26 // Prevent leaking max/min macros.
27 #undef NOMINMAX
28 #define NOMINMAX
29 #undef WIN32_LEAN_AND_MEAN
30 #define WIN32_LEAN_AND_MEAN
31 #include <errno.h> // NOLINT
32 #include <process.h> // NOLINT
33 #include <stddef.h> // NOLINT
34 #include <windows.h> // NOLINT
35 typedef HANDLE pthread_t;
36 typedef int pthread_attr_t;
37 typedef CRITICAL_SECTION pthread_mutex_t;
39 #if _WIN32_WINNT < 0x0600
40 #error _WIN32_WINNT must target Windows Vista / Server 2008 or newer.
41 #endif
42 typedef CONDITION_VARIABLE pthread_cond_t;
44 #ifndef WINAPI_FAMILY_PARTITION
45 #define WINAPI_PARTITION_DESKTOP 1
46 #define WINAPI_FAMILY_PARTITION(x) x
47 #endif
49 #if !WINAPI_FAMILY_PARTITION(WINAPI_PARTITION_DESKTOP)
50 #define USE_CREATE_THREAD
51 #endif
53 //------------------------------------------------------------------------------
54 // simplistic pthread emulation layer
56 // _beginthreadex requires __stdcall
57 #if defined(__GNUC__) && \
58 (__GNUC__ > 4 || (__GNUC__ == 4 && __GNUC_MINOR__ >= 2))
59 #define THREADFN __attribute__((force_align_arg_pointer)) unsigned int __stdcall
60 #else
61 #define THREADFN unsigned int __stdcall
62 #endif
63 #define THREAD_EXIT_SUCCESS 0
65 static INLINE int pthread_attr_init(pthread_attr_t *attr) {
66 (void)attr;
67 return 0;
70 static INLINE int pthread_attr_destroy(pthread_attr_t *attr) {
71 (void)attr;
72 return 0;
75 static INLINE int pthread_attr_getstacksize(const pthread_attr_t *attr,
76 size_t *stacksize) {
77 (void)attr;
78 (void)stacksize;
79 return EINVAL;
82 static INLINE int pthread_attr_setstacksize(pthread_attr_t *attr,
83 size_t stacksize) {
84 (void)attr;
85 (void)stacksize;
86 return EINVAL;
89 static INLINE int pthread_create(pthread_t *const thread,
90 const pthread_attr_t *attr,
91 unsigned int(__stdcall *start)(void *),
92 void *arg) {
93 (void)attr;
94 #ifdef USE_CREATE_THREAD
95 *thread = CreateThread(NULL, /* lpThreadAttributes */
96 0, /* dwStackSize */
97 start, arg, 0, /* dwStackSize */
98 NULL); /* lpThreadId */
99 #else
100 *thread = (pthread_t)_beginthreadex(NULL, /* void *security */
101 0, /* unsigned stack_size */
102 start, arg, 0, /* unsigned initflag */
103 NULL); /* unsigned *thrdaddr */
104 #endif
105 if (*thread == NULL) return 1;
106 SetThreadPriority(*thread, THREAD_PRIORITY_ABOVE_NORMAL);
107 return 0;
110 static INLINE int pthread_join(pthread_t thread, void **value_ptr) {
111 (void)value_ptr;
112 return (WaitForSingleObjectEx(thread, INFINITE, FALSE /*bAlertable*/) !=
113 WAIT_OBJECT_0 ||
114 CloseHandle(thread) == 0);
117 // Mutex
118 static INLINE int pthread_mutex_init(pthread_mutex_t *const mutex,
119 void *mutexattr) {
120 (void)mutexattr;
121 InitializeCriticalSectionEx(mutex, 0 /*dwSpinCount*/, 0 /*Flags*/);
122 return 0;
125 static INLINE int pthread_mutex_trylock(pthread_mutex_t *const mutex) {
126 return TryEnterCriticalSection(mutex) ? 0 : EBUSY;
129 static INLINE int pthread_mutex_lock(pthread_mutex_t *const mutex) {
130 EnterCriticalSection(mutex);
131 return 0;
134 static INLINE int pthread_mutex_unlock(pthread_mutex_t *const mutex) {
135 LeaveCriticalSection(mutex);
136 return 0;
139 static INLINE int pthread_mutex_destroy(pthread_mutex_t *const mutex) {
140 DeleteCriticalSection(mutex);
141 return 0;
144 // Condition
145 static INLINE int pthread_cond_destroy(pthread_cond_t *const condition) {
146 (void)condition;
147 return 0;
150 static INLINE int pthread_cond_init(pthread_cond_t *const condition,
151 void *cond_attr) {
152 (void)cond_attr;
153 InitializeConditionVariable(condition);
154 return 0;
157 static INLINE int pthread_cond_signal(pthread_cond_t *const condition) {
158 WakeConditionVariable(condition);
159 return 0;
162 static INLINE int pthread_cond_broadcast(pthread_cond_t *const condition) {
163 WakeAllConditionVariable(condition);
164 return 0;
167 static INLINE int pthread_cond_wait(pthread_cond_t *const condition,
168 pthread_mutex_t *const mutex) {
169 int ok;
170 ok = SleepConditionVariableCS(condition, mutex, INFINITE);
171 return !ok;
173 #else // _WIN32
174 #include <pthread.h> // NOLINT
175 #define THREADFN void *
176 #define THREAD_EXIT_SUCCESS NULL
177 #endif
179 #ifdef __cplusplus
180 } // extern "C"
181 #endif
183 #endif // CONFIG_MULTITHREAD
185 #endif // AOM_AOM_UTIL_AOM_PTHREAD_H_