From 7b31e11a109dd6bcf3de3b9e4785d9aecf9603b8 Mon Sep 17 00:00:00 2001 From: =?utf8?q?Piotr=20Da=C5=82ek?= Date: Wed, 8 Jul 2015 18:52:22 +0200 Subject: [PATCH] bufferlist: replace Mutex with RWlock MIME-Version: 1.0 Content-Type: text/plain; charset=utf8 Content-Transfer-Encoding: 8bit Replace Mutex with RWlock in bufferlist code, so it doesn't block on reads, also make it a tiny bit smarter, so it doesn't block writes on empty crc map clears. Use no-tracking RWlock for better overall performance across entire codebase, and don't use RWLocker helper classes for even larger performance gains. Signed-off-by: Piotr Dałek --- src/common/buffer.cc | 36 +++++++++++++++++++++++------------- 1 file changed, 23 insertions(+), 13 deletions(-) diff --git a/src/common/buffer.cc b/src/common/buffer.cc index c29c81e2913..346c6f51c86 100644 --- a/src/common/buffer.cc +++ b/src/common/buffer.cc @@ -21,7 +21,7 @@ #include "common/strtol.h" #include "common/likely.h" #include "include/atomic.h" -#include "common/Mutex.h" +#include "common/RWLock.h" #include "include/types.h" #include "include/compat.h" #if defined(HAVE_XIO) @@ -128,16 +128,16 @@ static simple_spinlock_t buffer_debug_lock = SIMPLE_SPINLOCK_INITIALIZER; unsigned len; atomic_t nref; - mutable Mutex crc_lock; + mutable RWLock crc_lock; map, pair > crc_map; raw(unsigned l) : data(NULL), len(l), nref(0), - crc_lock("buffer::raw::crc_lock", false, false) + crc_lock("buffer::raw::crc_lock", false) { } raw(char *c, unsigned l) : data(c), len(l), nref(0), - crc_lock("buffer::raw::crc_lock", false, false) + crc_lock("buffer::raw::crc_lock", false) { } virtual ~raw() {} @@ -173,23 +173,33 @@ static simple_spinlock_t buffer_debug_lock = SIMPLE_SPINLOCK_INITIALIZER; return true; } bool get_crc(const pair &fromto, - pair *crc) const { - Mutex::Locker l(crc_lock); + pair *crc) const { + crc_lock.get_read(); map, pair >::const_iterator i = - crc_map.find(fromto); - if (i == crc_map.end()) - return false; + crc_map.find(fromto); + if (i == crc_map.end()) { + crc_lock.unlock(); + return false; + } *crc = i->second; + crc_lock.unlock(); return true; } void set_crc(const pair &fromto, - const pair &crc) { - Mutex::Locker l(crc_lock); + const pair &crc) { + crc_lock.get_write(); crc_map[fromto] = crc; + crc_lock.unlock(); } void invalidate_crc() { - Mutex::Locker l(crc_lock); - crc_map.clear(); + // don't own the write lock when map is empty + crc_lock.get_read(); + if (crc_map.size() != 0) { + crc_lock.unlock(); + crc_lock.get_write(); + crc_map.clear(); + } + crc_lock.unlock(); } }; -- 2.47.3