Task.hpp 4.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293
  1. #ifndef _Task_hpp_
  2. #define _Task_hpp_
  3. #include <FpConfig.hpp>
  4. #include <Fw/Types/Serializable.hpp>
  5. #include <Os/TaskString.hpp>
  6. #include <Os/TaskId.hpp>
  7. #include <Fw/Deprecate.hpp>
  8. #include <limits>
  9. namespace Os {
  10. class TaskRegistry; //!< forward declaration
  11. class Task {
  12. public:
  13. static const NATIVE_UINT_TYPE TASK_DEFAULT;
  14. typedef enum {
  15. TASK_OK, //!< message sent/received okay
  16. TASK_INVALID_PARAMS, //!< started task with invalid parameters
  17. TASK_INVALID_STACK, //!< started with invalid stack size
  18. TASK_UNKNOWN_ERROR, //!< unexpected error return value
  19. TASK_INVALID_AFFINITY, //!< unable to set the task affinity
  20. TASK_DELAY_ERROR, //!< error trying to delay the task
  21. TASK_JOIN_ERROR, //!< error trying to join the task
  22. TASK_ERROR_RESOURCES, //!< unable to allocate more tasks
  23. TASK_ERROR_PERMISSION, //!< permissions error setting-up tasks
  24. } TaskStatus ;
  25. typedef void (*taskRoutine)(void* ptr); //!< prototype for task routine started in task context
  26. struct TaskRoutineWrapper {
  27. taskRoutine routine; //!< contains the task entrypoint
  28. void* arg; //!< contains the task entrypoint pointer
  29. };
  30. Task(); //!< constructor
  31. virtual ~Task(); //!< destructor
  32. TaskStatus start(const Fw::StringBase &name, taskRoutine routine, void* arg, NATIVE_UINT_TYPE priority = TASK_DEFAULT, NATIVE_UINT_TYPE stackSize = TASK_DEFAULT, NATIVE_UINT_TYPE cpuAffinity = TASK_DEFAULT, NATIVE_UINT_TYPE identifier = TASK_DEFAULT); //!< start the task
  33. // Deprecated: only the name, routine, and argument are **required** parameters. This ordering of parameters is therefore inappropriate and will be removed in the future
  34. DEPRECATED(TaskStatus start(const Fw::StringBase &name, NATIVE_INT_TYPE identifier, NATIVE_INT_TYPE priority, NATIVE_INT_TYPE stackSize, taskRoutine routine, void* arg, NATIVE_INT_TYPE cpuAffinity = static_cast<NATIVE_INT_TYPE>(TASK_DEFAULT)),
  35. "Please switch to start(Fw::StringBase &name, taskRoutine routine, void* arg, NATIVE_UINT_TYPE priority, NATIVE_UINT_TYPE stackSize, NATIVE_UINT_TYPE cpuAffinity, NATIVE_UINT_TYPE identifier)"); //!< start the task
  36. I32 getIdentifier(); //!< get the identifier for the task
  37. static TaskId getOsIdentifier(); //Gets the Os Task ID. Useful for passive components.
  38. static TaskStatus delay(NATIVE_UINT_TYPE msecs); //!< delay the task
  39. static NATIVE_INT_TYPE getNumTasks();
  40. TaskStatus join(void **value_ptr); //!< Wait for task to finish
  41. void suspend(bool onPurpose = false); //!< suspend task
  42. void resume(); //!< resume execution of task
  43. bool wasSuspended(); //!< returns whether or not task was suspended on purpose
  44. bool isSuspended(); //!< check with OS to see if it is suspended already
  45. bool isStarted(); //!< check to see if task is started
  46. void setStarted(bool started); //!< set task to started when thread is fully up. Avoids a VxWorks race condition.
  47. /**
  48. * Returns the task-handle owned by this task
  49. */
  50. POINTER_CAST getRawHandle();
  51. static void registerTaskRegistry(TaskRegistry* registry);
  52. private:
  53. POINTER_CAST m_handle; //!< handle for implementation specific task
  54. NATIVE_INT_TYPE m_identifier; //!< thread independent identifier
  55. TaskString m_name; //!< object name
  56. NATIVE_INT_TYPE m_affinity; //!< CPU affinity for SMP targets
  57. void toString(char* buf, NATIVE_INT_TYPE buffSize); //!< print a string of the state of the task
  58. bool m_started; //!< set when task has reached entry point
  59. bool m_suspendedOnPurpose; //!< set when task was suspended in purpose (i.e. simulation)
  60. TaskRoutineWrapper m_routineWrapper; //! Contains task entrypoint and argument for task wrapper
  61. static TaskRegistry* s_taskRegistry; //!< pointer to registered task
  62. static NATIVE_INT_TYPE s_numTasks; //!< stores the number of tasks created.
  63. };
  64. class TaskRegistry {
  65. public:
  66. TaskRegistry(); //!< constructor for task registry
  67. virtual ~TaskRegistry(); //!< destructor for task registry
  68. virtual void addTask(Task* task) = 0; //!< Add a task to the registry
  69. virtual void removeTask(Task* task) = 0; //!< remove a task from the registry
  70. private:
  71. };
  72. }
  73. #endif