1 | /** |
---|
2 | * @file |
---|
3 | * |
---|
4 | * @brief Seize Mutex with Blocking |
---|
5 | * @ingroup ScoreMutex |
---|
6 | */ |
---|
7 | |
---|
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.org/license/LICENSE. |
---|
15 | */ |
---|
16 | |
---|
17 | #if HAVE_CONFIG_H |
---|
18 | #include "config.h" |
---|
19 | #endif |
---|
20 | |
---|
21 | #include <rtems/score/coremuteximpl.h> |
---|
22 | #include <rtems/score/statesimpl.h> |
---|
23 | #include <rtems/score/thread.h> |
---|
24 | #include <rtems/score/watchdog.h> |
---|
25 | |
---|
26 | Status_Control _CORE_mutex_Seize_slow( |
---|
27 | CORE_mutex_Control *the_mutex, |
---|
28 | Thread_Control *executing, |
---|
29 | Thread_Control *owner, |
---|
30 | bool wait, |
---|
31 | Thread_queue_Context *queue_context |
---|
32 | ) |
---|
33 | { |
---|
34 | if ( !wait ) { |
---|
35 | _CORE_mutex_Release( the_mutex, queue_context ); |
---|
36 | return STATUS_UNAVAILABLE; |
---|
37 | } |
---|
38 | |
---|
39 | #if !defined(RTEMS_SMP) |
---|
40 | /* |
---|
41 | * We must disable thread dispatching here since we enable the interrupts for |
---|
42 | * priority inheritance mutexes. |
---|
43 | */ |
---|
44 | _Thread_Dispatch_disable(); |
---|
45 | |
---|
46 | /* |
---|
47 | * To enable interrupts here works only since exactly one executing thread |
---|
48 | * exists and only threads are allowed to seize and surrender mutexes with |
---|
49 | * the priority inheritance protocol. On SMP configurations more than one |
---|
50 | * executing thread may exist, so here we must not release the lock, since |
---|
51 | * otherwise the current owner may be no longer the owner of the mutex |
---|
52 | * once we released the lock. |
---|
53 | */ |
---|
54 | _CORE_mutex_Release( the_mutex, queue_context ); |
---|
55 | #endif |
---|
56 | |
---|
57 | #if defined(RTEMS_SMP) |
---|
58 | _Thread_queue_Context_set_expected_level( queue_context, 1 ); |
---|
59 | #else |
---|
60 | _ISR_lock_ISR_disable( &queue_context->Lock_context ); |
---|
61 | _CORE_mutex_Acquire_critical( the_mutex, queue_context ); |
---|
62 | _Thread_queue_Context_set_expected_level( queue_context, 2 ); |
---|
63 | #endif |
---|
64 | |
---|
65 | _Thread_queue_Context_set_deadlock_callout( |
---|
66 | queue_context, |
---|
67 | _Thread_queue_Deadlock_status |
---|
68 | ); |
---|
69 | |
---|
70 | _Thread_queue_Enqueue_critical( |
---|
71 | &the_mutex->Wait_queue.Queue, |
---|
72 | CORE_MUTEX_TQ_PRIORITY_INHERIT_OPERATIONS, |
---|
73 | executing, |
---|
74 | STATES_WAITING_FOR_MUTEX, |
---|
75 | queue_context |
---|
76 | ); |
---|
77 | |
---|
78 | #if !defined(RTEMS_SMP) |
---|
79 | _Thread_Dispatch_enable( _Per_CPU_Get() ); |
---|
80 | #endif |
---|
81 | |
---|
82 | return _Thread_Wait_get_status( executing ); |
---|
83 | } |
---|
84 | |
---|
85 | Status_Control _CORE_mutex_Seize_no_protocol_slow( |
---|
86 | CORE_mutex_Control *the_mutex, |
---|
87 | const Thread_queue_Operations *operations, |
---|
88 | Thread_Control *executing, |
---|
89 | bool wait, |
---|
90 | Thread_queue_Context *queue_context |
---|
91 | ) |
---|
92 | { |
---|
93 | if ( wait ) { |
---|
94 | _Thread_queue_Context_set_expected_level( queue_context, 1 ); |
---|
95 | _Thread_queue_Context_set_deadlock_callout( |
---|
96 | queue_context, |
---|
97 | _Thread_queue_Deadlock_status |
---|
98 | ); |
---|
99 | _Thread_queue_Enqueue_critical( |
---|
100 | &the_mutex->Wait_queue.Queue, |
---|
101 | operations, |
---|
102 | executing, |
---|
103 | STATES_WAITING_FOR_MUTEX, |
---|
104 | queue_context |
---|
105 | ); |
---|
106 | return _Thread_Wait_get_status( executing ); |
---|
107 | } else { |
---|
108 | _CORE_mutex_Release( the_mutex, queue_context ); |
---|
109 | return STATUS_UNAVAILABLE; |
---|
110 | } |
---|
111 | } |
---|