rtsched.h 5.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147
  1. /*
  2. * Copyright (c) 2023-2024, RT-Thread Development Team
  3. *
  4. * SPDX-License-Identifier: Apache-2.0
  5. *
  6. * Change Logs:
  7. * Date Author Notes
  8. * 2024-01-19 Shell Separate scheduling statements from rt_thread_t
  9. * to rt_sched_thread_ctx. Add definitions of scheduler.
  10. */
  11. #ifndef __RT_SCHED_H__
  12. #define __RT_SCHED_H__
  13. #include "rttypes.h"
  14. #include "rtcompiler.h"
  15. #ifdef __cplusplus
  16. extern "C" {
  17. #endif
  18. struct rt_thread;
  19. typedef rt_uint8_t rt_sched_thread_status_t;
  20. /**
  21. * Scheduler private status binding on thread. Caller should never accessing
  22. * these members.
  23. */
  24. struct rt_sched_thread_priv
  25. {
  26. rt_tick_t init_tick; /**< thread's initialized tick */
  27. rt_tick_t remaining_tick; /**< remaining tick */
  28. /* priority */
  29. rt_uint8_t current_priority; /**< current priority */
  30. rt_uint8_t init_priority; /**< initialized priority */
  31. #if RT_THREAD_PRIORITY_MAX > 32
  32. rt_uint8_t number; /**< priority low number */
  33. rt_uint8_t high_mask; /**< priority high mask */
  34. #endif /* RT_THREAD_PRIORITY_MAX > 32 */
  35. rt_uint32_t number_mask; /**< priority number mask */
  36. };
  37. /**
  38. * Scheduler public status binding on thread. Caller must hold the scheduler
  39. * lock before access any one of its member.
  40. */
  41. struct rt_sched_thread_ctx
  42. {
  43. rt_list_t thread_list_node; /**< node in thread list */
  44. rt_uint8_t stat; /**< thread status */
  45. rt_uint8_t sched_flag_locked:1; /**< calling thread have the scheduler locked */
  46. rt_uint8_t sched_flag_ttmr_set:1; /**< thread timer is start */
  47. #ifdef ARCH_USING_HW_THREAD_SELF
  48. rt_uint8_t critical_switch_flag:1; /**< critical switch pending */
  49. #endif /* ARCH_USING_HW_THREAD_SELF */
  50. #ifdef RT_USING_SMP
  51. rt_uint8_t bind_cpu; /**< thread is bind to cpu */
  52. rt_uint8_t oncpu; /**< process on cpu */
  53. rt_base_t critical_lock_nest; /**< critical lock count */
  54. #endif
  55. struct rt_sched_thread_priv sched_thread_priv; /**< private context of scheduler */
  56. };
  57. #define RT_SCHED_THREAD_CTX struct rt_sched_thread_ctx sched_thread_ctx;
  58. #define RT_SCHED_PRIV(thread) ((thread)->sched_thread_ctx.sched_thread_priv)
  59. #define RT_SCHED_CTX(thread) ((thread)->sched_thread_ctx)
  60. /**
  61. * Convert a list node in container RT_SCHED_CTX(thread)->thread_list_node
  62. * to a thread pointer.
  63. */
  64. #define RT_THREAD_LIST_NODE_ENTRY(node) \
  65. rt_container_of( \
  66. rt_list_entry((node), struct rt_sched_thread_ctx, thread_list_node), \
  67. struct rt_thread, sched_thread_ctx)
  68. #define RT_THREAD_LIST_NODE(thread) (RT_SCHED_CTX(thread).thread_list_node)
  69. /**
  70. * System Scheduler Locking
  71. */
  72. typedef rt_ubase_t rt_sched_lock_level_t;
  73. rt_err_t rt_sched_lock(rt_sched_lock_level_t *plvl);
  74. rt_err_t rt_sched_unlock(rt_sched_lock_level_t level);
  75. rt_err_t rt_sched_unlock_n_resched(rt_sched_lock_level_t level);
  76. rt_bool_t rt_sched_is_locked(void);
  77. #ifdef RT_USING_SMP
  78. #define RT_SCHED_DEBUG_IS_LOCKED do { RT_ASSERT(rt_sched_is_locked()); } while (0)
  79. #define RT_SCHED_DEBUG_IS_UNLOCKED do { RT_ASSERT(!rt_sched_is_locked()); } while (0)
  80. #else /* !RT_USING_SMP */
  81. #define RT_SCHED_DEBUG_IS_LOCKED
  82. #define RT_SCHED_DEBUG_IS_UNLOCKED
  83. #endif /* RT_USING_SMP */
  84. /**
  85. * NOTE: user should NEVER use these APIs directly. See rt_thread_.* or IPC
  86. * methods instead.
  87. */
  88. #if defined(__RT_KERNEL_SOURCE__) || defined(__RT_IPC_SOURCE__)
  89. /* thread initialization and startup routine */
  90. void rt_sched_thread_init_ctx(struct rt_thread *thread, rt_uint32_t tick, rt_uint8_t priority);
  91. void rt_sched_thread_init_priv(struct rt_thread *thread, rt_uint32_t tick, rt_uint8_t priority);
  92. void rt_sched_thread_startup(struct rt_thread *thread);
  93. /* scheduler related routine */
  94. void rt_sched_post_ctx_switch(struct rt_thread *thread);
  95. rt_err_t rt_sched_tick_increase(rt_tick_t tick);
  96. /* thread status operation */
  97. rt_uint8_t rt_sched_thread_get_stat(struct rt_thread *thread);
  98. rt_uint8_t rt_sched_thread_get_curr_prio(struct rt_thread *thread);
  99. rt_uint8_t rt_sched_thread_get_init_prio(struct rt_thread *thread);
  100. rt_err_t rt_sched_thread_yield(struct rt_thread *thread);
  101. rt_err_t rt_sched_thread_close(struct rt_thread *thread);
  102. rt_err_t rt_sched_thread_ready(struct rt_thread *thread);
  103. rt_err_t rt_sched_thread_suspend(struct rt_thread *thread, rt_sched_lock_level_t level);
  104. rt_err_t rt_sched_thread_change_priority(struct rt_thread *thread, rt_uint8_t priority);
  105. rt_err_t rt_sched_thread_reset_priority(struct rt_thread *thread, rt_uint8_t priority);
  106. rt_err_t rt_sched_thread_bind_cpu(struct rt_thread *thread, int cpu);
  107. rt_uint8_t rt_sched_thread_is_suspended(struct rt_thread *thread);
  108. rt_err_t rt_sched_thread_timer_stop(struct rt_thread *thread);
  109. rt_err_t rt_sched_thread_timer_start(struct rt_thread *thread);
  110. void rt_sched_insert_thread(struct rt_thread *thread);
  111. void rt_sched_remove_thread(struct rt_thread *thread);
  112. struct rt_thread *rt_sched_thread_self(void);
  113. #endif /* defined(__RT_KERNEL_SOURCE__) || defined(__RT_IPC_SOURCE__) */
  114. #ifdef __cplusplus
  115. }
  116. #endif
  117. #endif /* __RT_SCHED_H__ */