21 std::atomic<bool> PosixTask::s_permissions_reported(
false);
24 typedef void* (*pthread_func_ptr)(
void*);
33 #if defined(POSIX_THREADS_ENABLE_NAMES) && POSIX_THREADS_ENABLE_NAMES 37 (void)
set_task_name(handle->m_task_descriptor, handle->m_name);
39 wrapper.
run(&wrapper);
49 long page_size = sysconf(_SC_PAGESIZE);
54 Fw::Logger::log(
"[WARNING] %s could not determine page size %s. Skipping stack-size check.\n",
55 const_cast<CHAR*>(arguments.
m_name.
toChar()), strerror(errno));
56 }
else if ((stack % static_cast<FwSizeType>(page_size)) != 0) {
60 " is not multiple of page size %ld, rounding to %" PRI_FwSizeType "\n",
61 const_cast<CHAR*>(arguments.
m_name.
toChar()), stack, page_size, rounded);
66 if (stack <= static_cast<FwSizeType>(PTHREAD_STACK_MIN)) {
69 const_cast<CHAR*>(arguments.
m_name.
toChar()), stack, 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) {
84 const_cast<CHAR*>(arguments.
m_name.
toChar()), priority, min_priority);
85 priority = min_priority;
88 else if (priority > max_priority) {
90 const_cast<CHAR*>(arguments.
m_name.
toChar()), priority, max_priority);
91 priority = max_priority;
95 status = pthread_attr_setschedpolicy(&attributes,
SCHED_POLICY);
97 status = pthread_attr_setinheritsched(&attributes, PTHREAD_EXPLICIT_SCHED);
100 sched_param schedParam;
101 memset(&schedParam, 0,
sizeof(sched_param));
102 schedParam.sched_priority =
static_cast<int>(priority);
103 status = pthread_attr_setschedparam(&attributes, &schedParam);
113 #if defined(TGT_OS_TYPE_LINUX) && defined(__GLIBC__) && defined(_GNU_SOURCE) 117 CPU_SET(static_cast<int>(affinity), &cpu_set);
120 status = pthread_attr_setaffinity_np(&attributes,
sizeof(cpu_set_t), &cpu_set);
123 Fw::Logger::log(
"[WARNING] %s setting CPU affinity is only available with GNU pthreads\n",
134 #if defined(TGT_OS_TYPE_LINUX) && defined(__GLIBC__) && defined(_GNU_SOURCE) && defined(POSIX_THREADS_ENABLE_NAMES) && \ 135 POSIX_THREADS_ENABLE_NAMES 138 status = pthread_setname_np(thread, name);
146 PosixTaskHandle& handle = this->m_handle;
149 pthread_attr_t attributes;
150 memset(&attributes, 0,
sizeof(attributes));
151 pthread_status = pthread_attr_init(&attributes);
170 handle.m_is_valid =
true;
173 #if defined(POSIX_THREADS_ENABLE_NAMES) && POSIX_THREADS_ENABLE_NAMES 177 (void)pthread_attr_destroy(&attributes);
187 Os::Task::Status status = this->create(arguments, PermissionExpectation::EXPECT_PERMISSION);
189 if (status == Os::Task::Status::ERROR_PERMISSION) {
190 if (not PosixTask::s_permissions_reported) {
195 "[NOTE] You have insufficient permissions to create a task with priority and/or cpu affinity.\n");
196 Fw::Logger::log(
"[NOTE] A task without priority and affinity will be created.\n");
199 Fw::Logger::log(
"[NOTE] 1. Use tasks without priority and affinity using parameterless start()\n");
200 Fw::Logger::log(
"[NOTE] 2. Run this executable as a user with task priority permission\n");
201 Fw::Logger::log(
"[NOTE] 3. Grant capability with \"setcap 'cap_sys_nice=eip'\" or equivalent\n");
203 PosixTask::s_permissions_reported =
true;
206 status = this->create(arguments, PermissionExpectation::EXPECT_NO_PERMISSION);
208 Fw::Logger::log(
"[ERROR] Failed to create task with status: %d", static_cast<int>(status));
216 status = Os::Task::Status::INVALID_HANDLE;
225 return &this->m_handle;
240 timespec sleep_interval;
241 sleep_interval.tv_sec = interval.
getSeconds();
242 sleep_interval.tv_nsec = interval.
getUSeconds() * 1000;
244 timespec remaining_interval;
245 remaining_interval.tv_sec = 0;
246 remaining_interval.tv_nsec = 0;
249 int status = nanosleep(&sleep_interval, &remaining_interval);
255 else if (EINTR == errno) {
256 sleep_interval = remaining_interval;
261 task_status = Os::Task::Status::DELAY_ERROR;
static constexpr FwSizeType TASK_DEFAULT
TaskHandle * getHandle() override
return the underlying task handle (implementation specific)
Task handle representation.
PlatformSizeType FwSizeType
const char * toChar() const
Convert to a C-style char*.
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.
char * string_copy(char *destination, const char *source, FwSizeType num)
copy string with null-termination guaranteed
TaskHandle * getHandle() override
return the underlying task handle (implementation specific)
static void run(void *task_pointer)
run the task routine wrapper
static constexpr FwSizeType PTHREAD_NAME_LENGTH
Length of pthread name.
Wrapper for task routine that ensures onStart() is called once the task actually begins.
Task & m_task
Reference to owning task.
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
int set_task_name(pthread_t thread, char *name)