Skip to content

Commit 55db59f

Browse files
keeperdr-m
authored andcommitted
[MDEV-28162] Replace PFS_atomic with std::atomic<T>
PFS_atomic class contains wrappers around my_atomic_* operations, which are macros to GNU atomic operations (__atomic_*). Due to different implementations of compilers, clang may encounter errors when compiling on x86_32 architecture. The following functions are replaced with C++ std::atomic type in performance schema code base: - PFS_atomic::store_*() -> my_atomic_store* -> __atomic_store_n() => std::atomic<T>::store() - PFS_atomic::load_*() -> my_atomic_load* -> __atomic_load_n() => std::atomic<T>::load() - PFS_atomic::add_*() -> my_atomic_add* -> __atomic_fetch_add() => std::atomic<T>::fetch_add() - PFS_atomic::cas_*() -> my_atomic_cas* -> __atomic_compare_exchange_n() => std::atomic<T>::compare_exchange_strong() and PFS_atomic class could be dropped completely. Note that in the wrapper memory order passed to original GNU atomic extensions are hard-coded as `__ATOMIC_SEQ_CST`, which is equivalent to `std::memory_order_seq_cst` in C++, and is the default parameter for std::atomic_* functions. All new code of the whole pull request, including one or several files that are either new files or modified ones, are contributed under the BSD-new license. I am contributing on behalf of my employer Amazon Web Services.
1 parent d5bad49 commit 55db59f

18 files changed

+109
-248
lines changed

storage/perfschema/CMakeLists.txt

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -49,7 +49,6 @@ cursor_by_thread.h
4949
cursor_by_user.h
5050
pfs.h
5151
pfs_account.h
52-
pfs_atomic.h
5352
pfs_buffer_container.h
5453
pfs_builtin_memory.h
5554
pfs_column_types.h

storage/perfschema/pfs_account.h

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,7 @@
2727
@file storage/perfschema/pfs_account.h
2828
Performance schema account (declarations).
2929
*/
30+
#include <atomic>
3031

3132
#include "pfs_lock.h"
3233
#include "lf.h"
@@ -62,22 +63,22 @@ struct PFS_ALIGNED PFS_account : PFS_connection_slice
6263
public:
6364
inline void init_refcount(void)
6465
{
65-
PFS_atomic::store_32(& m_refcount, 1);
66+
m_refcount.store(1);
6667
}
6768

6869
inline int get_refcount(void)
6970
{
70-
return PFS_atomic::load_32(& m_refcount);
71+
return m_refcount.load();
7172
}
7273

7374
inline void inc_refcount(void)
7475
{
75-
PFS_atomic::add_32(& m_refcount, 1);
76+
m_refcount.fetch_add(1);
7677
}
7778

7879
inline void dec_refcount(void)
7980
{
80-
PFS_atomic::add_32(& m_refcount, -1);
81+
m_refcount.fetch_sub(1);
8182
}
8283

8384
void aggregate(bool alive, PFS_user *safe_user, PFS_host *safe_host);
@@ -109,7 +110,7 @@ struct PFS_ALIGNED PFS_account : PFS_connection_slice
109110
ulonglong m_disconnected_count;
110111

111112
private:
112-
int m_refcount;
113+
std::atomic<int> m_refcount;
113114
};
114115

115116
int init_account(const PFS_global_param *param);

storage/perfschema/pfs_atomic.h

Lines changed: 0 additions & 141 deletions
This file was deleted.

storage/perfschema/pfs_buffer_container.h

Lines changed: 8 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -87,7 +87,7 @@ class PFS_buffer_default_array
8787
if (m_full)
8888
return NULL;
8989

90-
monotonic= PFS_atomic::add_u32(& m_monotonic.m_u32, 1);
90+
monotonic= m_monotonic.m_u32.fetch_add(1);
9191
monotonic_max= monotonic + static_cast<uint>(m_max);
9292

9393
while (monotonic < monotonic_max)
@@ -99,7 +99,8 @@ class PFS_buffer_default_array
9999
{
100100
return pfs;
101101
}
102-
monotonic= PFS_atomic::add_u32(& m_monotonic.m_u32, 1);
102+
monotonic= m_monotonic.m_u32.fetch_add(1);
103+
103104
}
104105

