This is an automated email from the ASF dual-hosted git repository. xiaoxiang781216 pushed a commit to branch master in repository https://gitbox.apache.org/repos/asf/nuttx.git
commit 8242992c11179ffc4c0cff88803e385681943fb6 Author: ganjing <[email protected]> AuthorDate: Mon Nov 17 20:32:13 2025 +0800 !libc/pthread: Return EINVAL for invalid pthread objects. POSIX allows the pthread functions to fail with EINVAL when an attribute object, a condition variable or a mutex is not initialized or was destroyed, and PSE52 tests it. Most of the functions only checked for NULL or failed an assertion. Check that the object is valid: * pthread_attr_t: the priority is in range. * pthread_condattr_t, pthread_mutexattr_t: pshared is a valid value. * pthread_cond_t: clockid is CLOCK_REALTIME or CLOCK_MONOTONIC. pthread_cond_destroy() clears clockid, and returns EBUSY while threads wait on the condition. * pthread_mutex_t: the semaphore is a mutex. nxsem_destroy() clears the semaphore flags. A zero-filled object must not pass the checks, so 0 is no longer a valid PTHREAD_PROCESS_* or CLOCK_* value. CLOCK_FD, CLOCK_MASK and CLOCK_SHIFT move from <nuttx/clock.h> to <time.h>, next to the clock IDs. BREAKING CHANGE: PTHREAD_PROCESS_PRIVATE and PTHREAD_PROCESS_SHARED are now 1 and 2, and CLOCK_REALTIME, CLOCK_MONOTONIC, CLOCK_PROCESS_CPUTIME_ID, CLOCK_THREAD_CPUTIME_ID, CLOCK_BOOTTIME and CLOCK_FD are now 1 to 6. Code that uses the symbolic names only needs to be rebuilt. Code that takes numeric values from the user, such as the -c option of cyclictest in nuttx-apps, must map them to the names. pthread_cond_destroy() now returns EBUSY while threads wait on the condition: wake them before the condition is destroyed. Assisted-by: Kiro:auto Signed-off-by: ganjing <[email protected]> --- include/nuttx/clock.h | 19 ------ include/pthread.h | 10 ++-- include/semaphore.h | 6 +- include/time.h | 32 +++++++++-- libs/libc/pthread/pthread.h | 67 ++++++++++++++++++++++ libs/libc/pthread/pthread_attr_destroy.c | 9 ++- libs/libc/pthread/pthread_attr_getdetachstate.c | 9 ++- libs/libc/pthread/pthread_attr_getguardsize.c | 9 ++- libs/libc/pthread/pthread_attr_getinheritsched.c | 9 ++- libs/libc/pthread/pthread_attr_getschedparam.c | 9 ++- libs/libc/pthread/pthread_attr_getschedpolicy.c | 9 ++- libs/libc/pthread/pthread_attr_getscope.c | 11 ++++ libs/libc/pthread/pthread_attr_getstack.c | 9 ++- libs/libc/pthread/pthread_attr_getstackaddr.c | 9 ++- libs/libc/pthread/pthread_attr_getstacksize.c | 9 ++- libs/libc/pthread/pthread_attr_setdetachstate.c | 12 +++- libs/libc/pthread/pthread_attr_setinheritsched.c | 9 ++- libs/libc/pthread/pthread_attr_setschedparam.c | 9 ++- libs/libc/pthread/pthread_attr_setschedpolicy.c | 11 +++- libs/libc/pthread/pthread_attr_setscope.c | 11 ++++ libs/libc/pthread/pthread_attr_setstack.c | 11 +++- libs/libc/pthread/pthread_attr_setstackaddr.c | 9 ++- libs/libc/pthread/pthread_attr_setstacksize.c | 9 ++- libs/libc/pthread/pthread_condattr_destroy.c | 13 ++++- libs/libc/pthread/pthread_condattr_getclock.c | 9 ++- libs/libc/pthread/pthread_condattr_getpshared.c | 9 ++- libs/libc/pthread/pthread_condattr_setclock.c | 9 ++- libs/libc/pthread/pthread_condattr_setpshared.c | 10 +++- libs/libc/pthread/pthread_condbroadcast.c | 7 ++- libs/libc/pthread/pthread_condclockwait.c | 13 ++++- libs/libc/pthread/pthread_conddestroy.c | 20 ++++++- libs/libc/pthread/pthread_condinit.c | 13 ++++- libs/libc/pthread/pthread_condsignal.c | 7 ++- libs/libc/pthread/pthread_condwait.c | 2 +- libs/libc/pthread/pthread_mutex_destroy.c | 11 +++- libs/libc/pthread/pthread_mutex_init.c | 11 +++- libs/libc/pthread/pthread_mutex_setprioceiling.c | 13 +++++ libs/libc/pthread/pthread_mutexattr_destroy.c | 9 ++- .../pthread/pthread_mutexattr_getprioceiling.c | 4 +- libs/libc/pthread/pthread_mutexattr_getprotocol.c | 7 ++- libs/libc/pthread/pthread_mutexattr_getpshared.c | 4 +- libs/libc/pthread/pthread_mutexattr_gettype.c | 4 +- libs/libc/pthread/pthread_mutexattr_init.c | 2 +- .../pthread/pthread_mutexattr_setprioceiling.c | 5 +- libs/libc/pthread/pthread_mutexattr_setprotocol.c | 9 ++- libs/libc/pthread/pthread_mutexattr_setpshared.c | 6 +- libs/libc/pthread/pthread_mutexattr_settype.c | 4 +- libs/libc/semaphore/sem_init.c | 2 +- sched/semaphore/sem_destroy.c | 1 + sched/semaphore/sem_wait.c | 2 +- 50 files changed, 436 insertions(+), 77 deletions(-) diff --git a/include/nuttx/clock.h b/include/nuttx/clock.h index 1e3dab8171f..896fdd37611 100644 --- a/include/nuttx/clock.h +++ b/include/nuttx/clock.h @@ -76,25 +76,6 @@ # define __HAVE_KERNEL_GLOBALS 1 #endif -/* The following are the bit fields of the clockid_t - * bit 0~2: the clock type - * CLOCK_REALTIME - 0 - * CLOCK_MONOTONIC - 1 - * CLOCK_PROCESS_CPUTIME_ID - 2 - * CLOCK_THREAD_CPUTIME_ID - 3 - * CLOCK_BOOTTIME - 4 - * CLOCK_FD - 5 - * - * if the clockid value exceeds CLOCK_MASK, it indicates a dynamic clockid. - * bit 3~32: the fd, pid or tid value - * - * The CLOCK_MASK are using to extract the clock_type from the clockid_t - */ - -#define CLOCK_FD 5 -#define CLOCK_MASK 7 -#define CLOCK_SHIFT 3 - /* Timing constants *********************************************************/ #define NSEC_PER_SEC 1000000000L /* Seconds */ diff --git a/include/pthread.h b/include/pthread.h index 48c6f196b76..dca4df1008e 100644 --- a/include/pthread.h +++ b/include/pthread.h @@ -48,8 +48,8 @@ /* Values for the process shared (pshared) attribute */ -#define PTHREAD_PROCESS_PRIVATE 0 -#define PTHREAD_PROCESS_SHARED 1 +#define PTHREAD_PROCESS_PRIVATE 1 +#define PTHREAD_PROCESS_SHARED 2 /* Values for the mutext type attribute: * @@ -263,7 +263,7 @@ typedef struct pthread_cond_s pthread_cond_t; struct pthread_mutexattr_s { - uint8_t pshared : 1; /* PTHREAD_PROCESS_PRIVATE or PTHREAD_PROCESS_SHARED */ + uint8_t pshared : 2; /* PTHREAD_PROCESS_PRIVATE or PTHREAD_PROCESS_SHARED */ #if defined(CONFIG_PRIORITY_INHERITANCE) || defined(CONFIG_PRIORITY_PROTECT) uint8_t prio : 2; /* See PTHREAD_PRIO_* definitions */ #endif @@ -318,13 +318,13 @@ typedef struct pthread_mutex_s pthread_mutex_t; #ifdef CONFIG_PTHREAD_MUTEX_DEFAULT_PRIO_INHERIT # define PTHREAD_MUTEX_DEFAULT_PRIO_INHERIT SEM_PRIO_INHERIT #else -# define PTHREAD_MUTEX_DEFAULT_PRIO_INHERIT 0 +# define PTHREAD_MUTEX_DEFAULT_PRIO_INHERIT SEM_PRIO_NONE #endif #ifdef CONFIG_PTHREAD_MUTEX_DEFAULT_PRIO_PROTECT # define PTHREAD_MUTEX_DEFAULT_PRIO_PROTECT SEM_PRIO_PROTECT #else -# define PTHREAD_MUTEX_DEFAULT_PRIO_PROTECT 0 +# define PTHREAD_MUTEX_DEFAULT_PRIO_PROTECT SEM_PRIO_NONE #endif #define PTHREAD_MUTEX_DEFAULT_PRIO_FLAGS (PTHREAD_MUTEX_DEFAULT_PRIO_INHERIT | \ diff --git a/include/semaphore.h b/include/semaphore.h index 06cdfd07baf..b8b9d611295 100644 --- a/include/semaphore.h +++ b/include/semaphore.h @@ -149,18 +149,18 @@ typedef struct sem_s sem_t; /* semcount, flags, waitlist, hhead */ # define SEM_INITIALIZER(c) \ - {{(c)}, 0, SEM_WAITLIST_INITIALIZER, NULL} + {{(c)}, SEM_PRIO_NONE, SEM_WAITLIST_INITIALIZER, NULL} # else /* semcount, flags, waitlist, holder[2] */ # define SEM_INITIALIZER(c) \ - {{(c)}, 0, SEM_WAITLIST_INITIALIZER, SEMHOLDER_INITIALIZER} + {{(c)}, SEM_PRIO_NONE, SEM_WAITLIST_INITIALIZER, SEMHOLDER_INITIALIZER} # endif #else /* semcount, flags, waitlist */ # define SEM_INITIALIZER(c) \ - {{(c)}, 0, SEM_WAITLIST_INITIALIZER} + {{(c)}, SEM_PRIO_NONE, SEM_WAITLIST_INITIALIZER} #endif #define SEM_WAITLIST(sem) (&((sem)->waitlist)) diff --git a/include/time.h b/include/time.h index 299fd54231b..e537983411a 100644 --- a/include/time.h +++ b/include/time.h @@ -63,28 +63,50 @@ * forward and backward as the system time-of-day clock is changed. */ -#define CLOCK_REALTIME 0 +#define CLOCK_REALTIME 1 /* Clock that cannot be set and represents monotonic time since some * unspecified starting point. It is not affected by changes in the * system time-of-day clock. */ -#define CLOCK_MONOTONIC 1 +#define CLOCK_MONOTONIC 2 /* Clock that measures CPU time consumed by this process (i.e., CPU * time consumed by all threads in the process). */ -#define CLOCK_PROCESS_CPUTIME_ID 2 +#define CLOCK_PROCESS_CPUTIME_ID 3 /* Clock that measures CPU time consumed by this thread */ -#define CLOCK_THREAD_CPUTIME_ID 3 +#define CLOCK_THREAD_CPUTIME_ID 4 /* Monotonic system-wide clock that includes time spent in suspension. */ -#define CLOCK_BOOTTIME 4 +#define CLOCK_BOOTTIME 5 + +/* Clock sources that are managed via file descriptors(e.g. PTP driver) */ + +#define CLOCK_FD 6 + +/* The following are the bit fields of the clockid_t + * bit 0~2: the clock type + * CLOCK_REALTIME - 1 + * CLOCK_MONOTONIC - 2 + * CLOCK_PROCESS_CPUTIME_ID - 3 + * CLOCK_THREAD_CPUTIME_ID - 4 + * CLOCK_BOOTTIME - 5 + * CLOCK_FD - 6 + * + * if the clockid value exceeds CLOCK_MASK, it indicates a dynamic clockid. + * bit 3~31: the fd, pid or tid value + * + * CLOCK_MASK extracts the clock type from the clockid_t + */ + +#define CLOCK_MASK 7 +#define CLOCK_SHIFT 3 /* This is a flag that may be passed to the timer_settime() and * clock_nanosleep() functions. diff --git a/libs/libc/pthread/pthread.h b/libs/libc/pthread/pthread.h index 7673bf59301..e4a2e6bd4cd 100644 --- a/libs/libc/pthread/pthread.h +++ b/libs/libc/pthread/pthread.h @@ -75,6 +75,73 @@ #define COND_WAIT_COUNT(cond) ((FAR atomic_t *)&(cond)->wait_count) +/* Validate if a pthread_attr_t object is valid. + * + * This macro checks: + * - The attribute pointer is not NULL + * - The priority field is within valid scheduling priority range + */ + +#define PTHREAD_ATTR_IS_VALID(attr) \ + ((attr) != NULL && \ + (attr)->priority >= SCHED_PRIORITY_MIN && \ + (attr)->priority <= SCHED_PRIORITY_MAX) + +/* Validate if a pthread_condattr_t object is valid. + * + * This macro checks: + * - The attribute pointer is not NULL + * - The pshared field contains a valid value + */ + +#define PTHREAD_CONDATTR_IS_VALID(attr) \ + ((attr) != NULL && \ + ((attr)->pshared == PTHREAD_PROCESS_PRIVATE || \ + (attr)->pshared == PTHREAD_PROCESS_SHARED)) + +/* Validate if a pthread_cond_t object is valid. + * + * This macro checks: + * - The condition pointer is not NULL + * - The clockid field contains a valid value + */ + +#define PTHREAD_COND_IS_VALID(cond) \ + ((cond) != NULL && \ + ((cond)->clockid == CLOCK_REALTIME || \ + (cond)->clockid == CLOCK_MONOTONIC)) + +/* Validate if a pthread_mutexattr_t object is valid. + * + * This macro checks: + * - The attribute pointer is not NULL + * - The pshared field contains a valid value + */ + +#define PTHREAD_MUTEXATTR_IS_VALID(attr) \ + ((attr) != NULL && \ + ((attr)->pshared == PTHREAD_PROCESS_PRIVATE || \ + (attr)->pshared == PTHREAD_PROCESS_SHARED)) + +/* Validate if a pthread_mutex_t object is valid. + * + * This macro checks: + * - The mutex pointer is not NULL + * - The underlying semaphore is a mutex, + * when initialized, the flag SEM_TYPE_MUTEX is set. + * when destroyed, the flag SEM_TYPE_MUTEX is cleared. + */ + +#ifdef CONFIG_PTHREAD_MUTEX_TYPES +# define PTHREAD_MUTEX_IS_VALID(mutex) \ + ((mutex) != NULL && \ + ((mutex)->mutex.mutex.sem.flags & SEM_TYPE_MUTEX)) +#else +# define PTHREAD_MUTEX_IS_VALID(mutex) \ + ((mutex) != NULL && \ + ((mutex)->mutex.sem.flags & SEM_TYPE_MUTEX)) +#endif + /**************************************************************************** * Public Function Prototypes ****************************************************************************/ diff --git a/libs/libc/pthread/pthread_attr_destroy.c b/libs/libc/pthread/pthread_attr_destroy.c index da43fd7f491..51595ca8237 100644 --- a/libs/libc/pthread/pthread_attr_destroy.c +++ b/libs/libc/pthread/pthread_attr_destroy.c @@ -31,6 +31,8 @@ #include <nuttx/debug.h> #include <errno.h> +#include "pthread.h" + /**************************************************************************** * Public Functions ****************************************************************************/ @@ -57,7 +59,12 @@ int pthread_attr_destroy(FAR pthread_attr_t *attr) linfo("attr=%p\n", attr); - if (!attr) + /* When the attribute object specified by attr is invalid, + * then a call to pthread_attr_destroy(attr) + * returns EINVAL. + */ + + if (!PTHREAD_ATTR_IS_VALID(attr)) { ret = EINVAL; } diff --git a/libs/libc/pthread/pthread_attr_getdetachstate.c b/libs/libc/pthread/pthread_attr_getdetachstate.c index 6e9b146cdc9..227def2d3d4 100644 --- a/libs/libc/pthread/pthread_attr_getdetachstate.c +++ b/libs/libc/pthread/pthread_attr_getdetachstate.c @@ -29,6 +29,8 @@ #include <pthread.h> #include <errno.h> +#include "pthread.h" + /**************************************************************************** * Public Functions ****************************************************************************/ @@ -50,7 +52,12 @@ int pthread_attr_getdetachstate(FAR const pthread_attr_t *attr, FAR int *detachstate) { - if (!attr || !detachstate) + /* When the attribute object specified by attr is invalid, + * then a call to pthread_attr_getdetachstate(attr, detachstate) + * returns EINVAL. + */ + + if (!PTHREAD_ATTR_IS_VALID(attr) || !detachstate) { return EINVAL; } diff --git a/libs/libc/pthread/pthread_attr_getguardsize.c b/libs/libc/pthread/pthread_attr_getguardsize.c index d7381ed1298..57d8f2f9cc6 100644 --- a/libs/libc/pthread/pthread_attr_getguardsize.c +++ b/libs/libc/pthread/pthread_attr_getguardsize.c @@ -26,6 +26,8 @@ #include <pthread.h> +#include "pthread.h" + /**************************************************************************** * Public Functions ****************************************************************************/ @@ -53,7 +55,12 @@ int pthread_attr_getguardsize(FAR const pthread_attr_t *attr, { int ret; - if (!guardsize) + /* When the attribute object specified by attr is invalid, + * then a call to pthread_attr_getguardsize(attr, guardsize) + * returns EINVAL. + */ + + if (!PTHREAD_ATTR_IS_VALID(attr) || !guardsize) { ret = EINVAL; } diff --git a/libs/libc/pthread/pthread_attr_getinheritsched.c b/libs/libc/pthread/pthread_attr_getinheritsched.c index d5b3f49c74e..1f1b492165e 100644 --- a/libs/libc/pthread/pthread_attr_getinheritsched.c +++ b/libs/libc/pthread/pthread_attr_getinheritsched.c @@ -31,6 +31,8 @@ #include <nuttx/debug.h> #include <errno.h> +#include "pthread.h" + /**************************************************************************** * Public Functions ****************************************************************************/ @@ -61,7 +63,12 @@ int pthread_attr_getinheritsched(FAR const pthread_attr_t *attr, linfo("attr=%p inheritsched=%p\n", attr, inheritsched); - if (!attr || !inheritsched) + /* When the attribute object specified by attr is invalid, + * then a call to pthread_attr_getinheritsched(attr, inheritsched) + * returns EINVAL. + */ + + if (!PTHREAD_ATTR_IS_VALID(attr) || !inheritsched) { ret = EINVAL; } diff --git a/libs/libc/pthread/pthread_attr_getschedparam.c b/libs/libc/pthread/pthread_attr_getschedparam.c index 6e991560cbb..cdd59f4a231 100644 --- a/libs/libc/pthread/pthread_attr_getschedparam.c +++ b/libs/libc/pthread/pthread_attr_getschedparam.c @@ -32,6 +32,8 @@ #include <nuttx/debug.h> #include <errno.h> +#include "pthread.h" + /**************************************************************************** * Public Functions ****************************************************************************/ @@ -59,7 +61,12 @@ int pthread_attr_getschedparam(FAR const pthread_attr_t *attr, linfo("attr=%p param=%p\n", attr, param); - if (!attr || !param) + /* When the attribute object specified by attr is invalid, + * then a call to pthread_attr_getschedparam(attr, param) + * returns EINVAL. + */ + + if (!PTHREAD_ATTR_IS_VALID(attr) || !param) { ret = EINVAL; } diff --git a/libs/libc/pthread/pthread_attr_getschedpolicy.c b/libs/libc/pthread/pthread_attr_getschedpolicy.c index 15f08da1c48..80dd8676606 100644 --- a/libs/libc/pthread/pthread_attr_getschedpolicy.c +++ b/libs/libc/pthread/pthread_attr_getschedpolicy.c @@ -30,6 +30,8 @@ #include <nuttx/debug.h> #include <errno.h> +#include "pthread.h" + /**************************************************************************** * Public Functions ****************************************************************************/ @@ -58,7 +60,12 @@ int pthread_attr_getschedpolicy(FAR const pthread_attr_t *attr, linfo("attr=%p policy=%p\n", attr, policy); - if (!attr || !policy) + /* When the attribute object specified by attr is invalid, + * then a call to pthread_attr_getschedpolicy(attr, policy) + * returns EINVAL. + */ + + if (!PTHREAD_ATTR_IS_VALID(attr) || !policy) { ret = EINVAL; } diff --git a/libs/libc/pthread/pthread_attr_getscope.c b/libs/libc/pthread/pthread_attr_getscope.c index 7628b2b1215..eb107775075 100644 --- a/libs/libc/pthread/pthread_attr_getscope.c +++ b/libs/libc/pthread/pthread_attr_getscope.c @@ -26,6 +26,8 @@ #include <pthread.h> +#include "pthread.h" + /**************************************************************************** * Public Functions ****************************************************************************/ @@ -50,6 +52,15 @@ int pthread_attr_getscope(FAR const pthread_attr_t *attr, FAR int *scope) { + /* When the attribute object specified by attr is invalid, + * then a call to pthread_attr_getscope(attr, scope) returns EINVAL. + */ + + if (!PTHREAD_ATTR_IS_VALID(attr) || !scope) + { + return EINVAL; + } + *scope = PTHREAD_SCOPE_SYSTEM; return 0; } diff --git a/libs/libc/pthread/pthread_attr_getstack.c b/libs/libc/pthread/pthread_attr_getstack.c index 38d50599cb2..b341f5ef498 100644 --- a/libs/libc/pthread/pthread_attr_getstack.c +++ b/libs/libc/pthread/pthread_attr_getstack.c @@ -27,6 +27,8 @@ #include <pthread.h> #include <errno.h> +#include "pthread.h" + /**************************************************************************** * Public Functions ****************************************************************************/ @@ -55,7 +57,12 @@ int pthread_attr_getstack(FAR const pthread_attr_t *attr, { int ret; - if (!attr || !stackaddr || !stacksize) + /* When the attribute object specified by attr is invalid, + * then a call to pthread_attr_getstack(attr, stackaddr, stacksize) + * returns EINVAL. + */ + + if (!PTHREAD_ATTR_IS_VALID(attr) || !stackaddr || !stacksize) { ret = EINVAL; } diff --git a/libs/libc/pthread/pthread_attr_getstackaddr.c b/libs/libc/pthread/pthread_attr_getstackaddr.c index edcb4525327..190f724c250 100644 --- a/libs/libc/pthread/pthread_attr_getstackaddr.c +++ b/libs/libc/pthread/pthread_attr_getstackaddr.c @@ -27,6 +27,8 @@ #include <pthread.h> #include <errno.h> +#include "pthread.h" + /**************************************************************************** * Public Functions ****************************************************************************/ @@ -54,7 +56,12 @@ int pthread_attr_getstackaddr(FAR const pthread_attr_t *attr, { int ret; - if (!attr || !stackaddr) + /* When the attribute object specified by attr is invalid, + * then a call to pthread_attr_getstackaddr(attr, stackaddr) + * returns EINVAL. + */ + + if (!PTHREAD_ATTR_IS_VALID(attr) || !stackaddr) { ret = EINVAL; } diff --git a/libs/libc/pthread/pthread_attr_getstacksize.c b/libs/libc/pthread/pthread_attr_getstacksize.c index 97dcf3d9fd9..0e649bcde5c 100644 --- a/libs/libc/pthread/pthread_attr_getstacksize.c +++ b/libs/libc/pthread/pthread_attr_getstacksize.c @@ -27,6 +27,8 @@ #include <pthread.h> #include <errno.h> +#include "pthread.h" + /**************************************************************************** * Public Functions ****************************************************************************/ @@ -54,7 +56,12 @@ int pthread_attr_getstacksize(FAR const pthread_attr_t *attr, { int ret; - if (!stacksize) + /* When the attribute object specified by attr is invalid, + * then a call to pthread_attr_getstacksize(attr, stacksize) + * returns EINVAL. + */ + + if (!PTHREAD_ATTR_IS_VALID(attr) || !stacksize) { ret = EINVAL; } diff --git a/libs/libc/pthread/pthread_attr_setdetachstate.c b/libs/libc/pthread/pthread_attr_setdetachstate.c index 605da54f713..ab190206b2a 100644 --- a/libs/libc/pthread/pthread_attr_setdetachstate.c +++ b/libs/libc/pthread/pthread_attr_setdetachstate.c @@ -29,6 +29,8 @@ #include <pthread.h> #include <errno.h> +#include "pthread.h" + /**************************************************************************** * Public Functions ****************************************************************************/ @@ -49,8 +51,14 @@ int pthread_attr_setdetachstate(FAR pthread_attr_t *attr, int detachstate) { - if (!attr || (detachstate != PTHREAD_CREATE_DETACHED && - detachstate != PTHREAD_CREATE_JOINABLE)) + /* When the attribute object specified by attr is invalid, + * then a call to pthread_attr_setdetachstate(attr, detachstate) + * returns EINVAL. + */ + + if (!PTHREAD_ATTR_IS_VALID(attr) || + (detachstate != PTHREAD_CREATE_DETACHED && + detachstate != PTHREAD_CREATE_JOINABLE)) { return EINVAL; } diff --git a/libs/libc/pthread/pthread_attr_setinheritsched.c b/libs/libc/pthread/pthread_attr_setinheritsched.c index 62150857160..3efe631e629 100644 --- a/libs/libc/pthread/pthread_attr_setinheritsched.c +++ b/libs/libc/pthread/pthread_attr_setinheritsched.c @@ -32,6 +32,8 @@ #include <nuttx/debug.h> #include <errno.h> +#include "pthread.h" + /**************************************************************************** * Public Functions ****************************************************************************/ @@ -62,7 +64,12 @@ int pthread_attr_setinheritsched(FAR pthread_attr_t *attr, linfo("inheritsched=%d\n", inheritsched); - if (!attr || + /* When the attribute object specified by attr is invalid, + * then a call to pthread_attr_setinheritsched(attr, inheritsched) + * returns EINVAL. + */ + + if (!PTHREAD_ATTR_IS_VALID(attr) || (inheritsched != PTHREAD_INHERIT_SCHED && inheritsched != PTHREAD_EXPLICIT_SCHED)) { diff --git a/libs/libc/pthread/pthread_attr_setschedparam.c b/libs/libc/pthread/pthread_attr_setschedparam.c index 723ee153c0b..ac470ebdc82 100644 --- a/libs/libc/pthread/pthread_attr_setschedparam.c +++ b/libs/libc/pthread/pthread_attr_setschedparam.c @@ -32,6 +32,8 @@ #include <nuttx/debug.h> #include <errno.h> +#include "pthread.h" + /**************************************************************************** * Public Functions ****************************************************************************/ @@ -59,7 +61,12 @@ int pthread_attr_setschedparam(FAR pthread_attr_t *attr, linfo("attr=%p param=%p\n", attr, param); - if (!attr || !param) + /* When the attribute object specified by attr is invalid, + * then a call to pthread_attr_setschedparam(attr, param) + * returns EINVAL. + */ + + if (!PTHREAD_ATTR_IS_VALID(attr) || !param) { ret = EINVAL; } diff --git a/libs/libc/pthread/pthread_attr_setschedpolicy.c b/libs/libc/pthread/pthread_attr_setschedpolicy.c index 4cf2e6a56b2..d30449cb129 100644 --- a/libs/libc/pthread/pthread_attr_setschedpolicy.c +++ b/libs/libc/pthread/pthread_attr_setschedpolicy.c @@ -32,6 +32,8 @@ #include <nuttx/debug.h> #include <errno.h> +#include "pthread.h" + /**************************************************************************** * Public Functions ****************************************************************************/ @@ -59,8 +61,13 @@ int pthread_attr_setschedpolicy(FAR pthread_attr_t *attr, int policy) linfo("attr=%p policy=%d\n", attr, policy); - if (!attr || (policy != SCHED_OTHER - && policy != SCHED_FIFO + /* When the attribute object specified by attr is invalid, + * then a call to pthread_attr_setschedpolicy(attr, policy) + * returns EINVAL. + */ + + if (!PTHREAD_ATTR_IS_VALID(attr) || + (policy != SCHED_OTHER && policy != SCHED_FIFO #if CONFIG_RR_INTERVAL > 0 && policy != SCHED_RR #endif diff --git a/libs/libc/pthread/pthread_attr_setscope.c b/libs/libc/pthread/pthread_attr_setscope.c index 614a54b7ceb..e0766112872 100644 --- a/libs/libc/pthread/pthread_attr_setscope.c +++ b/libs/libc/pthread/pthread_attr_setscope.c @@ -27,6 +27,8 @@ #include <pthread.h> #include <errno.h> +#include "pthread.h" + /**************************************************************************** * Public Functions ****************************************************************************/ @@ -70,6 +72,15 @@ int pthread_attr_setscope(FAR pthread_attr_t *attr, int scope) { + /* When the attribute object specified by attr is invalid, + * then a call to pthread_attr_setscope(attr, scope) returns EINVAL. + */ + + if (!PTHREAD_ATTR_IS_VALID(attr)) + { + return EINVAL; + } + switch (scope) { case PTHREAD_SCOPE_SYSTEM: diff --git a/libs/libc/pthread/pthread_attr_setstack.c b/libs/libc/pthread/pthread_attr_setstack.c index f987e8f1efe..7fc4cb33676 100644 --- a/libs/libc/pthread/pthread_attr_setstack.c +++ b/libs/libc/pthread/pthread_attr_setstack.c @@ -31,6 +31,8 @@ #include <nuttx/tls.h> +#include "pthread.h" + /**************************************************************************** * Public Functions ****************************************************************************/ @@ -59,7 +61,14 @@ int pthread_attr_setstack(FAR pthread_attr_t *attr, { int ret; - if (!attr || !stackaddr || stacksize < PTHREAD_STACK_MIN) + /* If the implementation detects invalid pthread_attr_t objects: + * When the attribute object specified by attr is invalid, + * then a call to pthread_attr_setstack(attr, stackaddr, stacksize) + * returns EINVAL. + */ + + if (!PTHREAD_ATTR_IS_VALID(attr) || stackaddr == NULL || + stacksize < PTHREAD_STACK_MIN) { ret = EINVAL; } diff --git a/libs/libc/pthread/pthread_attr_setstackaddr.c b/libs/libc/pthread/pthread_attr_setstackaddr.c index 0b902933757..281c43a4665 100644 --- a/libs/libc/pthread/pthread_attr_setstackaddr.c +++ b/libs/libc/pthread/pthread_attr_setstackaddr.c @@ -27,6 +27,8 @@ #include <pthread.h> #include <errno.h> +#include "pthread.h" + /**************************************************************************** * Public Functions ****************************************************************************/ @@ -53,7 +55,12 @@ int pthread_attr_setstackaddr(FAR pthread_attr_t *attr, FAR void *stackaddr) { int ret; - if (!attr || !stackaddr) + /* When the attribute object specified by attr is invalid, + * then a call to pthread_attr_setstackaddr(attr, stackaddr) + * returns EINVAL. + */ + + if (!PTHREAD_ATTR_IS_VALID(attr) || !stackaddr) { ret = EINVAL; } diff --git a/libs/libc/pthread/pthread_attr_setstacksize.c b/libs/libc/pthread/pthread_attr_setstacksize.c index 5b2fed3a69b..9eae86cbbe4 100644 --- a/libs/libc/pthread/pthread_attr_setstacksize.c +++ b/libs/libc/pthread/pthread_attr_setstacksize.c @@ -31,6 +31,8 @@ #include <nuttx/tls.h> +#include "pthread.h" + /**************************************************************************** * Public Functions ****************************************************************************/ @@ -57,7 +59,12 @@ int pthread_attr_setstacksize(FAR pthread_attr_t *attr, size_t stacksize) { int ret; - if (!attr || stacksize < PTHREAD_STACK_MIN) + /* When the attribute object specified by attr is invalid, + * then a call to pthread_attr_setstacksize(attr, stacksize) + * returns EINVAL. + */ + + if (!PTHREAD_ATTR_IS_VALID(attr) || stacksize < PTHREAD_STACK_MIN) { ret = EINVAL; } diff --git a/libs/libc/pthread/pthread_condattr_destroy.c b/libs/libc/pthread/pthread_condattr_destroy.c index 94b654eb976..659fc8da779 100644 --- a/libs/libc/pthread/pthread_condattr_destroy.c +++ b/libs/libc/pthread/pthread_condattr_destroy.c @@ -30,6 +30,8 @@ #include <nuttx/debug.h> #include <errno.h> +#include "pthread.h" + /**************************************************************************** * Public Functions ****************************************************************************/ @@ -56,10 +58,19 @@ int pthread_condattr_destroy(FAR pthread_condattr_t *attr) linfo("attr=%p\n", attr); - if (!attr) + /* When the attribute object specified by attr is invalid, + * then a call to pthread_condattr_destroy(attr) + * returns EINVAL. + */ + + if (!PTHREAD_CONDATTR_IS_VALID(attr)) { ret = EINVAL; } + else + { + memset(attr, 0, sizeof(pthread_condattr_t)); + } linfo("Returning %d\n", ret); return ret; diff --git a/libs/libc/pthread/pthread_condattr_getclock.c b/libs/libc/pthread/pthread_condattr_getclock.c index a73a17e4aaf..1e8973d3fca 100644 --- a/libs/libc/pthread/pthread_condattr_getclock.c +++ b/libs/libc/pthread/pthread_condattr_getclock.c @@ -29,6 +29,8 @@ #include <pthread.h> #include <errno.h> +#include "pthread.h" + /**************************************************************************** * Public Functions ****************************************************************************/ @@ -53,7 +55,12 @@ int pthread_condattr_getclock(FAR const pthread_condattr_t *attr, FAR clockid_t *clock_id) { - if (!attr) + /* When the attribute object specified by attr is invalid, + * then a call to pthread_condattr_getclock(attr, clock_id) + * returns EINVAL. + */ + + if (!PTHREAD_CONDATTR_IS_VALID(attr) || clock_id == NULL) { return EINVAL; } diff --git a/libs/libc/pthread/pthread_condattr_getpshared.c b/libs/libc/pthread/pthread_condattr_getpshared.c index 66796d12a8d..cbcbca24bfa 100644 --- a/libs/libc/pthread/pthread_condattr_getpshared.c +++ b/libs/libc/pthread/pthread_condattr_getpshared.c @@ -30,6 +30,8 @@ #include <errno.h> #include <nuttx/debug.h> +#include "pthread.h" + /**************************************************************************** * Public Functions ****************************************************************************/ @@ -58,7 +60,12 @@ int pthread_condattr_getpshared(FAR const pthread_condattr_t *attr, { int ret = OK; - if (!attr || !pshared) + /* When the attribute object specified by attr is invalid, + * then a call to pthread_condattr_getpshared(attr, pshared) + * returns EINVAL. + */ + + if (!PTHREAD_CONDATTR_IS_VALID(attr) || !pshared) { ret = EINVAL; } diff --git a/libs/libc/pthread/pthread_condattr_setclock.c b/libs/libc/pthread/pthread_condattr_setclock.c index 1380bd60189..092df5a0b58 100644 --- a/libs/libc/pthread/pthread_condattr_setclock.c +++ b/libs/libc/pthread/pthread_condattr_setclock.c @@ -29,6 +29,8 @@ #include <pthread.h> #include <errno.h> +#include "pthread.h" + /**************************************************************************** * Public Functions ****************************************************************************/ @@ -52,7 +54,12 @@ int pthread_condattr_setclock(FAR pthread_condattr_t *attr, clockid_t clock_id) { - if (!attr || + /* When the attribute object specified by attr is invalid, + * then a call to pthread_condattr_setclock(attr, clock_id) + * returns EINVAL. + */ + + if (!PTHREAD_CONDATTR_IS_VALID(attr) || (clock_id != CLOCK_MONOTONIC && clock_id != CLOCK_REALTIME)) { diff --git a/libs/libc/pthread/pthread_condattr_setpshared.c b/libs/libc/pthread/pthread_condattr_setpshared.c index 61253426bf1..d7b96e5cc5e 100644 --- a/libs/libc/pthread/pthread_condattr_setpshared.c +++ b/libs/libc/pthread/pthread_condattr_setpshared.c @@ -30,6 +30,8 @@ #include <errno.h> #include <nuttx/debug.h> +#include "pthread.h" + /**************************************************************************** * Pre-processor Definitions ****************************************************************************/ @@ -87,7 +89,13 @@ int pthread_condattr_setpshared(FAR pthread_condattr_t *attr, int pshared) { int ret = OK; - if (!attr || (pshared != PTHREAD_PROCESS_SHARED && + /* When the attribute object specified by attr is invalid, + * then a call to pthread_condattr_setpshared(attr, pshared) + * returns EINVAL. + */ + + if (!PTHREAD_CONDATTR_IS_VALID(attr) || + (pshared != PTHREAD_PROCESS_SHARED && pshared != PTHREAD_PROCESS_PRIVATE)) { ret = EINVAL; diff --git a/libs/libc/pthread/pthread_condbroadcast.c b/libs/libc/pthread/pthread_condbroadcast.c index d8712a9afc4..792ef1c4224 100644 --- a/libs/libc/pthread/pthread_condbroadcast.c +++ b/libs/libc/pthread/pthread_condbroadcast.c @@ -61,7 +61,12 @@ int pthread_cond_broadcast(FAR pthread_cond_t *cond) sinfo("cond=%p\n", cond); - if (!cond) + /* If the implementation detects that the condition variable + * specified by cond does not refer to an initialized condition variable: + * a call to pthread_cond_broadcast() returns EINVAL. + */ + + if (!PTHREAD_COND_IS_VALID(cond)) { ret = EINVAL; } diff --git a/libs/libc/pthread/pthread_condclockwait.c b/libs/libc/pthread/pthread_condclockwait.c index d1b899f0998..cb37877371b 100644 --- a/libs/libc/pthread/pthread_condclockwait.c +++ b/libs/libc/pthread/pthread_condclockwait.c @@ -86,7 +86,7 @@ int pthread_cond_clockwait(FAR pthread_cond_t *cond, /* Make sure that non-NULL references were provided. */ - if (!cond || !mutex) + if (!PTHREAD_COND_IS_VALID(cond) || !PTHREAD_MUTEX_IS_VALID(mutex)) { ret = EINVAL; } @@ -107,6 +107,17 @@ int pthread_cond_clockwait(FAR pthread_cond_t *cond, ret = pthread_cond_wait(cond, mutex); } + /* When abstime is invalid, then pthread_cond_timedwait fails and + * returns EINVAL without modifying the state of the condition variable + * specified by cond or the mutex specified by mutex. + */ + + else if (abstime && (abstime->tv_nsec < 0 || + abstime->tv_nsec >= NSEC_PER_SEC)) + { + ret = EINVAL; + } + else { unsigned int nlocks; diff --git a/libs/libc/pthread/pthread_conddestroy.c b/libs/libc/pthread/pthread_conddestroy.c index d58d0b3f7a1..8fc30ce6d90 100644 --- a/libs/libc/pthread/pthread_conddestroy.c +++ b/libs/libc/pthread/pthread_conddestroy.c @@ -30,6 +30,10 @@ #include <nuttx/debug.h> #include <errno.h> +#include <nuttx/atomic.h> + +#include "pthread.h" + /**************************************************************************** * Public Functions ****************************************************************************/ @@ -60,13 +64,21 @@ int pthread_cond_destroy(FAR pthread_cond_t *cond) sinfo("cond=%p\n", cond); - if (!cond) + if (!PTHREAD_COND_IS_VALID(cond)) { ret = EINVAL; } - /* Destroy the semaphore contained in the structure */ + /* If the implementation detects an attempt to destroy the condition + * variable referenced by cond while it is referenced by another thread: + * a call to pthread_cond_destroy(cond) returns EBUSY without modifying + * the state of the condition variable. + */ + else if (atomic_read(COND_WAIT_COUNT(cond)) > 0) + { + ret = EBUSY; + } else { ret = nxsem_get_value(&cond->sem, &sval); @@ -81,6 +93,10 @@ int pthread_cond_destroy(FAR pthread_cond_t *cond) else { ret = -nxsem_destroy(&cond->sem); + if (ret == OK) + { + cond->clockid = 0; + } } } diff --git a/libs/libc/pthread/pthread_condinit.c b/libs/libc/pthread/pthread_condinit.c index 240bd07d80f..c13bf067441 100644 --- a/libs/libc/pthread/pthread_condinit.c +++ b/libs/libc/pthread/pthread_condinit.c @@ -31,6 +31,8 @@ #include <nuttx/debug.h> #include <errno.h> +#include "pthread.h" + /**************************************************************************** * Public Functions ****************************************************************************/ @@ -58,7 +60,16 @@ int pthread_cond_init(FAR pthread_cond_t *cond, sinfo("cond=%p attr=%p\n", cond, attr); - if (cond == NULL) + /* If the implementation detects that the attributes object + * specified by attr is invalid: + * a call to pthread_cond_init(cond, attr) returns EINVAL without + * modifying the state of the condition variable. + */ + + if (cond == NULL || + (attr && + attr->pshared != PTHREAD_PROCESS_PRIVATE && + attr->pshared != PTHREAD_PROCESS_SHARED)) { ret = EINVAL; } diff --git a/libs/libc/pthread/pthread_condsignal.c b/libs/libc/pthread/pthread_condsignal.c index b344c1e038e..39c07c977ac 100644 --- a/libs/libc/pthread/pthread_condsignal.c +++ b/libs/libc/pthread/pthread_condsignal.c @@ -60,7 +60,12 @@ int pthread_cond_signal(FAR pthread_cond_t *cond) sinfo("cond=%p\n", cond); - if (!cond) + /* If the implementation detects that the condition variable specified + * by cond does not refer to an initialized condition variable: + * a call to pthread_cond_signal(cond) returns EINVAL. + */ + + if (!PTHREAD_COND_IS_VALID(cond)) { ret = EINVAL; } diff --git a/libs/libc/pthread/pthread_condwait.c b/libs/libc/pthread/pthread_condwait.c index 840daa36ff1..638eb6a8727 100644 --- a/libs/libc/pthread/pthread_condwait.c +++ b/libs/libc/pthread/pthread_condwait.c @@ -71,7 +71,7 @@ int pthread_cond_wait(FAR pthread_cond_t *cond, FAR pthread_mutex_t *mutex) /* Make sure that non-NULL references were provided. */ - if (cond == NULL || mutex == NULL) + if (!PTHREAD_COND_IS_VALID(cond) || !PTHREAD_MUTEX_IS_VALID(mutex)) { ret = EINVAL; } diff --git a/libs/libc/pthread/pthread_mutex_destroy.c b/libs/libc/pthread/pthread_mutex_destroy.c index 6432bb6782b..dc6a91e79e2 100644 --- a/libs/libc/pthread/pthread_mutex_destroy.c +++ b/libs/libc/pthread/pthread_mutex_destroy.c @@ -64,7 +64,16 @@ int pthread_mutex_destroy(FAR pthread_mutex_t *mutex) int status; sinfo("mutex=%p\n", mutex); - DEBUGASSERT(mutex != NULL); + + /* If the implementation detects that the mutex referenced by the + * argument mutex is invalid: a call to pthread_mutex_destroy(mutex) + * returns EINVAL. + */ + + if (!PTHREAD_MUTEX_IS_VALID(mutex)) + { + return EINVAL; + } if (mutex != NULL) { diff --git a/libs/libc/pthread/pthread_mutex_init.c b/libs/libc/pthread/pthread_mutex_init.c index f985c1b53b5..280dc81d2cb 100644 --- a/libs/libc/pthread/pthread_mutex_init.c +++ b/libs/libc/pthread/pthread_mutex_init.c @@ -62,7 +62,16 @@ int pthread_mutex_init(FAR pthread_mutex_t *mutex, sinfo("mutex=%p attr=%p\n", mutex, attr); - if (!mutex) + /* If the implementation detects that the mutex attributes object + * specified by attr is invalid: + * a call to pthread_mutex_init(mutex, attr) returns EINVAL + * without modifying the state of the mutex. + */ + + if (!mutex || + (attr && + attr->pshared != PTHREAD_PROCESS_PRIVATE && + attr->pshared != PTHREAD_PROCESS_SHARED)) { return EINVAL; } diff --git a/libs/libc/pthread/pthread_mutex_setprioceiling.c b/libs/libc/pthread/pthread_mutex_setprioceiling.c index f98af46605b..565b7b73b8e 100644 --- a/libs/libc/pthread/pthread_mutex_setprioceiling.c +++ b/libs/libc/pthread/pthread_mutex_setprioceiling.c @@ -29,6 +29,8 @@ #include <pthread.h> +#include "pthread.h" + /**************************************************************************** * Public Functions ****************************************************************************/ @@ -59,6 +61,17 @@ int pthread_mutex_setprioceiling(FAR pthread_mutex_t *mutex, int ret = EINVAL; #ifdef CONFIG_PRIORITY_PROTECT + /* the implementation detects that the mutex argument does + * not refer to an initialized mutex: + * a call to pthread_mutex_setprioceiling(mutex, prioceiling, old_ceiling) + * returns EINVAL. + */ + + if (!PTHREAD_MUTEX_IS_VALID(mutex)) + { + return EINVAL; + } + ret = pthread_mutex_lock(mutex); if (ret != OK) { diff --git a/libs/libc/pthread/pthread_mutexattr_destroy.c b/libs/libc/pthread/pthread_mutexattr_destroy.c index e222d5b9280..a360db2d87e 100644 --- a/libs/libc/pthread/pthread_mutexattr_destroy.c +++ b/libs/libc/pthread/pthread_mutexattr_destroy.c @@ -30,6 +30,8 @@ #include <errno.h> #include <nuttx/debug.h> +#include "pthread.h" + /**************************************************************************** * Public Functions ****************************************************************************/ @@ -56,7 +58,12 @@ int pthread_mutexattr_destroy(FAR pthread_mutexattr_t *attr) linfo("attr=%p\n", attr); - if (!attr) + /* If the implementation detects that the value specified + * by attr is invalid: + * a call to pthread_mutexattr_destroy(attr) returns EINVAL. + */ + + if (!PTHREAD_MUTEXATTR_IS_VALID(attr)) { ret = EINVAL; } diff --git a/libs/libc/pthread/pthread_mutexattr_getprioceiling.c b/libs/libc/pthread/pthread_mutexattr_getprioceiling.c index a413a7876b6..19b1ba1f076 100644 --- a/libs/libc/pthread/pthread_mutexattr_getprioceiling.c +++ b/libs/libc/pthread/pthread_mutexattr_getprioceiling.c @@ -28,6 +28,8 @@ #include <pthread.h> #include <errno.h> +#include "pthread.h" + /**************************************************************************** * Public Functions ****************************************************************************/ @@ -54,7 +56,7 @@ int pthread_mutexattr_getprioceiling(FAR const pthread_mutexattr_t *attr, FAR int *prioceiling) { #ifdef CONFIG_PRIORITY_PROTECT - if (attr != NULL && prioceiling != NULL) + if (PTHREAD_MUTEXATTR_IS_VALID(attr) && prioceiling != NULL) { *prioceiling = attr->ceiling; return OK; diff --git a/libs/libc/pthread/pthread_mutexattr_getprotocol.c b/libs/libc/pthread/pthread_mutexattr_getprotocol.c index 2a8d8727e48..9277d652f1b 100644 --- a/libs/libc/pthread/pthread_mutexattr_getprotocol.c +++ b/libs/libc/pthread/pthread_mutexattr_getprotocol.c @@ -30,6 +30,8 @@ #include <assert.h> #include <nuttx/debug.h> +#include "pthread.h" + /**************************************************************************** * Public Functions ****************************************************************************/ @@ -53,7 +55,10 @@ int pthread_mutexattr_getprotocol(FAR const pthread_mutexattr_t *attr, FAR int *protocol) { - DEBUGASSERT(attr != NULL && protocol != NULL); + if (!PTHREAD_MUTEXATTR_IS_VALID(attr) || protocol == NULL) + { + return EINVAL; + } #if defined(CONFIG_PRIORITY_INHERITANCE) || defined(CONFIG_PRIORITY_PROTECT) linfo("Returning %d\n", attr->prio); diff --git a/libs/libc/pthread/pthread_mutexattr_getpshared.c b/libs/libc/pthread/pthread_mutexattr_getpshared.c index a6497993adc..477adab9e46 100644 --- a/libs/libc/pthread/pthread_mutexattr_getpshared.c +++ b/libs/libc/pthread/pthread_mutexattr_getpshared.c @@ -30,6 +30,8 @@ #include <errno.h> #include <nuttx/debug.h> +#include "pthread.h" + /**************************************************************************** * Public Functions ****************************************************************************/ @@ -58,7 +60,7 @@ int pthread_mutexattr_getpshared(FAR const pthread_mutexattr_t *attr, linfo("attr=%p pshared=%p\n", attr, pshared); - if (!attr || !pshared) + if (!PTHREAD_MUTEXATTR_IS_VALID(attr) || !pshared) { ret = EINVAL; } diff --git a/libs/libc/pthread/pthread_mutexattr_gettype.c b/libs/libc/pthread/pthread_mutexattr_gettype.c index 469eb961185..477f16f9d72 100644 --- a/libs/libc/pthread/pthread_mutexattr_gettype.c +++ b/libs/libc/pthread/pthread_mutexattr_gettype.c @@ -28,6 +28,8 @@ #include <pthread.h> #include <errno.h> +#include "pthread.h" + /**************************************************************************** * Public Functions ****************************************************************************/ @@ -53,7 +55,7 @@ int pthread_mutexattr_gettype(FAR const pthread_mutexattr_t *attr, FAR int *type) { - if (attr != NULL && type != NULL) + if (PTHREAD_MUTEXATTR_IS_VALID(attr) && type != NULL) { #ifdef CONFIG_PTHREAD_MUTEX_TYPES *type = attr->type; diff --git a/libs/libc/pthread/pthread_mutexattr_init.c b/libs/libc/pthread/pthread_mutexattr_init.c index 3ad9271509f..9c68fca9c5a 100644 --- a/libs/libc/pthread/pthread_mutexattr_init.c +++ b/libs/libc/pthread/pthread_mutexattr_init.c @@ -61,7 +61,7 @@ int pthread_mutexattr_init(FAR pthread_mutexattr_t *attr) } else { - attr->pshared = 0; + attr->pshared = PTHREAD_PROCESS_PRIVATE; #ifdef CONFIG_PRIORITY_PROTECT attr->prio = PTHREAD_PRIO_NONE; diff --git a/libs/libc/pthread/pthread_mutexattr_setprioceiling.c b/libs/libc/pthread/pthread_mutexattr_setprioceiling.c index 96c9ee3dfa1..079e74a7630 100644 --- a/libs/libc/pthread/pthread_mutexattr_setprioceiling.c +++ b/libs/libc/pthread/pthread_mutexattr_setprioceiling.c @@ -29,6 +29,8 @@ #include <errno.h> #include <sched.h> +#include "pthread.h" + /**************************************************************************** * Public Functions ****************************************************************************/ @@ -55,7 +57,8 @@ int pthread_mutexattr_setprioceiling(FAR pthread_mutexattr_t *attr, int prioceiling) { #ifdef CONFIG_PRIORITY_PROTECT - if (attr && prioceiling >= sched_get_priority_min(SCHED_FIFO) && + if (PTHREAD_MUTEXATTR_IS_VALID(attr) && + prioceiling >= sched_get_priority_min(SCHED_FIFO) && prioceiling <= sched_get_priority_max(SCHED_FIFO)) { attr->ceiling = prioceiling; diff --git a/libs/libc/pthread/pthread_mutexattr_setprotocol.c b/libs/libc/pthread/pthread_mutexattr_setprotocol.c index 3e8d2dff465..cd8caede4e9 100644 --- a/libs/libc/pthread/pthread_mutexattr_setprotocol.c +++ b/libs/libc/pthread/pthread_mutexattr_setprotocol.c @@ -31,6 +31,8 @@ #include <errno.h> #include <nuttx/debug.h> +#include "pthread.h" + /**************************************************************************** * Public Functions ****************************************************************************/ @@ -54,7 +56,10 @@ int pthread_mutexattr_setprotocol(FAR pthread_mutexattr_t *attr, int protocol) { linfo("attr=%p protocol=%d\n", attr, protocol); - DEBUGASSERT(attr != NULL); + if (!PTHREAD_MUTEXATTR_IS_VALID(attr)) + { + return EINVAL; + } switch (protocol) { @@ -81,7 +86,7 @@ int pthread_mutexattr_setprotocol(FAR pthread_mutexattr_t *attr, #endif /* CONFIG_PRIORITY_PROTECT */ default: - return EINVAL; + return ENOTSUP; } return OK; diff --git a/libs/libc/pthread/pthread_mutexattr_setpshared.c b/libs/libc/pthread/pthread_mutexattr_setpshared.c index 14b397aaaff..e29228903cc 100644 --- a/libs/libc/pthread/pthread_mutexattr_setpshared.c +++ b/libs/libc/pthread/pthread_mutexattr_setpshared.c @@ -30,6 +30,8 @@ #include <errno.h> #include <nuttx/debug.h> +#include "pthread.h" + /**************************************************************************** * Public Functions ****************************************************************************/ @@ -57,7 +59,9 @@ int pthread_mutexattr_setpshared(FAR pthread_mutexattr_t *attr, int pshared) linfo("attr=%p pshared=%d\n", attr, pshared); - if (!attr || (pshared != 0 && pshared != 1)) + if (!PTHREAD_MUTEXATTR_IS_VALID(attr) || + (pshared != PTHREAD_PROCESS_PRIVATE && + pshared != PTHREAD_PROCESS_SHARED)) { ret = EINVAL; } diff --git a/libs/libc/pthread/pthread_mutexattr_settype.c b/libs/libc/pthread/pthread_mutexattr_settype.c index 14cef9f263c..bd5dcaacc1d 100644 --- a/libs/libc/pthread/pthread_mutexattr_settype.c +++ b/libs/libc/pthread/pthread_mutexattr_settype.c @@ -28,6 +28,8 @@ #include <pthread.h> #include <errno.h> +#include "pthread.h" + /**************************************************************************** * Public Functions ****************************************************************************/ @@ -52,7 +54,7 @@ int pthread_mutexattr_settype(FAR pthread_mutexattr_t *attr, int type) { - if (attr && type >= PTHREAD_MUTEX_NORMAL && + if (PTHREAD_MUTEXATTR_IS_VALID(attr) && type >= PTHREAD_MUTEX_NORMAL && type <= PTHREAD_MUTEX_RECURSIVE) { #ifdef CONFIG_PTHREAD_MUTEX_TYPES diff --git a/libs/libc/semaphore/sem_init.c b/libs/libc/semaphore/sem_init.c index 544ccde87c6..fd07eb613e4 100644 --- a/libs/libc/semaphore/sem_init.c +++ b/libs/libc/semaphore/sem_init.c @@ -81,7 +81,7 @@ int nxsem_init(FAR sem_t *sem, int pshared, int32_t value) /* Initialize to support priority inheritance */ - sem->flags = 0; + sem->flags = SEM_PRIO_NONE; #ifdef CONFIG_PRIORITY_INHERITANCE # if CONFIG_SEM_PREALLOCHOLDERS > 0 diff --git a/sched/semaphore/sem_destroy.c b/sched/semaphore/sem_destroy.c index ad16acf4545..4bafe8ccdb5 100644 --- a/sched/semaphore/sem_destroy.c +++ b/sched/semaphore/sem_destroy.c @@ -90,5 +90,6 @@ int nxsem_destroy(FAR sem_t *sem) /* Release holders of the semaphore */ nxsem_destroyholder(sem); + sem->flags = 0; return OK; } diff --git a/sched/semaphore/sem_wait.c b/sched/semaphore/sem_wait.c index 6f9bc6949bf..4896ba0ebaf 100644 --- a/sched/semaphore/sem_wait.c +++ b/sched/semaphore/sem_wait.c @@ -309,7 +309,7 @@ int nxsem_wait_slow(FAR sem_t *sem) #endif #ifdef CONFIG_PRIORITY_INHERITANCE - if (prioinherit != 0) + if (prioinherit != SEM_PRIO_NONE) { sched_unlock(); }
