pthread: add pthread_attr_{get|set}guardsize support

This commit adds simple implementation of guardsize for pthreads.
At this moment this option simply increases the size of allocated pthread stack.

At default pthread guard size is set to 0.

Signed-off-by: p-szafonimateusz <p-szafonimateusz@xiaomi.com>
This commit is contained in:
p-szafonimateusz 2025-07-31 12:09:19 +02:00 committed by Xiang Xiao
parent 46bbde02b1
commit 8108aad1a4
10 changed files with 217 additions and 17 deletions

View file

@ -34,6 +34,8 @@ manage pthreads.
- :c:func:`pthread_attr_getinheritsched` - :c:func:`pthread_attr_getinheritsched`
- :c:func:`pthread_attr_setstacksize` - :c:func:`pthread_attr_setstacksize`
- :c:func:`pthread_attr_getstacksize` - :c:func:`pthread_attr_getstacksize`
- :c:func:`pthread_attr_setguardsize`
- :c:func:`pthread_attr_getguardsize`
- :c:func:`pthread_create` - :c:func:`pthread_create`
- :c:func:`pthread_detach` - :c:func:`pthread_detach`
- :c:func:`pthread_exit` - :c:func:`pthread_exit`
@ -109,11 +111,7 @@ The main task thread does not have thread-specific data.
No support for the following pthread interfaces is provided by NuttX: No support for the following pthread interfaces is provided by NuttX:
- ``pthread_attr_getguardsize``. get and set the thread guardsize
attribute.
- ``pthread_attr_getscope``. get and set the contentionscope attribute. - ``pthread_attr_getscope``. get and set the contentionscope attribute.
- ``pthread_attr_setguardsize``. get and set the thread guardsize
attribute.
- ``pthread_attr_setscope``. get and set the contentionscope attribute. - ``pthread_attr_setscope``. get and set the contentionscope attribute.
- ``pthread_getconcurrency``. get and set the level of concurrency. - ``pthread_getconcurrency``. get and set the level of concurrency.
- ``pthread_getcpuclockid``. access a thread CPU-time clock. - ``pthread_getcpuclockid``. access a thread CPU-time clock.
@ -328,6 +326,51 @@ No support for the following pthread interfaces is provided by NuttX:
**POSIX Compatibility:** Comparable to the POSIX interface of the same **POSIX Compatibility:** Comparable to the POSIX interface of the same
name. name.
.. c:function:: int pthread_attr_setguardsize(pthread_attr_t *attr, long guardsize);
Sets the thread guardsize attribute in the attr object. At this moment this
option simply increases the size of thread stacks.
**Input Parameters:**
- attr - thread attributes to be modified
- guardsize - guard size
**Returned Value:**
If successful, the ``pthread_attr_setguardsize()`` function will return
zero (``OK``). Otherwise, an error number will be returned to indicate
the error:
- ``To be provided``.
**Assumptions/Limitations:**
**POSIX Compatibility:** Comparable to the POSIX interface of the same
name.
.. c:function:: int pthread_attr_getguardsize(FAR const pthread_attr_t *attr, FAR size_t *stackaddr);
Gets the thread guardsize attributes from the attr object.
**Input Parameters:**
- attr - thread attributes to be queried
- guardsize - guard size pointer
**Returned Value:**
If successful, the ``pthread_attr_getguardsize()`` function will return
zero (``OK``). Otherwise, an error number will be returned to indicate
the error:
- ``To be provided``.
**Assumptions/Limitations:**
**POSIX Compatibility:** Comparable to the POSIX interface of the same
name.
.. c:function:: int pthread_create(pthread_t *thread, pthread_attr_t *attr, \ .. c:function:: int pthread_create(pthread_t *thread, pthread_attr_t *attr, \
pthread_startroutine_t startRoutine, \ pthread_startroutine_t startRoutine, \
pthread_addr_t arg); pthread_addr_t arg);

View file

