1#ifndef HEADER_CURL_EASY_LOCK_H
2#define HEADER_CURL_EASY_LOCK_H
3/***************************************************************************
4 * _ _ ____ _
5 * Project ___| | | | _ \| |
6 * / __| | | | |_) | |
7 * | (__| |_| | _ <| |___
8 * \___|\___/|_| \_\_____|
9 *
10 * Copyright (C) Daniel Stenberg, <daniel@haxx.se>, et al.
11 *
12 * This software is licensed as described in the file COPYING, which
13 * you should have received as part of this distribution. The terms
14 * are also available at https://curl.se/docs/copyright.html.
15 *
16 * You may opt to use, copy, modify, merge, publish, distribute and/or sell
17 * copies of the Software, and permit persons to whom the Software is
18 * furnished to do so, under the terms of the COPYING file.
19 *
20 * This software is distributed on an "AS IS" basis, WITHOUT WARRANTY OF ANY
21 * KIND, either express or implied.
22 *
23 * SPDX-License-Identifier: curl
24 *
25 ***************************************************************************/
26
27#include "curl_setup.h"
28
29#define GLOBAL_INIT_IS_THREADSAFE
30
31#if defined(_WIN32_WINNT) && _WIN32_WINNT >= 0x600
32
33#ifdef __MINGW32__
34#ifndef SRWLOCK_INIT
35#define SRWLOCK_INIT NULL
36#endif
37#endif /* __MINGW32__ */
38
39#define curl_simple_lock SRWLOCK
40#define CURL_SIMPLE_LOCK_INIT SRWLOCK_INIT
41
42#define curl_simple_lock_lock(m) AcquireSRWLockExclusive(m)
43#define curl_simple_lock_unlock(m) ReleaseSRWLockExclusive(m)
44
45#elif defined(HAVE_ATOMIC) && defined(HAVE_STDATOMIC_H)
46#include <stdatomic.h>
47#if defined(HAVE_SCHED_YIELD)
48#include <sched.h>
49#endif
50
51#define curl_simple_lock atomic_int
52#define CURL_SIMPLE_LOCK_INIT 0
53
54/* a clang-thing */
55#ifndef __has_builtin
56#define __has_builtin(x) 0
57#endif
58
59#ifndef __INTEL_COMPILER
60/* The Intel compiler tries to look like GCC *and* clang *and* lies in its
61 __has_builtin() function, so override it. */
62
63/* if GCC on i386/x86_64 or if the built-in is present */
64#if ( (defined(__GNUC__) && !defined(__clang__)) && \
65 (defined(__i386__) || defined(__x86_64__))) || \
66 __has_builtin(__builtin_ia32_pause)
67#define HAVE_BUILTIN_IA32_PAUSE
68#endif
69
70#endif
71
72static inline void curl_simple_lock_lock(curl_simple_lock *lock)
73{
74 for(;;) {
75 if(!atomic_exchange_explicit(lock, true, memory_order_acquire))
76 break;
77 /* Reduce cache coherency traffic */
78 while(atomic_load_explicit(lock, memory_order_relaxed)) {
79 /* Reduce load (not mandatory) */
80#ifdef HAVE_BUILTIN_IA32_PAUSE
81 __builtin_ia32_pause();
82#elif defined(__aarch64__)
83 __asm__ volatile("yield" ::: "memory");
84#elif defined(HAVE_SCHED_YIELD)
85 sched_yield();
86#endif
87 }
88 }
89}
90
91static inline void curl_simple_lock_unlock(curl_simple_lock *lock)
92{
93 atomic_store_explicit(lock, false, memory_order_release);
94}
95
96#else
97
98#undef GLOBAL_INIT_IS_THREADSAFE
99
100#endif
101
102#endif /* HEADER_CURL_EASY_LOCK_H */
103