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 
8 #include <FpConfig.hpp>
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  enum Status {
42  };
43 
47  };
48 
49  enum State {
57  };
58 
60  typedef void (*taskRoutine)(void* ptr);
61 
62  class Arguments {
63  public:
76  Arguments(const Fw::StringBase& name, const taskRoutine routine,
77  void* const routine_argument = nullptr,
78  const FwSizeType priority = TASK_DEFAULT,
79  const FwSizeType stackSize = TASK_DEFAULT,
80  const FwSizeType cpuAffinity = TASK_DEFAULT,
81  const PlatformUIntType identifier = static_cast<PlatformUIntType>(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  public:
201  public:
202  explicit TaskRoutineWrapper(Task& self);
203 
209  static void run(void* task_pointer);
210 
212  void invoke();
213 
216  void* m_user_argument = nullptr;
217  };
218 
221 
223  Task();
224 
226  ~Task() final;
227 
229  Task(const Task& other) = delete;
230 
232  Task& operator=(const Task& other) = delete;
233 
238  void suspend();
239 
246  State getState();
247 
262  DEPRECATED(Status start(const Fw::StringBase &name, const taskRoutine routine, void* const arg = nullptr,
263  const ParamType priority = TASK_DEFAULT,
264  const ParamType stackSize = TASK_DEFAULT,
265  const ParamType cpuAffinity = TASK_DEFAULT,
266  const ParamType identifier = TASK_DEFAULT), "Switch to Task::start(Arguments&)");
267 
273  //
278  Status start(const Arguments& arguments) override;
279 
281  void onStart() override;
282 
284  //~
287  void invokeRoutine();
288 
296  DEPRECATED(Status join(void** value_ptr), "Please switch to argument free join.");
297 
304  Status join() override;
305 
313  void suspend(SuspensionType suspensionType) override;
314 
319  void resume() override;
320 
328  Status _delay(Fw::TimeInterval interval) override;
329 
332  bool isCooperative() override;
333 
336 
339  TaskHandle* getHandle() override;
340 
342  static void init();
343 
346  static FwSizeType getNumTasks();
347 
350  static void registerTaskRegistry(TaskRegistry* registry);
351 
354  static Task& getSingleton();
355 
363  static Status delay(Fw::TimeInterval interval);
364 
365  PRIVATE:
366  static TaskRegistry* s_taskRegistry;
367  static FwSizeType s_numTasks;
368  static Mutex s_taskMutex;
369 
370  TaskString m_name;
371  TaskInterface::State m_state = Task::NOT_STARTED;
372  Mutex m_lock;
373  TaskRoutineWrapper m_wrapper;
374  FwSizeType m_priority = 0; // Storage of priority
375 
376  bool m_registered = false;
377 
378  // This section is used to store the implementation-defined file handle. To Os::File and fprime, this type is
379  // opaque and thus normal allocation cannot be done. Instead, we allow the implementor to store then handle in
380  // the byte-array here and set `handle` to that address for storage.
381  //
382  alignas(FW_HANDLE_ALIGNMENT) TaskHandleStorage m_handle_storage;
383  TaskInterface& m_delegate;
384  };
385 
386  class TaskRegistry {
387  public:
389  TaskRegistry() = default;
391  virtual ~TaskRegistry() = default;
395  virtual void addTask(Task* task) = 0;
396 
400  virtual void removeTask(Task* task) = 0;
401  };
402 }
403 
404 #endif
error trying to join the task
Definition: Task.hpp:37
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
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:35
virtual void resume()=0
resume a suspended task
static Task & getSingleton()
get a reference to singleton
Definition: Task.cpp:200
PlatformUIntType m_identifier
Definition: Task.hpp:90
Task is in an invalid state for the operation.
Definition: Task.hpp:41
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
PlatformSizeType FwSizeType
Definition: FpConfig.h:35
started task with invalid parameters
Definition: Task.hpp:32
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:33
permissions error setting-up tasks
Definition: Task.hpp:39
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
void * m_user_argument
Argument to user function.
Definition: Task.hpp:216
Arguments(const Fw::StringBase &name, const taskRoutine routine, void *const routine_argument=nullptr, const FwSizeType priority=TASK_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
error trying to delay the task
Definition: Task.hpp:36
Task handle invalid.
Definition: Task.hpp:31
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
FwSizeType getPriority()
get the task priority
Definition: Task.cpp:169
DEPRECATED(Status start(const Fw::StringBase &name, const taskRoutine routine, void *const arg=nullptr, const ParamType priority=TASK_DEFAULT, const ParamType stackSize=TASK_DEFAULT, const ParamType cpuAffinity=TASK_DEFAULT, const ParamType identifier=TASK_DEFAULT), "Switch to Task::start(Arguments&)")
start this task
virtual void suspend(SuspensionType suspensionType)=0
suspend the task given the suspension type
unexpected error return value
Definition: Task.hpp:34
message sent/received okay
Definition: Task.hpp:30
virtual Status start(const Arguments &arguments)=0
start the task
void suspend()
suspend the current task
Definition: Task.cpp:70
unsigned int PlatformUIntType
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:200
Task & m_task
Reference to owning task.
Definition: Task.hpp:214
const Os::TaskString m_name
Definition: Task.hpp:84
C++-compatible configuration header for fprime configuration.
FwSizeType m_cpuAffinity
Definition: Task.hpp:89
FwSizeType ParamType
backwards-compatible parameter type
Definition: Task.hpp:220
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:40
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:38
TaskInterface & operator=(const TaskInterface &other)=delete
assignment operator is forbidden
taskRoutine m_user_function
User function to run once started.
Definition: Task.hpp:215
static void init()
initialize singleton
Definition: Task.cpp:195
#define FW_HANDLE_ALIGNMENT
Alignment of handle storage.
Definition: FpConfig.h:440
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:60
virtual bool isCooperative()
determine if the task requires cooperative multitasking
Definition: Task.cpp:56