MySQL 26.7.0
Source Code Documentation
spin_lock_mutex.h
Go to the documentation of this file.
1// Copyright (c) 2026, Oracle and/or its affiliates.
2//
3// This program is free software; you can redistribute it and/or modify
4// it under the terms of the GNU General Public License, version 2.0,
5// as published by the Free Software Foundation.
6//
7// This program is designed to work with certain software (including
8// but not limited to OpenSSL) that is licensed under separate terms,
9// as designated in a particular file or component or in included license
10// documentation. The authors of MySQL hereby grant you an additional
11// permission to link the program and your derivative works with the
12// separately licensed software that they have either included with
13// the program or referenced in the documentation.
14//
15// This program is distributed in the hope that it will be useful,
16// but WITHOUT ANY WARRANTY; without even the implied warranty of
17// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
18// GNU General Public License, version 2.0, for more details.
19//
20// You should have received a copy of the GNU General Public License
21// along with this program; if not, write to the Free Software
22// Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
23
24/// @defgroup GroupLibsMysqlConcurrency Concurrency
25/// @ingroup GroupLibsMysql
26
27#ifndef MYSQL_CONCURRENCY_SPIN_LOCK_MUTEX
28#define MYSQL_CONCURRENCY_SPIN_LOCK_MUTEX
29
30#include <atomic>
31#include <thread>
33
34#if defined(_MSC_VER)
35#include <intrin.h>
36#elif defined(__i386__) || defined(__x86_64__)
37#if defined(__INTEL_COMPILER)
38#include <immintrin.h>
39#elif defined(__clang__)
40#include <emmintrin.h>
41#endif
42#endif
43
44/// @addtogroup GroupLibsMysqlConcurrency
45/// @{
46
47namespace mysql::concurrency {
48
49/// @brief Implementation of a spin-lock mutex. Satisfies requirements of
50/// *Mutex*
51/// - Lockable
52/// - DefaultConstructible
53/// - Destructible
54/// - not copyable
55/// - not movable
56/// @note Use with care. In general, is not recommended to use spin locks over
57/// mutexes. Spin locks should be used only when expected contention is minimal
58/// (e.g. when combining with strip-locking techniques).
60 public:
61 // Disable copy-move semantics
66
67 Spin_lock_mutex([[maybe_unused]] Mutex_key key = {}) {}
68 ~Spin_lock_mutex() = default;
69
70 /// @brief Acquire a lock, blocks access to a critical section until "unlock"
71 /// is called
72 void lock() noexcept;
73
74 /// Non-blocking try-lock operation
75 /// @retval true Lock has been acquired
76 /// @retval false Lock has NOT been acquired
77 bool try_lock() noexcept;
78
79 /// @brief Unblocks access to a critical section for other threads
80 void unlock();
81
82 private:
83 static inline void spin_yield() noexcept {
84// x86 / x86_64
85#if defined(_MSC_VER) && (defined(_M_IX86) || defined(_M_X64))
86 _mm_pause();
87#elif defined(__i386__) || defined(__x86_64__)
88 __builtin_ia32_pause();
89// ARM
90#elif defined(__arm__) || defined(__aarch64__)
91 __asm__ volatile("yield" ::: "memory");
92// Other architectures
93#else
94 std::this_thread::yield();
95#endif
96 }
97 /// Internal synchronization boolean, true means that lock is currently owned
98 /// Since C++20, default constructor sets atomic_flag to "clear" state
99 /// We use atomic_flag because it is guaranteed to be lock free
100 std::atomic_flag m_lock = ATOMIC_FLAG_INIT;
101};
102
103} // namespace mysql::concurrency
104
105/// @}
106
107#endif // MYSQL_CONCURRENCY_SPIN_LOCK_MUTEX
Implementation of a spin-lock mutex.
Definition: spin_lock_mutex.h:59
Spin_lock_mutex & operator=(Spin_lock_mutex &&src)=delete
Spin_lock_mutex(Spin_lock_mutex &&)=delete
Spin_lock_mutex(const Spin_lock_mutex &)=delete
bool try_lock() noexcept
Non-blocking try-lock operation.
Definition: spin_lock_mutex.cpp:41
Spin_lock_mutex & operator=(const Spin_lock_mutex &src)=delete
void unlock()
Unblocks access to a critical section for other threads.
Definition: spin_lock_mutex.cpp:46
std::atomic_flag m_lock
Internal synchronization boolean, true means that lock is currently owned Since C++20,...
Definition: spin_lock_mutex.h:100
static void spin_yield() noexcept
Definition: spin_lock_mutex.h:83
Spin_lock_mutex(Mutex_key key={})
Definition: spin_lock_mutex.h:67
void lock() noexcept
Acquire a lock, blocks access to a critical section until "unlock" is called.
Definition: spin_lock_mutex.cpp:28
Definition: cache_line_size.h:31
PSI_mutex_key Mutex_key
Definition: mutex_srv.h:41
noexcept
The return type for any call_and_catch(f, args...) call where f(args...) returns Type.
Definition: call_and_catch.h:76
required string key
Definition: replication_asynchronous_connection_failover.proto:60