3535 * 2023-09-15 xqyjlj perf rt_hw_interrupt_disable/enable
3636 * 2023-12-10 xqyjlj fix thread_exit/detach/delete
3737 * fix rt_thread_delay
38+ * 2025-09-01 Rbb666 add api:rt_thread_suspend_any.
3839 */
3940
4041#include <rthw.h>
@@ -883,12 +884,9 @@ static void _thread_set_suspend_state(struct rt_thread *thread, int suspend_flag
883884/**
884885 * @brief This function will suspend the specified thread and change it to suspend state.
885886 *
886- * @note This function ONLY can suspend current thread itself.
887- * rt_thread_suspend(rt_thread_self());
888- *
889- * Do not use the rt_thread_suspend to suspend other threads. You have no way of knowing what code a
890- * thread is executing when you suspend it. If you suspend a thread while sharing a resouce with
891- * other threads and occupying this resouce, starvation can occur very easily.
887+ * @note This function can suspend any thread including other threads.
888+ * WARNING: Suspending other threads can be dangerous and may cause deadlocks
889+ * if the target thread holds important resources.
892890 *
893891 * @param thread the thread to be suspended.
894892 * @param susp_list the list thread enqueued to. RT_NULL if no list.
@@ -902,19 +900,23 @@ static void _thread_set_suspend_state(struct rt_thread *thread, int suspend_flag
902900 * the first-in-first-out principle, and you clearly understand that all threads involved in
903901 * this semaphore will become non-real-time threads.
904902 * @param suspend_flag status flag of the thread to be suspended.
903+ * @param force_suspend if RT_TRUE, allows suspending other threads
905904 *
906905 * @return Return the operation status. If the return value is RT_EOK, the function is successfully executed.
907906 * If the return value is any other values, it means this operation failed.
908907 */
909- rt_err_t rt_thread_suspend_to_list (rt_thread_t thread , rt_list_t * susp_list , int ipc_flags , int suspend_flag )
908+ static rt_err_t rt_thread_suspend_to_list_ex (rt_thread_t thread , rt_list_t * susp_list , int ipc_flags , int suspend_flag , rt_bool_t force_suspend )
910909{
911910 rt_base_t stat ;
912911 rt_sched_lock_level_t slvl ;
913912
914913 /* parameter check */
915914 RT_ASSERT (thread != RT_NULL );
916915 RT_ASSERT (rt_object_get_type ((rt_object_t )thread ) == RT_Object_Class_Thread );
917- RT_ASSERT (thread == rt_thread_self ());
916+
917+ /* only allow suspending other threads if explicitly requested */
918+ if (!force_suspend )
919+ RT_ASSERT (thread == rt_thread_self ());
918920
919921 LOG_D ("thread suspend: %s" , thread -> parent .name );
920922
@@ -928,12 +930,6 @@ rt_err_t rt_thread_suspend_to_list(rt_thread_t thread, rt_list_t *susp_list, int
928930 return - RT_ERROR ;
929931 }
930932
931- if (stat == RT_THREAD_RUNNING )
932- {
933- /* not suspend running status thread on other core */
934- RT_ASSERT (thread == rt_thread_self ());
935- }
936-
937933#ifdef RT_USING_SMART
938934 if (thread -> lwp )
939935 {
@@ -982,6 +978,30 @@ rt_err_t rt_thread_suspend_to_list(rt_thread_t thread, rt_list_t *susp_list, int
982978 RT_OBJECT_HOOK_CALL (rt_thread_suspend_hook , (thread ));
983979 return RT_EOK ;
984980}
981+ RTM_EXPORT (rt_thread_suspend_to_list_ex );
982+
983+ /**
984+ * @brief This function will suspend any thread including other threads.
985+ *
986+ * @warning This function can be dangerous! Only use it if you understand the risks.
987+ * Suspending threads that hold mutexes or other resources can cause deadlocks.
988+ *
989+ * @param thread the thread to be suspended.
990+ * @param suspend_flag status flag of the thread to be suspended.
991+ *
992+ * @return Return the operation status. If the return value is RT_EOK, the function is successfully executed.
993+ * If the return value is any other values, it means this operation failed.
994+ */
995+ rt_err_t rt_thread_suspend_any (rt_thread_t thread , int suspend_flag )
996+ {
997+ return rt_thread_suspend_to_list_ex (thread , RT_NULL , 0 , suspend_flag , RT_TRUE );
998+ }
999+ RTM_EXPORT (rt_thread_suspend_any );
1000+
1001+ rt_err_t rt_thread_suspend_to_list (rt_thread_t thread , rt_list_t * susp_list , int ipc_flags , int suspend_flag )
1002+ {
1003+ return rt_thread_suspend_to_list_ex (thread , susp_list , ipc_flags , suspend_flag , RT_FALSE );
1004+ }
9851005RTM_EXPORT (rt_thread_suspend_to_list );
9861006
9871007/**
0 commit comments