From 6e954ef85d745b3b215d88e92e2efc7bb304dd1a Mon Sep 17 00:00:00 2001 From: EunBong Song Date: Fri, 14 Apr 2017 10:31:46 +0900 Subject: [PATCH] pthread: apply some missing patches from Nuttx This patch adds pthread_mutexattr_getrobust.c and pthread_mutexattr_setrobust.c and initialization of attr->robust in pthread_mutexattr_init(). All credits should go to Gregory Nutt who wrote the original commit. Change-Id: I02b979e0bd4e30d6e91eaf91941e3bcd6ac7e7c4 Signed-off-by: Gregory Nutt [Song: backported 666208cf, d1196ddb and b4d378ad from NuttX] Signed-off-by: EunBong Song --- lib/libc/pthread/Make.defs | 8 +- lib/libc/pthread/pthread_mutexattr_getrobust.c | 97 +++++++++++++++++++++++ lib/libc/pthread/pthread_mutexattr_setrobust.c | 105 +++++++++++++++++++++++++ lib/libc/pthread/pthread_mutexattrinit.c | 8 ++ os/include/pthread.h | 21 +++-- os/kernel/Kconfig | 6 +- os/kernel/pthread/pthread_mutexinit.c | 1 - 7 files changed, 232 insertions(+), 14 deletions(-) create mode 100644 lib/libc/pthread/pthread_mutexattr_getrobust.c create mode 100644 lib/libc/pthread/pthread_mutexattr_setrobust.c diff --git a/lib/libc/pthread/Make.defs b/lib/libc/pthread/Make.defs index 676cbf2..423ac9d 100644 --- a/lib/libc/pthread/Make.defs +++ b/lib/libc/pthread/Make.defs @@ -50,6 +50,8 @@ # ############################################################################ +ifneq ($(CONFIG_DISABLE_PTHREAD),y) + # Add the pthread C files to the build CSRCS += pthread_attrinit.c pthread_attrdestroy.c @@ -64,6 +66,7 @@ CSRCS += pthread_mutexattrinit.c pthread_mutexattrdestroy.c CSRCS += pthread_mutexattrgetpshared.c pthread_mutexattrsetpshared.c CSRCS += pthread_mutexattr_setprotocol.c pthread_mutexattr_getprotocol.c CSRCS += pthread_mutexattrsettype.c pthread_mutexattrgettype.c +CSRCS += pthread_mutexattr_setrobust.c pthread_mutexattr_getrobust.c CSRCS += pthread_setcancelstate.c pthread_setcanceltype.c CSRCS += pthread_testcancel.c @@ -71,9 +74,6 @@ ifeq ($(CONFIG_ENABLE_IOTIVITY),y) CSRCS += pthread_condattrsetclock.c endif -ifeq ($(CONFIG_PTHREAD_MUTEX_TYPES),y) -endif - ifeq ($(CONFIG_BUILD_PROTECTED),y) CSRCS += pthread_startup.c endif @@ -82,3 +82,5 @@ endif DEPPATH += --dep-path pthread VPATH += :pthread + +endif diff --git a/lib/libc/pthread/pthread_mutexattr_getrobust.c b/lib/libc/pthread/pthread_mutexattr_getrobust.c new file mode 100644 index 0000000..acd597c --- /dev/null +++ b/lib/libc/pthread/pthread_mutexattr_getrobust.c @@ -0,0 +1,97 @@ +/**************************************************************************** + * + * Copyright 2017 Samsung Electronics All Rights Reserved. + * + * Licensed 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. + * + ****************************************************************************/ +/**************************************************************************** + * lib/libc/pthread/pthread_mutexattr_getrobust.c + * + * Copyright (C) 2007-2009, 2011 Gregory Nutt. All rights reserved. + * Author: Gregory Nutt + * + * Redistribution and use in source and binary forms, with or without + * modification, are permitted provided that the following conditions + * are met: + * + * 1. Redistributions of source code must retain the above copyright + * notice, this list of conditions and the following disclaimer. + * 2. Redistributions in binary form must reproduce the above copyright + * notice, this list of conditions and the following disclaimer in + * the documentation and/or other materials provided with the + * distribution. + * 3. Neither the name NuttX nor the names of its contributors may be + * used to endorse or promote products derived from this software + * without specific prior written permission. + * + * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS + * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT + * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS + * FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE + * COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, + * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, + * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS + * OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED + * AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT + * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN + * ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE + * POSSIBILITY OF SUCH DAMAGE. + * + ****************************************************************************/ + +/**************************************************************************** + * Included Files + ****************************************************************************/ + +#include +#include +#include + +/**************************************************************************** + * Public Functions + ****************************************************************************/ + +/**************************************************************************** + * Function: pthread_mutexattr_getrobust + * + * Description: + * Return the mutex robustneess from the mutex attributes. + * + * Parameters: + * attr - The mutex attributes to query + * robust - Location to return the robustness indication + * + * Return Value: + * 0, if the robustness was successfully return in 'robust', or + * EINVAL, if any NULL pointers provided. + * + * Assumptions: + * + ****************************************************************************/ +int pthread_mutexattr_getrobust(FAR const pthread_mutexattr_t *attr, + FAR int *robust) +{ + if (attr != NULL && robust != NULL) { +#if defined(CONFIG_PTHREAD_MUTEX_UNSAFE) + *robust = PTHREAD_MUTEX_STALLED; +#elif defined(CONFIG_PTHREAD_MUTEX_BOTH) + *robust = attr->robust; +#else /* Default: CONFIG_PTHREAD_MUTEX_ROBUST */ + *robust = PTHREAD_MUTEX_ROBUST; +#endif + return 0; + } + + return EINVAL; +} diff --git a/lib/libc/pthread/pthread_mutexattr_setrobust.c b/lib/libc/pthread/pthread_mutexattr_setrobust.c new file mode 100644 index 0000000..b57a08b --- /dev/null +++ b/lib/libc/pthread/pthread_mutexattr_setrobust.c @@ -0,0 +1,105 @@ +/**************************************************************************** + * + * Copyright 2017 Samsung Electronics All Rights Reserved. + * + * Licensed 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. + * + ****************************************************************************/ +/**************************************************************************** + * lib/libc/pthread/pthread_mutexattr_setrobust.c + * + * Copyright (C) 2007-2009, 2011 Gregory Nutt. All rights reserved. + * Author: Gregory Nutt + * + * Redistribution and use in source and binary forms, with or without + * modification, are permitted provided that the following conditions + * are met: + * + * 1. Redistributions of source code must retain the above copyright + * notice, this list of conditions and the following disclaimer. + * 2. Redistributions in binary form must reproduce the above copyright + * notice, this list of conditions and the following disclaimer in + * the documentation and/or other materials provided with the + * distribution. + * 3. Neither the name NuttX nor the names of its contributors may be + * used to endorse or promote products derived from this software + * without specific prior written permission. + * + * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS + * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT + * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS + * FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE + * COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, + * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, + * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS + * OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED + * AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT + * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN + * ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE + * POSSIBILITY OF SUCH DAMAGE. + * + ****************************************************************************/ + +/**************************************************************************** + * Included Files + ****************************************************************************/ + +#include +#include +#include + +/**************************************************************************** + * Public Functions + ****************************************************************************/ + +/**************************************************************************** + * Function: pthread_mutexattr_setrobust + * + * Description: + * Set the mutex robustness in the mutex attributes. + * + * Parameters: + * attr - The mutex attributes in which to set the mutex type. + * robust - The mutex type value to set. + * + * Return Value: + * 0, if the mutex robustness was successfully set in 'attr', or + * EINVAL, if 'attr' is NULL or 'robust' unrecognized. + * + * Assumptions: + * + ****************************************************************************/ +int pthread_mutexattr_setrobust(pthread_mutexattr_t *attr, int robust) +{ +#if defined(CONFIG_PTHREAD_MUTEX_UNSAFE) + if (attr != NULL && robust == PTHREAD_MUTEX_STALLED) { + return OK; + } + + return EINVAL; +#elif defined(CONFIG_PTHREAD_MUTEX_BOTH) + if (attr != NULL && (robust == PTHREAD_MUTEX_STALLED || + robust == _PTHREAD_MFLAGS_ROBUST)) { + attr->robust = robust; + return OK; + } + + return EINVAL; +#else /* Default: CONFIG_PTHREAD_MUTEX_ROBUST */ + if (attr != NULL && robust == _PTHREAD_MFLAGS_ROBUST) { + return OK; + } + + return EINVAL; +#endif +} diff --git a/lib/libc/pthread/pthread_mutexattrinit.c b/lib/libc/pthread/pthread_mutexattrinit.c index 48ed2ab..472976b 100644 --- a/lib/libc/pthread/pthread_mutexattrinit.c +++ b/lib/libc/pthread/pthread_mutexattrinit.c @@ -118,6 +118,14 @@ int pthread_mutexattr_init(FAR pthread_mutexattr_t *attr) #ifdef CONFIG_PTHREAD_MUTEX_TYPES attr->type = PTHREAD_MUTEX_DEFAULT; #endif + +#ifdef CONFIG_PTHREAD_MUTEX_BOTH +#ifdef CONFIG_PTHREAD_MUTEX_DEFAULT_UNSAFE + attr->robust = PTHREAD_MUTEX_STALLED; +#else + attr->robust = PTHREAD_MUTEX_ROBUST; +#endif +#endif } sdbg("Returning %d\n", ret); diff --git a/os/include/pthread.h b/os/include/pthread.h index d3f9b0c..5cf1f95 100644 --- a/os/include/pthread.h +++ b/os/include/pthread.h @@ -166,7 +166,8 @@ #define PTHREAD_PRIO_INHERIT SEM_PRIO_INHERIT #define PTHREAD_PRIO_PROTECT SEM_PRIO_PROTECT -/* Values for robust argument of pthread_mutexattr_get/setrobust +/* + * Values for robust argument of pthread_mutexattr_get/setrobust * * PTHREAD_MUTEX_STALLED - No special actions are taken if the owner of the * mutex is terminated while holding the mutex lock. This can lead to @@ -191,14 +192,13 @@ * ENOTRECOVERABLE. The only permissible operation on such a mutex is * pthread_mutex_destroy(). */ - #define PTHREAD_MUTEX_STALLED 0 #define PTHREAD_MUTEX_ROBUST 1 -/* Values for struct pthread_mutex_s flags. These are non-standard and +/* + * Values for struct pthread_mutex_s flags. These are non-standard and * intended only for internal use within the OS. */ - #define _PTHREAD_MFLAGS_ROBUST (1 << 0) /* Robust (NORMAL) mutex */ #define _PTHREAD_MFLAGS_INCONSISTENT (1 << 1) /* Mutex is in an inconsistent state */ #define _PTHREAD_MFLAGS_NRECOVERABLE (1 << 2) /* Inconsistent mutex has been unlocked */ @@ -288,12 +288,15 @@ typedef struct pthread_cond_s pthread_cond_t; * @brief Structure of pthread mutex attr configuration */ struct pthread_mutexattr_s { - uint8_t pshared; /* PTHREAD_PROCESS_PRIVATE or PTHREAD_PROCESS_SHARED */ + uint8_t pshared : 1; /* PTHREAD_PROCESS_PRIVATE or PTHREAD_PROCESS_SHARED */ #ifdef CONFIG_PRIORITY_INHERITANCE - uint8_t proto; /* See PTHREAD_PRIO_* definitions */ + uint8_t proto : 2; /* See PTHREAD_PRIO_* definitions */ #endif #ifdef CONFIG_PTHREAD_MUTEX_TYPES - uint8_t type; /* Type of the mutex. See PTHREAD_MUTEX_* definitions */ + uint8_t type : 2; /* Type of the mutex. See PTHREAD_MUTEX_* definitions */ +#endif +#ifdef CONFIG_PTHREAD_MUTEX_BOTH + uint8_t robust : 1; /* PTHREAD_MUTEX_STALLED or PTHREAD_MUTEX_ROBUST */ #endif }; typedef struct pthread_mutexattr_s pthread_mutexattr_t; @@ -786,6 +789,10 @@ int pthread_mutexattr_getprotocol(FAR const pthread_mutexattr_t *attr, FAR int *protocol); int pthread_mutexattr_setprotocol(FAR pthread_mutexattr_t *attr, int protocol); +int pthread_mutexattr_getrobust(FAR const pthread_mutexattr_t *attr, + FAR int *robust); +int pthread_mutexattr_setrobust(FAR pthread_mutexattr_t *attr, + int robust); /* Operations on condition variables */ /** diff --git a/os/kernel/Kconfig b/os/kernel/Kconfig index aed7d5a..df4c49d 100644 --- a/os/kernel/Kconfig +++ b/os/kernel/Kconfig @@ -441,7 +441,7 @@ config PTHREAD_MUTEX_UNSAFE software you may be porting or, perhaps, if you are trying to minimize footprint. -config PTHREAD_MUTEX_BOTH + config PTHREAD_MUTEX_BOTH bool "Both robust and unsafe mutexes" ---help--- Support both forms of NORMAL mutexes. @@ -453,12 +453,12 @@ choice default PTHREAD_MUTEX_DEFAULT_ROBUST depends on PTHREAD_MUTEX_BOTH -config PTHREAD_MUTEX_DEFAULT_ROBUST + config PTHREAD_MUTEX_DEFAULT_ROBUST bool "Robust default" ---help--- The default is robust NORMAL mutexes (non-standard) -config PTHREAD_MUTEX_DEFAULT_UNSAFE + config PTHREAD_MUTEX_DEFAULT_UNSAFE bool "Unsafe default" ---help--- The default is traditional unsafe NORMAL mutexes (standard) diff --git a/os/kernel/pthread/pthread_mutexinit.c b/os/kernel/pthread/pthread_mutexinit.c index 508f478..71df04e 100644 --- a/os/kernel/pthread/pthread_mutexinit.c +++ b/os/kernel/pthread/pthread_mutexinit.c @@ -167,7 +167,6 @@ int pthread_mutex_init(FAR pthread_mutex_t *mutex, #ifndef CONFIG_PTHREAD_MUTEX_UNSAFE /* Initial internal fields of the mutex */ - mutex->flink = NULL; mutex->flags = (robust == PTHREAD_MUTEX_ROBUST ? _PTHREAD_MFLAGS_ROBUST : 0); #endif -- 2.7.4