/src/curl/lib/easy_lock.h
Line | Count | Source |
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 | | #include "curl_setup.h" |
27 | | |
28 | | #define GLOBAL_INIT_IS_THREADSAFE |
29 | | |
30 | | #ifdef _WIN32 |
31 | | |
32 | | #define curl_simple_lock SRWLOCK |
33 | | #define CURL_SIMPLE_LOCK_INIT SRWLOCK_INIT |
34 | | |
35 | | #define curl_simple_lock_lock(m) AcquireSRWLockExclusive(m) |
36 | | #define curl_simple_lock_unlock(m) ReleaseSRWLockExclusive(m) |
37 | | |
38 | | #elif defined(HAVE_ATOMIC) && defined(HAVE_STDATOMIC_H) |
39 | | #include <stdatomic.h> |
40 | | #ifdef HAVE_SCHED_YIELD |
41 | | #include <sched.h> |
42 | | #endif |
43 | | |
44 | | #define curl_simple_lock atomic_int |
45 | | #define CURL_SIMPLE_LOCK_INIT 0 |
46 | | |
47 | | #ifndef __INTEL_COMPILER |
48 | | /* The Intel compiler tries to look like GCC *and* clang *and* lies in its |
49 | | __has_builtin() function, so override it. */ |
50 | | |
51 | | /* if GCC on i386/x86_64 or if the built-in is present */ |
52 | | #if (defined(__GNUC__) && !defined(__clang__)) && \ |
53 | | (defined(__i386__) || defined(__x86_64__)) |
54 | | #define HAVE_BUILTIN_IA32_PAUSE |
55 | | #elif defined(__has_builtin) /* Keep this PP check separate from others */ |
56 | | #if __has_builtin(__builtin_ia32_pause) |
57 | | #define HAVE_BUILTIN_IA32_PAUSE |
58 | | #endif |
59 | | #endif |
60 | | |
61 | | #endif /* !__INTEL_COMPILER */ |
62 | | |
63 | | static CURL_INLINE void curl_simple_lock_lock(curl_simple_lock *lock) |
64 | 6.28k | { |
65 | 6.28k | for(;;) { |
66 | 6.28k | if(!atomic_exchange_explicit(lock, true, memory_order_acquire)) |
67 | 6.28k | break; |
68 | | /* Reduce cache coherency traffic */ |
69 | 0 | while(atomic_load_explicit(lock, memory_order_relaxed)) { |
70 | | /* Reduce load (not mandatory) */ |
71 | 0 | #ifdef HAVE_BUILTIN_IA32_PAUSE |
72 | 0 | __builtin_ia32_pause(); |
73 | | #elif defined(__aarch64__) |
74 | | __asm__ volatile("yield" ::: "memory"); |
75 | | #elif defined(HAVE_SCHED_YIELD) |
76 | | sched_yield(); |
77 | | #endif |
78 | 0 | } |
79 | 0 | } |
80 | 6.28k | } easy.c:curl_simple_lock_lock Line | Count | Source | 64 | 6.28k | { | 65 | 6.28k | for(;;) { | 66 | 6.28k | if(!atomic_exchange_explicit(lock, true, memory_order_acquire)) | 67 | 6.28k | break; | 68 | | /* Reduce cache coherency traffic */ | 69 | 0 | while(atomic_load_explicit(lock, memory_order_relaxed)) { | 70 | | /* Reduce load (not mandatory) */ | 71 | 0 | #ifdef HAVE_BUILTIN_IA32_PAUSE | 72 | 0 | __builtin_ia32_pause(); | 73 | | #elif defined(__aarch64__) | 74 | | __asm__ volatile("yield" ::: "memory"); | 75 | | #elif defined(HAVE_SCHED_YIELD) | 76 | | sched_yield(); | 77 | | #endif | 78 | 0 | } | 79 | 0 | } | 80 | 6.28k | } |
Unexecuted instantiation: hostip.c:curl_simple_lock_lock |
81 | | |
82 | | static CURL_INLINE void curl_simple_lock_unlock(curl_simple_lock *lock) |
83 | 6.28k | { |
84 | 6.28k | atomic_store_explicit(lock, false, memory_order_release); |
85 | 6.28k | } easy.c:curl_simple_lock_unlock Line | Count | Source | 83 | 6.28k | { | 84 | 6.28k | atomic_store_explicit(lock, false, memory_order_release); | 85 | 6.28k | } |
Unexecuted instantiation: hostip.c:curl_simple_lock_unlock |
86 | | |
87 | | #elif defined(HAVE_THREADS_POSIX) |
88 | | |
89 | | #define curl_simple_lock pthread_mutex_t |
90 | | #define CURL_SIMPLE_LOCK_INIT PTHREAD_MUTEX_INITIALIZER |
91 | | #define curl_simple_lock_lock(m) pthread_mutex_lock(m) |
92 | | #define curl_simple_lock_unlock(m) pthread_mutex_unlock(m) |
93 | | |
94 | | #else |
95 | | |
96 | | #undef GLOBAL_INIT_IS_THREADSAFE |
97 | | |
98 | | #endif |
99 | | |
100 | | #endif /* HEADER_CURL_EASY_LOCK_H */ |