xiaoxiang781216 edited a comment on pull request #3170:
URL: https://github.com/apache/incubator-nuttx/pull/3170#issuecomment-806920409
> > Yes, but it waste other thread local storage. We can simulate per-task
variable from per-thread variable, but I don't think this is right approach
from both the concept and implementation(memory) perspective.
>
> As soon as the pthread key is allocated, all of the pointers in all
threads in all tasks are committed to that single purpose and cannot be used
for anything else. Nothing additional is wasted by setting the value to point
to a common structure.
What's I mean waste here is that we reserve a slot but only use for the main
thread. The thread local storage slot is the scarce resource since the slot
number is very limited.
> The only way to avoid that would be to use hard-coded structures in TLS as
you suggested.
>
> Would that hard coded structure only exist for the main thread? I don't
think that is do-able. I think TLS must be the same structure in all threads.
But I would have to look at that more.
We can create a new slot space for task(task_key_create/task_key_destroy),
then the application use these functions for the task wide variables if they
want. For small task wide variables used in libc, we can extend:
```
struct tls_info_s
{
#if CONFIG_TLS_NELEM > 0
uintptr_t tl_elem[CONFIG_TLS_NELEM]; /* TLS elements */
#endif
int tl_errno; /* Per-thread error number */
};
```
to:
```
struct tls_task_info_s
{
struct tls_info_s thread;
#if CONFIG_TLS_TASK_NELEM > 0
uintptr_t tl_elem[CONFIG_TLS_TASK_NELEM]; /* TLS task elements */
#endif
int tl_optarg; /* Per-task optarg */
};
```
Then, the space for task wide variables never consume other thread's space.
--
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.
For queries about this service, please contact Infrastructure at:
[email protected]