Skip to content

Commit

Permalink
Rename performInit() back to init()
Browse files Browse the repository at this point in the history
Revert the change to performInit(), rename the StateMachine callback
from init() to initialise().
  • Loading branch information
frankosterfeld committed Mar 6, 2024
1 parent 5ca74df commit 8c4cc79
Show file tree
Hide file tree
Showing 8 changed files with 32 additions and 34 deletions.
10 changes: 4 additions & 6 deletions blocks/basic/include/gnuradio-4.0/basic/DataSink.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -439,13 +439,10 @@ class DataSink : public Block<DataSink<T>> {
}
};

~DataSink() {
stop();
DataSinkRegistry::instance().unregisterSink(this);
}

void
init() noexcept {
initialise() noexcept {
// TODO this should be in start() for symmetry, but then registering pollers/callbacks before graph execution wouldn't work
// and data might not be received by the listener/callback (important for unit tests)
DataSinkRegistry::instance().registerSink(this);
}

Expand Down Expand Up @@ -533,6 +530,7 @@ class DataSink : public Block<DataSink<T>> {
// TODO this code should be called at the end of graph processing
void
stop() noexcept {
DataSinkRegistry::instance().unregisterSink(this);
std::lock_guard lg(_listener_mutex);
for (auto &listener : _listeners) {
listener->stop();
Expand Down
2 changes: 1 addition & 1 deletion blocks/basic/test/qa_DataSink.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -402,7 +402,7 @@ const boost::ut::suite DataSinkTests = [] {
sched.runAndWait();

const auto pollerAfterStop = DataSinkRegistry::instance().getStreamingPoller<float>(DataSinkQuery::sinkName("test_sink"));
expect(pollerAfterStop->finished.load());
expect(eq(pollerAfterStop, nullptr));
}

const auto pollerAfterDestruction = DataSinkRegistry::instance().getStreamingPoller<float>(DataSinkQuery::sinkName("test_sink"));
Expand Down
4 changes: 2 additions & 2 deletions core/include/gnuradio-4.0/Block.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -322,7 +322,7 @@ struct isBlockDependent {
* The Scheduler invokes these methods on each Block instance, if they are implemented, just before invoking its corresponding method of the same name.
* @code
* struct userBlock : public Block<userBlock> {
* void init() {...} // Implement any initialisation logic required for the block within this method.
* void initialise() {...} // Implement any initialisation logic required for the block within this method.
* void start() {...} // Implement any startup logic required for the block within this method.
* void stop() {...} // Use this method for handling any clean-up procedures.
* void pause() {...} // Implement logic to temporarily halt the block's operation, maintaining its current state.
Expand Down Expand Up @@ -594,7 +594,7 @@ class Block : public lifecycle::StateMachine<Derived>, //
}

void
performInit(std::shared_ptr<gr::Sequence> progress_, std::shared_ptr<gr::thread_pool::BasicThreadPool> ioThreadPool_) {
init(std::shared_ptr<gr::Sequence> progress_, std::shared_ptr<gr::thread_pool::BasicThreadPool> ioThreadPool_) {
progress = std::move(progress_);
ioThreadPool = std::move(ioThreadPool_);

Expand Down
12 changes: 6 additions & 6 deletions core/include/gnuradio-4.0/Graph.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -179,7 +179,7 @@ class BlockModel {
* @brief to be called by scheduler->graph to initialise block
*/
virtual void
performInit(std::shared_ptr<gr::Sequence> progress, std::shared_ptr<gr::thread_pool::BasicThreadPool> ioThreadPool)
init(std::shared_ptr<gr::Sequence> progress, std::shared_ptr<gr::thread_pool::BasicThreadPool> ioThreadPool)
= 0;

/**
Expand Down Expand Up @@ -403,8 +403,8 @@ class BlockWrapper : public BlockModel {
}

void
performInit(std::shared_ptr<gr::Sequence> progress, std::shared_ptr<gr::thread_pool::BasicThreadPool> ioThreadPool) override {
return blockRef().performInit(progress, ioThreadPool);
init(std::shared_ptr<gr::Sequence> progress, std::shared_ptr<gr::thread_pool::BasicThreadPool> ioThreadPool) override {
return blockRef().init(progress, ioThreadPool);
}

[[nodiscard]] constexpr work::Result
Expand Down Expand Up @@ -771,7 +771,7 @@ class Graph : public gr::Block<Graph> {
BlockModel &
addBlock(std::unique_ptr<BlockModel> block) {
auto &new_block_ref = _blocks.emplace_back(std::move(block));
new_block_ref->performInit(progress, ioThreadPool);
new_block_ref->init(progress, ioThreadPool);
// TODO: Should we connectChildMessagePorts for these blocks as well?
return *new_block_ref.get();
}
Expand All @@ -782,7 +782,7 @@ class Graph : public gr::Block<Graph> {
static_assert(std::is_same_v<TBlock, std::remove_reference_t<TBlock>>);
auto &new_block_ref = _blocks.emplace_back(std::make_unique<BlockWrapper<TBlock>>(std::forward<Args>(args)...));
auto raw_ref = static_cast<TBlock *>(new_block_ref->raw());
raw_ref->performInit(progress, ioThreadPool);
raw_ref->init(progress, ioThreadPool);
return *raw_ref;
}

Expand All @@ -800,7 +800,7 @@ class Graph : public gr::Block<Graph> {
}
throw std::invalid_argument(fmt::format("initial Block settings could not be applied successfully - mismatched keys or value-type: {}\n", fmt::join(keys, ", ")));
}
raw_ref->performInit(progress, ioThreadPool);
raw_ref->init(progress, ioThreadPool);
return *raw_ref;
}

Expand Down
16 changes: 8 additions & 8 deletions core/include/gnuradio-4.0/LifeCycle.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -117,12 +117,12 @@ struct ErrorType {
* It is designed to be inherited by blocks (TDerived) to safely and effectively manage their lifecycle state transitions.
*
* If implemented in TDerived, the following specific lifecycle methods are called:
* - `init()` when transitioning from IDLE to INITIALISED
* - `start()` when transitioning from INITIALISED to RUNNING
* - `stop()` when transitioning from any `isActive(State)` to REQUESTED_STOP
* - `pause()` when transitioning from RUNNING to REQUESTED_PAUSE
* - `resume()` when transitioning from PAUSED to RUNNING
* - `reset()` when transitioning from any state (typically ERROR or STOPPED) to INITIALISED.
* - `initialise()` when transitioning from IDLE to INITIALISED
* - `start()` when transitioning from INITIALISED to RUNNING
* - `stop()` when transitioning from any `isActive(State)` to REQUESTED_STOP
* - `pause()` when transitioning from RUNNING to REQUESTED_PAUSE
* - `resume()` when transitioning from PAUSED to RUNNING
* - `reset()` when transitioning from any state (typically ERROR or STOPPED) to INITIALISED.
* If any of these methods throw an exception, the StateMachine transitions to the ERROR state, captures,
* and forward the exception details.
*
Expand Down Expand Up @@ -208,9 +208,9 @@ class StateMachine {
return {};
} else {
// Call specific methods in TDerived based on the state
if constexpr (requires(TDerived &d) { d.init(); }) {
if constexpr (requires(TDerived &d) { d.initialise(); }) {
if (oldState == State::IDLE && newState == State::INITIALISED) {
return invokeLifecycleMethod(&TDerived::init, location);
return invokeLifecycleMethod(&TDerived::initialise, location);
}
}
if constexpr (requires(TDerived &d) { d.start(); }) {
Expand Down
10 changes: 5 additions & 5 deletions core/include/gnuradio-4.0/Scheduler.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -225,7 +225,7 @@ class SchedulerBase : public lifecycle::StateMachine<Derived> {
}

void
init() {
initialise() {
[[maybe_unused]] const auto pe = _profiler_handler.startCompleteEvent("scheduler_base.init");
const auto result = _graph.performConnections();
if (!result) {
Expand Down Expand Up @@ -375,8 +375,8 @@ class Simple : public SchedulerBase<Simple<execution, TProfiler>, execution, TPr

private:
void
init() {
base_t::init();
initialise() {
base_t::initialise();
[[maybe_unused]] const auto pe = this->_profiler_handler.startCompleteEvent("scheduler_simple.init");
// generate job list
if constexpr (base_t::executionPolicy() == ExecutionPolicy::multiThreaded) {
Expand Down Expand Up @@ -440,10 +440,10 @@ class BreadthFirst : public SchedulerBase<BreadthFirst<execution, TProfiler>, ex

private:
void
init() {
initialise() {
[[maybe_unused]] const auto pe = this->_profiler_handler.startCompleteEvent("breadth_first.init");
using block_t = BlockModel *;
base_t::init();
base_t::initialise();
// calculate adjacency list
std::map<block_t, std::vector<block_t>> _adjacency_list{};
std::vector<block_t> _source_blocks{};
Expand Down
2 changes: 1 addition & 1 deletion core/test/qa_HierBlock.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -68,7 +68,7 @@ class HierBlock : public gr::lifecycle::StateMachine<HierBlock<T>>, public gr::B
~HierBlock() override = default;

void
performInit(std::shared_ptr<gr::Sequence> /*progress*/, std::shared_ptr<gr::thread_pool::BasicThreadPool> /*ioThreadPool*/) override {}
init(std::shared_ptr<gr::Sequence> /*progress*/, std::shared_ptr<gr::thread_pool::BasicThreadPool> /*ioThreadPool*/) override {}

[[nodiscard]] std::string_view
name() const override {
Expand Down
10 changes: 5 additions & 5 deletions core/test/qa_Settings.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -349,7 +349,7 @@ const boost::ut::suite SettingsTests = [] {
"constructor"_test = [] {
"empty"_test = [] {
auto block = TestBlock<float>();
block.performInit(block.progress, block.ioThreadPool); // N.B. self-assign existing progress and thread-pool (just for unit-tests)
block.init(block.progress, block.ioThreadPool); // N.B. self-assign existing progress and thread-pool (just for unit-tests)
expect(eq(block.settings().get().size(), 12UL));
expect(eq(std::get<float>(*block.settings().get("scaling_factor")), 1.f));
};
Expand All @@ -358,7 +358,7 @@ const boost::ut::suite SettingsTests = [] {
"with init parameter"_test = [] {
auto block = TestBlock<float>({ { "scaling_factor", 2.f } });
expect(eq(block.settings().stagedParameters().size(), 1u));
block.performInit(block.progress, block.ioThreadPool); // N.B. self-assign existing progress and thread-pool (just for unit-tests)
block.init(block.progress, block.ioThreadPool); // N.B. self-assign existing progress and thread-pool (just for unit-tests)
expect(eq(block.settings().stagedParameters().size(), 0u));
block.settings().updateActiveParameters();
expect(eq(block.settings().get().size(), 12UL));
Expand Down Expand Up @@ -393,7 +393,7 @@ const boost::ut::suite SettingsTests = [] {
block.debug = true;
const auto val = block.settings().set({ { "vector_setting", std::vector{ 42.f, 2.f, 3.f } }, { "string_vector_setting", std::vector<std::string>{ "A", "B", "C" } } });
expect(val.empty()) << "unable to stage settings";
block.performInit(block.progress, block.ioThreadPool); // N.B. self-assign existing progress and thread-pool (just for unit-tests)
block.init(block.progress, block.ioThreadPool); // N.B. self-assign existing progress and thread-pool (just for unit-tests)
expect(eq(block.vector_setting, std::vector{ 42.f, 2.f, 3.f }));
expect(eq(block.string_vector_setting.value, std::vector<std::string>{ "A", "B", "C" }));
expect(eq(block.update_count, 1)) << fmt::format("actual update count: {}\n", block.update_count);
Expand All @@ -417,7 +417,7 @@ const boost::ut::suite SettingsTests = [] {
auto ioThreadPool = std::make_shared<gr::thread_pool::BasicThreadPool>("test_pool", gr::thread_pool::TaskType::IO_BOUND, 2UZ, std::numeric_limits<uint32_t>::max());
//
auto wrapped1 = BlockWrapper<TestBlock<float>>();
wrapped1.performInit(progress, ioThreadPool);
wrapped1.init(progress, ioThreadPool);
wrapped1.setName("test_name");
expect(eq(wrapped1.name(), "test_name"sv)) << "BlockModel wrapper name";
expect(not wrapped1.uniqueName().empty()) << "unique name";
Expand All @@ -428,7 +428,7 @@ const boost::ut::suite SettingsTests = [] {
// via constructor
auto wrapped2 = BlockWrapper<TestBlock<float>>({ { "name", "test_name" } });
expect(wrapped2.settings().set({ { "context", "a string" } }).empty()) << "successful set returns empty map";
wrapped2.performInit(progress, ioThreadPool);
wrapped2.init(progress, ioThreadPool);
expect(eq(wrapped2.name(), "test_name"sv)) << "BlockModel wrapper name";
expect(not wrapped2.uniqueName().empty()) << "unique name";
expect(wrapped2.settings().set({ { "context", "a string" } }).empty()) << "successful set returns empty map";
Expand Down

0 comments on commit 8c4cc79

Please sign in to comment.