20 std::atomic<bool> PosixTask::s_permissions_reported(
false);
23 typedef void* (*pthread_func_ptr)(
void*);
28 wrapper.
run(&wrapper);
38 long page_size = sysconf(_SC_PAGESIZE);
43 Fw::Logger::log(
"[WARNING] %s could not determine page size %s. Skipping stack-size check.\n",
44 const_cast<CHAR*>(arguments.
m_name.
toChar()), strerror(errno));
45 }
else if ((stack % static_cast<FwSizeType>(page_size)) != 0) {
49 " is not multiple of page size %ld, rounding to %" PRI_FwSizeType "\n",
50 const_cast<CHAR*>(arguments.
m_name.
toChar()), stack, page_size, rounded);
55 if (stack <= static_cast<FwSizeType>(PTHREAD_STACK_MIN)) {
58 const_cast<CHAR*>(arguments.
m_name.
toChar()), stack, static_cast<FwSizeType>(PTHREAD_STACK_MIN));
59 stack =
static_cast<FwSizeType>(PTHREAD_STACK_MIN);
61 status = pthread_attr_setstacksize(&attributes, static_cast<size_t>(stack));
71 if (priority < min_priority) {
73 const_cast<CHAR*>(arguments.
m_name.
toChar()), priority, min_priority);
74 priority = min_priority;
77 else if (priority > max_priority) {
79 const_cast<CHAR*>(arguments.
m_name.
toChar()), priority, max_priority);
80 priority = max_priority;
84 status = pthread_attr_setschedpolicy(&attributes,
SCHED_POLICY);
86 status = pthread_attr_setinheritsched(&attributes, PTHREAD_EXPLICIT_SCHED);
89 sched_param schedParam;
90 memset(&schedParam, 0,
sizeof(sched_param));
91 schedParam.sched_priority =
static_cast<int>(priority);
92 status = pthread_attr_setschedparam(&attributes, &schedParam);
102 #if defined(TGT_OS_TYPE_LINUX) && defined(__GLIBC__) && defined(_GNU_SOURCE) 106 CPU_SET(static_cast<int>(affinity), &cpu_set);
109 status = pthread_attr_setaffinity_np(&attributes,
sizeof(cpu_set_t), &cpu_set);
112 Fw::Logger::log(
"[WARNING] %s setting CPU affinity is only available with GNU pthreads\n",
121 PosixTaskHandle& handle = this->m_handle;
124 pthread_attr_t attributes;
125 memset(&attributes, 0,
sizeof(attributes));
126 pthread_status = pthread_attr_init(&attributes);
145 handle.m_is_valid =
true;
148 (void)pthread_attr_destroy(&attributes);
158 Os::Task::Status status = this->create(arguments, PermissionExpectation::EXPECT_PERMISSION);
160 if (status == Os::Task::Status::ERROR_PERMISSION) {
161 if (not PosixTask::s_permissions_reported) {
166 "[NOTE] You have insufficient permissions to create a task with priority and/or cpu affinity.\n");
167 Fw::Logger::log(
"[NOTE] A task without priority and affinity will be created.\n");
170 Fw::Logger::log(
"[NOTE] 1. Use tasks without priority and affinity using parameterless start()\n");
171 Fw::Logger::log(
"[NOTE] 2. Run this executable as a user with task priority permission\n");
172 Fw::Logger::log(
"[NOTE] 3. Grant capability with \"setcap 'cap_sys_nice=eip'\" or equivalent\n");
174 PosixTask::s_permissions_reported =
true;
177 status = this->create(arguments, PermissionExpectation::EXPECT_NO_PERMISSION);
179 Fw::Logger::log(
"[ERROR] Failed to create task with status: %d", static_cast<int>(status));
187 status = Os::Task::Status::INVALID_HANDLE;
196 return &this->m_handle;
211 timespec sleep_interval;
212 sleep_interval.tv_sec = interval.
getSeconds();
213 sleep_interval.tv_nsec = interval.
getUSeconds() * 1000;
215 timespec remaining_interval;
216 remaining_interval.tv_sec = 0;
217 remaining_interval.tv_nsec = 0;
220 int status = nanosleep(&sleep_interval, &remaining_interval);
226 else if (EINTR == errno) {
227 sleep_interval = remaining_interval;
232 task_status = Os::Task::Status::DELAY_ERROR;
static constexpr FwSizeType TASK_DEFAULT
Task handle representation.
PlatformSizeType FwSizeType
const char * toChar() const
int set_priority_params(pthread_attr_t &attributes, const Os::Task::Arguments &arguments)
void suspend(SuspensionType suspensionType) override
suspend the task given the suspension type
static void log(const char *format,...)
log a formated string with supplied arguments
static constexpr int SUCCESS
Status _delay(Fw::TimeInterval interval) override
delay the current task
void * m_routine_argument
static constexpr FwTaskPriorityType TASK_PRIORITY_DEFAULT
Task::Status posix_status_to_task_status(int posix_status)
message sent/received okay
bool m_is_valid
Is the above descriptor valid.
PermissionExpectation
Enumeration of permission expectations.
pthread_t m_task_descriptor
Posix task descriptor.
TaskHandle * getHandle() override
return the underlying task handle (implementation specific)
static void run(void *task_pointer)
run the task routine wrapper
Wrapper for task routine that ensures onStart() is called once the task actually begins.
const Os::TaskString m_name
void * pthread_entry_wrapper(void *wrapper_pointer)
int set_stack_size(pthread_attr_t &attributes, const Os::Task::Arguments &arguments)
static const int SCHED_POLICY
Expect that you hold necessary permissions.
Status join() override
block until the task has ended
Status start(const Arguments &arguments) override
start the task
FwTaskPriorityType m_priority
int set_cpu_affinity(pthread_attr_t &attributes, const Os::Task::Arguments &arguments)
void resume() override
resume a suspended task
void onStart() override
perform required task start actions