From cb121be859d29599f8cca97a727eee347fdfd92c Mon Sep 17 00:00:00 2001 From: Daniel Black Date: Sun, 18 Jul 2021 17:03:04 +1000 Subject: WIP: use of omp atomic for statisic_{inc,dec,add,sub} TODO: When parallel threads start to operate on a single per session status variable there is more chance that inaccuracies will occur. With the advent of atomics can maybe we use "SAFE_STATISTICS" by default. The high content global variables are being replaces with more optimium low contention counters. thread_safe_{inc,dec,add,sub} could be removed as they are no longer in use. But is this an API for plugins that needs to be preserved? --- include/my_pthread.h | 32 ++++++++------------------------ 1 file changed, 8 insertions(+), 24 deletions(-) diff --git a/include/my_pthread.h b/include/my_pthread.h index d38d69d04a0..e9a49046a29 100644 --- a/include/my_pthread.h +++ b/include/my_pthread.h @@ -735,30 +735,14 @@ int set_mysys_var(struct st_my_thread_var *mysys_var); */ #ifndef thread_safe_increment -#ifdef _WIN32 -#define thread_safe_increment(V,L) InterlockedIncrement((long*) &(V)) -#define thread_safe_decrement(V,L) InterlockedDecrement((long*) &(V)) -#else #define thread_safe_increment(V,L) \ - (mysql_mutex_lock((L)), (V)++, mysql_mutex_unlock((L))) + std::atomic_fetch_add((long*) &(V), 1) #define thread_safe_decrement(V,L) \ - (mysql_mutex_lock((L)), (V)--, mysql_mutex_unlock((L))) -#endif + std::atomic_fetch_sub((long*) &(V), 1) +#define thread_safe_add(V,C,L) std::atomic_fetch_add((long*) &(V), (C)) +#define thread_safe_sub(V,C,L) std::atomic_fetch_sub((long*) &(V), (C)) #endif -#ifndef thread_safe_add -#ifdef _WIN32 -#define thread_safe_add(V,C,L) InterlockedExchangeAdd((long*) &(V),(C)) -#define thread_safe_sub(V,C,L) InterlockedExchangeAdd((long*) &(V),-(long) (C)) -#else -#define thread_safe_add(V,C,L) \ - (mysql_mutex_lock((L)), (V)+=(C), mysql_mutex_unlock((L))) -#define thread_safe_sub(V,C,L) \ - (mysql_mutex_lock((L)), (V)-=(C), mysql_mutex_unlock((L))) -#endif -#endif - - /* statistics_xxx functions are for non critical statistic, maintained in global variables. @@ -771,10 +755,10 @@ int set_mysys_var(struct st_my_thread_var *mysys_var); - the lock given is not honored. */ #ifdef SAFE_STATISTICS -#define statistic_increment(V,L) thread_safe_increment((V),(L)) -#define statistic_decrement(V,L) thread_safe_decrement((V),(L)) -#define statistic_add(V,C,L) thread_safe_add((V),(C),(L)) -#define statistic_sub(V,C,L) thread_safe_sub((V),(C),(L)) +#define statistic_increment(V,L) _Pragma("omp atomic") (V)++ +#define statistic_decrement(V,L) _Pragma("omp atomic") (V)-- +#define statistic_add(V,C,L) _Pragma("omp atomic") (V)+=(C) +#define statistic_sub(V,C,L) _Pragma("omp atomic") (V)-=(C) #else #define statistic_decrement(V,L) (V)-- #define statistic_increment(V,L) (V)++ -- cgit v1.2.1