diff --git a/include/chainbase/chainbase.hpp b/include/chainbase/chainbase.hpp index 0693420aeaa..a05747a8068 100644 --- a/include/chainbase/chainbase.hpp +++ b/include/chainbase/chainbase.hpp @@ -26,17 +26,18 @@ #include #include #include +#include #ifndef CHAINBASE_NUM_RW_LOCKS #define CHAINBASE_NUM_RW_LOCKS 10 #endif #ifdef CHAINBASE_CHECK_LOCKING - #define CHAINBASE_REQUIRE_READ_LOCK() require_read_lock() - #define CHAINBASE_REQUIRE_WRITE_LOCK() require_write_lock() + #define CHAINBASE_REQUIRE_READ_LOCK(m, t) require_read_lock(m, typeid(t).name()) + #define CHAINBASE_REQUIRE_WRITE_LOCK(m, t) require_write_lock(m, typeid(t).name()) #else - #define CHAINBASE_REQUIRE_READ_LOCK() - #define CHAINBASE_REQUIRE_WRITE_LOCK() + #define CHAINBASE_REQUIRE_READ_LOCK(m, t) + #define CHAINBASE_REQUIRE_WRITE_LOCK(m, t) #endif namespace chainbase { @@ -656,18 +657,18 @@ namespace chainbase { void set_require_locking( bool enable_require_locking ); #ifdef CHAINBASE_CHECK_LOCKING - void require_lock_fail( const char* lock_type )const; + void require_lock_fail( const char* method, const char* lock_type, const char* tname )const; - void require_read_lock()const + void require_read_lock( const char* method, const char* tname )const { if( BOOST_UNLIKELY( _enable_require_locking & _read_only & (_read_lock_count <= 0) ) ) - require_lock_fail("read"); + require_lock_fail(method, "read", tname); } - void require_write_lock() + void require_write_lock( const char* method, const char* tname ) { if( BOOST_UNLIKELY( _enable_require_locking & (_write_lock_count <= 0) ) ) - require_lock_fail("write"); + require_lock_fail(method, "write", tname); } #endif @@ -727,7 +728,7 @@ namespace chainbase { void set_revision( uint64_t revision ) { - CHAINBASE_REQUIRE_WRITE_LOCK(); + CHAINBASE_REQUIRE_WRITE_LOCK( "set_revision", uint64_t ); for( auto i : _index_list ) i->set_revision( revision ); } @@ -774,7 +775,7 @@ namespace chainbase { template const generic_index& get_index()const { - CHAINBASE_REQUIRE_READ_LOCK(); + CHAINBASE_REQUIRE_READ_LOCK("get_index", typename MultiIndexType::value_type); typedef generic_index index_type; typedef index_type* index_type_ptr; assert( _index_map.size() > index_type::value_type::type_id ); @@ -785,7 +786,7 @@ namespace chainbase { template auto get_index()const -> decltype( ((generic_index*)( nullptr ))->indicies().template get() ) { - CHAINBASE_REQUIRE_READ_LOCK(); + CHAINBASE_REQUIRE_READ_LOCK("get_index", typename MultiIndexType::value_type); typedef generic_index index_type; typedef index_type* index_type_ptr; assert( _index_map.size() > index_type::value_type::type_id ); @@ -796,7 +797,7 @@ namespace chainbase { template generic_index& get_mutable_index() { - CHAINBASE_REQUIRE_WRITE_LOCK(); + CHAINBASE_REQUIRE_WRITE_LOCK("get_mutable_index", typename MultiIndexType::value_type); typedef generic_index index_type; typedef index_type* index_type_ptr; assert( _index_map.size() > index_type::value_type::type_id ); @@ -807,7 +808,7 @@ namespace chainbase { template< typename ObjectType, typename IndexedByType, typename CompatibleKey > const ObjectType* find( CompatibleKey&& key )const { - CHAINBASE_REQUIRE_READ_LOCK(); + CHAINBASE_REQUIRE_READ_LOCK("find", ObjectType); typedef typename get_index_type< ObjectType >::type index_type; const auto& idx = get_index< index_type >().indicies().template get< IndexedByType >(); auto itr = idx.find( std::forward< CompatibleKey >( key ) ); @@ -818,7 +819,7 @@ namespace chainbase { template< typename ObjectType > const ObjectType* find( oid< ObjectType > key = oid< ObjectType >() ) const { - CHAINBASE_REQUIRE_READ_LOCK(); + CHAINBASE_REQUIRE_READ_LOCK("find", ObjectType); typedef typename get_index_type< ObjectType >::type index_type; const auto& idx = get_index< index_type >().indices(); auto itr = idx.find( key ); @@ -829,7 +830,7 @@ namespace chainbase { template< typename ObjectType, typename IndexedByType, typename CompatibleKey > const ObjectType& get( CompatibleKey&& key )const { - CHAINBASE_REQUIRE_READ_LOCK(); + CHAINBASE_REQUIRE_READ_LOCK("get", ObjectType); auto obj = find< ObjectType, IndexedByType >( std::forward< CompatibleKey >( key ) ); if( !obj ) BOOST_THROW_EXCEPTION( std::out_of_range( "unknown key" ) ); return *obj; @@ -838,7 +839,7 @@ namespace chainbase { template< typename ObjectType > const ObjectType& get( const oid< ObjectType >& key = oid< ObjectType >() )const { - CHAINBASE_REQUIRE_READ_LOCK(); + CHAINBASE_REQUIRE_READ_LOCK("get", ObjectType); auto obj = find< ObjectType >( key ); if( !obj ) BOOST_THROW_EXCEPTION( std::out_of_range( "unknown key") ); return *obj; @@ -847,7 +848,7 @@ namespace chainbase { template void modify( const ObjectType& obj, Modifier&& m ) { - CHAINBASE_REQUIRE_WRITE_LOCK(); + CHAINBASE_REQUIRE_WRITE_LOCK("modify", ObjectType); typedef typename get_index_type::type index_type; get_mutable_index().modify( obj, m ); } @@ -855,7 +856,7 @@ namespace chainbase { template void remove( const ObjectType& obj ) { - CHAINBASE_REQUIRE_WRITE_LOCK(); + CHAINBASE_REQUIRE_WRITE_LOCK("remove", ObjectType); typedef typename get_index_type::type index_type; return get_mutable_index().remove( obj ); } @@ -863,7 +864,7 @@ namespace chainbase { template const ObjectType& create( Constructor&& con ) { - CHAINBASE_REQUIRE_WRITE_LOCK(); + CHAINBASE_REQUIRE_WRITE_LOCK("create", ObjectType); typedef typename get_index_type::type index_type; return get_mutable_index().emplace( std::forward(con) ); } diff --git a/src/chainbase.cpp b/src/chainbase.cpp index 49b72bb0323..cf389816c06 100644 --- a/src/chainbase.cpp +++ b/src/chainbase.cpp @@ -138,9 +138,9 @@ namespace chainbase { } #ifdef CHAINBASE_CHECK_LOCKING - void database::require_lock_fail( const char* lock_type )const + void database::require_lock_fail( const char* method, const char* lock_type, const char* tname )const { - std::string err_msg = "require_" + std::string( lock_type ) + "_lock() failed"; + std::string err_msg = "database::" + std::string( method ) + " require_" + std::string( lock_type ) + "_lock() failed on type " + std::string( tname ); std::cerr << err_msg << std::endl; BOOST_THROW_EXCEPTION( std::runtime_error( err_msg ) ); }