/src/curl/lib/easy_lock.h
Line | Count | Source (jump to first uncovered line) |
1 | | /*************************************************************************** |
2 | | * _ _ ____ _ |
3 | | * Project ___| | | | _ \| | |
4 | | * / __| | | | |_) | | |
5 | | * | (__| |_| | _ <| |___ |
6 | | * \___|\___/|_| \_\_____| |
7 | | * |
8 | | * Copyright (C) Daniel Stenberg, <daniel@haxx.se>, et al. |
9 | | * |
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. |
13 | | * |
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. |
17 | | * |
18 | | * This software is distributed on an "AS IS" basis, WITHOUT WARRANTY OF ANY |
19 | | * KIND, either express or implied. |
20 | | * |
21 | | * SPDX-License-Identifier: curl |
22 | | * |
23 | | ***************************************************************************/ |
24 | | |
25 | | #include "curl_setup.h" |
26 | | |
27 | | #define GLOBAL_INIT_IS_THREADSAFE |
28 | | |
29 | | #if defined(_WIN32_WINNT) && _WIN32_WINNT >= 0x600 |
30 | | |
31 | | #ifdef __MINGW32__ |
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; |
37 | | #endif |
38 | | #endif |
39 | | #ifndef SRWLOCK_INIT |
40 | | #define SRWLOCK_INIT NULL |
41 | | #endif |
42 | | #endif /* __MINGW32__ */ |
43 | | |
44 | | #define curl_simple_lock SRWLOCK |
45 | | #define CURL_SIMPLE_LOCK_INIT SRWLOCK_INIT |
46 | | |
47 | | #define curl_simple_lock_lock(m) AcquireSRWLockExclusive(m) |
48 | | #define curl_simple_lock_unlock(m) ReleaseSRWLockExclusive(m) |
49 | | |
50 | | #elif defined(HAVE_ATOMIC) && defined(HAVE_STDATOMIC_H) |
51 | | #include <stdatomic.h> |
52 | | #if defined(HAVE_SCHED_YIELD) |
53 | | #include <sched.h> |
54 | | #endif |
55 | | |
56 | | #define curl_simple_lock atomic_int |
57 | | #define CURL_SIMPLE_LOCK_INIT 0 |
58 | | |
59 | | /* a clang-thing */ |
60 | | #ifndef __has_builtin |
61 | | #define __has_builtin(x) 0 |
62 | | #endif |
63 | | |
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. */ |
67 | | |
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 |
73 | | #endif |
74 | | |
75 | | #endif |
76 | | |
77 | | static inline void curl_simple_lock_lock(curl_simple_lock *lock) |
78 | 1.20k | { |
79 | 1.20k | for(;;) { |
80 | 1.20k | if(!atomic_exchange_explicit(lock, true, memory_order_acquire)) |
81 | 1.20k | break; |
82 | | /* Reduce cache coherency traffic */ |
83 | 0 | while(atomic_load_explicit(lock, memory_order_relaxed)) { |
84 | | /* Reduce load (not mandatory) */ |
85 | 0 | #ifdef HAVE_BUILTIN_IA32_PAUSE |
86 | 0 | __builtin_ia32_pause(); |
87 | | #elif defined(__aarch64__) |
88 | | __asm__ volatile("yield" ::: "memory"); |
89 | | #elif defined(HAVE_SCHED_YIELD) |
90 | | sched_yield(); |
91 | | #endif |
92 | 0 | } |
93 | 0 | } |
94 | 1.20k | } easy.c:curl_simple_lock_lock Line | Count | Source | 78 | 1.20k | { | 79 | 1.20k | for(;;) { | 80 | 1.20k | if(!atomic_exchange_explicit(lock, true, memory_order_acquire)) | 81 | 1.20k | break; | 82 | | /* Reduce cache coherency traffic */ | 83 | 0 | while(atomic_load_explicit(lock, memory_order_relaxed)) { | 84 | | /* Reduce load (not mandatory) */ | 85 | 0 | #ifdef HAVE_BUILTIN_IA32_PAUSE | 86 | 0 | __builtin_ia32_pause(); | 87 | | #elif defined(__aarch64__) | 88 | | __asm__ volatile("yield" ::: "memory"); | 89 | | #elif defined(HAVE_SCHED_YIELD) | 90 | | sched_yield(); | 91 | | #endif | 92 | 0 | } | 93 | 0 | } | 94 | 1.20k | } |
Unexecuted instantiation: hostip.c:curl_simple_lock_lock |
95 | | |
96 | | static inline void curl_simple_lock_unlock(curl_simple_lock *lock) |
97 | 1.20k | { |
98 | 1.20k | atomic_store_explicit(lock, false, memory_order_release); |
99 | 1.20k | } easy.c:curl_simple_lock_unlock Line | Count | Source | 97 | 1.20k | { | 98 | 1.20k | atomic_store_explicit(lock, false, memory_order_release); | 99 | 1.20k | } |
Unexecuted instantiation: hostip.c:curl_simple_lock_unlock |
100 | | |
101 | | #else |
102 | | |
103 | | #undef GLOBAL_INIT_IS_THREADSAFE |
104 | | |
105 | | #endif |