[3be0c9a] | 1 | /** |
---|
| 2 | * @file |
---|
| 3 | * |
---|
| 4 | * @ingroup ScoreUserExt |
---|
| 5 | * |
---|
[319cb20] | 6 | * @brief User Extension Handler API |
---|
[3be0c9a] | 7 | */ |
---|
| 8 | |
---|
| 9 | /* |
---|
| 10 | * COPYRIGHT (c) 1989-2009. |
---|
| 11 | * On-Line Applications Research Corporation (OAR). |
---|
| 12 | * |
---|
| 13 | * The license and distribution terms for this file may be |
---|
| 14 | * found in the file LICENSE in this distribution or at |
---|
[c499856] | 15 | * http://www.rtems.org/license/LICENSE. |
---|
[3be0c9a] | 16 | */ |
---|
| 17 | |
---|
| 18 | #ifndef _RTEMS_SCORE_USEREXTIMPL_H |
---|
| 19 | #define _RTEMS_SCORE_USEREXTIMPL_H |
---|
| 20 | |
---|
[963c6c2] | 21 | #include <rtems/score/userextdata.h> |
---|
[6e93dc4a] | 22 | #include <rtems/score/chainimpl.h> |
---|
[a11b98c] | 23 | #include <rtems/score/isrlock.h> |
---|
| 24 | #include <rtems/score/thread.h> |
---|
[48fed9a] | 25 | #include <rtems/score/percpu.h> |
---|
[3be0c9a] | 26 | |
---|
| 27 | #ifdef __cplusplus |
---|
| 28 | extern "C" { |
---|
| 29 | #endif |
---|
| 30 | |
---|
| 31 | /** |
---|
[319cb20] | 32 | * @defgroup ScoreUserExt User Extension Handler |
---|
| 33 | * |
---|
| 34 | * @ingroup Score |
---|
| 35 | * |
---|
[3be0c9a] | 36 | * @addtogroup ScoreUserExt |
---|
| 37 | */ |
---|
[b697bc6] | 38 | /**@{**/ |
---|
[3be0c9a] | 39 | |
---|
[709f38a] | 40 | /** |
---|
| 41 | * @brief Chain iterator for dynamic user extensions. |
---|
| 42 | * |
---|
| 43 | * Since user extensions may delete or restart the executing thread, we must |
---|
| 44 | * clean up registered iterators. |
---|
| 45 | * |
---|
| 46 | * @see _User_extensions_Iterate(), _User_extensions_Destroy_iterators() and |
---|
| 47 | * Thread_Control::last_user_extensions_iterator. |
---|
| 48 | */ |
---|
| 49 | typedef struct User_extensions_Iterator { |
---|
| 50 | Chain_Iterator Iterator; |
---|
| 51 | struct User_extensions_Iterator *previous; |
---|
| 52 | } User_extensions_Iterator; |
---|
| 53 | |
---|
| 54 | typedef struct { |
---|
| 55 | /** |
---|
| 56 | * @brief Active dynamically added user extensions. |
---|
| 57 | */ |
---|
| 58 | Chain_Control Active; |
---|
| 59 | |
---|
| 60 | /** |
---|
| 61 | * @brief Chain iterator registration. |
---|
| 62 | */ |
---|
| 63 | Chain_Iterator_registry Iterators; |
---|
| 64 | |
---|
| 65 | /** |
---|
| 66 | * @brief Lock to protect User_extensions_List::Active and |
---|
| 67 | * User_extensions_List::Iterators. |
---|
| 68 | */ |
---|
| 69 | ISR_LOCK_MEMBER( Lock ) |
---|
| 70 | } User_extensions_List; |
---|
| 71 | |
---|
[3be0c9a] | 72 | /** |
---|
| 73 | * @brief List of active extensions. |
---|
| 74 | */ |
---|
[709f38a] | 75 | extern User_extensions_List _User_extensions_List; |
---|
[3be0c9a] | 76 | |
---|
| 77 | /** |
---|
| 78 | * @brief List of active task switch extensions. |
---|
| 79 | */ |
---|
| 80 | extern Chain_Control _User_extensions_Switches_list; |
---|
| 81 | |
---|
| 82 | /** |
---|
| 83 | * @name Extension Maintainance |
---|
| 84 | */ |
---|
[b697bc6] | 85 | /**@{**/ |
---|
[3be0c9a] | 86 | |
---|
| 87 | void _User_extensions_Handler_initialization( void ); |
---|
| 88 | |
---|
| 89 | void _User_extensions_Add_set( |
---|
| 90 | User_extensions_Control *extension |
---|
| 91 | ); |
---|
| 92 | |
---|
| 93 | RTEMS_INLINE_ROUTINE void _User_extensions_Add_API_set( |
---|
| 94 | User_extensions_Control *extension |
---|
| 95 | ) |
---|
| 96 | { |
---|
| 97 | _User_extensions_Add_set( extension ); |
---|
| 98 | } |
---|
| 99 | |
---|
| 100 | RTEMS_INLINE_ROUTINE void _User_extensions_Add_set_with_table( |
---|
| 101 | User_extensions_Control *extension, |
---|
| 102 | const User_extensions_Table *extension_table |
---|
| 103 | ) |
---|
| 104 | { |
---|
| 105 | extension->Callouts = *extension_table; |
---|
| 106 | |
---|
| 107 | _User_extensions_Add_set( extension ); |
---|
| 108 | } |
---|
| 109 | |
---|
| 110 | void _User_extensions_Remove_set( |
---|
| 111 | User_extensions_Control *extension |
---|
| 112 | ); |
---|
| 113 | |
---|
| 114 | /** |
---|
| 115 | * @brief User extension visitor. |
---|
| 116 | * |
---|
| 117 | * @param[in, out] executing The currently executing thread. |
---|
| 118 | * @param[in, out] arg The argument passed to _User_extensions_Iterate(). |
---|
| 119 | * @param[in] callouts The current callouts. |
---|
| 120 | */ |
---|
| 121 | typedef void (*User_extensions_Visitor)( |
---|
| 122 | Thread_Control *executing, |
---|
| 123 | void *arg, |
---|
| 124 | const User_extensions_Table *callouts |
---|
| 125 | ); |
---|
| 126 | |
---|
| 127 | typedef struct { |
---|
| 128 | Thread_Control *created; |
---|
| 129 | bool ok; |
---|
| 130 | } User_extensions_Thread_create_context; |
---|
| 131 | |
---|
| 132 | void _User_extensions_Thread_create_visitor( |
---|
| 133 | Thread_Control *executing, |
---|
| 134 | void *arg, |
---|
| 135 | const User_extensions_Table *callouts |
---|
| 136 | ); |
---|
| 137 | |
---|
| 138 | void _User_extensions_Thread_delete_visitor( |
---|
| 139 | Thread_Control *executing, |
---|
| 140 | void *arg, |
---|
| 141 | const User_extensions_Table *callouts |
---|
| 142 | ); |
---|
| 143 | |
---|
| 144 | void _User_extensions_Thread_start_visitor( |
---|
| 145 | Thread_Control *executing, |
---|
| 146 | void *arg, |
---|
| 147 | const User_extensions_Table *callouts |
---|
| 148 | ); |
---|
| 149 | |
---|
| 150 | void _User_extensions_Thread_restart_visitor( |
---|
| 151 | Thread_Control *executing, |
---|
| 152 | void *arg, |
---|
| 153 | const User_extensions_Table *callouts |
---|
| 154 | ); |
---|
| 155 | |
---|
| 156 | void _User_extensions_Thread_begin_visitor( |
---|
| 157 | Thread_Control *executing, |
---|
| 158 | void *arg, |
---|
| 159 | const User_extensions_Table *callouts |
---|
| 160 | ); |
---|
| 161 | |
---|
| 162 | void _User_extensions_Thread_exitted_visitor( |
---|
| 163 | Thread_Control *executing, |
---|
| 164 | void *arg, |
---|
| 165 | const User_extensions_Table *callouts |
---|
| 166 | ); |
---|
| 167 | |
---|
| 168 | typedef struct { |
---|
| 169 | Internal_errors_Source source; |
---|
| 170 | Internal_errors_t error; |
---|
| 171 | } User_extensions_Fatal_context; |
---|
| 172 | |
---|
| 173 | void _User_extensions_Fatal_visitor( |
---|
| 174 | Thread_Control *executing, |
---|
| 175 | void *arg, |
---|
| 176 | const User_extensions_Table *callouts |
---|
| 177 | ); |
---|
| 178 | |
---|
[1b1be254] | 179 | void _User_extensions_Thread_terminate_visitor( |
---|
| 180 | Thread_Control *executing, |
---|
| 181 | void *arg, |
---|
| 182 | const User_extensions_Table *callouts |
---|
| 183 | ); |
---|
| 184 | |
---|
[3be0c9a] | 185 | /** |
---|
| 186 | * @brief Iterates through all user extensions and calls the visitor for each. |
---|
| 187 | * |
---|
| 188 | * @param[in, out] arg The argument passed to the visitor. |
---|
[709f38a] | 189 | * @param[in] visitor The visitor for each extension. |
---|
| 190 | * @param[in] direction The iteration direction for dynamic extensions. |
---|
[3be0c9a] | 191 | */ |
---|
| 192 | void _User_extensions_Iterate( |
---|
[709f38a] | 193 | void *arg, |
---|
| 194 | User_extensions_Visitor visitor, |
---|
| 195 | Chain_Iterator_direction direction |
---|
[3be0c9a] | 196 | ); |
---|
| 197 | |
---|
| 198 | /** @} */ |
---|
| 199 | |
---|
| 200 | /** |
---|
| 201 | * @name Extension Callout Dispatcher |
---|
| 202 | */ |
---|
[b697bc6] | 203 | /**@{**/ |
---|
[3be0c9a] | 204 | |
---|
| 205 | static inline bool _User_extensions_Thread_create( Thread_Control *created ) |
---|
| 206 | { |
---|
| 207 | User_extensions_Thread_create_context ctx = { created, true }; |
---|
| 208 | |
---|
[709f38a] | 209 | _User_extensions_Iterate( |
---|
| 210 | &ctx, |
---|
| 211 | _User_extensions_Thread_create_visitor, |
---|
| 212 | CHAIN_ITERATOR_FORWARD |
---|
| 213 | ); |
---|
[3be0c9a] | 214 | |
---|
| 215 | return ctx.ok; |
---|
| 216 | } |
---|
| 217 | |
---|
| 218 | static inline void _User_extensions_Thread_delete( Thread_Control *deleted ) |
---|
| 219 | { |
---|
| 220 | _User_extensions_Iterate( |
---|
| 221 | deleted, |
---|
[709f38a] | 222 | _User_extensions_Thread_delete_visitor, |
---|
| 223 | CHAIN_ITERATOR_BACKWARD |
---|
[3be0c9a] | 224 | ); |
---|
| 225 | } |
---|
| 226 | |
---|
| 227 | static inline void _User_extensions_Thread_start( Thread_Control *started ) |
---|
| 228 | { |
---|
| 229 | _User_extensions_Iterate( |
---|
| 230 | started, |
---|
[709f38a] | 231 | _User_extensions_Thread_start_visitor, |
---|
| 232 | CHAIN_ITERATOR_FORWARD |
---|
[3be0c9a] | 233 | ); |
---|
| 234 | } |
---|
| 235 | |
---|
| 236 | static inline void _User_extensions_Thread_restart( Thread_Control *restarted ) |
---|
| 237 | { |
---|
| 238 | _User_extensions_Iterate( |
---|
| 239 | restarted, |
---|
[709f38a] | 240 | _User_extensions_Thread_restart_visitor, |
---|
| 241 | CHAIN_ITERATOR_FORWARD |
---|
[3be0c9a] | 242 | ); |
---|
| 243 | } |
---|
| 244 | |
---|
| 245 | static inline void _User_extensions_Thread_begin( Thread_Control *executing ) |
---|
| 246 | { |
---|
| 247 | _User_extensions_Iterate( |
---|
| 248 | executing, |
---|
[709f38a] | 249 | _User_extensions_Thread_begin_visitor, |
---|
| 250 | CHAIN_ITERATOR_FORWARD |
---|
[3be0c9a] | 251 | ); |
---|
| 252 | } |
---|
| 253 | |
---|
[32b2c83] | 254 | static inline void _User_extensions_Thread_switch( |
---|
[3be0c9a] | 255 | Thread_Control *executing, |
---|
| 256 | Thread_Control *heir |
---|
[32b2c83] | 257 | ) |
---|
| 258 | { |
---|
| 259 | const Chain_Control *chain = &_User_extensions_Switches_list; |
---|
| 260 | const Chain_Node *tail = _Chain_Immutable_tail( chain ); |
---|
| 261 | const Chain_Node *node = _Chain_Immutable_first( chain ); |
---|
| 262 | |
---|
[38b59a6] | 263 | if ( node != tail ) { |
---|
[d5e073c] | 264 | Per_CPU_Control *cpu_self; |
---|
| 265 | #if defined(RTEMS_SMP) |
---|
| 266 | ISR_Level level; |
---|
| 267 | #endif |
---|
| 268 | |
---|
| 269 | cpu_self = _Per_CPU_Get(); |
---|
[32b2c83] | 270 | |
---|
[d5e073c] | 271 | #if defined(RTEMS_SMP) |
---|
| 272 | _ISR_Local_disable( level ); |
---|
| 273 | #endif |
---|
[38b59a6] | 274 | _Per_CPU_Acquire( cpu_self ); |
---|
[32b2c83] | 275 | |
---|
[38b59a6] | 276 | while ( node != tail ) { |
---|
| 277 | const User_extensions_Switch_control *extension = |
---|
| 278 | (const User_extensions_Switch_control *) node; |
---|
| 279 | |
---|
| 280 | (*extension->thread_switch)( executing, heir ); |
---|
| 281 | |
---|
| 282 | node = _Chain_Immutable_next( node ); |
---|
| 283 | } |
---|
| 284 | |
---|
| 285 | _Per_CPU_Release( cpu_self ); |
---|
[d5e073c] | 286 | #if defined(RTEMS_SMP) |
---|
| 287 | _ISR_Local_enable( level ); |
---|
| 288 | #endif |
---|
[32b2c83] | 289 | } |
---|
| 290 | } |
---|
[3be0c9a] | 291 | |
---|
| 292 | static inline void _User_extensions_Thread_exitted( Thread_Control *executing ) |
---|
| 293 | { |
---|
| 294 | _User_extensions_Iterate( |
---|
| 295 | executing, |
---|
[709f38a] | 296 | _User_extensions_Thread_exitted_visitor, |
---|
| 297 | CHAIN_ITERATOR_FORWARD |
---|
[3be0c9a] | 298 | ); |
---|
| 299 | } |
---|
| 300 | |
---|
| 301 | static inline void _User_extensions_Fatal( |
---|
| 302 | Internal_errors_Source source, |
---|
| 303 | Internal_errors_t error |
---|
| 304 | ) |
---|
| 305 | { |
---|
[b6606e8] | 306 | User_extensions_Fatal_context ctx = { source, error }; |
---|
[3be0c9a] | 307 | |
---|
[709f38a] | 308 | _User_extensions_Iterate( |
---|
| 309 | &ctx, |
---|
| 310 | _User_extensions_Fatal_visitor, |
---|
[6f6da82c] | 311 | CHAIN_ITERATOR_FORWARD |
---|
[709f38a] | 312 | ); |
---|
[3be0c9a] | 313 | } |
---|
| 314 | |
---|
[1b1be254] | 315 | static inline void _User_extensions_Thread_terminate( |
---|
| 316 | Thread_Control *executing |
---|
| 317 | ) |
---|
| 318 | { |
---|
| 319 | _User_extensions_Iterate( |
---|
| 320 | executing, |
---|
[709f38a] | 321 | _User_extensions_Thread_terminate_visitor, |
---|
[6f6da82c] | 322 | CHAIN_ITERATOR_BACKWARD |
---|
[709f38a] | 323 | ); |
---|
| 324 | } |
---|
| 325 | |
---|
| 326 | static inline void _User_extensions_Acquire( ISR_lock_Context *lock_context ) |
---|
| 327 | { |
---|
| 328 | _ISR_lock_ISR_disable_and_acquire( |
---|
| 329 | &_User_extensions_List.Lock, |
---|
| 330 | lock_context |
---|
[1b1be254] | 331 | ); |
---|
| 332 | } |
---|
| 333 | |
---|
[709f38a] | 334 | static inline void _User_extensions_Release( ISR_lock_Context *lock_context ) |
---|
| 335 | { |
---|
| 336 | _ISR_lock_Release_and_ISR_enable( |
---|
| 337 | &_User_extensions_List.Lock, |
---|
| 338 | lock_context |
---|
| 339 | ); |
---|
| 340 | } |
---|
| 341 | |
---|
| 342 | static inline void _User_extensions_Destroy_iterators( |
---|
| 343 | Thread_Control *the_thread |
---|
| 344 | ) |
---|
| 345 | { |
---|
| 346 | ISR_lock_Context lock_context; |
---|
| 347 | User_extensions_Iterator *iter; |
---|
| 348 | |
---|
| 349 | _User_extensions_Acquire( &lock_context ); |
---|
| 350 | |
---|
| 351 | iter = the_thread->last_user_extensions_iterator; |
---|
| 352 | |
---|
| 353 | while ( iter != NULL ) { |
---|
| 354 | _Chain_Iterator_destroy( &iter->Iterator ); |
---|
| 355 | iter = iter->previous; |
---|
| 356 | } |
---|
| 357 | |
---|
| 358 | _User_extensions_Release( &lock_context ); |
---|
| 359 | } |
---|
| 360 | |
---|
[3be0c9a] | 361 | /** @} */ |
---|
| 362 | |
---|
| 363 | /** @} */ |
---|
| 364 | |
---|
| 365 | #ifdef __cplusplus |
---|
| 366 | } |
---|
| 367 | #endif |
---|
| 368 | |
---|
| 369 | #endif |
---|
[b10825c] | 370 | /* end of include file */ |
---|