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-1999. |
---|
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; |
---|
52 | Thread_Control *executing; |
---|
53 | |
---|
54 | executing = _Thread_Executing; |
---|
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 | |
---|
64 | if ( !_Objects_Are_ids_equal( |
---|
65 | _Thread_Executing->Object.id, the_mutex->holder_id ) ) { |
---|
66 | |
---|
67 | switch ( the_mutex->Attributes.discipline ) { |
---|
68 | case CORE_MUTEX_DISCIPLINES_FIFO: |
---|
69 | case CORE_MUTEX_DISCIPLINES_PRIORITY: |
---|
70 | break; |
---|
71 | case CORE_MUTEX_DISCIPLINES_PRIORITY_CEILING: |
---|
72 | case CORE_MUTEX_DISCIPLINES_PRIORITY_INHERIT: |
---|
73 | return( CORE_MUTEX_STATUS_NOT_OWNER_OF_RESOURCE ); |
---|
74 | break; |
---|
75 | } |
---|
76 | } |
---|
77 | |
---|
78 | the_mutex->nest_count--; |
---|
79 | |
---|
80 | if ( the_mutex->nest_count != 0 ) |
---|
81 | return( CORE_MUTEX_STATUS_SUCCESSFUL ); |
---|
82 | |
---|
83 | _Thread_Executing->resource_count--; |
---|
84 | the_mutex->holder = NULL; |
---|
85 | the_mutex->holder_id = 0; |
---|
86 | |
---|
87 | /* |
---|
88 | * Whether or not someone is waiting for the mutex, an |
---|
89 | * inherited priority must be lowered if this is the last |
---|
90 | * mutex (i.e. resource) this task has. |
---|
91 | */ |
---|
92 | |
---|
93 | switch ( the_mutex->Attributes.discipline ) { |
---|
94 | case CORE_MUTEX_DISCIPLINES_FIFO: |
---|
95 | case CORE_MUTEX_DISCIPLINES_PRIORITY: |
---|
96 | break; |
---|
97 | case CORE_MUTEX_DISCIPLINES_PRIORITY_CEILING: |
---|
98 | case CORE_MUTEX_DISCIPLINES_PRIORITY_INHERIT: |
---|
99 | if ( executing->resource_count == 0 && |
---|
100 | executing->real_priority != executing->current_priority ) { |
---|
101 | _Thread_Change_priority( executing, executing->real_priority, TRUE ); |
---|
102 | } |
---|
103 | break; |
---|
104 | } |
---|
105 | |
---|
106 | |
---|
107 | if ( ( the_thread = _Thread_queue_Dequeue( &the_mutex->Wait_queue ) ) ) { |
---|
108 | |
---|
109 | #if defined(RTEMS_MULTIPROCESSING) |
---|
110 | if ( !_Objects_Is_local_id( the_thread->Object.id ) ) { |
---|
111 | |
---|
112 | the_mutex->holder = NULL; |
---|
113 | the_mutex->holder_id = the_thread->Object.id; |
---|
114 | the_mutex->nest_count = 1; |
---|
115 | |
---|
116 | ( *api_mutex_mp_support)( the_thread, id ); |
---|
117 | |
---|
118 | } else |
---|
119 | #endif |
---|
120 | { |
---|
121 | |
---|
122 | the_mutex->holder = the_thread; |
---|
123 | the_mutex->holder_id = the_thread->Object.id; |
---|
124 | the_thread->resource_count++; |
---|
125 | the_mutex->nest_count = 1; |
---|
126 | |
---|
127 | /* |
---|
128 | * No special action for priority inheritance or priority ceiling |
---|
129 | * because the_thread is guaranteed to be the highest priority |
---|
130 | * thread waiting for the mutex. |
---|
131 | */ |
---|
132 | } |
---|
133 | } else |
---|
134 | the_mutex->lock = CORE_MUTEX_UNLOCKED; |
---|
135 | |
---|
136 | return( CORE_MUTEX_STATUS_SUCCESSFUL ); |
---|
137 | } |
---|
138 | |
---|