[rtems commit] posix: Generalize _POSIX_Priority_To_core()

Sebastian Huber sebh at rtems.org
Wed Jun 22 12:47:12 UTC 2016


Module:    rtems
Branch:    master
Commit:    1a4eac500c9a2942f4aff7e27ccdfae3b99c4cb9
Changeset: http://git.rtems.org/rtems/commit/?id=1a4eac500c9a2942f4aff7e27ccdfae3b99c4cb9

Author:    Sebastian Huber <sebastian.huber at embedded-brains.de>
Date:      Wed Jun 15 16:31:33 2016 +0200

posix: Generalize _POSIX_Priority_To_core()

Move POSIX API priority validation into _POSIX_Priority_To_core().

---

 cpukit/posix/include/rtems/posix/priorityimpl.h | 16 ++++++++++------
 cpukit/posix/src/mutexinit.c                    |  8 ++++----
 cpukit/posix/src/mutexsetprioceiling.c          |  9 ++++-----
 cpukit/posix/src/psxpriorityisvalid.c           | 18 +++++++++++++-----
 cpukit/posix/src/pthreadcreate.c                | 10 +++++-----
 cpukit/posix/src/pthreadsetschedparam.c         | 10 +++++-----
 cpukit/posix/src/pthreadsetschedprio.c          |  5 +++--
 7 files changed, 44 insertions(+), 32 deletions(-)

