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