Skip to content

Commit 6a150e2

Browse files
committed
MDEV-17441 - InnoDB transition to C++11 atomics
Almost trivial TTASEventMutex::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.
1 parent 601f45f commit 6a150e2

File tree

1 file changed

+20
-22
lines changed

1 file changed

+20
-22
lines changed

storage/innobase/include/ib0mutex.h

Lines changed: 20 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -380,7 +380,7 @@ struct TTASEventMutex {
380380
~TTASEventMutex()
381381
UNIV_NOTHROW
382382
{
383-
ut_ad(m_lock_word == MUTEX_STATE_UNLOCKED);
383+
ut_ad(state() == MUTEX_STATE_UNLOCKED);
384384
}
385385

386386
/** Called when the mutex is "created". Note: Not from the constructor
@@ -389,7 +389,7 @@ struct TTASEventMutex {
389389
void init(latch_id_t id, const char*, uint32_t) UNIV_NOTHROW
390390
{
391391
ut_a(m_event == 0);
392-
ut_a(m_lock_word == MUTEX_STATE_UNLOCKED);
392+
ut_ad(state() == MUTEX_STATE_UNLOCKED);
393393

394394
m_event = os_event_create(sync_latch_get_name(id));
395395
}
@@ -400,7 +400,7 @@ struct TTASEventMutex {
400400
void destroy()
401401
UNIV_NOTHROW
402402
{
403-
ut_ad(m_lock_word == MUTEX_STATE_UNLOCKED);
403+
ut_ad(state() == MUTEX_STATE_UNLOCKED);
404404

405405
/* We have to free the event before InnoDB shuts down. */
406406
os_event_destroy(m_event);
@@ -412,20 +412,20 @@ struct TTASEventMutex {
412412
bool try_lock()
413413
UNIV_NOTHROW
414414
{
415-
int32 oldval = MUTEX_STATE_UNLOCKED;
416-
return(my_atomic_cas32_strong_explicit(&m_lock_word, &oldval,
417-
MUTEX_STATE_LOCKED,
418-
MY_MEMORY_ORDER_ACQUIRE,
419-
MY_MEMORY_ORDER_RELAXED));
415+
uint32_t oldval = MUTEX_STATE_UNLOCKED;
416+
return m_lock_word.compare_exchange_strong(
417+
oldval,
418+
MUTEX_STATE_LOCKED,
419+
std::memory_order_acquire,
420+
std::memory_order_relaxed);
420421
}
421422

422423
/** Release the mutex. */
423424
void exit()
424425
UNIV_NOTHROW
425426
{
426-
if (my_atomic_fas32_explicit(&m_lock_word,
427-
MUTEX_STATE_UNLOCKED,
428-
MY_MEMORY_ORDER_RELEASE)
427+
if (m_lock_word.exchange(MUTEX_STATE_UNLOCKED,
428+
std::memory_order_release)
429429
== MUTEX_STATE_WAITERS) {
430430
os_event_set(m_event);
431431
sync_array_object_signalled();
@@ -463,11 +463,12 @@ struct TTASEventMutex {
463463
: SYNC_MUTEX,
464464
filename, line, &cell);
465465

466-
int32 oldval = MUTEX_STATE_LOCKED;
467-
my_atomic_cas32_strong_explicit(&m_lock_word, &oldval,
468-
MUTEX_STATE_WAITERS,
469-
MY_MEMORY_ORDER_RELAXED,
470-
MY_MEMORY_ORDER_RELAXED);
466+
uint32_t oldval = MUTEX_STATE_LOCKED;
467+
m_lock_word.compare_exchange_strong(
468+
oldval,
469+
MUTEX_STATE_WAITERS,
470+
std::memory_order_relaxed,
471+
std::memory_order_relaxed);
471472

472473
if (oldval == MUTEX_STATE_UNLOCKED) {
473474
sync_array_free_cell(sync_arr, cell);
@@ -486,9 +487,7 @@ struct TTASEventMutex {
486487
int32 state() const
487488
UNIV_NOTHROW
488489
{
489-
return(my_atomic_load32_explicit(const_cast<int32*>
490-
(&m_lock_word),
491-
MY_MEMORY_ORDER_RELAXED));
490+
return m_lock_word.load(std::memory_order_relaxed);
492491
}
493492

494493
/** The event that the mutex will wait in sync0arr.cc
@@ -518,9 +517,8 @@ struct TTASEventMutex {
518517
TTASEventMutex(const TTASEventMutex&);
519518
TTASEventMutex& operator=(const TTASEventMutex&);
520519

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

525523
/** Used by sync0arr.cc for the wait queue */
526524
os_event_t m_event;

0 commit comments

Comments
 (0)