Skip to content

Commit 459b8dc

Browse files
committed
sys/posix/pthread: Add pthread_attr_getstack and pthread_attr_setstack
1 parent 0ffad1d commit 459b8dc

File tree

2 files changed

+47
-0
lines changed

2 files changed

+47
-0
lines changed

sys/posix/pthread/include/pthread_threading_attr.h

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -202,6 +202,33 @@ int pthread_attr_getstacksize(const pthread_attr_t *attr, size_t *stacksize);
202202
*/
203203
int pthread_attr_setstacksize(pthread_attr_t *attr, size_t stacksize);
204204

205+
/**
206+
* @brief Query set stacksize for new pthread.
207+
* @param[in] attr Attribute set to query.
208+
* @param[out] stackaddr Pointer to previously assigned stack, or `NULL` for dynamic allocation.
209+
* @param[out] stacksize Assigned or default stack size, resp.
210+
* @returns 0, this invocation cannot fail
211+
*/
212+
int pthread_attr_getstack(pthread_attr_t *attr, void **stackaddr, size_t *stacksize);
213+
214+
/**
215+
* @brief Set address and stack size of the stack to use for the new pthread.
216+
* @details This function requires setting the address as well as the size
217+
* since only setting the address will make the implementation
218+
* on some architectures impossible.
219+
* If `*stackaddr == NULL`, then the stack is dynamically allocated with malloc().
220+
* No two running threads may operate on the same stack.
221+
* The stack of a zombie thread (i.e. a non-detached thread that exited but was not yet joined)
222+
* may in theory be reused even before joining, though there might be problems
223+
* if the stack was preempted before pthread_exit() completed.
224+
* @param[in,out] attr Attribute set to operate on.
225+
* @param[in] stackaddr Static stack to use, or `NULL` for dynamic allocation.
226+
* @param[in] stacksize Size of the stack of the new thread.
227+
* Supply `0` to use the default value.
228+
* @returns 0, this invocation cannot fail
229+
*/
230+
int pthread_attr_setstack(pthread_attr_t *attr, void *stackaddr, size_t stacksize);
231+
205232
#ifdef __cplusplus
206233
}
207234
#endif

sys/posix/pthread/pthread_attr.c

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -158,3 +158,23 @@ int pthread_attr_setstacksize(pthread_attr_t *attr, size_t stacksize)
158158
attr->ss_size = stacksize;
159159
return 0;
160160
}
161+
162+
int pthread_attr_getstack(pthread_attr_t *attr, void **stackaddr, size_t *stacksize)
163+
{
164+
int res = pthread_attr_getstackaddr(attr, stackaddr);
165+
if (res != 0) {
166+
return res;
167+
}
168+
169+
return pthread_attr_getstacksize(attr, stacksize);
170+
}
171+
172+
int pthread_attr_setstack(pthread_attr_t *attr, void *stackaddr, size_t stacksize)
173+
{
174+
int res = pthread_attr_setstackaddr(attr, stackaddr);
175+
if (res != 0) {
176+
return res;
177+
}
178+
179+
return pthread_attr_setstacksize(attr, stacksize);
180+
}

0 commit comments

Comments
 (0)