1
1
/*
2
- * Copyright (c) 2006-2024 RT-Thread Development Team
2
+ * Copyright (c) 2006-2025 RT-Thread Development Team
3
3
*
4
4
* SPDX-License-Identifier: Apache-2.0
5
5
*
16
16
17
17
#include <rtthread.h>
18
18
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
+ */
19
33
void rt_sched_thread_init_ctx (struct rt_thread * thread , rt_uint32_t tick , rt_uint8_t priority )
20
34
{
21
35
/* setup thread status */
@@ -30,13 +44,33 @@ void rt_sched_thread_init_ctx(struct rt_thread *thread, rt_uint32_t tick, rt_uin
30
44
rt_sched_thread_init_priv (thread , tick , priority );
31
45
}
32
46
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
+ */
33
58
rt_err_t rt_sched_thread_timer_start (struct rt_thread * thread )
34
59
{
35
60
RT_SCHED_DEBUG_IS_LOCKED ;
36
61
RT_SCHED_CTX (thread ).sched_flag_ttmr_set = 1 ;
37
62
return RT_EOK ;
38
63
}
39
64
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
+ */
40
74
rt_err_t rt_sched_thread_timer_stop (struct rt_thread * thread )
41
75
{
42
76
rt_err_t error ;
@@ -56,40 +90,110 @@ rt_err_t rt_sched_thread_timer_stop(struct rt_thread *thread)
56
90
return error ;
57
91
}
58
92
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
+ */
59
104
rt_uint8_t rt_sched_thread_get_stat (struct rt_thread * thread )
60
105
{
61
106
RT_SCHED_DEBUG_IS_LOCKED ;
62
107
return RT_SCHED_CTX (thread ).stat & RT_THREAD_STAT_MASK ;
63
108
}
64
109
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
+ */
65
121
rt_uint8_t rt_sched_thread_get_curr_prio (struct rt_thread * thread )
66
122
{
67
123
RT_SCHED_DEBUG_IS_LOCKED ;
68
124
return RT_SCHED_PRIV (thread ).current_priority ;
69
125
}
70
126
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
+ */
71
138
rt_uint8_t rt_sched_thread_get_init_prio (struct rt_thread * thread )
72
139
{
73
- /* read only fields, so lock is unecessary */
140
+ /* read only fields, so lock is unnecessary */
74
141
return RT_SCHED_PRIV (thread ).init_priority ;
75
142
}
76
143
77
144
/**
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
79
158
*/
80
159
rt_uint8_t rt_sched_thread_is_suspended (struct rt_thread * thread )
81
160
{
82
161
RT_SCHED_DEBUG_IS_LOCKED ;
83
162
return (RT_SCHED_CTX (thread ).stat & RT_THREAD_SUSPEND_MASK ) == RT_THREAD_SUSPEND_MASK ;
84
163
}
85
164
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
+ */
86
177
rt_err_t rt_sched_thread_close (struct rt_thread * thread )
87
178
{
88
179
RT_SCHED_DEBUG_IS_LOCKED ;
89
180
RT_SCHED_CTX (thread ).stat = RT_THREAD_CLOSE ;
90
181
return RT_EOK ;
91
182
}
92
183
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
+ */
93
197
rt_err_t rt_sched_thread_yield (struct rt_thread * thread )
94
198
{
95
199
RT_SCHED_DEBUG_IS_LOCKED ;
@@ -100,6 +204,27 @@ rt_err_t rt_sched_thread_yield(struct rt_thread *thread)
100
204
return RT_EOK ;
101
205
}
102
206
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
+ */
103
228
rt_err_t rt_sched_thread_ready (struct rt_thread * thread )
104
229
{
105
230
rt_err_t error ;
@@ -144,6 +269,24 @@ rt_err_t rt_sched_thread_ready(struct rt_thread *thread)
144
269
return error ;
145
270
}
146
271
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
+ */
147
290
rt_err_t rt_sched_tick_increase (rt_tick_t tick )
148
291
{
149
292
struct rt_thread * thread ;
@@ -178,7 +321,26 @@ rt_err_t rt_sched_tick_increase(rt_tick_t tick)
178
321
}
179
322
180
323
/**
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
182
344
*/
183
345
static rt_err_t _rt_sched_update_priority (struct rt_thread * thread , rt_uint8_t priority , rt_bool_t update_init_prio )
184
346
{
@@ -249,6 +411,19 @@ rt_err_t rt_sched_thread_reset_priority(struct rt_thread *thread, rt_uint8_t pri
249
411
}
250
412
251
413
#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
+ */
252
427
void rt_scheduler_stack_check (struct rt_thread * thread )
253
428
{
254
429
RT_ASSERT (thread != RT_NULL );
@@ -306,4 +481,4 @@ void rt_scheduler_stack_check(struct rt_thread *thread)
306
481
#endif /* ARCH_CPU_STACK_GROWS_UPWARD */
307
482
}
308
483
309
- #endif /* RT_USING_OVERFLOW_CHECK */
484
+ #endif /* RT_USING_OVERFLOW_CHECK */
0 commit comments