From 045d04d838ba4fdc8a7d2ea448525ac345351b5d Mon Sep 17 00:00:00 2001 From: EunBong Song Date: Fri, 14 Apr 2017 08:50:39 +0900 Subject: [PATCH] pthread: disable priority inheritance for contained semaphore Provide do-nothing stubs for mutex attribute interfaces if features not enabled. pthread_cond includes a signaling semaphore and should call sem_setprotocol. All credits should go to Gregory Nutt who wrote the original commit. Change-Id: Ib6bcc4d63e8decd6f60e1bc944aecca88037fdc7 Signed-off-by: Gregory Nutt [Song: backported 796969f6 from NuttX] Signed-off-by: EunBong Song --- lib/libc/pthread/Make.defs | 31 +++++++++++------------- lib/libc/pthread/pthread_mutexattr_getprotocol.c | 5 ++++ lib/libc/pthread/pthread_mutexattr_setprotocol.c | 8 ++++++ lib/libc/pthread/pthread_mutexattrgettype.c | 10 ++++---- lib/libc/pthread/pthread_mutexattrsettype.c | 12 ++++++--- os/include/pthread.h | 15 ++++-------- os/kernel/pthread/pthread_condinit.c | 11 +++++++-- 7 files changed, 54 insertions(+), 38 deletions(-) diff --git a/lib/libc/pthread/Make.defs b/lib/libc/pthread/Make.defs index 2c03e99..676cbf2 100644 --- a/lib/libc/pthread/Make.defs +++ b/lib/libc/pthread/Make.defs @@ -52,29 +52,26 @@ # Add the pthread C files to the build -CSRCS += pthread_attrinit.c pthread_attrdestroy.c \ - pthread_attrsetschedpolicy.c pthread_attrgetschedpolicy.c \ - pthread_attrsetinheritsched.c pthread_attrgetinheritsched.c \ - pthread_attrsetstacksize.c pthread_attrgetstacksize.c \ - pthread_attrsetschedparam.c pthread_attrgetschedparam.c \ - pthread_barrierattrinit.c pthread_barrierattrdestroy.c \ - pthread_barrierattrgetpshared.c pthread_barrierattrsetpshared.c \ - pthread_condattrinit.c pthread_condattrdestroy.c \ - pthread_mutexattrinit.c pthread_mutexattrdestroy.c \ - pthread_mutexattrgetpshared.c pthread_mutexattrsetpshared.c \ - pthread_setcancelstate.c pthread_setcanceltype.c \ - pthread_testcancel.c +CSRCS += pthread_attrinit.c pthread_attrdestroy.c +CSRCS += pthread_attrsetschedpolicy.c pthread_attrgetschedpolicy.c +CSRCS += pthread_attrsetinheritsched.c pthread_attrgetinheritsched.c +CSRCS += pthread_attrsetstacksize.c pthread_attrgetstacksize.c +CSRCS += pthread_attrsetschedparam.c pthread_attrgetschedparam.c +CSRCS += pthread_barrierattrinit.c pthread_barrierattrdestroy.c +CSRCS += pthread_barrierattrgetpshared.c pthread_barrierattrsetpshared.c +CSRCS += pthread_condattrinit.c pthread_condattrdestroy.c +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_setcancelstate.c pthread_setcanceltype.c +CSRCS += pthread_testcancel.c ifeq ($(CONFIG_ENABLE_IOTIVITY),y) CSRCS += pthread_condattrsetclock.c endif ifeq ($(CONFIG_PTHREAD_MUTEX_TYPES),y) -CSRCS += pthread_mutexattrsettype.c pthread_mutexattrgettype.c -endif - -ifeq ($(CONFIG_PRIORITY_INHERITANCE),y) -CSRCS += pthread_mutexattr_setprotocol.c pthread_mutexattr_getprotocol.c endif ifeq ($(CONFIG_BUILD_PROTECTED),y) diff --git a/lib/libc/pthread/pthread_mutexattr_getprotocol.c b/lib/libc/pthread/pthread_mutexattr_getprotocol.c index ca0eba8..ac3bad4 100644 --- a/lib/libc/pthread/pthread_mutexattr_getprotocol.c +++ b/lib/libc/pthread/pthread_mutexattr_getprotocol.c @@ -85,6 +85,11 @@ int pthread_mutexattr_getprotocol(FAR const pthread_mutexattr_t *attr, { DEBUGASSERT(attr != NULL && protocol != NULL); +#ifdef CONFIG_PRIORITY_INHERITANCE linfo("Returning %d\n", attr->proto); return attr->proto; +#else + linfo("Returning %d\n", PTHREAD_PRIO_NONE); + return PTHREAD_PRIO_NONE; +#endif } diff --git a/lib/libc/pthread/pthread_mutexattr_setprotocol.c b/lib/libc/pthread/pthread_mutexattr_setprotocol.c index 17da81f..7c31493 100644 --- a/lib/libc/pthread/pthread_mutexattr_setprotocol.c +++ b/lib/libc/pthread/pthread_mutexattr_setprotocol.c @@ -86,6 +86,7 @@ int pthread_mutexattr_setprotocol(FAR pthread_mutexattr_t *attr, linfo("attr=0x%p protocol=%d\n", attr, protocol); DEBUGASSERT(attr != NULL); +#ifdef CONFIG_PRIORITY_INHERITANCE if (protocol >= PTHREAD_PRIO_NONE && protocol <= PTHREAD_PRIO_PROTECT) { attr->proto = protocol; @@ -93,4 +94,11 @@ int pthread_mutexattr_setprotocol(FAR pthread_mutexattr_t *attr, } return EINVAL; +#else + if (protocol == PTHREAD_PRIO_NONE) { + return OK; + } + + return ENOSYS; +#endif } diff --git a/lib/libc/pthread/pthread_mutexattrgettype.c b/lib/libc/pthread/pthread_mutexattrgettype.c index 8003e3d..3e4567e 100644 --- a/lib/libc/pthread/pthread_mutexattrgettype.c +++ b/lib/libc/pthread/pthread_mutexattrgettype.c @@ -58,8 +58,6 @@ #include #include -#ifdef CONFIG_PTHREAD_MUTEX_TYPES - /**************************************************************************** * Definitions ****************************************************************************/ @@ -104,11 +102,13 @@ int pthread_mutexattr_gettype(const pthread_mutexattr_t *attr, int *type) { - if (attr && type) { + if (attr != NULL && type != NULL) { +#ifdef CONFIG_PTHREAD_MUTEX_TYPES *type = attr->type; +#else + *type = PTHREAD_MUTEX_NORMAL; +#endif return 0; } return EINVAL; } - -#endif /* CONFIG_PTHREAD_MUTEX_TYPES */ diff --git a/lib/libc/pthread/pthread_mutexattrsettype.c b/lib/libc/pthread/pthread_mutexattrsettype.c index 43ec9e6..7e475b9 100644 --- a/lib/libc/pthread/pthread_mutexattrsettype.c +++ b/lib/libc/pthread/pthread_mutexattrsettype.c @@ -58,8 +58,6 @@ #include #include -#ifdef CONFIG_PTHREAD_MUTEX_TYPES - /**************************************************************************** * Definitions ****************************************************************************/ @@ -105,10 +103,16 @@ int pthread_mutexattr_settype(pthread_mutexattr_t *attr, int type) { if (attr && type >= PTHREAD_MUTEX_NORMAL && type <= PTHREAD_MUTEX_RECURSIVE) { +#ifdef CONFIG_PTHREAD_MUTEX_TYPES attr->type = type; +#else + if (type != PTHREAD_MUTEX_NORMAL) { + return ENOSYS; + } +#endif + return OK; } + return EINVAL; } - -#endif /* CONFIG_PTHREAD_MUTEX_TYPES */ diff --git a/os/include/pthread.h b/os/include/pthread.h index 39b3451..d3f9b0c 100644 --- a/os/include/pthread.h +++ b/os/include/pthread.h @@ -120,12 +120,10 @@ * An implementation is allowed to map this mutex to one of the other mutex types. */ -#ifdef CONFIG_PTHREAD_MUTEX_TYPES #define PTHREAD_MUTEX_NORMAL 0 #define PTHREAD_MUTEX_ERRORCHECK 1 #define PTHREAD_MUTEX_RECURSIVE 2 #define PTHREAD_MUTEX_DEFAULT PTHREAD_MUTEX_NORMAL -#endif /* Valid ranges for the pthread stacksize attribute */ @@ -590,12 +588,6 @@ int pthread_mutex_trylock(FAR pthread_mutex_t *mutex); */ int pthread_mutex_unlock(FAR pthread_mutex_t *mutex); -#ifdef CONFIG_PRIORITY_INHERITANCE -/* Manage priority inheritance */ -int pthread_mutexattr_getprotocol(FAR const pthread_mutexattr_t *attr, FAR int *protocol); -int pthread_mutexattr_setprotocol(FAR pthread_mutexattr_t *attr, int protocol); -#endif - /* A thread can create and delete condition variables. */ /** * @ingroup PTHREAD_KERNEL @@ -779,7 +771,6 @@ int pthread_mutexattr_getpshared(FAR const pthread_mutexattr_t *attr, FAR int *p * @since Tizen RT v1.0 */ int pthread_mutexattr_setpshared(FAR pthread_mutexattr_t *attr, int pshared); -#ifdef CONFIG_PTHREAD_MUTEX_TYPES /** * @brief POSIX APIs (refer to : http://pubs.opengroup.org/onlinepubs/9699919799/) * @since Tizen RT v1.0 @@ -790,7 +781,11 @@ int pthread_mutexattr_gettype(const pthread_mutexattr_t *attr, int *type); * @since Tizen RT v1.0 */ int pthread_mutexattr_settype(pthread_mutexattr_t *attr, int type); -#endif + +int pthread_mutexattr_getprotocol(FAR const pthread_mutexattr_t *attr, + FAR int *protocol); +int pthread_mutexattr_setprotocol(FAR pthread_mutexattr_t *attr, + int protocol); /* Operations on condition variables */ /** diff --git a/os/kernel/pthread/pthread_condinit.c b/os/kernel/pthread/pthread_condinit.c index 2bb8864..64e818e 100644 --- a/os/kernel/pthread/pthread_condinit.c +++ b/os/kernel/pthread/pthread_condinit.c @@ -88,16 +88,23 @@ int pthread_cond_init(FAR pthread_cond_t *cond, FAR const pthread_condattr_t *at svdbg("cond=0x%p attr=0x%p\n", cond, attr); - if (!cond) { + if (cond == NULL) { ret = EINVAL; } - /* Initialize the semaphore contained in the condition structure + /* + * Initialize the semaphore contained in the condition structure * with initial count = 0 */ else if (sem_init((sem_t *)&cond->sem, 0, 0) != OK) { ret = EINVAL; + } else { + /* + * The contained semaphore is used for signaling and, hence, + * should not have priority inheritance enabled. + */ + sem_setprotocol(&cond->sem, SEM_PRIO_NONE); } svdbg("Returning %d\n", ret); -- 2.7.4