[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 | * |
---|
[08311cc3] | 9 | * COPYRIGHT (c) 1989-1999. |
---|
[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 |
---|
| 14 | * http://www.OARcorp.com/rtems/license.html. |
---|
| 15 | * |
---|
| 16 | * $Id$ |
---|
| 17 | */ |
---|
| 18 | |
---|
| 19 | #include <rtems/system.h> |
---|
| 20 | #include <rtems/score/isr.h> |
---|
| 21 | #include <rtems/score/coremutex.h> |
---|
| 22 | #include <rtems/score/states.h> |
---|
| 23 | #include <rtems/score/thread.h> |
---|
| 24 | #include <rtems/score/threadq.h> |
---|
| 25 | |
---|
| 26 | /* |
---|
| 27 | * _CORE_mutex_Surrender |
---|
| 28 | * |
---|
| 29 | * DESCRIPTION: |
---|
| 30 | * |
---|
| 31 | * This routine frees a unit to the mutex. If a task was blocked waiting for |
---|
| 32 | * a unit from this mutex, then that task will be readied and the unit |
---|
| 33 | * given to that task. Otherwise, the unit will be returned to the mutex. |
---|
| 34 | * |
---|
| 35 | * Input parameters: |
---|
| 36 | * the_mutex - the mutex to be flushed |
---|
| 37 | * id - id of parent mutex |
---|
| 38 | * api_mutex_mp_support - api dependent MP support actions |
---|
| 39 | * |
---|
| 40 | * Output parameters: |
---|
| 41 | * CORE_MUTEX_STATUS_SUCCESSFUL - if successful |
---|
| 42 | * core error code - if unsuccessful |
---|
| 43 | */ |
---|
| 44 | |
---|
| 45 | CORE_mutex_Status _CORE_mutex_Surrender( |
---|
| 46 | CORE_mutex_Control *the_mutex, |
---|
| 47 | Objects_Id id, |
---|
| 48 | CORE_mutex_API_mp_support_callout api_mutex_mp_support |
---|
| 49 | ) |
---|
| 50 | { |
---|
| 51 | Thread_Control *the_thread; |
---|
[5870ac55] | 52 | Thread_Control *holder; |
---|
[7cc8d6c] | 53 | |
---|
[5870ac55] | 54 | holder = the_mutex->holder; |
---|
[7cc8d6c] | 55 | |
---|
| 56 | /* |
---|
| 57 | * The following code allows a thread (or ISR) other than the thread |
---|
| 58 | * which acquired the mutex to release that mutex. This is only |
---|
| 59 | * allowed when the mutex in quetion is FIFO or simple Priority |
---|
| 60 | * discipline. But Priority Ceiling or Priority Inheritance mutexes |
---|
| 61 | * must be released by the thread which acquired them. |
---|
| 62 | */ |
---|
| 63 | |
---|
[5870ac55] | 64 | if ( the_mutex->Attributes.only_owner_release ) { |
---|
| 65 | if ( !_Thread_Is_executing( holder ) ) |
---|
| 66 | return( CORE_MUTEX_STATUS_NOT_OWNER_OF_RESOURCE ); |
---|
[7cc8d6c] | 67 | } |
---|
| 68 | |
---|
[7d91d72] | 69 | /* XXX already unlocked -- not right status */ |
---|
| 70 | |
---|
| 71 | if ( !the_mutex->nest_count ) |
---|
| 72 | return( CORE_MUTEX_STATUS_SUCCESSFUL ); |
---|
| 73 | |
---|
[7cc8d6c] | 74 | the_mutex->nest_count--; |
---|
| 75 | |
---|
[db6ec79e] | 76 | if ( the_mutex->nest_count != 0 ) { |
---|
[5870ac55] | 77 | switch ( the_mutex->Attributes.lock_nesting_behavior ) { |
---|
| 78 | case CORE_MUTEX_NESTING_ACQUIRES: |
---|
| 79 | return CORE_MUTEX_STATUS_SUCCESSFUL; |
---|
| 80 | case CORE_MUTEX_NESTING_IS_ERROR: |
---|
| 81 | /* should never occur */ |
---|
| 82 | return CORE_MUTEX_STATUS_NESTING_NOT_ALLOWED; |
---|
| 83 | case CORE_MUTEX_NESTING_BLOCKS: |
---|
[e7d76dc] | 84 | /* Currently no API exercises this behavior. */ |
---|
[5870ac55] | 85 | break; |
---|
| 86 | } |
---|
[db6ec79e] | 87 | } |
---|
[7cc8d6c] | 88 | |
---|
| 89 | _Thread_Executing->resource_count--; |
---|
| 90 | the_mutex->holder = NULL; |
---|
| 91 | the_mutex->holder_id = 0; |
---|
| 92 | |
---|
| 93 | /* |
---|
| 94 | * Whether or not someone is waiting for the mutex, an |
---|
| 95 | * inherited priority must be lowered if this is the last |
---|
| 96 | * mutex (i.e. resource) this task has. |
---|
| 97 | */ |
---|
| 98 | |
---|
| 99 | switch ( the_mutex->Attributes.discipline ) { |
---|
| 100 | case CORE_MUTEX_DISCIPLINES_FIFO: |
---|
| 101 | case CORE_MUTEX_DISCIPLINES_PRIORITY: |
---|
| 102 | break; |
---|
| 103 | case CORE_MUTEX_DISCIPLINES_PRIORITY_CEILING: |
---|
| 104 | case CORE_MUTEX_DISCIPLINES_PRIORITY_INHERIT: |
---|
[5870ac55] | 105 | if ( holder->resource_count == 0 && |
---|
| 106 | holder->real_priority != holder->current_priority ) { |
---|
| 107 | _Thread_Change_priority( holder, holder->real_priority, TRUE ); |
---|
[7cc8d6c] | 108 | } |
---|
| 109 | break; |
---|
| 110 | } |
---|
| 111 | |
---|
| 112 | |
---|
| 113 | if ( ( the_thread = _Thread_queue_Dequeue( &the_mutex->Wait_queue ) ) ) { |
---|
| 114 | |
---|
| 115 | #if defined(RTEMS_MULTIPROCESSING) |
---|
| 116 | if ( !_Objects_Is_local_id( the_thread->Object.id ) ) { |
---|
| 117 | |
---|
| 118 | the_mutex->holder = NULL; |
---|
| 119 | the_mutex->holder_id = the_thread->Object.id; |
---|
| 120 | the_mutex->nest_count = 1; |
---|
| 121 | |
---|
| 122 | ( *api_mutex_mp_support)( the_thread, id ); |
---|
| 123 | |
---|
| 124 | } else |
---|
| 125 | #endif |
---|
| 126 | { |
---|
| 127 | |
---|
| 128 | the_mutex->holder = the_thread; |
---|
| 129 | the_mutex->holder_id = the_thread->Object.id; |
---|
| 130 | the_thread->resource_count++; |
---|
| 131 | the_mutex->nest_count = 1; |
---|
| 132 | |
---|
| 133 | /* |
---|
| 134 | * No special action for priority inheritance or priority ceiling |
---|
| 135 | * because the_thread is guaranteed to be the highest priority |
---|
| 136 | * thread waiting for the mutex. |
---|
| 137 | */ |
---|
| 138 | } |
---|
| 139 | } else |
---|
| 140 | the_mutex->lock = CORE_MUTEX_UNLOCKED; |
---|
| 141 | |
---|
| 142 | return( CORE_MUTEX_STATUS_SUCCESSFUL ); |
---|
| 143 | } |
---|
| 144 | |
---|