space map disk
This commit is contained in:
64
block.tcc
64
block.tcc
@@ -6,7 +6,6 @@
|
||||
#include <unistd.h>
|
||||
|
||||
#include <boost/bind.hpp>
|
||||
#include <iostream>
|
||||
#include <stdexcept>
|
||||
|
||||
using namespace boost;
|
||||
@@ -76,10 +75,11 @@ block_manager<BlockSize>::read_lock(block_address location) const
|
||||
|
||||
buffer buf;
|
||||
read_buffer(location, buf);
|
||||
|
||||
register_lock(location, READ_LOCK);
|
||||
return read_ref(
|
||||
typename block::ptr(
|
||||
new block(location, buf, lock_count_, ordinary_count_)));
|
||||
new block(location, buf, lock_count_, ordinary_count_),
|
||||
bind(&block_manager::read_release, this, _1)));
|
||||
}
|
||||
|
||||
template <uint32_t BlockSize>
|
||||
@@ -97,6 +97,7 @@ block_manager<BlockSize>::write_lock(block_address location)
|
||||
|
||||
buffer buf;
|
||||
read_buffer(location, buf);
|
||||
register_lock(location, WRITE_LOCK);
|
||||
return write_ref(
|
||||
typename block::ptr(
|
||||
new block(location, buf, lock_count_, ordinary_count_),
|
||||
@@ -113,6 +114,7 @@ block_manager<BlockSize>::write_lock_zero(block_address location)
|
||||
zero_buffer(buf);
|
||||
typename block::ptr b(new block(location, buf, lock_count_, ordinary_count_),
|
||||
bind(&block_manager<BlockSize>::write_release, this, _1));
|
||||
register_lock(location, WRITE_LOCK);
|
||||
return write_ref(b);
|
||||
}
|
||||
|
||||
@@ -125,7 +127,9 @@ block_manager<BlockSize>::read_lock(block_address location,
|
||||
|
||||
buffer buf;
|
||||
read_buffer(location, buf);
|
||||
typename block::ptr b(new block(location, buf, lock_count_, ordinary_count_, false, v));
|
||||
typename block::ptr b(new block(location, buf, lock_count_, ordinary_count_, false, v),
|
||||
bind(&block_manager::read_release, this, _1));
|
||||
register_lock(location, READ_LOCK);
|
||||
return read_ref(b);
|
||||
}
|
||||
|
||||
@@ -148,6 +152,7 @@ block_manager<BlockSize>::write_lock(block_address location,
|
||||
read_buffer(location, buf);
|
||||
typename block::ptr b(new block(location, buf, lock_count_, ordinary_count_, false, v),
|
||||
bind(&block_manager::write_release, this, _1));
|
||||
register_lock(location, WRITE_LOCK);
|
||||
return write_ref(b);
|
||||
}
|
||||
|
||||
@@ -162,6 +167,7 @@ block_manager<BlockSize>::write_lock_zero(block_address location,
|
||||
zero_buffer(buf);
|
||||
typename block::ptr b(new block(location, buf, lock_count_, ordinary_count_, false, v),
|
||||
bind(&block_manager::write_release, this, _1));
|
||||
register_lock(location, WRITE_LOCK);
|
||||
return write_ref(b);
|
||||
}
|
||||
|
||||
@@ -178,6 +184,7 @@ block_manager<BlockSize>::superblock(block_address location)
|
||||
read_buffer(location, buf);
|
||||
typename block::ptr b(new block(location, buf, lock_count_, superblock_count_, true),
|
||||
bind(&block_manager::write_release, this, _1));
|
||||
register_lock(location, WRITE_LOCK);
|
||||
return write_ref(b);
|
||||
}
|
||||
|
||||
@@ -194,6 +201,7 @@ block_manager<BlockSize>::superblock_zero(block_address location)
|
||||
zero_buffer(buf);
|
||||
typename block::ptr b(new block(location, buf, lock_count_, superblock_count_, true),
|
||||
bind(&block_manager::write_release, this, _1));
|
||||
register_lock(location, WRITE_LOCK);
|
||||
return write_ref(b);
|
||||
}
|
||||
|
||||
@@ -211,6 +219,7 @@ block_manager<BlockSize>::superblock(block_address location,
|
||||
read_buffer(location, buf);
|
||||
typename block::ptr b(new block(location, buf, lock_count_, superblock_count_, true, v),
|
||||
bind(&block_manager::write_release, this, _1));
|
||||
register_lock(location, WRITE_LOCK);
|
||||
return write_ref(b);
|
||||
}
|
||||
|
||||
@@ -228,6 +237,7 @@ block_manager<BlockSize>::superblock_zero(block_address location,
|
||||
zero_buffer(buf);
|
||||
typename block::ptr b(new block(location, buf, lock_count_, superblock_count_, true, v),
|
||||
bind(&block_manager::write_release, this, _1));
|
||||
register_lock(location, WRITE_LOCK);
|
||||
return write_ref(b);
|
||||
}
|
||||
|
||||
@@ -292,7 +302,7 @@ template <uint32_t BlockSize>
|
||||
void
|
||||
block_manager<BlockSize>::zero_buffer(block_manager<BlockSize>::buffer &buffer) const
|
||||
{
|
||||
memset(buffer, 0, BlockSize);
|
||||
::memset(buffer, 0, BlockSize);
|
||||
}
|
||||
|
||||
// FIXME: we don't need this anymore
|
||||
@@ -300,6 +310,7 @@ template <uint32_t BlockSize>
|
||||
void
|
||||
block_manager<BlockSize>::read_release(block *b) const
|
||||
{
|
||||
unregister_lock(b->location_, READ_LOCK);
|
||||
delete b;
|
||||
}
|
||||
|
||||
@@ -316,6 +327,7 @@ block_manager<BlockSize>::write_release(block *b)
|
||||
(*b->validator_)->prepare(*b);
|
||||
|
||||
write_buffer(b->location_, b->data_);
|
||||
unregister_lock(b->location_, WRITE_LOCK);
|
||||
delete b;
|
||||
}
|
||||
|
||||
@@ -327,4 +339,46 @@ block_manager<BlockSize>::check(block_address b) const
|
||||
throw std::runtime_error("block address out of bounds");
|
||||
}
|
||||
|
||||
template <uint32_t BlockSize>
|
||||
block_address
|
||||
block_manager<BlockSize>::get_nr_blocks() const
|
||||
{
|
||||
return nr_blocks_;
|
||||
}
|
||||
|
||||
// FIXME: how do we unregister if block construction throws?
|
||||
template <uint32_t BlockSize>
|
||||
void
|
||||
block_manager<BlockSize>::register_lock(block_address b, lock_type t) const
|
||||
{
|
||||
auto it = held_locks_.find(b);
|
||||
if (it == held_locks_.end())
|
||||
held_locks_.insert(make_pair(b, make_pair(t, 1)));
|
||||
else {
|
||||
if (it->second.first != t)
|
||||
throw std::runtime_error("lock type mismatch when locking");
|
||||
|
||||
if (it->second.first == WRITE_LOCK)
|
||||
throw std::runtime_error("cannot hold concurrent write locks");
|
||||
|
||||
it->second.second++;
|
||||
}
|
||||
}
|
||||
|
||||
template <uint32_t BlockSize>
|
||||
void
|
||||
block_manager<BlockSize>::unregister_lock(block_address b, lock_type t) const
|
||||
{
|
||||
auto it = held_locks_.find(b);
|
||||
if (it == held_locks_.end())
|
||||
throw std::runtime_error("lock not held");
|
||||
|
||||
if (it->second.first != t)
|
||||
throw std::runtime_error("lock type mismatch when unlocking");
|
||||
|
||||
it->second.second--;
|
||||
if (it->second.second == 0)
|
||||
held_locks_.erase(it);
|
||||
}
|
||||
|
||||
//----------------------------------------------------------------
|
||||
|
Reference in New Issue
Block a user