-
Notifications
You must be signed in to change notification settings - Fork 6.8k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
lib: posix: dynamic stack support for pthread_create() #25973
Comments
Not sure I understand the downsides of using a heap? You won't need to know how many stacks to reserve in the array in advance, nor would you have to come up with a CONFIG_PTHREAD_DYNAMIC_STACK_SIZE that works for all dynamic threads. It all depends on the app but threads tend to vary greatly in how much stack space they need. Ultimately it's up to you though.
This is almost possible, but not quite. If you wanted to go the heap route I think there are two things standing in the way of this:
I'll probably open a ticket for a general |
@andrewboie I would prefer to use the heap as well. The only downside that I can see is that if you do know the (maximum) number of "dynamic" stacks in advance, and say that threads were created and destroyed somewhat regularly, then there is a chance that an application could malloc a large amount and starve one of the threads. If the application is written in C++ with I could wait for #24714 if you want to exclusively go the heap route. There will need to be some toolchain work as well to use the |
Really rough draft atm @andrewboie, using the kernel thread macros mentioned in your comment here. The PR that's linked to this has my changes.
|
This change allows users to call pthread_create(3) with the pthread_attr_t argument equal to NULL, or with the pthread_attr_t argument specifying a NULL stack but a custom stack size. If either of the above to requirements are met, then a stack will be heap-allocated internally and freed again after the thread has terminated. This makes the Zephyr implementation of pthread_create(3) more compliant with the normative spec. Fixes zephyrproject-rtos#25973 Signed-off-by: Christopher Friedt <chrisfriedt@gmail.com>
Tests for dynamically allocated POSIX thread stacks. Fixes zephyrproject-rtos#25973 Signed-off-by: Christopher Friedt <chrisfriedt@gmail.com>
This change allows users to call pthread_create(3) with the pthread_attr_t argument equal to NULL, or with the pthread_attr_t argument specifying a NULL stack but a custom stack size. If either of the above to requirements are met, then a stack will be heap-allocated internally and freed again after the thread has terminated. This makes the Zephyr implementation of pthread_create(3) more compliant with the normative spec. Fixes zephyrproject-rtos#25973 Signed-off-by: Christopher Friedt <chrisfriedt@gmail.com>
Tests for dynamically allocated POSIX thread stacks. Fixes zephyrproject-rtos#25973 Signed-off-by: Christopher Friedt <chrisfriedt@gmail.com>
With `CONFIG_DYNAMIC_THREAD_STACKS`, Zephyr is able to dynamically allocate thread stacks. If the stack is `NULL`, then a stack will be dynamically allocated. When the stack size is 0, the stack size is defaulted to at least `CONFIG_DYNAMIC_THREAD_STACKS_DEFAULT_SIZE` bytes. Without `CONFIG_DYNAMIC_THREAD_STACKS`, `pthread_create()` behaves as it did previously. Fixes zephyrproject-rtos#25973 Signed-off-by: Christopher Friedt <chrisfriedt@gmail.com>
While zephyrproject-rtos/zephyr#25973 is not fixed, let's use a dirty hack in the benchmark sources.
This change allows users to call pthread_create(3) with the pthread_attr_t argument equal to NULL, or with the pthread_attr_t argument specifying a NULL stack but a custom stack size. If either of the above to requirements are met, then a stack will be heap-allocated internally and freed again after the thread has terminated. This makes the Zephyr implementation of pthread_create(3) more compliant with the normative spec. Fixes zephyrproject-rtos#25973 Signed-off-by: Christopher Friedt <chrisfriedt@gmail.com>
Tests for dynamically allocated POSIX thread stacks. Fixes zephyrproject-rtos#25973 Signed-off-by: Christopher Friedt <chrisfriedt@gmail.com>
This change allows users to call pthread_create(3) with the pthread_attr_t argument equal to NULL, or with the pthread_attr_t argument specifying a NULL stack but a custom stack size. If either of the above to requirements are met, then a stack will be heap-allocated internally and freed again after the thread has terminated. This makes the Zephyr implementation of pthread_create(3) more compliant with the normative spec. Fixes zephyrproject-rtos#25973 Signed-off-by: Christopher Friedt <chrisfriedt@gmail.com>
Tests for dynamically allocated POSIX thread stacks. Fixes zephyrproject-rtos#25973 Signed-off-by: Christopher Friedt <chrisfriedt@gmail.com>
This change allows users to call pthread_create(3) with the pthread_attr_t argument equal to NULL, or with the pthread_attr_t argument specifying a NULL stack but a custom stack size. If either of the above to requirements are met, then a stack will be heap-allocated internally and freed again after the thread has terminated. This makes the Zephyr implementation of pthread_create(3) more compliant with the normative spec. Fixes zephyrproject-rtos#25973 Signed-off-by: Christopher Friedt <chrisfriedt@gmail.com>
Tests for dynamically allocated POSIX thread stacks. Fixes zephyrproject-rtos#25973 Signed-off-by: Christopher Friedt <chrisfriedt@gmail.com>
Tests for dynamically allocated POSIX thread stacks. Fixes zephyrproject-rtos#25973 Signed-off-by: Christopher Friedt <chrisfriedt@gmail.com>
This change allows users to call pthread_create(3) with the pthread_attr_t argument equal to NULL, or with the pthread_attr_t argument specifying a NULL stack but a custom stack size. If either of the above to requirements are met, then a stack will be heap-allocated internally and freed again after the thread has terminated. This makes the Zephyr implementation of pthread_create(3) more compliant with the normative spec. Fixes zephyrproject-rtos#25973 Signed-off-by: Christopher Friedt <chrisfriedt@gmail.com>
Tests for dynamically allocated POSIX thread stacks. Fixes zephyrproject-rtos#25973 Signed-off-by: Christopher Friedt <chrisfriedt@gmail.com>
Is your enhancement proposal related to a problem? Please describe.
Currently, Zephyr's pthread_create() implementation requires that a
pthread_attr_t
be passed in whereas the POSIX specification explicitly allowsNULL
, in which case a suitably-sized stack is automatically allocated.For the majority of use cases, static stack allocation should continue to be the preferred approach. However, there are some specific uses cases where this might not be ideal. For example, supporting
std::thread
with our existing toolchain and libstdc++'s default gthr-posix configuration.Describe the solution you'd like
IIRC, we should be able to use the
K_THREAD_STACK_LEN()
macro for calculating the amount of space to allocate for a thread stack, and then theZ_THREAD_STACK_BUFFER()
macro to get the offset into the allocated buffer that takes alignment and padding into account.It doesn't even really make sense to limit this to pthread stacks, but it should be possible to use it for zephyr stacks in general. I will need to take some caution and ensure that permissions are correct for the kernel object.
There will be some additional Kconfig values. E.g
Describe alternatives you've considered
Originally my solution involved heap-allocating stacks, and then I flip-flopped and went with statically allocating a pool of thread stacks, and now I'm back to heap-allocated!
However, it's likely that we will just use heap-allocated stacks.
Additional context
I'm hoping to use this for #25572.
While ultimately, it would probably be desireable to use native zephyr threads for
std::thread
in order to keep the APIs separable, pthreads could be used as a stop-gap implementation as @pabigot pointed out in #25572.The text was updated successfully, but these errors were encountered: