From 8bf693b36256439e91b4996c449c937ab508fe6f Mon Sep 17 00:00:00 2001 From: Xiang Xiao <xiaoxiang@xiaomi.com> Date: Fri, 20 Jan 2023 22:41:20 +0800 Subject: [PATCH] libc/pthread: Implement pthread_condattr_[set|get]pshared https://pubs.opengroup.org/onlinepubs/009696799/functions/pthread_condattr_getpshared.html Signed-off-by: Xiang Xiao <xiaoxiang@xiaomi.com> --- Documentation/reference/user/08_pthread.rst | 4 - include/pthread.h | 4 + libs/libc/pthread/Make.defs | 1 + .../pthread/pthread_condattr_getpshared.c | 68 +++++++++++++ .../pthread/pthread_condattr_setpshared.c | 99 +++++++++++++++++++ 5 files changed, 172 insertions(+), 4 deletions(-) create mode 100644 libs/libc/pthread/pthread_condattr_getpshared.c create mode 100644 libs/libc/pthread/pthread_condattr_setpshared.c diff --git a/Documentation/reference/user/08_pthread.rst b/Documentation/reference/user/08_pthread.rst index ea406e5b38..77017cef04 100644 --- a/Documentation/reference/user/08_pthread.rst +++ b/Documentation/reference/user/08_pthread.rst @@ -115,10 +115,6 @@ No support for the following pthread interfaces is provided by NuttX: - ``pthread_attr_setguardsize``. get and set the thread guardsize attribute. - ``pthread_attr_setscope``. get and set the contentionscope attribute. - - ``pthread_condattr_getpshared``. get the process-shared condition - variable attribute. - - ``pthread_condattr_setpshared``. set the process-shared condition - variable attribute. - ``pthread_getconcurrency``. get and set the level of concurrency. - ``pthread_getcpuclockid``. access a thread CPU-time clock. - ``pthread_mutex_getprioceiling``. get and set the priority ceiling of diff --git a/include/pthread.h b/include/pthread.h index 89db797e8b..f03f007fe9 100644 --- a/include/pthread.h +++ b/include/pthread.h @@ -248,6 +248,7 @@ typedef pid_t pthread_t; struct pthread_condattr_s { + int pshared; clockid_t clockid; }; @@ -615,6 +616,9 @@ int pthread_mutex_consistent(FAR pthread_mutex_t *mutex); int pthread_condattr_init(FAR pthread_condattr_t *attr); int pthread_condattr_destroy(FAR pthread_condattr_t *attr); +int pthread_condattr_getpshared(FAR const pthread_condattr_t *attr, + FAR int *pshared); +int pthread_condattr_setpshared(FAR pthread_condattr_t *attr, int pshared); int pthread_condattr_getclock(FAR const pthread_condattr_t *attr, clockid_t *clock_id); int pthread_condattr_setclock(FAR pthread_condattr_t *attr, diff --git a/libs/libc/pthread/Make.defs b/libs/libc/pthread/Make.defs index dd590e1643..fe9849c3c1 100644 --- a/libs/libc/pthread/Make.defs +++ b/libs/libc/pthread/Make.defs @@ -39,6 +39,7 @@ 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 CSRCS += pthread_condattr_init.c pthread_condattr_destroy.c +CSRCS += pthread_condattr_getpshared.c pthread_condattr_setpshared.c CSRCS += pthread_condattr_setclock.c pthread_condattr_getclock.c CSRCS += pthread_condinit.c pthread_conddestroy.c pthread_condtimedwait.c CSRCS += pthread_create.c pthread_exit.c diff --git a/libs/libc/pthread/pthread_condattr_getpshared.c b/libs/libc/pthread/pthread_condattr_getpshared.c new file mode 100644 index 0000000000..5667a19ca4 --- /dev/null +++ b/libs/libc/pthread/pthread_condattr_getpshared.c @@ -0,0 +1,68 @@ +/******************************************************************************** + * libs/libc/pthread/pthread_condattr_getpshared.c + * + * 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 <nuttx/config.h> + +#include <pthread.h> +#include <errno.h> +#include <debug.h> + +/******************************************************************************** + * Public Functions + ********************************************************************************/ + +/******************************************************************************** + * Name: pthread_condattr_getpshared + * + * Description: + * The pthread_condattr_getpshared() function will obtain the value of the + * process-shared attribute from the attributes object referenced by attr. + * + * Input Parameters: + * attr - cond attributes to be queried. + * pshared - the location to stored the current value of the pshared attribute. + * + * Returned Value: + * 0 (OK) on success or EINVAL if either attr or pshared is invalid. + * + * Assumptions: + * + ********************************************************************************/ + +int pthread_condattr_getpshared(FAR const pthread_condattr_t *attr, + FAR int *pshared) +{ + int ret = OK; + + if (!attr || !pshared) + { + ret = EINVAL; + } + else + { + *pshared = attr->pshared; + } + + return ret; +} diff --git a/libs/libc/pthread/pthread_condattr_setpshared.c b/libs/libc/pthread/pthread_condattr_setpshared.c new file mode 100644 index 0000000000..a5a17cadbe --- /dev/null +++ b/libs/libc/pthread/pthread_condattr_setpshared.c @@ -0,0 +1,99 @@ +/******************************************************************************** + * libs/libc/pthread/pthread_condattr_setpshared.c + * + * 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 <nuttx/config.h> + +#include <pthread.h> +#include <errno.h> +#include <debug.h> + +/******************************************************************************** + * Pre-processor Definitions + ********************************************************************************/ + +/******************************************************************************** + * Private Type Declarations + ********************************************************************************/ + +/******************************************************************************** + * Public Data + ********************************************************************************/ + +/******************************************************************************** + * Private Data + ********************************************************************************/ + +/******************************************************************************** + * Private Function Prototypes + ********************************************************************************/ + +/******************************************************************************** + * Public Functions + ********************************************************************************/ + +/******************************************************************************** + * Name: pthread_condattr_setpshared + * + * Description: + * The process-shared attribute is set to PTHREAD_PROCESS_SHARED to permit + * a cond to be operated upon by any thread that has access to the + * memory where the cond is allocated. If the process-shared attribute + * is PTHREAD_PROCESS_PRIVATE, the cond can only be operated upon by + * threads created within the same process as the thread that initialized + * the cond. + * If threads of different processes attempt to operate on such a cond, + * the behavior is undefined. The default value of the attribute is + * PTHREAD_PROCESS_PRIVATE. + * + * Both constants PTHREAD_PROCESS_SHARED and PTHREAD_PROCESS_PRIVATE are + * defined in pthread.h. + * + * Input Parameters: + * attr - cond attributes to be modified. + * pshared - the new value of the pshared attribute. + * + * Returned Value: + * 0 (OK) on success or EINVAL if either attr is invalid or pshared is not + * one of PTHREAD_PROCESS_SHARED or PTHREAD_PROCESS_PRIVATE. + * + * Assumptions: + * + ********************************************************************************/ + +int pthread_condattr_setpshared(FAR pthread_condattr_t *attr, int pshared) +{ + int ret = OK; + + if (!attr || (pshared != PTHREAD_PROCESS_SHARED && + pshared != PTHREAD_PROCESS_PRIVATE)) + { + ret = EINVAL; + } + else + { + attr->pshared = pshared; + } + + return ret; +}