put linux prinouts in namespace

This commit is contained in:
Robin Müller 2020-04-30 22:03:16 +02:00
parent 2c6b446500
commit e39d669ed8
7 changed files with 76 additions and 42 deletions

View File

@ -9,7 +9,9 @@
uint32_t FixedTimeslotTask::deadlineMissedCount = 0; uint32_t FixedTimeslotTask::deadlineMissedCount = 0;
const size_t PeriodicTaskIF::MINIMUM_STACK_SIZE = PTHREAD_STACK_MIN; const size_t PeriodicTaskIF::MINIMUM_STACK_SIZE = PTHREAD_STACK_MIN;
FixedTimeslotTask::FixedTimeslotTask(const char* name_, int priority_, size_t stackSize_, uint32_t periodMs_):PosixThread(name_,priority_,stackSize_),pst(periodMs_),started(false) { FixedTimeslotTask::FixedTimeslotTask(const char* name_, int priority_,
size_t stackSize_, uint32_t periodMs_):
PosixThread(name_,priority_,stackSize_),pst(periodMs_),started(false) {
} }
FixedTimeslotTask::~FixedTimeslotTask() { FixedTimeslotTask::~FixedTimeslotTask() {
@ -40,6 +42,12 @@ uint32_t FixedTimeslotTask::getPeriodMs() const {
ReturnValue_t FixedTimeslotTask::addSlot(object_id_t componentId, ReturnValue_t FixedTimeslotTask::addSlot(object_id_t componentId,
uint32_t slotTimeMs, int8_t executionStep) { uint32_t slotTimeMs, int8_t executionStep) {
if (!objectManager->get<ExecutableObjectIF>(componentId)) {
sif::error << "Component " << std::hex << componentId
<< " not found, not adding it to pst" << std::endl;
return HasReturnvaluesIF::RETURN_FAILED;
}
pst.addSlot(componentId, slotTimeMs, executionStep, this); pst.addSlot(componentId, slotTimeMs, executionStep, this);
return HasReturnvaluesIF::RETURN_OK; return HasReturnvaluesIF::RETURN_OK;
} }
@ -80,7 +88,7 @@ void FixedTimeslotTask::taskFunctionality() {
void FixedTimeslotTask::missedDeadlineCounter() { void FixedTimeslotTask::missedDeadlineCounter() {
FixedTimeslotTask::deadlineMissedCount++; FixedTimeslotTask::deadlineMissedCount++;
if (FixedTimeslotTask::deadlineMissedCount % 10 == 0) { if (FixedTimeslotTask::deadlineMissedCount % 10 == 0) {
error << "PST missed " << FixedTimeslotTask::deadlineMissedCount sif::error << "PST missed " << FixedTimeslotTask::deadlineMissedCount
<< " deadlines." << std::endl; << " deadlines." << std::endl;
} }
} }

View File

@ -17,11 +17,11 @@ MessageQueue::MessageQueue(size_t message_depth, size_t max_message_size) :
attributes.mq_maxmsg = message_depth; attributes.mq_maxmsg = message_depth;
attributes.mq_msgsize = max_message_size; attributes.mq_msgsize = max_message_size;
attributes.mq_flags = 0; //Flags are ignored on Linux during mq_open attributes.mq_flags = 0; //Flags are ignored on Linux during mq_open
//Set the name of the queue //Set the name of the queue
sprintf(name, "/Q%u\n", queueCounter++); sprintf(name, "/Q%u\n", queueCounter++);
//Create a nonblocking queue if the name is available (the queue is Read and writable for the owner as well as the group) //Create a nonblocking queue if the name is available (the queue is Read and
// writable for the owner as well as the group)
mqd_t tempId = mq_open(name, O_NONBLOCK | O_RDWR | O_CREAT | O_EXCL, mqd_t tempId = mq_open(name, O_NONBLOCK | O_RDWR | O_CREAT | O_EXCL,
S_IWUSR | S_IREAD | S_IWGRP | S_IRGRP | S_IROTH | S_IWOTH, &attributes); S_IWUSR | S_IREAD | S_IWGRP | S_IRGRP | S_IROTH | S_IWOTH, &attributes);
if (tempId == -1) { if (tempId == -1) {
@ -32,8 +32,8 @@ MessageQueue::MessageQueue(size_t message_depth, size_t max_message_size) :
//We unlink the other queue //We unlink the other queue
int status = mq_unlink(name); int status = mq_unlink(name);
if (status != 0) { if (status != 0) {
error << "mq_unlink Failed with status: " << strerror(errno) sif::error << "mq_unlink Failed with status: " << strerror(errno)
<< std::endl; << std::endl;
} else { } else {
//Successful unlinking, try to open again //Successful unlinking, try to open again
mqd_t tempId = mq_open(name, mqd_t tempId = mq_open(name,
@ -47,7 +47,7 @@ MessageQueue::MessageQueue(size_t message_depth, size_t max_message_size) :
} }
} }
//Failed either the first time or the second time //Failed either the first time or the second time
error << "MessageQueue::MessageQueue: Creating Queue " << std::hex sif::error << "MessageQueue::MessageQueue: Creating Queue " << std::hex
<< name << std::dec << " failed with status: " << name << std::dec << " failed with status: "
<< strerror(errno) << std::endl; << strerror(errno) << std::endl;
} else { } else {
@ -59,11 +59,13 @@ MessageQueue::MessageQueue(size_t message_depth, size_t max_message_size) :
MessageQueue::~MessageQueue() { MessageQueue::~MessageQueue() {
int status = mq_close(this->id); int status = mq_close(this->id);
if(status != 0){ if(status != 0){
error << "MessageQueue::Destructor: mq_close Failed with status: " << strerror(errno) <<std::endl; sif::error << "MessageQueue::Destructor: mq_close Failed with status: "
<< strerror(errno) <<std::endl;
} }
status = mq_unlink(name); status = mq_unlink(name);
if(status != 0){ if(status != 0){
error << "MessageQueue::Destructor: mq_unlink Failed with status: " << strerror(errno) <<std::endl; sif::error << "MessageQueue::Destructor: mq_unlink Failed with status: "
<< strerror(errno) <<std::endl;
} }
} }
@ -93,7 +95,8 @@ ReturnValue_t MessageQueue::receiveMessage(MessageQueueMessage* message,
ReturnValue_t MessageQueue::receiveMessage(MessageQueueMessage* message) { ReturnValue_t MessageQueue::receiveMessage(MessageQueueMessage* message) {
unsigned int messagePriority = 0; unsigned int messagePriority = 0;
int status = mq_receive(id,reinterpret_cast<char*>(message->getBuffer()),message->MAX_MESSAGE_SIZE,&messagePriority); int status = mq_receive(id,reinterpret_cast<char*>(message->getBuffer()),
message->MAX_MESSAGE_SIZE,&messagePriority);
if (status > 0) { if (status > 0) {
this->lastPartner = message->getSender(); this->lastPartner = message->getSender();
//Check size of incoming message. //Check size of incoming message.
@ -114,7 +117,8 @@ ReturnValue_t MessageQueue::receiveMessage(MessageQueueMessage* message) {
return MessageQueueIF::EMPTY; return MessageQueueIF::EMPTY;
case EBADF: case EBADF:
//mqdes doesn't represent a valid queue open for reading. //mqdes doesn't represent a valid queue open for reading.
error << "MessageQueue::receive: configuration error " << strerror(errno) << std::endl; sif::error << "MessageQueue::receive: configuration error "
<< strerror(errno) << std::endl;
/*NO BREAK*/ /*NO BREAK*/
case EINVAL: case EINVAL:
/* /*
@ -123,7 +127,8 @@ ReturnValue_t MessageQueue::receiveMessage(MessageQueueMessage* message) {
* * The number of bytes requested, msg_len is less than zero. * * The number of bytes requested, msg_len is less than zero.
* * msg_len is anything other than the mq_msgsize of the specified queue, and the QNX extended option MQ_READBUF_DYNAMIC hasn't been set in the queue's mq_flags. * * msg_len is anything other than the mq_msgsize of the specified queue, and the QNX extended option MQ_READBUF_DYNAMIC hasn't been set in the queue's mq_flags.
*/ */
error << "MessageQueue::receive: configuration error " << strerror(errno) << std::endl; sif::error << "MessageQueue::receive: configuration error "
<< strerror(errno) << std::endl;
/*NO BREAK*/ /*NO BREAK*/
case EMSGSIZE: case EMSGSIZE:
/* /*
@ -131,7 +136,8 @@ ReturnValue_t MessageQueue::receiveMessage(MessageQueueMessage* message) {
* * the QNX extended option MQ_READBUF_DYNAMIC hasn't been set, and the given msg_len is shorter than the mq_msgsize for the given queue. * * the QNX extended option MQ_READBUF_DYNAMIC hasn't been set, and the given msg_len is shorter than the mq_msgsize for the given queue.
* * the extended option MQ_READBUF_DYNAMIC has been set, but the given msg_len is too short for the message that would have been received. * * the extended option MQ_READBUF_DYNAMIC has been set, but the given msg_len is too short for the message that would have been received.
*/ */
error << "MessageQueue::receive: configuration error " << strerror(errno) << std::endl; sif::error << "MessageQueue::receive: configuration error "
<< strerror(errno) << std::endl;
/*NO BREAK*/ /*NO BREAK*/
case EINTR: case EINTR:
//The operation was interrupted by a signal. //The operation was interrupted by a signal.
@ -154,7 +160,8 @@ ReturnValue_t MessageQueue::flush(uint32_t* count) {
switch(errno){ switch(errno){
case EBADF: case EBADF:
//mqdes doesn't represent a valid message queue. //mqdes doesn't represent a valid message queue.
error << "MessageQueue::flush configuration error, called flush with an invalid queue ID" << std::endl; sif::error << "MessageQueue::flush configuration error, "
"called flush with an invalid queue ID" << std::endl;
/*NO BREAK*/ /*NO BREAK*/
case EINVAL: case EINVAL:
//mq_attr is NULL //mq_attr is NULL
@ -169,7 +176,8 @@ ReturnValue_t MessageQueue::flush(uint32_t* count) {
switch(errno){ switch(errno){
case EBADF: case EBADF:
//mqdes doesn't represent a valid message queue. //mqdes doesn't represent a valid message queue.
error << "MessageQueue::flush configuration error, called flush with an invalid queue ID" << std::endl; sif::error << "MessageQueue::flush configuration error, "
"called flush with an invalid queue ID" << std::endl;
/*NO BREAK*/ /*NO BREAK*/
case EINVAL: case EINVAL:
/* /*
@ -237,7 +245,9 @@ ReturnValue_t MessageQueue::sendMessageFromMessageQueue(MessageQueueId_t sendTo,
return MessageQueueIF::FULL; return MessageQueueIF::FULL;
case EBADF: case EBADF:
//mq_des doesn't represent a valid message queue descriptor, or mq_des wasn't opened for writing. //mq_des doesn't represent a valid message queue descriptor, or mq_des wasn't opened for writing.
error << "MessageQueue::sendMessage: Configuration error " << strerror(errno) << " in mq_send mqSendTo: " << sendTo << " sent from " << sentFrom << std::endl; sif::error << "MessageQueue::sendMessage: Configuration error "
<< strerror(errno) << " in mq_send mqSendTo: " << sendTo
<< " sent from " << sentFrom << std::endl;
/*NO BREAK*/ /*NO BREAK*/
case EINTR: case EINTR:
//The call was interrupted by a signal. //The call was interrupted by a signal.
@ -248,9 +258,11 @@ ReturnValue_t MessageQueue::sendMessageFromMessageQueue(MessageQueueId_t sendTo,
* * msg_len is negative. * * msg_len is negative.
* * msg_prio is greater than MQ_PRIO_MAX. * * msg_prio is greater than MQ_PRIO_MAX.
* * msg_prio is less than 0. * * msg_prio is less than 0.
* * MQ_PRIO_RESTRICT is set in the mq_attr of mq_des, and msg_prio is greater than the priority of the calling process. * * MQ_PRIO_RESTRICT is set in the mq_attr of mq_des,
* and msg_prio is greater than the priority of the calling process.
* */ * */
error << "MessageQueue::sendMessage: Configuration error " << strerror(errno) << " in mq_send" << std::endl; sif::error << "MessageQueue::sendMessage: Configuration error "
<< strerror(errno) << " in mq_send" << std::endl;
/*NO BREAK*/ /*NO BREAK*/
case EMSGSIZE: case EMSGSIZE:
//The msg_len is greater than the msgsize associated with the specified queue. //The msg_len is greater than the msgsize associated with the specified queue.

View File

@ -13,22 +13,22 @@ Mutex::Mutex() {
pthread_mutexattr_t mutexAttr; pthread_mutexattr_t mutexAttr;
int status = pthread_mutexattr_init(&mutexAttr); int status = pthread_mutexattr_init(&mutexAttr);
if (status != 0) { if (status != 0) {
error << "Mutex: Attribute init failed with: " << strerror(status) << std::endl; sif::error << "Mutex: Attribute init failed with: " << strerror(status) << std::endl;
} }
status = pthread_mutexattr_setprotocol(&mutexAttr, PTHREAD_PRIO_INHERIT); status = pthread_mutexattr_setprotocol(&mutexAttr, PTHREAD_PRIO_INHERIT);
if (status != 0) { if (status != 0) {
error << "Mutex: Attribute set PRIO_INHERIT failed with: " << strerror(status) sif::error << "Mutex: Attribute set PRIO_INHERIT failed with: " << strerror(status)
<< std::endl; << std::endl;
} }
status = pthread_mutex_init(&mutex, &mutexAttr); status = pthread_mutex_init(&mutex, &mutexAttr);
if (status != 0) { if (status != 0) {
error << "Mutex: creation with name, id " << mutex.__data.__count sif::error << "Mutex: creation with name, id " << mutex.__data.__count
<< ", " << " failed with " << strerror(status) << std::endl; << ", " << " failed with " << strerror(status) << std::endl;
} }
//After a mutex attributes object has been used to initialize one or more mutexes, any function affecting the attributes object (including destruction) shall not affect any previously initialized mutexes. //After a mutex attributes object has been used to initialize one or more mutexes, any function affecting the attributes object (including destruction) shall not affect any previously initialized mutexes.
status = pthread_mutexattr_destroy(&mutexAttr); status = pthread_mutexattr_destroy(&mutexAttr);
if (status != 0) { if (status != 0) {
error << "Mutex: Attribute destroy failed with " << strerror(status) << std::endl; sif::error << "Mutex: Attribute destroy failed with " << strerror(status) << std::endl;
} }
} }

View File

@ -56,9 +56,9 @@ void PeriodicPosixTask::taskFunctionality(void){
char name[20] = {0}; char name[20] = {0};
int status = pthread_getname_np(pthread_self(),name,sizeof(name)); int status = pthread_getname_np(pthread_self(),name,sizeof(name));
if(status==0){ if(status==0){
error << "ObjectTask: " << name << " Deadline missed." << std::endl; sif::error << "ObjectTask: " << name << " Deadline missed." << std::endl;
}else{ }else{
error << "ObjectTask: X Deadline missed. " << status << std::endl; sif::error << "ObjectTask: X Deadline missed. " << status << std::endl;
} }
if (this->deadlineMissedFunc != NULL) { if (this->deadlineMissedFunc != NULL) {
this->deadlineMissedFunc(); this->deadlineMissedFunc();

View File

@ -9,7 +9,8 @@
class PeriodicPosixTask: public PosixThread, public PeriodicTaskIF { class PeriodicPosixTask: public PosixThread, public PeriodicTaskIF {
public: public:
PeriodicPosixTask(const char* name_, int priority_, size_t stackSize_, uint32_t period_, void(*deadlineMissedFunc_)()); PeriodicPosixTask(const char* name_, int priority_, size_t stackSize_,
uint32_t period_, void(*deadlineMissedFunc_)());
virtual ~PeriodicPosixTask(); virtual ~PeriodicPosixTask();
/** /**
* @brief The method to start the task. * @brief The method to start the task.

View File

@ -22,7 +22,8 @@ ReturnValue_t PosixThread::sleep(uint64_t ns) {
//The nanosleep() function was interrupted by a signal. //The nanosleep() function was interrupted by a signal.
return HasReturnvaluesIF::RETURN_FAILED; return HasReturnvaluesIF::RETURN_FAILED;
case EINVAL: case EINVAL:
//The rqtp argument specified a nanosecond value less than zero or greater than or equal to 1000 million. //The rqtp argument specified a nanosecond value less than zero or
// greater than or equal to 1000 million.
return HasReturnvaluesIF::RETURN_FAILED; return HasReturnvaluesIF::RETURN_FAILED;
default: default:
return HasReturnvaluesIF::RETURN_FAILED; return HasReturnvaluesIF::RETURN_FAILED;
@ -40,8 +41,8 @@ void PosixThread::suspend() {
sigaddset(&waitSignal, SIGUSR1); sigaddset(&waitSignal, SIGUSR1);
sigwait(&waitSignal, &caughtSig); sigwait(&waitSignal, &caughtSig);
if (caughtSig != SIGUSR1) { if (caughtSig != SIGUSR1) {
error << "FixedTimeslotTask: Unknown Signal received: " << caughtSig sif::error << "FixedTimeslotTask: Unknown Signal received: " <<
<< std::endl; caughtSig << std::endl;
} }
} }
@ -112,14 +113,15 @@ uint64_t PosixThread::getCurrentMonotonicTimeMs(){
return currentTime_ms; return currentTime_ms;
} }
PosixThread::PosixThread(const char* name_, int priority_, size_t stackSize_):thread(0),priority(priority_),stackSize(stackSize_) { PosixThread::PosixThread(const char* name_, int priority_, size_t stackSize_):
thread(0),priority(priority_),stackSize(stackSize_) {
strcpy(name,name_); strcpy(name,name_);
} }
void PosixThread::createTask(void* (*fnc_)(void*), void* arg_) { void PosixThread::createTask(void* (*fnc_)(void*), void* arg_) {
debug << "PosixThread::createTask" << std::endl; //sif::debug << "PosixThread::createTask" << std::endl;
/* /*
* The attr argument points to a pthread_attr_t structure whose contents * The attr argument points to a pthread_attr_t structure whose contents
are used at thread creation time to determine attributes for the new are used at thread creation time to determine attributes for the new
@ -130,35 +132,41 @@ void PosixThread::createTask(void* (*fnc_)(void*), void* arg_) {
pthread_attr_t attributes; pthread_attr_t attributes;
int status = pthread_attr_init(&attributes); int status = pthread_attr_init(&attributes);
if(status != 0){ if(status != 0){
error << "Posix Thread attribute init failed with: " << strerror(status) << std::endl; sif::error << "Posix Thread attribute init failed with: " <<
strerror(status) << std::endl;
} }
void* sp; void* sp;
status = posix_memalign(&sp, sysconf(_SC_PAGESIZE), stackSize); status = posix_memalign(&sp, sysconf(_SC_PAGESIZE), stackSize);
if(status != 0){ if(status != 0){
error << "Posix Thread stack init failed with: " << strerror(status) << std::endl; sif::error << "Posix Thread stack init failed with: " <<
strerror(status) << std::endl;
} }
status = pthread_attr_setstack(&attributes, sp, stackSize); status = pthread_attr_setstack(&attributes, sp, stackSize);
if(status != 0){ if(status != 0){
error << "Posix Thread attribute setStack failed with: " << strerror(status) << std::endl; sif::error << "Posix Thread attribute setStack failed with: " <<
strerror(status) << std::endl;
} }
status = pthread_attr_setinheritsched(&attributes, PTHREAD_EXPLICIT_SCHED); status = pthread_attr_setinheritsched(&attributes, PTHREAD_EXPLICIT_SCHED);
if(status != 0){ if(status != 0){
error << "Posix Thread attribute setinheritsched failed with: " << strerror(status) << std::endl; sif::error << "Posix Thread attribute setinheritsched failed with: " <<
strerror(status) << std::endl;
} }
//TODO FIFO -> This needs root privileges for the process //TODO FIFO -> This needs root privileges for the process
status = pthread_attr_setschedpolicy(&attributes,SCHED_FIFO); status = pthread_attr_setschedpolicy(&attributes,SCHED_FIFO);
if(status != 0){ if(status != 0){
error << "Posix Thread attribute schedule policy failed with: " << strerror(status) << std::endl; sif::error << "Posix Thread attribute schedule policy failed with: " <<
strerror(status) << std::endl;
} }
sched_param scheduleParams; sched_param scheduleParams;
scheduleParams.__sched_priority = priority; scheduleParams.__sched_priority = priority;
status = pthread_attr_setschedparam(&attributes, &scheduleParams); status = pthread_attr_setschedparam(&attributes, &scheduleParams);
if(status != 0){ if(status != 0){
error << "Posix Thread attribute schedule params failed with: " << strerror(status) << std::endl; sif::error << "Posix Thread attribute schedule params failed with: " <<
strerror(status) << std::endl;
} }
//Set Signal Mask for suspend until startTask is called //Set Signal Mask for suspend until startTask is called
@ -167,22 +175,26 @@ void PosixThread::createTask(void* (*fnc_)(void*), void* arg_) {
sigaddset(&waitSignal, SIGUSR1); sigaddset(&waitSignal, SIGUSR1);
status = pthread_sigmask(SIG_BLOCK, &waitSignal, NULL); status = pthread_sigmask(SIG_BLOCK, &waitSignal, NULL);
if(status != 0){ if(status != 0){
error << "Posix Thread sigmask failed failed with: " << strerror(status) << " errno: " << strerror(errno) << std::endl; sif::error << "Posix Thread sigmask failed failed with: " <<
strerror(status) << " errno: " << strerror(errno) << std::endl;
} }
status = pthread_create(&thread,&attributes,fnc_,arg_); status = pthread_create(&thread,&attributes,fnc_,arg_);
if(status != 0){ if(status != 0){
error << "Posix Thread create failed with: " << strerror(status) << std::endl; sif::error << "Posix Thread create failed with: " <<
strerror(status) << std::endl;
} }
status = pthread_setname_np(thread,name); status = pthread_setname_np(thread,name);
if(status != 0){ if(status != 0){
error << "Posix Thread setname failed with: " << strerror(status) << std::endl; sif::error << "Posix Thread setname failed with: " <<
strerror(status) << std::endl;
} }
status = pthread_attr_destroy(&attributes); status = pthread_attr_destroy(&attributes);
if(status!=0){ if(status!=0){
error << "Posix Thread attribute destroy failed with: " << strerror(status) << std::endl; sif::error << "Posix Thread attribute destroy failed with: " <<
strerror(status) << std::endl;
} }
} }

View File

@ -9,7 +9,8 @@ Timer::Timer() {
sigEvent.sigev_value.sival_ptr = &timerId; sigEvent.sigev_value.sival_ptr = &timerId;
int status = timer_create(CLOCK_MONOTONIC, &sigEvent, &timerId); int status = timer_create(CLOCK_MONOTONIC, &sigEvent, &timerId);
if(status!=0){ if(status!=0){
error << "Timer creation failed with: " << status << " errno: " << errno << std::endl; sif::error << "Timer creation failed with: " << status <<
" errno: " << errno << std::endl;
} }
} }