7seg: modify the file operation
Open the file descriptor in main() and close the file descriptor only
when the program is about to terminate. Closing the file descriptor at
DisplayDbusValue() in 7seg.cpp will make the program using poll() to
receive POST codes always return the POLLHUP and the poll() is not
actually blocked, which makes the program always preempt the CPU. This
modification prevents the user space program recieving POST codes via
poll() from preempting the CPU too much time.
Tested: using `top` to inspect whether the program receiving POST codes
from 7seg.cpp does not preempt the CPU resources.
Signed-off-by: wukaihua-fii-na <eason.kh.wu@fii-na.com>
Change-Id: I0824727d8cb207c1622da12ac833a089a676661e
diff --git a/7seg.cpp b/7seg.cpp
index 2efc25c..daa71f9 100644
--- a/7seg.cpp
+++ b/7seg.cpp
@@ -20,17 +20,14 @@
#include <lpcsnoop/snoop_listen.hpp>
#include <string>
-static const char* device_node_path;
-
namespace fs = std::filesystem;
-static void DisplayDbusValue(postcode_t postcodes)
+static void DisplayDbusValue(FILE* f, postcode_t postcodes)
{
auto postcode = std::get<primary_post_code_t>(postcodes);
// Uses cstdio instead of streams because the device file has
// very strict requirements about the data format and streaming
// abstractions tend to muck it up.
- FILE* f = std::fopen(device_node_path, "r+");
if (f)
{
int rc = std::fprintf(f, "%d%02x\n", (postcode > 0xff),
@@ -40,7 +37,6 @@
std::fprintf(stderr, "failed to write 7seg value: rc=%d\n", rc);
}
std::fflush(f);
- std::fclose(f);
}
}
@@ -58,17 +54,26 @@
return -1;
}
- device_node_path = argv[1];
+ static bool sig_recv = false;
+ FILE* f = std::fopen(argv[1], "r+");
auto ListenBus = sdbusplus::bus::new_default();
std::unique_ptr<lpcsnoop::SnoopListen> snoop =
- std::make_unique<lpcsnoop::SnoopListen>(ListenBus, DisplayDbusValue);
+ std::make_unique<lpcsnoop::SnoopListen>(ListenBus, DisplayDbusValue, f);
- while (true)
+ signal(SIGINT, [](int signum) {
+ if (signum == SIGINT)
+ {
+ sig_recv = true:
+ }
+ });
+
+ while (!sig_recv)
{
ListenBus.process_discard();
ListenBus.wait();
}
+ std::fclose(f);
return 0;
}
diff --git a/example.cpp b/example.cpp
index 9bec1b9..917c12e 100644
--- a/example.cpp
+++ b/example.cpp
@@ -22,7 +22,7 @@
#include <memory>
/* Example PostCode handler which simply prints them */
-static void printPostcode(postcode_t postcode)
+static void printPostcode(FILE*, postcode_t postcode)
{
/* Print output to verify the example program is receiving values. */
std::printf("recv: 0x%" PRIx64 "\n",
diff --git a/lpcsnoop/snoop_listen.hpp b/lpcsnoop/snoop_listen.hpp
index 876c119..292745b 100644
--- a/lpcsnoop/snoop_listen.hpp
+++ b/lpcsnoop/snoop_listen.hpp
@@ -36,7 +36,7 @@
class SnoopListen
{
using message_handler_t = std::function<void(sdbusplus::message::message&)>;
- using postcode_handler_t = std::function<void(postcode_t)>;
+ using postcode_handler_t = std::function<void(FILE*, postcode_t)>;
public:
SnoopListen(sdbusplus::bus::bus& busIn, sd_bus_message_handler_t handler) :
@@ -49,8 +49,9 @@
{
}
- SnoopListen(sdbusplus::bus::bus& busIn, postcode_handler_t handler) :
- SnoopListen(busIn, std::bind(defaultMessageHandler, handler,
+ SnoopListen(sdbusplus::bus::bus& busIn, postcode_handler_t handler,
+ FILE* f = NULL) :
+ SnoopListen(busIn, std::bind(defaultMessageHandler, handler, f,
std::placeholders::_1))
{
}
@@ -69,7 +70,7 @@
* Default message handler which listens to published messages on snoop
* DBus path, and calls the given postcode_handler on each value received.
*/
- static void defaultMessageHandler(postcode_handler_t& handler,
+ static void defaultMessageHandler(postcode_handler_t& handler, FILE* f,
sdbusplus::message::message& m)
{
std::string messageBusName;
@@ -81,7 +82,7 @@
if (messageBusName == snoopDbus &&
messageData.find(propertyKey) != messageData.end())
{
- handler(get<postcode_t>(messageData[propertyKey]));
+ handler(f, get<postcode_t>(messageData[propertyKey]));
}
}
};