2013-01-07 20:29:41 +05:30
|
|
|
// Copyright (C) 2012 Red Hat, Inc. All rights reserved.
|
|
|
|
//
|
|
|
|
// This file is part of the thin-provisioning-tools source.
|
|
|
|
//
|
|
|
|
// thin-provisioning-tools is free software: you can redistribute it
|
|
|
|
// and/or modify it under the terms of the GNU General Public License
|
|
|
|
// as published by the Free Software Foundation, either version 3 of
|
|
|
|
// the License, or (at your option) any later version.
|
|
|
|
//
|
|
|
|
// thin-provisioning-tools is distributed in the hope that it will be
|
|
|
|
// useful, but WITHOUT ANY WARRANTY; without even the implied warranty
|
|
|
|
// of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
|
|
// GNU General Public License for more details.
|
|
|
|
//
|
|
|
|
// You should have received a copy of the GNU General Public License along
|
|
|
|
// with thin-provisioning-tools. If not, see
|
|
|
|
// <http://www.gnu.org/licenses/>.
|
|
|
|
|
|
|
|
#include "lock_tracker.h"
|
|
|
|
|
|
|
|
#include <stdexcept>
|
|
|
|
|
|
|
|
using namespace persistent_data;
|
|
|
|
using namespace std;
|
|
|
|
|
|
|
|
//----------------------------------------------------------------
|
|
|
|
|
|
|
|
lock_tracker::lock_tracker(uint64_t low, uint64_t high)
|
|
|
|
: low_(low),
|
|
|
|
high_(high)
|
|
|
|
{
|
|
|
|
}
|
|
|
|
|
|
|
|
void
|
|
|
|
lock_tracker::read_lock(uint64_t key)
|
|
|
|
{
|
|
|
|
check_key(key);
|
|
|
|
|
2013-01-07 21:27:19 +05:30
|
|
|
LockMap::iterator it = locks_.find(key);
|
2013-01-07 20:29:41 +05:30
|
|
|
if (found(it)) {
|
|
|
|
if (it->second < 0)
|
|
|
|
throw runtime_error("already write locked");
|
|
|
|
|
2013-01-07 21:27:19 +05:30
|
|
|
it->second++;
|
2013-01-07 20:29:41 +05:30
|
|
|
|
|
|
|
} else
|
|
|
|
locks_.insert(make_pair(key, 1));
|
|
|
|
}
|
|
|
|
|
|
|
|
void
|
|
|
|
lock_tracker::write_lock(uint64_t key)
|
|
|
|
{
|
|
|
|
check_key(key);
|
|
|
|
|
|
|
|
LockMap::const_iterator it = locks_.find(key);
|
|
|
|
if (found(it))
|
|
|
|
throw runtime_error("already locked");
|
|
|
|
|
|
|
|
locks_.insert(make_pair(key, -1));
|
|
|
|
}
|
|
|
|
|
2013-01-07 21:00:51 +05:30
|
|
|
void
|
|
|
|
lock_tracker::superblock_lock(uint64_t key)
|
|
|
|
{
|
|
|
|
if (superblock_)
|
|
|
|
throw runtime_error("superblock already held");
|
|
|
|
|
|
|
|
superblock_ = boost::optional<uint64_t>(key);
|
|
|
|
try {
|
|
|
|
write_lock(key);
|
|
|
|
|
|
|
|
} catch (...) {
|
|
|
|
superblock_ = boost::optional<uint64_t>();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2013-01-07 20:29:41 +05:30
|
|
|
void
|
|
|
|
lock_tracker::unlock(uint64_t key)
|
|
|
|
{
|
|
|
|
check_key(key);
|
|
|
|
|
|
|
|
LockMap::const_iterator it = locks_.find(key);
|
|
|
|
if (!found(it))
|
|
|
|
throw runtime_error("not locked");
|
|
|
|
|
2013-01-07 21:27:19 +05:30
|
|
|
if (superblock_ && *superblock_ == key) {
|
|
|
|
if (locks_.size() > 1)
|
|
|
|
throw runtime_error("superblock unlocked while other locks still held");
|
|
|
|
|
|
|
|
superblock_ = boost::optional<uint64_t>();
|
|
|
|
}
|
|
|
|
|
2013-01-07 20:29:41 +05:30
|
|
|
if (it->second > 1)
|
|
|
|
locks_.insert(make_pair(key, it->second - 1));
|
|
|
|
else
|
|
|
|
locks_.erase(key);
|
2013-01-07 21:00:51 +05:30
|
|
|
|
2013-01-07 20:29:41 +05:30
|
|
|
}
|
|
|
|
|
|
|
|
bool
|
|
|
|
lock_tracker::found(LockMap::const_iterator it) const
|
|
|
|
{
|
|
|
|
return it != locks_.end();
|
|
|
|
}
|
|
|
|
|
|
|
|
bool
|
|
|
|
lock_tracker::valid_key(uint64_t key) const
|
|
|
|
{
|
|
|
|
return (key >= low_ && key <= high_);
|
|
|
|
}
|
|
|
|
|
|
|
|
void
|
|
|
|
lock_tracker::check_key(uint64_t key) const
|
|
|
|
{
|
|
|
|
if (!valid_key(key))
|
|
|
|
throw runtime_error("invalid key");
|
|
|
|
}
|
|
|
|
|
2013-04-29 17:01:30 +05:30
|
|
|
bool
|
|
|
|
lock_tracker::is_locked(uint64_t key) const
|
|
|
|
{
|
|
|
|
check_key(key);
|
|
|
|
return found(locks_.find(key));
|
|
|
|
}
|
|
|
|
|
2013-01-07 20:29:41 +05:30
|
|
|
//----------------------------------------------------------------
|
|
|
|
|