easy_lock.h 3.0 KB

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