@@ -23,6 +23,22 @@ RT_DEFINE_HW_SPINLOCK(pth_lock);
23
23
_pthread_data_t * pth_table [PTHREAD_NUM_MAX ] = {NULL };
24
24
static int concurrency_level ;
25
25
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
+ */
26
42
_pthread_data_t * _pthread_get_data (pthread_t thread )
27
43
{
28
44
_pthread_data_t * ptd ;
@@ -38,6 +54,24 @@ _pthread_data_t *_pthread_get_data(pthread_t thread)
38
54
return NULL ;
39
55
}
40
56
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
+ */
41
75
pthread_t _pthread_data_get_pth (_pthread_data_t * ptd )
42
76
{
43
77
int index ;
@@ -52,6 +86,26 @@ pthread_t _pthread_data_get_pth(_pthread_data_t *ptd)
52
86
return index ;
53
87
}
54
88
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
+ */
55
109
pthread_t _pthread_data_create (void )
56
110
{
57
111
int index ;
@@ -87,6 +141,27 @@ pthread_t _pthread_data_create(void)
87
141
return index ;
88
142
}
89
143
144
+ /**
145
+ * @brief Create and initialize a new thread data structure with index allocation
146
+ *
147
+ * This function allocates memory for a new thread data structure, initializes its default state,
148
+ * and registers it in the global thread table. Uses spinlock synchronization to ensure safe
149
+ * access to the global thread table in multi-threaded environments.
150
+ *
151
+ * @return Allocated index value of type pthread_t
152
+ * - Returns valid index (0~PTHREAD_NUM_MAX-1) on successful allocation
153
+ * - Returns PTHREAD_NUM_MAX when memory allocation fails or no space available in thread table
154
+ *
155
+ * @note
156
+ * - Protects global thread table access with spinlock to prevent data races
157
+ * - Uses sequential search to find first available slot in thread table
158
+ * - Frees allocated memory and returns error when no available slots
159
+ * - Initializes thread data with default states:
160
+ * - Cancellation disabled (PTHREAD_CANCEL_DISABLE)
161
+ * - Deferred cancellation type (PTHREAD_CANCEL_DEFERRED)
162
+ * - Magic number validation (PTHREAD_MAGIC) for structure integrity
163
+ * - Explicitly clears magic number before freeing memory to prevent reuse
164
+ */
90
165
static inline void _destroy_item (int index , _pthread_data_t * ptd )
91
166
{
92
167
extern _pthread_key_data_t _thread_keys [PTHREAD_KEY_MAX ];
@@ -106,6 +181,30 @@ static inline void _destroy_item(int index, _pthread_data_t *ptd)
106
181
#define NOT_USE_CXX_TLS -1
107
182
#endif
108
183
184
+
185
+ /**
186
+ * @brief Destroy and clean up a thread data structure along with associated resources
187
+ *
188
+ * This function releases all resources associated with a thread data structure including:
189
+ * - Thread-local storage (TLS) destructors execution
190
+ * - Joinable semaphore deletion
191
+ * - Global thread table entry cleanup
192
+ * - Memory deallocation after proper resource release
193
+ *
194
+ * @param ptd Pointer to the thread data structure to be destroyed
195
+ *
196
+ * @note
197
+ * - Protects global thread table access with spinlock to prevent data races
198
+ * - Handles TLS destruction differently based on C++11 support:
199
+ * - C++11: Reverse iteration from emutls key position for safe destructor calls
200
+ * - C-only: Forward iteration through all thread keys
201
+ * - Maintains strict resource cleanup order:
202
+ * 1. TLS destructors -> 2. Global table removal -> 3. Semaphore deletion -> 4. Memory release
203
+ * - Uses magic number validation to prevent double-free and invalid access
204
+ * - Explicitly clears magic number before final memory release
205
+ * - Nullifies pointer references after freeing to prevent dangling references
206
+ * - Thread-safe operation through spinlock protection during critical sections
207
+ */
109
208
void _pthread_data_destroy (_pthread_data_t * ptd )
110
209
{
111
210
pthread_t pth ;
@@ -174,6 +273,26 @@ void _pthread_data_destroy(_pthread_data_t *ptd)
174
273
}
175
274
}
176
275
276
+ /**
277
+ * @brief Perform final cleanup of thread resources during thread termination
278
+ *
279
+ * This function handles the complete resource cleanup for a terminated thread, including:
280
+ * - Clearing cleanup handlers
281
+ * - Releasing thread stack memory
282
+ * - Detaching thread data structures
283
+ * - Final deallocation of thread control block
284
+ *
285
+ * @param tid Thread control block pointer to be cleaned up
286
+ *
287
+ * @note
288
+ * - Must be called as the final cleanup step after thread termination
289
+ * - Follows strict resource release order:
290
+ * 1. Clear cleanup handlers -> 2. Release stack -> 3. Detach data -> 4. Free control block
291
+ * - Explicitly nullifies pointers after freeing to prevent dangling references
292
+ * - Thread-safe operation assumes caller has handled synchronization
293
+ * - Handles both joinable and detached thread cleanup scenarios
294
+ * - Designed to work with thread detachment and join completion mechanisms
295
+ */
177
296
static void _pthread_cleanup (rt_thread_t tid )
178
297
{
179
298
/* clear cleanup function */
@@ -189,6 +308,26 @@ static void _pthread_cleanup(rt_thread_t tid)
189
308
rt_free (tid );
190
309
}
191
310
311
+ /**
312
+ * @brief Thread entry point stub that manages thread execution and resource cleanup
313
+ *
314
+ * This function serves as the entry point for POSIX threads, executing the thread's main
315
+ * function and handling post-exit resource management based on the thread's detach state.
316
+ *
317
+ * @param parameter Thread parameter containing the _pthread_data_t structure pointer
318
+ *
319
+ * @note
320
+ * - Executes thread's main function through thread_entry callback
321
+ * - Handles two resource management scenarios:
322
+ * - JOINABLE threads: Store return value and release join semaphore
323
+ * - DETACHED threads: Immediately destroy thread resources
324
+ * - Maintains strict execution flow:
325
+ * 1. Execute user thread function -> 2. Check detach state -> 3. Handle cleanup
326
+ * - Properly coordinates with joinable semaphore mechanism for synchronous termination
327
+ * - Assumes thread data structure remains valid until cleanup completion
328
+ * - Thread-safe operation relies on proper synchronization in resource destruction
329
+ * - Integrates with pthread lifecycle management system for complete resource recovery
330
+ */
192
331
static void pthread_entry_stub (void * parameter )
193
332
{
194
333
void * value ;
0 commit comments