Commit 1292a01c authored by Marko Mäkelä's avatar Marko Mäkelä

Simplify simple_counter

Introduce a separate simple_atomic_counter
parent 8861d442
...@@ -148,7 +148,7 @@ struct srv_stats_t ...@@ -148,7 +148,7 @@ struct srv_stats_t
ulint_ctr_1_t n_lock_wait_count; ulint_ctr_1_t n_lock_wait_count;
/** Number of threads currently waiting on database locks */ /** Number of threads currently waiting on database locks */
simple_counter<ulint, true> n_lock_wait_current_count; simple_atomic_counter<> n_lock_wait_current_count;
/** Number of rows read. */ /** Number of rows read. */
ulint_ctr_64_t n_rows_read; ulint_ctr_64_t n_rows_read;
......
...@@ -1186,50 +1186,43 @@ static inline void my_atomic_storelint(ulint *A, ulint B) ...@@ -1186,50 +1186,43 @@ static inline void my_atomic_storelint(ulint *A, ulint B)
#endif #endif
} }
/** Simple counter aligned to CACHE_LINE_SIZE /** Simple non-atomic counter aligned to CACHE_LINE_SIZE
@tparam Type the integer type of the counter @tparam Type the integer type of the counter */
@tparam atomic whether to use atomic memory access */ template <typename Type>
template <typename Type = ulint, bool atomic = false>
struct MY_ALIGNED(CPU_LEVEL1_DCACHE_LINESIZE) simple_counter struct MY_ALIGNED(CPU_LEVEL1_DCACHE_LINESIZE) simple_counter
{ {
/** Increment the counter */ /** Increment the counter */
Type inc() { return add(1); } Type inc() { return add(1); }
/** Decrement the counter */ /** Decrement the counter */
Type dec() { return sub(1); } Type dec() { return add(Type(~0)); }
/** Add to the counter /** Add to the counter
@param[in] i amount to be added @param[in] i amount to be added
@return the value of the counter after adding */ @return the value of the counter after adding */
Type add(Type i) Type add(Type i) { return m_counter += i; }
{
compile_time_assert(!atomic || sizeof(Type) == sizeof(lint)); /** @return the value of the counter */
if (atomic) { operator Type() const { return m_counter; }
#ifdef _MSC_VER
// Suppress type conversion/ possible loss of data warning private:
#pragma warning (push) /** The counter */
#pragma warning (disable : 4244) Type m_counter;
#endif };
return Type(my_atomic_addlint(reinterpret_cast<ulint*>
(&m_counter), i)); /** Simple atomic counter aligned to CACHE_LINE_SIZE
#ifdef _MSC_VER @tparam Type lint or ulint */
#pragma warning (pop) template <typename Type = ulint>
#endif struct MY_ALIGNED(CPU_LEVEL1_DCACHE_LINESIZE) simple_atomic_counter
} else { {
return m_counter += i; /** Increment the counter */
} Type inc() { return add(1); }
} /** Decrement the counter */
/** Subtract from the counter Type dec() { return add(Type(~0)); }
@param[in] i amount to be subtracted
/** Add to the counter
@param[in] i amount to be added
@return the value of the counter after adding */ @return the value of the counter after adding */
Type sub(Type i) Type add(Type i) { return my_atomic_addlint(&m_counter, i); }
{
compile_time_assert(!atomic || sizeof(Type) == sizeof(lint));
if (atomic) {
return Type(my_atomic_addlint(&m_counter, -lint(i)));
} else {
return m_counter -= i;
}
}
/** @return the value of the counter (non-atomic access)! */ /** @return the value of the counter (non-atomic access)! */
operator Type() const { return m_counter; } operator Type() const { return m_counter; }
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment