MySQL 8.3.0
Source Code Documentation
lock_free_pool.h
Go to the documentation of this file.
1/* Copyright (c) 2020, 2023, Oracle and/or its affiliates.
2
3This program is free software; you can redistribute it and/or modify it under
4the terms of the GNU General Public License, version 2.0, as published by the
5Free Software Foundation.
6
7This program is also distributed with certain software (including but not
8limited to OpenSSL) that is licensed under separate terms, as designated in a
9particular file or component or in included license documentation. The authors
10of MySQL hereby grant you an additional permission to link the program and
11your derivative works with the separately licensed software that they have
12included with MySQL.
13
14This program is distributed in the hope that it will be useful, but WITHOUT
15ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
16FOR A PARTICULAR PURPOSE. See the GNU General Public License, version 2.0,
17for more details.
18
19You should have received a copy of the GNU General Public License along with
20this program; if not, write to the Free Software Foundation, Inc.,
2151 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA */
22
23/** @file storage/temptable/include/temptable/lock_free_pool.h
24Lock-free pool implementation. */
25
26#ifndef TEMPTABLE_LOCK_FREE_POOL_H
27#define TEMPTABLE_LOCK_FREE_POOL_H
28
29#include <array>
30
32
33namespace temptable {
34
35/** Lock-free pool which consists of POOL_SIZE Lock_free_type elements. It has
36 * all the guarantees and properties of Lock_free_type and its consisting pieces
37 * so for more details please consult the documentation of those. E.g. user code
38 * can opt-in for different alignment-requirements and/or lock-free type
39 * selection algorithms. This type is envisioned to be used as a building block
40 * for higher-abstraction (lock-free) ADTs.
41 * */
42template <typename T, size_t POOL_SIZE,
43 Alignment ALIGNMENT = Alignment::NATURAL,
44 template <typename, typename = void> class TypeSelector =
45 Lock_free_type_selector>
47 std::array<Lock_free_type<T, ALIGNMENT, TypeSelector>, POOL_SIZE> m_lock_free;
48
49 public:
51
52 /** Default constructor. Uses value-initialization to initialize underlying
53 * T's.*/
55
56 /** Constructor. Uses explicit value to initialize underlying T's. */
58 for (auto &v : m_lock_free) v.m_value = default_value;
59 }
60
61 /** Copy-constructor and copy-assignment operator are disabled. */
62 Lock_free_pool(const Lock_free_pool &) = delete;
64
65 /** Ditto for move-constructor and move-assignment operator. */
68
69 /** Atomically replaces current value in an array at given index.
70 *
71 * [in] idx Index of an element whose value is to be replaced.
72 * [in] value Value to store into the atomic variable.
73 * [in] order Memory order constraints to enforce.
74 * */
75 void store(size_t idx, Lock_free_pool::Type value,
76 std::memory_order order = std::memory_order_seq_cst) {
77 m_lock_free[idx].m_value.store(value, order);
78 }
79
80 /** Atomically loads and returns the current value from an array at given
81 * index.
82 *
83 * [in] idx Index of an element whose value is to be returned.
84 * [in] order Memory order constraints to enforce.
85 * @return Returns the current value from given index.
86 * */
88 size_t idx, std::memory_order order = std::memory_order_seq_cst) const {
89 return m_lock_free[idx].m_value.load(order);
90 }
91
92 /** Atomically compares the object representation of an array element at given
93 * index with the expected, and if those are bitwise-equal, replaces the
94 * former with desired.
95 *
96 * [in] idx Index of an element whose value is to be compared against.
97 * [in] expected Value expected to be found in the atomic object. Gets stored
98 * with the actual value of *this if the comparison fails.
99 * [in] desired Value to store in the atomic object if it is as expected.
100 * [in] order Memory order constraints to enforce.
101 * @return True if the underlying atomic value was successfully changed, false
102 * otherwise.
103 * */
105 size_t idx, Lock_free_pool::Type &expected, Lock_free_pool::Type desired,
106 std::memory_order order = std::memory_order_seq_cst) {
107 return m_lock_free[idx].m_value.compare_exchange_strong(expected, desired,
108 order);
109 }
110
111 /** Returns the number of elements this pool contains.
112 *
113 * @return Number of elements in the pool.
114 * */
115 constexpr size_t size() const { return POOL_SIZE; }
116};
117
118} // namespace temptable
119
120#endif /* TEMPTABLE_LOCK_FREE_POOL_H */
Lock-free pool which consists of POOL_SIZE Lock_free_type elements.
Definition: lock_free_pool.h:46
constexpr size_t size() const
Returns the number of elements this pool contains.
Definition: lock_free_pool.h:115
Lock_free_pool(const Lock_free_pool &)=delete
Copy-constructor and copy-assignment operator are disabled.
bool compare_exchange_strong(size_t idx, Lock_free_pool::Type &expected, Lock_free_pool::Type desired, std::memory_order order=std::memory_order_seq_cst)
Atomically compares the object representation of an array element at given index with the expected,...
Definition: lock_free_pool.h:104
Lock_free_pool(Lock_free_pool::Type default_value)
Constructor.
Definition: lock_free_pool.h:57
Lock_free_pool()
Default constructor.
Definition: lock_free_pool.h:54
typename Lock_free_type< T, ALIGNMENT, TypeSelector >::Type Type
Definition: lock_free_pool.h:50
Lock_free_pool::Type load(size_t idx, std::memory_order order=std::memory_order_seq_cst) const
Atomically loads and returns the current value from an array at given index.
Definition: lock_free_pool.h:87
Lock_free_pool(Lock_free_pool &&)=delete
Ditto for move-constructor and move-assignment operator.
Lock_free_pool & operator=(Lock_free_pool &&)=delete
Lock_free_pool & operator=(const Lock_free_pool &)=delete
void store(size_t idx, Lock_free_pool::Type value, std::memory_order order=std::memory_order_seq_cst)
Atomically replaces current value in an array at given index.
Definition: lock_free_pool.h:75
std::array< Lock_free_type< T, ALIGNMENT, TypeSelector >, POOL_SIZE > m_lock_free
Definition: lock_free_pool.h:47
Lock-free type (selection) implementation.
Definition: allocator.h:44
Alignment
Enum class describing alignment-requirements.
Definition: lock_free_type.h:38
typename TypeSelector< T >::Type Type
Definition: lock_free_type.h:268