aom: Split pthread wrapper to aom_pthread.h.

From bad2f8dda3ba894f08913d2e85a5272a31f75277 Mon Sep 17 00:00:00 2001
From: Peter Kasting <[EMAIL REDACTED]>
Date: Mon, 12 Feb 2024 14:24:41 -0800
Subject: [PATCH] Split pthread wrapper to aom_pthread.h.

Also does a bit of cleanup to the THREAD macros as suggested in review.

Bug: none
Change-Id: I26cd85f09f138e59c49f03469262edcda5c007d8
---
 aom_dsp/flow_estimation/corner_detect.c |   1 +
 aom_dsp/flow_estimation/corner_detect.h |   2 +-
 aom_dsp/pyramid.c                       |   2 +-
 aom_dsp/pyramid.h                       |   2 +-
 aom_util/aom_pthread.h                  | 159 ++++++++++++++++++++++++
 aom_util/aom_thread.c                   |  19 ++-
 aom_util/aom_thread.h                   | 145 ---------------------
 aom_util/aom_util.cmake                 |   3 +-
 av1/av1_cx_iface.c                      |   1 +
 av1/av1_dx_iface.c                      |   1 +
 av1/common/alloccommon.c                |   1 +
 av1/common/av1_common_int.h             |   2 +-
 av1/common/cdef.c                       |   1 +
 av1/common/restoration.c                |   1 +
 av1/common/thread_common.c              |   1 +
 av1/common/thread_common.h              |   1 +
 av1/decoder/decodeframe.c               |   1 +
 av1/decoder/decoder.c                   |   1 +
 av1/decoder/dthread.h                   |   1 -
 av1/encoder/allintra_vis.c              |   2 +
 av1/encoder/bitstream.h                 |   1 +
 av1/encoder/encodeframe.c               |   2 +-
 av1/encoder/encoder.c                   |   1 +
 av1/encoder/encoder.h                   |   1 +
 av1/encoder/ethread.c                   |   2 +
 av1/encoder/firstpass.c                 |   1 +
 av1/encoder/global_motion.h             |   1 +
 av1/encoder/temporal_filter.h           |   2 +
 av1/encoder/tpl_model.c                 |   1 +
 av1/encoder/tpl_model.h                 |   1 +
 30 files changed, 207 insertions(+), 153 deletions(-)
 create mode 100644 aom_util/aom_pthread.h

diff --git a/aom_dsp/flow_estimation/corner_detect.c b/aom_dsp/flow_estimation/corner_detect.c
index 284d1bd7b8..c3c0939e32 100644
--- a/aom_dsp/flow_estimation/corner_detect.c
+++ b/aom_dsp/flow_estimation/corner_detect.c
@@ -20,6 +20,7 @@
 #include "aom_dsp/aom_dsp_common.h"
 #include "aom_dsp/flow_estimation/corner_detect.h"
 #include "aom_mem/aom_mem.h"
+#include "aom_util/aom_pthread.h"
 #include "av1/common/common.h"
 
 #define FAST_BARRIER 18
diff --git a/aom_dsp/flow_estimation/corner_detect.h b/aom_dsp/flow_estimation/corner_detect.h
index d05846ce5d..4c6add2bf9 100644
--- a/aom_dsp/flow_estimation/corner_detect.h
+++ b/aom_dsp/flow_estimation/corner_detect.h
@@ -18,7 +18,7 @@
 #include <memory.h>
 
 #include "aom_dsp/pyramid.h"
