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