1
0
mirror of https://github.com/gnss-sdr/gnss-sdr synced 2025-11-02 16:23:06 +00:00

Replace the System V queues by boost::interprocess

This commit is contained in:
Carles Fernandez
2024-12-21 13:20:00 +01:00
parent 47e3cab802
commit cfb56d35d6
7 changed files with 222 additions and 175 deletions

View File

@@ -21,11 +21,13 @@
#include "control_thread.h"
#include "file_configuration.h"
#include "gnss_flowgraph.h"
#include "gnss_sdr_make_unique.h"
#include "gps_acq_assist.h"
#include "in_memory_configuration.h"
#include <boost/date_time/posix_time/posix_time.hpp>
#include <boost/exception/diagnostic_information.hpp>
#include <boost/exception/exception.hpp>
#include <boost/interprocess/ipc/message_queue.hpp>
#include <gtest/gtest.h>
#include <cerrno>
#include <chrono>
@@ -35,9 +37,6 @@
#include <numeric>
#include <random>
#include <string>
#include <sys/ipc.h>
#include <sys/msg.h>
#include <sys/types.h>
#include <thread>
#if USE_GLOG_AND_GFLAGS
@@ -79,13 +78,6 @@ Concurrent_Map<Gps_Acq_Assist> global_gps_acq_assist_map;
std::vector<double> TTFF_v;
typedef struct
{
long mtype; // required by SysV message
double ttff;
} ttff_msgbuf;
class TtffTest : public ::testing::Test
{
public:
@@ -301,45 +293,72 @@ void TtffTest::config_2()
void receive_msg()
{
ttff_msgbuf msg;
ttff_msgbuf msg_stop;
msg_stop.mtype = 1;
msg_stop.ttff = -200.0;
double ttff_msg = 0.0;
int msgrcv_size = sizeof(msg.ttff);
int msqid;
int msqid_stop = -1;
key_t key = 1101;
key_t key_stop = 1102;
bool leave = false;
while (!leave)
{
// wait for the queue to be created
while ((msqid = msgget(key, 0644)) == -1)
// wait for the queues to be created
const std::string queue_name = "gnss_sdr_ttff_message_queue";
std::unique_ptr<boost::interprocess::message_queue> d_mq;
bool queue_found = false;
while (!queue_found)
{
try
{
// Attempt to open the message queue
d_mq = std::make_unique<boost::interprocess::message_queue>(boost::interprocess::open_only, queue_name.c_str());
queue_found = true; // Queue found
}
catch (const boost::interprocess::interprocess_exception &)
{
// Queue not found, wait and retry
std::this_thread::sleep_for(std::chrono::milliseconds(100));
}
}
if (msgrcv(msqid, &msg, msgrcv_size, 1, 0) != -1)
const std::string queue_name_stop = "receiver_control_queue";
std::unique_ptr<boost::interprocess::message_queue> d_mq_stop;
bool queue_found2 = false;
while (!queue_found2)
{
ttff_msg = msg.ttff;
if ((ttff_msg != 0) && (ttff_msg != -1))
try
{
TTFF_v.push_back(ttff_msg);
LOG(INFO) << "Valid Time-To-First-Fix: " << ttff_msg << "[s]";
// Stop the receiver
while (((msqid_stop = msgget(key_stop, 0644))) == -1)
{
}
double msgsend_size = sizeof(msg_stop.ttff);
msgsnd(msqid_stop, &msg_stop, msgsend_size, IPC_NOWAIT);
// Attempt to open the message queue
d_mq_stop = std::make_unique<boost::interprocess::message_queue>(boost::interprocess::open_only, queue_name_stop.c_str());
queue_found2 = true; // Queue found
}
catch (const boost::interprocess::interprocess_exception &)
{
// Queue not found, wait and retry
std::this_thread::sleep_for(std::chrono::milliseconds(100));
}
}
if (std::abs(ttff_msg - (-1.0)) < 10 * std::numeric_limits<double>::epsilon())
double received_message;
unsigned int priority;
std::size_t received_size;
// Receive a message (non-blocking)
if (d_mq->try_receive(&received_message, sizeof(received_message), received_size, priority))
{
// Validate the size of the received message
if (received_size == sizeof(double) && (received_message != 0) && (received_message != -1))
{
TTFF_v.push_back(received_message);
LOG(INFO) << "Valid Time-To-First-Fix: " << received_message << " [s]";
// Stop the receiver
double stop_message = -200.0;
d_mq_stop->send(&stop_message, sizeof(stop_message), 0);
}
if (received_size == sizeof(double) && std::abs(received_message - (-1.0)) < 10 * std::numeric_limits<double>::epsilon())
{
leave = true;
}
}
else
{
// No message available, add a small delay to prevent busy-waiting
std::this_thread::sleep_for(std::chrono::milliseconds(200));
}
}
}
@@ -715,23 +734,26 @@ int main(int argc, char **argv)
}
// Terminate the queue thread
key_t sysv_msg_key;
int sysv_msqid;
sysv_msg_key = 1101;
int msgflg = IPC_CREAT | 0666;
if ((sysv_msqid = msgget(sysv_msg_key, msgflg)) == -1)
const std::string queue_name = "gnss_sdr_ttff_message_queue";
std::unique_ptr<boost::interprocess::message_queue> mq;
bool queue_found = false;
while (!queue_found)
{
std::cout << "GNSS-SDR can not create message queues!\n";
return 1;
try
{
// Attempt to open the message queue
mq = std::make_unique<boost::interprocess::message_queue>(boost::interprocess::open_only, queue_name.c_str());
queue_found = true; // Queue found
}
catch (const boost::interprocess::interprocess_exception &)
{
// Queue not found, wait and retry
std::this_thread::sleep_for(std::chrono::milliseconds(100));
}
}
ttff_msgbuf msg;
msg.mtype = 1;
msg.ttff = -1;
int msgsend_size;
msgsend_size = sizeof(msg.ttff);
msgsnd(sysv_msqid, &msg, msgsend_size, IPC_NOWAIT);
receive_msg_thread.join();
msgctl(sysv_msqid, IPC_RMID, nullptr);
double finish = -1.0;
mq->send(&finish, sizeof(finish), 0);
boost::interprocess::message_queue::remove(queue_name.c_str());
#if USE_GLOG_AND_GFLAGS
gflags::ShutDownCommandLineFlags();