[c4f58558] | 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 |
---|
[c499856] | 16 | * http://www.rtems.org/license/LICENSE. |
---|
[c4f58558] | 17 | */ |
---|
| 18 | |
---|
| 19 | #ifndef _RTEMS_SCORE_CORESEMIMPL_H |
---|
| 20 | #define _RTEMS_SCORE_CORESEMIMPL_H |
---|
| 21 | |
---|
| 22 | #include <rtems/score/coresem.h> |
---|
[d12b313] | 23 | #include <rtems/score/objectimpl.h> |
---|
[0c3edbf] | 24 | #include <rtems/score/threaddispatch.h> |
---|
[a112364] | 25 | #include <rtems/score/threadqimpl.h> |
---|
[9f10911] | 26 | #include <rtems/score/statesimpl.h> |
---|
[c4f58558] | 27 | |
---|
| 28 | #ifdef __cplusplus |
---|
| 29 | extern "C" { |
---|
| 30 | #endif |
---|
| 31 | |
---|
| 32 | /** |
---|
| 33 | * @addtogroup ScoreSemaphore |
---|
| 34 | */ |
---|
| 35 | /**@{**/ |
---|
| 36 | |
---|
| 37 | /** |
---|
| 38 | * Core Semaphore handler return statuses. |
---|
| 39 | */ |
---|
| 40 | typedef enum { |
---|
| 41 | /** This status indicates that the operation completed successfully. */ |
---|
| 42 | CORE_SEMAPHORE_STATUS_SUCCESSFUL, |
---|
| 43 | /** This status indicates that the calling task did not want to block |
---|
| 44 | * and the operation was unable to complete immediately because the |
---|
| 45 | * resource was unavailable. |
---|
| 46 | */ |
---|
| 47 | CORE_SEMAPHORE_STATUS_UNSATISFIED_NOWAIT, |
---|
| 48 | /** This status indicates that the thread was blocked waiting for an |
---|
| 49 | * operation to complete and the semaphore was deleted. |
---|
| 50 | */ |
---|
| 51 | CORE_SEMAPHORE_WAS_DELETED, |
---|
| 52 | /** This status indicates that the calling task was willing to block |
---|
| 53 | * but the operation was unable to complete within the time allotted |
---|
| 54 | * because the resource never became available. |
---|
| 55 | */ |
---|
| 56 | CORE_SEMAPHORE_TIMEOUT, |
---|
| 57 | /** This status indicates that an attempt was made to unlock the semaphore |
---|
| 58 | * and this would have made its count greater than that allowed. |
---|
| 59 | */ |
---|
| 60 | CORE_SEMAPHORE_MAXIMUM_COUNT_EXCEEDED |
---|
| 61 | } CORE_semaphore_Status; |
---|
| 62 | |
---|
| 63 | /** |
---|
| 64 | * @brief Core semaphore last status value. |
---|
| 65 | * |
---|
| 66 | * This is the last status value. |
---|
| 67 | */ |
---|
| 68 | #define CORE_SEMAPHORE_STATUS_LAST CORE_SEMAPHORE_MAXIMUM_COUNT_EXCEEDED |
---|
| 69 | |
---|
| 70 | /** |
---|
| 71 | * @brief Initialize the semaphore based on the parameters passed. |
---|
| 72 | * |
---|
| 73 | * This package is the implementation of the CORE Semaphore Handler. |
---|
| 74 | * This core object utilizes standard Dijkstra counting semaphores to provide |
---|
| 75 | * synchronization and mutual exclusion capabilities. |
---|
| 76 | * |
---|
| 77 | * This routine initializes the semaphore based on the parameters passed. |
---|
| 78 | * |
---|
| 79 | * @param[in] the_semaphore is the semaphore to initialize |
---|
[1e1a91ed] | 80 | * @param[in] discipline the blocking discipline |
---|
[c4f58558] | 81 | * @param[in] initial_value is the initial count of the semaphore |
---|
| 82 | */ |
---|
| 83 | void _CORE_semaphore_Initialize( |
---|
[1e1a91ed] | 84 | CORE_semaphore_Control *the_semaphore, |
---|
| 85 | CORE_semaphore_Disciplines discipline, |
---|
| 86 | uint32_t initial_value |
---|
[c4f58558] | 87 | ); |
---|
| 88 | |
---|
[02c4c441] | 89 | RTEMS_INLINE_ROUTINE void _CORE_semaphore_Destroy( |
---|
| 90 | CORE_semaphore_Control *the_semaphore |
---|
| 91 | ) |
---|
| 92 | { |
---|
| 93 | _Thread_queue_Destroy( &the_semaphore->Wait_queue ); |
---|
| 94 | } |
---|
| 95 | |
---|
[8f96581] | 96 | RTEMS_INLINE_ROUTINE CORE_semaphore_Status _CORE_semaphore_Do_surrender( |
---|
| 97 | CORE_semaphore_Control *the_semaphore, |
---|
| 98 | #if defined(RTEMS_MULTIPROCESSING) |
---|
| 99 | Thread_queue_MP_callout mp_callout, |
---|
| 100 | Objects_Id mp_id, |
---|
| 101 | #endif |
---|
| 102 | ISR_lock_Context *lock_context |
---|
[d12b313] | 103 | ) |
---|
| 104 | { |
---|
| 105 | Thread_Control *the_thread; |
---|
| 106 | CORE_semaphore_Status status; |
---|
| 107 | |
---|
| 108 | status = CORE_SEMAPHORE_STATUS_SUCCESSFUL; |
---|
| 109 | |
---|
| 110 | _Thread_queue_Acquire_critical( &the_semaphore->Wait_queue, lock_context ); |
---|
| 111 | |
---|
[1e1a91ed] | 112 | the_thread = _Thread_queue_First_locked( |
---|
| 113 | &the_semaphore->Wait_queue, |
---|
| 114 | the_semaphore->operations |
---|
| 115 | ); |
---|
[d12b313] | 116 | if ( the_thread != NULL ) { |
---|
| 117 | _Thread_queue_Extract_critical( |
---|
[e2735012] | 118 | &the_semaphore->Wait_queue.Queue, |
---|
[1e1a91ed] | 119 | the_semaphore->operations, |
---|
[d12b313] | 120 | the_thread, |
---|
[8f96581] | 121 | mp_callout, |
---|
| 122 | mp_id, |
---|
[d12b313] | 123 | lock_context |
---|
| 124 | ); |
---|
| 125 | } else { |
---|
[97bbf02] | 126 | if ( the_semaphore->count < UINT32_MAX ) |
---|
[d12b313] | 127 | the_semaphore->count += 1; |
---|
| 128 | else |
---|
| 129 | status = CORE_SEMAPHORE_MAXIMUM_COUNT_EXCEEDED; |
---|
| 130 | |
---|
| 131 | _Thread_queue_Release( &the_semaphore->Wait_queue, lock_context ); |
---|
| 132 | } |
---|
| 133 | |
---|
| 134 | return status; |
---|
| 135 | } |
---|
[c4f58558] | 136 | |
---|
[8f96581] | 137 | /** |
---|
| 138 | * @brief Surrender a unit to a semaphore. |
---|
| 139 | * |
---|
| 140 | * This routine frees a unit to the semaphore. If a task was blocked waiting |
---|
| 141 | * for a unit from this semaphore, then that task will be readied and the unit |
---|
| 142 | * given to that task. Otherwise, the unit will be returned to the semaphore. |
---|
| 143 | * |
---|
| 144 | * @param[in] the_semaphore is the semaphore to surrender |
---|
| 145 | * @param[in] mp_callout is the routine to invoke if the |
---|
| 146 | * thread unblocked is remote |
---|
| 147 | * @param[in] mp_id is the Id of the API level Semaphore object associated |
---|
| 148 | * with this instance of a SuperCore Semaphore |
---|
| 149 | * @param[in] lock_context is a temporary variable used to contain the ISR |
---|
| 150 | * disable level cookie |
---|
| 151 | * |
---|
| 152 | * @retval an indication of whether the routine succeeded or failed |
---|
| 153 | */ |
---|
| 154 | #if defined(RTEMS_MULTIPROCESSING) |
---|
| 155 | #define _CORE_semaphore_Surrender( \ |
---|
| 156 | the_semaphore, \ |
---|
| 157 | mp_callout, \ |
---|
| 158 | mp_id, \ |
---|
| 159 | lock_context \ |
---|
| 160 | ) \ |
---|
| 161 | _CORE_semaphore_Do_surrender( \ |
---|
| 162 | the_semaphore, \ |
---|
| 163 | mp_callout, \ |
---|
| 164 | mp_id, \ |
---|
| 165 | lock_context \ |
---|
| 166 | ) |
---|
| 167 | #else |
---|
| 168 | #define _CORE_semaphore_Surrender( \ |
---|
| 169 | the_semaphore, \ |
---|
| 170 | mp_callout, \ |
---|
| 171 | mp_id, \ |
---|
| 172 | lock_context \ |
---|
| 173 | ) \ |
---|
| 174 | _CORE_semaphore_Do_surrender( \ |
---|
| 175 | the_semaphore, \ |
---|
| 176 | lock_context \ |
---|
| 177 | ) |
---|
| 178 | #endif |
---|
| 179 | |
---|
[9809d6e0] | 180 | /* Must be a macro due to the multiprocessing dependent parameters */ |
---|
| 181 | #define _CORE_semaphore_Flush( \ |
---|
| 182 | the_semaphore, \ |
---|
| 183 | status, \ |
---|
| 184 | mp_callout, \ |
---|
| 185 | mp_id \ |
---|
| 186 | ) \ |
---|
| 187 | _Thread_queue_Flush( \ |
---|
| 188 | &( the_semaphore )->Wait_queue, \ |
---|
| 189 | ( the_semaphore )->operations, \ |
---|
| 190 | status, \ |
---|
| 191 | mp_callout, \ |
---|
| 192 | mp_id \ |
---|
| 193 | ) |
---|
[c4f58558] | 194 | |
---|
| 195 | /** |
---|
| 196 | * This routine returns the current count associated with the semaphore. |
---|
| 197 | * |
---|
| 198 | * @param[in] the_semaphore is the semaphore to obtain the count of |
---|
| 199 | * |
---|
| 200 | * @return the current count of this semaphore |
---|
| 201 | */ |
---|
| 202 | RTEMS_INLINE_ROUTINE uint32_t _CORE_semaphore_Get_count( |
---|
| 203 | CORE_semaphore_Control *the_semaphore |
---|
| 204 | ) |
---|
| 205 | { |
---|
| 206 | return the_semaphore->count; |
---|
| 207 | } |
---|
| 208 | |
---|
| 209 | /** |
---|
| 210 | * This routine attempts to receive a unit from the_semaphore. |
---|
| 211 | * If a unit is available or if the wait flag is false, then the routine |
---|
| 212 | * returns. Otherwise, the calling task is blocked until a unit becomes |
---|
| 213 | * available. |
---|
| 214 | * |
---|
| 215 | * @param[in] the_semaphore is the semaphore to obtain |
---|
[e36f2b8] | 216 | * @param[in,out] executing The currently executing thread. |
---|
[c4f58558] | 217 | * @param[in] id is the Id of the owning API level Semaphore object |
---|
| 218 | * @param[in] wait is true if the thread is willing to wait |
---|
| 219 | * @param[in] timeout is the maximum number of ticks to block |
---|
[4db0ae8e] | 220 | * @param[in] lock_context is a temporary variable used to contain the ISR |
---|
[c4f58558] | 221 | * disable level cookie |
---|
| 222 | * |
---|
| 223 | * @note There is currently no MACRO version of this routine. |
---|
| 224 | */ |
---|
[cfa5aab] | 225 | RTEMS_INLINE_ROUTINE void _CORE_semaphore_Seize( |
---|
[c4f58558] | 226 | CORE_semaphore_Control *the_semaphore, |
---|
[e36f2b8] | 227 | Thread_Control *executing, |
---|
[c4f58558] | 228 | Objects_Id id, |
---|
| 229 | bool wait, |
---|
| 230 | Watchdog_Interval timeout, |
---|
[4db0ae8e] | 231 | ISR_lock_Context *lock_context |
---|
[c4f58558] | 232 | ) |
---|
| 233 | { |
---|
| 234 | /* disabled when you get here */ |
---|
| 235 | |
---|
| 236 | executing->Wait.return_code = CORE_SEMAPHORE_STATUS_SUCCESSFUL; |
---|
[e76c517] | 237 | _Thread_queue_Acquire_critical( &the_semaphore->Wait_queue, lock_context ); |
---|
[c4f58558] | 238 | if ( the_semaphore->count != 0 ) { |
---|
| 239 | the_semaphore->count -= 1; |
---|
[e76c517] | 240 | _Thread_queue_Release( &the_semaphore->Wait_queue, lock_context ); |
---|
[c4f58558] | 241 | return; |
---|
| 242 | } |
---|
| 243 | |
---|
| 244 | if ( !wait ) { |
---|
[e76c517] | 245 | _Thread_queue_Release( &the_semaphore->Wait_queue, lock_context ); |
---|
[c4f58558] | 246 | executing->Wait.return_code = CORE_SEMAPHORE_STATUS_UNSATISFIED_NOWAIT; |
---|
| 247 | return; |
---|
| 248 | } |
---|
| 249 | |
---|
[02c4c441] | 250 | _Thread_queue_Enqueue_critical( |
---|
[e2735012] | 251 | &the_semaphore->Wait_queue.Queue, |
---|
[1e1a91ed] | 252 | the_semaphore->operations, |
---|
[9f10911] | 253 | executing, |
---|
| 254 | STATES_WAITING_FOR_SEMAPHORE, |
---|
[02c4c441] | 255 | timeout, |
---|
[f5d6c8b] | 256 | CORE_SEMAPHORE_TIMEOUT, |
---|
[02c4c441] | 257 | lock_context |
---|
[9f10911] | 258 | ); |
---|
[c4f58558] | 259 | } |
---|
| 260 | |
---|
| 261 | /** @} */ |
---|
| 262 | |
---|
| 263 | #ifdef __cplusplus |
---|
| 264 | } |
---|
| 265 | #endif |
---|
| 266 | |
---|
| 267 | #endif |
---|
| 268 | /* end of include file */ |
---|