diff --git a/Documentation/reference/user/08_pthread.rst b/Documentation/reference/user/08_pthread.rst index 77017cef04f..67208b5ca59 100644 --- a/Documentation/reference/user/08_pthread.rst +++ b/Documentation/reference/user/08_pthread.rst @@ -34,6 +34,8 @@ manage pthreads. - :c:func:`pthread_attr_getinheritsched` - :c:func:`pthread_attr_setstacksize` - :c:func:`pthread_attr_getstacksize` + - :c:func:`pthread_attr_setguardsize` + - :c:func:`pthread_attr_getguardsize` - :c:func:`pthread_create` - :c:func:`pthread_detach` - :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: - - ``pthread_attr_getguardsize``. get and set the thread guardsize - 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_getconcurrency``. get and set the level of concurrency. - ``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 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, \ pthread_startroutine_t startRoutine, \ pthread_addr_t arg); diff --git a/Documentation/standards/posix.rst b/Documentation/standards/posix.rst index 35187b2b4d9..ebf9e34142e 100644 --- a/Documentation/standards/posix.rst +++ b/Documentation/standards/posix.rst @@ -32,7 +32,7 @@ Units of Functionality Requirements: +------------------------------+----------------+---------+ | `POSIX_THREADS_BASE`_ | Yes | | +------------------------------+----------------+---------+ -| `POSIX_THREADS_EXT`_ [#fn2]_ | 2/4 | | +| `POSIX_THREADS_EXT`_ [#fn2]_ | Yes | | +------------------------------+----------------+---------+ | `XSI_THREADS_EXT`_ | 2/4 | | +------------------------------+----------------+---------+ @@ -754,17 +754,17 @@ POSIX_THREADS_EXT Extended Threads: -+-------------------------------------+---------+ -| API | Support | -+=====================================+=========+ -| pthread_attr_getguardsize() | No | -+-------------------------------------+---------+ -| pthread_attr_setguardsize() | No | -+-------------------------------------+---------+ -| :c:func:`pthread_mutexattr_gettype` | Yes | -+-------------------------------------+---------+ -| :c:func:`pthread_mutexattr_settype` | Yes | -+-------------------------------------+---------+ ++--------------------------------------+---------+ +| API | Support | ++======================================+=========+ +| :c:func:`pthread_attr_getguardsize` | Yes | ++--------------------------------------+---------+ +| :c:func:`pthread_attr_setguardsize` | Yes | ++--------------------------------------+---------+ +| :c:func:`pthread_mutexattr_gettype` | Yes | ++--------------------------------------+---------+ +| :c:func:`pthread_mutexattr_settype` | Yes | ++--------------------------------------+---------+ POSIX_C_LANG_MATH ----------------- diff --git a/include/nuttx/pthread.h b/include/nuttx/pthread.h index 6a80835fc46..a2d7114e9fa 100644 --- a/include/nuttx/pthread.h +++ b/include/nuttx/pthread.h @@ -56,6 +56,7 @@ 0, /* affinity */ \ NULL, /* stackaddr */ \ PTHREAD_STACK_DEFAULT, /* stacksize */ \ + PTHREAD_GUARD_DEFAULT, /* guardsize */ \ {0, 0}, /* repl_period */ \ {0, 0} /* budget */ \ } @@ -70,6 +71,7 @@ 0, /* max_repl */ \ NULL, /* stackaddr */ \ PTHREAD_STACK_DEFAULT, /* stacksize */ \ + PTHREAD_GUARD_DEFAULT, /* guardsize */ \ {0, 0}, /* repl_period */ \ {0, 0}, /* budget */ \ } @@ -83,6 +85,7 @@ 0, /* affinity */ \ NULL, /* stackaddr */ \ PTHREAD_STACK_DEFAULT, /* stacksize */ \ + PTHREAD_GUARD_DEFAULT, /* guardsize */ \ } #else # define PTHREAD_ATTR_INITIALIZER \ @@ -93,6 +96,7 @@ PTHREAD_CREATE_JOINABLE, /* detachstate */ \ NULL, /* stackaddr */ \ PTHREAD_STACK_DEFAULT, /* stacksize */ \ + PTHREAD_GUARD_DEFAULT, /* guardsize */ \ } #endif diff --git a/include/pthread.h b/include/pthread.h index ef6a831af7d..56bb6aba055 100644 --- a/include/pthread.h +++ b/include/pthread.h @@ -107,6 +107,7 @@ #define PTHREAD_STACK_MIN CONFIG_PTHREAD_STACK_MIN #define PTHREAD_STACK_DEFAULT CONFIG_PTHREAD_STACK_DEFAULT +#define PTHREAD_GUARD_DEFAULT CONFIG_PTHREAD_GUARDSIZE_DEFAULT /* 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 */ 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 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_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 */ int pthread_setname_np(pthread_t thread, FAR const char *name); diff --git a/libs/libc/pthread/CMakeLists.txt b/libs/libc/pthread/CMakeLists.txt index fd46897e701..61ef6158cac 100644 --- a/libs/libc/pthread/CMakeLists.txt +++ b/libs/libc/pthread/CMakeLists.txt @@ -51,6 +51,8 @@ if(NOT CONFIG_DISABLE_PTHREAD) pthread_attr_getschedparam.c pthread_attr_setscope.c pthread_attr_getscope.c + pthread_attr_setguardsize.c + pthread_attr_getguardsize.c pthread_barrierattr_init.c pthread_barrierattr_destroy.c pthread_barrierattr_getpshared.c diff --git a/libs/libc/pthread/Make.defs b/libs/libc/pthread/Make.defs index 69c6ce4f4a4..581fcc50a55 100644 --- a/libs/libc/pthread/Make.defs +++ b/libs/libc/pthread/Make.defs @@ -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_setschedparam.c pthread_attr_getschedparam.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_getpshared.c pthread_barrierattr_setpshared.c CSRCS += pthread_barrierinit.c pthread_barrierdestroy.c pthread_barrierwait.c diff --git a/libs/libc/pthread/pthread_attr_getguardsize.c b/libs/libc/pthread/pthread_attr_getguardsize.c new file mode 100644 index 00000000000..d7381ed1298 --- /dev/null +++ b/libs/libc/pthread/pthread_attr_getguardsize.c @@ -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 + +/**************************************************************************** + * 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; +} diff --git a/libs/libc/pthread/pthread_attr_setguardsize.c b/libs/libc/pthread/pthread_attr_setguardsize.c new file mode 100644 index 00000000000..2686146cc5f --- /dev/null +++ b/libs/libc/pthread/pthread_attr_setguardsize.c @@ -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 + +/**************************************************************************** + * 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; +} diff --git a/sched/Kconfig b/sched/Kconfig index 81f226c1cef..1d5c88f16a1 100644 --- a/sched/Kconfig +++ b/sched/Kconfig @@ -1960,6 +1960,14 @@ config PTHREAD_STACK_DEFAULT ---help--- 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 config SCHED_BACKTRACE diff --git a/sched/pthread/pthread_create.c b/sched/pthread/pthread_create.c index 227266cba76..142b6df5286 100644 --- a/sched/pthread/pthread_create.c +++ b/sched/pthread/pthread_create.c @@ -259,7 +259,8 @@ int nx_pthread_create(pthread_trampoline_t trampoline, FAR pthread_t *thread, { /* 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); }