entity-manager: remove global io_context
Remove the global var
```
boost::asio::io_context io
```
and move it to be a local var in the main function.
Since boost::asio::io_context io is declared first, it in scope for the
entire program duration and should not cause any issues from that
perspective.
The io_context is passed through where needed. In case there is a class
already defined, the class now has a reference to the io_context to
avoid passing it through everywhere.
Tested: Capturing or passing a reference which is always valid should
not introduce any issues.
Tested on Tyan S8030:
```
Jul 01 09:59:26 s8030-bmc-30303035c0c1 entity-manager[4204]: Inventory Added: Supermicro PWS 920P SQ 0
Jul 01 09:59:26 s8030-bmc-30303035c0c1 entity-manager[4204]: Inventory Added: Supermicro PWS 920P SQ 1
Jul 01 09:59:26 s8030-bmc-30303035c0c1 entity-manager[4204]: Inventory Added: chassis
Jul 01 09:59:26 s8030-bmc-30303035c0c1 entity-manager[4204]: Inventory Added: MBX 1.57 Chassis
```
busctl tree output as before
Change-Id: Ie8f7d18c38d166c57a9cb645ab45c9103bbdff6e
Signed-off-by: Alexander Hansen <alexander.hansen@9elements.com>
diff --git a/src/entity_manager/overlay.cpp b/src/entity_manager/overlay.cpp
index 7d7de96..958ced5 100644
--- a/src/entity_manager/overlay.cpp
+++ b/src/entity_manager/overlay.cpp
@@ -169,7 +169,8 @@
const std::string& parameters, uint64_t bus, uint64_t address,
const std::string& constructor, const std::string& destructor,
const devices::createsHWMon hasHWMonDir,
- std::vector<std::string> channelNames, const size_t retries = 5)
+ std::vector<std::string> channelNames, boost::asio::io_context& io,
+ const size_t retries = 5)
{
if (retries == 0U)
{
@@ -193,8 +194,8 @@
createTimer->async_wait(
[createTimer, name, busPath, parameters, bus, address,
constructor, destructor, hasHWMonDir,
- channelNames(std::move(channelNames)),
- retries](const boost::system::error_code& ec) mutable {
+ channelNames(std::move(channelNames)), retries,
+ &io](const boost::system::error_code& ec) mutable {
if (ec)
{
std::cerr << "Timer error: " << ec << "\n";
@@ -202,7 +203,8 @@
}
return buildDevice(name, busPath, parameters, bus, address,
constructor, destructor, hasHWMonDir,
- std::move(channelNames), retries - 1);
+ std::move(channelNames), io,
+ retries - 1);
});
return -1;
}
@@ -219,7 +221,8 @@
void exportDevice(const std::string& type,
const devices::ExportTemplate& exportTemplate,
- const nlohmann::json& configuration)
+ const nlohmann::json& configuration,
+ boost::asio::io_context& io)
{
std::string parameters = exportTemplate.parameters;
std::string busPath = exportTemplate.busPath;
@@ -273,10 +276,11 @@
}
buildDevice(name, busPath, parameters, *bus, *address, constructor,
- destructor, hasHWMonDir, std::move(channels));
+ destructor, hasHWMonDir, std::move(channels), io);
}
-bool loadOverlays(const nlohmann::json& systemConfiguration)
+bool loadOverlays(const nlohmann::json& systemConfiguration,
+ boost::asio::io_context& io)
{
std::filesystem::create_directory(outputDir);
for (auto entity = systemConfiguration.begin();
@@ -307,7 +311,7 @@
auto device = devices::exportTemplates.find(type.c_str());
if (device != devices::exportTemplates.end())
{
- exportDevice(type, device->second, configuration);
+ exportDevice(type, device->second, configuration, io);
continue;
}