[7cc8d6c] | 1 | /* |
---|
| 2 | * Mutex Handler |
---|
| 3 | * |
---|
| 4 | * DESCRIPTION: |
---|
| 5 | * |
---|
| 6 | * This package is the implementation of the Mutex Handler. |
---|
| 7 | * This handler provides synchronization and mutual exclusion capabilities. |
---|
| 8 | * |
---|
[96d0b64] | 9 | * COPYRIGHT (c) 1989-2006. |
---|
[7cc8d6c] | 10 | * On-Line Applications Research Corporation (OAR). |
---|
| 11 | * |
---|
| 12 | * The license and distribution terms for this file may be |
---|
| 13 | * found in the file LICENSE in this distribution or at |
---|
[dd687d97] | 14 | * http://www.rtems.com/license/LICENSE. |
---|
[7cc8d6c] | 15 | * |
---|
| 16 | * $Id$ |
---|
| 17 | */ |
---|
| 18 | |
---|
[a8eed23] | 19 | #if HAVE_CONFIG_H |
---|
| 20 | #include "config.h" |
---|
| 21 | #endif |
---|
| 22 | |
---|
[7cc8d6c] | 23 | #include <rtems/system.h> |
---|
| 24 | #include <rtems/score/isr.h> |
---|
| 25 | #include <rtems/score/coremutex.h> |
---|
| 26 | #include <rtems/score/states.h> |
---|
| 27 | #include <rtems/score/thread.h> |
---|
| 28 | #include <rtems/score/threadq.h> |
---|
| 29 | |
---|
| 30 | /* |
---|
| 31 | * _CORE_mutex_Surrender |
---|
| 32 | * |
---|
| 33 | * DESCRIPTION: |
---|
| 34 | * |
---|
| 35 | * This routine frees a unit to the mutex. If a task was blocked waiting for |
---|
| 36 | * a unit from this mutex, then that task will be readied and the unit |
---|
| 37 | * given to that task. Otherwise, the unit will be returned to the mutex. |
---|
| 38 | * |
---|
| 39 | * Input parameters: |
---|
| 40 | * the_mutex - the mutex to be flushed |
---|
| 41 | * id - id of parent mutex |
---|
| 42 | * api_mutex_mp_support - api dependent MP support actions |
---|
| 43 | * |
---|
| 44 | * Output parameters: |
---|
| 45 | * CORE_MUTEX_STATUS_SUCCESSFUL - if successful |
---|
| 46 | * core error code - if unsuccessful |
---|
| 47 | */ |
---|
| 48 | |
---|
| 49 | CORE_mutex_Status _CORE_mutex_Surrender( |
---|
| 50 | CORE_mutex_Control *the_mutex, |
---|
| 51 | Objects_Id id, |
---|
| 52 | CORE_mutex_API_mp_support_callout api_mutex_mp_support |
---|
| 53 | ) |
---|
| 54 | { |
---|
| 55 | Thread_Control *the_thread; |
---|
[5870ac55] | 56 | Thread_Control *holder; |
---|
[fd84982] | 57 | #ifdef __STRICT_ORDER_MUTEX__ |
---|
| 58 | Chain_Node *first_node; |
---|
| 59 | #endif |
---|
[5870ac55] | 60 | holder = the_mutex->holder; |
---|
[7cc8d6c] | 61 | |
---|
| 62 | /* |
---|
| 63 | * The following code allows a thread (or ISR) other than the thread |
---|
| 64 | * which acquired the mutex to release that mutex. This is only |
---|
| 65 | * allowed when the mutex in quetion is FIFO or simple Priority |
---|
| 66 | * discipline. But Priority Ceiling or Priority Inheritance mutexes |
---|
| 67 | * must be released by the thread which acquired them. |
---|
[a0ed4ed] | 68 | */ |
---|
[7cc8d6c] | 69 | |
---|
[5870ac55] | 70 | if ( the_mutex->Attributes.only_owner_release ) { |
---|
| 71 | if ( !_Thread_Is_executing( holder ) ) |
---|
[42ed3004] | 72 | return CORE_MUTEX_STATUS_NOT_OWNER_OF_RESOURCE; |
---|
[7cc8d6c] | 73 | } |
---|
| 74 | |
---|
[7d91d72] | 75 | /* XXX already unlocked -- not right status */ |
---|
| 76 | |
---|
[a0ed4ed] | 77 | if ( !the_mutex->nest_count ) |
---|
[42ed3004] | 78 | return CORE_MUTEX_STATUS_SUCCESSFUL; |
---|
[7d91d72] | 79 | |
---|
[7cc8d6c] | 80 | the_mutex->nest_count--; |
---|
| 81 | |
---|
[db6ec79e] | 82 | if ( the_mutex->nest_count != 0 ) { |
---|
[5870ac55] | 83 | switch ( the_mutex->Attributes.lock_nesting_behavior ) { |
---|
| 84 | case CORE_MUTEX_NESTING_ACQUIRES: |
---|
| 85 | return CORE_MUTEX_STATUS_SUCCESSFUL; |
---|
| 86 | case CORE_MUTEX_NESTING_IS_ERROR: |
---|
| 87 | /* should never occur */ |
---|
| 88 | return CORE_MUTEX_STATUS_NESTING_NOT_ALLOWED; |
---|
| 89 | case CORE_MUTEX_NESTING_BLOCKS: |
---|
[e7d76dc] | 90 | /* Currently no API exercises this behavior. */ |
---|
[5870ac55] | 91 | break; |
---|
| 92 | } |
---|
[db6ec79e] | 93 | } |
---|
[7cc8d6c] | 94 | |
---|
[96d0b64] | 95 | /* |
---|
| 96 | * Formally release the mutex before possibly transferring it to a |
---|
| 97 | * blocked thread. |
---|
| 98 | */ |
---|
[fb1d8f81] | 99 | if ( _CORE_mutex_Is_inherit_priority( &the_mutex->Attributes ) || |
---|
[fd84982] | 100 | _CORE_mutex_Is_priority_ceiling( &the_mutex->Attributes ) ){ |
---|
| 101 | #ifdef __STRICT_ORDER_MUTEX__ |
---|
| 102 | /*Check whether the holder release the mutex in LIFO order |
---|
| 103 | if not return error code*/ |
---|
| 104 | if(holder->lock_mutex.first != &the_mutex->queue.lock_queue){ |
---|
| 105 | the_mutex->nest_count++; |
---|
| 106 | return CORE_MUTEX_RELEASE_NOT_ORDER; |
---|
| 107 | } |
---|
| 108 | first_node = _Chain_Get_first_unprotected(&holder->lock_mutex); |
---|
| 109 | #endif |
---|
[fb1d8f81] | 110 | holder->resource_count--; |
---|
[fd84982] | 111 | } |
---|
[7cc8d6c] | 112 | the_mutex->holder = NULL; |
---|
| 113 | the_mutex->holder_id = 0; |
---|
| 114 | |
---|
| 115 | /* |
---|
| 116 | * Whether or not someone is waiting for the mutex, an |
---|
| 117 | * inherited priority must be lowered if this is the last |
---|
| 118 | * mutex (i.e. resource) this task has. |
---|
| 119 | */ |
---|
[fb1d8f81] | 120 | if ( _CORE_mutex_Is_inherit_priority( &the_mutex->Attributes ) || |
---|
| 121 | _CORE_mutex_Is_priority_ceiling( &the_mutex->Attributes ) ) { |
---|
[fd84982] | 122 | #ifdef __STRICT_ORDER_MUTEX__ |
---|
| 123 | if(the_mutex->queue.priority_before != holder->current_priority) |
---|
| 124 | _Thread_Change_priority(holder,the_mutex->queue.priority_before,TRUE); |
---|
| 125 | #endif |
---|
[a0ed4ed] | 126 | if ( holder->resource_count == 0 && |
---|
[fb1d8f81] | 127 | holder->real_priority != holder->current_priority ) { |
---|
| 128 | _Thread_Change_priority( holder, holder->real_priority, TRUE ); |
---|
| 129 | } |
---|
[7cc8d6c] | 130 | } |
---|
| 131 | |
---|
[96d0b64] | 132 | /* |
---|
| 133 | * Now we check if another thread was waiting for this mutex. If so, |
---|
| 134 | * transfer the mutex to that thread. |
---|
| 135 | */ |
---|
[7cc8d6c] | 136 | if ( ( the_thread = _Thread_queue_Dequeue( &the_mutex->Wait_queue ) ) ) { |
---|
| 137 | |
---|
| 138 | #if defined(RTEMS_MULTIPROCESSING) |
---|
| 139 | if ( !_Objects_Is_local_id( the_thread->Object.id ) ) { |
---|
[05279b84] | 140 | |
---|
[7cc8d6c] | 141 | the_mutex->holder = NULL; |
---|
| 142 | the_mutex->holder_id = the_thread->Object.id; |
---|
| 143 | the_mutex->nest_count = 1; |
---|
| 144 | |
---|
| 145 | ( *api_mutex_mp_support)( the_thread, id ); |
---|
| 146 | |
---|
[a0ed4ed] | 147 | } else |
---|
[7cc8d6c] | 148 | #endif |
---|
| 149 | { |
---|
| 150 | |
---|
| 151 | the_mutex->holder = the_thread; |
---|
| 152 | the_mutex->holder_id = the_thread->Object.id; |
---|
| 153 | the_mutex->nest_count = 1; |
---|
| 154 | |
---|
[83df49f] | 155 | switch ( the_mutex->Attributes.discipline ) { |
---|
| 156 | case CORE_MUTEX_DISCIPLINES_FIFO: |
---|
| 157 | case CORE_MUTEX_DISCIPLINES_PRIORITY: |
---|
| 158 | break; |
---|
| 159 | case CORE_MUTEX_DISCIPLINES_PRIORITY_INHERIT: |
---|
[fd84982] | 160 | #ifdef __STRICT_ORDER_MUTEX__ |
---|
| 161 | _Chain_Prepend_unprotected(&the_thread->lock_mutex,&the_mutex->queue.lock_queue); |
---|
| 162 | the_mutex->queue.priority_before = the_thread->current_priority; |
---|
| 163 | #endif |
---|
[83df49f] | 164 | the_thread->resource_count++; |
---|
| 165 | break; |
---|
| 166 | case CORE_MUTEX_DISCIPLINES_PRIORITY_CEILING: |
---|
[fd84982] | 167 | #ifdef __STRICT_ORDER_MUTEX__ |
---|
| 168 | _Chain_Prepend_unprotected(&the_thread->lock_mutex,&the_mutex->queue.lock_queue); |
---|
| 169 | the_mutex->queue.priority_before = the_thread->current_priority; |
---|
| 170 | #endif |
---|
[83df49f] | 171 | the_thread->resource_count++; |
---|
| 172 | if (the_mutex->Attributes.priority_ceiling < |
---|
| 173 | the_thread->current_priority){ |
---|
| 174 | _Thread_Change_priority( |
---|
| 175 | the_thread, |
---|
| 176 | the_mutex->Attributes.priority_ceiling, |
---|
| 177 | FALSE |
---|
| 178 | ); |
---|
| 179 | } |
---|
| 180 | break; |
---|
| 181 | } |
---|
[7cc8d6c] | 182 | } |
---|
| 183 | } else |
---|
| 184 | the_mutex->lock = CORE_MUTEX_UNLOCKED; |
---|
| 185 | |
---|
[42ed3004] | 186 | return CORE_MUTEX_STATUS_SUCCESSFUL; |
---|
[7cc8d6c] | 187 | } |
---|