Skip to content

Commit

Permalink
sys/posix/pthread: Add pthread_attr_getstack and pthread_attr_setstack
Browse files Browse the repository at this point in the history
  • Loading branch information
LasseRosenow committed Oct 22, 2024
1 parent 0ffad1d commit a923ea4
Show file tree
Hide file tree
Showing 2 changed files with 45 additions and 0 deletions.
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 @@ -202,6 +202,33 @@ int pthread_attr_getstacksize(const pthread_attr_t *attr, size_t *stacksize);
*/
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
18 changes: 18 additions & 0 deletions sys/posix/pthread/pthread_attr.c
Original file line number Diff line number Diff line change
Expand Up @@ -158,3 +158,21 @@ int pthread_attr_setstacksize(pthread_attr_t *attr, size_t stacksize)
attr->ss_size = stacksize;
return 0;
}

int pthread_attr_getstack(pthread_attr_t *attr, void **stackaddr, size_t *stacksize)
{
*stackaddr = attr->ss_sp;
*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. */
}
return 0;
}

int pthread_attr_setstack(pthread_attr_t *attr, void *stackaddr, size_t stacksize)
{
attr->ss_sp = (char *)stackaddr;
attr->ss_size = stacksize;
return 0;
}

0 comments on commit a923ea4

Please sign in to comment.