113498266Sopenharmony_ci#ifndef HEADER_CURL_EASY_LOCK_H
213498266Sopenharmony_ci#define HEADER_CURL_EASY_LOCK_H
313498266Sopenharmony_ci/***************************************************************************
413498266Sopenharmony_ci *                                  _   _ ____  _
513498266Sopenharmony_ci *  Project                     ___| | | |  _ \| |
613498266Sopenharmony_ci *                             / __| | | | |_) | |
713498266Sopenharmony_ci *                            | (__| |_| |  _ <| |___
813498266Sopenharmony_ci *                             \___|\___/|_| \_\_____|
913498266Sopenharmony_ci *
1013498266Sopenharmony_ci * Copyright (C) Daniel Stenberg, <daniel@haxx.se>, et al.
1113498266Sopenharmony_ci *
1213498266Sopenharmony_ci * This software is licensed as described in the file COPYING, which
1313498266Sopenharmony_ci * you should have received as part of this distribution. The terms
1413498266Sopenharmony_ci * are also available at https://curl.se/docs/copyright.html.
1513498266Sopenharmony_ci *
1613498266Sopenharmony_ci * You may opt to use, copy, modify, merge, publish, distribute and/or sell
1713498266Sopenharmony_ci * copies of the Software, and permit persons to whom the Software is
1813498266Sopenharmony_ci * furnished to do so, under the terms of the COPYING file.
1913498266Sopenharmony_ci *
2013498266Sopenharmony_ci * This software is distributed on an "AS IS" basis, WITHOUT WARRANTY OF ANY
2113498266Sopenharmony_ci * KIND, either express or implied.
2213498266Sopenharmony_ci *
2313498266Sopenharmony_ci * SPDX-License-Identifier: curl
2413498266Sopenharmony_ci *
2513498266Sopenharmony_ci ***************************************************************************/
2613498266Sopenharmony_ci
2713498266Sopenharmony_ci#include "curl_setup.h"
2813498266Sopenharmony_ci
2913498266Sopenharmony_ci#define GLOBAL_INIT_IS_THREADSAFE
3013498266Sopenharmony_ci
3113498266Sopenharmony_ci#if defined(_WIN32_WINNT) && _WIN32_WINNT >= 0x600
3213498266Sopenharmony_ci
3313498266Sopenharmony_ci#ifdef __MINGW32__
3413498266Sopenharmony_ci#ifndef SRWLOCK_INIT
3513498266Sopenharmony_ci#define SRWLOCK_INIT NULL
3613498266Sopenharmony_ci#endif
3713498266Sopenharmony_ci#endif /* __MINGW32__ */
3813498266Sopenharmony_ci
3913498266Sopenharmony_ci#define curl_simple_lock SRWLOCK
4013498266Sopenharmony_ci#define CURL_SIMPLE_LOCK_INIT SRWLOCK_INIT
4113498266Sopenharmony_ci
4213498266Sopenharmony_ci#define curl_simple_lock_lock(m) AcquireSRWLockExclusive(m)
4313498266Sopenharmony_ci#define curl_simple_lock_unlock(m) ReleaseSRWLockExclusive(m)
4413498266Sopenharmony_ci
4513498266Sopenharmony_ci#elif defined(HAVE_ATOMIC) && defined(HAVE_STDATOMIC_H)
4613498266Sopenharmony_ci#include <stdatomic.h>
4713498266Sopenharmony_ci#if defined(HAVE_SCHED_YIELD)
4813498266Sopenharmony_ci#include <sched.h>
4913498266Sopenharmony_ci#endif
5013498266Sopenharmony_ci
5113498266Sopenharmony_ci#define curl_simple_lock atomic_int
5213498266Sopenharmony_ci#define CURL_SIMPLE_LOCK_INIT 0
5313498266Sopenharmony_ci
5413498266Sopenharmony_ci/* a clang-thing */
5513498266Sopenharmony_ci#ifndef __has_builtin
5613498266Sopenharmony_ci#define __has_builtin(x) 0
5713498266Sopenharmony_ci#endif
5813498266Sopenharmony_ci
5913498266Sopenharmony_ci#ifndef __INTEL_COMPILER
6013498266Sopenharmony_ci/* The Intel compiler tries to look like GCC *and* clang *and* lies in its
6113498266Sopenharmony_ci   __has_builtin() function, so override it. */
6213498266Sopenharmony_ci
6313498266Sopenharmony_ci/* if GCC on i386/x86_64 or if the built-in is present */
6413498266Sopenharmony_ci#if ( (defined(__GNUC__) && !defined(__clang__)) &&     \
6513498266Sopenharmony_ci      (defined(__i386__) || defined(__x86_64__))) ||    \
6613498266Sopenharmony_ci  __has_builtin(__builtin_ia32_pause)
6713498266Sopenharmony_ci#define HAVE_BUILTIN_IA32_PAUSE
6813498266Sopenharmony_ci#endif
6913498266Sopenharmony_ci
7013498266Sopenharmony_ci#endif
7113498266Sopenharmony_ci
7213498266Sopenharmony_cistatic inline void curl_simple_lock_lock(curl_simple_lock *lock)
7313498266Sopenharmony_ci{
7413498266Sopenharmony_ci  for(;;) {
7513498266Sopenharmony_ci    if(!atomic_exchange_explicit(lock, true, memory_order_acquire))
7613498266Sopenharmony_ci      break;
7713498266Sopenharmony_ci    /* Reduce cache coherency traffic */
7813498266Sopenharmony_ci    while(atomic_load_explicit(lock, memory_order_relaxed)) {
7913498266Sopenharmony_ci      /* Reduce load (not mandatory) */
8013498266Sopenharmony_ci#ifdef HAVE_BUILTIN_IA32_PAUSE
8113498266Sopenharmony_ci      __builtin_ia32_pause();
8213498266Sopenharmony_ci#elif defined(__aarch64__)
8313498266Sopenharmony_ci      __asm__ volatile("yield" ::: "memory");
8413498266Sopenharmony_ci#elif defined(HAVE_SCHED_YIELD)
8513498266Sopenharmony_ci      sched_yield();
8613498266Sopenharmony_ci#endif
8713498266Sopenharmony_ci    }
8813498266Sopenharmony_ci  }
8913498266Sopenharmony_ci}
9013498266Sopenharmony_ci
9113498266Sopenharmony_cistatic inline void curl_simple_lock_unlock(curl_simple_lock *lock)
9213498266Sopenharmony_ci{
9313498266Sopenharmony_ci  atomic_store_explicit(lock, false, memory_order_release);
9413498266Sopenharmony_ci}
9513498266Sopenharmony_ci
9613498266Sopenharmony_ci#elif defined(USE_THREADS_POSIX) && defined(HAVE_PTHREAD_H)
9713498266Sopenharmony_ci
9813498266Sopenharmony_ci#include <pthread.h>
9913498266Sopenharmony_ci
10013498266Sopenharmony_ci#define curl_simple_lock pthread_mutex_t
10113498266Sopenharmony_ci#define CURL_SIMPLE_LOCK_INIT PTHREAD_MUTEX_INITIALIZER
10213498266Sopenharmony_ci#define curl_simple_lock_lock(m) pthread_mutex_lock(m)
10313498266Sopenharmony_ci#define curl_simple_lock_unlock(m) pthread_mutex_unlock(m)
10413498266Sopenharmony_ci
10513498266Sopenharmony_ci#else
10613498266Sopenharmony_ci
10713498266Sopenharmony_ci#undef  GLOBAL_INIT_IS_THREADSAFE
10813498266Sopenharmony_ci
10913498266Sopenharmony_ci#endif
11013498266Sopenharmony_ci
11113498266Sopenharmony_ci#endif /* HEADER_CURL_EASY_LOCK_H */
112