F´ Flight Software - C/C++ Documentation
A framework for building embedded system applications to NASA flight quality standards.
Task.cpp
Go to the documentation of this file.
1 // ======================================================================
2 // \title Os/Posix/Task.cpp
3 // \brief implementation of Posix implementation of Os::Task
4 // ======================================================================
5 #include <pthread.h>
6 #include <unistd.h>
7 #include <cerrno>
8 #include <climits>
9 #include <cstring>
10 
11 #include "Fw/Logger/Logger.hpp"
12 #include "Fw/Types/Assert.hpp"
13 #include "Fw/Types/StringUtils.hpp"
14 #include "Os/Posix/Task.hpp"
15 #include "Os/Posix/error.hpp"
16 #include "Os/Task.hpp"
17 
18 namespace Os {
19 namespace Posix {
20 namespace Task {
21 std::atomic<bool> PosixTask::s_permissions_reported(false);
22 static const int SCHED_POLICY = SCHED_RR;
23 
24 typedef void* (*pthread_func_ptr)(void*);
25 
26 // Forward declaration
27 int set_task_name(pthread_t thread, char* name);
28 
29 void* pthread_entry_wrapper(void* wrapper_pointer) {
30  FW_ASSERT(wrapper_pointer != nullptr);
31  // Both downcasts are safe because we know the types
32  Os::Task::TaskRoutineWrapper& wrapper = *reinterpret_cast<Os::Task::TaskRoutineWrapper*>(wrapper_pointer);
33 #if defined(POSIX_THREADS_ENABLE_NAMES) && POSIX_THREADS_ENABLE_NAMES
34  auto handle = reinterpret_cast<Os::Posix::Task::PosixTaskHandle*>(wrapper.m_task.getHandle());
35  FW_ASSERT(handle != nullptr);
36  // Task name is on a best effort basis
37  (void)set_task_name(handle->m_task_descriptor, handle->m_name);
38 #endif
39  wrapper.run(&wrapper);
40  return nullptr;
41 }
42 
43 int set_stack_size(pthread_attr_t& attributes, const Os::Task::Arguments& arguments) {
44  int status = PosixTaskHandle::SUCCESS;
45  FwSizeType stack = arguments.m_stackSize;
46 // Check for stack size multiple of page size or skip when the function
47 // is unavailable.
48 #ifdef _SC_PAGESIZE
49  long page_size = sysconf(_SC_PAGESIZE);
50 #else
51  long page_size = -1; // Force skip and warning
52 #endif
53  if (page_size <= 0) {
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) {
57  // Round-down to nearest page size multiple
58  FwSizeType rounded = (stack / static_cast<FwSizeType>(page_size)) * static_cast<FwSizeType>(page_size);
59  Fw::Logger::log("[WARNING] %s stack size of %" PRI_FwSizeType
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);
62  stack = rounded;
63  }
64 
65  // Clamp invalid stack sizes
66  if (stack <= static_cast<FwSizeType>(PTHREAD_STACK_MIN)) {
68  "[WARNING] %s stack size of %" PRI_FwSizeType " is too small, clamping to %" PRI_FwSizeType "\n",
69  const_cast<CHAR*>(arguments.m_name.toChar()), stack, static_cast<FwSizeType>(PTHREAD_STACK_MIN));
70  stack = static_cast<FwSizeType>(PTHREAD_STACK_MIN);
71  }
72  status = pthread_attr_setstacksize(&attributes, static_cast<size_t>(stack));
73  return status;
74 }
75 
76 int set_priority_params(pthread_attr_t& attributes, const Os::Task::Arguments& arguments) {
77  const FwSizeType min_priority = static_cast<FwSizeType>(sched_get_priority_min(SCHED_POLICY));
78  const FwSizeType max_priority = static_cast<FwSizeType>(sched_get_priority_max(SCHED_POLICY));
79  int status = PosixTaskHandle::SUCCESS;
80  FwSizeType priority = arguments.m_priority;
81  // Clamp to minimum priority
82  if (priority < min_priority) {
83  Fw::Logger::log("[WARNING] %s low task priority of %" PRI_FwSizeType " clamped to %" PRI_FwSizeType "\n",
84  const_cast<CHAR*>(arguments.m_name.toChar()), priority, min_priority);
85  priority = min_priority;
86  }
87  // Clamp to maximum priority
88  else if (priority > max_priority) {
89  Fw::Logger::log("[WARNING] %s high task priority of %" PRI_FwSizeType " clamped to %" PRI_FwSizeType "\n",
90  const_cast<CHAR*>(arguments.m_name.toChar()), priority, max_priority);
91  priority = max_priority;
92  }
93 
94  // Set attributes required for priority
95  status = pthread_attr_setschedpolicy(&attributes, SCHED_POLICY);
96  if (status == PosixTaskHandle::SUCCESS) {
97  status = pthread_attr_setinheritsched(&attributes, PTHREAD_EXPLICIT_SCHED);
98  }
99  if (status == PosixTaskHandle::SUCCESS) {
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);
104  }
105  return status;
106 }
107 
108 int set_cpu_affinity(pthread_attr_t& attributes, const Os::Task::Arguments& arguments) {
109  int status = 0;
110 // pthread_attr_setaffinity_np is a non-POSIX function. Notably, it is not available on musl.
111 // Limit its use to builds that involve glibc, on Linux, with _GNU_SOURCE defined.
112 // That's the circumstance in which we expect this feature to work.
113 #if defined(TGT_OS_TYPE_LINUX) && defined(__GLIBC__) && defined(_GNU_SOURCE)
114  const FwSizeType affinity = arguments.m_cpuAffinity;
115  cpu_set_t cpu_set;
116  CPU_ZERO(&cpu_set);
117  CPU_SET(static_cast<int>(affinity), &cpu_set);
118 
119  // According to the man-page this function sets errno rather than returning an error status like other functions
120  status = pthread_attr_setaffinity_np(&attributes, sizeof(cpu_set_t), &cpu_set);
121  status = (status == PosixTaskHandle::SUCCESS) ? status : errno;
122 #else
123  Fw::Logger::log("[WARNING] %s setting CPU affinity is only available with GNU pthreads\n",
124  const_cast<CHAR*>(arguments.m_name.toChar()));
125 #endif
126  return status;
127 }
128 
129 int set_task_name(pthread_t thread, char* name) {
130  int status = 0;
131 // pthread_setname_np is a non-POSIX function.
132 // Limit its use to builds that involve glibc, on Linux, with _GNU_SOURCE defined.
133 // That's the circumstance in which we expect this feature to work.
134 #if defined(TGT_OS_TYPE_LINUX) && defined(__GLIBC__) && defined(_GNU_SOURCE) && defined(POSIX_THREADS_ENABLE_NAMES) && \
135  POSIX_THREADS_ENABLE_NAMES
136  // Force safe name usage
138  status = pthread_setname_np(thread, name);
139 #endif
140  return status;
141 }
142 
143 Os::Task::Status PosixTask::create(const Os::Task::Arguments& arguments,
144  const PosixTask::PermissionExpectation permissions) {
145  int pthread_status = PosixTaskHandle::SUCCESS;
146  PosixTaskHandle& handle = this->m_handle;
147  const bool expect_permission = (permissions == EXPECT_PERMISSION);
148  // Initialize and clear pthread attributes
149  pthread_attr_t attributes;
150  memset(&attributes, 0, sizeof(attributes));
151  pthread_status = pthread_attr_init(&attributes);
152  if ((arguments.m_stackSize != Os::Task::TASK_DEFAULT) && (expect_permission) &&
153  (pthread_status == PosixTaskHandle::SUCCESS)) {
154  pthread_status = set_stack_size(attributes, arguments);
155  }
156  if ((arguments.m_priority != Os::Task::TASK_PRIORITY_DEFAULT) && (expect_permission) &&
157  (pthread_status == PosixTaskHandle::SUCCESS)) {
158  pthread_status = set_priority_params(attributes, arguments);
159  }
160  if ((arguments.m_cpuAffinity != Os::Task::TASK_DEFAULT) && (expect_permission) &&
161  (pthread_status == PosixTaskHandle::SUCCESS)) {
162  pthread_status = set_cpu_affinity(attributes, arguments);
163  }
164  if (pthread_status == PosixTaskHandle::SUCCESS) {
165  pthread_status =
166  pthread_create(&handle.m_task_descriptor, &attributes, pthread_entry_wrapper, arguments.m_routine_argument);
167  }
168  // Successful execution of all precious steps will result in a valid task handle
169  if (pthread_status == PosixTaskHandle::SUCCESS) {
170  handle.m_is_valid = true;
171  }
172 
173 #if defined(POSIX_THREADS_ENABLE_NAMES) && POSIX_THREADS_ENABLE_NAMES
174  Fw::StringUtils::string_copy(handle.m_name, arguments.m_name.toChar(), sizeof(handle.m_name));
175 #endif
176 
177  (void)pthread_attr_destroy(&attributes);
178  return Posix::posix_status_to_task_status(pthread_status);
179 }
180 
182 
184  FW_ASSERT(arguments.m_routine != nullptr);
185 
186  // Try to create thread with assuming permissions
187  Os::Task::Status status = this->create(arguments, PermissionExpectation::EXPECT_PERMISSION);
188  // Failure due to permission automatically retried
189  if (status == Os::Task::Status::ERROR_PERMISSION) {
190  if (not PosixTask::s_permissions_reported) {
191  Fw::Logger::log("\n");
192  Fw::Logger::log("[NOTE] Task Permissions:\n");
193  Fw::Logger::log("[NOTE]\n");
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");
197  Fw::Logger::log("[NOTE]\n");
198  Fw::Logger::log("[NOTE] There are three possible resolutions:\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");
202  Fw::Logger::log("\n");
203  PosixTask::s_permissions_reported = true;
204  }
205  // Fallback with no permission
206  status = this->create(arguments, PermissionExpectation::EXPECT_NO_PERMISSION);
207  } else if (status != Os::Task::Status::OP_OK) {
208  Fw::Logger::log("[ERROR] Failed to create task with status: %d", static_cast<int>(status));
209  }
210  return status;
211 }
212 
214  Os::Task::Status status = Os::Task::Status::JOIN_ERROR;
215  if (not this->m_handle.m_is_valid) {
216  status = Os::Task::Status::INVALID_HANDLE;
217  } else {
218  int stat = ::pthread_join(this->m_handle.m_task_descriptor, nullptr);
219  status = (stat == PosixTaskHandle::SUCCESS) ? Os::Task::Status::OP_OK : Os::Task::Status::JOIN_ERROR;
220  }
221  return status;
222 }
223 
225  return &this->m_handle;
226 }
227 
228 // Note: not implemented for Posix threads. Must be manually done using a mutex or other blocking construct as there
229 // is no top-level pthreads support for suspend and resume.
231  FW_ASSERT(0);
232 }
233 
235  FW_ASSERT(0);
236 }
237 
239  Os::Task::Status task_status = Os::Task::OP_OK;
240  timespec sleep_interval;
241  sleep_interval.tv_sec = interval.getSeconds();
242  sleep_interval.tv_nsec = interval.getUSeconds() * 1000;
243 
244  timespec remaining_interval;
245  remaining_interval.tv_sec = 0;
246  remaining_interval.tv_nsec = 0;
247 
248  while (true) {
249  int status = nanosleep(&sleep_interval, &remaining_interval);
250  // Success, return ok
251  if (0 == status) {
252  break;
253  }
254  // Interrupted, reset sleep and iterate
255  else if (EINTR == errno) {
256  sleep_interval = remaining_interval;
257  continue;
258  }
259  // Anything else is an error
260  else {
261  task_status = Os::Task::Status::DELAY_ERROR;
262  break;
263  }
264  }
265  return task_status;
266 }
267 
268 } // end namespace Task
269 } // end namespace Posix
270 } // end namespace Os
static constexpr FwSizeType TASK_DEFAULT
Definition: Task.hpp:41
TaskHandle * getHandle() override
return the underlying task handle (implementation specific)
Definition: Task.cpp:187
Task handle representation.
Definition: Task.hpp:33
Operation succeeded.
Definition: Os.hpp:26
PlatformSizeType FwSizeType
const char * toChar() const
Convert to a C-style char*.
Definition: TaskString.hpp:45
#define PRI_FwSizeType
int set_priority_params(pthread_attr_t &attributes, const Os::Task::Arguments &arguments)
Definition: Task.cpp:76
void suspend(SuspensionType suspensionType) override
suspend the task given the suspension type
Definition: Task.cpp:230
static void log(const char *format,...)
log a formated string with supplied arguments
Definition: Logger.cpp:21
static constexpr int SUCCESS
Definition: Task.hpp:26
Status _delay(Fw::TimeInterval interval) override
delay the current task
Definition: Task.cpp:238
static constexpr FwTaskPriorityType TASK_PRIORITY_DEFAULT
Definition: Task.hpp:47
Task::Status posix_status_to_task_status(int posix_status)
Definition: error.cpp:147
message sent/received okay
Definition: Task.hpp:50
bool m_is_valid
Is the above descriptor valid.
Definition: Task.hpp:31
PermissionExpectation
Enumeration of permission expectations.
Definition: Task.hpp:41
pthread_t m_task_descriptor
Posix task descriptor.
Definition: Task.hpp:29
char * string_copy(char *destination, const char *source, FwSizeType num)
copy string with null-termination guaranteed
Definition: StringUtils.cpp:7
TaskHandle * getHandle() override
return the underlying task handle (implementation specific)
Definition: Task.cpp:224
static void run(void *task_pointer)
run the task routine wrapper
Definition: Task.cpp:29
static constexpr FwSizeType PTHREAD_NAME_LENGTH
Length of pthread name.
Definition: Task.hpp:25
Wrapper for task routine that ensures onStart() is called once the task actually begins.
Definition: Task.hpp:212
Task & m_task
Reference to owning task.
Definition: Task.hpp:226
const Os::TaskString m_name
Definition: Task.hpp:94
void * pthread_entry_wrapper(void *wrapper_pointer)
Definition: Task.cpp:29
FwSizeType m_cpuAffinity
Definition: Task.hpp:99
int set_stack_size(pthread_attr_t &attributes, const Os::Task::Arguments &arguments)
Definition: Task.cpp:43
U32 getUSeconds() const
static const int SCHED_POLICY
Definition: Task.cpp:22
Expect that you hold necessary permissions.
Definition: Task.hpp:42
Status join() override
block until the task has ended
Definition: Task.cpp:213
Status start(const Arguments &arguments) override
start the task
Definition: Task.cpp:183
FwTaskPriorityType m_priority
Definition: Task.hpp:97
int set_cpu_affinity(pthread_attr_t &attributes, const Os::Task::Arguments &arguments)
Definition: Task.cpp:108
void resume() override
resume a suspended task
Definition: Task.cpp:234
void onStart() override
perform required task start actions
Definition: Task.cpp:181
#define FW_ASSERT(...)
Definition: Assert.hpp:14
U32 getSeconds() const
int set_task_name(pthread_t thread, char *name)
Definition: Task.cpp:129