Commit f18c297a authored by Marko Mäkelä's avatar Marko Mäkelä

Default to template<spinloop=false>

It is easy to grep "lock<true" to find spin-looped acquisitions.
Note that for operations that are not perceived time critical,
we can disable spinloops even for other acquisitions of the same latch
we can enable spinloops.
parent 217826b7
......@@ -5890,7 +5890,7 @@ static bool innobase_instant_try(
/* Acquire the ahi latch to avoid a race condition
between ahi access and instant alter table */
srw_lock* ahi_latch = btr_search_sys.get_latch(*index);
ahi_latch->wr_lock<false>(SRW_LOCK_CALL);
ahi_latch->wr_lock(SRW_LOCK_CALL);
#endif /* BTR_CUR_HASH_ADAPT */
const bool metadata_changed = ctx->instant_column();
#ifdef BTR_CUR_HASH_ADAPT
......
......@@ -366,7 +366,7 @@ TRANSACTIONAL_INLINE inline ulint dict_index_t::n_ahi_pages() const
return ref_count;
}
#endif
latch->rd_lock<true>(SRW_LOCK_CALL);
latch->rd_lock(SRW_LOCK_CALL);
ulint ref_count= search_info->ref_count;
latch->rd_unlock();
return ref_count;
......
......@@ -87,7 +87,7 @@ btr_search_info_update(
static inline void btr_search_x_lock_all()
{
for (ulint i = 0; i < btr_ahi_parts; ++i) {
btr_search_sys.parts[i].latch.wr_lock<false>(SRW_LOCK_CALL);
btr_search_sys.parts[i].latch.wr_lock(SRW_LOCK_CALL);
}
}
......@@ -103,7 +103,7 @@ static inline void btr_search_x_unlock_all()
static inline void btr_search_s_lock_all()
{
for (ulint i = 0; i < btr_ahi_parts; ++i) {
btr_search_sys.parts[i].latch.rd_lock<false>(SRW_LOCK_CALL);
btr_search_sys.parts[i].latch.rd_lock(SRW_LOCK_CALL);
}
}
......
......@@ -182,14 +182,14 @@ class ReadView: public ReadViewBase
*/
trx_id_t m_creator_trx_id;
void lock() const { m_mutex.wr_lock<false>(); }
void lock() const { m_mutex.wr_lock(); }
void unlock() const { m_mutex.wr_unlock(); }
public:
ReadView()
{
memset(reinterpret_cast<void*>(this), 0, sizeof *this);
m_mutex.init<false>();
m_mutex.init();
}
~ReadView() { m_mutex.destroy(); }
......
......@@ -49,23 +49,21 @@ class pthread_mutex_wrapper final
}
void destroy() { pthread_mutex_destroy(&lock); }
# ifdef PTHREAD_ADAPTIVE_MUTEX_INITIALIZER_NP
template<bool spinloop> void wr_lock() { pthread_mutex_lock(&lock); }
template<bool spinloop=false> void wr_lock() { pthread_mutex_lock(&lock); }
# else
private:
void wr_wait();
public:
template<bool spinloop> inline void wr_lock();
template<bool spinloop=false> inline void wr_lock();
# endif
void wr_unlock() { pthread_mutex_unlock(&lock); }
bool wr_lock_try() { return !pthread_mutex_trylock(&lock); }
};
# ifndef PTHREAD_ADAPTIVE_MUTEX_INITIALIZER_NP
template<>
inline void pthread_mutex_wrapper::wr_lock<false>()
template<> inline void pthread_mutex_wrapper::wr_lock<false>()
{ pthread_mutex_lock(&lock); }
template<>
inline void pthread_mutex_wrapper::wr_lock<true>()
template<> inline void pthread_mutex_wrapper::wr_lock<true>()
{ if (!wr_lock_try()) wr_wait(); }
# endif
#endif
......@@ -134,7 +132,7 @@ class srw_mutex_impl final
std::memory_order_relaxed);
}
template<bool spinloop>
template<bool spinloop=false>
void wr_lock() { if (!wr_lock_try()) wait_and_lock<spinloop>(); }
void wr_unlock()
{
......@@ -246,7 +244,7 @@ class ssux_lock_impl
return false;
}
template<bool spinloop> void rd_lock()
template<bool spinloop=false> void rd_lock()
{ if (!rd_lock_try()) rd_wait<spinloop>(); }
template<bool spinloop> void u_lock() { writer.wr_lock<spinloop>(); }
template<bool spin_writer,bool spin_readers> void wr_lock()
......@@ -265,7 +263,8 @@ class ssux_lock_impl
wr_wait<spin_readers>(lk);
#endif
}
template<bool spinloop> void wr_lock() { wr_lock<spinloop,spinloop>(); }
template<bool spinloop=false> void wr_lock()
{ wr_lock<spinloop,spinloop>(); }
template<bool spinloop> void u_wr_upgrade()
{
......@@ -309,9 +308,9 @@ class ssux_lock_impl
bool is_locked_or_waiting() const noexcept
{ return is_locked() || writer.is_locked_or_waiting(); }
template<bool spinloop> void lock_shared() { rd_lock<spinloop>(); }
template<bool spinloop=false> void lock_shared() { rd_lock<spinloop>(); }
void unlock_shared() { rd_unlock(); }
template<bool spin,bool loop> void lock() { wr_lock<spin,loop>(); }
template<bool spinloop=false> void lock() { wr_lock<spinloop>(); }
void unlock() { wr_unlock(); }
};
......@@ -333,8 +332,8 @@ class srw_lock_
public:
void init() { IF_WIN(,my_rwlock_init(&lk, nullptr)); }
void destroy() { IF_WIN(,rwlock_destroy(&lk)); }
template<bool spinloop> inline void rd_lock();
template<bool spinloop> inline void wr_lock();
template<bool spinloop=false> inline void rd_lock();
template<bool spinloop=false> inline void wr_lock();
bool rd_lock_try()
{ return IF_WIN(TryAcquireSRWLockShared(&lk), !rw_tryrdlock(&lk)); }
void rd_unlock()
......@@ -355,18 +354,16 @@ class srw_lock_
return is_locked();
}
template<bool spinloop> void lock_shared() { rd_lock<spinloop>(); }
template<bool spinloop=false> void lock_shared() { rd_lock<spinloop>(); }
void unlock_shared() { rd_unlock(); }
template<bool spinloop> void lock() { wr_lock<spinloop>(); }
template<bool spinloop=false> void lock() { wr_lock<spinloop>(); }
void unlock() { wr_unlock(); }
#endif
};
template<>
inline void srw_lock_::rd_lock<false>()
template<> inline void srw_lock_::rd_lock<false>()
{ IF_WIN(AcquireSRWLockShared(&lk), rw_rdlock(&lk)); }
template<>
inline void srw_lock_::wr_lock<false>()
template<> inline void srw_lock_::wr_lock<false>()
{ IF_WIN(AcquireSRWLockExclusive(&lk), rw_wrlock(&lk)); }
template<>
......@@ -503,7 +500,7 @@ class srw_lock_psi
}
lock.destroy();
}
template<bool spinloop>
template<bool spinloop=false>
void rd_lock(const char *file, unsigned line)
{
if (psi_likely(pfs_psi != nullptr))
......@@ -517,7 +514,7 @@ class srw_lock_psi
PSI_RWLOCK_CALL(unlock_rwlock)(pfs_psi);
lock.rd_unlock();
}
template<bool spinloop>
template<bool spinloop=false>
void wr_lock(const char *file, unsigned line)
{
if (psi_likely(pfs_psi != nullptr))
......@@ -533,7 +530,8 @@ class srw_lock_psi
}
bool rd_lock_try() { return lock.rd_lock_try(); }
bool wr_lock_try() { return lock.wr_lock_try(); }
void lock_shared() { return rd_lock<true>(SRW_LOCK_CALL); }
template<bool spinloop>
void lock_shared() { return rd_lock<spinloop>(SRW_LOCK_CALL); }
void unlock_shared() { return rd_unlock(); }
#ifndef SUX_LOCK_GENERIC
/** @return whether any lock may be held by any thread */
......
......@@ -136,7 +136,7 @@ class sux_lock final
void s_lock_register()
{
const pthread_t id= pthread_self();
readers_lock.wr_lock<false>();
readers_lock.wr_lock();
auto r= readers.load(std::memory_order_relaxed);
if (!r)
{
......@@ -173,7 +173,7 @@ class sux_lock final
{
if (auto r= readers.load(std::memory_order_relaxed))
{
readers_lock.wr_lock<false>();
readers_lock.wr_lock();
bool found= r->find(pthread_self()) != r->end();
readers_lock.wr_unlock();
return found;
......@@ -235,7 +235,7 @@ class sux_lock final
const pthread_t id= pthread_self();
auto r= readers.load(std::memory_order_relaxed);
ut_ad(r);
readers_lock.wr_lock<false>();
readers_lock.wr_lock();
auto i= r->find(id);
ut_ad(i != r->end());
r->erase(i);
......@@ -294,7 +294,7 @@ template<> inline void sux_lock<ssux_lock_impl>::init()
lock.init();
ut_ad(!writer.load(std::memory_order_relaxed));
ut_ad(!recursive);
ut_d(readers_lock.init<false>());
ut_d(readers_lock.init());
#ifdef UNIV_DEBUG
if (auto r= readers.load(std::memory_order_relaxed))
ut_ad(r->empty());
......
......@@ -333,13 +333,13 @@ struct rw_trx_hash_element_t
rw_trx_hash_element_t()
{
memset(reinterpret_cast<void*>(this), 0, sizeof *this);
mutex.init<false>();
mutex.init();
}
~rw_trx_hash_element_t() { mutex.destroy(); }
void mutex_lock() { mutex.wr_lock<false>(); }
void mutex_lock() { mutex.wr_lock(); }
void mutex_unlock() { mutex.wr_unlock(); }
trx_id_t id; /* lf_hash_init() relies on this to be first in the struct */
......
......@@ -810,7 +810,7 @@ srv_printf_innodb_monitor(
#ifdef BTR_CUR_HASH_ADAPT
for (ulint i = 0; i < btr_ahi_parts && btr_search_enabled; ++i) {
const auto part= &btr_search_sys.parts[i];
part->latch.rd_lock<false>(SRW_LOCK_CALL);
part->latch.rd_lock(SRW_LOCK_CALL);
ut_ad(part->heap->type == MEM_HEAP_FOR_BTR_SEARCH);
fprintf(file, "Hash table size " ULINTPF
", node heap has " ULINTPF " buffer(s)\n",
......@@ -947,7 +947,7 @@ srv_export_innodb_status(void)
ulint mem_adaptive_hash = 0;
for (ulong i = 0; i < btr_ahi_parts; i++) {
const auto part= &btr_search_sys.parts[i];
part->latch.rd_lock<false>(SRW_LOCK_CALL);
part->latch.rd_lock(SRW_LOCK_CALL);
if (part->heap) {
ut_ad(part->heap->type == MEM_HEAP_FOR_BTR_SEARCH);
......@@ -1265,7 +1265,7 @@ static void purge_worker_callback(void*);
static void purge_coordinator_callback(void*);
static void purge_truncation_callback(void*)
{
purge_sys.latch.rd_lock<false>(SRW_LOCK_CALL);
purge_sys.latch.rd_lock(SRW_LOCK_CALL);
const purge_sys_t::iterator head= purge_sys.head;
purge_sys.latch.rd_unlock();
head.free_history();
......@@ -1298,7 +1298,7 @@ bool purge_sys_t::running()
void purge_sys_t::stop_FTS()
{
latch.rd_lock<false>(SRW_LOCK_CALL);
latch.rd_lock(SRW_LOCK_CALL);
m_FTS_paused++;
latch.rd_unlock();
while (m_active)
......@@ -1308,7 +1308,7 @@ void purge_sys_t::stop_FTS()
/** Stop purge during FLUSH TABLES FOR EXPORT */
void purge_sys_t::stop()
{
latch.wr_lock<false>(SRW_LOCK_CALL);
latch.wr_lock(SRW_LOCK_CALL);
if (!enabled())
{
......@@ -1351,7 +1351,7 @@ void purge_sys_t::resume()
ut_ad(!srv_read_only_mode);
ut_ad(srv_force_recovery < SRV_FORCE_NO_BACKGROUND);
purge_coordinator_task.enable();
latch.wr_lock<false>(SRW_LOCK_CALL);
latch.wr_lock(SRW_LOCK_CALL);
int32_t paused= m_paused--;
ut_a(paused);
......
......@@ -659,7 +659,7 @@ bool srw_lock_debug::wr_lock_try()
void srw_lock_debug::wr_lock(SRW_LOCK_ARGS(const char *file, unsigned line))
{
ut_ad(!have_any());
srw_lock::wr_lock<false>(SRW_LOCK_ARGS(file, line));
srw_lock::wr_lock(SRW_LOCK_ARGS(file, line));
ut_ad(!writer);
writer.store(pthread_self(), std::memory_order_relaxed);
}
......@@ -673,7 +673,7 @@ void srw_lock_debug::wr_unlock()
void srw_lock_debug::readers_register()
{
readers_lock.wr_lock<false>();
readers_lock.wr_lock();
auto r= readers.load(std::memory_order_relaxed);
if (!r)
{
......@@ -696,7 +696,7 @@ bool srw_lock_debug::rd_lock_try()
void srw_lock_debug::rd_lock(SRW_LOCK_ARGS(const char *file, unsigned line))
{
ut_ad(!have_any());
srw_lock::rd_lock<false>(SRW_LOCK_ARGS(file, line));
srw_lock::rd_lock(SRW_LOCK_ARGS(file, line));
readers_register();
}
......@@ -704,7 +704,7 @@ void srw_lock_debug::rd_unlock()
{
const pthread_t self= pthread_self();
ut_ad(writer != self);
readers_lock.wr_lock<false>();
readers_lock.wr_lock();
auto r= readers.load(std::memory_order_relaxed);
ut_ad(r);
auto i= r->find(self);
......@@ -719,7 +719,7 @@ bool srw_lock_debug::have_rd() const noexcept
{
if (auto r= readers.load(std::memory_order_relaxed))
{
readers_lock.wr_lock<false>();
readers_lock.wr_lock();
bool found= r->find(pthread_self()) != r->end();
readers_lock.wr_unlock();
# ifndef SUX_LOCK_GENERIC
......
......@@ -1317,7 +1317,7 @@ trx_i_s_cache_start_read(
/*=====================*/
trx_i_s_cache_t* cache) /*!< in: cache */
{
cache->rw_lock.rd_lock<false>(SRW_LOCK_CALL);
cache->rw_lock.rd_lock(SRW_LOCK_CALL);
}
/*******************************************************************//**
......@@ -1338,7 +1338,7 @@ trx_i_s_cache_start_write(
/*======================*/
trx_i_s_cache_t* cache) /*!< in: cache */
{
cache->rw_lock.wr_lock<false>(SRW_LOCK_CALL);
cache->rw_lock.wr_lock(SRW_LOCK_CALL);
}
/*******************************************************************//**
......
......@@ -1067,7 +1067,7 @@ void purge_sys_t::wait_FTS(bool also_sys)
{
for (;;)
{
latch.wr_lock<false>(SRW_LOCK_CALL);
latch.wr_lock(SRW_LOCK_CALL);
bool paused= m_FTS_paused || (also_sys && m_SYS_paused);
latch.wr_unlock();
if (!paused)
......@@ -1385,7 +1385,7 @@ void purge_sys_t::batch_cleanup(const purge_sys_t::iterator &head)
else
#endif
{
end_latch.wr_lock<false>();
end_latch.wr_lock();
this->head= head;
end_view= view;
end_view.clamp_low_limit_id(trx_no);
......
......@@ -33,7 +33,7 @@ static void test_srw_mutex()
{
for (auto i= N_ROUNDS * M_ROUNDS; i--; )
{
m.wr_lock<false>();
m.wr_lock();
assert(!critical);
critical= true;
critical= false;
......@@ -47,7 +47,7 @@ static void test_srw_lock()
{
for (auto i= N_ROUNDS; i--; )
{
l.wr_lock<false>();
l.wr_lock();
assert(!critical);
critical= true;
critical= false;
......@@ -55,7 +55,7 @@ static void test_srw_lock()
for (auto j= M_ROUNDS; j--; )
{
l.rd_lock<false>();
l.rd_lock();
assert(!critical);
l.rd_unlock();
}
......
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