Implement WEBP_DSP_INIT with SRWLOCK for Windows

A slim reader/writer (SRW) lock can be initialized statically with the
constant SRWLOCK_INIT. It is the only Windows synchronization object I
can find with this property.

Note: On old Windows versions that don't have SRWLOCK, use the fallback,
thread-unsafe implementation.

Change a NOLINT comment to a NOLINTNEXTLINE comment to prevent
clang-format from aligning the #else and #endif comments in undesired
way.

Bug: 435213378
Change-Id: Iecff615a14a1905aedd2c05ad9444889f711cc17
(cherry picked from commit 54f23b049e)
This commit is contained in:
Wan-Teh Chang
2025-08-01 16:20:52 -07:00
committed by James Zern
parent 166968b809
commit 991170bbab

View File

@@ -189,8 +189,40 @@
#endif
#endif
#if defined(WEBP_USE_THREAD) && !defined(_WIN32)
#include <pthread.h> // NOLINT
#if defined(WEBP_USE_THREAD)
#if defined(_WIN32)
#include <windows.h>
#if _WIN32_WINNT >= 0x0600
// clang-format off
#define WEBP_DSP_INIT_VARS(func) \
static VP8CPUInfo func##_last_cpuinfo_used = \
(VP8CPUInfo)&func##_last_cpuinfo_used; \
static SRWLOCK func##_lock = SRWLOCK_INIT
#define WEBP_DSP_INIT(func) \
do { \
AcquireSRWLockExclusive(&func##_lock); \
if (func##_last_cpuinfo_used != VP8GetCPUInfo) func(); \
func##_last_cpuinfo_used = VP8GetCPUInfo; \
ReleaseSRWLockExclusive(&func##_lock); \
} while (0)
// clang-format on
#else // _WIN32_WINNT < 0x0600
// clang-format off
#define WEBP_DSP_INIT_VARS(func) \
static volatile VP8CPUInfo func##_last_cpuinfo_used = \
(VP8CPUInfo)&func##_last_cpuinfo_used
#define WEBP_DSP_INIT(func) \
do { \
if (func##_last_cpuinfo_used == VP8GetCPUInfo) break; \
func(); \
func##_last_cpuinfo_used = VP8GetCPUInfo; \
} while (0)
// clang-format on
#endif // _WIN32_WINNT >= 0x0600
#else // !defined(_WIN32)
// NOLINTNEXTLINE
#include <pthread.h>
#define WEBP_DSP_INIT_VARS(func) \
static VP8CPUInfo func##_last_cpuinfo_used = \
@@ -203,7 +235,8 @@
func##_last_cpuinfo_used = VP8GetCPUInfo; \
(void)pthread_mutex_unlock(&func##_lock); \
} while (0)
#else // !(defined(WEBP_USE_THREAD) && !defined(_WIN32))
#endif // defined(_WIN32)
#else // !defined(WEBP_USE_THREAD)
#define WEBP_DSP_INIT_VARS(func) \
static volatile VP8CPUInfo func##_last_cpuinfo_used = \
(VP8CPUInfo)&func##_last_cpuinfo_used
@@ -213,7 +246,7 @@
func(); \
func##_last_cpuinfo_used = VP8GetCPUInfo; \
} while (0)
#endif // defined(WEBP_USE_THREAD) && !defined(_WIN32)
#endif // defined(WEBP_USE_THREAD)
// Defines an Init + helper function that control multiple initialization of
// function pointers / tables.