diff options
author | Nan Zhou <nanzhoumails@gmail.com> | 2022-06-24 01:28:25 +0300 |
---|---|---|
committer | Ed Tanous <ed@tanous.net> | 2022-07-07 21:55:31 +0300 |
commit | 6384e3239a6567db6e9b0df73f77193768c5a86d (patch) | |
tree | 46021e320115bfae86f7886e1895bd8fb6f49793 /src | |
parent | 16a5535f15af187b56d4c927aa9dec3ef8f66af4 (diff) | |
download | bmcweb-6384e3239a6567db6e9b0df73f77193768c5a86d.tar.xz |
dbus_singleton: use stack variable and extern
Currently, the |systemBus| connection is a static variable declared in
headers. This has a problem that every translation unit will keep its
own copy. It's not a problem today because there's only one translation
unit "webserver_main.cpp.o". This issue was brounght up in
https://gerrit.openbmc.org/c/openbmc/bmcweb/+/54758
Actually, the |systemBus| doesn't need to be a singleton. It can just be
a stack variable, which is normally more efficient than heap variables.
To keep minimum changes treeside, this commits keeps the existing
|systemBus| variable as an external variable. It is defined in its own
translation unit. It is initialized in the main translation unit.
Reference:
1. Extern
https://stackoverflow.com/questions/1433204/how-do-i-use-extern-to-share-variables-between-source-files
Tested:
1. Romulus QEMU robot Redfish test passed;
2. Start and restart service on real hardware, no issues;
3. No new validator failures
4. Code compies
Signed-off-by: Nan Zhou <nanzhoumails@gmail.com>
Change-Id: I03b387bd5f218a86c9d1765415a46e3c2ad83ff9
Diffstat (limited to 'src')
-rw-r--r-- | src/dbus_singleton.cpp | 13 | ||||
-rw-r--r-- | src/webserver_main.cpp | 7 |
2 files changed, 17 insertions, 3 deletions
diff --git a/src/dbus_singleton.cpp b/src/dbus_singleton.cpp new file mode 100644 index 0000000000..f78164fbe2 --- /dev/null +++ b/src/dbus_singleton.cpp @@ -0,0 +1,13 @@ +#include "dbus_singleton.hpp" + +#include <boost/asio/io_context.hpp> + +namespace crow +{ +namespace connections +{ + +sdbusplus::asio::connection* systemBus = nullptr; + +} // namespace connections +} // namespace crow diff --git a/src/webserver_main.cpp b/src/webserver_main.cpp index ff0bbd7420..c306dea958 100644 --- a/src/webserver_main.cpp +++ b/src/webserver_main.cpp @@ -65,8 +65,8 @@ static int run() auto io = std::make_shared<boost::asio::io_context>(); App app(io); - crow::connections::systemBus = - std::make_shared<sdbusplus::asio::connection>(*io); + sdbusplus::asio::connection systemBus(*io); + crow::connections::systemBus = &systemBus; // Static assets need to be initialized before Authorization, because auth // needs to build the whitelist from the static routes @@ -147,7 +147,8 @@ static int run() app.run(); io->run(); - crow::connections::systemBus.reset(); + crow::connections::systemBus = nullptr; + return 0; } |