11/*
2- * Copyright (c) 2006-2024 RT-Thread Development Team
2+ * Copyright (c) 2006-2025 RT-Thread Development Team
33 *
44 * SPDX-License-Identifier: Apache-2.0
55 *
1616
1717#include <rtthread.h>
1818
19+ /**
20+ * @brief Initialize thread scheduling context
21+ *
22+ * @param thread The thread to be initialized
23+ * @param tick Initial time slice value for the thread
24+ * @param priority Initial priority of the thread
25+ *
26+ * @details This function performs the following initialization:
27+ * - Sets thread status to INIT
28+ * - For SMP systems:
29+ * * Sets bind CPU to none (RT_CPUS_NR)
30+ * * Marks CPU as detached (RT_CPU_DETACHED)
31+ * - Calls rt_sched_thread_init_priv() for private scheduling data initialization
32+ */
1933void rt_sched_thread_init_ctx (struct rt_thread * thread , rt_uint32_t tick , rt_uint8_t priority )
2034{
2135 /* setup thread status */
@@ -30,13 +44,33 @@ void rt_sched_thread_init_ctx(struct rt_thread *thread, rt_uint32_t tick, rt_uin
3044 rt_sched_thread_init_priv (thread , tick , priority );
3145}
3246
47+ /**
48+ * @brief Start the thread timer for scheduling
49+ *
50+ * @param thread The thread whose timer needs to be started
51+ *
52+ * @return rt_err_t Always returns RT_EOK on success
53+ *
54+ * @details This function:
55+ * - Requires scheduler lock to be held.
56+ * - Sets the thread's timer flag (sched_flag_ttmr_set) to indicate timer is active
57+ */
3358rt_err_t rt_sched_thread_timer_start (struct rt_thread * thread )
3459{
3560 RT_SCHED_DEBUG_IS_LOCKED ;
3661 RT_SCHED_CTX (thread ).sched_flag_ttmr_set = 1 ;
3762 return RT_EOK ;
3863}
3964
65+ /**
66+ * @brief Stop the thread timer for scheduling
67+ *
68+ * @param thread The thread whose timer needs to be stopped
69+ *
70+ * @return rt_err_t
71+ * - RT_EOK if timer was successfully stopped or not active
72+ * - Other error codes from rt_timer_stop() if stop operation failed
73+ */
4074rt_err_t rt_sched_thread_timer_stop (struct rt_thread * thread )
4175{
4276 rt_err_t error ;
@@ -56,40 +90,110 @@ rt_err_t rt_sched_thread_timer_stop(struct rt_thread *thread)
5690 return error ;
5791}
5892
93+ /**
94+ * @brief Get the current status of a thread
95+ *
96+ * @param thread The thread to get status from
97+ *
98+ * @return rt_uint8_t The thread status masked with RT_THREAD_STAT_MASK
99+ *
100+ * @details This function:
101+ * - Requires scheduler lock to be held (RT_SCHED_DEBUG_IS_LOCKED)
102+ * - Returns the thread's status field masked with RT_THREAD_STAT_MASK
103+ */
59104rt_uint8_t rt_sched_thread_get_stat (struct rt_thread * thread )
60105{
61106 RT_SCHED_DEBUG_IS_LOCKED ;
62107 return RT_SCHED_CTX (thread ).stat & RT_THREAD_STAT_MASK ;
63108}
64109
110+ /**
111+ * @brief Get the current priority of a thread
112+ *
113+ * @param thread The thread to get priority from
114+ *
115+ * @return rt_uint8_t The current priority value of the thread
116+ *
117+ * @details This function:
118+ * - Requires scheduler lock to be held (RT_SCHED_DEBUG_IS_LOCKED)
119+ * - Returns the thread's current priority field from its private scheduling data
120+ */
65121rt_uint8_t rt_sched_thread_get_curr_prio (struct rt_thread * thread )
66122{
67123 RT_SCHED_DEBUG_IS_LOCKED ;
68124 return RT_SCHED_PRIV (thread ).current_priority ;
69125}
70126
127+ /**
128+ * @brief Get the initial priority of a thread
129+ *
130+ * @param thread The thread to get priority from
131+ *
132+ * @return rt_uint8_t The initial priority value of the thread
133+ *
134+ * @details This function:
135+ * - Returns the thread's initial priority field from its private scheduling data
136+ * - Does not require scheduler lock as it accesses read-only fields
137+ */
71138rt_uint8_t rt_sched_thread_get_init_prio (struct rt_thread * thread )
72139{
73140 /* read only fields, so lock is unecessary */
74141 return RT_SCHED_PRIV (thread ).init_priority ;
75142}
76143
77144/**
78- * @note Caller must hold the scheduler lock
145+ * @brief Check if a thread is in suspended state
146+ *
147+ * @param thread The thread to check
148+ *
149+ * @return rt_uint8_t
150+ * - 1 if thread is suspended (matches RT_THREAD_SUSPEND_MASK)
151+ * - 0 otherwise
152+ *
153+ * @details This function:
154+ * - Requires scheduler lock to be held (RT_SCHED_DEBUG_IS_LOCKED)
155+ * - Checks thread's status field against RT_THREAD_SUSPEND_MASK
156+ *
157+ * @note Caller must hold the scheduler lock before calling this function
79158 */
80159rt_uint8_t rt_sched_thread_is_suspended (struct rt_thread * thread )
81160{
82161 RT_SCHED_DEBUG_IS_LOCKED ;
83162 return (RT_SCHED_CTX (thread ).stat & RT_THREAD_SUSPEND_MASK ) == RT_THREAD_SUSPEND_MASK ;
84163}
85164
165+ /**
166+ * @brief Close a thread by setting its status to CLOSED
167+ *
168+ * @param thread The thread to be closed
169+ * @return rt_err_t Always returns RT_EOK on success
170+ *
171+ * @details This function:
172+ * - Requires scheduler lock to be held (RT_SCHED_DEBUG_IS_LOCKED)
173+ * - Sets the thread's status to RT_THREAD_CLOSE
174+ *
175+ * @note Must be called with scheduler lock held
176+ */
86177rt_err_t rt_sched_thread_close (struct rt_thread * thread )
87178{
88179 RT_SCHED_DEBUG_IS_LOCKED ;
89180 RT_SCHED_CTX (thread ).stat = RT_THREAD_CLOSE ;
90181 return RT_EOK ;
91182}
92183
184+ /**
185+ * @brief Yield the current thread's remaining time slice
186+ *
187+ * @param thread The thread to yield
188+ * @return rt_err_t Always returns RT_EOK on success
189+ *
190+ * @details This function:
191+ * - Requires scheduler lock to be held (RT_SCHED_DEBUG_IS_LOCKED)
192+ * - Resets the thread's remaining tick count to its initial value
193+ * - Sets the thread's status to YIELD state
194+ *
195+ * @note Must be called with scheduler lock held
196+ */
93197rt_err_t rt_sched_thread_yield (struct rt_thread * thread )
94198{
95199 RT_SCHED_DEBUG_IS_LOCKED ;
@@ -100,6 +204,27 @@ rt_err_t rt_sched_thread_yield(struct rt_thread *thread)
100204 return RT_EOK ;
101205}
102206
207+ /**
208+ * @brief Make a suspended thread ready for scheduling
209+ *
210+ * @param thread The thread to be made ready
211+ *
212+ * @return rt_err_t
213+ * - RT_EOK if operation succeeded
214+ * - -RT_EINVAL if thread is not suspended
215+ * - Other error codes from rt_sched_thread_timer_stop() if timer stop failed
216+ *
217+ * @details This function:
218+ * - Requires scheduler lock to be held (RT_SCHED_DEBUG_IS_LOCKED)
219+ * - Checks if thread is suspended (returns -RT_EINVAL if not)
220+ * - Stops thread timer if active
221+ * - Removes thread from suspend list
222+ * - Clears wakeup handler (if RT_USING_SMART is defined)
223+ * - Inserts thread into ready queue
224+ *
225+ * @note Must be called with scheduler lock held
226+ * May fail due to racing conditions with timeout ISR
227+ */
103228rt_err_t rt_sched_thread_ready (struct rt_thread * thread )
104229{
105230 rt_err_t error ;
@@ -144,6 +269,24 @@ rt_err_t rt_sched_thread_ready(struct rt_thread *thread)
144269 return error ;
145270}
146271
272+ /**
273+ * @brief Increase the system tick and update thread's remaining time slice
274+ *
275+ * @param tick The number of ticks to increase
276+ * @return rt_err_t Always returns RT_EOK
277+ *
278+ * @details This function:
279+ * - Gets the current thread
280+ * - Locks the scheduler
281+ * - Decreases the thread's remaining tick count by the specified amount
282+ * - If remaining ticks reach zero:
283+ * * Calls rt_sched_thread_yield() to yield the thread
284+ * * Requests a reschedule with rt_sched_unlock_n_resched()
285+ * - Otherwise simply unlocks the scheduler
286+ *
287+ * @note This function is typically called from timer interrupt context
288+ * It handles both SMP and non-SMP cases
289+ */
147290rt_err_t rt_sched_tick_increase (rt_tick_t tick )
148291{
149292 struct rt_thread * thread ;
@@ -178,7 +321,26 @@ rt_err_t rt_sched_tick_increase(rt_tick_t tick)
178321}
179322
180323/**
181- * @brief Update priority of the target thread
324+ * @brief Update thread priority and adjust scheduling attributes
325+ *
326+ * @param thread The thread to update priority for
327+ * @param priority New priority value to set
328+ * @param update_init_prio Flag to determine if initial priority should also be updated
329+ * @return rt_err_t Always returns RT_EOK on success
330+ *
331+ * @details This function:
332+ * - Requires scheduler lock to be held (RT_SCHED_DEBUG_IS_LOCKED)
333+ * - For ready threads:
334+ * * Removes from ready queue
335+ * * Updates priority values
336+ * * Recalculates priority attributes (number, mask, etc.)
337+ * * Reinserts into ready queue with new priority
338+ * - For non-ready threads:
339+ * * Only updates priority values and attributes
340+ * - Handles both 32-bit and >32-bit priority systems
341+ *
342+ * @note Must be called with scheduler lock held
343+ * Thread status must be valid before calling
182344 */
183345static rt_err_t _rt_sched_update_priority (struct rt_thread * thread , rt_uint8_t priority , rt_bool_t update_init_prio )
184346{
@@ -249,6 +411,19 @@ rt_err_t rt_sched_thread_reset_priority(struct rt_thread *thread, rt_uint8_t pri
249411}
250412
251413#ifdef RT_USING_OVERFLOW_CHECK
414+ /**
415+ * @brief Check thread stack for overflow or near-overflow conditions
416+ *
417+ * @param thread The thread to check stack for
418+ *
419+ * @details This function performs the following checks:
420+ * - For SMART mode without MMU: skips check if SP is in user data section
421+ * - Without hardware stack guard:
422+ * * For upward-growing stacks: checks magic number at top and SP range
423+ * * For downward-growing stacks: checks magic number at bottom and SP range
424+ * * Triggers error and infinite loop on overflow
425+ * - Additional warnings when stack pointer is near boundaries
426+ */
252427void rt_scheduler_stack_check (struct rt_thread * thread )
253428{
254429 RT_ASSERT (thread != RT_NULL );
@@ -306,4 +481,4 @@ void rt_scheduler_stack_check(struct rt_thread *thread)
306481#endif /* ARCH_CPU_STACK_GROWS_UPWARD */
307482}
308483
309- #endif /* RT_USING_OVERFLOW_CHECK */
484+ #endif /* RT_USING_OVERFLOW_CHECK */
0 commit comments