| 1 | // | 
|---|
| 2 | // Cforall Version 1.0.0 Copyright (C) 2016 University of Waterloo | 
|---|
| 3 | // | 
|---|
| 4 | // The contents of this file are covered under the licence agreement in the | 
|---|
| 5 | // file "LICENCE" distributed with Cforall. | 
|---|
| 6 | // | 
|---|
| 7 | // monitor_desc.c -- | 
|---|
| 8 | // | 
|---|
| 9 | // Author           : Thierry Delisle | 
|---|
| 10 | // Created On       : Thd Feb 23 12:27:26 2017 | 
|---|
| 11 | // Last Modified By : Peter A. Buhr | 
|---|
| 12 | // Last Modified On : Fri Mar 30 14:30:26 2018 | 
|---|
| 13 | // Update Count     : 9 | 
|---|
| 14 | // | 
|---|
| 15 |  | 
|---|
| 16 | #include "monitor" | 
|---|
| 17 |  | 
|---|
| 18 | #include <stdlib> | 
|---|
| 19 | #include <inttypes.h> | 
|---|
| 20 |  | 
|---|
| 21 | #include "kernel_private.h" | 
|---|
| 22 |  | 
|---|
| 23 | #include "bits/algorithms.h" | 
|---|
| 24 |  | 
|---|
| 25 | //----------------------------------------------------------------------------- | 
|---|
| 26 | // Forward declarations | 
|---|
| 27 | static inline void set_owner ( monitor_desc * this, thread_desc * owner ); | 
|---|
| 28 | static inline void set_owner ( monitor_desc * storage [], __lock_size_t count, thread_desc * owner ); | 
|---|
| 29 | static inline void set_mask  ( monitor_desc * storage [], __lock_size_t count, const __waitfor_mask_t & mask ); | 
|---|
| 30 | static inline void reset_mask( monitor_desc * this ); | 
|---|
| 31 |  | 
|---|
| 32 | static inline thread_desc * next_thread( monitor_desc * this ); | 
|---|
| 33 | static inline bool is_accepted( monitor_desc * this, const __monitor_group_t & monitors ); | 
|---|
| 34 |  | 
|---|
| 35 | static inline void lock_all  ( __spinlock_t * locks [], __lock_size_t count ); | 
|---|
| 36 | static inline void lock_all  ( monitor_desc * source [], __spinlock_t * /*out*/ locks [], __lock_size_t count ); | 
|---|
| 37 | static inline void unlock_all( __spinlock_t * locks [], __lock_size_t count ); | 
|---|
| 38 | static inline void unlock_all( monitor_desc * locks [], __lock_size_t count ); | 
|---|
| 39 |  | 
|---|
| 40 | static inline void save   ( monitor_desc * ctx [], __lock_size_t count, __spinlock_t * locks [], unsigned int /*out*/ recursions [], __waitfor_mask_t /*out*/ masks [] ); | 
|---|
| 41 | static inline void restore( monitor_desc * ctx [], __lock_size_t count, __spinlock_t * locks [], unsigned int /*in */ recursions [], __waitfor_mask_t /*in */ masks [] ); | 
|---|
| 42 |  | 
|---|
| 43 | static inline void init     ( __lock_size_t count, monitor_desc * monitors [], __condition_node_t & waiter, __condition_criterion_t criteria [] ); | 
|---|
| 44 | static inline void init_push( __lock_size_t count, monitor_desc * monitors [], __condition_node_t & waiter, __condition_criterion_t criteria [] ); | 
|---|
| 45 |  | 
|---|
| 46 | static inline thread_desc *        check_condition   ( __condition_criterion_t * ); | 
|---|
| 47 | static inline void                 brand_condition   ( condition & ); | 
|---|
| 48 | static inline [thread_desc *, int] search_entry_queue( const __waitfor_mask_t &, monitor_desc * monitors [], __lock_size_t count ); | 
|---|
| 49 |  | 
|---|
| 50 | forall(dtype T | sized( T )) | 
|---|
| 51 | static inline __lock_size_t insert_unique( T * array [], __lock_size_t & size, T * val ); | 
|---|
| 52 | static inline __lock_size_t count_max    ( const __waitfor_mask_t & mask ); | 
|---|
| 53 | static inline __lock_size_t aggregate    ( monitor_desc * storage [], const __waitfor_mask_t & mask ); | 
|---|
| 54 |  | 
|---|
| 55 | //----------------------------------------------------------------------------- | 
|---|
| 56 | // Useful defines | 
|---|
| 57 | #define wait_ctx(thrd, user_info)                               /* Create the necessary information to use the signaller stack                         */ \ | 
|---|
| 58 | __condition_node_t waiter = { thrd, count, user_info };   /* Create the node specific to this wait operation                                     */ \ | 
|---|
| 59 | __condition_criterion_t criteria[count];                  /* Create the creteria this wait operation needs to wake up                            */ \ | 
|---|
| 60 | init( count, monitors, waiter, criteria );                /* Link everything together                                                            */ \ | 
|---|
| 61 |  | 
|---|
| 62 | #define wait_ctx_primed(thrd, user_info)                        /* Create the necessary information to use the signaller stack                         */ \ | 
|---|
| 63 | __condition_node_t waiter = { thrd, count, user_info };   /* Create the node specific to this wait operation                                     */ \ | 
|---|
| 64 | __condition_criterion_t criteria[count];                  /* Create the creteria this wait operation needs to wake up                            */ \ | 
|---|
| 65 | init_push( count, monitors, waiter, criteria );           /* Link everything together and push it to the AS-Stack                                */ \ | 
|---|
| 66 |  | 
|---|
| 67 | #define monitor_ctx( mons, cnt )                                /* Define that create the necessary struct for internal/external scheduling operations */ \ | 
|---|
| 68 | monitor_desc ** monitors = mons;                          /* Save the targeted monitors                                                          */ \ | 
|---|
| 69 | __lock_size_t count = cnt;                                /* Save the count to a local variable                                                  */ \ | 
|---|
| 70 | unsigned int recursions[ count ];                         /* Save the current recursion levels to restore them later                             */ \ | 
|---|
| 71 | __waitfor_mask_t masks [ count ];                         /* Save the current waitfor masks to restore them later                                */ \ | 
|---|
| 72 | __spinlock_t *   locks [ count ];                         /* We need to pass-in an array of locks to BlockInternal                               */ \ | 
|---|
| 73 |  | 
|---|
| 74 | #define monitor_save    save   ( monitors, count, locks, recursions, masks ) | 
|---|
| 75 | #define monitor_restore restore( monitors, count, locks, recursions, masks ) | 
|---|
| 76 |  | 
|---|
| 77 |  | 
|---|
| 78 | //----------------------------------------------------------------------------- | 
|---|
| 79 | // Enter/Leave routines | 
|---|
| 80 |  | 
|---|
| 81 |  | 
|---|
| 82 | extern "C" { | 
|---|
| 83 | // Enter single monitor | 
|---|
| 84 | static void __enter_monitor_desc( monitor_desc * this, const __monitor_group_t & group ) { | 
|---|
| 85 | // Lock the monitor spinlock | 
|---|
| 86 | lock( this->lock __cfaabi_dbg_ctx2 ); | 
|---|
| 87 | // Interrupts disable inside critical section | 
|---|
| 88 | thread_desc * thrd = kernelTLS.this_thread; | 
|---|
| 89 |  | 
|---|
| 90 | __cfaabi_dbg_print_safe( "Kernel : %10p Entering mon %p (%p)\n", thrd, this, this->owner); | 
|---|
| 91 |  | 
|---|
| 92 | if( !this->owner ) { | 
|---|
| 93 | // No one has the monitor, just take it | 
|---|
| 94 | set_owner( this, thrd ); | 
|---|
| 95 |  | 
|---|
| 96 | __cfaabi_dbg_print_safe( "Kernel :  mon is free \n" ); | 
|---|
| 97 | } | 
|---|
| 98 | else if( this->owner == thrd) { | 
|---|
| 99 | // We already have the monitor, just note how many times we took it | 
|---|
| 100 | this->recursion += 1; | 
|---|
| 101 |  | 
|---|
| 102 | __cfaabi_dbg_print_safe( "Kernel :  mon already owned \n" ); | 
|---|
| 103 | } | 
|---|
| 104 | else if( is_accepted( this, group) ) { | 
|---|
| 105 | // Some one was waiting for us, enter | 
|---|
| 106 | set_owner( this, thrd ); | 
|---|
| 107 |  | 
|---|
| 108 | // Reset mask | 
|---|
| 109 | reset_mask( this ); | 
|---|
| 110 |  | 
|---|
| 111 | __cfaabi_dbg_print_safe( "Kernel :  mon accepts \n" ); | 
|---|
| 112 | } | 
|---|
| 113 | else { | 
|---|
| 114 | __cfaabi_dbg_print_safe( "Kernel :  blocking \n" ); | 
|---|
| 115 |  | 
|---|
| 116 | // Some one else has the monitor, wait in line for it | 
|---|
| 117 | append( this->entry_queue, thrd ); | 
|---|
| 118 |  | 
|---|
| 119 | BlockInternal( &this->lock ); | 
|---|
| 120 |  | 
|---|
| 121 | __cfaabi_dbg_print_safe( "Kernel : %10p Entered  mon %p\n", thrd, this); | 
|---|
| 122 |  | 
|---|
| 123 | // BlockInternal will unlock spinlock, no need to unlock ourselves | 
|---|
| 124 | return; | 
|---|
| 125 | } | 
|---|
| 126 |  | 
|---|
| 127 | __cfaabi_dbg_print_safe( "Kernel : %10p Entered  mon %p\n", thrd, this); | 
|---|
| 128 |  | 
|---|
| 129 | // Release the lock and leave | 
|---|
| 130 | unlock( this->lock ); | 
|---|
| 131 | return; | 
|---|
| 132 | } | 
|---|
| 133 |  | 
|---|
| 134 | static void __enter_monitor_dtor( monitor_desc * this, fptr_t func ) { | 
|---|
| 135 | // Lock the monitor spinlock | 
|---|
| 136 | lock( this->lock __cfaabi_dbg_ctx2 ); | 
|---|
| 137 | // Interrupts disable inside critical section | 
|---|
| 138 | thread_desc * thrd = kernelTLS.this_thread; | 
|---|
| 139 |  | 
|---|
| 140 | __cfaabi_dbg_print_safe( "Kernel : %10p Entering dtor for mon %p (%p)\n", thrd, this, this->owner); | 
|---|
| 141 |  | 
|---|
| 142 |  | 
|---|
| 143 | if( !this->owner ) { | 
|---|
| 144 | __cfaabi_dbg_print_safe( "Kernel : Destroying free mon %p\n", this); | 
|---|
| 145 |  | 
|---|
| 146 | // No one has the monitor, just take it | 
|---|
| 147 | set_owner( this, thrd ); | 
|---|
| 148 |  | 
|---|
| 149 | unlock( this->lock ); | 
|---|
| 150 | return; | 
|---|
| 151 | } | 
|---|
| 152 | else if( this->owner == thrd) { | 
|---|
| 153 | // We already have the monitor... but where about to destroy it so the nesting will fail | 
|---|
| 154 | // Abort! | 
|---|
| 155 | abort( "Attempt to destroy monitor %p by thread \"%.256s\" (%p) in nested mutex.", this, thrd->self_cor.name, thrd ); | 
|---|
| 156 | } | 
|---|
| 157 |  | 
|---|
| 158 | __lock_size_t count = 1; | 
|---|
| 159 | monitor_desc ** monitors = &this; | 
|---|
| 160 | __monitor_group_t group = { &this, 1, func }; | 
|---|
| 161 | if( is_accepted( this, group) ) { | 
|---|
| 162 | __cfaabi_dbg_print_safe( "Kernel :  mon accepts dtor, block and signal it \n" ); | 
|---|
| 163 |  | 
|---|
| 164 | // Wake the thread that is waiting for this | 
|---|
| 165 | __condition_criterion_t * urgent = pop( this->signal_stack ); | 
|---|
| 166 | verify( urgent ); | 
|---|
| 167 |  | 
|---|
| 168 | // Reset mask | 
|---|
| 169 | reset_mask( this ); | 
|---|
| 170 |  | 
|---|
| 171 | // Create the node specific to this wait operation | 
|---|
| 172 | wait_ctx_primed( thrd, 0 ) | 
|---|
| 173 |  | 
|---|
| 174 | // Some one else has the monitor, wait for him to finish and then run | 
|---|
| 175 | BlockInternal( &this->lock, urgent->owner->waiting_thread ); | 
|---|
| 176 |  | 
|---|
| 177 | // Some one was waiting for us, enter | 
|---|
| 178 | set_owner( this, thrd ); | 
|---|
| 179 | } | 
|---|
| 180 | else { | 
|---|
| 181 | __cfaabi_dbg_print_safe( "Kernel :  blocking \n" ); | 
|---|
| 182 |  | 
|---|
| 183 | wait_ctx( thrd, 0 ) | 
|---|
| 184 | this->dtor_node = &waiter; | 
|---|
| 185 |  | 
|---|
| 186 | // Some one else has the monitor, wait in line for it | 
|---|
| 187 | append( this->entry_queue, thrd ); | 
|---|
| 188 | BlockInternal( &this->lock ); | 
|---|
| 189 |  | 
|---|
| 190 | // BlockInternal will unlock spinlock, no need to unlock ourselves | 
|---|
| 191 | return; | 
|---|
| 192 | } | 
|---|
| 193 |  | 
|---|
| 194 | __cfaabi_dbg_print_safe( "Kernel : Destroying %p\n", this); | 
|---|
| 195 |  | 
|---|
| 196 | } | 
|---|
| 197 |  | 
|---|
| 198 | // Leave single monitor | 
|---|
| 199 | void __leave_monitor_desc( monitor_desc * this ) { | 
|---|
| 200 | // Lock the monitor spinlock | 
|---|
| 201 | lock( this->lock __cfaabi_dbg_ctx2 ); | 
|---|
| 202 |  | 
|---|
| 203 | __cfaabi_dbg_print_safe( "Kernel : %10p Leaving mon %p (%p)\n", kernelTLS.this_thread, this, this->owner); | 
|---|
| 204 |  | 
|---|
| 205 | verifyf( kernelTLS.this_thread == this->owner, "Expected owner to be %p, got %p (r: %i, m: %p)", kernelTLS.this_thread, this->owner, this->recursion, this ); | 
|---|
| 206 |  | 
|---|
| 207 | // Leaving a recursion level, decrement the counter | 
|---|
| 208 | this->recursion -= 1; | 
|---|
| 209 |  | 
|---|
| 210 | // If we haven't left the last level of recursion | 
|---|
| 211 | // it means we don't need to do anything | 
|---|
| 212 | if( this->recursion != 0) { | 
|---|
| 213 | __cfaabi_dbg_print_safe( "Kernel :  recursion still %d\n", this->recursion); | 
|---|
| 214 | unlock( this->lock ); | 
|---|
| 215 | return; | 
|---|
| 216 | } | 
|---|
| 217 |  | 
|---|
| 218 | // Get the next thread, will be null on low contention monitor | 
|---|
| 219 | thread_desc * new_owner = next_thread( this ); | 
|---|
| 220 |  | 
|---|
| 221 | // We can now let other threads in safely | 
|---|
| 222 | unlock( this->lock ); | 
|---|
| 223 |  | 
|---|
| 224 | //We need to wake-up the thread | 
|---|
| 225 | WakeThread( new_owner ); | 
|---|
| 226 | } | 
|---|
| 227 |  | 
|---|
| 228 | // Leave single monitor for the last time | 
|---|
| 229 | void __leave_dtor_monitor_desc( monitor_desc * this ) { | 
|---|
| 230 | __cfaabi_dbg_debug_do( | 
|---|
| 231 | if( TL_GET( this_thread ) != this->owner ) { | 
|---|
| 232 | abort( "Destroyed monitor %p has inconsistent owner, expected %p got %p.\n", this, TL_GET( this_thread ), this->owner); | 
|---|
| 233 | } | 
|---|
| 234 | if( this->recursion != 1 ) { | 
|---|
| 235 | abort( "Destroyed monitor %p has %d outstanding nested calls.\n", this, this->recursion - 1); | 
|---|
| 236 | } | 
|---|
| 237 | ) | 
|---|
| 238 | } | 
|---|
| 239 |  | 
|---|
| 240 | // Leave the thread monitor | 
|---|
| 241 | // last routine called by a thread. | 
|---|
| 242 | // Should never return | 
|---|
| 243 | void __leave_thread_monitor( thread_desc * thrd ) { | 
|---|
| 244 | monitor_desc * this = &thrd->self_mon; | 
|---|
| 245 |  | 
|---|
| 246 | // Lock the monitor now | 
|---|
| 247 | lock( this->lock __cfaabi_dbg_ctx2 ); | 
|---|
| 248 |  | 
|---|
| 249 | disable_interrupts(); | 
|---|
| 250 |  | 
|---|
| 251 | thrd->self_cor.state = Halted; | 
|---|
| 252 |  | 
|---|
| 253 | verifyf( thrd == this->owner, "Expected owner to be %p, got %p (r: %i, m: %p)", thrd, this->owner, this->recursion, this ); | 
|---|
| 254 |  | 
|---|
| 255 | // Leaving a recursion level, decrement the counter | 
|---|
| 256 | this->recursion -= 1; | 
|---|
| 257 |  | 
|---|
| 258 | // If we haven't left the last level of recursion | 
|---|
| 259 | // it must mean there is an error | 
|---|
| 260 | if( this->recursion != 0) { abort( "Thread internal monitor has unbalanced recursion" ); } | 
|---|
| 261 |  | 
|---|
| 262 | // Fetch the next thread, can be null | 
|---|
| 263 | thread_desc * new_owner = next_thread( this ); | 
|---|
| 264 |  | 
|---|
| 265 | // Leave the thread, this will unlock the spinlock | 
|---|
| 266 | // Use leave thread instead of BlockInternal which is | 
|---|
| 267 | // specialized for this case and supports null new_owner | 
|---|
| 268 | LeaveThread( &this->lock, new_owner ); | 
|---|
| 269 |  | 
|---|
| 270 | // Control flow should never reach here! | 
|---|
| 271 | } | 
|---|
| 272 | } | 
|---|
| 273 |  | 
|---|
| 274 | // Enter multiple monitor | 
|---|
| 275 | // relies on the monitor array being sorted | 
|---|
| 276 | static inline void enter( __monitor_group_t monitors ) { | 
|---|
| 277 | for( __lock_size_t i = 0; i < monitors.size; i++) { | 
|---|
| 278 | __enter_monitor_desc( monitors[i], monitors ); | 
|---|
| 279 | } | 
|---|
| 280 | } | 
|---|
| 281 |  | 
|---|
| 282 | // Leave multiple monitor | 
|---|
| 283 | // relies on the monitor array being sorted | 
|---|
| 284 | static inline void leave(monitor_desc * monitors [], __lock_size_t count) { | 
|---|
| 285 | for( __lock_size_t i = count - 1; i >= 0; i--) { | 
|---|
| 286 | __leave_monitor_desc( monitors[i] ); | 
|---|
| 287 | } | 
|---|
| 288 | } | 
|---|
| 289 |  | 
|---|
| 290 | // Ctor for monitor guard | 
|---|
| 291 | // Sorts monitors before entering | 
|---|
| 292 | void ?{}( monitor_guard_t & this, monitor_desc * m [], __lock_size_t count, fptr_t func ) { | 
|---|
| 293 | thread_desc * thrd = TL_GET( this_thread ); | 
|---|
| 294 |  | 
|---|
| 295 | // Store current array | 
|---|
| 296 | this.m = m; | 
|---|
| 297 | this.count = count; | 
|---|
| 298 |  | 
|---|
| 299 | // Sort monitors based on address -> TODO use a sort specialized for small numbers | 
|---|
| 300 | __libcfa_small_sort(this.m, count); | 
|---|
| 301 |  | 
|---|
| 302 | // Save previous thread context | 
|---|
| 303 | this.prev = thrd->monitors; | 
|---|
| 304 |  | 
|---|
| 305 | // Update thread context (needed for conditions) | 
|---|
| 306 | (thrd->monitors){m, count, func}; | 
|---|
| 307 |  | 
|---|
| 308 | // __cfaabi_dbg_print_safe( "MGUARD : enter %d\n", count); | 
|---|
| 309 |  | 
|---|
| 310 | // Enter the monitors in order | 
|---|
| 311 | __monitor_group_t group = {this.m, this.count, func}; | 
|---|
| 312 | enter( group ); | 
|---|
| 313 |  | 
|---|
| 314 | // __cfaabi_dbg_print_safe( "MGUARD : entered\n" ); | 
|---|
| 315 | } | 
|---|
| 316 |  | 
|---|
| 317 |  | 
|---|
| 318 | // Dtor for monitor guard | 
|---|
| 319 | void ^?{}( monitor_guard_t & this ) { | 
|---|
| 320 | // __cfaabi_dbg_print_safe( "MGUARD : leaving %d\n", this.count); | 
|---|
| 321 |  | 
|---|
| 322 | // Leave the monitors in order | 
|---|
| 323 | leave( this.m, this.count ); | 
|---|
| 324 |  | 
|---|
| 325 | // __cfaabi_dbg_print_safe( "MGUARD : left\n" ); | 
|---|
| 326 |  | 
|---|
| 327 | // Restore thread context | 
|---|
| 328 | TL_GET( this_thread )->monitors = this.prev; | 
|---|
| 329 | } | 
|---|
| 330 |  | 
|---|
| 331 | // Ctor for monitor guard | 
|---|
| 332 | // Sorts monitors before entering | 
|---|
| 333 | void ?{}( monitor_dtor_guard_t & this, monitor_desc * m [], fptr_t func ) { | 
|---|
| 334 | // optimization | 
|---|
| 335 | thread_desc * thrd = TL_GET( this_thread ); | 
|---|
| 336 |  | 
|---|
| 337 | // Store current array | 
|---|
| 338 | this.m = *m; | 
|---|
| 339 |  | 
|---|
| 340 | // Save previous thread context | 
|---|
| 341 | this.prev = thrd->monitors; | 
|---|
| 342 |  | 
|---|
| 343 | // Update thread context (needed for conditions) | 
|---|
| 344 | (thrd->monitors){m, 1, func}; | 
|---|
| 345 |  | 
|---|
| 346 | __enter_monitor_dtor( this.m, func ); | 
|---|
| 347 | } | 
|---|
| 348 |  | 
|---|
| 349 | // Dtor for monitor guard | 
|---|
| 350 | void ^?{}( monitor_dtor_guard_t & this ) { | 
|---|
| 351 | // Leave the monitors in order | 
|---|
| 352 | __leave_dtor_monitor_desc( this.m ); | 
|---|
| 353 |  | 
|---|
| 354 | // Restore thread context | 
|---|
| 355 | TL_GET( this_thread )->monitors = this.prev; | 
|---|
| 356 | } | 
|---|
| 357 |  | 
|---|
| 358 | //----------------------------------------------------------------------------- | 
|---|
| 359 | // Internal scheduling types | 
|---|
| 360 | void ?{}(__condition_node_t & this, thread_desc * waiting_thread, __lock_size_t count, uintptr_t user_info ) { | 
|---|
| 361 | this.waiting_thread = waiting_thread; | 
|---|
| 362 | this.count = count; | 
|---|
| 363 | this.next = NULL; | 
|---|
| 364 | this.user_info = user_info; | 
|---|
| 365 | } | 
|---|
| 366 |  | 
|---|
| 367 | void ?{}(__condition_criterion_t & this ) with( this ) { | 
|---|
| 368 | ready  = false; | 
|---|
| 369 | target = NULL; | 
|---|
| 370 | owner  = NULL; | 
|---|
| 371 | next   = NULL; | 
|---|
| 372 | } | 
|---|
| 373 |  | 
|---|
| 374 | void ?{}(__condition_criterion_t & this, monitor_desc * target, __condition_node_t & owner ) { | 
|---|
| 375 | this.ready  = false; | 
|---|
| 376 | this.target = target; | 
|---|
| 377 | this.owner  = &owner; | 
|---|
| 378 | this.next   = NULL; | 
|---|
| 379 | } | 
|---|
| 380 |  | 
|---|
| 381 | //----------------------------------------------------------------------------- | 
|---|
| 382 | // Internal scheduling | 
|---|
| 383 | void wait( condition & this, uintptr_t user_info = 0 ) { | 
|---|
| 384 | brand_condition( this ); | 
|---|
| 385 |  | 
|---|
| 386 | // Check that everything is as expected | 
|---|
| 387 | assertf( this.monitors != NULL, "Waiting with no monitors (%p)", this.monitors ); | 
|---|
| 388 | verifyf( this.monitor_count != 0, "Waiting with 0 monitors (%"PRIiFAST16")", this.monitor_count ); | 
|---|
| 389 | verifyf( this.monitor_count < 32u, "Excessive monitor count (%"PRIiFAST16")", this.monitor_count ); | 
|---|
| 390 |  | 
|---|
| 391 | // Create storage for monitor context | 
|---|
| 392 | monitor_ctx( this.monitors, this.monitor_count ); | 
|---|
| 393 |  | 
|---|
| 394 | // Create the node specific to this wait operation | 
|---|
| 395 | wait_ctx( TL_GET( this_thread ), user_info ); | 
|---|
| 396 |  | 
|---|
| 397 | // Append the current wait operation to the ones already queued on the condition | 
|---|
| 398 | // We don't need locks for that since conditions must always be waited on inside monitor mutual exclusion | 
|---|
| 399 | append( this.blocked, &waiter ); | 
|---|
| 400 |  | 
|---|
| 401 | // Lock all monitors (aggregates the locks as well) | 
|---|
| 402 | lock_all( monitors, locks, count ); | 
|---|
| 403 |  | 
|---|
| 404 | // Find the next thread(s) to run | 
|---|
| 405 | __lock_size_t thread_count = 0; | 
|---|
| 406 | thread_desc * threads[ count ]; | 
|---|
| 407 | __builtin_memset( threads, 0, sizeof( threads ) ); | 
|---|
| 408 |  | 
|---|
| 409 | // Save monitor states | 
|---|
| 410 | monitor_save; | 
|---|
| 411 |  | 
|---|
| 412 | // Remove any duplicate threads | 
|---|
| 413 | for( __lock_size_t i = 0; i < count; i++) { | 
|---|
| 414 | thread_desc * new_owner = next_thread( monitors[i] ); | 
|---|
| 415 | insert_unique( threads, thread_count, new_owner ); | 
|---|
| 416 | } | 
|---|
| 417 |  | 
|---|
| 418 | // Everything is ready to go to sleep | 
|---|
| 419 | BlockInternal( locks, count, threads, thread_count ); | 
|---|
| 420 |  | 
|---|
| 421 | // We are back, restore the owners and recursions | 
|---|
| 422 | monitor_restore; | 
|---|
| 423 | } | 
|---|
| 424 |  | 
|---|
| 425 | bool signal( condition & this ) { | 
|---|
| 426 | if( is_empty( this ) ) { return false; } | 
|---|
| 427 |  | 
|---|
| 428 | //Check that everything is as expected | 
|---|
| 429 | verify( this.monitors ); | 
|---|
| 430 | verify( this.monitor_count != 0 ); | 
|---|
| 431 |  | 
|---|
| 432 | //Some more checking in debug | 
|---|
| 433 | __cfaabi_dbg_debug_do( | 
|---|
| 434 | thread_desc * this_thrd = TL_GET( this_thread ); | 
|---|
| 435 | if ( this.monitor_count != this_thrd->monitors.size ) { | 
|---|
| 436 | abort( "Signal on condition %p made with different number of monitor(s), expected %zi got %zi", &this, this.monitor_count, this_thrd->monitors.size ); | 
|---|
| 437 | } | 
|---|
| 438 |  | 
|---|
| 439 | for(int i = 0; i < this.monitor_count; i++) { | 
|---|
| 440 | if ( this.monitors[i] != this_thrd->monitors[i] ) { | 
|---|
| 441 | abort( "Signal on condition %p made with different monitor, expected %p got %p", &this, this.monitors[i], this_thrd->monitors[i] ); | 
|---|
| 442 | } | 
|---|
| 443 | } | 
|---|
| 444 | ); | 
|---|
| 445 |  | 
|---|
| 446 | __lock_size_t count = this.monitor_count; | 
|---|
| 447 |  | 
|---|
| 448 | // Lock all monitors | 
|---|
| 449 | lock_all( this.monitors, NULL, count ); | 
|---|
| 450 |  | 
|---|
| 451 | //Pop the head of the waiting queue | 
|---|
| 452 | __condition_node_t * node = pop_head( this.blocked ); | 
|---|
| 453 |  | 
|---|
| 454 | //Add the thread to the proper AS stack | 
|---|
| 455 | for(int i = 0; i < count; i++) { | 
|---|
| 456 | __condition_criterion_t * crit = &node->criteria[i]; | 
|---|
| 457 | assert( !crit->ready ); | 
|---|
| 458 | push( crit->target->signal_stack, crit ); | 
|---|
| 459 | } | 
|---|
| 460 |  | 
|---|
| 461 | //Release | 
|---|
| 462 | unlock_all( this.monitors, count ); | 
|---|
| 463 |  | 
|---|
| 464 | return true; | 
|---|
| 465 | } | 
|---|
| 466 |  | 
|---|
| 467 | bool signal_block( condition & this ) { | 
|---|
| 468 | if( !this.blocked.head ) { return false; } | 
|---|
| 469 |  | 
|---|
| 470 | //Check that everything is as expected | 
|---|
| 471 | verifyf( this.monitors != NULL, "Waiting with no monitors (%p)", this.monitors ); | 
|---|
| 472 | verifyf( this.monitor_count != 0, "Waiting with 0 monitors (%"PRIiFAST16")", this.monitor_count ); | 
|---|
| 473 |  | 
|---|
| 474 | // Create storage for monitor context | 
|---|
| 475 | monitor_ctx( this.monitors, this.monitor_count ); | 
|---|
| 476 |  | 
|---|
| 477 | // Lock all monitors (aggregates the locks them as well) | 
|---|
| 478 | lock_all( monitors, locks, count ); | 
|---|
| 479 |  | 
|---|
| 480 |  | 
|---|
| 481 | // Create the node specific to this wait operation | 
|---|
| 482 | wait_ctx_primed( kernelTLS.this_thread, 0 ) | 
|---|
| 483 |  | 
|---|
| 484 | //save contexts | 
|---|
| 485 | monitor_save; | 
|---|
| 486 |  | 
|---|
| 487 | //Find the thread to run | 
|---|
| 488 | thread_desc * signallee = pop_head( this.blocked )->waiting_thread; | 
|---|
| 489 | set_owner( monitors, count, signallee ); | 
|---|
| 490 |  | 
|---|
| 491 | __cfaabi_dbg_print_buffer_decl( "Kernel : signal_block condition %p (s: %p)\n", &this, signallee ); | 
|---|
| 492 |  | 
|---|
| 493 | //Everything is ready to go to sleep | 
|---|
| 494 | BlockInternal( locks, count, &signallee, 1 ); | 
|---|
| 495 |  | 
|---|
| 496 |  | 
|---|
| 497 | // WE WOKE UP | 
|---|
| 498 |  | 
|---|
| 499 |  | 
|---|
| 500 | __cfaabi_dbg_print_buffer_local( "Kernel :   signal_block returned\n" ); | 
|---|
| 501 |  | 
|---|
| 502 | //We are back, restore the masks and recursions | 
|---|
| 503 | monitor_restore; | 
|---|
| 504 |  | 
|---|
| 505 | return true; | 
|---|
| 506 | } | 
|---|
| 507 |  | 
|---|
| 508 | // Access the user_info of the thread waiting at the front of the queue | 
|---|
| 509 | uintptr_t front( condition & this ) { | 
|---|
| 510 | verifyf( !is_empty(this), | 
|---|
| 511 | "Attempt to access user data on an empty condition.\n" | 
|---|
| 512 | "Possible cause is not checking if the condition is empty before reading stored data." | 
|---|
| 513 | ); | 
|---|
| 514 | return ((typeof(this.blocked.head))this.blocked.head)->user_info; | 
|---|
| 515 | } | 
|---|
| 516 |  | 
|---|
| 517 | //----------------------------------------------------------------------------- | 
|---|
| 518 | // External scheduling | 
|---|
| 519 | // cases to handle : | 
|---|
| 520 | //      - target already there : | 
|---|
| 521 | //              block and wake | 
|---|
| 522 | //      - dtor already there | 
|---|
| 523 | //              put thread on signaller stack | 
|---|
| 524 | //      - non-blocking | 
|---|
| 525 | //              return else | 
|---|
| 526 | //      - timeout | 
|---|
| 527 | //              return timeout | 
|---|
| 528 | //      - block | 
|---|
| 529 | //              setup mask | 
|---|
| 530 | //              block | 
|---|
| 531 | void __waitfor_internal( const __waitfor_mask_t & mask, int duration ) { | 
|---|
| 532 | // This statment doesn't have a contiguous list of monitors... | 
|---|
| 533 | // Create one! | 
|---|
| 534 | __lock_size_t max = count_max( mask ); | 
|---|
| 535 | monitor_desc * mon_storage[max]; | 
|---|
| 536 | __builtin_memset( mon_storage, 0, sizeof( mon_storage ) ); | 
|---|
| 537 | __lock_size_t actual_count = aggregate( mon_storage, mask ); | 
|---|
| 538 |  | 
|---|
| 539 | __cfaabi_dbg_print_buffer_decl( "Kernel : waitfor %"PRIdFAST16" (s: %"PRIdFAST16", m: %"PRIdFAST16")\n", actual_count, mask.size, (__lock_size_t)max); | 
|---|
| 540 |  | 
|---|
| 541 | if(actual_count == 0) return; | 
|---|
| 542 |  | 
|---|
| 543 | __cfaabi_dbg_print_buffer_local( "Kernel : waitfor internal proceeding\n" ); | 
|---|
| 544 |  | 
|---|
| 545 | // Create storage for monitor context | 
|---|
| 546 | monitor_ctx( mon_storage, actual_count ); | 
|---|
| 547 |  | 
|---|
| 548 | // Lock all monitors (aggregates the locks as well) | 
|---|
| 549 | lock_all( monitors, locks, count ); | 
|---|
| 550 |  | 
|---|
| 551 | { | 
|---|
| 552 | // Check if the entry queue | 
|---|
| 553 | thread_desc * next; int index; | 
|---|
| 554 | [next, index] = search_entry_queue( mask, monitors, count ); | 
|---|
| 555 |  | 
|---|
| 556 | if( next ) { | 
|---|
| 557 | *mask.accepted = index; | 
|---|
| 558 | __acceptable_t& accepted = mask[index]; | 
|---|
| 559 | if( accepted.is_dtor ) { | 
|---|
| 560 | __cfaabi_dbg_print_buffer_local( "Kernel : dtor already there\n" ); | 
|---|
| 561 | verifyf( accepted.size == 1,  "ERROR: Accepted dtor has more than 1 mutex parameter." ); | 
|---|
| 562 |  | 
|---|
| 563 | monitor_desc * mon2dtor = accepted[0]; | 
|---|
| 564 | verifyf( mon2dtor->dtor_node, "ERROR: Accepted monitor has no dtor_node." ); | 
|---|
| 565 |  | 
|---|
| 566 | __condition_criterion_t * dtor_crit = mon2dtor->dtor_node->criteria; | 
|---|
| 567 | push( mon2dtor->signal_stack, dtor_crit ); | 
|---|
| 568 |  | 
|---|
| 569 | unlock_all( locks, count ); | 
|---|
| 570 | } | 
|---|
| 571 | else { | 
|---|
| 572 | __cfaabi_dbg_print_buffer_local( "Kernel : thread present, baton-passing\n" ); | 
|---|
| 573 |  | 
|---|
| 574 | // Create the node specific to this wait operation | 
|---|
| 575 | wait_ctx_primed( kernelTLS.this_thread, 0 ); | 
|---|
| 576 |  | 
|---|
| 577 | // Save monitor states | 
|---|
| 578 | monitor_save; | 
|---|
| 579 |  | 
|---|
| 580 | __cfaabi_dbg_print_buffer_local( "Kernel :  baton of %"PRIdFAST16" monitors : ", count ); | 
|---|
| 581 | #ifdef __CFA_DEBUG_PRINT__ | 
|---|
| 582 | for( int i = 0; i < count; i++) { | 
|---|
| 583 | __cfaabi_dbg_print_buffer_local( "%p %p ", monitors[i], monitors[i]->signal_stack.top ); | 
|---|
| 584 | } | 
|---|
| 585 | #endif | 
|---|
| 586 | __cfaabi_dbg_print_buffer_local( "\n" ); | 
|---|
| 587 |  | 
|---|
| 588 | // Set the owners to be the next thread | 
|---|
| 589 | set_owner( monitors, count, next ); | 
|---|
| 590 |  | 
|---|
| 591 | // Everything is ready to go to sleep | 
|---|
| 592 | BlockInternal( locks, count, &next, 1 ); | 
|---|
| 593 |  | 
|---|
| 594 | // We are back, restore the owners and recursions | 
|---|
| 595 | monitor_restore; | 
|---|
| 596 |  | 
|---|
| 597 | __cfaabi_dbg_print_buffer_local( "Kernel : thread present, returned\n" ); | 
|---|
| 598 | } | 
|---|
| 599 |  | 
|---|
| 600 | __cfaabi_dbg_print_buffer_local( "Kernel : accepted %d\n", *mask.accepted); | 
|---|
| 601 | return; | 
|---|
| 602 | } | 
|---|
| 603 | } | 
|---|
| 604 |  | 
|---|
| 605 |  | 
|---|
| 606 | if( duration == 0 ) { | 
|---|
| 607 | __cfaabi_dbg_print_buffer_local( "Kernel : non-blocking, exiting\n" ); | 
|---|
| 608 |  | 
|---|
| 609 | unlock_all( locks, count ); | 
|---|
| 610 |  | 
|---|
| 611 | __cfaabi_dbg_print_buffer_local( "Kernel : accepted %d\n", *mask.accepted); | 
|---|
| 612 | return; | 
|---|
| 613 | } | 
|---|
| 614 |  | 
|---|
| 615 |  | 
|---|
| 616 | verifyf( duration < 0, "Timeout on waitfor statments not supported yet." ); | 
|---|
| 617 |  | 
|---|
| 618 | __cfaabi_dbg_print_buffer_local( "Kernel : blocking waitfor\n" ); | 
|---|
| 619 |  | 
|---|
| 620 | // Create the node specific to this wait operation | 
|---|
| 621 | wait_ctx_primed( kernelTLS.this_thread, 0 ); | 
|---|
| 622 |  | 
|---|
| 623 | monitor_save; | 
|---|
| 624 | set_mask( monitors, count, mask ); | 
|---|
| 625 |  | 
|---|
| 626 | for( __lock_size_t i = 0; i < count; i++) { | 
|---|
| 627 | verify( monitors[i]->owner == kernelTLS.this_thread ); | 
|---|
| 628 | } | 
|---|
| 629 |  | 
|---|
| 630 | //Everything is ready to go to sleep | 
|---|
| 631 | BlockInternal( locks, count ); | 
|---|
| 632 |  | 
|---|
| 633 |  | 
|---|
| 634 | // WE WOKE UP | 
|---|
| 635 |  | 
|---|
| 636 |  | 
|---|
| 637 | //We are back, restore the masks and recursions | 
|---|
| 638 | monitor_restore; | 
|---|
| 639 |  | 
|---|
| 640 | __cfaabi_dbg_print_buffer_local( "Kernel : exiting\n" ); | 
|---|
| 641 |  | 
|---|
| 642 | __cfaabi_dbg_print_buffer_local( "Kernel : accepted %d\n", *mask.accepted); | 
|---|
| 643 | } | 
|---|
| 644 |  | 
|---|
| 645 | //----------------------------------------------------------------------------- | 
|---|
| 646 | // Utilities | 
|---|
| 647 |  | 
|---|
| 648 | static inline void set_owner( monitor_desc * this, thread_desc * owner ) { | 
|---|
| 649 | // __cfaabi_dbg_print_safe( "Kernal :   Setting owner of %p to %p ( was %p)\n", this, owner, this->owner ); | 
|---|
| 650 |  | 
|---|
| 651 | //Pass the monitor appropriately | 
|---|
| 652 | this->owner = owner; | 
|---|
| 653 |  | 
|---|
| 654 | //We are passing the monitor to someone else, which means recursion level is not 0 | 
|---|
| 655 | this->recursion = owner ? 1 : 0; | 
|---|
| 656 | } | 
|---|
| 657 |  | 
|---|
| 658 | static inline void set_owner( monitor_desc * monitors [], __lock_size_t count, thread_desc * owner ) { | 
|---|
| 659 | monitors[0]->owner     = owner; | 
|---|
| 660 | monitors[0]->recursion = 1; | 
|---|
| 661 | for( __lock_size_t i = 1; i < count; i++ ) { | 
|---|
| 662 | monitors[i]->owner     = owner; | 
|---|
| 663 | monitors[i]->recursion = 0; | 
|---|
| 664 | } | 
|---|
| 665 | } | 
|---|
| 666 |  | 
|---|
| 667 | static inline void set_mask( monitor_desc * storage [], __lock_size_t count, const __waitfor_mask_t & mask ) { | 
|---|
| 668 | for( __lock_size_t i = 0; i < count; i++) { | 
|---|
| 669 | storage[i]->mask = mask; | 
|---|
| 670 | } | 
|---|
| 671 | } | 
|---|
| 672 |  | 
|---|
| 673 | static inline void reset_mask( monitor_desc * this ) { | 
|---|
| 674 | this->mask.accepted = NULL; | 
|---|
| 675 | this->mask.data = NULL; | 
|---|
| 676 | this->mask.size = 0; | 
|---|
| 677 | } | 
|---|
| 678 |  | 
|---|
| 679 | static inline thread_desc * next_thread( monitor_desc * this ) { | 
|---|
| 680 | //Check the signaller stack | 
|---|
| 681 | __cfaabi_dbg_print_safe( "Kernel :  mon %p AS-stack top %p\n", this, this->signal_stack.top); | 
|---|
| 682 | __condition_criterion_t * urgent = pop( this->signal_stack ); | 
|---|
| 683 | if( urgent ) { | 
|---|
| 684 | //The signaller stack is not empty, | 
|---|
| 685 | //regardless of if we are ready to baton pass, | 
|---|
| 686 | //we need to set the monitor as in use | 
|---|
| 687 | set_owner( this,  urgent->owner->waiting_thread ); | 
|---|
| 688 |  | 
|---|
| 689 | return check_condition( urgent ); | 
|---|
| 690 | } | 
|---|
| 691 |  | 
|---|
| 692 | // No signaller thread | 
|---|
| 693 | // Get the next thread in the entry_queue | 
|---|
| 694 | thread_desc * new_owner = pop_head( this->entry_queue ); | 
|---|
| 695 | set_owner( this, new_owner ); | 
|---|
| 696 |  | 
|---|
| 697 | return new_owner; | 
|---|
| 698 | } | 
|---|
| 699 |  | 
|---|
| 700 | static inline bool is_accepted( monitor_desc * this, const __monitor_group_t & group ) { | 
|---|
| 701 | __acceptable_t * it = this->mask.data; // Optim | 
|---|
| 702 | __lock_size_t count = this->mask.size; | 
|---|
| 703 |  | 
|---|
| 704 | // Check if there are any acceptable functions | 
|---|
| 705 | if( !it ) return false; | 
|---|
| 706 |  | 
|---|
| 707 | // If this isn't the first monitor to test this, there is no reason to repeat the test. | 
|---|
| 708 | if( this != group[0] ) return group[0]->mask.accepted >= 0; | 
|---|
| 709 |  | 
|---|
| 710 | // For all acceptable functions check if this is the current function. | 
|---|
| 711 | for( __lock_size_t i = 0; i < count; i++, it++ ) { | 
|---|
| 712 | if( *it == group ) { | 
|---|
| 713 | *this->mask.accepted = i; | 
|---|
| 714 | return true; | 
|---|
| 715 | } | 
|---|
| 716 | } | 
|---|
| 717 |  | 
|---|
| 718 | // No function matched | 
|---|
| 719 | return false; | 
|---|
| 720 | } | 
|---|
| 721 |  | 
|---|
| 722 | static inline void init( __lock_size_t count, monitor_desc * monitors [], __condition_node_t & waiter, __condition_criterion_t criteria [] ) { | 
|---|
| 723 | for( __lock_size_t i = 0; i < count; i++) { | 
|---|
| 724 | (criteria[i]){ monitors[i], waiter }; | 
|---|
| 725 | } | 
|---|
| 726 |  | 
|---|
| 727 | waiter.criteria = criteria; | 
|---|
| 728 | } | 
|---|
| 729 |  | 
|---|
| 730 | static inline void init_push( __lock_size_t count, monitor_desc * monitors [], __condition_node_t & waiter, __condition_criterion_t criteria [] ) { | 
|---|
| 731 | for( __lock_size_t i = 0; i < count; i++) { | 
|---|
| 732 | (criteria[i]){ monitors[i], waiter }; | 
|---|
| 733 | __cfaabi_dbg_print_safe( "Kernel :  target %p = %p\n", criteria[i].target, &criteria[i] ); | 
|---|
| 734 | push( criteria[i].target->signal_stack, &criteria[i] ); | 
|---|
| 735 | } | 
|---|
| 736 |  | 
|---|
| 737 | waiter.criteria = criteria; | 
|---|
| 738 | } | 
|---|
| 739 |  | 
|---|
| 740 | static inline void lock_all( __spinlock_t * locks [], __lock_size_t count ) { | 
|---|
| 741 | for( __lock_size_t i = 0; i < count; i++ ) { | 
|---|
| 742 | lock( *locks[i] __cfaabi_dbg_ctx2 ); | 
|---|
| 743 | } | 
|---|
| 744 | } | 
|---|
| 745 |  | 
|---|
| 746 | static inline void lock_all( monitor_desc * source [], __spinlock_t * /*out*/ locks [], __lock_size_t count ) { | 
|---|
| 747 | for( __lock_size_t i = 0; i < count; i++ ) { | 
|---|
| 748 | __spinlock_t * l = &source[i]->lock; | 
|---|
| 749 | lock( *l __cfaabi_dbg_ctx2 ); | 
|---|
| 750 | if(locks) locks[i] = l; | 
|---|
| 751 | } | 
|---|
| 752 | } | 
|---|
| 753 |  | 
|---|
| 754 | static inline void unlock_all( __spinlock_t * locks [], __lock_size_t count ) { | 
|---|
| 755 | for( __lock_size_t i = 0; i < count; i++ ) { | 
|---|
| 756 | unlock( *locks[i] ); | 
|---|
| 757 | } | 
|---|
| 758 | } | 
|---|
| 759 |  | 
|---|
| 760 | static inline void unlock_all( monitor_desc * locks [], __lock_size_t count ) { | 
|---|
| 761 | for( __lock_size_t i = 0; i < count; i++ ) { | 
|---|
| 762 | unlock( locks[i]->lock ); | 
|---|
| 763 | } | 
|---|
| 764 | } | 
|---|
| 765 |  | 
|---|
| 766 | static inline void save( | 
|---|
| 767 | monitor_desc * ctx [], | 
|---|
| 768 | __lock_size_t count, | 
|---|
| 769 | __attribute((unused)) __spinlock_t * locks [], | 
|---|
| 770 | unsigned int /*out*/ recursions [], | 
|---|
| 771 | __waitfor_mask_t /*out*/ masks [] | 
|---|
| 772 | ) { | 
|---|
| 773 | for( __lock_size_t i = 0; i < count; i++ ) { | 
|---|
| 774 | recursions[i] = ctx[i]->recursion; | 
|---|
| 775 | masks[i]      = ctx[i]->mask; | 
|---|
| 776 | } | 
|---|
| 777 | } | 
|---|
| 778 |  | 
|---|
| 779 | static inline void restore( | 
|---|
| 780 | monitor_desc * ctx [], | 
|---|
| 781 | __lock_size_t count, | 
|---|
| 782 | __spinlock_t * locks [], | 
|---|
| 783 | unsigned int /*out*/ recursions [], | 
|---|
| 784 | __waitfor_mask_t /*out*/ masks [] | 
|---|
| 785 | ) { | 
|---|
| 786 | lock_all( locks, count ); | 
|---|
| 787 | for( __lock_size_t i = 0; i < count; i++ ) { | 
|---|
| 788 | ctx[i]->recursion = recursions[i]; | 
|---|
| 789 | ctx[i]->mask      = masks[i]; | 
|---|
| 790 | } | 
|---|
| 791 | unlock_all( locks, count ); | 
|---|
| 792 | } | 
|---|
| 793 |  | 
|---|
| 794 | // Function has 2 different behavior | 
|---|
| 795 | // 1 - Marks a monitors as being ready to run | 
|---|
| 796 | // 2 - Checks if all the monitors are ready to run | 
|---|
| 797 | //     if so return the thread to run | 
|---|
| 798 | static inline thread_desc * check_condition( __condition_criterion_t * target ) { | 
|---|
| 799 | __condition_node_t * node = target->owner; | 
|---|
| 800 | unsigned short count = node->count; | 
|---|
| 801 | __condition_criterion_t * criteria = node->criteria; | 
|---|
| 802 |  | 
|---|
| 803 | bool ready2run = true; | 
|---|
| 804 |  | 
|---|
| 805 | for(    int i = 0; i < count; i++ ) { | 
|---|
| 806 |  | 
|---|
| 807 | // __cfaabi_dbg_print_safe( "Checking %p for %p\n", &criteria[i], target ); | 
|---|
| 808 | if( &criteria[i] == target ) { | 
|---|
| 809 | criteria[i].ready = true; | 
|---|
| 810 | // __cfaabi_dbg_print_safe( "True\n" ); | 
|---|
| 811 | } | 
|---|
| 812 |  | 
|---|
| 813 | ready2run = criteria[i].ready && ready2run; | 
|---|
| 814 | } | 
|---|
| 815 |  | 
|---|
| 816 | __cfaabi_dbg_print_safe( "Kernel :  Runing %i (%p)\n", ready2run, ready2run ? node->waiting_thread : NULL ); | 
|---|
| 817 | return ready2run ? node->waiting_thread : NULL; | 
|---|
| 818 | } | 
|---|
| 819 |  | 
|---|
| 820 | static inline void brand_condition( condition & this ) { | 
|---|
| 821 | thread_desc * thrd = TL_GET( this_thread ); | 
|---|
| 822 | if( !this.monitors ) { | 
|---|
| 823 | // __cfaabi_dbg_print_safe( "Branding\n" ); | 
|---|
| 824 | assertf( thrd->monitors.data != NULL, "No current monitor to brand condition %p", thrd->monitors.data ); | 
|---|
| 825 | this.monitor_count = thrd->monitors.size; | 
|---|
| 826 |  | 
|---|
| 827 | this.monitors = (monitor_desc **)malloc( this.monitor_count * sizeof( *this.monitors ) ); | 
|---|
| 828 | for( int i = 0; i < this.monitor_count; i++ ) { | 
|---|
| 829 | this.monitors[i] = thrd->monitors[i]; | 
|---|
| 830 | } | 
|---|
| 831 | } | 
|---|
| 832 | } | 
|---|
| 833 |  | 
|---|
| 834 | static inline [thread_desc *, int] search_entry_queue( const __waitfor_mask_t & mask, monitor_desc * monitors [], __lock_size_t count ) { | 
|---|
| 835 |  | 
|---|
| 836 | __queue_t(thread_desc) & entry_queue = monitors[0]->entry_queue; | 
|---|
| 837 |  | 
|---|
| 838 | // For each thread in the entry-queue | 
|---|
| 839 | for(    thread_desc ** thrd_it = &entry_queue.head; | 
|---|
| 840 | *thrd_it; | 
|---|
| 841 | thrd_it = &(*thrd_it)->next | 
|---|
| 842 | ) { | 
|---|
| 843 | // For each acceptable check if it matches | 
|---|
| 844 | int i = 0; | 
|---|
| 845 | __acceptable_t * end   = end  (mask); | 
|---|
| 846 | __acceptable_t * begin = begin(mask); | 
|---|
| 847 | for( __acceptable_t * it = begin; it != end; it++, i++ ) { | 
|---|
| 848 | // Check if we have a match | 
|---|
| 849 | if( *it == (*thrd_it)->monitors ) { | 
|---|
| 850 |  | 
|---|
| 851 | // If we have a match return it | 
|---|
| 852 | // after removeing it from the entry queue | 
|---|
| 853 | return [remove( entry_queue, thrd_it ), i]; | 
|---|
| 854 | } | 
|---|
| 855 | } | 
|---|
| 856 | } | 
|---|
| 857 |  | 
|---|
| 858 | return [0, -1]; | 
|---|
| 859 | } | 
|---|
| 860 |  | 
|---|
| 861 | forall(dtype T | sized( T )) | 
|---|
| 862 | static inline __lock_size_t insert_unique( T * array [], __lock_size_t & size, T * val ) { | 
|---|
| 863 | if( !val ) return size; | 
|---|
| 864 |  | 
|---|
| 865 | for( __lock_size_t i = 0; i <= size; i++) { | 
|---|
| 866 | if( array[i] == val ) return size; | 
|---|
| 867 | } | 
|---|
| 868 |  | 
|---|
| 869 | array[size] = val; | 
|---|
| 870 | size = size + 1; | 
|---|
| 871 | return size; | 
|---|
| 872 | } | 
|---|
| 873 |  | 
|---|
| 874 | static inline __lock_size_t count_max( const __waitfor_mask_t & mask ) { | 
|---|
| 875 | __lock_size_t max = 0; | 
|---|
| 876 | for( __lock_size_t i = 0; i < mask.size; i++ ) { | 
|---|
| 877 | __acceptable_t & accepted = mask[i]; | 
|---|
| 878 | max += accepted.size; | 
|---|
| 879 | } | 
|---|
| 880 | return max; | 
|---|
| 881 | } | 
|---|
| 882 |  | 
|---|
| 883 | static inline __lock_size_t aggregate( monitor_desc * storage [], const __waitfor_mask_t & mask ) { | 
|---|
| 884 | __lock_size_t size = 0; | 
|---|
| 885 | for( __lock_size_t i = 0; i < mask.size; i++ ) { | 
|---|
| 886 | __acceptable_t & accepted = mask[i]; | 
|---|
| 887 | __libcfa_small_sort( accepted.data, accepted.size ); | 
|---|
| 888 | for( __lock_size_t j = 0; j < accepted.size; j++) { | 
|---|
| 889 | insert_unique( storage, size, accepted[j] ); | 
|---|
| 890 | } | 
|---|
| 891 | } | 
|---|
| 892 | // TODO insertion sort instead of this | 
|---|
| 893 | __libcfa_small_sort( storage, size ); | 
|---|
| 894 | return size; | 
|---|
| 895 | } | 
|---|
| 896 |  | 
|---|
| 897 | // Local Variables: // | 
|---|
| 898 | // mode: c // | 
|---|
| 899 | // tab-width: 4 // | 
|---|
| 900 | // End: // | 
|---|