Changeset 6fc34e4 in rtems


Ignore:
Timestamp:
Jun 6, 2016, 8:58:04 AM (3 years ago)
Author:
Sebastian Huber <sebastian.huber@…>
Branches:
master
Children:
cc8bb9e3
Parents:
2f3d804
git-author:
Sebastian Huber <sebastian.huber@…> (06/06/16 08:58:04)
git-committer:
Sebastian Huber <sebastian.huber@…> (06/07/16 08:18:39)
Message:

rtems: Simplify rtems_semaphore_create()

Remove superfluous includes. Use one attribute compare for each
semaphore variant. Text size drops by 10% on PowerPC due to this.

File:
1 edited

Legend:

Unmodified
Added
Removed
  • cpukit/rtems/src/semcreate.c

    r2f3d804 r6fc34e4  
    1919#endif
    2020
    21 #include <rtems/system.h>
    22 #include <rtems/rtems/status.h>
    23 #include <rtems/rtems/support.h>
     21#include <rtems/rtems/semimpl.h>
    2422#include <rtems/rtems/attrimpl.h>
    25 #include <rtems/score/isr.h>
    26 #include <rtems/rtems/options.h>
    27 #include <rtems/rtems/semimpl.h>
    2823#include <rtems/rtems/statusimpl.h>
    2924#include <rtems/rtems/tasksimpl.h>
    30 #include <rtems/score/coremuteximpl.h>
    31 #include <rtems/score/coresemimpl.h>
    32 #include <rtems/score/threaddispatch.h>
    3325#include <rtems/score/sysstate.h>
    3426
    35 #include <rtems/score/interr.h>
    36 
    37 /*
    38  *  rtems_semaphore_create
    39  *
    40  *  This directive creates a semaphore and sets the initial value based
    41  *  on the given count.  A semaphore id is returned.
    42  *
    43  *  Input parameters:
    44  *    name             - user defined semaphore name
    45  *    count            - initial count of semaphore
    46  *    attribute_set    - semaphore attributes
    47  *    priority_ceiling - semaphore's ceiling priority
    48  *    id               - pointer to semaphore id
    49  *
    50  *  Output parameters:
    51  *    id       - semaphore id
    52  *    RTEMS_SUCCESSFUL - if successful
    53  *    error code - if unsuccessful
    54  */
     27#define SEMAPHORE_KIND_MASK ( RTEMS_SEMAPHORE_CLASS | RTEMS_INHERIT_PRIORITY \
     28  | RTEMS_PRIORITY_CEILING | RTEMS_MULTIPROCESSOR_RESOURCE_SHARING )
    5529
    5630rtems_status_code rtems_semaphore_create(
     
    6539  Thread_Control    *executing;
    6640  Status_Control     status;
     41  rtems_attribute    maybe_global;
     42  rtems_attribute    mutex_with_protocol;
     43  Semaphore_Variant  variant;
    6744
    6845  if ( !rtems_is_name_valid( name ) )
     
    7350
    7451#if defined(RTEMS_MULTIPROCESSING)
    75   if ( _Attributes_Is_global( attribute_set ) ) {
    76 
    77     if ( !_System_state_Is_multiprocessing )
    78       return RTEMS_MP_NOT_CONFIGURED;
    79 
    80     if ( _Attributes_Is_inherit_priority( attribute_set ) ||
    81          _Attributes_Is_priority_ceiling( attribute_set ) ||
    82          _Attributes_Is_multiprocessor_resource_sharing( attribute_set ) )
    83       return RTEMS_NOT_DEFINED;
    84 
    85   } else
    86 #endif
    87 
    88   if ( _Attributes_Is_multiprocessor_resource_sharing( attribute_set ) &&
    89        !( _Attributes_Is_binary_semaphore( attribute_set ) &&
    90          !_Attributes_Is_priority( attribute_set ) ) ) {
     52  if (
     53    _Attributes_Is_global( attribute_set )
     54      && !_System_state_Is_multiprocessing
     55  ) {
     56    return RTEMS_MP_NOT_CONFIGURED;
     57  }
     58#endif
     59
     60  /* Attribute subset defining a potentially global semaphore variant */
     61  maybe_global = attribute_set & SEMAPHORE_KIND_MASK;
     62
     63  /* Attribute subset defining a mutex variant with a locking protocol */
     64  mutex_with_protocol =
     65    attribute_set & ( SEMAPHORE_KIND_MASK | RTEMS_GLOBAL | RTEMS_PRIORITY );
     66
     67  if ( maybe_global == RTEMS_COUNTING_SEMAPHORE ) {
     68    variant = SEMAPHORE_VARIANT_COUNTING;
     69  } else if ( count > 1 ) {
     70    /*
     71     * The remaining variants are all binary semphores, thus reject an invalid
     72     * count value.
     73     */
     74    return RTEMS_INVALID_NUMBER;
     75  } else if ( maybe_global == RTEMS_SIMPLE_BINARY_SEMAPHORE ) {
     76    variant = SEMAPHORE_VARIANT_SIMPLE_BINARY;
     77  } else if ( maybe_global == RTEMS_BINARY_SEMAPHORE ) {
     78    variant = SEMAPHORE_VARIANT_MUTEX_NO_PROTOCOL;
     79  } else if (
     80    mutex_with_protocol
     81      == ( RTEMS_BINARY_SEMAPHORE | RTEMS_PRIORITY | RTEMS_INHERIT_PRIORITY )
     82  ) {
     83    variant = SEMAPHORE_VARIANT_MUTEX_INHERIT_PRIORITY;
     84  } else if (
     85    mutex_with_protocol
     86      == ( RTEMS_BINARY_SEMAPHORE | RTEMS_PRIORITY | RTEMS_PRIORITY_CEILING )
     87  ) {
     88    variant = SEMAPHORE_VARIANT_MUTEX_PRIORITY_CEILING;
     89  } else if (
     90    mutex_with_protocol
     91      == ( RTEMS_BINARY_SEMAPHORE | RTEMS_MULTIPROCESSOR_RESOURCE_SHARING )
     92  ) {
     93#if defined(RTEMS_SMP)
     94    variant = SEMAPHORE_VARIANT_MRSP;
     95#else
     96    /*
     97     * On uni-processor configurations the Multiprocessor Resource Sharing
     98     * Protocol is equivalent to the Priority Ceiling Protocol.
     99     */
     100    variant = SEMAPHORE_VARIANT_MUTEX_PRIORITY_CEILING;
     101#endif
     102  } else {
    91103    return RTEMS_NOT_DEFINED;
    92104  }
    93 
    94   if ( _Attributes_Is_inherit_priority( attribute_set ) ||
    95               _Attributes_Is_priority_ceiling( attribute_set ) ) {
    96 
    97     if ( ! (_Attributes_Is_binary_semaphore( attribute_set ) &&
    98             _Attributes_Is_priority( attribute_set ) ) )
    99       return RTEMS_NOT_DEFINED;
    100 
    101   }
    102 
    103   if ( !_Attributes_Has_at_most_one_protocol( attribute_set ) )
    104     return RTEMS_NOT_DEFINED;
    105 
    106   if ( !_Attributes_Is_counting_semaphore( attribute_set ) && ( count > 1 ) )
    107     return RTEMS_INVALID_NUMBER;
    108 
    109 #if !defined(RTEMS_SMP)
    110   /*
    111    * On uni-processor configurations the Multiprocessor Resource Sharing
    112    * Protocol is equivalent to the Priority Ceiling Protocol.
    113    */
    114   if ( _Attributes_Is_multiprocessor_resource_sharing( attribute_set ) ) {
    115     attribute_set |= RTEMS_PRIORITY_CEILING | RTEMS_PRIORITY;
    116   }
    117 #endif
    118105
    119106  the_semaphore = _Semaphore_Allocate();
     
    139126  executing = _Thread_Get_executing();
    140127
     128  the_semaphore->variant = variant;
     129
    141130  if ( _Attributes_Is_priority( attribute_set ) ) {
    142131    the_semaphore->discipline = SEMAPHORE_DISCIPLINE_PRIORITY;
     
    145134  }
    146135
    147   if ( _Attributes_Is_counting_semaphore( attribute_set ) ) {
    148     the_semaphore->variant = SEMAPHORE_VARIANT_COUNTING;
    149     _CORE_semaphore_Initialize(
    150       &the_semaphore->Core_control.Semaphore,
    151       count
    152     );
    153     status = STATUS_SUCCESSFUL;
    154   } else if ( _Attributes_Is_simple_binary_semaphore( attribute_set ) ) {
    155     the_semaphore->variant = SEMAPHORE_VARIANT_SIMPLE_BINARY;
    156     _CORE_semaphore_Initialize(
    157       &the_semaphore->Core_control.Semaphore,
    158       count != 0
    159     );
    160     status = STATUS_SUCCESSFUL;
     136  switch ( the_semaphore->variant ) {
     137    case SEMAPHORE_VARIANT_MUTEX_NO_PROTOCOL:
     138    case SEMAPHORE_VARIANT_MUTEX_INHERIT_PRIORITY:
     139      _CORE_recursive_mutex_Initialize(
     140        &the_semaphore->Core_control.Mutex.Recursive
     141      );
     142
     143      if ( count == 0 ) {
     144        _CORE_mutex_Set_owner(
     145          &the_semaphore->Core_control.Mutex.Recursive.Mutex,
     146          executing
     147        );
     148
     149        if ( variant == SEMAPHORE_VARIANT_MUTEX_INHERIT_PRIORITY ) {
     150          ++executing->resource_count;
     151        }
     152      }
     153
     154      status = STATUS_SUCCESSFUL;
     155      break;
     156    case SEMAPHORE_VARIANT_MUTEX_PRIORITY_CEILING:
     157      _CORE_ceiling_mutex_Initialize(
     158        &the_semaphore->Core_control.Mutex,
     159        priority_ceiling
     160      );
     161
     162      if ( count == 0 ) {
     163        Thread_queue_Context queue_context;
     164
     165        _Thread_queue_Context_initialize( &queue_context );
     166        _ISR_lock_ISR_disable( &queue_context.Lock_context );
     167        _CORE_mutex_Acquire_critical(
     168          &the_semaphore->Core_control.Mutex.Recursive.Mutex,
     169          &queue_context
     170        );
     171        status = _CORE_ceiling_mutex_Set_owner(
     172          &the_semaphore->Core_control.Mutex,
     173          executing,
     174          &queue_context
     175        );
     176      } else {
     177        status = STATUS_SUCCESSFUL;
     178      }
     179
     180      break;
    161181#if defined(RTEMS_SMP)
    162   } else if ( _Attributes_Is_multiprocessor_resource_sharing( attribute_set ) ) {
    163     the_semaphore->variant = SEMAPHORE_VARIANT_MRSP;
    164     status = _MRSP_Initialize(
    165       &the_semaphore->Core_control.MRSP,
    166       priority_ceiling,
    167       executing,
    168       count != 1
    169     );
    170 #endif
    171   } else if ( _Attributes_Is_priority_ceiling( attribute_set ) ) {
    172     _Assert( _Attributes_Is_binary_semaphore( attribute_set ) );
    173     the_semaphore->variant = SEMAPHORE_VARIANT_MUTEX_PRIORITY_CEILING;
    174     _CORE_ceiling_mutex_Initialize(
    175       &the_semaphore->Core_control.Mutex,
    176       priority_ceiling
    177     );
    178 
    179     if ( count == 0 ) {
    180       Thread_queue_Context queue_context;
    181 
    182       _Thread_queue_Context_initialize( &queue_context );
    183       _ISR_lock_ISR_disable( &queue_context.Lock_context );
    184       _CORE_mutex_Acquire_critical(
    185         &the_semaphore->Core_control.Mutex.Recursive.Mutex,
    186         &queue_context
    187       );
    188       status = _CORE_ceiling_mutex_Set_owner(
    189         &the_semaphore->Core_control.Mutex,
     182    case SEMAPHORE_VARIANT_MRSP:
     183      status = _MRSP_Initialize(
     184        &the_semaphore->Core_control.MRSP,
     185        priority_ceiling,
    190186        executing,
    191         &queue_context
    192       );
    193     } else {
     187        count == 0
     188      );
     189      break;
     190#endif
     191    default:
     192      _Assert(
     193        the_semaphore->variant == SEMAPHORE_VARIANT_SIMPLE_BINARY
     194          || the_semaphore->variant == SEMAPHORE_VARIANT_COUNTING
     195      );
     196      _CORE_semaphore_Initialize(
     197        &the_semaphore->Core_control.Semaphore,
     198        count
     199      );
    194200      status = STATUS_SUCCESSFUL;
    195     }
    196   } else {
    197     _Assert( _Attributes_Is_binary_semaphore( attribute_set ) );
    198 
    199     if ( _Attributes_Is_inherit_priority( attribute_set ) ) {
    200       the_semaphore->variant = SEMAPHORE_VARIANT_MUTEX_INHERIT_PRIORITY;
    201     } else {
    202       the_semaphore->variant = SEMAPHORE_VARIANT_MUTEX_NO_PROTOCOL;
    203     }
    204 
    205     _CORE_recursive_mutex_Initialize(
    206       &the_semaphore->Core_control.Mutex.Recursive
    207     );
    208 
    209     if ( count == 0 ) {
    210       _CORE_mutex_Set_owner(
    211         &the_semaphore->Core_control.Mutex.Recursive.Mutex,
    212         executing
    213       );
    214 
    215       if ( _Attributes_Is_inherit_priority( attribute_set ) ) {
    216         ++executing->resource_count;
    217       }
    218     }
    219 
    220     status = STATUS_SUCCESSFUL;
     201      break;
    221202  }
    222203
Note: See TracChangeset for help on using the changeset viewer.