From 401807c8bf46776f03286a3873d777f417189067 Mon Sep 17 00:00:00 2001 From: XNNPACK Team Date: Wed, 7 Aug 2024 13:33:44 -0700 Subject: [PATCH] Add XNN_HAS_PTHREADS to common.h, as a way to indicate that a given platform doesn't support pthreads() at all. Note that it's initially (effectively) unused, since it's only set to 0 for Windows, which already has its own custom code path for threading; however, this flag will be used for at least one upcoming target and landing it separately from introduction of that target seems easier to review. PiperOrigin-RevId: 660518641 --- src/xnnpack/common.h | 6 ++++++ src/xnnpack/init-once.h | 22 ++++++++++++++++++++-- 2 files changed, 26 insertions(+), 2 deletions(-) diff --git a/src/xnnpack/common.h b/src/xnnpack/common.h index 86e9c9b1da36..19eb1d427a28 100644 --- a/src/xnnpack/common.h +++ b/src/xnnpack/common.h @@ -149,6 +149,12 @@ #define XNN_HAS_MMAP 1 #endif +#if XNN_PLATFORM_WINDOWS + #define XNN_HAS_PTHREADS 0 +#else + #define XNN_HAS_PTHREADS 1 +#endif + // Define compile identification macros #if defined(__clang__) diff --git a/src/xnnpack/init-once.h b/src/xnnpack/init-once.h index 9c8637517523..1bcc7fd1e4a0 100644 --- a/src/xnnpack/init-once.h +++ b/src/xnnpack/init-once.h @@ -1,4 +1,4 @@ -// Copyright 2019 Google LLC +// Copyright 2024 Google LLC // // This source code is licensed under the BSD-style license found in the // LICENSE file in the root directory of this source tree. @@ -9,6 +9,7 @@ #include #else #include + #include #endif #include "xnnpack.h" @@ -27,7 +28,7 @@ #define XNN_INIT_ONCE(name) \ InitOnceExecuteOnce(&name##_guard, &name##_windows_wrapper, NULL, NULL) /* no semicolon */ -#else +#elif XNN_HAS_PTHREADS #define XNN_INIT_ONCE_GUARD(name) \ static void init_##name##_config(void); \ @@ -36,4 +37,21 @@ #define XNN_INIT_ONCE(name) \ pthread_once(&name##_guard, &init_##name##_config) /* no semicolon */ +#else + + // If we don't have pthreads available (and there isn't any other platform + // specialization), assume we can just get by without special synchronization. + + #define XNN_INIT_ONCE_GUARD(name) \ + static void init_##name##_config(void); \ + static bool name##_guard = 0 /* no semicolon */ + + #define XNN_INIT_ONCE(name) \ + do { \ + if (!(name##_guard)) { \ + init_##name##_config(); \ + name##_guard = 1; \ + } \ + } while (0) /* no semicolon */ + #endif