@ -32,7 +32,7 @@ Units of Functionality Requirements:
+------------------------------+----------------+---------+ +------------------------------+----------------+---------+
| `POSIX_THREADS_BASE`_ | Yes | | | `POSIX_THREADS_BASE`_ | Yes | |
+------------------------------+----------------+---------+ +------------------------------+----------------+---------+
| `POSIX_THREADS_EXT`_ [#fn2]_ | 2/4 | | | `POSIX_THREADS_EXT`_ [#fn2]_ | Yes | |
+------------------------------+----------------+---------+ +------------------------------+----------------+---------+
| `XSI_THREADS_EXT`_ | 2/4 | | | `XSI_THREADS_EXT`_ | 2/4 | |
+------------------------------+----------------+---------+ +------------------------------+----------------+---------+
@ -754,17 +754,17 @@ POSIX_THREADS_EXT
Extended Threads: Extended Threads:
+-------------------------------------+---------+ +--------------------------------------+---------+
| API | Support | | API | Support |
+=====================================+=========+ +======================================+=========+
| pthread_attr_getguardsize() | No | | :c:func:`pthread_attr_getguardsize` | Yes |
+-------------------------------------+---------+ +--------------------------------------+---------+
| pthread_attr_setguardsize() | No | | :c:func:`pthread_attr_setguardsize` | Yes |
+-------------------------------------+---------+ +--------------------------------------+---------+
| :c:func:`pthread_mutexattr_gettype` | Yes | | :c:func:`pthread_mutexattr_gettype` | Yes |
+-------------------------------------+---------+ +--------------------------------------+---------+
| :c:func:`pthread_mutexattr_settype` | Yes | | :c:func:`pthread_mutexattr_settype` | Yes |
+-------------------------------------+---------+ +--------------------------------------+---------+
POSIX_C_LANG_MATH POSIX_C_LANG_MATH
----------------- -----------------

View file

@ -56,6 +56,7 @@
0, /* affinity */ \ 0, /* affinity */ \
NULL, /* stackaddr */ \ NULL, /* stackaddr */ \
PTHREAD_STACK_DEFAULT, /* stacksize */ \ PTHREAD_STACK_DEFAULT, /* stacksize */ \
PTHREAD_GUARD_DEFAULT, /* guardsize */ \
{0, 0}, /* repl_period */ \ {0, 0}, /* repl_period */ \
{0, 0} /* budget */ \ {0, 0} /* budget */ \
} }
@ -70,6 +71,7 @@
0, /* max_repl */ \ 0, /* max_repl */ \
NULL, /* stackaddr */ \ NULL, /* stackaddr */ \
PTHREAD_STACK_DEFAULT, /* stacksize */ \ PTHREAD_STACK_DEFAULT, /* stacksize */ \
PTHREAD_GUARD_DEFAULT, /* guardsize */ \
{0, 0}, /* repl_period */ \ {0, 0}, /* repl_period */ \
{0, 0}, /* budget */ \ {0, 0}, /* budget */ \
} }
@ -83,6 +85,7 @@
0, /* affinity */ \ 0, /* affinity */ \
NULL, /* stackaddr */ \ NULL, /* stackaddr */ \
PTHREAD_STACK_DEFAULT, /* stacksize */ \ PTHREAD_STACK_DEFAULT, /* stacksize */ \
PTHREAD_GUARD_DEFAULT, /* guardsize */ \
} }
#else #else
# define PTHREAD_ATTR_INITIALIZER \ # define PTHREAD_ATTR_INITIALIZER \
@ -93,6 +96,7 @@
PTHREAD_CREATE_JOINABLE, /* detachstate */ \ PTHREAD_CREATE_JOINABLE, /* detachstate */ \
NULL, /* stackaddr */ \ NULL, /* stackaddr */ \
PTHREAD_STACK_DEFAULT, /* stacksize */ \ PTHREAD_STACK_DEFAULT, /* stacksize */ \
PTHREAD_GUARD_DEFAULT, /* guardsize */ \
} }
#endif #endif

View file

