Skip to content
Permalink
Browse files

Merge pull request #9940 from aclamk/common-recursive-mutex-fix

common: fix lockdep vs recursive mutexes

Reviewed-by: Sage Weil <sage@redhat.com>
Reviewed-by: Kefu Chai <kchai@redhat.com>
  • Loading branch information...
tchaikov committed Mar 23, 2017
2 parents 950abe1 + b08d688 commit 136c28f9dc4dabcbcd61c7de9e7c97cbff233773
Showing with 74 additions and 1 deletion.
  1. +1 −1 src/common/Mutex.cc
  2. +7 −0 src/test/common/CMakeLists.txt
  3. +66 −0 src/test/common/test_mutex.cc
@@ -92,7 +92,7 @@ Mutex::~Mutex() {
void Mutex::Lock(bool no_lockdep) {
int r;

if (lockdep && g_lockdep && !no_lockdep) _will_lock();
if (lockdep && g_lockdep && !no_lockdep && !recursive) _will_lock();

if (logger && cct && cct->_conf->mutex_perf_counter) {
utime_t start;
@@ -202,6 +202,13 @@ add_executable(unittest_mutex_debug
add_ceph_unittest(unittest_mutex_debug ${CMAKE_RUNTIME_OUTPUT_DIRECTORY}/unittest_mutex_debug)
target_link_libraries(unittest_mutex_debug global ${BLKID_LIBRARIES} ${EXTRALIBS})

# unittest_mutex
add_executable(unittest_mutex
test_mutex.cc
)
add_ceph_unittest(unittest_mutex ${CMAKE_RUNTIME_OUTPUT_DIRECTORY}/unittest_mutex)
target_link_libraries(unittest_mutex global ${BLKID_LIBRARIES} ${EXTRALIBS})

# unittest_shunique_lock
add_executable(unittest_shunique_lock
test_shunique_lock.cc
@@ -0,0 +1,66 @@
// -*- mode:C++; tab-width:8; c-basic-offset:2; indent-tabs-mode:t -*-
// vim: ts=8 sw=2 &smarttab
/*
* Ceph - scalable distributed file system
*
*/

#include <common/Mutex.h>
#include "gtest/gtest.h"
#include "common/ceph_context.h"
#include "common/config.h"

/*
* Override normal ceph assert.
* It is needed to prevent hang when we assert() and THEN still wait on lock().
*/
namespace ceph
{
void __ceph_assert_fail(const char *assertion, const char *file, int line,
const char *func)
{
throw 0;
}
}

void do_init() {
static CephContext* cct = nullptr;
if (cct == nullptr) {
cct = new CephContext(0);
lockdep_register_ceph_context(cct);
}
}

TEST(Mutex, NormalAsserts) {
Mutex* m = new Mutex("Normal",false);
m->Lock();
EXPECT_THROW(m->Lock(), int);
}

TEST(Mutex, RecursiveWithLockdep) {
do_init();
g_lockdep = 1;
Mutex* m = new Mutex("Recursive1",true);
m->Lock();
m->Lock();
m->Unlock();
m->Unlock();
delete m;
}

TEST(Mutex, RecursiveWithoutLockdep) {
do_init();
g_lockdep = 0;
Mutex* m = new Mutex("Recursive2",true);
m->Lock();
m->Lock();
m->Unlock();
m->Unlock();
delete m;
}

TEST(Mutex, DeleteLocked) {
Mutex* m = new Mutex("Recursive3",false);
m->Lock();
EXPECT_DEATH(delete m,".*");
}

0 comments on commit 136c28f

Please sign in to comment.
You can’t perform that action at this time.