-#include "aom_util/aom_thread.h"
+#include "aom_util/aom_pthread.h"
 
 #ifdef __cplusplus
 extern "C" {
diff --git a/aom_dsp/pyramid.c b/aom_dsp/pyramid.c
index 324a18baea..94c970b78a 100644
--- a/aom_dsp/pyramid.c
+++ b/aom_dsp/pyramid.c
@@ -12,7 +12,7 @@
 #include "aom_dsp/pyramid.h"
 #include "aom_mem/aom_mem.h"
 #include "aom_ports/bitops.h"
-#include "aom_util/aom_thread.h"
+#include "aom_util/aom_pthread.h"
 
 // TODO(rachelbarker): Move needed code from av1/ to aom_dsp/
 #include "av1/common/resize.h"
diff --git a/aom_dsp/pyramid.h b/aom_dsp/pyramid.h
index 9442a1ff08..a3d63d338b 100644
--- a/aom_dsp/pyramid.h
+++ b/aom_dsp/pyramid.h
@@ -19,7 +19,7 @@
 #include "config/aom_config.h"
 
 #include "aom_scale/yv12config.h"
-#include "aom_util/aom_thread.h"
+#include "aom_util/aom_pthread.h"
 
 #ifdef __cplusplus
 extern "C" {
diff --git a/aom_util/aom_pthread.h b/aom_util/aom_pthread.h
new file mode 100644
index 0000000000..fbf0d1c5e0
--- /dev/null
+++ b/aom_util/aom_pthread.h
@@ -0,0 +1,159 @@
+/*
+ * Copyright (c) 2024, Alliance for Open Media. All rights reserved
+ *
+ * This source code is subject to the terms of the BSD 2 Clause License and
+ * the Alliance for Open Media Patent License 1.0. If the BSD 2 Clause License
+ * was not distributed with this source code in the LICENSE file, you can
+ * obtain it at www.aomedia.org/license/software. If the Alliance for Open
+ * Media Patent License 1.0 was not distributed with this source code in the
+ * PATENTS file, you can obtain it at www.aomedia.org/license/patent.
+ */
+//
+// pthread.h wrapper
+
+#ifndef AOM_AOM_UTIL_AOM_PTHREAD_H_
+#define AOM_AOM_UTIL_AOM_PTHREAD_H_
+
+#include "config/aom_config.h"
+
+#if CONFIG_MULTITHREAD
+
+#ifdef __cplusplus
+extern "C" {
+#endif
+
+#if defined(_WIN32) && !HAVE_PTHREAD_H
+// Prevent leaking max/min macros.
+#undef NOMINMAX
+#define NOMINMAX
+#undef WIN32_LEAN_AND_MEAN
+#define WIN32_LEAN_AND_MEAN
+#include <process.h>  // NOLINT
+#include <stddef.h>   // NOLINT
+#include <windows.h>  // NOLINT
+typedef HANDLE pthread_t;
+typedef int pthread_attr_t;
+typedef CRITICAL_SECTION pthread_mutex_t;
+
+#if _WIN32_WINNT < 0x0600
+#error _WIN32_WINNT must target Windows Vista / Server 2008 or newer.
+#endif
+typedef CONDITION_VARIABLE pthread_cond_t;
+
+#ifndef WINAPI_FAMILY_PARTITION
+#define WINAPI_PARTITION_DESKTOP 1
+#define WINAPI_FAMILY_PARTITION(x) x
+#endif
+
+#if !WINAPI_FAMILY_PARTITION(WINAPI_PARTITION_DESKTOP)
+#define USE_CREATE_THREAD
+#endif
+
+//------------------------------------------------------------------------------
+// simplistic pthread emulation layer
+
+static INLINE int pthread_attr_init(pthread_attr_t *attr) {
+  (void)attr;
+  return 0;
+}
+
+static INLINE int pthread_attr_destroy(pthread_attr_t *attr) {
+  (void)attr;
+  return 0;
+}
+
+static INLINE int pthread_create(pthread_t *const thread,
+                                 const pthread_attr_t *attr,
+                                 unsigned int(__stdcall *start)(void *),
+                                 void *arg) {
+  (void)attr;
+#ifdef USE_CREATE_THREAD
+  *thread = CreateThread(NULL,          /* lpThreadAttributes */
+                         0,             /* dwStackSize */
+                         start, arg, 0, /* dwStackSize */
+                         NULL);         /* lpThreadId */
+#else
+  *thread = (pthread_t)_beginthreadex(NULL,          /* void *security */
+                                      0,             /* unsigned stack_size */
+                                      start, arg, 0, /* unsigned initflag */
+                                      NULL);         /* unsigned *thrdaddr */
+#endif
+  if (*thread == NULL) return 1;
+  SetThreadPriority(*thread, THREAD_PRIORITY_ABOVE_NORMAL);
+  return 0;
+}
+
+static INLINE int pthread_join(pthread_t thread, void **value_ptr) {
+  (void)value_ptr;
+  return (WaitForSingleObjectEx(thread, INFINITE, FALSE /*bAlertable*/) !=
+              WAIT_OBJECT_0 ||
+          CloseHandle(thread) == 0);
+}
+
+// Mutex
+static INLINE int pthread_mutex_init(pthread_mutex_t *const mutex,
+                                     void *mutexattr) {
+  (void)mutexattr;
+  InitializeCriticalSectionEx(mutex, 0 /*dwSpinCount*/, 0 /*Flags*/);
+  return 0;
+}
+
+static INLINE int pthread_mutex_trylock(pthread_mutex_t *const mutex) {
+  return TryEnterCriticalSection(mutex) ? 0 : EBUSY;
+}
+
+static INLINE int pthread_mutex_lock(pthread_mutex_t *const mutex) {
+  EnterCriticalSection(mutex);
+  return 0;
+}
+
+static INLINE int pthread_mutex_unlock(pthread_mutex_t *const mutex) {
+  LeaveCriticalSection(mutex);
+  return 0;
+}
+
+static INLINE int pthread_mutex_destroy(pthread_mutex_t *const mutex) {
+  DeleteCriticalSection(mutex);
+  return 0;
+}
+
+// Condition
+static INLINE int pthread_cond_destroy(pthread_cond_t *const condition) {
+  (void)condition;
+  return 0;
+}
+
+static INLINE int pthread_cond_init(pthread_cond_t *const condition,
+                                    void *cond_attr) {
+  (void)cond_attr;
+  InitializeConditionVariable(condition);
+  return 0;
+}
+
+static INLINE int pthread_cond_signal(pthread_cond_t *const condition) {
+  WakeConditionVariable(condition);
+  return 0;
+}
+
+static INLINE int pthread_cond_broadcast(pthread_cond_t *const condition) {
+  WakeAllConditionVariable(condition);
+  return 0;
+}
+
+static INLINE int pthread_cond_wait(pthread_cond_t *const condition,
+                                    pthread_mutex_t *const mutex) {
+  int ok;
+  ok = SleepConditionVariableCS(condition, mutex, INFINITE);
+  return !ok;
+}
+#else                 // _WIN32
+#include <pthread.h>  // NOLINT
+#endif
+
+#ifdef __cplusplus
+}  // extern "C"
+#endif
+
+#endif  // CONFIG_MULTITHREAD
+
+#endif  // AOM_AOM_UTIL_AOM_PTHREAD_H_
diff --git a/aom_util/aom_thread.c b/aom_util/aom_thread.c
index fa3b0a25e4..501827dddb 100644
--- a/aom_util/aom_thread.c
+++ b/aom_util/aom_thread.c
@@ -23,12 +23,29 @@
 #include <assert.h>
 #include <string.h>  // for memset()
 
+#include "config/aom_config.h"
+
 #include "aom_mem/aom_mem.h"
 #include "aom_ports/sanitizer.h"
+#include "aom_util/aom_pthread.h"
 #include "aom_util/aom_thread.h"
 
 #if CONFIG_MULTITHREAD
 
+#if defined(_WIN32) && !HAVE_PTHREAD_H
+// _beginthreadex requires __stdcall
+#if defined(__GNUC__) && \
+    (__GNUC__ > 4 || (__GNUC__ == 4 && __GNUC_MINOR__ >= 2))
+#define THREADFN __attribute__((force_align_arg_pointer)) unsigned int __stdcall
+#else
+#define THREADFN unsigned int __stdcall
+#endif
+#define THREAD_EXIT_SUCCESS 0
+#else  // _WIN32
+#define THREADFN void *
+#define THREAD_EXIT_SUCCESS NULL
+#endif
+
 struct AVxWorkerImpl {
   pthread_mutex_t mutex_;
   pthread_cond_t condition_;
@@ -87,7 +104,7 @@ static THREADFN thread_loop(void *ptr) {
     }
   }
   pthread_mutex_unlock(&worker->impl_->mutex_);
-  return THREAD_RETURN(NULL);  // Thread is finished
+  return THREAD_EXIT_SUCCESS;  // Thread is finished
 }
 
 // main thread state control
diff --git a/aom_util/aom_thread.h b/aom_util/aom_thread.h
index 7ebd2ccc72..db454e78c1 100644
--- a/aom_util/aom_thread.h
+++ b/aom_util/aom_thread.h
@@ -17,157 +17,12 @@
 #ifndef AOM_AOM_UTIL_AOM_THREAD_H_
 #define AOM_AOM_UTIL_AOM_THREAD_H_
 
-#include "config/aom_config.h"
-
 #ifdef __cplusplus
 extern "C" {
 #endif
 
 #define MAX_NUM_THREADS 64
 
-#if CONFIG_MULTITHREAD
-
-#if defined(_WIN32) && !HAVE_PTHREAD_H
-// Prevent leaking max/min macros.
-#undef NOMINMAX
-#define NOMINMAX
-#undef WIN32_LEAN_AND_MEAN
-#define WIN32_LEAN_AND_MEAN
-#include <errno.h>    // NOLINT
-#include <process.h>  // NOLINT
-#include <windows.h>  // NOLINT
-typedef HANDLE pthread_t;
-typedef int pthread_attr_t;
-typedef CRITICAL_SECTION pthread_mutex_t;
-
-#if _WIN32_WINNT < 0x0600
-#error _WIN32_WINNT must target Windows Vista / Server 2008 or newer.
-#endif
-typedef CONDITION_VARIABLE pthread_cond_t;
-
-#ifndef WINAPI_FAMILY_PARTITION
-#define WINAPI_PARTITION_DESKTOP 1
-#define WINAPI_FAMILY_PARTITION(x) x
-#endif
-
-#if !WINAPI_FAMILY_PARTITION(WINAPI_PARTITION_DESKTOP)
-#define USE_CREATE_THREAD
-#endif
-
-//------------------------------------------------------------------------------
-// simplistic pthread emulation layer
-
-// _beginthreadex requires __stdcall
-#if defined(__GNUC__) && \
-    (__GNUC__ > 4 || (__GNUC__ == 4 && __GNUC_MINOR__ >= 2))
-#define THREADFN __attribute__((force_align_arg_pointer)) unsigned int __stdcall
-#else
-#define THREADFN unsigned int __stdcall
-#endif
-#define THREAD_RETURN(val) (unsigned int)((DWORD_PTR)val)
-
-static INLINE int pthread_attr_init(pthread_attr_t *attr) {
-  (void)attr;
-  return 0;
-}
-
-static INLINE int pthread_attr_destroy(pthread_attr_t *attr) {
-  (void)attr;
-  return 0;
-}
-
-static INLINE int pthread_create(pthread_t *const thread,
-                                 const pthread_attr_t *attr,
-                                 unsigned int(__stdcall *start)(void *),
-                                 void *arg) {
-  (void)attr;
-#ifdef USE_CREATE_THREAD
-  *thread = CreateThread(NULL,          /* lpThreadAttributes */
-                         0,             /* dwStackSize */
-                         start, arg, 0, /* dwStackSize */
-                         NULL);         /* lpThreadId */
-#else
-  *thread = (pthread_t)_beginthreadex(NULL,          /* void *security */
-                                      0,             /* unsigned stack_size */
-                                      start, arg, 0, /* unsigned initflag */
-                                      NULL);         /* unsigned *thrdaddr */
-#endif
-  if (*thread == NULL) return 1;
-  SetThreadPriority(*thread, THREAD_PRIORITY_ABOVE_NORMAL);
-  return 0;
-}
-
-static INLINE int pthread_join(pthread_t thread, void **value_ptr) {
-  (void)value_ptr;
-  return (WaitForSingleObjectEx(thread, INFINITE, FALSE /*bAlertable*/) !=
-              WAIT_OBJECT_0 ||
-          CloseHandle(thread) == 0);
-}
-
-// Mutex
-static INLINE int pthread_mutex_init(pthread_mutex_t *const mutex,
-                                     void *mutexattr) {
-  (void)mutexattr;
-  InitializeCriticalSectionEx(mutex, 0 /*dwSpinCount*/, 0 /*Flags*/);
-  return 0;
-}
-
-static INLINE int pthread_mutex_trylock(pthread_mutex_t *const mutex) {
-  return TryEnterCriticalSection(mutex) ? 0 : EBUSY;
-}
-
-static INLINE int pthread_mutex_lock(pthread_mutex_t *const mutex) {
-  EnterCriticalSection(mutex);
-  return 0;
-}
-
-static INLINE int pthread_mutex_unlock(pthread_mutex_t *const mutex) {
-  LeaveCriticalSection(mutex);
-  return 0;
-}
-
-static INLINE int pthread_mutex_destroy(pthread_mutex_t *const mutex) {
-  DeleteCriticalSection(mutex);
-  return 0;
-}
-
-// Condition
-static INLINE int pthread_cond_destroy(pthread_cond_t *const condition) {
-  (void)condition;
-  return 0;
-}
-
-static INLINE int pthread_cond_init(pthread_cond_t *const condition,
-                                    void *cond_attr) {
-  (void)cond_attr;
-  InitializeConditionVariable(condition);
-  return 0;
-}
-
-static INLINE int pthread_cond_signal(pthread_cond_t *const condition) {
-  WakeConditionVariable(condition);
-  return 0;
-}
-
-static INLINE int pthread_cond_broadcast(pthread_cond_t *const condition) {
-  WakeAllConditionVariable(condition);
-  return 0;
-}
-
-static INLINE int pthread_cond_wait(pthread_cond_t *const condition,
-                                    pthread_mutex_t *const mutex) {
-  int ok;
-  ok = SleepConditionVariableCS(condition, mutex, INFINITE);
-  return !ok;
-}
-#else                 // _WIN32
-#include <pthread.h>  // NOLINT
-#define THREADFN void *
-#define THREAD_RETURN(val) val
-#endif
-
-#endif  // CONFIG_MULTITHREAD
-
 // State of the worker thread object
 typedef enum {
   NOT_OK = 0,  // object is unusable
diff --git a/aom_util/aom_util.cmake b/aom_util/aom_util.cmake
index 6bf4fafc4c..d3da550485 100644
--- a/aom_util/aom_util.cmake
+++ b/aom_util/aom_util.cmake
@@ -13,7 +13,8 @@ if(AOM_AOM_UTIL_AOM_UTIL_CMAKE_)
 endif() # AOM_AOM_UTIL_AOM_UTIL_CMAKE_
 set(AOM_AOM_UTIL_AOM_UTIL_CMAKE_ 1)
 
-list(APPEND AOM_UTIL_SOURCES "${AOM_ROOT}/aom_util/aom_thread.c"
+list(APPEND AOM_UTIL_SOURCES "${AOM_ROOT}/aom_util/aom_pthread.h"
+            "${AOM_ROOT}/aom_util/aom_thread.c"
             "${AOM_ROOT}/aom_util/aom_thread.h"
             "${AOM_ROOT}/aom_util/endian_inl.h")
 
diff --git a/av1/av1_cx_iface.c b/av1/av1_cx_iface.c
index 9214feb4e6..190a43a6dd 100644
--- a/av1/av1_cx_iface.c
+++ b/av1/av1_cx_iface.c
@@ -17,6 +17,7 @@
 #include "config/aom_version.h"
 
 #include "aom_ports/mem_ops.h"
+#include "aom_util/aom_pthread.h"
 
 #include "aom/aom_encoder.h"
 #include "aom/internal/aom_codec_internal.h"
diff --git a/av1/av1_dx_iface.c b/av1/av1_dx_iface.c
index 3d7e132ab8..e05a7057e3 100644
--- a/av1/av1_dx_iface.c
+++ b/av1/av1_dx_iface.c
@@ -22,6 +22,7 @@
 #include "aom_dsp/bitreader_buffer.h"
 #include "aom_dsp/aom_dsp_common.h"
 #include "aom_ports/mem_ops.h"
+#include "aom_util/aom_pthread.h"
 #include "aom_util/aom_thread.h"
 
 #include "av1/common/alloccommon.h"
diff --git a/av1/common/alloccommon.c b/av1/common/alloccommon.c
index 2a9a8beb40..d6228be3bf 100644
--- a/av1/common/alloccommon.c
+++ b/av1/common/alloccommon.c
@@ -13,6 +13,7 @@
 #include "config/aom_config.h"
 
 #include "aom_mem/aom_mem.h"
+#include "aom_util/aom_pthread.h"
 
 #include "av1/common/alloccommon.h"
 #include "av1/common/av1_common_int.h"
diff --git a/av1/common/av1_common_int.h b/av1/common/av1_common_int.h
index 4c0cb99d2b..4e14c4a8be 100644
--- a/av1/common/av1_common_int.h
+++ b/av1/common/av1_common_int.h
@@ -17,7 +17,7 @@
 
 #include "aom/internal/aom_codec_internal.h"
 #include "aom_dsp/flow_estimation/corner_detect.h"
-#include "aom_util/aom_thread.h"
+#include "aom_util/aom_pthread.h"
 #include "av1/common/alloccommon.h"
 #include "av1/common/av1_loopfilter.h"
 #include "av1/common/entropy.h"
diff --git a/av1/common/cdef.c b/av1/common/cdef.c
index 12e9545441..7a35b90e90 100644
--- a/av1/common/cdef.c
+++ b/av1/common/cdef.c
@@ -16,6 +16,7 @@
 #include "config/aom_scale_rtcd.h"
 
 #include "aom/aom_integer.h"
+#include "aom_util/aom_pthread.h"
 #include "av1/common/av1_common_int.h"
 #include "av1/common/cdef.h"
 #include "av1/common/cdef_block.h"
diff --git a/av1/common/restoration.c b/av1/common/restoration.c
index 0be126fa65..131ef92108 100644
--- a/av1/common/restoration.c
+++ b/av1/common/restoration.c
@@ -17,6 +17,7 @@
 #include "config/aom_scale_rtcd.h"
 
 #include "aom_mem/aom_mem.h"
+#include "aom_util/aom_pthread.h"
 #include "av1/common/av1_common_int.h"
 #include "av1/common/resize.h"
 #include "av1/common/restoration.h"
diff --git a/av1/common/thread_common.c b/av1/common/thread_common.c
index 45695147ff..35a864550b 100644
--- a/av1/common/thread_common.c
+++ b/av1/common/thread_common.c
@@ -15,6 +15,7 @@
 
 #include "aom_dsp/aom_dsp_common.h"
 #include "aom_mem/aom_mem.h"
+#include "aom_util/aom_pthread.h"
 #include "av1/common/av1_loopfilter.h"
 #include "av1/common/entropymode.h"
 #include "av1/common/thread_common.h"
diff --git a/av1/common/thread_common.h b/av1/common/thread_common.h
index 675687dc98..7e681f322b 100644
--- a/av1/common/thread_common.h
+++ b/av1/common/thread_common.h
@@ -16,6 +16,7 @@
 
 #include "av1/common/av1_loopfilter.h"
 #include "av1/common/cdef.h"
+#include "aom_util/aom_pthread.h"
 #include "aom_util/aom_thread.h"
 
 #ifdef __cplusplus
diff --git a/av1/decoder/decodeframe.c b/av1/decoder/decodeframe.c
index bb09347e1c..3b4c601ac4 100644
--- a/av1/decoder/decodeframe.c
+++ b/av1/decoder/decodeframe.c
@@ -28,6 +28,7 @@
 #include "aom_ports/mem.h"
 #include "aom_ports/mem_ops.h"
 #include "aom_scale/aom_scale.h"
+#include "aom_util/aom_pthread.h"
 #include "aom_util/aom_thread.h"
 
 #if CONFIG_BITSTREAM_DEBUG || CONFIG_MISMATCH_DEBUG
diff --git a/av1/decoder/decoder.c b/av1/decoder/decoder.c
index 32e94840be..a886ed469c 100644
--- a/av1/decoder/decoder.c
+++ b/av1/decoder/decoder.c
@@ -21,6 +21,7 @@
 #include "aom_mem/aom_mem.h"
 #include "aom_ports/aom_timer.h"
 #include "aom_scale/aom_scale.h"
+#include "aom_util/aom_pthread.h"
 #include "aom_util/aom_thread.h"
 
 #include "av1/common/alloccommon.h"
diff --git a/av1/decoder/dthread.h b/av1/decoder/dthread.h
index f82b9d8ccf..b0f6fda829 100644
--- a/av1/decoder/dthread.h
+++ b/av1/decoder/dthread.h
@@ -14,7 +14,6 @@
 
 #include "config/aom_config.h"
 
-#include "aom_util/aom_thread.h"
 #include "aom/internal/aom_codec_internal.h"
 
 #ifdef __cplusplus
diff --git a/av1/encoder/allintra_vis.c b/av1/encoder/allintra_vis.c
index 8dcef5fc85..2f42fe1a0d 100644
--- a/av1/encoder/allintra_vis.c
+++ b/av1/encoder/allintra_vis.c
@@ -13,6 +13,8 @@
 
 #include "config/aom_config.h"
 
+#include "aom_util/aom_pthread.h"
+
 #if CONFIG_TFLITE
 #include "tensorflow/lite/c/c_api.h"
 #include "av1/encoder/deltaq4_model.c"
diff --git a/av1/encoder/bitstream.h b/av1/encoder/bitstream.h
index 12e8a630db..d037039593 100644
--- a/av1/encoder/bitstream.h
+++ b/av1/encoder/bitstream.h
@@ -21,6 +21,7 @@ extern "C" {
 #include "av1/common/enums.h"
 #include "av1/encoder/level.h"
 #include "aom_dsp/bitwriter.h"
+#include "aom_util/aom_pthread.h"
 
 struct aom_write_bit_buffer;
 struct AV1_COMP;
diff --git a/av1/encoder/encodeframe.c b/av1/encoder/encodeframe.c
index e2213a8355..c1d1b533b3 100644
--- a/av1/encoder/encodeframe.c
+++ b/av1/encoder/encodeframe.c
@@ -23,7 +23,7 @@
 #include "aom_dsp/binary_codes_writer.h"
 #include "aom_ports/mem.h"
 #include "aom_ports/aom_timer.h"
-
+#include "aom_util/aom_pthread.h"
 #if CONFIG_MISMATCH_DEBUG
 #include "aom_util/debug_util.h"
 #endif  // CONFIG_MISMATCH_DEBUG
diff --git a/av1/encoder/encoder.c b/av1/encoder/encoder.c
index 6324818108..4f8938bf85 100644
--- a/av1/encoder/encoder.c
+++ b/av1/encoder/encoder.c
@@ -35,6 +35,7 @@
 #include "aom_ports/aom_timer.h"
 #include "aom_ports/mem.h"
 #include "aom_scale/aom_scale.h"
+#include "aom_util/aom_pthread.h"
 #if CONFIG_BITSTREAM_DEBUG
 #include "aom_util/debug_util.h"
 #endif  // CONFIG_BITSTREAM_DEBUG
diff --git a/av1/encoder/encoder.h b/av1/encoder/encoder.h
index ff7dc0b7de..270d7a3603 100644
--- a/av1/encoder/encoder.h
+++ b/av1/encoder/encoder.h
@@ -21,6 +21,7 @@
 #include "config/aom_config.h"
 
 #include "aom/aomcx.h"
+#include "aom_util/aom_pthread.h"
 
 #include "av1/common/alloccommon.h"
 #include "av1/common/av1_common_int.h"
diff --git a/av1/encoder/ethread.c b/av1/encoder/ethread.c
index d6a806d504..ccb38f75a4 100644
--- a/av1/encoder/ethread.c
+++ b/av1/encoder/ethread.c
@@ -12,6 +12,8 @@
 #include <assert.h>
 #include <stdbool.h>
 
+#include "aom_util/aom_pthread.h"
+
 #include "av1/common/warped_motion.h"
 #include "av1/common/thread_common.h"
 
diff --git a/av1/encoder/firstpass.c b/av1/encoder/firstpass.c
index e20b6c177e..b94a50714a 100644
--- a/av1/encoder/firstpass.c
+++ b/av1/encoder/firstpass.c
@@ -22,6 +22,7 @@
 #include "aom_ports/mem.h"
 #include "aom_scale/aom_scale.h"
 #include "aom_scale/yv12config.h"
+#include "aom_util/aom_pthread.h"
 
 #include "av1/common/entropymv.h"
 #include "av1/common/quant_common.h"
diff --git a/av1/encoder/global_motion.h b/av1/encoder/global_motion.h
index 8c9c60f0f5..793bf92729 100644
--- a/av1/encoder/global_motion.h
+++ b/av1/encoder/global_motion.h
@@ -15,6 +15,7 @@
 #include "aom/aom_integer.h"
 #include "aom_dsp/flow_estimation/flow_estimation.h"
 #include "aom_scale/yv12config.h"
+#include "aom_util/aom_pthread.h"
 #include "aom_util/aom_thread.h"
 
 #ifdef __cplusplus
diff --git a/av1/encoder/temporal_filter.h b/av1/encoder/temporal_filter.h
index 6504b91b66..a40fb039b9 100644
--- a/av1/encoder/temporal_filter.h
+++ b/av1/encoder/temporal_filter.h
@@ -14,6 +14,8 @@
 
 #include <stdbool.h>
 
+#include "aom_util/aom_pthread.h"
+
 #ifdef __cplusplus
 extern "C" {
 #endif
diff --git a/av1/encoder/tpl_model.c b/av1/encoder/tpl_model.c
index ca60e4981e..59667bcfa3 100644
--- a/av1/encoder/tpl_model.c
+++ b/av1/encoder/tpl_model.c
@@ -19,6 +19,7 @@
 #include "config/aom_scale_rtcd.h"
 
 #include "aom/aom_codec.h"
+#include "aom_util/aom_pthread.h"
 
 #include "av1/common/av1_common_int.h"
 #include "av1/common/enums.h"
diff --git a/av1/encoder/tpl_model.h b/av1/encoder/tpl_model.h
index bcd58216c5..0150c702f9 100644
--- a/av1/encoder/tpl_model.h
+++ b/av1/encoder/tpl_model.h
@@ -30,6 +30,7 @@ struct TPL_INFO;
 #include "config/aom_config.h"
 
 #include "aom_scale/yv12config.h"
+#include "aom_util/aom_pthread.h"
 
 #include "av1/common/mv.h"
 #include "av1/common/scale.h"