@ -107,6 +107,7 @@
#define PTHREAD_STACK_MIN CONFIG_PTHREAD_STACK_MIN #define PTHREAD_STACK_MIN CONFIG_PTHREAD_STACK_MIN
#define PTHREAD_STACK_DEFAULT CONFIG_PTHREAD_STACK_DEFAULT #define PTHREAD_STACK_DEFAULT CONFIG_PTHREAD_STACK_DEFAULT
#define PTHREAD_GUARD_DEFAULT CONFIG_PTHREAD_GUARDSIZE_DEFAULT
/* Values for the pthread inheritsched attribute */ /* Values for the pthread inheritsched attribute */
@ -237,6 +238,7 @@ struct pthread_attr_s
FAR void *stackaddr; /* Address of memory to be used as stack */ FAR void *stackaddr; /* Address of memory to be used as stack */
size_t stacksize; /* Size of the stack allocated for the pthread */ size_t stacksize; /* Size of the stack allocated for the pthread */
size_t guardsize; /* Size of the guard area for the pthread's stack */
#ifdef CONFIG_SCHED_SPORADIC #ifdef CONFIG_SCHED_SPORADIC
struct timespec repl_period; /* Replenishment period */ struct timespec repl_period; /* Replenishment period */
@ -529,6 +531,12 @@ int pthread_attr_getstack(FAR const pthread_attr_t *attr,
int pthread_attr_setscope(FAR pthread_attr_t *attr, int scope); int pthread_attr_setscope(FAR pthread_attr_t *attr, int scope);
int pthread_attr_getscope(FAR const pthread_attr_t *attr, FAR int *scope); int pthread_attr_getscope(FAR const pthread_attr_t *attr, FAR int *scope);
/* Set/get guardsize attribute in thread attributes object */
int pthread_attr_setguardsize(FAR pthread_attr_t *attr, size_t guardsize);
int pthread_attr_getguardsize(FAR const pthread_attr_t *attr,
FAR size_t *guardsize);
/* Set or get the name of a thread */ /* Set or get the name of a thread */
int pthread_setname_np(pthread_t thread, FAR const char *name); int pthread_setname_np(pthread_t thread, FAR const char *name);

View file

@ -51,6 +51,8 @@ if(NOT CONFIG_DISABLE_PTHREAD)
pthread_attr_getschedparam.c pthread_attr_getschedparam.c
pthread_attr_setscope.c pthread_attr_setscope.c
pthread_attr_getscope.c pthread_attr_getscope.c
pthread_attr_setguardsize.c
pthread_attr_getguardsize.c
pthread_barrierattr_init.c pthread_barrierattr_init.c
pthread_barrierattr_destroy.c pthread_barrierattr_destroy.c
pthread_barrierattr_getpshared.c pthread_barrierattr_getpshared.c

View file

@ -38,6 +38,7 @@ CSRCS += pthread_attr_setstacksize.c pthread_attr_getstacksize.c
CSRCS += pthread_attr_setstack.c pthread_attr_getstack.c CSRCS += pthread_attr_setstack.c pthread_attr_getstack.c
CSRCS += pthread_attr_setschedparam.c pthread_attr_getschedparam.c CSRCS += pthread_attr_setschedparam.c pthread_attr_getschedparam.c
CSRCS += pthread_attr_setscope.c pthread_attr_getscope.c CSRCS += pthread_attr_setscope.c pthread_attr_getscope.c
CSRCS += pthread_attr_setguardsize.c pthread_attr_getguardsize.c
CSRCS += pthread_barrierattr_init.c pthread_barrierattr_destroy.c CSRCS += pthread_barrierattr_init.c pthread_barrierattr_destroy.c
CSRCS += pthread_barrierattr_getpshared.c pthread_barrierattr_setpshared.c CSRCS += pthread_barrierattr_getpshared.c pthread_barrierattr_setpshared.c
CSRCS += pthread_barrierinit.c pthread_barrierdestroy.c pthread_barrierwait.c CSRCS += pthread_barrierinit.c pthread_barrierdestroy.c pthread_barrierwait.c

View file

@ -0,0 +1,67 @@
/****************************************************************************
* libs/libc/pthread/pthread_attr_getguardsize.c
*
* SPDX-License-Identifier: Apache-2.0
*
* Licensed to the Apache Software Foundation (ASF) under one or more
* contributor license agreements. See the NOTICE file distributed with
* this work for additional information regarding copyright ownership. The
* ASF licenses this file to you under the Apache License, Version 2.0 (the
* "License"); you may not use this file except in compliance with the
* License. You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
* WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
* License for the specific language governing permissions and limitations
* under the License.
*
****************************************************************************/
/****************************************************************************
* Included Files
****************************************************************************/
#include <pthread.h>
/****************************************************************************
* Public Functions
****************************************************************************/
/****************************************************************************
* Name: pthread_attr_getguardsize
*
* Description:
* The pthread_attr_getstack() function shall get the thread guardsize
* attributes from the attr object.
*
* Input Parameters:
* attr - thread attributes to be queried.
* guardsize - guard size pointer
*
* Returned Value:
* 0 if successful. Otherwise, an error code.
*
* Assumptions:
*
****************************************************************************/
int pthread_attr_getguardsize(FAR const pthread_attr_t *attr,
FAR size_t *guardsize)
{
int ret;
if (!guardsize)
{
ret = EINVAL;
}
else
{
*guardsize = attr->guardsize;
ret = OK;
}
return ret;
}

View file

@ -0,0 +1,66 @@
/****************************************************************************
* libs/libc/pthread/pthread_attr_setguardsize.c
*
* SPDX-License-Identifier: Apache-2.0
*
* Licensed to the Apache Software Foundation (ASF) under one or more
* contributor license agreements. See the NOTICE file distributed with
* this work for additional information regarding copyright ownership. The
* ASF licenses this file to you under the Apache License, Version 2.0 (the
* "License"); you may not use this file except in compliance with the
* License. You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
* WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
* License for the specific language governing permissions and limitations
* under the License.
*
****************************************************************************/
/****************************************************************************
* Included Files
****************************************************************************/
#include <pthread.h>
/****************************************************************************
* Public Functions
****************************************************************************/
/****************************************************************************
* Name: pthread_attr_setguardsize
*
* Description:
* The pthread_attr_setstack() function shall set the thread guardsize
* attribute in the attr object.
*
* Parameters:
* attr - thread attributes to be modified.
* guardsize - guard size
*
* Return Value:
* 0 if successful. Otherwise, an error code.
*
* Assumptions:
*
****************************************************************************/
int pthread_attr_setguardsize(FAR pthread_attr_t *attr, size_t guardsize)
{
int ret;
if (!attr)
{
ret = EINVAL;
}
else
{
attr->guardsize = guardsize;
ret = OK;
}
return ret;
}

View file

@ -1960,6 +1960,14 @@ config PTHREAD_STACK_DEFAULT
---help--- ---help---
Default pthread stack size Default pthread stack size
config PTHREAD_GUARDSIZE_DEFAULT
int "Default pthread guard area size"
default 0
---help---
This is the default amount of space to reserve at the overflow end of a
pthread stack. At this moment this option simply increases the size
of thread stacks.
endmenu # Stack and heap information endmenu # Stack and heap information
config SCHED_BACKTRACE config SCHED_BACKTRACE

View file

@ -259,7 +259,8 @@ int nx_pthread_create(pthread_trampoline_t trampoline, FAR pthread_t *thread,
{ {
/* Allocate the stack for the TCB */ /* Allocate the stack for the TCB */
ret = up_create_stack((FAR struct tcb_s *)ptcb, attr->stacksize, ret = up_create_stack((FAR struct tcb_s *)ptcb,
attr->stacksize + attr->guardsize,
TCB_FLAG_TTYPE_PTHREAD); TCB_FLAG_TTYPE_PTHREAD);
} }