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
The following commit(s) were added to refs/heads/master by this push:
new 44ec4c6b9d8 libc/pthread: Accept any pshared value in
pthread_spin_init().
44ec4c6b9d8 is described below
commit 44ec4c6b9d8662bad72e8a42a4d50c1af14eebd3
Author: Marco Casaroli <[email protected]>
AuthorDate: Sun Nov 22 16:28:13 2026 +0100
libc/pthread: Accept any pshared value in pthread_spin_init().
PTHREAD_PROCESS_PRIVATE is now 1 and PTHREAD_PROCESS_SHARED is 2.
Since then, pthread_spin_init() returns EINVAL for pshared 0. Many
programs written for Linux give a literal 0 as the private value, for
example pthread_spin_init(&lock, 0). In glibc, PTHREAD_PROCESS_PRIVATE
is 0, and pthread_spin_init() does not check pshared at all.
The spin lock is an atomic variable. It works the same in private and
in shared memory, so pshared does not change the lock. Do as glibc
does: do not check pshared, and give a private lock for every value
other than PTHREAD_PROCESS_SHARED. pthread_spin_init() still returns
EINVAL when lock is NULL.
The _setpshared() functions of the attribute objects do not change.
pthread_mutexattr_setpshared(), pthread_condattr_setpshared(),
pthread_rwlockattr_setpshared() and pthread_barrierattr_setpshared()
still return EINVAL for a value that is not PTHREAD_PROCESS_PRIVATE or
PTHREAD_PROCESS_SHARED. glibc also returns EINVAL there. The two
behaviours are different, but they are the same as in glibc.
Assisted-by: Claude Code:claude-opus-5-5
Signed-off-by: Marco Casaroli <[email protected]>
---
libs/libc/pthread/pthread_spinlock.c | 13 +++++++++----
1 file changed, 9 insertions(+), 4 deletions(-)
diff --git a/libs/libc/pthread/pthread_spinlock.c
b/libs/libc/pthread/pthread_spinlock.c
index 63d8b911c7f..c895c020d3c 100644
--- a/libs/libc/pthread/pthread_spinlock.c
+++ b/libs/libc/pthread/pthread_spinlock.c
@@ -60,19 +60,24 @@
* variable, so it also works in memory that processes share
* (PTHREAD_PROCESS_SHARED).
*
+ * pshared does not change the lock, so the function does not check it.
+ * All values other than PTHREAD_PROCESS_SHARED give a private lock, as
+ * in glibc. Programs written for Linux often give 0 as the private
+ * value, but PTHREAD_PROCESS_PRIVATE is not 0 here.
+ *
* Input Parameters:
* lock - A reference to the spinlock object to be initialized.
- * pshared - PTHREAD_PROCESS_PRIVATE or PTHREAD_PROCESS_SHARED
+ * pshared - PTHREAD_PROCESS_SHARED, or any other value for a lock that
+ * only the threads of one process use.
*
* Returned Value:
- * Zero (OK) on success, EINVAL if an argument is not valid.
+ * Zero (OK) on success, EINVAL if lock is NULL.
*
****************************************************************************/
int pthread_spin_init(FAR pthread_spinlock_t *lock, int pshared)
{
- if (lock == NULL || (pshared != PTHREAD_PROCESS_PRIVATE &&
- pshared != PTHREAD_PROCESS_SHARED))
+ if (lock == NULL)
{
return EINVAL;
}