1 | /** |
---|
2 | * @file |
---|
3 | * |
---|
4 | * @brief RTEMS Obtain Semaphore |
---|
5 | * @ingroup ClassicSem |
---|
6 | */ |
---|
7 | |
---|
8 | /* |
---|
9 | * COPYRIGHT (c) 1989-2014. |
---|
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/rtems/semimpl.h> |
---|
22 | #include <rtems/rtems/optionsimpl.h> |
---|
23 | #include <rtems/rtems/statusimpl.h> |
---|
24 | |
---|
25 | THREAD_QUEUE_OBJECT_ASSERT( |
---|
26 | Semaphore_Control, |
---|
27 | Core_control.Wait_queue, |
---|
28 | SEMAPHORE_CONTROL_GENERIC |
---|
29 | ); |
---|
30 | |
---|
31 | THREAD_QUEUE_OBJECT_ASSERT( |
---|
32 | Semaphore_Control, |
---|
33 | Core_control.Mutex.Recursive.Mutex.Wait_queue, |
---|
34 | SEMAPHORE_CONTROL_MUTEX |
---|
35 | ); |
---|
36 | |
---|
37 | THREAD_QUEUE_OBJECT_ASSERT( |
---|
38 | Semaphore_Control, |
---|
39 | Core_control.Semaphore.Wait_queue, |
---|
40 | SEMAPHORE_CONTROL_SEMAPHORE |
---|
41 | ); |
---|
42 | |
---|
43 | #if defined(RTEMS_SMP) |
---|
44 | THREAD_QUEUE_OBJECT_ASSERT( |
---|
45 | Semaphore_Control, |
---|
46 | Core_control.MRSP.Wait_queue, |
---|
47 | SEMAPHORE_CONTROL_MRSP |
---|
48 | ); |
---|
49 | #endif |
---|
50 | |
---|
51 | rtems_status_code rtems_semaphore_obtain( |
---|
52 | rtems_id id, |
---|
53 | rtems_option option_set, |
---|
54 | rtems_interval timeout |
---|
55 | ) |
---|
56 | { |
---|
57 | Semaphore_Control *the_semaphore; |
---|
58 | Thread_queue_Context queue_context; |
---|
59 | Thread_Control *executing; |
---|
60 | bool wait; |
---|
61 | Status_Control status; |
---|
62 | |
---|
63 | the_semaphore = _Semaphore_Get( id, &queue_context ); |
---|
64 | |
---|
65 | if ( the_semaphore == NULL ) { |
---|
66 | #if defined(RTEMS_MULTIPROCESSING) |
---|
67 | return _Semaphore_MP_Obtain( id, option_set, timeout ); |
---|
68 | #else |
---|
69 | return RTEMS_INVALID_ID; |
---|
70 | #endif |
---|
71 | } |
---|
72 | |
---|
73 | executing = _Thread_Executing; |
---|
74 | wait = !_Options_Is_no_wait( option_set ); |
---|
75 | |
---|
76 | if ( wait ) { |
---|
77 | _Thread_queue_Context_set_enqueue_timeout_ticks( &queue_context, timeout ); |
---|
78 | } else { |
---|
79 | _Thread_queue_Context_set_enqueue_do_nothing_extra( &queue_context ); |
---|
80 | } |
---|
81 | |
---|
82 | switch ( the_semaphore->variant ) { |
---|
83 | case SEMAPHORE_VARIANT_MUTEX_INHERIT_PRIORITY: |
---|
84 | status = _CORE_recursive_mutex_Seize( |
---|
85 | &the_semaphore->Core_control.Mutex.Recursive, |
---|
86 | CORE_MUTEX_TQ_PRIORITY_INHERIT_OPERATIONS, |
---|
87 | executing, |
---|
88 | wait, |
---|
89 | _CORE_recursive_mutex_Seize_nested, |
---|
90 | &queue_context |
---|
91 | ); |
---|
92 | break; |
---|
93 | case SEMAPHORE_VARIANT_MUTEX_PRIORITY_CEILING: |
---|
94 | status = _CORE_ceiling_mutex_Seize( |
---|
95 | &the_semaphore->Core_control.Mutex, |
---|
96 | executing, |
---|
97 | wait, |
---|
98 | _CORE_recursive_mutex_Seize_nested, |
---|
99 | &queue_context |
---|
100 | ); |
---|
101 | break; |
---|
102 | case SEMAPHORE_VARIANT_MUTEX_NO_PROTOCOL: |
---|
103 | status = _CORE_recursive_mutex_Seize( |
---|
104 | &the_semaphore->Core_control.Mutex.Recursive, |
---|
105 | _Semaphore_Get_operations( the_semaphore ), |
---|
106 | executing, |
---|
107 | wait, |
---|
108 | _CORE_recursive_mutex_Seize_nested, |
---|
109 | &queue_context |
---|
110 | ); |
---|
111 | break; |
---|
112 | #if defined(RTEMS_SMP) |
---|
113 | case SEMAPHORE_VARIANT_MRSP: |
---|
114 | status = _MRSP_Seize( |
---|
115 | &the_semaphore->Core_control.MRSP, |
---|
116 | executing, |
---|
117 | wait, |
---|
118 | &queue_context |
---|
119 | ); |
---|
120 | break; |
---|
121 | #endif |
---|
122 | default: |
---|
123 | _Assert( |
---|
124 | the_semaphore->variant == SEMAPHORE_VARIANT_SIMPLE_BINARY |
---|
125 | || the_semaphore->variant == SEMAPHORE_VARIANT_COUNTING |
---|
126 | ); |
---|
127 | status = _CORE_semaphore_Seize( |
---|
128 | &the_semaphore->Core_control.Semaphore, |
---|
129 | _Semaphore_Get_operations( the_semaphore ), |
---|
130 | executing, |
---|
131 | wait, |
---|
132 | &queue_context |
---|
133 | ); |
---|
134 | break; |
---|
135 | } |
---|
136 | |
---|
137 | return _Status_Get( status ); |
---|
138 | } |
---|