1 | /** |
---|
2 | * @file |
---|
3 | * |
---|
4 | * @brief Inlined Routines Associated with the SuperCore Semaphore |
---|
5 | * |
---|
6 | * This include file contains all of the inlined routines associated |
---|
7 | * with the SuperCore semaphore. |
---|
8 | */ |
---|
9 | |
---|
10 | /* |
---|
11 | * COPYRIGHT (c) 1989-2006. |
---|
12 | * On-Line Applications Research Corporation (OAR). |
---|
13 | * |
---|
14 | * The license and distribution terms for this file may be |
---|
15 | * found in the file LICENSE in this distribution or at |
---|
16 | * http://www.rtems.org/license/LICENSE. |
---|
17 | */ |
---|
18 | |
---|
19 | #ifndef _RTEMS_SCORE_CORESEMIMPL_H |
---|
20 | #define _RTEMS_SCORE_CORESEMIMPL_H |
---|
21 | |
---|
22 | #include <rtems/score/coresem.h> |
---|
23 | #include <rtems/score/objectimpl.h> |
---|
24 | #include <rtems/score/threaddispatch.h> |
---|
25 | #include <rtems/score/threadimpl.h> |
---|
26 | #include <rtems/score/threadqimpl.h> |
---|
27 | #include <rtems/score/statesimpl.h> |
---|
28 | #include <rtems/score/status.h> |
---|
29 | |
---|
30 | #ifdef __cplusplus |
---|
31 | extern "C" { |
---|
32 | #endif |
---|
33 | |
---|
34 | /** |
---|
35 | * @addtogroup ScoreSemaphore |
---|
36 | */ |
---|
37 | /**@{**/ |
---|
38 | |
---|
39 | /** |
---|
40 | * @brief Initialize the semaphore based on the parameters passed. |
---|
41 | * |
---|
42 | * This package is the implementation of the CORE Semaphore Handler. |
---|
43 | * This core object utilizes standard Dijkstra counting semaphores to provide |
---|
44 | * synchronization and mutual exclusion capabilities. |
---|
45 | * |
---|
46 | * This routine initializes the semaphore based on the parameters passed. |
---|
47 | * |
---|
48 | * @param[in] the_semaphore is the semaphore to initialize |
---|
49 | * @param[in] discipline the blocking discipline |
---|
50 | * @param[in] initial_value is the initial count of the semaphore |
---|
51 | */ |
---|
52 | void _CORE_semaphore_Initialize( |
---|
53 | CORE_semaphore_Control *the_semaphore, |
---|
54 | CORE_semaphore_Disciplines discipline, |
---|
55 | uint32_t initial_value |
---|
56 | ); |
---|
57 | |
---|
58 | RTEMS_INLINE_ROUTINE void _CORE_semaphore_Acquire_critical( |
---|
59 | CORE_semaphore_Control *the_semaphore, |
---|
60 | Thread_queue_Context *queue_context |
---|
61 | ) |
---|
62 | { |
---|
63 | _Thread_queue_Acquire_critical( |
---|
64 | &the_semaphore->Wait_queue, |
---|
65 | &queue_context->Lock_context |
---|
66 | ); |
---|
67 | } |
---|
68 | |
---|
69 | RTEMS_INLINE_ROUTINE void _CORE_semaphore_Release( |
---|
70 | CORE_semaphore_Control *the_semaphore, |
---|
71 | Thread_queue_Context *queue_context |
---|
72 | ) |
---|
73 | { |
---|
74 | _Thread_queue_Release( |
---|
75 | &the_semaphore->Wait_queue, |
---|
76 | &queue_context->Lock_context |
---|
77 | ); |
---|
78 | } |
---|
79 | |
---|
80 | RTEMS_INLINE_ROUTINE void _CORE_semaphore_Destroy( |
---|
81 | CORE_semaphore_Control *the_semaphore, |
---|
82 | Thread_queue_Context *queue_context |
---|
83 | ) |
---|
84 | { |
---|
85 | _Thread_queue_Flush_critical( |
---|
86 | &the_semaphore->Wait_queue.Queue, |
---|
87 | the_semaphore->operations, |
---|
88 | _Thread_queue_Flush_status_object_was_deleted, |
---|
89 | queue_context |
---|
90 | ); |
---|
91 | _Thread_queue_Destroy( &the_semaphore->Wait_queue ); |
---|
92 | } |
---|
93 | |
---|
94 | /** |
---|
95 | * @brief Surrender a unit to a semaphore. |
---|
96 | * |
---|
97 | * This routine frees a unit to the semaphore. If a task was blocked waiting |
---|
98 | * for a unit from this semaphore, then that task will be readied and the unit |
---|
99 | * given to that task. Otherwise, the unit will be returned to the semaphore. |
---|
100 | * |
---|
101 | * @param[in] the_semaphore is the semaphore to surrender |
---|
102 | * @param[in] queue_context is a temporary variable used to contain the ISR |
---|
103 | * disable level cookie |
---|
104 | * |
---|
105 | * @retval an indication of whether the routine succeeded or failed |
---|
106 | */ |
---|
107 | RTEMS_INLINE_ROUTINE Status_Control _CORE_semaphore_Surrender( |
---|
108 | CORE_semaphore_Control *the_semaphore, |
---|
109 | uint32_t maximum_count, |
---|
110 | Thread_queue_Context *queue_context |
---|
111 | ) |
---|
112 | { |
---|
113 | Thread_Control *the_thread; |
---|
114 | Status_Control status; |
---|
115 | |
---|
116 | status = STATUS_SUCCESSFUL; |
---|
117 | |
---|
118 | _CORE_semaphore_Acquire_critical( the_semaphore, queue_context ); |
---|
119 | |
---|
120 | the_thread = _Thread_queue_First_locked( |
---|
121 | &the_semaphore->Wait_queue, |
---|
122 | the_semaphore->operations |
---|
123 | ); |
---|
124 | if ( the_thread != NULL ) { |
---|
125 | _Thread_queue_Extract_critical( |
---|
126 | &the_semaphore->Wait_queue.Queue, |
---|
127 | the_semaphore->operations, |
---|
128 | the_thread, |
---|
129 | queue_context |
---|
130 | ); |
---|
131 | } else { |
---|
132 | if ( the_semaphore->count < maximum_count ) |
---|
133 | the_semaphore->count += 1; |
---|
134 | else |
---|
135 | status = STATUS_MAXIMUM_COUNT_EXCEEDED; |
---|
136 | |
---|
137 | _CORE_semaphore_Release( the_semaphore, queue_context ); |
---|
138 | } |
---|
139 | |
---|
140 | return status; |
---|
141 | } |
---|
142 | |
---|
143 | RTEMS_INLINE_ROUTINE void _CORE_semaphore_Flush( |
---|
144 | CORE_semaphore_Control *the_semaphore, |
---|
145 | Thread_queue_Context *queue_context |
---|
146 | ) |
---|
147 | { |
---|
148 | _Thread_queue_Flush_critical( |
---|
149 | &the_semaphore->Wait_queue.Queue, |
---|
150 | the_semaphore->operations, |
---|
151 | _Thread_queue_Flush_status_unavailable, |
---|
152 | queue_context |
---|
153 | ); |
---|
154 | } |
---|
155 | |
---|
156 | /** |
---|
157 | * This routine returns the current count associated with the semaphore. |
---|
158 | * |
---|
159 | * @param[in] the_semaphore is the semaphore to obtain the count of |
---|
160 | * |
---|
161 | * @return the current count of this semaphore |
---|
162 | */ |
---|
163 | RTEMS_INLINE_ROUTINE uint32_t _CORE_semaphore_Get_count( |
---|
164 | CORE_semaphore_Control *the_semaphore |
---|
165 | ) |
---|
166 | { |
---|
167 | return the_semaphore->count; |
---|
168 | } |
---|
169 | |
---|
170 | /** |
---|
171 | * This routine attempts to receive a unit from the_semaphore. |
---|
172 | * If a unit is available or if the wait flag is false, then the routine |
---|
173 | * returns. Otherwise, the calling task is blocked until a unit becomes |
---|
174 | * available. |
---|
175 | * |
---|
176 | * @param[in] the_semaphore is the semaphore to obtain |
---|
177 | * @param[in,out] executing The currently executing thread. |
---|
178 | * @param[in] wait is true if the thread is willing to wait |
---|
179 | * @param[in] timeout is the maximum number of ticks to block |
---|
180 | * @param[in] queue_context is a temporary variable used to contain the ISR |
---|
181 | * disable level cookie |
---|
182 | * |
---|
183 | * @note There is currently no MACRO version of this routine. |
---|
184 | */ |
---|
185 | RTEMS_INLINE_ROUTINE Status_Control _CORE_semaphore_Seize( |
---|
186 | CORE_semaphore_Control *the_semaphore, |
---|
187 | Thread_Control *executing, |
---|
188 | bool wait, |
---|
189 | Watchdog_Interval timeout, |
---|
190 | Thread_queue_Context *queue_context |
---|
191 | ) |
---|
192 | { |
---|
193 | /* disabled when you get here */ |
---|
194 | |
---|
195 | _CORE_semaphore_Acquire_critical( the_semaphore, queue_context ); |
---|
196 | if ( the_semaphore->count != 0 ) { |
---|
197 | the_semaphore->count -= 1; |
---|
198 | _CORE_semaphore_Release( the_semaphore, queue_context ); |
---|
199 | return STATUS_SUCCESSFUL; |
---|
200 | } |
---|
201 | |
---|
202 | if ( !wait ) { |
---|
203 | _CORE_semaphore_Release( the_semaphore, queue_context ); |
---|
204 | return STATUS_UNSATISFIED; |
---|
205 | } |
---|
206 | |
---|
207 | _Thread_queue_Enqueue_critical( |
---|
208 | &the_semaphore->Wait_queue.Queue, |
---|
209 | the_semaphore->operations, |
---|
210 | executing, |
---|
211 | STATES_WAITING_FOR_SEMAPHORE, |
---|
212 | timeout, |
---|
213 | &queue_context->Lock_context |
---|
214 | ); |
---|
215 | return _Thread_Wait_get_status( executing ); |
---|
216 | } |
---|
217 | |
---|
218 | /** @} */ |
---|
219 | |
---|
220 | #ifdef __cplusplus |
---|
221 | } |
---|
222 | #endif |
---|
223 | |
---|
224 | #endif |
---|
225 | /* end of include file */ |
---|