F´ Flight Software - C/C++ Documentation
A framework for building embedded system applications to NASA flight quality standards.
Task.hpp
Go to the documentation of this file.
1 // ======================================================================
2 // \title Os/Task.hpp
3 // \brief common function definitions for Os::Task
4 // ======================================================================
5 #ifndef Os_Task_hpp_
6 #define Os_Task_hpp_
7 
11 #include <Os/Mutex.hpp>
12 #include <Os/Os.hpp>
13 #include <Os/TaskString.hpp>
14 
15 #include <Fw/Deprecate.hpp>
16 #include <limits>
17 
18 // Forward declare for UTs
19 namespace Os {
20 namespace Test {
21 namespace Task {
22 struct Tester;
23 }
24 } // namespace Test
25 } // namespace Os
26 
27 namespace Os {
28 
29 // Forward declarations
30 class TaskRegistry;
31 
33 class TaskHandle {};
34 
36  public:
41  static constexpr FwSizeType TASK_DEFAULT = std::numeric_limits<FwSizeType>::max();
42 
47  static constexpr FwTaskPriorityType TASK_PRIORITY_DEFAULT = std::numeric_limits<FwTaskPriorityType>::max();
48 
49  enum Status {
62  };
63 
65 
67 
69  typedef void (*taskRoutine)(void* ptr);
70 
71  class Arguments {
72  public:
85  Arguments(const Fw::StringBase& name,
86  const taskRoutine routine,
87  void* const routine_argument = nullptr,
89  const FwSizeType stackSize = TASK_DEFAULT,
90  const FwSizeType cpuAffinity = TASK_DEFAULT,
91  const FwTaskIdType identifier = static_cast<FwTaskIdType>(TASK_DEFAULT));
92 
93  public:
101  };
102 
104  TaskInterface() = default;
105 
107  virtual ~TaskInterface() = default;
108 
110  TaskInterface(const TaskInterface& other) = delete;
111 
113  TaskInterface& operator=(const TaskInterface& other) = delete;
114 
115  // =================
116  // Implementation functions (static) to be supplied by the linker
117  // =================
118 
138  static TaskInterface* getDelegate(TaskHandleStorage& aligned_placement_new_memory);
139 
140  // =================
141  // Implementation functions (instance) to be supplied by the Os::TaskInterface children
142  // =================
143 
145  virtual void onStart() = 0;
146 
153  virtual Status join() = 0;
154 
162  virtual void suspend(SuspensionType suspensionType) = 0;
163 
168  virtual void resume() = 0;
169 
177  virtual Status _delay(Fw::TimeInterval interval) = 0;
178 
189  virtual bool isCooperative();
190 
193  virtual TaskHandle* getHandle() = 0;
194 
201  virtual Status start(const Arguments& arguments) = 0;
202 };
203 
207 class Task final : public TaskInterface {
208  friend struct Os::Test::Task::Tester;
209 
210  public:
213  public:
214  explicit TaskRoutineWrapper(Task& self);
215 
221  static void run(void* task_pointer);
222 
224  void invoke();
225 
228  void* m_user_argument = nullptr;
229  };
230 
233 
235  Task();
236 
238  ~Task() final;
239 
241  Task(const Task& other) = delete;
242 
244  Task& operator=(const Task& other) = delete;
245 
250  void suspend();
251 
258  State getState();
259 
274  DEPRECATED(Status start(const Fw::StringBase& name,
275  const taskRoutine routine,
276  void* const arg = nullptr,
277  const FwTaskPriorityType priority = TASK_PRIORITY_DEFAULT,
278  const ParamType stackSize = TASK_DEFAULT,
279  const ParamType cpuAffinity = TASK_DEFAULT,
280  const ParamType identifier = TASK_DEFAULT),
281  "Switch to Task::start(Arguments&)");
282 
288  //
293  Status start(const Arguments& arguments) override;
294 
296  void onStart() override;
297 
299  //~
302  void invokeRoutine();
303 
311  DEPRECATED(Status join(void** value_ptr), "Please switch to argument free join.");
312 
319  Status join() override;
320 
328  void suspend(SuspensionType suspensionType) override;
329 
334  void resume() override;
335 
343  Status _delay(Fw::TimeInterval interval) override;
344 
347  bool isCooperative() override;
348 
351 
354  TaskHandle* getHandle() override;
355 
357  static void init();
358 
361  static FwSizeType getNumTasks();
362 
365  static void registerTaskRegistry(TaskRegistry* registry);
366 
369  static Task& getSingleton();
370 
378  static Status delay(Fw::TimeInterval interval);
379 
380  private:
381  static TaskRegistry* s_taskRegistry;
382  static FwSizeType s_numTasks;
383  static Mutex s_taskMutex;
384 
385  TaskString m_name;
386  TaskInterface::State m_state = Task::NOT_STARTED;
387  Mutex m_lock;
388  TaskRoutineWrapper m_wrapper;
389  FwTaskPriorityType m_priority = 0; // Storage of priority
390 
391  bool m_registered = false;
392 
393  // This section is used to store the implementation-defined file handle. To Os::File and fprime, this type is
394  // opaque and thus normal allocation cannot be done. Instead, we allow the implementor to store then handle in
395  // the byte-array here and set `handle` to that address for storage.
396  //
397  alignas(FW_HANDLE_ALIGNMENT) TaskHandleStorage m_handle_storage;
398  TaskInterface& m_delegate;
399 };
400 
402  public:
404  TaskRegistry() = default;
406  virtual ~TaskRegistry() = default;
410  virtual void addTask(Task* task) = 0;
411 
415  virtual void removeTask(Task* task) = 0;
416 };
417 } // namespace Os
418 
419 #endif
error trying to join the task
Definition: Task.hpp:57
static Status delay(Fw::TimeInterval interval)
delay the current task
Definition: Task.cpp:199
virtual ~TaskInterface()=default
default virtual destructor
static constexpr FwSizeType TASK_DEFAULT
Definition: Task.hpp:41
PlatformTaskIdType FwTaskIdType
The type of task priorities used.
TaskHandle * getHandle() override
return the underlying task handle (implementation specific)
Definition: Task.cpp:182
Task handle representation.
Definition: Task.hpp:33
unable to set the task affinity
Definition: Task.hpp:55
DEPRECATED(Status start(const Fw::StringBase &name, const taskRoutine routine, void *const arg=nullptr, const FwTaskPriorityType priority=TASK_PRIORITY_DEFAULT, const ParamType stackSize=TASK_DEFAULT, const ParamType cpuAffinity=TASK_DEFAULT, const ParamType identifier=TASK_DEFAULT), "Switch to Task::start(Arguments&)")
start this task
PlatformSizeType FwSizeType
virtual void resume()=0
resume a suspended task
FwTaskPriorityType getPriority()
get the task priority
Definition: Task.cpp:177
static Task & getSingleton()
get a reference to singleton
Definition: Task.cpp:208
Task is in an invalid state for the operation.
Definition: Task.hpp:61
virtual Status _delay(Fw::TimeInterval interval)=0
delay the currently scheduled task using the given architecture
State getState()
get the task&#39;s state
Definition: Task.cpp:76
started task with invalid parameters
Definition: Task.hpp:52
static void registerTaskRegistry(TaskRegistry *registry)
register a task registry to track Threads
Definition: Task.cpp:213
A class to represent a time interval holding two U32 seconds and microseconds values.
started with invalid stack size
Definition: Task.hpp:53
permissions error setting-up tasks
Definition: Task.hpp:59
Task()
default constructor
Definition: Task.cpp:62
Status start(const Arguments &arguments) override
start the task
Definition: Task.cpp:84
Status _delay(Fw::TimeInterval interval) override
delay the current task
Definition: Task.cpp:194
virtual Status join()=0
block until the task has ended
~Task() final
default virtual destructor
Definition: Task.cpp:64
U8 TaskHandleStorage[FW_TASK_HANDLE_MAX_SIZE]
Definition: Os.hpp:14
virtual void onStart()=0
perform required task start actions
#define FW_HANDLE_ALIGNMENT
Alignment of handle storage.
Definition: FpConfig.h:321
void * m_user_argument
Argument to user function.
Definition: Task.hpp:228
Arguments(const Fw::StringBase &name, const taskRoutine routine, void *const routine_argument=nullptr, const FwTaskPriorityType priority=TASK_PRIORITY_DEFAULT, const FwSizeType stackSize=TASK_DEFAULT, const FwSizeType cpuAffinity=TASK_DEFAULT, const FwTaskIdType identifier=static_cast< FwTaskIdType >(TASK_DEFAULT))
construct a set of arguments to start a task
Definition: Task.cpp:10
static constexpr FwTaskPriorityType TASK_PRIORITY_DEFAULT
Definition: Task.hpp:47
error trying to delay the task
Definition: Task.hpp:56
Task handle invalid.
Definition: Task.hpp:51
static TaskInterface * getDelegate(TaskHandleStorage &aligned_placement_new_memory)
provide a pointer to a task delegate object
Definition: DefaultTask.cpp:11
bool isCooperative() override
determine if the task is cooperative multitasking (implementation specific)
Definition: Task.cpp:172
virtual void suspend(SuspensionType suspensionType)=0
suspend the task given the suspension type
unexpected error return value
Definition: Task.hpp:54
message sent/received okay
Definition: Task.hpp:50
virtual Status start(const Arguments &arguments)=0
start the task
void suspend()
suspend the current task
Definition: Task.cpp:72
Status join() override
block until the task has ended
Definition: Task.cpp:141
static FwSizeType getNumTasks()
get the current number of tasks
Definition: Task.cpp:187
TaskInterface()=default
default constructor
static void run(void *task_pointer)
run the task routine wrapper
Definition: Task.cpp:29
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
FwTaskIdType m_identifier
Definition: Task.hpp:100
PlatformTaskPriorityType FwTaskPriorityType
The type of task priorities used.
FwSizeType m_cpuAffinity
Definition: Task.hpp:99
FwSizeType ParamType
backwards-compatible parameter type
Definition: Task.hpp:232
void onStart() override
perform delegate&#39;s required task start actions
Definition: Task.cpp:132
friend struct Os::Test::Task::Tester
Definition: Task.hpp:208
TaskRoutineWrapper(Task &self)
Definition: Task.cpp:27
void invokeRoutine()
invoke the task&#39;s routine
Definition: Task.cpp:137
Task feature is not supported.
Definition: Task.hpp:60
void resume() override
resume a suspended task
Definition: Task.cpp:167
void invoke()
invoke the run method with "self" as argument
Definition: Task.cpp:50
unable to allocate more tasks
Definition: Task.hpp:58
TaskInterface & operator=(const TaskInterface &other)=delete
assignment operator is forbidden
taskRoutine m_user_function
User function to run once started.
Definition: Task.hpp:227
FwTaskPriorityType m_priority
Definition: Task.hpp:97
static void init()
initialize singleton
Definition: Task.cpp:203
Implementation of malloc based allocator.
virtual TaskHandle * getHandle()=0
return the underlying task handle (implementation specific)
void(* taskRoutine)(void *ptr)
Prototype for task routine started in task context.
Definition: Task.hpp:69
virtual bool isCooperative()
determine if the task requires cooperative multitasking
Definition: Task.cpp:58