• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (c) 2024, 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 // pthread.h wrapper
13 
14 #ifndef AOM_AOM_UTIL_AOM_PTHREAD_H_
15 #define AOM_AOM_UTIL_AOM_PTHREAD_H_
16 
17 #include "config/aom_config.h"
18 
19 #if CONFIG_MULTITHREAD
20 
21 #ifdef __cplusplus
22 extern "C" {
23 #endif
24 
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;
38 
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;
43 
44 #ifndef WINAPI_FAMILY_PARTITION
45 #define WINAPI_PARTITION_DESKTOP 1
46 #define WINAPI_FAMILY_PARTITION(x) x
47 #endif
48 
49 #if !WINAPI_FAMILY_PARTITION(WINAPI_PARTITION_DESKTOP)
50 #define USE_CREATE_THREAD
51 #endif
52 
53 //------------------------------------------------------------------------------
54 // simplistic pthread emulation layer
55 
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
64 
pthread_attr_init(pthread_attr_t * attr)65 static INLINE int pthread_attr_init(pthread_attr_t *attr) {
66   (void)attr;
67   return 0;
68 }
69 
pthread_attr_destroy(pthread_attr_t * attr)70 static INLINE int pthread_attr_destroy(pthread_attr_t *attr) {
71   (void)attr;
72   return 0;
73 }
74 
pthread_create(pthread_t * const thread,const pthread_attr_t * attr,unsigned int (__stdcall * start)(void *),void * arg)75 static INLINE int pthread_create(pthread_t *const thread,
76                                  const pthread_attr_t *attr,
77                                  unsigned int(__stdcall *start)(void *),
78                                  void *arg) {
79   (void)attr;
80 #ifdef USE_CREATE_THREAD
81   *thread = CreateThread(NULL,          /* lpThreadAttributes */
82                          0,             /* dwStackSize */
83                          start, arg, 0, /* dwStackSize */
84                          NULL);         /* lpThreadId */
85 #else
86   *thread = (pthread_t)_beginthreadex(NULL,          /* void *security */
87                                       0,             /* unsigned stack_size */
88                                       start, arg, 0, /* unsigned initflag */
89                                       NULL);         /* unsigned *thrdaddr */
90 #endif
91   if (*thread == NULL) return 1;
92   SetThreadPriority(*thread, THREAD_PRIORITY_ABOVE_NORMAL);
93   return 0;
94 }
95 
pthread_join(pthread_t thread,void ** value_ptr)96 static INLINE int pthread_join(pthread_t thread, void **value_ptr) {
97   (void)value_ptr;
98   return (WaitForSingleObjectEx(thread, INFINITE, FALSE /*bAlertable*/) !=
99               WAIT_OBJECT_0 ||
100           CloseHandle(thread) == 0);
101 }
102 
103 // Mutex
pthread_mutex_init(pthread_mutex_t * const mutex,void * mutexattr)104 static INLINE int pthread_mutex_init(pthread_mutex_t *const mutex,
105                                      void *mutexattr) {
106   (void)mutexattr;
107   InitializeCriticalSectionEx(mutex, 0 /*dwSpinCount*/, 0 /*Flags*/);
108   return 0;
109 }
110 
pthread_mutex_trylock(pthread_mutex_t * const mutex)111 static INLINE int pthread_mutex_trylock(pthread_mutex_t *const mutex) {
112   return TryEnterCriticalSection(mutex) ? 0 : EBUSY;
113 }
114 
pthread_mutex_lock(pthread_mutex_t * const mutex)115 static INLINE int pthread_mutex_lock(pthread_mutex_t *const mutex) {
116   EnterCriticalSection(mutex);
117   return 0;
118 }
119 
pthread_mutex_unlock(pthread_mutex_t * const mutex)120 static INLINE int pthread_mutex_unlock(pthread_mutex_t *const mutex) {
121   LeaveCriticalSection(mutex);
122   return 0;
123 }
124 
pthread_mutex_destroy(pthread_mutex_t * const mutex)125 static INLINE int pthread_mutex_destroy(pthread_mutex_t *const mutex) {
126   DeleteCriticalSection(mutex);
127   return 0;
128 }
129 
130 // Condition
pthread_cond_destroy(pthread_cond_t * const condition)131 static INLINE int pthread_cond_destroy(pthread_cond_t *const condition) {
132   (void)condition;
133   return 0;
134 }
135 
pthread_cond_init(pthread_cond_t * const condition,void * cond_attr)136 static INLINE int pthread_cond_init(pthread_cond_t *const condition,
137                                     void *cond_attr) {
138   (void)cond_attr;
139   InitializeConditionVariable(condition);
140   return 0;
141 }
142 
pthread_cond_signal(pthread_cond_t * const condition)143 static INLINE int pthread_cond_signal(pthread_cond_t *const condition) {
144   WakeConditionVariable(condition);
145   return 0;
146 }
147 
pthread_cond_broadcast(pthread_cond_t * const condition)148 static INLINE int pthread_cond_broadcast(pthread_cond_t *const condition) {
149   WakeAllConditionVariable(condition);
150   return 0;
151 }
152 
pthread_cond_wait(pthread_cond_t * const condition,pthread_mutex_t * const mutex)153 static INLINE int pthread_cond_wait(pthread_cond_t *const condition,
154                                     pthread_mutex_t *const mutex) {
155   int ok;
156   ok = SleepConditionVariableCS(condition, mutex, INFINITE);
157   return !ok;
158 }
159 #else                 // _WIN32
160 #include <pthread.h>  // NOLINT
161 #define THREADFN void *
162 #define THREAD_EXIT_SUCCESS NULL
163 #endif
164 
165 #ifdef __cplusplus
166 }  // extern "C"
167 #endif
168 
169 #endif  // CONFIG_MULTITHREAD
170 
171 #endif  // AOM_AOM_UTIL_AOM_PTHREAD_H_
172