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 | |
---|
25 | Status_Control _CORE_mutex_Seize_slow( |
---|
26 | CORE_mutex_Control *the_mutex, |
---|
27 | Thread_Control *executing, |
---|
28 | Thread_Control *owner, |
---|
29 | bool wait, |
---|
30 | Watchdog_Interval timeout, |
---|
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 | _Thread_Inherit_priority( owner, executing ); |
---|
58 | |
---|
59 | #if defined(RTEMS_SMP) |
---|
60 | _Thread_queue_Context_set_expected_level( queue_context, 1 ); |
---|
61 | #else |
---|
62 | _ISR_lock_ISR_disable( &queue_context->Lock_context ); |
---|
63 | _CORE_mutex_Acquire_critical( the_mutex, queue_context ); |
---|
64 | _Thread_queue_Context_set_expected_level( queue_context, 2 ); |
---|
65 | #endif |
---|
66 | |
---|
67 | _Thread_queue_Enqueue_critical( |
---|
68 | &the_mutex->Wait_queue.Queue, |
---|
69 | CORE_MUTEX_TQ_OPERATIONS, |
---|
70 | executing, |
---|
71 | STATES_WAITING_FOR_MUTEX, |
---|
72 | timeout, |
---|
73 | queue_context |
---|
74 | ); |
---|
75 | |
---|
76 | #if !defined(RTEMS_SMP) |
---|
77 | _Thread_Dispatch_enable( _Per_CPU_Get() ); |
---|
78 | #endif |
---|
79 | |
---|
80 | return _Thread_Wait_get_status( executing ); |
---|
81 | } |
---|
82 | |
---|
83 | Status_Control _CORE_mutex_Seize_no_protocol_slow( |
---|
84 | CORE_mutex_Control *the_mutex, |
---|
85 | const Thread_queue_Operations *operations, |
---|
86 | Thread_Control *executing, |
---|
87 | bool wait, |
---|
88 | Watchdog_Interval timeout, |
---|
89 | Thread_queue_Context *queue_context |
---|
90 | ) |
---|
91 | { |
---|
92 | if ( wait ) { |
---|
93 | _Thread_queue_Context_set_expected_level( queue_context, 1 ); |
---|
94 | _Thread_queue_Enqueue_critical( |
---|
95 | &the_mutex->Wait_queue.Queue, |
---|
96 | operations, |
---|
97 | executing, |
---|
98 | STATES_WAITING_FOR_MUTEX, |
---|
99 | timeout, |
---|
100 | queue_context |
---|
101 | ); |
---|
102 | return _Thread_Wait_get_status( executing ); |
---|
103 | } else { |
---|
104 | _CORE_mutex_Release( the_mutex, queue_context ); |
---|
105 | return STATUS_UNAVAILABLE; |
---|
106 | } |
---|
107 | } |
---|