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/Os.hpp>
12 #include <Os/TaskString.hpp>
13 #include <Os/Mutex.hpp>
14 
15 #include <Fw/Deprecate.hpp>
16 #include <limits>
17 
18 // Forward declare for UTs
19 namespace Os {namespace Test {namespace Task { struct Tester; }}}
20 
21 namespace Os {
22 
23  // Forward declarations
24  class TaskRegistry;
25 
27  class TaskHandle {};
28 
29  class TaskInterface {
30  public:
31  static constexpr FwSizeType TASK_DEFAULT = std::numeric_limits<FwSizeType>::max();
32  static constexpr FwTaskPriorityType TASK_PRIORITY_DEFAULT = std::numeric_limits<FwTaskPriorityType>::max();
33  enum Status {
46  };
47 
51  };
52 
53  enum State {
61  };
62 
64  typedef void (*taskRoutine)(void* ptr);
65 
66  class Arguments {
67  public:
80  Arguments(const Fw::StringBase& name, const taskRoutine routine,
81  void* const routine_argument = nullptr,
83  const FwSizeType stackSize = TASK_DEFAULT,
84  const FwSizeType cpuAffinity = TASK_DEFAULT,
85  const FwTaskIdType identifier = static_cast<FwTaskIdType>(TASK_DEFAULT));
86 
87  public:
95  };
96 
98  TaskInterface() = default;
99 
101  virtual ~TaskInterface() = default;
102 
104  TaskInterface(const TaskInterface& other) = delete;
105 
107  TaskInterface& operator=(const TaskInterface& other) = delete;
108 
109  // =================
110  // Implementation functions (static) to be supplied by the linker
111  // =================
112 
132  static TaskInterface* getDelegate(TaskHandleStorage& aligned_placement_new_memory);
133 
134  // =================
135  // Implementation functions (instance) to be supplied by the Os::TaskInterface children
136  // =================
137 
139  virtual void onStart() = 0;
140 
147  virtual Status join() = 0;
148 
156  virtual void suspend(SuspensionType suspensionType) = 0;
157 
162  virtual void resume() = 0;
163 
171  virtual Status _delay(Fw::TimeInterval interval) = 0;
172 
183  virtual bool isCooperative();
184 
187  virtual TaskHandle* getHandle() = 0;
188 
195  virtual Status start(const Arguments& arguments) = 0;
196  };
197 
201  class Task final : public TaskInterface {
202  friend struct Os::Test::Task::Tester;
203  public:
206  public:
207  explicit TaskRoutineWrapper(Task& self);
208 
214  static void run(void* task_pointer);
215 
217  void invoke();
218 
221  void* m_user_argument = nullptr;
222  };
223 
226 
228  Task();
229 
231  ~Task() final;
232 
234  Task(const Task& other) = delete;
235 
237  Task& operator=(const Task& other) = delete;
238 
243  void suspend();
244 
251  State getState();
252 
267  DEPRECATED(Status start(const Fw::StringBase &name, const taskRoutine routine, void* const arg = nullptr,
268  const FwTaskPriorityType priority = TASK_PRIORITY_DEFAULT,
269  const ParamType stackSize = TASK_DEFAULT,
270  const ParamType cpuAffinity = TASK_DEFAULT,
271  const ParamType identifier = TASK_DEFAULT), "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 
391  class TaskRegistry {
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 }
408 
409 #endif
error trying to join the task
Definition: Task.hpp:41
static Status delay(Fw::TimeInterval interval)
delay the current task
Definition: Task.cpp:191
virtual ~TaskInterface()=default
default virtual destructor
static constexpr FwSizeType TASK_DEFAULT
Definition: Task.hpp:31
PlatformTaskIdType FwTaskIdType
The type of task priorities used.
TaskHandle * getHandle() override
return the underlying task handle (implementation specific)
Definition: Task.cpp:174
Task handle representation.
Definition: Task.hpp:27
unable to set the task affinity
Definition: Task.hpp:39
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:169
static Task & getSingleton()
get a reference to singleton
Definition: Task.cpp:200
Task is in an invalid state for the operation.
Definition: Task.hpp:45
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:74
started task with invalid parameters
Definition: Task.hpp:36
static void registerTaskRegistry(TaskRegistry *registry)
register a task registry to track Threads
Definition: Task.cpp:205
A class to represent a time interval holding two U32 seconds and microseconds values.
started with invalid stack size
Definition: Task.hpp:37
permissions error setting-up tasks
Definition: Task.hpp:43
Task()
default constructor
Definition: Task.cpp:60
Status start(const Arguments &arguments) override
start the task
Definition: Task.cpp:82
Status _delay(Fw::TimeInterval interval) override
delay the current task
Definition: Task.cpp:186
virtual Status join()=0
block until the task has ended
~Task() final
default virtual destructor
Definition: Task.cpp:62
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:346
void * m_user_argument
Argument to user function.
Definition: Task.hpp:221
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:32
error trying to delay the task
Definition: Task.hpp:40
Task handle invalid.
Definition: Task.hpp:35
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:164
virtual void suspend(SuspensionType suspensionType)=0
suspend the task given the suspension type
unexpected error return value
Definition: Task.hpp:38
message sent/received okay
Definition: Task.hpp:34
virtual Status start(const Arguments &arguments)=0
start the task
void suspend()
suspend the current task
Definition: Task.cpp:70
Status join() override
block until the task has ended
Definition: Task.cpp:134
static FwSizeType getNumTasks()
get the current number of tasks
Definition: Task.cpp:179
TaskInterface()=default
default constructor
static void run(void *task_pointer)
run the task routine wrapper
Definition: Task.cpp:27
Wrapper for task routine that ensures onStart() is called once the task actually begins.
Definition: Task.hpp:205
Task & m_task
Reference to owning task.
Definition: Task.hpp:219
const Os::TaskString m_name
Definition: Task.hpp:88
FwTaskIdType m_identifier
Definition: Task.hpp:94
PlatformTaskPriorityType FwTaskPriorityType
The type of task priorities used.
FwSizeType m_cpuAffinity
Definition: Task.hpp:93
FwSizeType ParamType
backwards-compatible parameter type
Definition: Task.hpp:225
void onStart() override
perform delegate&#39;s required task start actions
Definition: Task.cpp:125
friend struct Os::Test::Task::Tester
Definition: Task.hpp:202
TaskRoutineWrapper(Task &self)
Definition: Task.cpp:25
void invokeRoutine()
invoke the task&#39;s routine
Definition: Task.cpp:130
Task feature is not supported.
Definition: Task.hpp:44
void resume() override
resume a suspended task
Definition: Task.cpp:159
void invoke()
invoke the run method with "self" as argument
Definition: Task.cpp:48
unable to allocate more tasks
Definition: Task.hpp:42
TaskInterface & operator=(const TaskInterface &other)=delete
assignment operator is forbidden
taskRoutine m_user_function
User function to run once started.
Definition: Task.hpp:220
FwTaskPriorityType m_priority
Definition: Task.hpp:91
static void init()
initialize singleton
Definition: Task.cpp:195
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:64
virtual bool isCooperative()
determine if the task requires cooperative multitasking
Definition: Task.cpp:56