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