Skip to content

Commit

Permalink
MDEV-17441 - InnoDB transition to C++11 atomics
Browse files Browse the repository at this point in the history
Almost trivial TTASMutex::m_lock_word transition. Since C++11 doesn't
seem to allow mixed (atomic and non-atomic) access to atomic variables,
we have to perform all accesses atomically.
  • Loading branch information
Sergey Vojtovich committed Dec 27, 2018
1 parent 2e5d359 commit 601f45f
Showing 1 changed file with 18 additions and 14 deletions.
32 changes: 18 additions & 14 deletions storage/innobase/include/ib0mutex.h
Original file line number Diff line number Diff line change
Expand Up @@ -277,40 +277,45 @@ struct TTASMutex {

~TTASMutex()
{
ut_ad(m_lock_word == MUTEX_STATE_UNLOCKED);
ut_ad(m_lock_word.load(std::memory_order_relaxed)
== MUTEX_STATE_UNLOCKED);
}

/** Called when the mutex is "created". Note: Not from the constructor
but when the mutex is initialised. */
void init(latch_id_t) UNIV_NOTHROW
{
ut_ad(m_lock_word == MUTEX_STATE_UNLOCKED);
ut_ad(m_lock_word.load(std::memory_order_relaxed)
== MUTEX_STATE_UNLOCKED);
}

/** Destroy the mutex. */
void destroy() UNIV_NOTHROW
{
/* The destructor can be called at shutdown. */
ut_ad(m_lock_word == MUTEX_STATE_UNLOCKED);
ut_ad(m_lock_word.load(std::memory_order_relaxed)
== MUTEX_STATE_UNLOCKED);
}

/** Try and lock the mutex.
@return true on success */
bool try_lock() UNIV_NOTHROW
{
int32 oldval = MUTEX_STATE_UNLOCKED;
return(my_atomic_cas32_strong_explicit(&m_lock_word, &oldval,
MUTEX_STATE_LOCKED,
MY_MEMORY_ORDER_ACQUIRE,
MY_MEMORY_ORDER_RELAXED));
uint32_t oldval = MUTEX_STATE_UNLOCKED;
return m_lock_word.compare_exchange_strong(
oldval,
MUTEX_STATE_LOCKED,
std::memory_order_acquire,
std::memory_order_relaxed);
}

/** Release the mutex. */
void exit() UNIV_NOTHROW
{
ut_ad(m_lock_word == MUTEX_STATE_LOCKED);
my_atomic_store32_explicit(&m_lock_word, MUTEX_STATE_UNLOCKED,
MY_MEMORY_ORDER_RELEASE);
ut_ad(m_lock_word.load(std::memory_order_relaxed)
== MUTEX_STATE_LOCKED);
m_lock_word.store(MUTEX_STATE_UNLOCKED,
std::memory_order_release);
}

/** Acquire the mutex.
Expand Down Expand Up @@ -353,9 +358,8 @@ struct TTASMutex {
/** Policy data */
MutexPolicy m_policy;

/** lock_word is the target of the atomic test-and-set instruction
when atomic operations are enabled. */
int32 m_lock_word;
/** mutex state */
std::atomic<uint32_t> m_lock_word;
};

template <template <typename> class Policy = NoPolicy>
Expand Down

0 comments on commit 601f45f

Please sign in to comment.