From d5cc9fd6f1fd6268c4872e4145fb27fa5a08a16e Mon Sep 17 00:00:00 2001 From: Sebastian Huber Date: Thu, 28 Apr 2016 06:51:25 +0200 Subject: score: __RTEMS_DO_NOT_INLINE_CORE_MUTEX_SEIZE__ Delete __RTEMS_DO_NOT_INLINE_CORE_MUTEX_SEIZE__ as a preparation to restructure the CORE mutex variants and reduce the branch complexity. --- cpukit/score/include/rtems/score/coremuteximpl.h | 391 +++++++++-------------- 1 file changed, 158 insertions(+), 233 deletions(-) (limited to 'cpukit/score/include/rtems/score/coremuteximpl.h') diff --git a/cpukit/score/include/rtems/score/coremuteximpl.h b/cpukit/score/include/rtems/score/coremuteximpl.h index 69935b5af3..ef116ec7b8 100644 --- a/cpukit/score/include/rtems/score/coremuteximpl.h +++ b/cpukit/score/include/rtems/score/coremuteximpl.h @@ -122,64 +122,6 @@ RTEMS_INLINE_ROUTINE void _CORE_mutex_Release( _Thread_queue_Release( &the_mutex->Wait_queue, lock_context ); } -/** - * @brief Attempt to receive a unit from the_mutex. - * - * This routine attempts to receive a unit from the_mutex. - * If a unit is available or if the wait flag is false, then the routine - * returns. Otherwise, the calling task is blocked until a unit becomes - * available. - * - * @param[in,out] executing The currently executing thread. - * @param[in,out] the_mutex is the mutex to attempt to lock - * @param[in] lock_context is the interrupt level - * - * @retval This routine returns 0 if "trylock" can resolve whether or not - * the mutex is immediately obtained or there was an error attempting to - * get it. It returns 1 to indicate that the caller cannot obtain - * the mutex and will have to block to do so. - * - * @note For performance reasons, this routine is implemented as - * a macro that uses two support routines. - */ - -RTEMS_INLINE_ROUTINE int _CORE_mutex_Seize_interrupt_trylock_body( - CORE_mutex_Control *the_mutex, - Thread_Control *executing, - ISR_lock_Context *lock_context -); - -#if defined(__RTEMS_DO_NOT_INLINE_CORE_MUTEX_SEIZE__) - /** - * @brief Interrupt trylock CORE mutex seize. - * - * When doing test coverage analysis or trying to minimize the code - * space for RTEMS, it is often helpful to not inline this method - * multiple times. It is fairly large and has a high branch complexity - * which makes it harder to get full binary test coverage. - * - * @param[in] the_mutex will attempt to lock - * @param[in] _executing points to the executing thread - * @param[in] level_p is the interrupt level - */ - int _CORE_mutex_Seize_interrupt_trylock( - CORE_mutex_Control *the_mutex, - Thread_Control *executing, - ISR_lock_Context *lock_context - ); -#else - /** - * The default is to favor speed and inlining this definitely saves - * a few instructions. This is very important for mutex performance. - * - * @param[in] _mutex will attempt to lock - * @param[in] _executing points to the executing thread - * @param[in] _lock_context is the interrupt level - */ - #define _CORE_mutex_Seize_interrupt_trylock( _mutex, _executing, _lock_context ) \ - _CORE_mutex_Seize_interrupt_trylock_body( _mutex, _executing, _lock_context ) -#endif - /** * @brief Performs the blocking portion of a mutex obtain. * @@ -213,6 +155,163 @@ void _CORE_mutex_Seize_interrupt_blocking( && (_wait) \ && (_System_state_Get() >= SYSTEM_STATE_UP)) +/** + * @brief Is mutex locked. + * + * This routine returns true if the mutex specified is locked and false + * otherwise. + * + * @param[in] the_mutex is the mutex to check. + * + * @retval true The mutex is locked. + * @retval false The mutex is not locked. + */ +RTEMS_INLINE_ROUTINE bool _CORE_mutex_Is_locked( + const CORE_mutex_Control *the_mutex +) +{ + return the_mutex->holder != NULL; +} + +/** + * @brief Does mutex use priority inheritance. + * + * This routine returns true if the mutex's wait discipline is + * INHERIT_PRIORITY and false otherwise. + * + * @param[in] the_attribute is the attribute set of the mutex. + * + * @retval true The mutex is using priority inheritance. + * @retval false The mutex is not using priority inheritance. + */ +RTEMS_INLINE_ROUTINE bool _CORE_mutex_Is_inherit_priority( + const CORE_mutex_Attributes *the_attribute +) +{ + return the_attribute->discipline == CORE_MUTEX_DISCIPLINES_PRIORITY_INHERIT; +} + +/** + * @brief Does mutex use priority ceiling. + * + * This routine returns true if the mutex's wait discipline is + * PRIORITY_CEILING and false otherwise. + * + * @param[in] the_attribute is the attribute set of the mutex. + * + * @retval true The mutex is using priority ceiling. + * @retval false The mutex is not using priority ceiling. + */ +RTEMS_INLINE_ROUTINE bool _CORE_mutex_Is_priority_ceiling( + const CORE_mutex_Attributes *the_attribute +) +{ + return the_attribute->discipline == CORE_MUTEX_DISCIPLINES_PRIORITY_CEILING; +} + +/** + * @brief Attempt to receive a unit from the_mutex. + * + * This routine attempts to receive a unit from the_mutex. + * If a unit is available or if the wait flag is false, then the routine + * returns. Otherwise, the calling task is blocked until a unit becomes + * available. + * + * @param[in,out] executing The currently executing thread. + * @param[in,out] the_mutex is the mutex to attempt to lock + * @param[in] lock_context is the interrupt level + * + * @retval This routine returns 0 if "trylock" can resolve whether or not + * the mutex is immediately obtained or there was an error attempting to + * get it. It returns 1 to indicate that the caller cannot obtain + * the mutex and will have to block to do so. + */ +RTEMS_INLINE_ROUTINE int _CORE_mutex_Seize_interrupt_trylock( + CORE_mutex_Control *the_mutex, + Thread_Control *executing, + ISR_lock_Context *lock_context +) +{ + /* disabled when you get here */ + + executing->Wait.return_code = CORE_MUTEX_STATUS_SUCCESSFUL; + if ( !_CORE_mutex_Is_locked( the_mutex ) ) { + the_mutex->holder = executing; + the_mutex->nest_count = 1; + if ( _CORE_mutex_Is_inherit_priority( &the_mutex->Attributes ) || + _CORE_mutex_Is_priority_ceiling( &the_mutex->Attributes ) ){ + executing->resource_count++; + } + + if ( !_CORE_mutex_Is_priority_ceiling( &the_mutex->Attributes ) ) { + _CORE_mutex_Release( the_mutex, lock_context ); + return 0; + } /* else must be CORE_MUTEX_DISCIPLINES_PRIORITY_CEILING + * + * we possibly bump the priority of the current holder -- which + * happens to be _Thread_Executing. + */ + { + Priority_Control ceiling; + Priority_Control current; + + ceiling = the_mutex->Attributes.priority_ceiling; + current = executing->current_priority; + if ( current == ceiling ) { + _CORE_mutex_Release( the_mutex, lock_context ); + return 0; + } + + if ( current > ceiling ) { + Per_CPU_Control *cpu_self; + + cpu_self = _Thread_Dispatch_disable_critical( lock_context ); + _CORE_mutex_Release( the_mutex, lock_context ); + _Thread_Raise_priority( executing, ceiling ); + _Thread_Dispatch_enable( cpu_self ); + return 0; + } + /* if ( current < ceiling ) */ { + executing->Wait.return_code = CORE_MUTEX_STATUS_CEILING_VIOLATED; + the_mutex->holder = NULL; + the_mutex->nest_count = 0; /* undo locking above */ + executing->resource_count--; /* undo locking above */ + _CORE_mutex_Release( the_mutex, lock_context ); + return 0; + } + } + return 0; + } + + /* + * At this point, we know the mutex was not available. If this thread + * is the thread that has locked the mutex, let's see if we are allowed + * to nest access. + */ + if ( _Thread_Is_executing( the_mutex->holder ) ) { + switch ( the_mutex->Attributes.lock_nesting_behavior ) { + case CORE_MUTEX_NESTING_ACQUIRES: + the_mutex->nest_count++; + _CORE_mutex_Release( the_mutex, lock_context ); + return 0; + #if defined(RTEMS_POSIX_API) + case CORE_MUTEX_NESTING_IS_ERROR: + executing->Wait.return_code = CORE_MUTEX_STATUS_NESTING_NOT_ALLOWED; + _CORE_mutex_Release( the_mutex, lock_context ); + return 0; + #endif + case CORE_MUTEX_NESTING_BLOCKS: + break; + } + } + + /* + * The mutex is not available and the caller must deal with the possibility + * of blocking. + */ + return 1; +} + /** * @brief Attempt to obtain the mutex. * @@ -239,7 +338,7 @@ void _CORE_mutex_Seize_interrupt_blocking( * * If the caller is willing to wait * then they are blocked. */ -RTEMS_INLINE_ROUTINE void _CORE_mutex_Seize_body( +RTEMS_INLINE_ROUTINE void _CORE_mutex_Seize( CORE_mutex_Control *the_mutex, Thread_Control *executing, bool wait, @@ -271,31 +370,6 @@ RTEMS_INLINE_ROUTINE void _CORE_mutex_Seize_body( } } -/** - * This method is used to obtain a core mutex. - * - * @param[in] _the_mutex is the mutex to attempt to lock - * @param[in] _executing The currently executing thread. - * @param[in] _wait is true if the thread is willing to wait - * @param[in] _timeout is the maximum number of ticks to block - * @param[in] _lock_context is a temporary variable used to contain the ISR - * disable level cookie - */ -#if defined(__RTEMS_DO_NOT_INLINE_CORE_MUTEX_SEIZE__) - void _CORE_mutex_Seize( - CORE_mutex_Control *_the_mutex, - Thread_Control *_executing, - bool _wait, - Watchdog_Interval _timeout, - ISR_lock_Context *_lock_context - ); -#else - #define _CORE_mutex_Seize( \ - _the_mutex, _executing, _wait, _timeout, _lock_context ) \ - _CORE_mutex_Seize_body( \ - _the_mutex, _executing, _wait, _timeout, _lock_context ) -#endif - CORE_mutex_Status _CORE_mutex_Do_surrender( CORE_mutex_Control *the_mutex, #if defined(RTEMS_MULTIPROCESSING) @@ -360,24 +434,6 @@ Thread_Control *_CORE_mutex_Unsatisfied_nowait( lock_context \ ) -/** - * @brief Is mutex locked. - * - * This routine returns true if the mutex specified is locked and false - * otherwise. - * - * @param[in] the_mutex is the mutex to check. - * - * @retval true The mutex is locked. - * @retval false The mutex is not locked. - */ -RTEMS_INLINE_ROUTINE bool _CORE_mutex_Is_locked( - const CORE_mutex_Control *the_mutex -) -{ - return the_mutex->holder != NULL; -} - RTEMS_INLINE_ROUTINE bool _CORE_mutex_Is_owner( const CORE_mutex_Control *the_mutex, const Thread_Control *the_thread @@ -423,137 +479,6 @@ RTEMS_INLINE_ROUTINE bool _CORE_mutex_Is_priority( return the_attribute->discipline == CORE_MUTEX_DISCIPLINES_PRIORITY; } -/** - * @brief Does mutex use priority inheritance. - * - * This routine returns true if the mutex's wait discipline is - * INHERIT_PRIORITY and false otherwise. - * - * @param[in] the_attribute is the attribute set of the mutex. - * - * @retval true The mutex is using priority inheritance. - * @retval false The mutex is not using priority inheritance. - */ -RTEMS_INLINE_ROUTINE bool _CORE_mutex_Is_inherit_priority( - const CORE_mutex_Attributes *the_attribute -) -{ - return the_attribute->discipline == CORE_MUTEX_DISCIPLINES_PRIORITY_INHERIT; -} - -/** - * @brief Does mutex use priority ceiling. - * - * This routine returns true if the mutex's wait discipline is - * PRIORITY_CEILING and false otherwise. - * - * @param[in] the_attribute is the attribute set of the mutex. - * - * @retval true The mutex is using priority ceiling. - * @retval false The mutex is not using priority ceiling. - */ -RTEMS_INLINE_ROUTINE bool _CORE_mutex_Is_priority_ceiling( - const CORE_mutex_Attributes *the_attribute -) -{ - return the_attribute->discipline == CORE_MUTEX_DISCIPLINES_PRIORITY_CEILING; -} - -/* - * Seize Mutex with Quick Success Path - * - * NOTE: There is no MACRO version of this routine. A body is in - * coremutexseize.c that is duplicated from the .inl by hand. - * - * NOTE: The Doxygen for this routine is in the .h file. - */ - -RTEMS_INLINE_ROUTINE int _CORE_mutex_Seize_interrupt_trylock_body( - CORE_mutex_Control *the_mutex, - Thread_Control *executing, - ISR_lock_Context *lock_context -) -{ - /* disabled when you get here */ - - executing->Wait.return_code = CORE_MUTEX_STATUS_SUCCESSFUL; - if ( !_CORE_mutex_Is_locked( the_mutex ) ) { - the_mutex->holder = executing; - the_mutex->nest_count = 1; - if ( _CORE_mutex_Is_inherit_priority( &the_mutex->Attributes ) || - _CORE_mutex_Is_priority_ceiling( &the_mutex->Attributes ) ){ - executing->resource_count++; - } - - if ( !_CORE_mutex_Is_priority_ceiling( &the_mutex->Attributes ) ) { - _CORE_mutex_Release( the_mutex, lock_context ); - return 0; - } /* else must be CORE_MUTEX_DISCIPLINES_PRIORITY_CEILING - * - * we possibly bump the priority of the current holder -- which - * happens to be _Thread_Executing. - */ - { - Priority_Control ceiling; - Priority_Control current; - - ceiling = the_mutex->Attributes.priority_ceiling; - current = executing->current_priority; - if ( current == ceiling ) { - _CORE_mutex_Release( the_mutex, lock_context ); - return 0; - } - - if ( current > ceiling ) { - Per_CPU_Control *cpu_self; - - cpu_self = _Thread_Dispatch_disable_critical( lock_context ); - _CORE_mutex_Release( the_mutex, lock_context ); - _Thread_Raise_priority( executing, ceiling ); - _Thread_Dispatch_enable( cpu_self ); - return 0; - } - /* if ( current < ceiling ) */ { - executing->Wait.return_code = CORE_MUTEX_STATUS_CEILING_VIOLATED; - the_mutex->holder = NULL; - the_mutex->nest_count = 0; /* undo locking above */ - executing->resource_count--; /* undo locking above */ - _CORE_mutex_Release( the_mutex, lock_context ); - return 0; - } - } - return 0; - } - - /* - * At this point, we know the mutex was not available. If this thread - * is the thread that has locked the mutex, let's see if we are allowed - * to nest access. - */ - if ( _Thread_Is_executing( the_mutex->holder ) ) { - switch ( the_mutex->Attributes.lock_nesting_behavior ) { - case CORE_MUTEX_NESTING_ACQUIRES: - the_mutex->nest_count++; - _CORE_mutex_Release( the_mutex, lock_context ); - return 0; - #if defined(RTEMS_POSIX_API) - case CORE_MUTEX_NESTING_IS_ERROR: - executing->Wait.return_code = CORE_MUTEX_STATUS_NESTING_NOT_ALLOWED; - _CORE_mutex_Release( the_mutex, lock_context ); - return 0; - #endif - case CORE_MUTEX_NESTING_BLOCKS: - break; - } - } - - /* - * The mutex is not available and the caller must deal with the possibility - * of blocking. - */ - return 1; -} - /** @} */ #ifdef __cplusplus -- cgit v1.2.3