easy_lock.h 3.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109
  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. #define GLOBAL_INIT_IS_THREADSAFE
  28. #if defined(_WIN32_WINNT) && _WIN32_WINNT >= 0x600
  29. #ifdef __MINGW32__
  30. #ifndef __MINGW64_VERSION_MAJOR
  31. #if (__MINGW32_MAJOR_VERSION < 5) || \
  32. (__MINGW32_MAJOR_VERSION == 5 && __MINGW32_MINOR_VERSION == 0)
  33. /* mingw >= 5.0.1 defines SRWLOCK, and slightly different from MS define */
  34. typedef PVOID SRWLOCK, *PSRWLOCK;
  35. #endif
  36. #endif
  37. #ifndef SRWLOCK_INIT
  38. #define SRWLOCK_INIT NULL
  39. #endif
  40. #endif /* __MINGW32__ */
  41. #define curl_simple_lock SRWLOCK
  42. #define CURL_SIMPLE_LOCK_INIT SRWLOCK_INIT
  43. #define curl_simple_lock_lock(m) AcquireSRWLockExclusive(m)
  44. #define curl_simple_lock_unlock(m) ReleaseSRWLockExclusive(m)
  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. #define curl_simple_lock atomic_int
  51. #define CURL_SIMPLE_LOCK_INIT 0
  52. /* a clang-thing */
  53. #ifndef __has_builtin
  54. #define __has_builtin(x) 0
  55. #endif
  56. #ifndef __INTEL_COMPILER
  57. /* The Intel compiler tries to look like GCC *and* clang *and* lies in its
  58. __has_builtin() function, so override it. */
  59. /* if GCC on i386/x86_64 or if the built-in is present */
  60. #if ( (defined(__GNUC__) && !defined(__clang__)) && \
  61. (defined(__i386__) || defined(__x86_64__))) || \
  62. __has_builtin(__builtin_ia32_pause)
  63. #define HAVE_BUILTIN_IA32_PAUSE
  64. #endif
  65. #endif
  66. static inline void curl_simple_lock_lock(curl_simple_lock *lock)
  67. {
  68. for(;;) {
  69. if(!atomic_exchange_explicit(lock, true, memory_order_acquire))
  70. break;
  71. /* Reduce cache coherency traffic */
  72. while(atomic_load_explicit(lock, memory_order_relaxed)) {
  73. /* Reduce load (not mandatory) */
  74. #ifdef HAVE_BUILTIN_IA32_PAUSE
  75. __builtin_ia32_pause();
  76. #elif defined(__aarch64__)
  77. __asm__ volatile("yield" ::: "memory");
  78. #elif defined(HAVE_SCHED_YIELD)
  79. sched_yield();
  80. #endif
  81. }
  82. }
  83. }
  84. static inline void curl_simple_lock_unlock(curl_simple_lock *lock)
  85. {
  86. atomic_store_explicit(lock, false, memory_order_release);
  87. }
  88. #else
  89. #undef GLOBAL_INIT_IS_THREADSAFE
  90. #endif
  91. #endif /* HEADER_CURL_EASY_LOCK_H */