Skip to content

Commit

Permalink
pthread/realtime: export interfaces about pthread ceiling priority
Browse files Browse the repository at this point in the history
pthread_mutex_setprioceiling and pthread_mutex_getprioceiling refers
https://pubs.opengroup.org/onlinepubs/9699919799/functions/pthread_mutex_setprioceiling.html
Signed-off-by: makejian <[email protected]>
  • Loading branch information
makejian committed Sep 9, 2024
1 parent f5f6f53 commit 792d3cd
Show file tree
Hide file tree
Showing 12 changed files with 354 additions and 45 deletions.
13 changes: 12 additions & 1 deletion include/pthread.h
Original file line number Diff line number Diff line change
Expand Up @@ -279,9 +279,12 @@ typedef struct pthread_cond_s pthread_cond_t;
struct pthread_mutexattr_s
{
uint8_t pshared : 1; /* PTHREAD_PROCESS_PRIVATE or PTHREAD_PROCESS_SHARED */
#ifdef CONFIG_PRIORITY_INHERITANCE
#if defined(CONFIG_PRIORITY_INHERITANCE) || defined(CONFIG_PRIORITY_PROTECT)
uint8_t proto : 2; /* See PTHREAD_PRIO_* definitions */
#endif
#ifdef CONFIG_PRIORITY_PROTECT
uint8_t ceiling; /* Priority ceiling */
#endif
#ifdef CONFIG_PTHREAD_MUTEX_TYPES
uint8_t type : 2; /* Type of the mutex. See PTHREAD_MUTEX_* definitions */
#endif
Expand Down Expand Up @@ -612,6 +615,14 @@ int pthread_mutexattr_getrobust(FAR const pthread_mutexattr_t *attr,
FAR int *robust);
int pthread_mutexattr_setrobust(FAR pthread_mutexattr_t *attr,
int robust);
int pthread_mutexattr_getprioceiling(FAR const pthread_mutexattr_t *attr,
FAR int *prioceiling);
int pthread_mutexattr_setprioceiling(FAR pthread_mutexattr_t *attr,
int prioceiling);
int pthread_mutex_getprioceiling(FAR const pthread_mutex_t *mutex,
FAR int *prioceiling);
int pthread_mutex_setprioceiling(FAR pthread_mutex_t *mutex,
int prioceiling, FAR int *old_ceiling);

/* The following routines create, delete, lock and unlock mutexes. */

Expand Down
4 changes: 4 additions & 0 deletions libs/libc/libc.csv
Original file line number Diff line number Diff line change
Expand Up @@ -217,11 +217,15 @@
"pthread_gettid_np","pthread.h","!defined(CONFIG_DISABLE_PTHREAD)","pid_t","pthread_t"
"pthread_key_create","pthread.h","!defined(CONFIG_DISABLE_PTHREAD) && CONFIG_TLS_NELEM > 0","int","FAR pthread_key_t *","FAR void (*) (void *)|FAR void *"
"pthread_key_delete","pthread.h","!defined(CONFIG_DISABLE_PTHREAD) && CONFIG_TLS_NELEM > 0","int","pthread_key_t"
"pthread_mutex_getprioceiling","pthread.h","!defined(CONFIG_DISABLE_PTHREAD) && defined(CONFIG_PRIORITY_PROTECT)","int","FAR const pthread_mutex_t *","FAR int *"
"pthread_mutex_lock","pthread.h","!defined(CONFIG_DISABLE_PTHREAD)","int","FAR pthread_mutex_t *"
"pthread_mutex_setprioceiling","pthread.h","!defined(CONFIG_DISABLE_PTHREAD) && defined(CONFIG_PRIORITY_PROTECT)","int","FAR pthread_mutex_t *","int","FAR int *"
"pthread_mutexattr_destroy","pthread.h","!defined(CONFIG_DISABLE_PTHREAD)","int","FAR pthread_mutexattr_t *"
"pthread_mutexattr_getprioceiling","pthread.h","!defined(CONFIG_DISABLE_PTHREAD) && defined(CONFIG_PRIORITY_PROTECT)","int","FAR pthread_mutexattr_t *","FAR int *"
"pthread_mutexattr_getpshared","pthread.h","!defined(CONFIG_DISABLE_PTHREAD)","int","FAR pthread_mutexattr_t *","FAR int *"
"pthread_mutexattr_gettype","pthread.h","!defined(CONFIG_DISABLE_PTHREAD) && defined(CONFIG_PTHREAD_MUTEX_TYPES)","int","FAR const pthread_mutexattr_t *","FAR int *"
"pthread_mutexattr_init","pthread.h","!defined(CONFIG_DISABLE_PTHREAD)","int","FAR pthread_mutexattr_t *"
"pthread_mutexattr_setprioceiling","pthread.h","!defined(CONFIG_DISABLE_PTHREAD) && defined(CONFIG_PRIORITY_PROTECT)","int","FAR pthread_mutexattr_t *","int"
"pthread_mutexattr_setpshared","pthread.h","!defined(CONFIG_DISABLE_PTHREAD)","int","FAR pthread_mutexattr_t *","int "
"pthread_mutexattr_settype","pthread.h","!defined(CONFIG_DISABLE_PTHREAD) && defined(CONFIG_PTHREAD_MUTEX_TYPES)","int","FAR pthread_mutexattr_t *","int"
"pthread_once","pthread.h","!defined(CONFIG_DISABLE_PTHREAD)","int","FAR pthread_once_t*","CODE void (*)(void)"
Expand Down
4 changes: 4 additions & 0 deletions libs/libc/pthread/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -79,7 +79,11 @@ if(NOT CONFIG_DISABLE_PTHREAD)
pthread_mutexattr_gettype.c
pthread_mutexattr_setrobust.c
pthread_mutexattr_getrobust.c
pthread_mutexattr_setprioceiling.c
pthread_mutexattr_getprioceiling.c
pthread_mutex_lock.c
pthread_mutex_setprioceiling.c
pthread_mutex_getprioceiling.c
pthread_once.c
pthread_yield.c
pthread_atfork.c
Expand Down
2 changes: 2 additions & 0 deletions libs/libc/pthread/Make.defs
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,9 @@ CSRCS += pthread_mutexattr_getpshared.c pthread_mutexattr_setpshared.c
CSRCS += pthread_mutexattr_setprotocol.c pthread_mutexattr_getprotocol.c
CSRCS += pthread_mutexattr_settype.c pthread_mutexattr_gettype.c
CSRCS += pthread_mutexattr_setrobust.c pthread_mutexattr_getrobust.c
CSRCS += pthread_mutexattr_setprioceiling.c pthread_mutexattr_getprioceiling.c
CSRCS += pthread_mutex_lock.c
CSRCS += pthread_mutex_setprioceiling.c pthread_mutex_getprioceiling.c
CSRCS += pthread_once.c pthread_yield.c pthread_atfork.c
CSRCS += pthread_rwlockattr_init.c pthread_rwlockattr_destroy.c
CSRCS += pthread_rwlockattr_getpshared.c pthread_rwlockattr_setpshared.c
Expand Down
64 changes: 64 additions & 0 deletions libs/libc/pthread/pthread_mutex_getprioceiling.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,64 @@
/****************************************************************************
* libs/libc/pthread/pthread_mutex_getprioceiling.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 <nuttx/mutex.h>

#include <pthread.h>

/****************************************************************************
* Public Functions
****************************************************************************/

/****************************************************************************
* Name: pthread_mutex_getprioceiling
*
* Description:
* Return the mutex priority ceiling from the mutex.
*
* Input Parameters:
* mutex - The mutex to query
* prioceiling - Location to return the mutex priority ceiling
*
* Returned Value:
* 0, if the mutex type was successfully return in 'prioceiling', or
* EINVAL, if any NULL pointers provided.
*
* Assumptions:
*
****************************************************************************/

int pthread_mutex_getprioceiling(FAR const pthread_mutex_t *mutex,
FAR int *prioceiling)
{
#ifdef CONFIG_PRIORITY_PROTECT
# ifdef CONFIG_PTHREAD_MUTEX_TYPES
return -nxrmutex_getprioceiling(&mutex->mutex, prioceiling);
# else
return -nxmutex_getprioceiling(&mutex->mutex, prioceiling);
# endif
#else
return EINVAL;
#endif
}
74 changes: 74 additions & 0 deletions libs/libc/pthread/pthread_mutex_setprioceiling.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,74 @@
/****************************************************************************
* libs/libc/pthread/pthread_mutex_setprioceiling.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 <nuttx/mutex.h>

#include <pthread.h>

/****************************************************************************
* Public Functions
****************************************************************************/

/****************************************************************************
* Name: pthread_mutex_setprioceiling
*
* Description:
* Set the priority ceiling of a mutex.
*
* Input Parameters:
* mutex - The mutex in which to set the mutex priority ceiling.
* prioceiling - The mutex priority ceiling value to set.
* old_ceiling - Location to return the mutex ceiling priority set before.
*
* Returned Value:
* 0, indicating the mutex priority ceiling was successfully set, or
* EINVAL, indicating 'mutex' or 'old_ceiling' is NULL, or 'prioceiling'
* is out of range.
*
* Assumptions:
*
****************************************************************************/

int pthread_mutex_setprioceiling(FAR pthread_mutex_t *mutex,
int prioceiling, FAR int *old_ceiling)
{
int ret = EINVAL;

#ifdef CONFIG_PRIORITY_PROTECT
ret = pthread_mutex_lock(mutex);
if (ret != OK)
{
return ret;
}

# ifdef CONFIG_PTHREAD_MUTEX_TYPES
ret = -nxrmutex_setprioceiling(&mutex->mutex, prioceiling, old_ceiling);
# else
ret = -nxmutex_setprioceiling(&mutex->mutex, prioceiling, old_ceiling);
# endif
pthread_mutex_unlock(mutex);
#endif
return ret;
}
63 changes: 63 additions & 0 deletions libs/libc/pthread/pthread_mutexattr_getprioceiling.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,63 @@
/****************************************************************************
* libs/libc/pthread/pthread_mutexattr_getprioceiling.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>

/****************************************************************************
* Public Functions
****************************************************************************/

/****************************************************************************
* Name: pthread_mutexattr_getprioceiling
*
* Description:
* Return the mutex ceiling priority from the mutex attributes.
*
* Input Parameters:
* attr - The mutex attributes to query
* prioceiling - Location to return the mutex ceiling priority
*
* Returned Value:
* 0, if the mutex type was successfully return in 'prioceiling', or
* EINVAL, if any NULL pointers provided.
*
* Assumptions:
*
****************************************************************************/

int pthread_mutexattr_getprioceiling(FAR const pthread_mutexattr_t *attr,
FAR int *prioceiling)
{
#ifdef CONFIG_PRIORITY_PROTECT
if (attr != NULL && prioceiling != NULL)
{
*prioceiling = attr->ceiling;
return OK;
}
#endif

return EINVAL;
}
4 changes: 2 additions & 2 deletions libs/libc/pthread/pthread_mutexattr_getprotocol.c
Original file line number Diff line number Diff line change
Expand Up @@ -53,13 +53,13 @@ int pthread_mutexattr_getprotocol(FAR const pthread_mutexattr_t *attr,
{
DEBUGASSERT(attr != NULL && protocol != NULL);

#ifdef CONFIG_PRIORITY_INHERITANCE
#if defined(CONFIG_PRIORITY_INHERITANCE) || defined(CONFIG_PRIORITY_PROTECT)
linfo("Returning %d\n", attr->proto);
*protocol = attr->proto;
#else
linfo("Returning %d\n", PTHREAD_PRIO_NONE);
*protocol = PTHREAD_PRIO_NONE;
#endif /* CONFIG_PRIORITY_INHERITANCE */
#endif /* CONFIG_PRIORITY_INHERITANCE || CONFIG_PRIORITY_PROTECT */

return 0;
}
6 changes: 6 additions & 0 deletions libs/libc/pthread/pthread_mutexattr_init.c
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@
#include <pthread.h>
#include <errno.h>
#include <debug.h>
#include <sched.h>

/****************************************************************************
* Public Functions
Expand Down Expand Up @@ -60,6 +61,11 @@ int pthread_mutexattr_init(FAR pthread_mutexattr_t *attr)
{
attr->pshared = 0;

#ifdef CONFIG_PRIORITY_PROTECT
attr->proto = PTHREAD_PRIO_NONE;
attr->ceiling = sched_get_priority_min(SCHED_FIFO);
#endif

#ifdef CONFIG_PRIORITY_INHERITANCE
# ifdef CONFIG_PTHREAD_MUTEX_DEFAULT_PRIO_INHERIT
attr->proto = PTHREAD_PRIO_INHERIT;
Expand Down
65 changes: 65 additions & 0 deletions libs/libc/pthread/pthread_mutexattr_setprioceiling.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,65 @@
/****************************************************************************
* libs/libc/pthread/pthread_mutexattr_setprioceiling.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 <sched.h>

/****************************************************************************
* Public Functions
****************************************************************************/

/****************************************************************************
* Name: pthread_mutexattr_setprioceiling
*
* Description:
* Set the mutex ceiling priority in the mutex attributes.
*
* Input Parameters:
* attr - The mutex attributes in which to set the mutex type.
* prioceiling - The mutex ceiling priority value to set.
*
* Returned Value:
* 0, if the mutex type was successfully set in 'attr', or
* EINVAL, if 'attr' is NULL or 'prioceiling' out of range.
*
* Assumptions:
*
****************************************************************************/

int pthread_mutexattr_setprioceiling(FAR pthread_mutexattr_t *attr,
int prioceiling)
{
#ifdef CONFIG_PRIORITY_PROTECT
if (attr && prioceiling >= sched_get_priority_min(SCHED_FIFO) &&
prioceiling <= sched_get_priority_max(SCHED_FIFO))
{
attr->ceiling = prioceiling;
return OK;
}
#endif

return EINVAL;
}
Loading

0 comments on commit 792d3cd

Please sign in to comment.