diff --git a/cpukit/posix/include/rtems/posix/priorityimpl.h b/cpukit/posix/include/rtems/posix/priorityimpl.h
index d06b600..ae2e763 100644
--- a/cpukit/posix/include/rtems/posix/priorityimpl.h
+++ b/cpukit/posix/include/rtems/posix/priorityimpl.h
@@ -51,7 +51,8 @@ extern "C" {
 int _POSIX_Priority_Get_maximum( const Scheduler_Control *scheduler );
 
 /**
- * @brief Check if POSIX priority is valid.
+ * @brief Converts the POSIX API priority to the corresponding SuperCore
+ * priority and validates it.
  * 
  * According to POSIX, numerically higher values represent higher priorities.
  * Thus, SuperCore has priorities run in the opposite sense of the POSIX API.
@@ -65,14 +66,17 @@ int _POSIX_Priority_Get_maximum( const Scheduler_Control *scheduler );
  * having N priorities.
  *
  * @param[in] scheduler The scheduler instance.
- * @param[in] priority The POSIX API priority to test.
+ * @param[in] priority The POSIX API priority to convert and validate.
+ * @param[out] valid Indicates if the POSIX API priority is valid and a
+ *   corresponding SuperCore priority in the specified scheduler instance
+ *   exists.
  *
- * @retval true The priority is valid.
- * @retval false Otherwise.
+ * @return The corresponding SuperCore priority.
  */
-bool _POSIX_Priority_Is_valid(
+Priority_Control _POSIX_Priority_To_core(
   const Scheduler_Control *scheduler,
-  int                      priority
+  int                      priority,
+  bool                    *valid
 );
 
 /**
diff --git a/cpukit/posix/src/mutexinit.c b/cpukit/posix/src/mutexinit.c
index 04c36e1..2cda90e 100644
--- a/cpukit/posix/src/mutexinit.c
+++ b/cpukit/posix/src/mutexinit.c
@@ -107,7 +107,8 @@ int pthread_mutex_init(
 #endif
 
   if ( protocol == POSIX_MUTEX_PRIORITY_CEILING ) {
-    int prio_ceiling;
+    int  prio_ceiling;
+    bool valid;
 
     scheduler = _Scheduler_Get_own( _Thread_Get_executing() );
     prio_ceiling = the_attr->prio_ceiling;
@@ -116,11 +117,10 @@ int pthread_mutex_init(
       prio_ceiling = _POSIX_Priority_Get_maximum( scheduler );
     }
 
-    if ( !_POSIX_Priority_Is_valid( scheduler, prio_ceiling ) ) {
+    priority = _POSIX_Priority_To_core( scheduler, prio_ceiling, &valid );
+    if ( !valid ) {
       return EINVAL;
     }
-
-    priority = _POSIX_Priority_To_core( scheduler, prio_ceiling );
   }
 
   the_mutex = _POSIX_Mutex_Allocate();
diff --git a/cpukit/posix/src/mutexsetprioceiling.c b/cpukit/posix/src/mutexsetprioceiling.c
index 65b93c7..478aafa 100644
--- a/cpukit/posix/src/mutexsetprioceiling.c
+++ b/cpukit/posix/src/mutexsetprioceiling.c
@@ -33,6 +33,8 @@ int pthread_mutex_setprioceiling(
 {
   POSIX_Mutex_Control     *the_mutex;
   const Scheduler_Control *scheduler;
+  bool                     valid;
+  Priority_Control         priority;
   int                      error;
   int                      unlock_error;
 
@@ -59,12 +61,9 @@ int pthread_mutex_setprioceiling(
     the_mutex->Mutex.priority_ceiling
   );
 
-  if ( _POSIX_Priority_Is_valid( scheduler, prioceiling ) ) {
-    Priority_Control priority;
-
-    priority = _POSIX_Priority_To_core( scheduler, prioceiling );
+  priority = _POSIX_Priority_To_core( scheduler, prioceiling, &valid );
+  if ( valid ) {
     the_mutex->Mutex.priority_ceiling = priority;
-
     error = 0;
   } else {
     error = EINVAL;
diff --git a/cpukit/posix/src/psxpriorityisvalid.c b/cpukit/posix/src/psxpriorityisvalid.c
index ea7f6f4..22c4ac0 100644
--- a/cpukit/posix/src/psxpriorityisvalid.c
+++ b/cpukit/posix/src/psxpriorityisvalid.c
@@ -29,12 +29,20 @@ int _POSIX_Priority_Get_maximum( const Scheduler_Control *scheduler )
   }
 }
 
-bool _POSIX_Priority_Is_valid(
+Priority_Control _POSIX_Priority_To_core(
   const Scheduler_Control *scheduler,
-  int                      priority
+  int                      posix_priority,
+  bool                    *valid
 )
 {
-  return priority >= POSIX_SCHEDULER_MINIMUM_PRIORITY
-    && (Priority_Control) priority < scheduler->maximum_priority;
-}
+  Priority_Control core_posix_priority;
+  Priority_Control core_priority;
+
+  core_posix_priority = (Priority_Control) posix_priority;
+  core_priority = scheduler->maximum_priority - core_posix_priority;
 
+  *valid = ( posix_priority >= POSIX_SCHEDULER_MINIMUM_PRIORITY
+    && core_posix_priority < scheduler->maximum_priority );
+
+  return core_priority;
+}
diff --git a/cpukit/posix/src/pthreadcreate.c b/cpukit/posix/src/pthreadcreate.c
index a4b4684..b266a4c 100644
--- a/cpukit/posix/src/pthreadcreate.c
+++ b/cpukit/posix/src/pthreadcreate.c
@@ -62,6 +62,7 @@ int pthread_create(
   const pthread_attr_t               *the_attr;
   int                                 low_prio;
   int                                 high_prio;
+  bool                                valid;
   Priority_Control                    core_low_prio;
   Priority_Control                    core_high_prio;
   Thread_CPU_budget_algorithms        budget_algorithm;
@@ -154,17 +155,16 @@ int pthread_create(
 
   scheduler = _Scheduler_Get_own( executing );
 
-  if ( !_POSIX_Priority_Is_valid( scheduler, low_prio ) ) {
+  core_low_prio = _POSIX_Priority_To_core( scheduler, low_prio, &valid );
+  if ( !valid ) {
     return EINVAL;
   }
 
-  if ( !_POSIX_Priority_Is_valid( scheduler, high_prio ) ) {
+  core_high_prio = _POSIX_Priority_To_core( scheduler, high_prio, &valid );
+  if ( !valid ) {
     return EINVAL;
   }
 
-  core_low_prio = _POSIX_Priority_To_core( scheduler, low_prio );
-  core_high_prio = _POSIX_Priority_To_core( scheduler, high_prio );
-
 #if defined(RTEMS_SMP)
 #if __RTEMS_HAVE_SYS_CPUSET_H__
   status = _CPU_set_Is_valid( the_attr->affinityset, the_attr->affinitysetsize );
diff --git a/cpukit/posix/src/pthreadsetschedparam.c b/cpukit/posix/src/pthreadsetschedparam.c
index 148391d..92560fc 100644
--- a/cpukit/posix/src/pthreadsetschedparam.c
+++ b/cpukit/posix/src/pthreadsetschedparam.c
@@ -50,6 +50,7 @@ static bool _POSIX_Set_sched_param_filter(
   POSIX_API_Control             *api;
   int                            low_prio;
   int                            high_prio;
+  bool                           valid;
   Priority_Control               core_low_prio;
   Priority_Control               core_high_prio;
   Priority_Control               current_priority;
@@ -66,19 +67,18 @@ static bool _POSIX_Set_sched_param_filter(
     high_prio = low_prio;
   }
 
-  if ( !_POSIX_Priority_Is_valid( scheduler, low_prio ) ) {
+  core_low_prio = _POSIX_Priority_To_core( scheduler, low_prio, &valid );
+  if ( !valid ) {
     context->error = EINVAL;
     return false;
   }
 
-  if ( !_POSIX_Priority_Is_valid( scheduler, high_prio ) ) {
+  core_high_prio = _POSIX_Priority_To_core( scheduler, high_prio, &valid );
+  if ( !valid ) {
     context->error = EINVAL;
     return false;
   }
 
-  core_low_prio = _POSIX_Priority_To_core( scheduler, low_prio );
-  core_high_prio = _POSIX_Priority_To_core( scheduler, high_prio );
-
   *new_priority_p = core_high_prio;
 
   current_priority = the_thread->current_priority;
diff --git a/cpukit/posix/src/pthreadsetschedprio.c b/cpukit/posix/src/pthreadsetschedprio.c
index dace70a..fd77b68 100644
--- a/cpukit/posix/src/pthreadsetschedprio.c
+++ b/cpukit/posix/src/pthreadsetschedprio.c
@@ -33,6 +33,7 @@ static bool _POSIX_Set_sched_prio_filter(
   int                           prio;
   const Scheduler_Control      *scheduler;
   POSIX_API_Control            *api;
+  bool                          valid;
   Priority_Control              current_priority;
   Priority_Control              new_priority;
 
@@ -40,12 +41,12 @@ static bool _POSIX_Set_sched_prio_filter(
   prio = context->prio;
   scheduler = _Scheduler_Get_own( the_thread );
 
-  if ( !_POSIX_Priority_Is_valid( scheduler, prio ) ) {
+  new_priority = _POSIX_Priority_To_core( scheduler, prio, &valid );
+  if ( !valid ) {
     context->error = EINVAL;
     return false;
   }
 
-  new_priority = _POSIX_Priority_To_core( scheduler, prio );
   *new_priority_p = new_priority;
 
   current_priority = the_thread->current_priority;



More information about the vc mailing list