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.
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
25 #if defined(_WIN32) && !HAVE_PTHREAD_H
26 // Prevent leaking max/min macros.
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.
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
49 #if !WINAPI_FAMILY_PARTITION(WINAPI_PARTITION_DESKTOP)
50 #define USE_CREATE_THREAD
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
61 #define THREADFN unsigned int __stdcall
63 #define THREAD_EXIT_SUCCESS 0
65 static INLINE
int pthread_attr_init(pthread_attr_t
*attr
) {
70 static INLINE
int pthread_attr_destroy(pthread_attr_t
*attr
) {
75 static INLINE
int pthread_attr_getstacksize(const pthread_attr_t
*attr
,
82 static INLINE
int pthread_attr_setstacksize(pthread_attr_t
*attr
,
89 static INLINE
int pthread_create(pthread_t
*const thread
,
90 const pthread_attr_t
*attr
,
91 unsigned int(__stdcall
*start
)(void *),
94 #ifdef USE_CREATE_THREAD
95 *thread
= CreateThread(NULL
, /* lpThreadAttributes */
97 start
, arg
, 0, /* dwStackSize */
98 NULL
); /* lpThreadId */
100 *thread
= (pthread_t
)_beginthreadex(NULL
, /* void *security */
101 0, /* unsigned stack_size */
102 start
, arg
, 0, /* unsigned initflag */
103 NULL
); /* unsigned *thrdaddr */
105 if (*thread
== NULL
) return 1;
106 SetThreadPriority(*thread
, THREAD_PRIORITY_ABOVE_NORMAL
);
110 static INLINE
int pthread_join(pthread_t thread
, void **value_ptr
) {
112 return (WaitForSingleObjectEx(thread
, INFINITE
, FALSE
/*bAlertable*/) !=
114 CloseHandle(thread
) == 0);
118 static INLINE
int pthread_mutex_init(pthread_mutex_t
*const mutex
,
121 InitializeCriticalSectionEx(mutex
, 0 /*dwSpinCount*/, 0 /*Flags*/);
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
);
134 static INLINE
int pthread_mutex_unlock(pthread_mutex_t
*const mutex
) {
135 LeaveCriticalSection(mutex
);
139 static INLINE
int pthread_mutex_destroy(pthread_mutex_t
*const mutex
) {
140 DeleteCriticalSection(mutex
);
145 static INLINE
int pthread_cond_destroy(pthread_cond_t
*const condition
) {
150 static INLINE
int pthread_cond_init(pthread_cond_t
*const condition
,
153 InitializeConditionVariable(condition
);
157 static INLINE
int pthread_cond_signal(pthread_cond_t
*const condition
) {
158 WakeConditionVariable(condition
);
162 static INLINE
int pthread_cond_broadcast(pthread_cond_t
*const condition
) {
163 WakeAllConditionVariable(condition
);
167 static INLINE
int pthread_cond_wait(pthread_cond_t
*const condition
,
168 pthread_mutex_t
*const mutex
) {
170 ok
= SleepConditionVariableCS(condition
, mutex
, INFINITE
);
174 #include <pthread.h> // NOLINT
175 #define THREADFN void *
176 #define THREAD_EXIT_SUCCESS NULL
183 #endif // CONFIG_MULTITHREAD
185 #endif // AOM_AOM_UTIL_AOM_PTHREAD_H_