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