11/*
2- * Copyright (c) 2006-2021, RT-Thread Development Team
2+ * Copyright (c) 2006-2025 RT-Thread Development Team
33 *
44 * SPDX-License-Identifier: Apache-2.0
55 *
@@ -38,11 +38,29 @@ static int current_tid = 0;
3838
3939static struct rt_mutex tid_lock ;
4040
41+ /**
42+ * @brief Initialize the thread ID manager
43+ *
44+ * @return int Returns RT_EOK (0) on success, error code on failure
45+ *
46+ * @note This function initializes a mutex lock used for thread ID management.
47+ */
4148int lwp_tid_init (void )
4249{
4350 return rt_mutex_init (& tid_lock , "tidmtx" , RT_IPC_FLAG_PRIO );
4451}
4552
53+ /**
54+ * @brief Allocates a thread ID (TID) from available resources
55+ *
56+ * @return int The allocated thread ID, or 0 if no TID available (with warning log)
57+ *
58+ * @note This function performs thread-safe allocation of a TID by:
59+ * 1. First checking the free list of available TIDs
60+ * 2. If none available, allocating from the TID array if space remains
61+ * 3. Performing a two-phase search for unused TIDs (current_tid+1 to TID_MAX, then 1 to current_tid)
62+ * 4. Inserting the allocated TID into the AVL tree for tracking
63+ */
4664int lwp_tid_get (void )
4765{
4866 struct lwp_avl_struct * p ;
@@ -97,6 +115,16 @@ int lwp_tid_get(void)
97115 return tid ;
98116}
99117
118+ /**
119+ * @brief Releases a thread ID (TID) and associated resources
120+ *
121+ * @param[in] tid The thread ID to release
122+ *
123+ * @note This function performs thread-safe release of a TID by:
124+ * 1. Finding the TID in the AVL tree and removing it
125+ * 2. Adding the freed TID structure to the free list for reuse
126+ * 3. Handling thread reference counting and potential suspension
127+ */
100128void lwp_tid_put (int tid )
101129{
102130 struct lwp_avl_struct * p ;
@@ -133,6 +161,13 @@ void lwp_tid_put(int tid)
133161 lwp_mutex_release_safe (& tid_lock );
134162}
135163
164+ /**
165+ * @brief Retrieves the thread object associated with a thread ID (TID)
166+ *
167+ * @param[in] tid The thread ID to look up
168+ *
169+ * @return rt_thread_t The associated thread object, or RT_NULL if not found
170+ */
136171rt_thread_t lwp_tid_get_thread_raw (int tid )
137172{
138173 struct lwp_avl_struct * p ;
@@ -146,6 +181,19 @@ rt_thread_t lwp_tid_get_thread_raw(int tid)
146181 return thread ;
147182}
148183
184+ /**
185+ * @brief Retrieves a thread object by TID and increments its reference count
186+ *
187+ * @param[in] tid The thread ID to look up (0 means current thread)
188+ *
189+ * @return rt_thread_t The associated thread object, or RT_NULL if not found
190+ *
191+ * @note This function provides thread-safe access to a thread object while:
192+ * 1. Acquiring the tid_lock mutex for synchronization
193+ * 2. Looking up the thread by TID (or returning current thread if tid=0)
194+ * 3. Incrementing the thread's reference count if found
195+ * 4. Releasing the mutex before returning
196+ */
149197rt_thread_t lwp_tid_get_thread_and_inc_ref (int tid )
150198{
151199 rt_thread_t thread = RT_NULL ;
@@ -160,6 +208,15 @@ rt_thread_t lwp_tid_get_thread_and_inc_ref(int tid)
160208 return thread ;
161209}
162210
211+ /**
212+ * @brief Decrement the reference count of a thread and potentially resume its suspender
213+ *
214+ * @param[in] thread The thread object whose reference count needs to be decremented
215+ *
216+ * @note This function safely decreases the reference count of a thread object. If the reference
217+ * count reaches zero and there is a suspender thread waiting (susp_recycler), it will
218+ * be resumed.
219+ */
163220void lwp_tid_dec_ref (rt_thread_t thread )
164221{
165222 rt_thread_t susp_putter ;
@@ -179,6 +236,15 @@ void lwp_tid_dec_ref(rt_thread_t thread)
179236 }
180237}
181238
239+ /**
240+ * @brief Associate a thread with a given TID in the thread ID management system
241+ *
242+ * @param[in] tid The thread ID to associate with the thread
243+ * @param[in] thread The thread object to be associated with the TID
244+ *
245+ * @note This function safely associates a thread object with a specified thread ID (TID)
246+ * in the system's AVL tree. The operation is protected by a mutex to ensure thread safety.
247+ */
182248void lwp_tid_set_thread (int tid , rt_thread_t thread )
183249{
184250 struct lwp_avl_struct * p ;
0 commit comments