diff --git a/core/AuxTaskRT.cpp b/core/AuxTaskRT.cpp index 4bd312d0a..895a3a92b 100644 --- a/core/AuxTaskRT.cpp +++ b/core/AuxTaskRT.cpp @@ -4,34 +4,58 @@ #include #include #include + AuxTaskRT::~AuxTaskRT() { lShouldStop = true; - struct timespec absoluteTimeout = {0, 0}; - // non blocking write, so if the queue is full it won't fail - char c = 0; - int ret = BELA_RT_WRAP(mq_timedsend(queueDesc, &c, sizeof(c), 0, &absoluteTimeout)); - if(ret) - fprintf(stderr, "Error mq_timedsend: %d %s\n", errno, strerror(errno)); + if(queueValid) + { + struct timespec absoluteTimeout = {0, 0}; + // non blocking write, so if the queue is full it won't fail + char c = 0; + int ret = BELA_RT_WRAP(mq_timedsend(queueDesc, &c, sizeof(c), 0, &absoluteTimeout)); + if(ret) + fprintf(stderr, "Error mq_timedsend: %d %s\n", errno, strerror(errno)); + } join(); - ret = BELA_RT_WRAP(mq_close(queueDesc)); - if(ret) - fprintf(stderr, "Error closing queueDesc: %d %s\n", errno, strerror(errno)); - ret = BELA_RT_WRAP(mq_unlink(queueName.c_str())); - if(ret) - fprintf(stderr, "Error unlinking queue: %d %s\n", errno, strerror(errno)); + if(queueValid) + { + int ret = BELA_RT_WRAP(mq_close(queueDesc)); + if(ret) + fprintf(stderr, "Error closing queueDesc: %d %s\n", errno, strerror(errno)); + ret = BELA_RT_WRAP(mq_unlink(queueName.c_str())); + if(ret) + fprintf(stderr, "Error unlinking queue: %d %s\n", errno, strerror(errno)); + } } int AuxTaskRT::commsInit() { // create a queue, with prefixed name - queueName = std::string("/q_") + name; + queueName = std::string("/q_") + name + std::to_string((long long unsigned)(this)); struct mq_attr attr; attr.mq_maxmsg = 100; attr.mq_msgsize = 100000; - queueDesc = BELA_RT_WRAP(mq_open(queueName.c_str(), O_CREAT | O_RDWR, 0644, &attr)); - if((mqd_t)-1 == queueDesc) + // first try with our desired queue properties, failing that try with the system defaults + for(auto& ptr : {&attr, (struct mq_attr*)nullptr}) + { + queueDesc = BELA_RT_WRAP(mq_open(queueName.c_str(), O_CREAT | O_RDWR, 0644, ptr)); + queueValid = ((mqd_t)-1 != queueDesc); + if(queueValid) + { + break; + } else { + if(&attr == ptr) + { + fprintf(stderr, "Opening queue %s with desired settings failed, trying with system defaults\n", queueName.c_str()); + } + } + } + if(!queueValid) + { + fprintf(stderr, "Error creating queue %s: %d %s\n", queueName.c_str(), errno, strerror(errno)); return 1; + } return 0; } diff --git a/include/AuxTaskRT.h b/include/AuxTaskRT.h index 080c67ba1..8e2837ec3 100644 --- a/include/AuxTaskRT.h +++ b/include/AuxTaskRT.h @@ -18,5 +18,6 @@ class AuxTaskRT : public SchedulableThread int commsSend(const void* buf, size_t size); ssize_t commsReceive(char* buf, size_t size); mqd_t queueDesc; + bool queueValid = false; std::string queueName; };