105106
m_full= true;
@@ -517,7 +518,7 @@ class PFS_buffer_scalable_container
517518

518519
ulong get_row_count()
519520
{
520-
ulong page_count= PFS_atomic::load_u32(& m_max_page_index.m_u32);
521+
ulong page_count= m_max_page_index.m_u32.load();
521522

522523
return page_count * PFS_PAGE_SIZE;
523524
}
@@ -554,11 +555,11 @@ class PFS_buffer_scalable_container
554555
/*
555556
1: Try to find an available record within the existing pages
556557
*/
557-
current_page_count= PFS_atomic::load_u32(& m_max_page_index.m_u32);
558+
current_page_count= m_max_page_index.m_u32.load();
558559

559560
if (current_page_count != 0)
560561
{
561-
monotonic= PFS_atomic::load_u32(& m_monotonic.m_u32);
562+
monotonic= m_monotonic.m_u32.load();
562563
monotonic_max= monotonic + current_page_count;
563564

564565
while (monotonic < monotonic_max)
@@ -602,7 +603,7 @@ class PFS_buffer_scalable_container
602603
counter faster and then move on to the detection of new pages,
603604
in part 2: below.
604605
*/
605-
monotonic= PFS_atomic::add_u32(& m_monotonic.m_u32, 1);
606+
monotonic= m_monotonic.m_u32.fetch_add(1);
606607
};
607608
}
608609

@@ -683,7 +684,7 @@ class PFS_buffer_scalable_container
683684
my_atomic_storeptr(typed_addr, ptr);
684685

685686
/* Advertise the new page */
686-
PFS_atomic::add_u32(& m_max_page_index.m_u32, 1);
687+
m_max_page_index.m_u32.fetch_add(1);
687688
}
688689

689690
pthread_mutex_unlock(& m_critical_section);

storage/perfschema/pfs_digest.cc

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -75,7 +75,7 @@ int init_digest(const PFS_global_param *param)
7575
*/
7676
digest_max= param->m_digest_sizing;
7777
digest_lost= 0;
78-
PFS_atomic::store_u32(& digest_monotonic_index.m_u32, 1);
78+
digest_monotonic_index.m_u32.store(1);
7979
digest_full= false;
8080

