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