kernel.h 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129
  1. #ifndef CX_ZIVUOS_KERNEL_H_INCLUDED
  2. #define CX_ZIVUOS_KERNEL_H_INCLUDED
  3. #include <stdint.h>
  4. #include <stdbool.h>
  5. #include "zv_types.h"
  6. #include "task.h"
  7. #include "idle.h"
  8. /**
  9. * @brief That return number of tasks from tasks array (x for zv_task_t [x]).
  10. * Usefull for zv_kernel_init.
  11. *
  12. * @param X Tasks stack.
  13. * @return Tasks count on tasks stack.
  14. */
  15. #define ZV_COUNT_TASKS(X) (sizeof(X) / sizeof(zv_task_t))
  16. /**
  17. * @brief That is main kernel structure. Do not modify without API.
  18. *
  19. * It store pointer to tasks stack, size of that stack and idle worker.
  20. */
  21. typedef struct zv_kernel_s {
  22. /** Pointer to tasks stack. */
  23. zv_task_t *tasks;
  24. /** Size of tasks stack. */
  25. zv_tid_t tasks_count;
  26. /** That is idle worker. By default do nothing. Could be replaced. */
  27. zv_idle_worker_t idle_worker;
  28. } zv_kernel_t;
  29. /**
  30. * @brief That initialize kernel structure.
  31. *
  32. * That function initialize kernel structure. It must be called to initialize
  33. * kernel.
  34. * Kernel initialization require tasks stack and kernel structure which is
  35. * NULL pointers, and tasks stack count which must be greater than 0.
  36. *
  37. * @param self Pointer to kernel structure to work on.
  38. * @param tasks Pointer to tasks stack.
  39. * @param count Number of tasks slots on stack.
  40. * @return True when kernel initialize success, false if cannot initialize it.
  41. */
  42. bool zv_kernel_init(zv_kernel_t *self, zv_task_t *tasks, zv_tid_t count);
  43. /**
  44. * @brief That boot OS. It return when task stack is empty.
  45. *
  46. * That is main kernel loop. By default it would not return.
  47. * It could be break only when tasks stack is empty, or when any task
  48. * raise error.
  49. *
  50. * @return True when everything is executed, and all tasks had been killed.
  51. * False when something went wrong.
  52. */
  53. bool zv_kernel_boot(zv_kernel_t *self);
  54. /**
  55. * @brief That function create process.
  56. *
  57. * That function create process, require process worker, process priority
  58. * and process param. Process param is pointer to any data or number as
  59. * intptr_r or uintptr_t. It return task ID or ZV_TID_NULL when can not
  60. * create process. Process creation fails when worker is NULL pointer or
  61. * when tasks stack is full.
  62. *
  63. * @param self Kernel to work on.
  64. * @param worker Worker of the process.
  65. * @param priority Priority of the process. Greater number is bigger priority.
  66. * @param param Param of the process.
  67. * @return Task ID on success, ZV_TID_NULL on fail.
  68. */
  69. zv_tid_t zv_kernel_create_process(
  70. zv_kernel_t *self,
  71. zv_worker_t worker,
  72. zv_priority_t priority,
  73. void *param
  74. );
  75. /**
  76. * @brief That function kill process.
  77. *
  78. * That function kill process. Process can not being killed when not exists.
  79. *
  80. * @param self Kernel to work on.
  81. * @param target Task ID of task to kill.
  82. * @return True when task killed successfull, false when failed.
  83. */
  84. bool zv_kernel_kill_process(
  85. zv_kernel_t *self,
  86. zv_tid_t target
  87. );
  88. /**
  89. * That function return first empty slot TID or ZV_TID_NULL when tasks stack
  90. * is full.
  91. *
  92. * @param self Kernel to work on.
  93. * @return First empty task slot TID or ZV_TID_NULL when tasks stack is full.
  94. */
  95. zv_tid_t __zv_kernel_get_empty_slot(zv_kernel_t *self);
  96. /**
  97. * That function return pointer to task based on task ID.
  98. *
  99. * @param self Kernel to work on.
  100. * @param target Task ID to get pointer.
  101. * @return Pointer to task with given ID or NULL when task slot not exists.
  102. */
  103. zv_task_t * __zv_kernel_get_task(zv_kernel_t *self, zv_tid_t target);
  104. /**
  105. * That function return task ID based on pointer to task on task stack.
  106. *
  107. * @param self Kernel to work on.
  108. * @param target Pointer to task.
  109. * @return Task ID of that task.
  110. */
  111. zv_tid_t __zv_kernel_get_tid(zv_kernel_t *self, zv_task_t *target);
  112. #endif