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 | * |
---|
9 | * COPYRIGHT (c) 1989-2006. |
---|
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.rtems.com/license/LICENSE. |
---|
15 | * |
---|
16 | * $Id$ |
---|
17 | */ |
---|
18 | |
---|
19 | #if HAVE_CONFIG_H |
---|
20 | #include "config.h" |
---|
21 | #endif |
---|
22 | |
---|
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; |
---|
56 | Thread_Control *holder; |
---|
57 | #ifdef __STRICT_ORDER_MUTEX__ |
---|
58 | Chain_Node *first_node; |
---|
59 | #endif |
---|
60 | holder = the_mutex->holder; |
---|
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. |
---|
68 | */ |
---|
69 | |
---|
70 | if ( the_mutex->Attributes.only_owner_release ) { |
---|
71 | if ( !_Thread_Is_executing( holder ) ) |
---|
72 | return CORE_MUTEX_STATUS_NOT_OWNER_OF_RESOURCE; |
---|
73 | } |
---|
74 | |
---|
75 | /* XXX already unlocked -- not right status */ |
---|
76 | |
---|
77 | if ( !the_mutex->nest_count ) |
---|
78 | return CORE_MUTEX_STATUS_SUCCESSFUL; |
---|
79 | |
---|
80 | the_mutex->nest_count--; |
---|
81 | |
---|
82 | if ( the_mutex->nest_count != 0 ) { |
---|
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: |
---|
90 | /* Currently no API exercises this behavior. */ |
---|
91 | break; |
---|
92 | } |
---|
93 | } |
---|
94 | |
---|
95 | /* |
---|
96 | * Formally release the mutex before possibly transferring it to a |
---|
97 | * blocked thread. |
---|
98 | */ |
---|
99 | if ( _CORE_mutex_Is_inherit_priority( &the_mutex->Attributes ) || |
---|
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 |
---|
110 | holder->resource_count--; |
---|
111 | } |
---|
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 | */ |
---|
120 | if ( _CORE_mutex_Is_inherit_priority( &the_mutex->Attributes ) || |
---|
121 | _CORE_mutex_Is_priority_ceiling( &the_mutex->Attributes ) ) { |
---|
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 |
---|
126 | if ( holder->resource_count == 0 && |
---|
127 | holder->real_priority != holder->current_priority ) { |
---|
128 | _Thread_Change_priority( holder, holder->real_priority, TRUE ); |
---|
129 | } |
---|
130 | } |
---|
131 | |
---|
132 | /* |
---|
133 | * Now we check if another thread was waiting for this mutex. If so, |
---|
134 | * transfer the mutex to that thread. |
---|
135 | */ |
---|
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 ) ) { |
---|
140 | |
---|
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 | |
---|
147 | } else |
---|
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 | |
---|
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: |
---|
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 |
---|
164 | the_thread->resource_count++; |
---|
165 | break; |
---|
166 | case CORE_MUTEX_DISCIPLINES_PRIORITY_CEILING: |
---|
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 |
---|
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 | } |
---|
182 | } |
---|
183 | } else |
---|
184 | the_mutex->lock = CORE_MUTEX_UNLOCKED; |
---|
185 | |
---|
186 | return CORE_MUTEX_STATUS_SUCCESSFUL; |
---|
187 | } |
---|