xref: /aosp_15_r20/external/libvpx/vpx_util/vpx_pthread.h (revision fb1b10ab9aebc7c7068eedab379b749d7e3900be)
1 // Copyright 2024 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 // pthread.h wrapper
11 
12 #ifndef VPX_VPX_UTIL_VPX_PTHREAD_H_
13 #define VPX_VPX_UTIL_VPX_PTHREAD_H_
14 
15 #include "./vpx_config.h"
16 
17 #if CONFIG_MULTITHREAD
18 
19 #ifdef __cplusplus
20 extern "C" {
21 #endif
22 
23 #if defined(_WIN32) && !HAVE_PTHREAD_H
24 // Prevent leaking max/min macros.
25 #undef NOMINMAX
26 #define NOMINMAX
27 #undef WIN32_LEAN_AND_MEAN
28 #define WIN32_LEAN_AND_MEAN
29 #include <errno.h>    // NOLINT
30 #include <process.h>  // NOLINT
31 #include <stddef.h>   // NOLINT
32 #include <windows.h>  // NOLINT
33 typedef HANDLE pthread_t;
34 typedef CRITICAL_SECTION pthread_mutex_t;
35 
36 #if _WIN32_WINNT < 0x0600
37 #error _WIN32_WINNT must target Windows Vista / Server 2008 or newer.
38 #endif
39 typedef CONDITION_VARIABLE pthread_cond_t;
40 
41 #ifndef WINAPI_FAMILY_PARTITION
42 #define WINAPI_PARTITION_DESKTOP 1
43 #define WINAPI_FAMILY_PARTITION(x) x
44 #endif
45 
46 #if !WINAPI_FAMILY_PARTITION(WINAPI_PARTITION_DESKTOP)
47 #define USE_CREATE_THREAD
48 #endif
49 
50 //------------------------------------------------------------------------------
51 // simplistic pthread emulation layer
52 
53 // _beginthreadex requires __stdcall
54 #if defined(__GNUC__) && \
55     (__GNUC__ > 4 || (__GNUC__ == 4 && __GNUC_MINOR__ >= 2))
56 #define THREADFN __attribute__((force_align_arg_pointer)) unsigned int __stdcall
57 #else
58 #define THREADFN unsigned int __stdcall
59 #endif
60 #define THREAD_EXIT_SUCCESS 0
61 
pthread_create(pthread_t * const thread,const void * attr,unsigned int (__stdcall * start)(void *),void * arg)62 static INLINE int pthread_create(pthread_t *const thread, const void *attr,
63                                  unsigned int(__stdcall *start)(void *),
64                                  void *arg) {
65   (void)attr;
66 #ifdef USE_CREATE_THREAD
67   *thread = CreateThread(NULL,          /* lpThreadAttributes */
68                          0,             /* dwStackSize */
69                          start, arg, 0, /* dwStackSize */
70                          NULL);         /* lpThreadId */
71 #else
72   *thread = (pthread_t)_beginthreadex(NULL,          /* void *security */
73                                       0,             /* unsigned stack_size */
74                                       start, arg, 0, /* unsigned initflag */
75                                       NULL);         /* unsigned *thrdaddr */
76 #endif
77   if (*thread == NULL) return 1;
78   SetThreadPriority(*thread, THREAD_PRIORITY_ABOVE_NORMAL);
79   return 0;
80 }
81 
pthread_join(pthread_t thread,void ** value_ptr)82 static INLINE int pthread_join(pthread_t thread, void **value_ptr) {
83   (void)value_ptr;
84   return (WaitForSingleObjectEx(thread, INFINITE, FALSE /*bAlertable*/) !=
85               WAIT_OBJECT_0 ||
86           CloseHandle(thread) == 0);
87 }
88 
89 // Mutex
pthread_mutex_init(pthread_mutex_t * const mutex,void * mutexattr)90 static INLINE int pthread_mutex_init(pthread_mutex_t *const mutex,
91                                      void *mutexattr) {
92   (void)mutexattr;
93   InitializeCriticalSectionEx(mutex, 0 /*dwSpinCount*/, 0 /*Flags*/);
94   return 0;
95 }
96 
pthread_mutex_trylock(pthread_mutex_t * const mutex)97 static INLINE int pthread_mutex_trylock(pthread_mutex_t *const mutex) {
98   return TryEnterCriticalSection(mutex) ? 0 : EBUSY;
99 }
100 
pthread_mutex_lock(pthread_mutex_t * const mutex)101 static INLINE int pthread_mutex_lock(pthread_mutex_t *const mutex) {
102   EnterCriticalSection(mutex);
103   return 0;
104 }
105 
pthread_mutex_unlock(pthread_mutex_t * const mutex)106 static INLINE int pthread_mutex_unlock(pthread_mutex_t *const mutex) {
107   LeaveCriticalSection(mutex);
108   return 0;
109 }
110 
pthread_mutex_destroy(pthread_mutex_t * const mutex)111 static INLINE int pthread_mutex_destroy(pthread_mutex_t *const mutex) {
112   DeleteCriticalSection(mutex);
113   return 0;
114 }
115 
116 // Condition
pthread_cond_destroy(pthread_cond_t * const condition)117 static INLINE int pthread_cond_destroy(pthread_cond_t *const condition) {
118   (void)condition;
119   return 0;
120 }
121 
pthread_cond_init(pthread_cond_t * const condition,void * cond_attr)122 static INLINE int pthread_cond_init(pthread_cond_t *const condition,
123                                     void *cond_attr) {
124   (void)cond_attr;
125   InitializeConditionVariable(condition);
126   return 0;
127 }
128 
pthread_cond_signal(pthread_cond_t * const condition)129 static INLINE int pthread_cond_signal(pthread_cond_t *const condition) {
130   WakeConditionVariable(condition);
131   return 0;
132 }
133 
pthread_cond_broadcast(pthread_cond_t * const condition)134 static INLINE int pthread_cond_broadcast(pthread_cond_t *const condition) {
135   WakeAllConditionVariable(condition);
136   return 0;
137 }
138 
pthread_cond_wait(pthread_cond_t * const condition,pthread_mutex_t * const mutex)139 static INLINE int pthread_cond_wait(pthread_cond_t *const condition,
140                                     pthread_mutex_t *const mutex) {
141   int ok;
142   ok = SleepConditionVariableCS(condition, mutex, INFINITE);
143   return !ok;
144 }
145 #else                 // _WIN32
146 #include <pthread.h>  // NOLINT
147 #define THREADFN void *
148 #define THREAD_EXIT_SUCCESS NULL
149 #endif
150 
151 #ifdef __cplusplus
152 }  // extern "C"
153 #endif
154 
155 #endif  // CONFIG_MULTITHREAD
156 
157 #endif  // VPX_VPX_UTIL_VPX_PTHREAD_H_
158