2020-08-13 20:53:35 +02:00
|
|
|
#include "PosixThread.h"
|
2020-12-14 21:46:33 +01:00
|
|
|
|
2021-01-09 16:20:35 +01:00
|
|
|
#include "../../serviceinterface/ServiceInterface.h"
|
2020-12-14 21:46:33 +01:00
|
|
|
|
2018-07-13 18:28:26 +02:00
|
|
|
#include <cstring>
|
|
|
|
#include <errno.h>
|
|
|
|
|
2020-06-06 15:47:33 +02:00
|
|
|
PosixThread::PosixThread(const char* name_, int priority_, size_t stackSize_):
|
|
|
|
thread(0),priority(priority_),stackSize(stackSize_) {
|
2020-06-06 14:39:08 +02:00
|
|
|
name[0] = '\0';
|
2020-06-06 18:45:55 +02:00
|
|
|
std::strncat(name, name_, PTHREAD_MAX_NAMELEN - 1);
|
2020-06-06 15:47:33 +02:00
|
|
|
}
|
2018-07-13 18:28:26 +02:00
|
|
|
|
|
|
|
PosixThread::~PosixThread() {
|
|
|
|
//No deletion and no free of Stack Pointer
|
|
|
|
}
|
|
|
|
|
|
|
|
ReturnValue_t PosixThread::sleep(uint64_t ns) {
|
|
|
|
//TODO sleep might be better with timer instead of sleep()
|
|
|
|
timespec time;
|
|
|
|
time.tv_sec = ns/1000000000;
|
|
|
|
time.tv_nsec = ns - time.tv_sec*1e9;
|
|
|
|
|
|
|
|
//Remaining Time is not set here
|
|
|
|
int status = nanosleep(&time,NULL);
|
|
|
|
if(status != 0){
|
|
|
|
switch(errno){
|
|
|
|
case EINTR:
|
|
|
|
//The nanosleep() function was interrupted by a signal.
|
|
|
|
return HasReturnvaluesIF::RETURN_FAILED;
|
|
|
|
case EINVAL:
|
2020-04-30 22:03:16 +02:00
|
|
|
//The rqtp argument specified a nanosecond value less than zero or
|
|
|
|
// greater than or equal to 1000 million.
|
2018-07-13 18:28:26 +02:00
|
|
|
return HasReturnvaluesIF::RETURN_FAILED;
|
|
|
|
default:
|
|
|
|
return HasReturnvaluesIF::RETURN_FAILED;
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|
|
|
|
return HasReturnvaluesIF::RETURN_OK;
|
|
|
|
}
|
|
|
|
|
|
|
|
void PosixThread::suspend() {
|
|
|
|
//Wait for SIGUSR1
|
|
|
|
int caughtSig = 0;
|
|
|
|
sigset_t waitSignal;
|
|
|
|
sigemptyset(&waitSignal);
|
|
|
|
sigaddset(&waitSignal, SIGUSR1);
|
|
|
|
sigwait(&waitSignal, &caughtSig);
|
|
|
|
if (caughtSig != SIGUSR1) {
|
2021-01-03 14:16:52 +01:00
|
|
|
#if FSFW_CPP_OSTREAM_ENABLED == 1
|
2020-04-30 22:03:16 +02:00
|
|
|
sif::error << "FixedTimeslotTask: Unknown Signal received: " <<
|
|
|
|
caughtSig << std::endl;
|
2021-01-03 13:58:18 +01:00
|
|
|
#endif
|
2018-07-13 18:28:26 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void PosixThread::resume(){
|
|
|
|
/* Signal the thread to start. Makes sense to call kill to start or? ;)
|
|
|
|
*
|
|
|
|
* According to Posix raise(signal) will call pthread_kill(pthread_self(), sig),
|
|
|
|
* but as the call must be done from the thread itsself this is not possible here
|
|
|
|
*/
|
|
|
|
pthread_kill(thread,SIGUSR1);
|
|
|
|
}
|
|
|
|
|
|
|
|
bool PosixThread::delayUntil(uint64_t* const prevoiusWakeTime_ms,
|
|
|
|
const uint64_t delayTime_ms) {
|
|
|
|
uint64_t nextTimeToWake_ms;
|
|
|
|
bool shouldDelay = false;
|
|
|
|
//Get current Time
|
|
|
|
const uint64_t currentTime_ms = getCurrentMonotonicTimeMs();
|
|
|
|
/* Generate the tick time at which the task wants to wake. */
|
|
|
|
nextTimeToWake_ms = (*prevoiusWakeTime_ms) + delayTime_ms;
|
|
|
|
|
|
|
|
if (currentTime_ms < *prevoiusWakeTime_ms) {
|
|
|
|
/* The tick count has overflowed since this function was
|
|
|
|
lasted called. In this case the only time we should ever
|
|
|
|
actually delay is if the wake time has also overflowed,
|
|
|
|
and the wake time is greater than the tick time. When this
|
|
|
|
is the case it is as if neither time had overflowed. */
|
|
|
|
if ((nextTimeToWake_ms < *prevoiusWakeTime_ms)
|
|
|
|
&& (nextTimeToWake_ms > currentTime_ms)) {
|
|
|
|
shouldDelay = true;
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
/* The tick time has not overflowed. In this case we will
|
|
|
|
delay if either the wake time has overflowed, and/or the
|
|
|
|
tick time is less than the wake time. */
|
|
|
|
if ((nextTimeToWake_ms < *prevoiusWakeTime_ms)
|
|
|
|
|| (nextTimeToWake_ms > currentTime_ms)) {
|
|
|
|
shouldDelay = true;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Update the wake time ready for the next call. */
|
|
|
|
|
|
|
|
(*prevoiusWakeTime_ms) = nextTimeToWake_ms;
|
|
|
|
|
|
|
|
if (shouldDelay) {
|
|
|
|
uint64_t sleepTime = nextTimeToWake_ms - currentTime_ms;
|
|
|
|
PosixThread::sleep(sleepTime * 1000000ull);
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
//We are shifting the time in case the deadline was missed like rtems
|
|
|
|
(*prevoiusWakeTime_ms) = currentTime_ms;
|
|
|
|
return false;
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
uint64_t PosixThread::getCurrentMonotonicTimeMs(){
|
|
|
|
timespec timeNow;
|
|
|
|
clock_gettime(CLOCK_MONOTONIC_RAW, &timeNow);
|
|
|
|
uint64_t currentTime_ms = (uint64_t) timeNow.tv_sec * 1000
|
|
|
|
+ timeNow.tv_nsec / 1000000;
|
|
|
|
|
|
|
|
return currentTime_ms;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
void PosixThread::createTask(void* (*fnc_)(void*), void* arg_) {
|
2021-01-03 14:16:52 +01:00
|
|
|
#if FSFW_CPP_OSTREAM_ENABLED == 1
|
2020-04-30 22:03:16 +02:00
|
|
|
//sif::debug << "PosixThread::createTask" << std::endl;
|
2021-01-03 13:58:18 +01:00
|
|
|
#endif
|
2018-07-13 18:28:26 +02:00
|
|
|
/*
|
|
|
|
* The attr argument points to a pthread_attr_t structure whose contents
|
|
|
|
are used at thread creation time to determine attributes for the new
|
|
|
|
thread; this structure is initialized using pthread_attr_init(3) and
|
|
|
|
related functions. If attr is NULL, then the thread is created with
|
|
|
|
default attributes.
|
|
|
|
*/
|
|
|
|
pthread_attr_t attributes;
|
|
|
|
int status = pthread_attr_init(&attributes);
|
|
|
|
if(status != 0){
|
2021-01-03 14:16:52 +01:00
|
|
|
#if FSFW_CPP_OSTREAM_ENABLED == 1
|
2020-04-30 22:03:16 +02:00
|
|
|
sif::error << "Posix Thread attribute init failed with: " <<
|
|
|
|
strerror(status) << std::endl;
|
2021-01-03 13:58:18 +01:00
|
|
|
#endif
|
2018-07-13 18:28:26 +02:00
|
|
|
}
|
2020-06-06 15:47:33 +02:00
|
|
|
void* stackPointer;
|
|
|
|
status = posix_memalign(&stackPointer, sysconf(_SC_PAGESIZE), stackSize);
|
2018-07-13 18:28:26 +02:00
|
|
|
if(status != 0){
|
2021-01-03 14:16:52 +01:00
|
|
|
#if FSFW_CPP_OSTREAM_ENABLED == 1
|
2020-06-06 15:47:33 +02:00
|
|
|
sif::error << "PosixThread::createTask: Stack init failed with: " <<
|
2020-04-30 22:03:16 +02:00
|
|
|
strerror(status) << std::endl;
|
2021-01-03 13:58:18 +01:00
|
|
|
#endif
|
2020-06-06 15:47:33 +02:00
|
|
|
if(errno == ENOMEM) {
|
2021-01-09 16:20:35 +01:00
|
|
|
size_t stackMb = stackSize/10e6;
|
2021-01-03 14:16:52 +01:00
|
|
|
#if FSFW_CPP_OSTREAM_ENABLED == 1
|
2020-06-06 15:47:33 +02:00
|
|
|
sif::error << "PosixThread::createTask: Insufficient memory for"
|
|
|
|
" the requested " << stackMb << " MB" << std::endl;
|
2021-01-09 16:20:35 +01:00
|
|
|
#else
|
|
|
|
fsfw::printError("PosixThread::createTask: Insufficient memory for "
|
|
|
|
"the requested %zu MB\n", stackMb);
|
2021-01-03 13:58:18 +01:00
|
|
|
#endif
|
2020-06-06 15:47:33 +02:00
|
|
|
}
|
|
|
|
else if(errno == EINVAL) {
|
2021-01-03 14:16:52 +01:00
|
|
|
#if FSFW_CPP_OSTREAM_ENABLED == 1
|
2020-06-06 15:47:33 +02:00
|
|
|
sif::error << "PosixThread::createTask: Wrong alignment argument!"
|
|
|
|
<< std::endl;
|
2021-01-09 16:20:35 +01:00
|
|
|
#else
|
|
|
|
fsfw::printError("PosixThread::createTask: "
|
|
|
|
"Wrong alignment argument!\n");
|
2021-01-03 13:58:18 +01:00
|
|
|
#endif
|
2020-06-06 15:47:33 +02:00
|
|
|
}
|
|
|
|
return;
|
2018-07-13 18:28:26 +02:00
|
|
|
}
|
|
|
|
|
2020-06-06 15:47:33 +02:00
|
|
|
status = pthread_attr_setstack(&attributes, stackPointer, stackSize);
|
2018-07-13 18:28:26 +02:00
|
|
|
if(status != 0){
|
2021-01-03 14:16:52 +01:00
|
|
|
#if FSFW_CPP_OSTREAM_ENABLED == 1
|
2020-12-14 21:46:33 +01:00
|
|
|
sif::error << "PosixThread::createTask: pthread_attr_setstack "
|
|
|
|
" failed with: " << strerror(status) << std::endl;
|
|
|
|
sif::error << "Make sure the specified stack size is valid and is "
|
|
|
|
"larger than the minimum allowed stack size." << std::endl;
|
2021-01-03 13:58:18 +01:00
|
|
|
#endif
|
2018-07-13 18:28:26 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
status = pthread_attr_setinheritsched(&attributes, PTHREAD_EXPLICIT_SCHED);
|
|
|
|
if(status != 0){
|
2021-01-03 14:16:52 +01:00
|
|
|
#if FSFW_CPP_OSTREAM_ENABLED == 1
|
2020-04-30 22:03:16 +02:00
|
|
|
sif::error << "Posix Thread attribute setinheritsched failed with: " <<
|
|
|
|
strerror(status) << std::endl;
|
2021-01-03 13:58:18 +01:00
|
|
|
#endif
|
2018-07-13 18:28:26 +02:00
|
|
|
}
|
|
|
|
|
2020-06-06 18:48:28 +02:00
|
|
|
// TODO FIFO -> This needs root privileges for the process
|
2018-07-13 18:28:26 +02:00
|
|
|
status = pthread_attr_setschedpolicy(&attributes,SCHED_FIFO);
|
|
|
|
if(status != 0){
|
2021-01-03 14:16:52 +01:00
|
|
|
#if FSFW_CPP_OSTREAM_ENABLED == 1
|
2020-04-30 22:03:16 +02:00
|
|
|
sif::error << "Posix Thread attribute schedule policy failed with: " <<
|
|
|
|
strerror(status) << std::endl;
|
2021-01-03 13:58:18 +01:00
|
|
|
#endif
|
2018-07-13 18:28:26 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
sched_param scheduleParams;
|
|
|
|
scheduleParams.__sched_priority = priority;
|
|
|
|
status = pthread_attr_setschedparam(&attributes, &scheduleParams);
|
|
|
|
if(status != 0){
|
2021-01-03 14:16:52 +01:00
|
|
|
#if FSFW_CPP_OSTREAM_ENABLED == 1
|
2020-04-30 22:03:16 +02:00
|
|
|
sif::error << "Posix Thread attribute schedule params failed with: " <<
|
|
|
|
strerror(status) << std::endl;
|
2021-01-03 13:58:18 +01:00
|
|
|
#endif
|
2018-07-13 18:28:26 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
//Set Signal Mask for suspend until startTask is called
|
|
|
|
sigset_t waitSignal;
|
|
|
|
sigemptyset(&waitSignal);
|
|
|
|
sigaddset(&waitSignal, SIGUSR1);
|
|
|
|
status = pthread_sigmask(SIG_BLOCK, &waitSignal, NULL);
|
|
|
|
if(status != 0){
|
2021-01-03 14:16:52 +01:00
|
|
|
#if FSFW_CPP_OSTREAM_ENABLED == 1
|
2020-04-30 22:03:16 +02:00
|
|
|
sif::error << "Posix Thread sigmask failed failed with: " <<
|
|
|
|
strerror(status) << " errno: " << strerror(errno) << std::endl;
|
2021-01-03 13:58:18 +01:00
|
|
|
#endif
|
2018-07-13 18:28:26 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
status = pthread_create(&thread,&attributes,fnc_,arg_);
|
|
|
|
if(status != 0){
|
2021-01-03 14:16:52 +01:00
|
|
|
#if FSFW_CPP_OSTREAM_ENABLED == 1
|
2020-04-30 22:03:16 +02:00
|
|
|
sif::error << "Posix Thread create failed with: " <<
|
|
|
|
strerror(status) << std::endl;
|
2021-01-03 13:58:18 +01:00
|
|
|
#endif
|
2018-07-13 18:28:26 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
status = pthread_setname_np(thread,name);
|
|
|
|
if(status != 0){
|
2021-01-03 14:16:52 +01:00
|
|
|
#if FSFW_CPP_OSTREAM_ENABLED == 1
|
2020-06-06 15:47:33 +02:00
|
|
|
sif::error << "PosixThread::createTask: setname failed with: " <<
|
2020-04-30 22:03:16 +02:00
|
|
|
strerror(status) << std::endl;
|
2021-01-03 13:58:18 +01:00
|
|
|
#endif
|
2020-06-06 15:47:33 +02:00
|
|
|
if(status == ERANGE) {
|
2021-01-03 14:16:52 +01:00
|
|
|
#if FSFW_CPP_OSTREAM_ENABLED == 1
|
2020-06-06 15:47:33 +02:00
|
|
|
sif::error << "PosixThread::createTask: Task name length longer"
|
|
|
|
" than 16 chars. Truncating.." << std::endl;
|
2021-01-03 13:58:18 +01:00
|
|
|
#endif
|
2020-06-06 15:47:33 +02:00
|
|
|
name[15] = '\0';
|
|
|
|
status = pthread_setname_np(thread,name);
|
|
|
|
if(status != 0){
|
2021-01-03 14:16:52 +01:00
|
|
|
#if FSFW_CPP_OSTREAM_ENABLED == 1
|
2020-06-06 15:47:33 +02:00
|
|
|
sif::error << "PosixThread::createTask: Setting name"
|
|
|
|
" did not work.." << std::endl;
|
2021-01-03 13:58:18 +01:00
|
|
|
#endif
|
2020-06-06 15:47:33 +02:00
|
|
|
}
|
|
|
|
}
|
2018-07-13 18:28:26 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
status = pthread_attr_destroy(&attributes);
|
|
|
|
if(status!=0){
|
2021-01-03 14:16:52 +01:00
|
|
|
#if FSFW_CPP_OSTREAM_ENABLED == 1
|
2020-04-30 22:03:16 +02:00
|
|
|
sif::error << "Posix Thread attribute destroy failed with: " <<
|
|
|
|
strerror(status) << std::endl;
|
2021-01-03 13:58:18 +01:00
|
|
|
#endif
|
2018-07-13 18:28:26 +02:00
|
|
|
}
|
|
|
|
}
|