35
35
* 2023-09-15 xqyjlj perf rt_hw_interrupt_disable/enable
36
36
* 2023-12-10 xqyjlj fix thread_exit/detach/delete
37
37
* fix rt_thread_delay
38
+ * 2025-09-01 Rbb666 add api:rt_thread_suspend_any.
38
39
*/
39
40
40
41
#include <rthw.h>
@@ -883,12 +884,9 @@ static void _thread_set_suspend_state(struct rt_thread *thread, int suspend_flag
883
884
/**
884
885
* @brief This function will suspend the specified thread and change it to suspend state.
885
886
*
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.
892
890
*
893
891
* @param thread the thread to be suspended.
894
892
* @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
902
900
* the first-in-first-out principle, and you clearly understand that all threads involved in
903
901
* this semaphore will become non-real-time threads.
904
902
* @param suspend_flag status flag of the thread to be suspended.
903
+ * @param force_suspend if RT_TRUE, allows suspending other threads
905
904
*
906
905
* @return Return the operation status. If the return value is RT_EOK, the function is successfully executed.
907
906
* If the return value is any other values, it means this operation failed.
908
907
*/
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 )
910
909
{
911
910
rt_base_t stat ;
912
911
rt_sched_lock_level_t slvl ;
913
912
914
913
/* parameter check */
915
914
RT_ASSERT (thread != RT_NULL );
916
915
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 ());
918
920
919
921
LOG_D ("thread suspend: %s" , thread -> parent .name );
920
922
@@ -928,12 +930,6 @@ rt_err_t rt_thread_suspend_to_list(rt_thread_t thread, rt_list_t *susp_list, int
928
930
return - RT_ERROR ;
929
931
}
930
932
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
-
937
933
#ifdef RT_USING_SMART
938
934
if (thread -> lwp )
939
935
{
@@ -982,6 +978,30 @@ rt_err_t rt_thread_suspend_to_list(rt_thread_t thread, rt_list_t *susp_list, int
982
978
RT_OBJECT_HOOK_CALL (rt_thread_suspend_hook , (thread ));
983
979
return RT_EOK ;
984
980
}
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
+ }
985
1005
RTM_EXPORT (rt_thread_suspend_to_list );
986
1006
987
1007
/**
0 commit comments