Skip to content
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

sys/posix/pthread: Add pthread_attr_getstack and pthread_attr_setstack #20934

Merged
merged 1 commit into from
Oct 29, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
27 changes: 27 additions & 0 deletions sys/posix/pthread/include/pthread_threading_attr.h
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@
* @details A zeroed out datum is default initialized.
* @see pthread_create() for further information
*/
typedef struct

Check warning on line 29 in sys/posix/pthread/include/pthread_threading_attr.h

View workflow job for this annotation

GitHub Actions / static-tests

keyword 'struct' not followed by a single space
{
uint8_t detached; /**< Start in detached state. */
char *ss_sp; /**< Stack to use for new thread. */
Expand Down Expand Up @@ -70,7 +70,7 @@

/**
* @brief Sets whether to create a new pthread in a detached state.
* @note Supplying a value different form PTHREAD_CREATE_JOINABLE or PTHREAD_CREATE_DETACHED

Check warning on line 73 in sys/posix/pthread/include/pthread_threading_attr.h

View workflow job for this annotation

GitHub Actions / static-tests

line is longer than 100 characters
* causes undefined behavior.
* @param[in,out] attr Attribute set to operate on.
* @param[in] detachstate Either PTHREAD_CREATE_JOINABLE or PTHREAD_CREATE_DETACHED.
Expand Down Expand Up @@ -163,7 +163,7 @@
* @brief Query assigned stack for new pthread.
* @see pthread_attr_setstackaddr() for more information
* @param[in] attr Attribute set to query
* @param[out] stackaddr Pointer to previously assigned stack, or `NULL` for dynamic allocation.

Check warning on line 166 in sys/posix/pthread/include/pthread_threading_attr.h

View workflow job for this annotation

GitHub Actions / static-tests

line is longer than 100 characters
* @returns 0, this invocation cannot fail
*/
int pthread_attr_getstackaddr(const pthread_attr_t *attr, void **stackaddr);
Expand All @@ -172,7 +172,7 @@
* @brief Set address of the stack to use for the new pthread.
* @details If `*stackaddr == NULL`, then the stack is dynamically allocated with malloc().
* No two running threads may operate on the same stack.
* The stack of a zombie thread (i.e. a non-detached thread that exited but was not yet joined)

Check warning on line 175 in sys/posix/pthread/include/pthread_threading_attr.h

View workflow job for this annotation

GitHub Actions / static-tests

line is longer than 100 characters
* may in theory be reused even before joining, though there might be problems
* if the stack was preempted before pthread_exit() completed.
* @param[in,out] attr Attribute set to operate on.
Expand Down Expand Up @@ -202,6 +202,33 @@
*/
int pthread_attr_setstacksize(pthread_attr_t *attr, size_t stacksize);

/**
* @brief Query set stacksize for new pthread.
* @param[in] attr Attribute set to query.
* @param[out] stackaddr Pointer to previously assigned stack, or `NULL` for dynamic allocation.

Check warning on line 208 in sys/posix/pthread/include/pthread_threading_attr.h

View workflow job for this annotation

GitHub Actions / static-tests

line is longer than 100 characters
* @param[out] stacksize Assigned or default stack size, resp.
* @returns 0, this invocation cannot fail
*/
int pthread_attr_getstack(pthread_attr_t *attr, void **stackaddr, size_t *stacksize);

/**
* @brief Set address and stack size of the stack to use for the new pthread.
* @details This function requires setting the address as well as the size
* since only setting the address will make the implementation
* on some architectures impossible.
* If `*stackaddr == NULL`, then the stack is dynamically allocated with malloc().
* No two running threads may operate on the same stack.
* The stack of a zombie thread (i.e. a non-detached thread that exited but was not yet joined)

Check warning on line 221 in sys/posix/pthread/include/pthread_threading_attr.h

View workflow job for this annotation

GitHub Actions / static-tests

line is longer than 100 characters
* may in theory be reused even before joining, though there might be problems
* if the stack was preempted before pthread_exit() completed.
* @param[in,out] attr Attribute set to operate on.
* @param[in] stackaddr Static stack to use, or `NULL` for dynamic allocation.
* @param[in] stacksize Size of the stack of the new thread.
* Supply `0` to use the default value.
* @returns 0, this invocation cannot fail
*/
int pthread_attr_setstack(pthread_attr_t *attr, void *stackaddr, size_t stacksize);

#ifdef __cplusplus
}
#endif
Expand Down
20 changes: 20 additions & 0 deletions sys/posix/pthread/pthread_attr.c
Original file line number Diff line number Diff line change
Expand Up @@ -148,7 +148,7 @@
{
*stacksize = attr->ss_size;
if (*stacksize == 0) {
/* FIXME: the standard says that this function should return the default value if no explicit value was set. */

Check warning on line 151 in sys/posix/pthread/pthread_attr.c

View workflow job for this annotation

GitHub Actions / static-tests

line is longer than 100 characters
}
return 0;
}
Expand All @@ -158,3 +158,23 @@
attr->ss_size = stacksize;
return 0;
}

int pthread_attr_getstack(pthread_attr_t *attr, void **stackaddr, size_t *stacksize)
{
int res = pthread_attr_getstackaddr(attr, stackaddr);
if (res != 0) {
return res;
}

return pthread_attr_getstacksize(attr, stacksize);
}

int pthread_attr_setstack(pthread_attr_t *attr, void *stackaddr, size_t stacksize)
{
int res = pthread_attr_setstackaddr(attr, stackaddr);
if (res != 0) {
return res;
}

return pthread_attr_setstacksize(attr, stacksize);
}
Loading