8181
if (digest_max == 0)
@@ -274,7 +274,7 @@ find_or_create_digest(PFS_thread *thread,
274274

275275
while (++attempts <= digest_max)
276276
{
277-
safe_index= PFS_atomic::add_u32(& digest_monotonic_index.m_u32, 1) % digest_max;
277+
safe_index= digest_monotonic_index.m_u32.fetch_add(1) % digest_max;
278278
if (safe_index == 0)
279279
{
280280
/* Record [0] is reserved. */
@@ -406,7 +406,7 @@ void reset_esms_by_digest()
406406
Reset index which indicates where the next calculated digest information
407407
to be inserted in statements_digest_stat_array.
408408
*/
409-
PFS_atomic::store_u32(& digest_monotonic_index.m_u32, 1);
409+
digest_monotonic_index.m_u32.store(1);
410410
digest_full= false;
411411
}
412412

storage/perfschema/pfs_events_stages.cc

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -34,7 +34,6 @@
3434
#include "pfs_host.h"
3535
#include "pfs_user.h"
3636
#include "pfs_events_stages.h"
37-
#include "pfs_atomic.h"
3837
#include "pfs_buffer_container.h"
3938
#include "pfs_builtin_memory.h"
4039
#include "m_string.h"
@@ -62,7 +61,7 @@ int init_events_stages_history_long(uint events_stages_history_long_sizing)
6261
{
6362
events_stages_history_long_size= events_stages_history_long_sizing;
6463
events_stages_history_long_full= false;
65-
PFS_atomic::store_u32(&events_stages_history_long_index.m_u32, 0);
64+
events_stages_history_long_index.m_u32.store(0);
6665

6766
if (events_stages_history_long_size == 0)
6867
return 0;
@@ -135,7 +134,7 @@ void insert_events_stages_history_long(PFS_events_stages *stage)
135134

136135
assert(events_stages_history_long_array != NULL);
137136

138-
uint index= PFS_atomic::add_u32(&events_stages_history_long_index.m_u32, 1);
137+
uint index= events_stages_history_long_index.m_u32.fetch_add(1);
139138

140139
index= index % events_stages_history_long_size;
141140
if (index == 0)
@@ -176,7 +175,7 @@ void reset_events_stages_history(void)
176175
/** Reset table EVENTS_STAGES_HISTORY_LONG data. */
177176
void reset_events_stages_history_long(void)
178177
{
179-
PFS_atomic::store_u32(&events_stages_history_long_index.m_u32, 0);
178+
events_stages_history_long_index.m_u32.store(0);
180179
events_stages_history_long_full= false;
181180

182181
PFS_events_stages *pfs= events_stages_history_long_array;

storage/perfschema/pfs_events_statements.cc

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -34,7 +34,6 @@
3434
#include "pfs_host.h"
3535
#include "pfs_user.h"
3636
#include "pfs_events_statements.h"
37-
#include "pfs_atomic.h"
3837
#include "pfs_buffer_container.h"
3938
#include "pfs_builtin_memory.h"
4039
#include "m_string.h"
@@ -64,7 +63,7 @@ int init_events_statements_history_long(size_t events_statements_history_long_si
6463
{
6564
events_statements_history_long_size= events_statements_history_long_sizing;
6665
events_statements_history_long_full= false;
67-
PFS_atomic::store_u32(&events_statements_history_long_index.m_u32, 0);
66+
events_statements_history_long_index.m_u32.store(0);
6867

6968
if (events_statements_history_long_size == 0)
7069
return 0;
@@ -213,7 +212,7 @@ void insert_events_statements_history_long(PFS_events_statements *statement)
213212

214213
assert(events_statements_history_long_array != NULL);
215214

216-
uint index= PFS_atomic::add_u32(&events_statements_history_long_index.m_u32, 1);
215+
uint index= events_statements_history_long_index.m_u32.fetch_add(1);
217216

218217
index= index % events_statements_history_long_size;
219218
if (index == 0)
@@ -258,7 +257,7 @@ void reset_events_statements_history(void)
258257
/** Reset table EVENTS_STATEMENTS_HISTORY_LONG data. */
259258
void reset_events_statements_history_long(void)
260259
{
261-
PFS_atomic::store_u32(&events_statements_history_long_index.m_u32, 0);
260+
events_statements_history_long_index.m_u32.store(0);
262261
events_statements_history_long_full= false;
263262

264263
PFS_events_statements *pfs= events_statements_history_long_array;

storage/perfschema/pfs_events_transactions.cc

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -34,7 +34,6 @@
3434
#include "pfs_host.h"
3535
#include "pfs_user.h"
3636
#include "pfs_events_transactions.h"
37-
#include "pfs_atomic.h"
3837
#include "pfs_buffer_container.h"
3938
#include "pfs_builtin_memory.h"
4039
#include "m_string.h"
@@ -62,7 +61,7 @@ int init_events_transactions_history_long(uint events_transactions_history_long_
6261
{
6362
events_transactions_history_long_size= events_transactions_history_long_sizing;
6463
events_transactions_history_long_full= false;
65-
PFS_atomic::store_u32(&events_transactions_history_long_index.m_u32, 0);
64+
events_transactions_history_long_index.m_u32.store(0);
6665

6766
if (events_transactions_history_long_size == 0)
6867
return 0;
@@ -135,7 +134,7 @@ void insert_events_transactions_history_long(PFS_events_transactions *transactio
135134

136135
assert(events_transactions_history_long_array != NULL);
137136

138-
uint index= PFS_atomic::add_u32(&events_transactions_history_long_index.m_u32, 1);
137+
uint index= events_transactions_history_long_index.m_u32.fetch_add(1);
139138

140139
index= index % events_transactions_history_long_size;
141140
if (index == 0)
@@ -176,7 +175,7 @@ void reset_events_transactions_history(void)
176175
/** Reset table EVENTS_TRANSACTIONS_HISTORY_LONG data. */
177176
void reset_events_transactions_history_long(void)
178177
{
179-
PFS_atomic::store_u32(&events_transactions_history_long_index.m_u32, 0);
178+
events_transactions_history_long_index.m_u32.store(0);
180179
events_transactions_history_long_full= false;
181180

182181
PFS_events_transactions *pfs= events_transactions_history_long_array;

0 commit comments

Comments
 (0)