-
Notifications
You must be signed in to change notification settings - Fork 57
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
10 changed files
with
408 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,87 @@ | ||
/** | ||
* Copyright (c) 2011-2023 libbitcoin developers (see AUTHORS) | ||
* | ||
* This file is part of libbitcoin. | ||
* | ||
* This program is free software: you can redistribute it and/or modify | ||
* it under the terms of the GNU Affero General Public License as published by | ||
* the Free Software Foundation, either version 3 of the License, or | ||
* (at your option) any later version. | ||
* | ||
* This program 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 Affero General Public License for more details. | ||
* | ||
* You should have received a copy of the GNU Affero General Public License | ||
* along with this program. If not, see <http://www.gnu.org/licenses/>. | ||
*/ | ||
#ifndef LIBBITCOIN_NETWORK_ASYNC_RACE_UNITY_HPP | ||
#define LIBBITCOIN_NETWORK_ASYNC_RACE_UNITY_HPP | ||
|
||
#include <memory> | ||
#include <tuple> | ||
#include <utility> | ||
#include <bitcoin/system.hpp> | ||
#include <bitcoin/network/define.hpp> | ||
|
||
namespace libbitcoin { | ||
namespace network { | ||
|
||
/// Not thread safe. | ||
/// Used in node validation chaser to invoke upon complete, with first error. | ||
/// race_unity invokes complete(args) provided at start(complete), with args | ||
/// from first failed call to finish(args), with failure determined by first | ||
/// argument in 'args', at the last expected invocation of finish(...), based | ||
/// on the constructor 'size' parameter. | ||
template <typename... Args> | ||
class race_unity final | ||
{ | ||
public: | ||
typedef std::shared_ptr<race_unity> ptr; | ||
typedef std::function<void(Args...)> handler; | ||
|
||
DELETE_COPY_MOVE(race_unity); | ||
|
||
race_unity(size_t size) NOEXCEPT; | ||
~race_unity() NOEXCEPT; | ||
|
||
/// True if the race_unity is running. | ||
inline bool running() const NOEXCEPT; | ||
|
||
/// False implies invalid usage. | ||
bool start(handler&& complete) NOEXCEPT; | ||
|
||
/// True implies winning finisher (last invocation, none failed). | ||
/// First arg is an 'error code', cast to bool (failed if true). | ||
/// There may be no winner, in which case last finish is invoked. | ||
bool finish(const Args&... args) NOEXCEPT; | ||
|
||
private: | ||
template <size_t... Pack> | ||
using unpack = std::index_sequence<Pack...>; | ||
using packed = std::tuple<std::decay_t<Args>...>; | ||
using sequence = std::index_sequence_for<Args...>; | ||
|
||
template<size_t... Index> | ||
void invoker(const handler& complete, const packed& args, | ||
unpack<Index...>) NOEXCEPT; | ||
bool invoke() NOEXCEPT; | ||
bool set_failure(bool failure) NOEXCEPT; | ||
|
||
// This is thread safe. | ||
const size_t size_; | ||
|
||
// These are not thread safe. | ||
packed args_{}; | ||
bool failure_{}; | ||
size_t runners_{}; | ||
std::shared_ptr<handler> complete_{}; | ||
}; | ||
|
||
} // namespace network | ||
} // namespace libbitcoin | ||
|
||
#include <bitcoin/network/impl/async/race_unity.ipp> | ||
|
||
#endif |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,142 @@ | ||
/** | ||
* Copyright (c) 2011-2023 libbitcoin developers (see AUTHORS) | ||
* | ||
* This file is part of libbitcoin. | ||
* | ||
* This program is free software: you can redistribute it and/or modify | ||
* it under the terms of the GNU Affero General Public License as published by | ||
* the Free Software Foundation, either version 3 of the License, or | ||
* (at your option) any later version. | ||
* | ||
* This program 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 Affero General Public License for more details. | ||
* | ||
* You should have received a copy of the GNU Affero General Public License | ||
* along with this program. If not, see <http://www.gnu.org/licenses/>. | ||
*/ | ||
#ifndef LIBBITCOIN_NETWORK_ASYNC_RACE_UNITY_IPP | ||
#define LIBBITCOIN_NETWORK_ASYNC_RACE_UNITY_IPP | ||
|
||
#include <memory> | ||
#include <tuple> | ||
#include <utility> | ||
#include <bitcoin/system.hpp> | ||
#include <bitcoin/network/define.hpp> | ||
|
||
namespace libbitcoin { | ||
namespace network { | ||
|
||
template <typename... Args> | ||
race_unity<Args...>:: | ||
race_unity(size_t size) NOEXCEPT | ||
: size_(size) | ||
{ | ||
} | ||
|
||
template <typename... Args> | ||
race_unity<Args...>:: | ||
~race_unity() NOEXCEPT | ||
{ | ||
BC_ASSERT_MSG(!running() && !complete_, "deleting running race_unity"); | ||
} | ||
|
||
template <typename... Args> | ||
inline bool race_unity<Args...>:: | ||
running() const NOEXCEPT | ||
{ | ||
return to_bool(runners_); | ||
} | ||
|
||
template <typename... Args> | ||
bool race_unity<Args...>:: | ||
start(handler&& complete) NOEXCEPT | ||
{ | ||
// false implies logic error. | ||
if (running()) | ||
return false; | ||
|
||
BC_PUSH_WARNING(NO_THROW_IN_NOEXCEPT) | ||
complete_ = std::make_shared<handler>(std::forward<handler>(complete)); | ||
BC_POP_WARNING() | ||
|
||
failure_ = false; | ||
runners_ = size_; | ||
return true; | ||
} | ||
|
||
template <typename... Args> | ||
bool race_unity<Args...>:: | ||
finish(const Args&... args) NOEXCEPT | ||
{ | ||
// false implies logic error. | ||
if (!running()) | ||
return false; | ||
|
||
// First argument (by convention) determines failure. | ||
// Capture parameter pack as tuple of copied arguments. | ||
auto values = std::tuple<Args...>(args...); | ||
const auto& ec = std::get<0>(values); | ||
const auto failer = set_failure(!!ec); | ||
|
||
// Save args for failer (first failure) or last success. | ||
const auto last = (runners_ == one); | ||
if (failer || (last && !failure_)) | ||
args_ = std::move(values); | ||
|
||
// false invoke implies logic error. | ||
return invoke() && last && !failure_; | ||
} | ||
|
||
// private | ||
// ---------------------------------------------------------------------------- | ||
|
||
template <typename... Args> | ||
bool race_unity<Args...>:: | ||
set_failure(bool failure) NOEXCEPT | ||
{ | ||
// Return is failer (first to fail) and set failure. | ||
failure &= !failure_; | ||
failure_ |= failure; | ||
return failure; | ||
} | ||
|
||
template <typename... Args> | ||
bool race_unity<Args...>:: | ||
invoke() NOEXCEPT | ||
{ | ||
// false implies logic error. | ||
if (!running()) | ||
return false; | ||
|
||
--runners_; | ||
if (running()) | ||
return true; | ||
|
||
// false implies logic error. | ||
if (!complete_) | ||
return false; | ||
|
||
// Invoke completion handler. | ||
invoker(*complete_, args_, sequence{}); | ||
|
||
// Clear all resources. | ||
complete_.reset(); | ||
args_ = {}; | ||
return true; | ||
} | ||
|
||
template <typename... Args> | ||
template<size_t... Index> | ||
void race_unity<Args...>:: | ||
invoker(const handler& complete, const packed& args, unpack<Index...>) NOEXCEPT | ||
{ | ||
// Expand tuple into parameter pack. | ||
complete(std::get<Index>(args)...); | ||
} | ||
|
||
} // namespace network | ||
} // namespace libbitcoin | ||
|
||
#endif |
Oops, something went wrong.