Skip to content

Commit dd709c1

Browse files
GorrayLimysterywolf
authored andcommitted
[components/libc/posix]add comments for thread local storage APIs.
1 parent 2a68412 commit dd709c1

File tree

1 file changed

+109
-1
lines changed

1 file changed

+109
-1
lines changed

components/libc/posix/pthreads/pthread_tls.c

Lines changed: 109 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright (c) 2006-2021, RT-Thread Development Team
2+
* Copyright (c) 2006-2024 RT-Thread Development Team
33
*
44
* SPDX-License-Identifier: Apache-2.0
55
*
@@ -21,6 +21,31 @@ static int pthread_key_system_init(void)
2121
}
2222
INIT_COMPONENT_EXPORT(pthread_key_system_init);
2323

24+
/**
25+
* @brief Retrieves the value associated with a thread-specific data key for the calling thread.
26+
*
27+
* This function returns the value that was previously set for the specified key
28+
* in the calling thread using `pthread_setspecific`. Each thread has its own independent
29+
* value for the same key.
30+
*
31+
* @param[in] key
32+
* The thread-specific data key, created using `pthread_key_create`.
33+
*
34+
* @return
35+
* - The value associated with the key for the calling thread, or `NULL` if no value
36+
* has been set for the key in this thread.
37+
*
38+
* @note
39+
* - If no value has been set for the key in the calling thread, `pthread_getspecific`
40+
* returns `NULL`. This does not indicate an error unless `NULL` was explicitly set as the value.
41+
* - The value retrieved is specific to the calling thread and may differ for other threads.
42+
*
43+
* @attention
44+
* - If the key has been deleted using `pthread_key_delete`, the behavior of this
45+
* function is undefined.
46+
*
47+
* @see pthread_key_create(), pthread_setspecific(), pthread_key_delete()
48+
*/
2449
void *pthread_getspecific(pthread_key_t key)
2550
{
2651
struct _pthread_data* ptd;
@@ -41,6 +66,37 @@ void *pthread_getspecific(pthread_key_t key)
4166
}
4267
RTM_EXPORT(pthread_getspecific);
4368

69+
/**
70+
* @brief Associates a value with a thread-specific data key for the calling thread.
71+
*
72+
* This function sets a thread-specific value for the given key. Each thread has its
73+
* own independent value associated with the same key, and this value is not shared with
74+
* other threads.
75+
*
76+
* @param[in] key
77+
* The thread-specific data key, created using `pthread_key_create`.
78+
* @param[in] value
79+
* The value to associate with the key for the calling thread. The value can be
80+
* a pointer to any data or `NULL`.
81+
*
82+
* @return
83+
* - `0`: The value was successfully set.
84+
* - `EINVAL`: The specified key is invalid or not initialized.
85+
*
86+
* @note
87+
* - Setting a new value for a key in a thread overwrites any previously set value
88+
* for that key in the same thread.
89+
* - The value set using `pthread_setspecific` is accessible via `pthread_getspecific`
90+
* for the same thread.
91+
* - The association between the key and value is valid until the thread terminates,
92+
* the key is deleted using `pthread_key_delete`, or a new value is set with this function.
93+
*
94+
* @attention
95+
* - The value is specific to the calling thread; other threads will not be affected
96+
* and will continue to maintain their own independent values for the same key.
97+
*
98+
* @see pthread_key_create(), pthread_getspecific(), pthread_key_delete()
99+
*/
44100
int pthread_setspecific(pthread_key_t key, const void *value)
45101
{
46102
struct _pthread_data* ptd;
@@ -68,6 +124,35 @@ int pthread_setspecific(pthread_key_t key, const void *value)
68124
}
69125
RTM_EXPORT(pthread_setspecific);
70126

127+
/**
128+
* @brief Creates a thread-specific data key.
129+
*
130+
* This function allocates a unique key for thread-specific data (TSD).
131+
* Each thread can use this key to access its own specific data associated with it.
132+
*
133+
* @param[out] key
134+
* A pointer to a variable where the newly created key will be stored.
135+
* On success, `*key` will hold the newly allocated key.
136+
* @param[in] destructor
137+
* An optional destructor function pointer. This function will be called to
138+
* clean up thread-specific data associated with the key when a thread terminates.
139+
* Pass `NULL` if no cleanup is required.
140+
*
141+
* @return
142+
* - `0`: The key was successfully created.
143+
* - `EAGAIN`: The system has reached the maximum number of available keys, and no new key can be allocated.
144+
*
145+
* @note
146+
* - Each thread can use `pthread_setspecific` and `pthread_getspecific` to set and retrieve the thread-specific data associated with the key.
147+
* - The destructor function will be invoked automatically by the system when the thread terminates, if not explicitly called.
148+
*
149+
* @attention
150+
* - If the `destructor` function is invoked and it sets thread-specific data for the same key,
151+
* the destructor may be called multiple times, up to a limit defined by the system
152+
* (typically `PTHREAD_DESTRUCTOR_ITERATIONS`).
153+
*
154+
* @see pthread_setspecific(), pthread_getspecific(), pthread_key_delete()
155+
*/
71156
int pthread_key_create(pthread_key_t *key, void (*destructor)(void*))
72157
{
73158
rt_uint32_t index;
@@ -94,6 +179,29 @@ int pthread_key_create(pthread_key_t *key, void (*destructor)(void*))
94179
}
95180
RTM_EXPORT(pthread_key_create);
96181

182+
/**
183+
* @brief Deletes a thread-specific data key.
184+
*
185+
* This function deletes a previously created thread-specific data key.
186+
* The key will no longer be valid for use in `pthread_setspecific` or `pthread_getspecific`.
187+
*
188+
* @param[in] key
189+
* The thread-specific data key to delete. The key must have been created with
190+
* `pthread_key_create`.
191+
*
192+
* @return
193+
* - `0`: The key was successfully deleted.
194+
* - `EINVAL`: The specified key is invalid or has not been initialized.
195+
*
196+
* @note
197+
* - Deleting a key does not automatically free or clean up the thread-specific data
198+
* associated with the key. It is the programmer's responsibility to ensure that
199+
* associated resources are properly released, if necessary.
200+
* - After deletion, using the key in `pthread_setspecific` or `pthread_getspecific`
201+
* will result in undefined behavior.
202+
*
203+
* @see pthread_key_create(), pthread_setspecific(), pthread_getspecific()
204+
*/
97205
int pthread_key_delete(pthread_key_t key)
98206
{
99207
if (key >= PTHREAD_KEY_MAX)

0 commit comments

Comments
 (0)