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);
44 "[WARNING] %s could not determine page size %s. Skipping stack-size check.\n",
49 else if ((stack % static_cast<FwSizeType>(page_size)) != 0) {
63 if (stack <= static_cast<FwSizeType>(PTHREAD_STACK_MIN)) {
68 static_cast<FwSizeType>(PTHREAD_STACK_MIN)
70 stack =
static_cast<FwSizeType>(PTHREAD_STACK_MIN);
72 status = pthread_attr_setstacksize(&attributes, static_cast<size_t>(stack));
82 if (priority < min_priority) {
87 priority = min_priority;
90 else if (priority > max_priority) {
95 priority = max_priority;
99 status = pthread_attr_setschedpolicy(&attributes,
SCHED_POLICY);
101 status = pthread_attr_setinheritsched(&attributes, PTHREAD_EXPLICIT_SCHED);
104 sched_param schedParam;
105 memset(&schedParam, 0,
sizeof(sched_param));
106 schedParam.sched_priority =
static_cast<PlatformIntType
>(priority);
107 status = pthread_attr_setschedparam(&attributes, &schedParam);
113 PlatformIntType status = 0;
117 #if defined(TGT_OS_TYPE_LINUX) && defined(__GLIBC__) && defined(_GNU_SOURCE) 121 CPU_SET(static_cast<PlatformIntType>(affinity), &cpu_set);
124 status = pthread_attr_setaffinity_np(&attributes,
sizeof(cpu_set_t), &cpu_set);
127 Fw::Logger::log(
"[WARNING] %s setting CPU affinity is only available with GNU pthreads\n",
135 PosixTaskHandle& handle = this->m_handle;
138 pthread_attr_t attributes;
139 memset(&attributes, 0,
sizeof(attributes));
140 pthread_status = pthread_attr_init(&attributes);
156 handle.m_is_valid =
true;
159 (void) pthread_attr_destroy(&attributes);
169 Os::Task::Status status = this->create(arguments, PermissionExpectation::EXPECT_PERMISSION);
171 if (status == Os::Task::Status::ERROR_PERMISSION) {
172 if (not PosixTask::s_permissions_reported) {
176 Fw::Logger::log(
"[NOTE] You have insufficient permissions to create a task with priority and/or cpu affinity.\n");
177 Fw::Logger::log(
"[NOTE] A task without priority and affinity will be created.\n");
180 Fw::Logger::log(
"[NOTE] 1. Use tasks without priority and affinity using parameterless start()\n");
181 Fw::Logger::log(
"[NOTE] 2. Run this executable as a user with task priority permission\n");
182 Fw::Logger::log(
"[NOTE] 3. Grant capability with \"setcap 'cap_sys_nice=eip'\" or equivalent\n");
184 PosixTask::s_permissions_reported =
true;
187 status = this->create(arguments, PermissionExpectation::EXPECT_NO_PERMISSION);
190 static_cast<PlatformIntType>(status));
198 status = Os::Task::Status::INVALID_HANDLE;
200 PlatformIntType stat = ::pthread_join(this->m_handle.
m_task_descriptor,
nullptr);
207 return &this->m_handle;
223 timespec sleep_interval;
224 sleep_interval.tv_sec = interval.
getSeconds();
225 sleep_interval.tv_nsec = interval.
getUSeconds() * 1000;
227 timespec remaining_interval;
228 remaining_interval.tv_sec = 0;
229 remaining_interval.tv_nsec = 0;
232 PlatformIntType status = nanosleep(&sleep_interval, &remaining_interval);
238 else if (EINTR == errno) {
239 sleep_interval = remaining_interval;
244 task_status = Os::Task::Status::DELAY_ERROR;
static constexpr FwSizeType TASK_DEFAULT
Task handle representation.
PlatformSizeType FwSizeType
static constexpr PlatformIntType SUCCESS
const char * toChar() const
PlatformIntType set_cpu_affinity(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
Status _delay(Fw::TimeInterval interval) override
delay the current task
PlatformIntType set_priority_params(pthread_attr_t &attributes, const Os::Task::Arguments &arguments)
void * m_routine_argument
static const PlatformIntType SCHED_POLICY
Task::Status posix_status_to_task_status(PlatformIntType 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)
Expect that you hold necessary permissions.
Status join() override
block until the task has ended
PlatformIntType set_stack_size(pthread_attr_t &attributes, const Os::Task::Arguments &arguments)
Status start(const Arguments &arguments) override
start the task
void resume() override
resume a suspended task
void onStart() override
perform required task start actions