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 namespace Os {
19 
20  // Forward declarations
21  class TaskRegistry;
22 
24  class TaskHandle {};
25 
26  class TaskInterface {
27  public:
28  static constexpr FwSizeType TASK_DEFAULT = std::numeric_limits<FwSizeType>::max();
29  static constexpr FwTaskPriorityType TASK_PRIORITY_DEFAULT = std::numeric_limits<FwTaskPriorityType>::max();
30  enum Status {
43  };
44 
48  };
49 
50  enum State {
58  };
59 
61  typedef void (*taskRoutine)(void* ptr);
62 
63  class Arguments {
64  public:
77  Arguments(const Fw::StringBase& name, const taskRoutine routine,
78  void* const routine_argument = nullptr,
80  const FwSizeType stackSize = TASK_DEFAULT,
81  const FwSizeType cpuAffinity = TASK_DEFAULT,
82  const PlatformUIntType identifier = static_cast<PlatformUIntType>(TASK_DEFAULT));
83 
84  public:
91  PlatformUIntType m_identifier;
92  };
93 
95  TaskInterface() = default;
96 
98  virtual ~TaskInterface() = default;
99 
101  TaskInterface(const TaskInterface& other) = delete;
102 
104  TaskInterface& operator=(const TaskInterface& other) = delete;
105 
106  // =================
107  // Implementation functions (static) to be supplied by the linker
108  // =================
109 
129  static TaskInterface* getDelegate(TaskHandleStorage& aligned_placement_new_memory);
130 
131  // =================
132  // Implementation functions (instance) to be supplied by the Os::TaskInterface children
133  // =================
134 
136  virtual void onStart() = 0;
137 
144  virtual Status join() = 0;
145 
153  virtual void suspend(SuspensionType suspensionType) = 0;
154 
159  virtual void resume() = 0;
160 
168  virtual Status _delay(Fw::TimeInterval interval) = 0;
169 
180  virtual bool isCooperative();
181 
184  virtual TaskHandle* getHandle() = 0;
185 
192  virtual Status start(const Arguments& arguments) = 0;
193  };
194 
198  class Task final : public TaskInterface {
199  public:
202  public:
203  explicit TaskRoutineWrapper(Task& self);
204 
210  static void run(void* task_pointer);
211 
213  void invoke();
214 
217  void* m_user_argument = nullptr;
218  };
219 
222 
224  Task();
225 
227  ~Task() final;
228 
230  Task(const Task& other) = delete;
231 
233  Task& operator=(const Task& other) = delete;
234 
239  void suspend();
240 
247  State getState();
248 
263  DEPRECATED(Status start(const Fw::StringBase &name, const taskRoutine routine, void* const arg = nullptr,
264  const FwTaskPriorityType priority = TASK_PRIORITY_DEFAULT,
265  const ParamType stackSize = TASK_DEFAULT,
266  const ParamType cpuAffinity = TASK_DEFAULT,
267  const ParamType identifier = TASK_DEFAULT), "Switch to Task::start(Arguments&)");
268 
274  //
279  Status start(const Arguments& arguments) override;
280 
282  void onStart() override;
283 
285  //~
288  void invokeRoutine();
289 
297  DEPRECATED(Status join(void** value_ptr), "Please switch to argument free join.");
298 
305  Status join() override;
306 
314  void suspend(SuspensionType suspensionType) override;
315 
320  void resume() override;
321 
329  Status _delay(Fw::TimeInterval interval) override;
330 
333  bool isCooperative() override;
334 
337 
340  TaskHandle* getHandle() override;
341 
343  static void init();
344 
347  static FwSizeType getNumTasks();
348 
351  static void registerTaskRegistry(TaskRegistry* registry);
352 
355  static Task& getSingleton();
356 
364  static Status delay(Fw::TimeInterval interval);
365 
366  PRIVATE:
367  static TaskRegistry* s_taskRegistry;
368  static FwSizeType s_numTasks;
369  static Mutex s_taskMutex;
370 
371  TaskString m_name;
372  TaskInterface::State m_state = Task::NOT_STARTED;
373  Mutex m_lock;
374  TaskRoutineWrapper m_wrapper;
375  FwTaskPriorityType m_priority = 0; // Storage of priority
376 
377  bool m_registered = false;
378 
379  // This section is used to store the implementation-defined file handle. To Os::File and fprime, this type is
380  // opaque and thus normal allocation cannot be done. Instead, we allow the implementor to store then handle in
381  // the byte-array here and set `handle` to that address for storage.
382  //
383  alignas(FW_HANDLE_ALIGNMENT) TaskHandleStorage m_handle_storage;
384  TaskInterface& m_delegate;
385  };
386 
387  class TaskRegistry {
388  public:
390  TaskRegistry() = default;
392  virtual ~TaskRegistry() = default;
396  virtual void addTask(Task* task) = 0;
397 
401  virtual void removeTask(Task* task) = 0;
402  };
403 }
404 
405 #endif
error trying to join the task
Definition: Task.hpp:38
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:28
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 PlatformUIntType identifier=static_cast< PlatformUIntType >(TASK_DEFAULT))
construct a set of arguments to start a task
Definition: Task.cpp:10
TaskHandle * getHandle() override
return the underlying task handle (implementation specific)
Definition: Task.cpp:174
Task handle representation.
Definition: Task.hpp:24
unable to set the task affinity
Definition: Task.hpp:36
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
PlatformUIntType m_identifier
Definition: Task.hpp:91
Task is in an invalid state for the operation.
Definition: Task.hpp:42
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:33
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:34
permissions error setting-up tasks
Definition: Task.hpp:40
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:217
static constexpr FwTaskPriorityType TASK_PRIORITY_DEFAULT
Definition: Task.hpp:29
error trying to delay the task
Definition: Task.hpp:37
Task handle invalid.
Definition: Task.hpp:32
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:35
message sent/received okay
Definition: Task.hpp:31
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:201
Task & m_task
Reference to owning task.
Definition: Task.hpp:215
const Os::TaskString m_name
Definition: Task.hpp:85
PlatformTaskPriorityType FwTaskPriorityType
The type of task priorities used.
FwSizeType m_cpuAffinity
Definition: Task.hpp:90
FwSizeType ParamType
backwards-compatible parameter type
Definition: Task.hpp:221
void onStart() override
perform delegate&#39;s required task start actions
Definition: Task.cpp:125
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:41
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:39
TaskInterface & operator=(const TaskInterface &other)=delete
assignment operator is forbidden
taskRoutine m_user_function
User function to run once started.
Definition: Task.hpp:216
FwTaskPriorityType m_priority
Definition: Task.hpp:88
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:61
virtual bool isCooperative()
determine if the task requires cooperative multitasking
Definition: Task.cpp:56