1 /***************************************************************************
3 * Project ___| | | | _ \| |
5 * | (__| |_| | _ <| |___
6 * \___|\___/|_| \_\_____|
8 * Copyright (C) 1998 - 2022, Daniel Stenberg, <daniel@haxx.se>, et al.
10 * This software is licensed as described in the file COPYING, which
11 * you should have received as part of this distribution. The terms
12 * are also available at https://curl.se/docs/copyright.html.
14 * You may opt to use, copy, modify, merge, publish, distribute and/or sell
15 * copies of the Software, and permit persons to whom the Software is
16 * furnished to do so, under the terms of the COPYING file.
18 * This software is distributed on an "AS IS" basis, WITHOUT WARRANTY OF ANY
19 * KIND, either express or implied.
21 * SPDX-License-Identifier: curl
23 ***************************************************************************/
25 #include "curl_setup.h"
27 #define GLOBAL_INIT_IS_THREADSAFE
29 #if defined(_WIN32_WINNT) && _WIN32_WINNT >= 0x600
32 #ifndef __MINGW64_VERSION_MAJOR
33 #if (__MINGW32_MAJOR_VERSION < 5) || \
34 (__MINGW32_MAJOR_VERSION == 5 && __MINGW32_MINOR_VERSION == 0)
35 /* mingw >= 5.0.1 defines SRWLOCK, and slightly different from MS define */
36 typedef PVOID SRWLOCK, *PSRWLOCK;
40 #define SRWLOCK_INIT NULL
42 #endif /* __MINGW32__ */
44 #define curl_simple_lock SRWLOCK
45 #define CURL_SIMPLE_LOCK_INIT SRWLOCK_INIT
47 #define curl_simple_lock_lock(m) AcquireSRWLockExclusive(m)
48 #define curl_simple_lock_unlock(m) ReleaseSRWLockExclusive(m)
50 #elif defined(HAVE_ATOMIC) && defined(HAVE_STDATOMIC_H)
51 #include <stdatomic.h>
52 #if defined(HAVE_SCHED_YIELD)
56 #define curl_simple_lock atomic_int
57 #define CURL_SIMPLE_LOCK_INIT 0
61 #define __has_builtin(x) 0
64 #ifndef __INTEL_COMPILER
65 /* The Intel compiler tries to look like GCC *and* clang *and* lies in its
66 __has_builtin() function, so override it. */
68 /* if GCC on i386/x86_64 or if the built-in is present */
69 #if ( (defined(__GNUC__) && !defined(__clang__)) && \
70 (defined(__i386__) || defined(__x86_64__))) || \
71 __has_builtin(__builtin_ia32_pause)
72 #define HAVE_BUILTIN_IA32_PAUSE
77 static inline void curl_simple_lock_lock(curl_simple_lock *lock)
80 if(!atomic_exchange_explicit(lock, true, memory_order_acquire))
82 /* Reduce cache coherency traffic */
83 while(atomic_load_explicit(lock, memory_order_relaxed)) {
84 /* Reduce load (not mandatory) */
85 #ifdef HAVE_BUILTIN_IA32_PAUSE
86 __builtin_ia32_pause();
87 #elif defined(__aarch64__)
88 __asm__ volatile("yield" ::: "memory");
89 #elif defined(HAVE_SCHED_YIELD)
96 static inline void curl_simple_lock_unlock(curl_simple_lock *lock)
98 atomic_store_explicit(lock, false, memory_order_release);
103 #undef GLOBAL_INIT_IS_THREADSAFE