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/threaddispatch.h> |
---|
24 | #include <rtems/score/threadqimpl.h> |
---|
25 | #include <rtems/score/statesimpl.h> |
---|
26 | |
---|
27 | #ifdef __cplusplus |
---|
28 | extern "C" { |
---|
29 | #endif |
---|
30 | |
---|
31 | /** |
---|
32 | * @addtogroup ScoreSemaphore |
---|
33 | */ |
---|
34 | /**@{**/ |
---|
35 | |
---|
36 | /** |
---|
37 | * Core Semaphore handler return statuses. |
---|
38 | */ |
---|
39 | typedef enum { |
---|
40 | /** This status indicates that the operation completed successfully. */ |
---|
41 | CORE_SEMAPHORE_STATUS_SUCCESSFUL, |
---|
42 | /** This status indicates that the calling task did not want to block |
---|
43 | * and the operation was unable to complete immediately because the |
---|
44 | * resource was unavailable. |
---|
45 | */ |
---|
46 | CORE_SEMAPHORE_STATUS_UNSATISFIED_NOWAIT, |
---|
47 | /** This status indicates that the thread was blocked waiting for an |
---|
48 | * operation to complete and the semaphore was deleted. |
---|
49 | */ |
---|
50 | CORE_SEMAPHORE_WAS_DELETED, |
---|
51 | /** This status indicates that the calling task was willing to block |
---|
52 | * but the operation was unable to complete within the time allotted |
---|
53 | * because the resource never became available. |
---|
54 | */ |
---|
55 | CORE_SEMAPHORE_TIMEOUT, |
---|
56 | /** This status indicates that an attempt was made to unlock the semaphore |
---|
57 | * and this would have made its count greater than that allowed. |
---|
58 | */ |
---|
59 | CORE_SEMAPHORE_MAXIMUM_COUNT_EXCEEDED |
---|
60 | } CORE_semaphore_Status; |
---|
61 | |
---|
62 | /** |
---|
63 | * @brief Core semaphore last status value. |
---|
64 | * |
---|
65 | * This is the last status value. |
---|
66 | */ |
---|
67 | #define CORE_SEMAPHORE_STATUS_LAST CORE_SEMAPHORE_MAXIMUM_COUNT_EXCEEDED |
---|
68 | |
---|
69 | /** |
---|
70 | * The following type defines the callout which the API provides |
---|
71 | * to support global/multiprocessor operations on semaphores. |
---|
72 | */ |
---|
73 | typedef void ( *CORE_semaphore_API_mp_support_callout )( |
---|
74 | Thread_Control *, |
---|
75 | Objects_Id |
---|
76 | ); |
---|
77 | |
---|
78 | /** |
---|
79 | * @brief Initialize the semaphore based on the parameters passed. |
---|
80 | * |
---|
81 | * This package is the implementation of the CORE Semaphore Handler. |
---|
82 | * This core object utilizes standard Dijkstra counting semaphores to provide |
---|
83 | * synchronization and mutual exclusion capabilities. |
---|
84 | * |
---|
85 | * This routine initializes the semaphore based on the parameters passed. |
---|
86 | * |
---|
87 | * @param[in] the_semaphore is the semaphore to initialize |
---|
88 | * @param[in] the_semaphore_attributes define the behavior of this instance |
---|
89 | * @param[in] initial_value is the initial count of the semaphore |
---|
90 | */ |
---|
91 | void _CORE_semaphore_Initialize( |
---|
92 | CORE_semaphore_Control *the_semaphore, |
---|
93 | const CORE_semaphore_Attributes *the_semaphore_attributes, |
---|
94 | uint32_t initial_value |
---|
95 | ); |
---|
96 | |
---|
97 | RTEMS_INLINE_ROUTINE void _CORE_semaphore_Destroy( |
---|
98 | CORE_semaphore_Control *the_semaphore |
---|
99 | ) |
---|
100 | { |
---|
101 | _Thread_queue_Destroy( &the_semaphore->Wait_queue ); |
---|
102 | } |
---|
103 | |
---|
104 | /** |
---|
105 | * @brief Surrender a unit to a semaphore. |
---|
106 | * |
---|
107 | * This routine frees a unit to the semaphore. If a task was blocked waiting |
---|
108 | * for a unit from this semaphore, then that task will be readied and the unit |
---|
109 | * given to that task. Otherwise, the unit will be returned to the semaphore. |
---|
110 | * |
---|
111 | * @param[in] the_semaphore is the semaphore to surrender |
---|
112 | * @param[in] id is the Id of the API level Semaphore object associated |
---|
113 | * with this instance of a SuperCore Semaphore |
---|
114 | * @param[in] api_semaphore_mp_support is the routine to invoke if the |
---|
115 | * thread unblocked is remote |
---|
116 | * @param[in] lock_context is a temporary variable used to contain the ISR |
---|
117 | * disable level cookie |
---|
118 | * |
---|
119 | * @retval an indication of whether the routine succeeded or failed |
---|
120 | */ |
---|
121 | CORE_semaphore_Status _CORE_semaphore_Surrender( |
---|
122 | CORE_semaphore_Control *the_semaphore, |
---|
123 | Objects_Id id, |
---|
124 | CORE_semaphore_API_mp_support_callout api_semaphore_mp_support, |
---|
125 | ISR_lock_Context *lock_context |
---|
126 | ); |
---|
127 | |
---|
128 | /** |
---|
129 | * @brief Core semaphore flush. |
---|
130 | * |
---|
131 | * This package is the implementation of the CORE Semaphore Handler. |
---|
132 | * This core object utilizes standard Dijkstra counting semaphores to provide |
---|
133 | * synchronization and mutual exclusion capabilities. |
---|
134 | * |
---|
135 | * This routine assists in the deletion of a semaphore by flushing the |
---|
136 | * associated wait queue. |
---|
137 | * |
---|
138 | * @param[in] the_semaphore is the semaphore to flush |
---|
139 | * @param[in] remote_extract_callout is the routine to invoke if the |
---|
140 | * thread unblocked is remote |
---|
141 | * @param[in] status is the status to be returned to the unblocked thread |
---|
142 | */ |
---|
143 | RTEMS_INLINE_ROUTINE void _CORE_semaphore_Flush( |
---|
144 | CORE_semaphore_Control *the_semaphore, |
---|
145 | Thread_queue_Flush_callout remote_extract_callout, |
---|
146 | uint32_t status |
---|
147 | ) |
---|
148 | { |
---|
149 | _Thread_queue_Flush( |
---|
150 | &the_semaphore->Wait_queue, |
---|
151 | remote_extract_callout, |
---|
152 | status |
---|
153 | ); |
---|
154 | } |
---|
155 | |
---|
156 | /** |
---|
157 | * This function returns true if the priority attribute is |
---|
158 | * enabled in the @a attribute_set and false otherwise. |
---|
159 | * |
---|
160 | * @param[in] the_attribute is the attribute set to test |
---|
161 | * |
---|
162 | * @return true if the priority attribute is enabled |
---|
163 | */ |
---|
164 | RTEMS_INLINE_ROUTINE bool _CORE_semaphore_Is_priority( |
---|
165 | const CORE_semaphore_Attributes *the_attribute |
---|
166 | ) |
---|
167 | { |
---|
168 | return ( the_attribute->discipline == CORE_SEMAPHORE_DISCIPLINES_PRIORITY ); |
---|
169 | } |
---|
170 | |
---|
171 | /** |
---|
172 | * This routine returns the current count associated with the semaphore. |
---|
173 | * |
---|
174 | * @param[in] the_semaphore is the semaphore to obtain the count of |
---|
175 | * |
---|
176 | * @return the current count of this semaphore |
---|
177 | */ |
---|
178 | RTEMS_INLINE_ROUTINE uint32_t _CORE_semaphore_Get_count( |
---|
179 | CORE_semaphore_Control *the_semaphore |
---|
180 | ) |
---|
181 | { |
---|
182 | return the_semaphore->count; |
---|
183 | } |
---|
184 | |
---|
185 | /** |
---|
186 | * This routine attempts to receive a unit from the_semaphore. |
---|
187 | * If a unit is available or if the wait flag is false, then the routine |
---|
188 | * returns. Otherwise, the calling task is blocked until a unit becomes |
---|
189 | * available. |
---|
190 | * |
---|
191 | * @param[in] the_semaphore is the semaphore to obtain |
---|
192 | * @param[in,out] executing The currently executing thread. |
---|
193 | * @param[in] id is the Id of the owning API level Semaphore object |
---|
194 | * @param[in] wait is true if the thread is willing to wait |
---|
195 | * @param[in] timeout is the maximum number of ticks to block |
---|
196 | * @param[in] lock_context is a temporary variable used to contain the ISR |
---|
197 | * disable level cookie |
---|
198 | * |
---|
199 | * @note There is currently no MACRO version of this routine. |
---|
200 | */ |
---|
201 | RTEMS_INLINE_ROUTINE void _CORE_semaphore_Seize( |
---|
202 | CORE_semaphore_Control *the_semaphore, |
---|
203 | Thread_Control *executing, |
---|
204 | Objects_Id id, |
---|
205 | bool wait, |
---|
206 | Watchdog_Interval timeout, |
---|
207 | ISR_lock_Context *lock_context |
---|
208 | ) |
---|
209 | { |
---|
210 | /* disabled when you get here */ |
---|
211 | |
---|
212 | executing->Wait.return_code = CORE_SEMAPHORE_STATUS_SUCCESSFUL; |
---|
213 | _Thread_queue_Acquire_critical( &the_semaphore->Wait_queue, lock_context ); |
---|
214 | if ( the_semaphore->count != 0 ) { |
---|
215 | the_semaphore->count -= 1; |
---|
216 | _Thread_queue_Release( &the_semaphore->Wait_queue, lock_context ); |
---|
217 | return; |
---|
218 | } |
---|
219 | |
---|
220 | if ( !wait ) { |
---|
221 | _Thread_queue_Release( &the_semaphore->Wait_queue, lock_context ); |
---|
222 | executing->Wait.return_code = CORE_SEMAPHORE_STATUS_UNSATISFIED_NOWAIT; |
---|
223 | return; |
---|
224 | } |
---|
225 | |
---|
226 | executing->Wait.id = id; |
---|
227 | _Thread_queue_Enqueue_critical( |
---|
228 | &the_semaphore->Wait_queue, |
---|
229 | executing, |
---|
230 | STATES_WAITING_FOR_SEMAPHORE, |
---|
231 | timeout, |
---|
232 | CORE_SEMAPHORE_TIMEOUT, |
---|
233 | lock_context |
---|
234 | ); |
---|
235 | } |
---|
236 | |
---|
237 | /** @} */ |
---|
238 | |
---|
239 | #ifdef __cplusplus |
---|
240 | } |
---|
241 | #endif |
---|
242 | |
---|
243 | #endif |
---|
244 | /* end of include file */ |
---|