Changeset View
Changeset View
Standalone View
Standalone View
src/interfaces/chain.cpp
// Copyright (c) 2018 The Bitcoin Core developers | // Copyright (c) 2018 The Bitcoin Core developers | ||||
// Distributed under the MIT software license, see the accompanying | // Distributed under the MIT software license, see the accompanying | ||||
// file COPYING or http://www.opensource.org/licenses/mit-license.php. | // file COPYING or http://www.opensource.org/licenses/mit-license.php. | ||||
#include <interfaces/chain.h> | #include <interfaces/chain.h> | ||||
#include <sync.h> | |||||
#include <util/system.h> | #include <util/system.h> | ||||
#include <validation.h> | |||||
#include <memory> | |||||
#include <utility> | |||||
namespace interfaces { | namespace interfaces { | ||||
namespace { | namespace { | ||||
class ChainImpl : public Chain {}; | class LockImpl : public Chain::Lock {}; | ||||
class LockingStateImpl : public LockImpl, | |||||
public UniqueLock<CCriticalSection> { | |||||
using UniqueLock::UniqueLock; | |||||
}; | |||||
class ChainImpl : public Chain { | |||||
public: | |||||
std::unique_ptr<Chain::Lock> lock(bool try_lock) override { | |||||
auto result = std::make_unique<LockingStateImpl>( | |||||
::cs_main, "cs_main", __FILE__, __LINE__, try_lock); | |||||
if (try_lock && result && !*result) { | |||||
return {}; | |||||
} | |||||
// std::move necessary on some compilers due to conversion from | |||||
// LockingStateImpl to Lock pointer | |||||
return std::move(result); | |||||
} | |||||
std::unique_ptr<Chain::Lock> assumeLocked() override { | |||||
return std::make_unique<LockImpl>(); | |||||
} | |||||
}; | |||||
} // namespace | } // namespace | ||||
std::unique_ptr<Chain> MakeChain() { | std::unique_ptr<Chain> MakeChain() { | ||||
return std::make_unique<ChainImpl>(); | return std::make_unique<ChainImpl>(); | ||||
} | } | ||||
} // namespace interfaces | } // namespace interfaces |