@@ -23,6 +23,22 @@ RT_DEFINE_HW_SPINLOCK(pth_lock);
2323_pthread_data_t * pth_table [PTHREAD_NUM_MAX ] = {NULL };
2424static int concurrency_level ;
2525
26+ /**
27+ * @brief Retrieves the private data structure of a specified thread
28+ *
29+ * This function locates and validates the thread-private data structure associated with the given thread ID.
30+ * It uses a spinlock to synchronize access to the global thread table, ensuring data consistency in multithreaded environments.
31+ * A magic number validation is performed before returning to guarantee structural integrity.
32+ *
33+ * @param thread Thread ID used to index into the global thread data table
34+ *
35+ * @return Pointer to the thread's private data structure on success; NULL if thread ID is invalid or data not initialized
36+ *
37+ * @note
38+ * - Protects global thread table access with spinlock to prevent data race conditions
39+ * - Magic number validation (magic) prevents access to released or corrupted thread data
40+ * - Internal interface typically used by other POSIX thread library functions
41+ */
2642_pthread_data_t * _pthread_get_data (pthread_t thread )
2743{
2844 _pthread_data_t * ptd ;
@@ -38,6 +54,24 @@ _pthread_data_t *_pthread_get_data(pthread_t thread)
3854 return NULL ;
3955}
4056
57+ /**
58+ * @brief Get the index position of a thread data in the global thread table
59+ *
60+ * This function searches for matching thread data index by traversing the global thread table.
61+ * Uses spinlock synchronization mechanism to ensure safe access to the global thread table
62+ * in multi-threaded environments.
63+ *
64+ * @param ptd Pointer to the target thread data structure
65+ *
66+ * @return Index value of type pthread_t
67+ * - Returns corresponding index (0~PTHREAD_NUM_MAX-1) when found
68+ * - Returns PTHREAD_NUM_MAX when no match is found
69+ *
70+ * @note
71+ * - Protects global thread table access with spinlock to prevent data races from concurrent access
72+ * - Uses sequential traversal method to find matching thread data
73+ * - Return value of PTHREAD_NUM_MAX indicates no match found in the global table
74+ */
4175pthread_t _pthread_data_get_pth (_pthread_data_t * ptd )
4276{
4377 int index ;
@@ -52,6 +86,26 @@ pthread_t _pthread_data_get_pth(_pthread_data_t *ptd)
5286 return index ;
5387}
5488
89+ /**
90+ * @brief Create and initialize a new thread data structure with index allocation
91+ *
92+ * This function allocates memory for a new thread data structure, initializes its default state,
93+ * and registers it in the global thread table. Uses spinlock synchronization to ensure safe
94+ * access to the global thread table in multi-threaded environments.
95+ *
96+ * @return Allocated index value of type pthread_t
97+ * - Returns valid index (0~PTHREAD_NUM_MAX-1) on successful allocation
98+ * - Returns PTHREAD_NUM_MAX when memory allocation fails or no space available in thread table
99+ *
100+ * @note
101+ * - Protects global thread table access with spinlock to prevent data races
102+ * - Uses sequential search to find first available slot in thread table
103+ * - Cleans up allocated memory and returns error when no available slots
104+ * - Initializes thread data with default states:
105+ * - Cancellation disabled (PTHREAD_CANCEL_DISABLE)
106+ * - Deferred cancellation type (PTHREAD_CANCEL_DEFERRED)
107+ * - Magic number validation (PTHREAD_MAGIC) for structure integrity
108+ */
55109pthread_t _pthread_data_create (void )
56110{
57111 int index ;
@@ -87,6 +141,24 @@ pthread_t _pthread_data_create(void)
87141 return index ;
88142}
89143
144+ /**
145+ * @brief Destroy thread local storage item at specified index
146+ *
147+ * This function cleans up thread-local storage data by:
148+ * 1. Checking if the key at given index is active
149+ * 2. If TLS data exists and a destructor is registered, invoking the destructor
150+ * 3. Properly releasing resources associated with the TLS slot
151+ *
152+ * @param index Index into the thread keys array (0 to PTHREAD_KEY_MAX-1)
153+ * @param ptd Pointer to thread data structure containing TLS information
154+ *
155+ * @note
156+ * - Relies on external spinlock protection when accessing shared data
157+ * - Only processes valid keys that have been initialized
158+ * - Safely handles NULL pointers and missing destructors
159+ * - Designed to be called during thread cleanup or explicit TLS destruction
160+ * - Matches POSIX thread standard requirements for TLS destructor invocation
161+ */
90162static inline void _destroy_item (int index , _pthread_data_t * ptd )
91163{
92164 extern _pthread_key_data_t _thread_keys [PTHREAD_KEY_MAX ];
@@ -106,6 +178,30 @@ static inline void _destroy_item(int index, _pthread_data_t *ptd)
106178 #define NOT_USE_CXX_TLS -1
107179#endif
108180
181+
182+ /**
183+ * @brief Destroy and clean up a thread data structure along with associated resources
184+ *
185+ * This function releases all resources associated with a thread data structure including:
186+ * - Thread-local storage (TLS) destructors execution
187+ * - Joinable semaphore deletion
188+ * - Global thread table entry cleanup
189+ * - Memory deallocation after proper resource release
190+ *
191+ * @param ptd Pointer to the thread data structure to be destroyed
192+ *
193+ * @note
194+ * - Protects global thread table access with spinlock to prevent data races
195+ * - Handles TLS destruction differently based on C++11 support:
196+ * - C++11: Reverse iteration from emutls key position for safe destructor calls
197+ * - C-only: Forward iteration through all thread keys
198+ * - Maintains strict resource cleanup order:
199+ * 1. TLS destructors -> 2. Global table removal -> 3. Semaphore deletion -> 4. Memory release
200+ * - Uses magic number validation to prevent double-free and invalid access
201+ * - Explicitly clears magic number before final memory release
202+ * - Nullifies pointer references after freeing to prevent dangling references
203+ * - Thread-safe operation through spinlock protection during critical sections
204+ */
109205void _pthread_data_destroy (_pthread_data_t * ptd )
110206{
111207 pthread_t pth ;
@@ -174,6 +270,26 @@ void _pthread_data_destroy(_pthread_data_t *ptd)
174270 }
175271}
176272
273+ /**
274+ * @brief Perform final cleanup of thread resources during thread termination
275+ *
276+ * This function handles the complete resource cleanup for a terminated thread, including:
277+ * - Clearing cleanup handlers
278+ * - Releasing thread stack memory
279+ * - Detaching thread data structures
280+ * - Final deallocation of thread control block
281+ *
282+ * @param tid Thread control block pointer to be cleaned up
283+ *
284+ * @note
285+ * - Must be called as the final cleanup step after thread termination
286+ * - Follows strict resource release order:
287+ * 1. Clear cleanup handlers -> 2. Release stack -> 3. Detach data -> 4. Free control block
288+ * - Explicitly nullifies pointers after freeing to prevent dangling references
289+ * - Thread-safe operation assumes caller has handled synchronization
290+ * - Handles both joinable and detached thread cleanup scenarios
291+ * - Designed to work with thread detachment and join completion mechanisms
292+ */
177293static void _pthread_cleanup (rt_thread_t tid )
178294{
179295 /* clear cleanup function */
@@ -189,6 +305,26 @@ static void _pthread_cleanup(rt_thread_t tid)
189305 rt_free (tid );
190306}
191307
308+ /**
309+ * @brief Thread entry point stub that manages thread execution and resource cleanup
310+ *
311+ * This function serves as the entry point for POSIX threads, executing the thread's main
312+ * function and handling post-exit resource management based on the thread's detach state.
313+ *
314+ * @param parameter Thread parameter containing the _pthread_data_t structure pointer
315+ *
316+ * @note
317+ * - Executes thread's main function through thread_entry callback
318+ * - Handles two resource management scenarios:
319+ * - JOINABLE threads: Store return value and release join semaphore
320+ * - DETACHED threads: Immediately destroy thread resources
321+ * - Maintains strict execution flow:
322+ * 1. Execute user thread function -> 2. Check detach state -> 3. Handle cleanup
323+ * - Properly coordinates with joinable semaphore mechanism for synchronous termination
324+ * - Assumes thread data structure remains valid until cleanup completion
325+ * - Thread-safe operation relies on proper synchronization in resource destruction
326+ * - Integrates with pthread lifecycle management system for complete resource recovery
327+ */
192328static void pthread_entry_stub (void * parameter )
193329{
194330 void * value ;
@@ -370,6 +506,8 @@ int pthread_create(pthread_t *pid,
370506}
371507RTM_EXPORT (pthread_create );
372508
509+
510+
373511/**
374512 * @brief Marks a thread as detached, allowing its resources to be automatically released upon termination.
375513 *
0 commit comments