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:
37  static constexpr FwSizeType TASK_DEFAULT = std::numeric_limits<FwSizeType>::max();
38  static constexpr FwTaskPriorityType TASK_PRIORITY_DEFAULT = std::numeric_limits<FwTaskPriorityType>::max();
39  enum Status {
52  };
53 
55 
57 
59  typedef void (*taskRoutine)(void* ptr);
60 
61  class Arguments {
62  public:
75  Arguments(const Fw::StringBase& name,
76  const taskRoutine routine,
77  void* const routine_argument = nullptr,
79  const FwSizeType stackSize = TASK_DEFAULT,
80  const FwSizeType cpuAffinity = TASK_DEFAULT,
81  const FwTaskIdType identifier = static_cast<FwTaskIdType>(TASK_DEFAULT));
82 
83  public:
91  };
92 
94  TaskInterface() = default;
95 
97  virtual ~TaskInterface() = default;
98 
100  TaskInterface(const TaskInterface& other) = delete;
101 
103  TaskInterface& operator=(const TaskInterface& other) = delete;
104 
105  // =================
106  // Implementation functions (static) to be supplied by the linker
107  // =================
108 
128  static TaskInterface* getDelegate(TaskHandleStorage& aligned_placement_new_memory);
129 
130  // =================
131  // Implementation functions (instance) to be supplied by the Os::TaskInterface children
132  // =================
133 
135  virtual void onStart() = 0;
136 
143  virtual Status join() = 0;
144 
152  virtual void suspend(SuspensionType suspensionType) = 0;
153 
158  virtual void resume() = 0;
159 
167  virtual Status _delay(Fw::TimeInterval interval) = 0;
168 
179  virtual bool isCooperative();
180 
183  virtual TaskHandle* getHandle() = 0;
184 
191  virtual Status start(const Arguments& arguments) = 0;
192 };
193 
197 class Task final : public TaskInterface {
198  friend struct Os::Test::Task::Tester;
199 
200  public:
203  public:
204  explicit TaskRoutineWrapper(Task& self);
205 
211  static void run(void* task_pointer);
212 
214  void invoke();
215 
218  void* m_user_argument = nullptr;
219  };
220 
223 
225  Task();
226 
228  ~Task() final;
229 
231  Task(const Task& other) = delete;
232 
234  Task& operator=(const Task& other) = delete;
235 
240  void suspend();
241 
248  State getState();
249 
264  DEPRECATED(Status start(const Fw::StringBase& name,
265  const taskRoutine routine,
266  void* const arg = nullptr,
267  const FwTaskPriorityType priority = TASK_PRIORITY_DEFAULT,
268  const ParamType stackSize = TASK_DEFAULT,
269  const ParamType cpuAffinity = TASK_DEFAULT,
270  const ParamType identifier = TASK_DEFAULT),
271  "Switch to Task::start(Arguments&)");
272 
278  //
283  Status start(const Arguments& arguments) override;
284 
286  void onStart() override;
287 
289  //~
292  void invokeRoutine();
293 
301  DEPRECATED(Status join(void** value_ptr), "Please switch to argument free join.");
302 
309  Status join() override;
310 
318  void suspend(SuspensionType suspensionType) override;
319 
324  void resume() override;
325 
333  Status _delay(Fw::TimeInterval interval) override;
334 
337  bool isCooperative() override;
338 
341 
344  TaskHandle* getHandle() override;
345 
347  static void init();
348 
351  static FwSizeType getNumTasks();
352 
355  static void registerTaskRegistry(TaskRegistry* registry);
356 
359  static Task& getSingleton();
360 
368  static Status delay(Fw::TimeInterval interval);
369 
370  private:
371  static TaskRegistry* s_taskRegistry;
372  static FwSizeType s_numTasks;
373  static Mutex s_taskMutex;
374 
375  TaskString m_name;
376  TaskInterface::State m_state = Task::NOT_STARTED;
377  Mutex m_lock;
378  TaskRoutineWrapper m_wrapper;
379  FwTaskPriorityType m_priority = 0; // Storage of priority
380 
381  bool m_registered = false;
382 
383  // This section is used to store the implementation-defined file handle. To Os::File and fprime, this type is
384  // opaque and thus normal allocation cannot be done. Instead, we allow the implementor to store then handle in
385  // the byte-array here and set `handle` to that address for storage.
386  //
387  alignas(FW_HANDLE_ALIGNMENT) TaskHandleStorage m_handle_storage;
388  TaskInterface& m_delegate;
389 };
390 
392  public:
394  TaskRegistry() = default;
396  virtual ~TaskRegistry() = default;
400  virtual void addTask(Task* task) = 0;
401 
405  virtual void removeTask(Task* task) = 0;
406 };
407 } // namespace Os
408 
409 #endif
error trying to join the task
Definition: Task.hpp:47
static Status delay(Fw::TimeInterval interval)
delay the current task
Definition: Task.cpp:194
virtual ~TaskInterface()=default
default virtual destructor
static constexpr FwSizeType TASK_DEFAULT
Definition: Task.hpp:37
PlatformTaskIdType FwTaskIdType
The type of task priorities used.
TaskHandle * getHandle() override
return the underlying task handle (implementation specific)
Definition: Task.cpp:177
Task handle representation.
Definition: Task.hpp:33
unable to set the task affinity
Definition: Task.hpp:45
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:172
static Task & getSingleton()
get a reference to singleton
Definition: Task.cpp:203
Task is in an invalid state for the operation.
Definition: Task.hpp:51
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:42
static void registerTaskRegistry(TaskRegistry *registry)
register a task registry to track Threads
Definition: Task.cpp:208
A class to represent a time interval holding two U32 seconds and microseconds values.
started with invalid stack size
Definition: Task.hpp:43
permissions error setting-up tasks
Definition: Task.hpp:49
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:189
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:322
void * m_user_argument
Argument to user function.
Definition: Task.hpp:218
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:38
error trying to delay the task
Definition: Task.hpp:46
Task handle invalid.
Definition: Task.hpp:41
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:167
virtual void suspend(SuspensionType suspensionType)=0
suspend the task given the suspension type
unexpected error return value
Definition: Task.hpp:44
message sent/received okay
Definition: Task.hpp:40
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:136
static FwSizeType getNumTasks()
get the current number of tasks
Definition: Task.cpp:182
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:202
Task & m_task
Reference to owning task.
Definition: Task.hpp:216
const Os::TaskString m_name
Definition: Task.hpp:84
FwTaskIdType m_identifier
Definition: Task.hpp:90
PlatformTaskPriorityType FwTaskPriorityType
The type of task priorities used.
FwSizeType m_cpuAffinity
Definition: Task.hpp:89
FwSizeType ParamType
backwards-compatible parameter type
Definition: Task.hpp:222
void onStart() override
perform delegate&#39;s required task start actions
Definition: Task.cpp:127
friend struct Os::Test::Task::Tester
Definition: Task.hpp:198
TaskRoutineWrapper(Task &self)
Definition: Task.cpp:27
void invokeRoutine()
invoke the task&#39;s routine
Definition: Task.cpp:132
Task feature is not supported.
Definition: Task.hpp:50
void resume() override
resume a suspended task
Definition: Task.cpp:162
void invoke()
invoke the run method with "self" as argument
Definition: Task.cpp:50
unable to allocate more tasks
Definition: Task.hpp:48
TaskInterface & operator=(const TaskInterface &other)=delete
assignment operator is forbidden
taskRoutine m_user_function
User function to run once started.
Definition: Task.hpp:217
FwTaskPriorityType m_priority
Definition: Task.hpp:87
static void init()
initialize singleton
Definition: Task.cpp:198
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:59
virtual bool isCooperative()
determine if the task requires cooperative multitasking
Definition: Task.cpp:58