Skip to content

Commit

Permalink
Print type name when lock fails EOSIO#10
Browse files Browse the repository at this point in the history
  • Loading branch information
theoreticalbts committed Jan 12, 2017
1 parent ac77ced commit e3ab1aa
Show file tree
Hide file tree
Showing 2 changed files with 23 additions and 22 deletions.
41 changes: 21 additions & 20 deletions include/chainbase/chainbase.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -26,17 +26,18 @@
#include <iostream>
#include <stdexcept>
#include <typeindex>
#include <typeinfo>

#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(t) require_read_lock(typeid(t).name())
#define CHAINBASE_REQUIRE_WRITE_LOCK(t) require_write_lock(typeid(t).name())
#else
#define CHAINBASE_REQUIRE_READ_LOCK()
#define CHAINBASE_REQUIRE_WRITE_LOCK()
#define CHAINBASE_REQUIRE_READ_LOCK(t)
#define CHAINBASE_REQUIRE_WRITE_LOCK(t)
#endif

namespace chainbase {
Expand Down Expand Up @@ -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* lock_type, const char* tname )const;

void require_read_lock()const
void require_read_lock( const char* tname )const
{
if( BOOST_UNLIKELY( _enable_require_locking & _read_only & (_read_lock_count <= 0) ) )
require_lock_fail("read");
require_lock_fail("read", tname);
}

void require_write_lock()
void require_write_lock( const char* tname )
{
if( BOOST_UNLIKELY( _enable_require_locking & (_write_lock_count <= 0) ) )
require_lock_fail("write");
require_lock_fail("write", tname);
}
#endif

Expand Down Expand Up @@ -727,7 +728,7 @@ namespace chainbase {

void set_revision( uint64_t revision )
{
CHAINBASE_REQUIRE_WRITE_LOCK();
CHAINBASE_REQUIRE_WRITE_LOCK( uint64_t );
for( auto i : _index_list ) i->set_revision( revision );
}

Expand Down Expand Up @@ -774,7 +775,7 @@ namespace chainbase {
template<typename MultiIndexType>
const generic_index<MultiIndexType>& get_index()const
{
CHAINBASE_REQUIRE_READ_LOCK();
CHAINBASE_REQUIRE_READ_LOCK(typename MultiIndexType::value_type);
typedef generic_index<MultiIndexType> index_type;
typedef index_type* index_type_ptr;
assert( _index_map.size() > index_type::value_type::type_id );
Expand All @@ -785,7 +786,7 @@ namespace chainbase {
template<typename MultiIndexType, typename ByIndex>
auto get_index()const -> decltype( ((generic_index<MultiIndexType>*)( nullptr ))->indicies().template get<ByIndex>() )
{
CHAINBASE_REQUIRE_READ_LOCK();
CHAINBASE_REQUIRE_READ_LOCK(typename MultiIndexType::value_type);
typedef generic_index<MultiIndexType> index_type;
typedef index_type* index_type_ptr;
assert( _index_map.size() > index_type::value_type::type_id );
Expand All @@ -796,7 +797,7 @@ namespace chainbase {
template<typename MultiIndexType>
generic_index<MultiIndexType>& get_mutable_index()
{
CHAINBASE_REQUIRE_WRITE_LOCK();
CHAINBASE_REQUIRE_WRITE_LOCK(typename MultiIndexType::value_type);
typedef generic_index<MultiIndexType> index_type;
typedef index_type* index_type_ptr;
assert( _index_map.size() > index_type::value_type::type_id );
Expand All @@ -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(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 ) );
Expand All @@ -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(ObjectType);
typedef typename get_index_type< ObjectType >::type index_type;
const auto& idx = get_index< index_type >().indices();
auto itr = idx.find( key );
Expand All @@ -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(ObjectType);
auto obj = find< ObjectType, IndexedByType >( std::forward< CompatibleKey >( key ) );
if( !obj ) BOOST_THROW_EXCEPTION( std::out_of_range( "unknown key" ) );
return *obj;
Expand All @@ -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(ObjectType);
auto obj = find< ObjectType >( key );
if( !obj ) BOOST_THROW_EXCEPTION( std::out_of_range( "unknown key") );
return *obj;
Expand All @@ -847,23 +848,23 @@ namespace chainbase {
template<typename ObjectType, typename Modifier>
void modify( const ObjectType& obj, Modifier&& m )
{
CHAINBASE_REQUIRE_WRITE_LOCK();
CHAINBASE_REQUIRE_WRITE_LOCK(ObjectType);
typedef typename get_index_type<ObjectType>::type index_type;
get_mutable_index<index_type>().modify( obj, m );
}

template<typename ObjectType>
void remove( const ObjectType& obj )
{
CHAINBASE_REQUIRE_WRITE_LOCK();
CHAINBASE_REQUIRE_WRITE_LOCK(ObjectType);
typedef typename get_index_type<ObjectType>::type index_type;
return get_mutable_index<index_type>().remove( obj );
}

template<typename ObjectType, typename Constructor>
const ObjectType& create( Constructor&& con )
{
CHAINBASE_REQUIRE_WRITE_LOCK();
CHAINBASE_REQUIRE_WRITE_LOCK(ObjectType);
typedef typename get_index_type<ObjectType>::type index_type;
return get_mutable_index<index_type>().emplace( std::forward<Constructor>(con) );
}
Expand Down
4 changes: 2 additions & 2 deletions src/chainbase.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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* lock_type, const char* tname )const
{
std::string err_msg = "require_" + std::string( lock_type ) + "_lock() failed";
std::string err_msg = "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 ) );
}
Expand Down

0 comments on commit e3ab1aa

Please sign in to comment.