| [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 |  | 
|---|
| [73abe95] | 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 |  | 
|---|
| [6b224a52] | 371 |  | 
|---|
| [97e3296] | 372 | // Dtor for monitor guard | 
|---|
| [242a902] | 373 | void ^?{}( monitor_guard_t & this ) { | 
|---|
| [169d944] | 374 | // __cfaabi_dbg_print_safe( "MGUARD : leaving %d\n", this.count); | 
|---|
| [a843067] | 375 |  | 
|---|
| [97e3296] | 376 | // Leave the monitors in order | 
|---|
| [242a902] | 377 | leave( this.m, this.count ); | 
|---|
| [5ea06d6] | 378 |  | 
|---|
| [169d944] | 379 | // __cfaabi_dbg_print_safe( "MGUARD : left\n" ); | 
|---|
| [a843067] | 380 |  | 
|---|
| [97e3296] | 381 | // Restore thread context | 
|---|
| [8fc652e0] | 382 | active_thread()->monitors = this.prev; | 
|---|
| [5ea06d6] | 383 | } | 
|---|
|  | 384 |  | 
|---|
| [549c006] | 385 | // Ctor for monitor guard | 
|---|
|  | 386 | // Sorts monitors before entering | 
|---|
| [e84ab3d] | 387 | void ?{}( monitor_dtor_guard_t & this, monitor$ * m [], fptr_t func, bool join ) { | 
|---|
| [14a61b5] | 388 | // optimization | 
|---|
| [e84ab3d] | 389 | thread$ * thrd = active_thread(); | 
|---|
| [14a61b5] | 390 |  | 
|---|
| [549c006] | 391 | // Store current array | 
|---|
|  | 392 | this.m = *m; | 
|---|
|  | 393 |  | 
|---|
|  | 394 | // Save previous thread context | 
|---|
| [14a61b5] | 395 | this.prev = thrd->monitors; | 
|---|
| [549c006] | 396 |  | 
|---|
| [77a2994] | 397 | // Save whether we are in a join or not | 
|---|
|  | 398 | this.join = join; | 
|---|
|  | 399 |  | 
|---|
| [549c006] | 400 | // Update thread context (needed for conditions) | 
|---|
| [14a61b5] | 401 | (thrd->monitors){m, 1, func}; | 
|---|
| [549c006] | 402 |  | 
|---|
| [77a2994] | 403 | __dtor_enter( this.m, func, join ); | 
|---|
| [549c006] | 404 | } | 
|---|
|  | 405 |  | 
|---|
|  | 406 | // Dtor for monitor guard | 
|---|
|  | 407 | void ^?{}( monitor_dtor_guard_t & this ) { | 
|---|
|  | 408 | // Leave the monitors in order | 
|---|
| [77a2994] | 409 | __dtor_leave( this.m, this.join ); | 
|---|
| [549c006] | 410 |  | 
|---|
|  | 411 | // Restore thread context | 
|---|
| [8fc652e0] | 412 | active_thread()->monitors = this.prev; | 
|---|
| [549c006] | 413 | } | 
|---|
|  | 414 |  | 
|---|
| [97e3296] | 415 | //----------------------------------------------------------------------------- | 
|---|
|  | 416 | // Internal scheduling types | 
|---|
| [e84ab3d] | 417 | void ?{}(__condition_node_t & this, thread$ * waiting_thread, __lock_size_t count, uintptr_t user_info ) { | 
|---|
| [242a902] | 418 | this.waiting_thread = waiting_thread; | 
|---|
|  | 419 | this.count = count; | 
|---|
| [121be3e] | 420 | this.next = 0p; | 
|---|
| [242a902] | 421 | this.user_info = user_info; | 
|---|
| [be3d020] | 422 | } | 
|---|
|  | 423 |  | 
|---|
| [c40e7c5] | 424 | void ?{}(__condition_criterion_t & this ) with( this ) { | 
|---|
|  | 425 | ready  = false; | 
|---|
| [121be3e] | 426 | target = 0p; | 
|---|
|  | 427 | owner  = 0p; | 
|---|
|  | 428 | next   = 0p; | 
|---|
| [be3d020] | 429 | } | 
|---|
|  | 430 |  | 
|---|
| [e84ab3d] | 431 | void ?{}(__condition_criterion_t & this, monitor$ * target, __condition_node_t & owner ) { | 
|---|
| [242a902] | 432 | this.ready  = false; | 
|---|
|  | 433 | this.target = target; | 
|---|
| [8fc45b7] | 434 | this.owner  = &owner; | 
|---|
| [121be3e] | 435 | this.next   = 0p; | 
|---|
| [ad1a8dd] | 436 | } | 
|---|
|  | 437 |  | 
|---|
| [5ea06d6] | 438 | //----------------------------------------------------------------------------- | 
|---|
|  | 439 | // Internal scheduling | 
|---|
| [4cedd9f] | 440 | void wait( condition & this, uintptr_t user_info = 0 ) { | 
|---|
| [0c78741] | 441 | brand_condition( this ); | 
|---|
| [5ea06d6] | 442 |  | 
|---|
| [97e3296] | 443 | // Check that everything is as expected | 
|---|
| [121be3e] | 444 | assertf( this.monitors != 0p, "Waiting with no monitors (%p)", this.monitors ); | 
|---|
| [2f6a7e93] | 445 | verifyf( this.monitor_count != 0, "Waiting with 0 monitors (%"PRIiFAST16")", this.monitor_count ); | 
|---|
|  | 446 | verifyf( this.monitor_count < 32u, "Excessive monitor count (%"PRIiFAST16")", this.monitor_count ); | 
|---|
| [5ea06d6] | 447 |  | 
|---|
| [97e3296] | 448 | // Create storage for monitor context | 
|---|
| [4cedd9f] | 449 | monitor_ctx( this.monitors, this.monitor_count ); | 
|---|
| [0c78741] | 450 |  | 
|---|
| [97e3296] | 451 | // Create the node specific to this wait operation | 
|---|
| [8fc652e0] | 452 | wait_ctx( active_thread(), user_info ); | 
|---|
| [0c78741] | 453 |  | 
|---|
| [97e3296] | 454 | // Append the current wait operation to the ones already queued on the condition | 
|---|
|  | 455 | // We don't need locks for that since conditions must always be waited on inside monitor mutual exclusion | 
|---|
| [3381ed7] | 456 | /* paranoid */ verify( waiter.next == 0p ); | 
|---|
| [8fc45b7] | 457 | append( this.blocked, &waiter ); | 
|---|
| [3381ed7] | 458 | /* paranoid */ verify( waiter.next == 1p ); | 
|---|
| [0c78741] | 459 |  | 
|---|
| [6ff4507] | 460 | // Lock all monitors (aggregates the locks as well) | 
|---|
| [97e3296] | 461 | lock_all( monitors, locks, count ); | 
|---|
| [5ea06d6] | 462 |  | 
|---|
| [97e3296] | 463 | // Find the next thread(s) to run | 
|---|
| [59a0bde] | 464 | __lock_size_t thread_count = 0; | 
|---|
| [e84ab3d] | 465 | thread$ * threads[ count ]; | 
|---|
| [66298de] | 466 | __builtin_memset( threads, 0, sizeof( threads ) ); | 
|---|
| [ad1a8dd] | 467 |  | 
|---|
| [a843067] | 468 | // Save monitor states | 
|---|
|  | 469 | monitor_save; | 
|---|
|  | 470 |  | 
|---|
| [97e3296] | 471 | // Remove any duplicate threads | 
|---|
| [59a0bde] | 472 | for( __lock_size_t i = 0; i < count; i++) { | 
|---|
| [e84ab3d] | 473 | thread$ * new_owner = next_thread( monitors[i] ); | 
|---|
| [6ff4507] | 474 | insert_unique( threads, thread_count, new_owner ); | 
|---|
| [5ea06d6] | 475 | } | 
|---|
|  | 476 |  | 
|---|
| [3381ed7] | 477 | // Unlock the locks, we don't need them anymore | 
|---|
|  | 478 | for(int i = 0; i < count; i++) { | 
|---|
|  | 479 | unlock( *locks[i] ); | 
|---|
|  | 480 | } | 
|---|
|  | 481 |  | 
|---|
|  | 482 | // Wake the threads | 
|---|
|  | 483 | for(int i = 0; i < thread_count; i++) { | 
|---|
| [e235429] | 484 | unpark( threads[i] ); | 
|---|
| [3381ed7] | 485 | } | 
|---|
|  | 486 |  | 
|---|
| [a843067] | 487 | // Everything is ready to go to sleep | 
|---|
| [e235429] | 488 | park(); | 
|---|
| [a843067] | 489 |  | 
|---|
|  | 490 | // We are back, restore the owners and recursions | 
|---|
|  | 491 | monitor_restore; | 
|---|
| [5ea06d6] | 492 | } | 
|---|
|  | 493 |  | 
|---|
| [4cedd9f] | 494 | bool signal( condition & this ) { | 
|---|
| [97e3296] | 495 | if( is_empty( this ) ) { return false; } | 
|---|
| [5ea06d6] | 496 |  | 
|---|
|  | 497 | //Check that everything is as expected | 
|---|
| [4cedd9f] | 498 | verify( this.monitors ); | 
|---|
|  | 499 | verify( this.monitor_count != 0 ); | 
|---|
| [0c78741] | 500 |  | 
|---|
| [44264c5] | 501 | //Some more checking in debug | 
|---|
| [36982fc] | 502 | __cfaabi_dbg_debug_do( | 
|---|
| [e84ab3d] | 503 | thread$ * this_thrd = active_thread(); | 
|---|
| [4cedd9f] | 504 | if ( this.monitor_count != this_thrd->monitors.size ) { | 
|---|
| [c2ca04d] | 505 | 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] | 506 | } | 
|---|
| [0c78741] | 507 |  | 
|---|
| [4cedd9f] | 508 | for(int i = 0; i < this.monitor_count; i++) { | 
|---|
| [0cf5b79] | 509 | if ( this.monitors[i] != this_thrd->monitors[i] ) { | 
|---|
| [2fdbb3b] | 510 | abort( "Signal on condition %p made with different monitor, expected %p got %p", &this, this.monitors[i], this_thrd->monitors[i] ); | 
|---|
| [97e3296] | 511 | } | 
|---|
| [0c78741] | 512 | } | 
|---|
| [5ea06d6] | 513 | ); | 
|---|
|  | 514 |  | 
|---|
| [59a0bde] | 515 | __lock_size_t count = this.monitor_count; | 
|---|
| [97e3296] | 516 |  | 
|---|
|  | 517 | // Lock all monitors | 
|---|
| [121be3e] | 518 | lock_all( this.monitors, 0p, count ); | 
|---|
| [0c78741] | 519 |  | 
|---|
| [44264c5] | 520 | //Pop the head of the waiting queue | 
|---|
| [8fc45b7] | 521 | __condition_node_t * node = pop_head( this.blocked ); | 
|---|
| [44264c5] | 522 |  | 
|---|
|  | 523 | //Add the thread to the proper AS stack | 
|---|
| [0c78741] | 524 | for(int i = 0; i < count; i++) { | 
|---|
|  | 525 | __condition_criterion_t * crit = &node->criteria[i]; | 
|---|
|  | 526 | assert( !crit->ready ); | 
|---|
| [8fc45b7] | 527 | push( crit->target->signal_stack, crit ); | 
|---|
| [5ea06d6] | 528 | } | 
|---|
| [0c78741] | 529 |  | 
|---|
| [44264c5] | 530 | //Release | 
|---|
| [4cedd9f] | 531 | unlock_all( this.monitors, count ); | 
|---|
| [be3d020] | 532 |  | 
|---|
|  | 533 | return true; | 
|---|
| [5ea06d6] | 534 | } | 
|---|
|  | 535 |  | 
|---|
| [4cedd9f] | 536 | bool signal_block( condition & this ) { | 
|---|
|  | 537 | if( !this.blocked.head ) { return false; } | 
|---|
| [44264c5] | 538 |  | 
|---|
|  | 539 | //Check that everything is as expected | 
|---|
| [121be3e] | 540 | verifyf( this.monitors != 0p, "Waiting with no monitors (%p)", this.monitors ); | 
|---|
| [2f6a7e93] | 541 | verifyf( this.monitor_count != 0, "Waiting with 0 monitors (%"PRIiFAST16")", this.monitor_count ); | 
|---|
| [44264c5] | 542 |  | 
|---|
| [97e3296] | 543 | // Create storage for monitor context | 
|---|
| [4cedd9f] | 544 | monitor_ctx( this.monitors, this.monitor_count ); | 
|---|
| [44264c5] | 545 |  | 
|---|
| [97e3296] | 546 | // Lock all monitors (aggregates the locks them as well) | 
|---|
|  | 547 | lock_all( monitors, locks, count ); | 
|---|
| [44264c5] | 548 |  | 
|---|
| [2e9aed4] | 549 |  | 
|---|
| [97e3296] | 550 | // Create the node specific to this wait operation | 
|---|
| [8fc652e0] | 551 | wait_ctx_primed( active_thread(), 0 ) | 
|---|
| [44264c5] | 552 |  | 
|---|
|  | 553 | //save contexts | 
|---|
| [6ff4507] | 554 | monitor_save; | 
|---|
| [44264c5] | 555 |  | 
|---|
|  | 556 | //Find the thread to run | 
|---|
| [e84ab3d] | 557 | thread$ * signallee = pop_head( this.blocked )->waiting_thread; | 
|---|
| [c7a900a] | 558 | __set_owner( monitors, count, signallee ); | 
|---|
| [44264c5] | 559 |  | 
|---|
| [36982fc] | 560 | __cfaabi_dbg_print_buffer_decl( "Kernel : signal_block condition %p (s: %p)\n", &this, signallee ); | 
|---|
| [de737c8] | 561 |  | 
|---|
| [3381ed7] | 562 | // unlock all the monitors | 
|---|
|  | 563 | unlock_all( locks, count ); | 
|---|
|  | 564 |  | 
|---|
|  | 565 | // unpark the thread we signalled | 
|---|
| [e235429] | 566 | unpark( signallee ); | 
|---|
| [3381ed7] | 567 |  | 
|---|
| [44264c5] | 568 | //Everything is ready to go to sleep | 
|---|
| [e235429] | 569 | park(); | 
|---|
| [44264c5] | 570 |  | 
|---|
| [c81ebf9] | 571 |  | 
|---|
| [97e3296] | 572 | // WE WOKE UP | 
|---|
| [c81ebf9] | 573 |  | 
|---|
|  | 574 |  | 
|---|
| [36982fc] | 575 | __cfaabi_dbg_print_buffer_local( "Kernel :   signal_block returned\n" ); | 
|---|
| [de737c8] | 576 |  | 
|---|
| [6ff4507] | 577 | //We are back, restore the masks and recursions | 
|---|
|  | 578 | monitor_restore; | 
|---|
| [be3d020] | 579 |  | 
|---|
|  | 580 | return true; | 
|---|
|  | 581 | } | 
|---|
|  | 582 |  | 
|---|
| [97e3296] | 583 | // Access the user_info of the thread waiting at the front of the queue | 
|---|
| [4cedd9f] | 584 | uintptr_t front( condition & this ) { | 
|---|
| [2ac095d] | 585 | verifyf( !is_empty(this), | 
|---|
| [4aa2fb2] | 586 | "Attempt to access user data on an empty condition.\n" | 
|---|
|  | 587 | "Possible cause is not checking if the condition is empty before reading stored data." | 
|---|
| [be3d020] | 588 | ); | 
|---|
| [0cf5b79] | 589 | return ((typeof(this.blocked.head))this.blocked.head)->user_info; | 
|---|
| [44264c5] | 590 | } | 
|---|
|  | 591 |  | 
|---|
| [c81ebf9] | 592 | //----------------------------------------------------------------------------- | 
|---|
| [b18830e] | 593 | // External scheduling | 
|---|
|  | 594 | // cases to handle : | 
|---|
|  | 595 | //      - target already there : | 
|---|
|  | 596 | //              block and wake | 
|---|
|  | 597 | //      - dtor already there | 
|---|
|  | 598 | //              put thread on signaller stack | 
|---|
|  | 599 | //      - non-blocking | 
|---|
|  | 600 | //              return else | 
|---|
|  | 601 | //      - timeout | 
|---|
|  | 602 | //              return timeout | 
|---|
|  | 603 | //      - block | 
|---|
|  | 604 | //              setup mask | 
|---|
|  | 605 | //              block | 
|---|
| [6ae8c92] | 606 | void __waitfor_internal( const __waitfor_mask_t & mask, int duration ) { | 
|---|
| [b18830e] | 607 | // This statment doesn't have a contiguous list of monitors... | 
|---|
|  | 608 | // Create one! | 
|---|
| [59a0bde] | 609 | __lock_size_t max = count_max( mask ); | 
|---|
| [e84ab3d] | 610 | monitor$ * mon_storage[max]; | 
|---|
| [66298de] | 611 | __builtin_memset( mon_storage, 0, sizeof( mon_storage ) ); | 
|---|
| [59a0bde] | 612 | __lock_size_t actual_count = aggregate( mon_storage, mask ); | 
|---|
| [97e3296] | 613 |  | 
|---|
| [523232d] | 614 | __cfaabi_dbg_print_buffer_decl( "Kernel : waitfor %"PRIdFAST16" (s: %"PRIdFAST16", m: %"PRIdFAST16")\n", actual_count, mask.size, (__lock_size_t)max); | 
|---|
| [66298de] | 615 |  | 
|---|
| [daacf82] | 616 | if(actual_count == 0) return; | 
|---|
| [19c43b7] | 617 |  | 
|---|
| [169d944] | 618 | __cfaabi_dbg_print_buffer_local( "Kernel : waitfor internal proceeding\n" ); | 
|---|
| [4cc9b13] | 619 |  | 
|---|
| [97e3296] | 620 | // Create storage for monitor context | 
|---|
| [b18830e] | 621 | monitor_ctx( mon_storage, actual_count ); | 
|---|
| [c81ebf9] | 622 |  | 
|---|
| [6ff4507] | 623 | // Lock all monitors (aggregates the locks as well) | 
|---|
| [97e3296] | 624 | lock_all( monitors, locks, count ); | 
|---|
| [c81ebf9] | 625 |  | 
|---|
| [b18830e] | 626 | { | 
|---|
|  | 627 | // Check if the entry queue | 
|---|
| [e84ab3d] | 628 | thread$ * next; int index; | 
|---|
| [6ae8c92] | 629 | [next, index] = search_entry_queue( mask, monitors, count ); | 
|---|
| [b18830e] | 630 |  | 
|---|
|  | 631 | if( next ) { | 
|---|
| [19c43b7] | 632 | *mask.accepted = index; | 
|---|
| [0cf5b79] | 633 | __acceptable_t& accepted = mask[index]; | 
|---|
|  | 634 | if( accepted.is_dtor ) { | 
|---|
| [169d944] | 635 | __cfaabi_dbg_print_buffer_local( "Kernel : dtor already there\n" ); | 
|---|
| [0cf5b79] | 636 | verifyf( accepted.size == 1,  "ERROR: Accepted dtor has more than 1 mutex parameter." ); | 
|---|
| [549c006] | 637 |  | 
|---|
| [e84ab3d] | 638 | monitor$ * mon2dtor = accepted[0]; | 
|---|
| [549c006] | 639 | verifyf( mon2dtor->dtor_node, "ERROR: Accepted monitor has no dtor_node." ); | 
|---|
|  | 640 |  | 
|---|
|  | 641 | __condition_criterion_t * dtor_crit = mon2dtor->dtor_node->criteria; | 
|---|
| [8fc45b7] | 642 | push( mon2dtor->signal_stack, dtor_crit ); | 
|---|
| [549c006] | 643 |  | 
|---|
|  | 644 | unlock_all( locks, count ); | 
|---|
| [b18830e] | 645 | } | 
|---|
|  | 646 | else { | 
|---|
| [169d944] | 647 | __cfaabi_dbg_print_buffer_local( "Kernel : thread present, baton-passing\n" ); | 
|---|
| [19c43b7] | 648 |  | 
|---|
|  | 649 | // Create the node specific to this wait operation | 
|---|
| [8fc652e0] | 650 | wait_ctx_primed( active_thread(), 0 ); | 
|---|
| [19c43b7] | 651 |  | 
|---|
|  | 652 | // Save monitor states | 
|---|
|  | 653 | monitor_save; | 
|---|
|  | 654 |  | 
|---|
| [523232d] | 655 | __cfaabi_dbg_print_buffer_local( "Kernel :  baton of %"PRIdFAST16" monitors : ", count ); | 
|---|
| [6a5be52] | 656 | #ifdef __CFA_DEBUG_PRINT__ | 
|---|
|  | 657 | for( int i = 0; i < count; i++) { | 
|---|
| [36982fc] | 658 | __cfaabi_dbg_print_buffer_local( "%p %p ", monitors[i], monitors[i]->signal_stack.top ); | 
|---|
| [6a5be52] | 659 | } | 
|---|
|  | 660 | #endif | 
|---|
| [169d944] | 661 | __cfaabi_dbg_print_buffer_local( "\n" ); | 
|---|
| [6a5be52] | 662 |  | 
|---|
| [19c43b7] | 663 | // Set the owners to be the next thread | 
|---|
| [c7a900a] | 664 | __set_owner( monitors, count, next ); | 
|---|
| [19c43b7] | 665 |  | 
|---|
| [3381ed7] | 666 | // unlock all the monitors | 
|---|
|  | 667 | unlock_all( locks, count ); | 
|---|
| [19c43b7] | 668 |  | 
|---|
| [3381ed7] | 669 | // unpark the thread we signalled | 
|---|
| [e235429] | 670 | unpark( next ); | 
|---|
| [3381ed7] | 671 |  | 
|---|
|  | 672 | //Everything is ready to go to sleep | 
|---|
| [e235429] | 673 | park(); | 
|---|
| [19c43b7] | 674 |  | 
|---|
|  | 675 | // We are back, restore the owners and recursions | 
|---|
|  | 676 | monitor_restore; | 
|---|
|  | 677 |  | 
|---|
| [169d944] | 678 | __cfaabi_dbg_print_buffer_local( "Kernel : thread present, returned\n" ); | 
|---|
| [b18830e] | 679 | } | 
|---|
|  | 680 |  | 
|---|
| [36982fc] | 681 | __cfaabi_dbg_print_buffer_local( "Kernel : accepted %d\n", *mask.accepted); | 
|---|
| [19c43b7] | 682 | return; | 
|---|
| [90c4df0] | 683 | } | 
|---|
|  | 684 | } | 
|---|
|  | 685 |  | 
|---|
| [c81ebf9] | 686 |  | 
|---|
| [4cc9b13] | 687 | if( duration == 0 ) { | 
|---|
| [169d944] | 688 | __cfaabi_dbg_print_buffer_local( "Kernel : non-blocking, exiting\n" ); | 
|---|
| [19c43b7] | 689 |  | 
|---|
| [4cc9b13] | 690 | unlock_all( locks, count ); | 
|---|
| [19c43b7] | 691 |  | 
|---|
| [36982fc] | 692 | __cfaabi_dbg_print_buffer_local( "Kernel : accepted %d\n", *mask.accepted); | 
|---|
| [4cc9b13] | 693 | return; | 
|---|
|  | 694 | } | 
|---|
| [b18830e] | 695 |  | 
|---|
|  | 696 |  | 
|---|
| [169d944] | 697 | verifyf( duration < 0, "Timeout on waitfor statments not supported yet." ); | 
|---|
| [b18830e] | 698 |  | 
|---|
| [169d944] | 699 | __cfaabi_dbg_print_buffer_local( "Kernel : blocking waitfor\n" ); | 
|---|
| [19c43b7] | 700 |  | 
|---|
|  | 701 | // Create the node specific to this wait operation | 
|---|
| [8fc652e0] | 702 | wait_ctx_primed( active_thread(), 0 ); | 
|---|
| [b18830e] | 703 |  | 
|---|
| [6ff4507] | 704 | monitor_save; | 
|---|
| [6ae8c92] | 705 | set_mask( monitors, count, mask ); | 
|---|
| [c81ebf9] | 706 |  | 
|---|
| [59a0bde] | 707 | for( __lock_size_t i = 0; i < count; i++) { | 
|---|
| [8fc652e0] | 708 | verify( monitors[i]->owner == active_thread() ); | 
|---|
| [daacf82] | 709 | } | 
|---|
|  | 710 |  | 
|---|
| [3381ed7] | 711 | // unlock all the monitors | 
|---|
|  | 712 | unlock_all( locks, count ); | 
|---|
|  | 713 |  | 
|---|
| [19c43b7] | 714 | //Everything is ready to go to sleep | 
|---|
| [e235429] | 715 | park(); | 
|---|
| [19c43b7] | 716 |  | 
|---|
|  | 717 |  | 
|---|
|  | 718 | // WE WOKE UP | 
|---|
|  | 719 |  | 
|---|
|  | 720 |  | 
|---|
|  | 721 | //We are back, restore the masks and recursions | 
|---|
|  | 722 | monitor_restore; | 
|---|
|  | 723 |  | 
|---|
| [169d944] | 724 | __cfaabi_dbg_print_buffer_local( "Kernel : exiting\n" ); | 
|---|
| [19c43b7] | 725 |  | 
|---|
| [36982fc] | 726 | __cfaabi_dbg_print_buffer_local( "Kernel : accepted %d\n", *mask.accepted); | 
|---|
| [c81ebf9] | 727 | } | 
|---|
|  | 728 |  | 
|---|
| [0c78741] | 729 | //----------------------------------------------------------------------------- | 
|---|
|  | 730 | // Utilities | 
|---|
|  | 731 |  | 
|---|
| [e84ab3d] | 732 | static inline void __set_owner( monitor$ * this, thread$ * owner ) { | 
|---|
| [3381ed7] | 733 | /* paranoid */ verify( this->lock.lock ); | 
|---|
| [a843067] | 734 |  | 
|---|
| [0c78741] | 735 | //Pass the monitor appropriately | 
|---|
|  | 736 | this->owner = owner; | 
|---|
|  | 737 |  | 
|---|
|  | 738 | //We are passing the monitor to someone else, which means recursion level is not 0 | 
|---|
|  | 739 | this->recursion = owner ? 1 : 0; | 
|---|
|  | 740 | } | 
|---|
|  | 741 |  | 
|---|
| [e84ab3d] | 742 | static inline void __set_owner( monitor$ * monitors [], __lock_size_t count, thread$ * owner ) { | 
|---|
| [3381ed7] | 743 | /* paranoid */ verify ( monitors[0]->lock.lock ); | 
|---|
| [8fc652e0] | 744 | /* 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] | 745 | monitors[0]->owner        = owner; | 
|---|
|  | 746 | monitors[0]->recursion    = 1; | 
|---|
| [513daec] | 747 | for( __lock_size_t i = 1; i < count; i++ ) { | 
|---|
| [3381ed7] | 748 | /* paranoid */ verify ( monitors[i]->lock.lock ); | 
|---|
| [8fc652e0] | 749 | /* 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] | 750 | monitors[i]->owner        = owner; | 
|---|
|  | 751 | monitors[i]->recursion    = 0; | 
|---|
| [6ff4507] | 752 | } | 
|---|
|  | 753 | } | 
|---|
|  | 754 |  | 
|---|
| [e84ab3d] | 755 | static inline void set_mask( monitor$ * storage [], __lock_size_t count, const __waitfor_mask_t & mask ) { | 
|---|
| [513daec] | 756 | for( __lock_size_t i = 0; i < count; i++) { | 
|---|
| [6ff4507] | 757 | storage[i]->mask = mask; | 
|---|
|  | 758 | } | 
|---|
|  | 759 | } | 
|---|
|  | 760 |  | 
|---|
| [e84ab3d] | 761 | static inline void reset_mask( monitor$ * this ) { | 
|---|
| [121be3e] | 762 | this->mask.accepted = 0p; | 
|---|
|  | 763 | this->mask.data = 0p; | 
|---|
| [daacf82] | 764 | this->mask.size = 0; | 
|---|
|  | 765 | } | 
|---|
|  | 766 |  | 
|---|
| [e84ab3d] | 767 | static inline thread$ * next_thread( monitor$ * this ) { | 
|---|
| [0c78741] | 768 | //Check the signaller stack | 
|---|
| [169d944] | 769 | __cfaabi_dbg_print_safe( "Kernel :  mon %p AS-stack top %p\n", this, this->signal_stack.top); | 
|---|
| [8fc45b7] | 770 | __condition_criterion_t * urgent = pop( this->signal_stack ); | 
|---|
| [0c78741] | 771 | if( urgent ) { | 
|---|
|  | 772 | //The signaller stack is not empty, | 
|---|
|  | 773 | //regardless of if we are ready to baton pass, | 
|---|
|  | 774 | //we need to set the monitor as in use | 
|---|
| [8fc652e0] | 775 | /* 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] | 776 | __set_owner( this,  urgent->owner->waiting_thread ); | 
|---|
| [0c78741] | 777 |  | 
|---|
|  | 778 | return check_condition( urgent ); | 
|---|
|  | 779 | } | 
|---|
|  | 780 |  | 
|---|
|  | 781 | // No signaller thread | 
|---|
|  | 782 | // Get the next thread in the entry_queue | 
|---|
| [e84ab3d] | 783 | thread$ * new_owner = pop_head( this->entry_queue ); | 
|---|
| [8fc652e0] | 784 | /* 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] | 785 | /* paranoid */ verify( !new_owner || new_owner->link.next == 0p ); | 
|---|
| [c7a900a] | 786 | __set_owner( this, new_owner ); | 
|---|
| [0c78741] | 787 |  | 
|---|
|  | 788 | return new_owner; | 
|---|
|  | 789 | } | 
|---|
|  | 790 |  | 
|---|
| [e84ab3d] | 791 | static inline bool is_accepted( monitor$ * this, const __monitor_group_t & group ) { | 
|---|
| [0cf5b79] | 792 | __acceptable_t * it = this->mask.data; // Optim | 
|---|
| [59a0bde] | 793 | __lock_size_t count = this->mask.size; | 
|---|
| [6ff4507] | 794 |  | 
|---|
|  | 795 | // Check if there are any acceptable functions | 
|---|
| [a843067] | 796 | if( !it ) return false; | 
|---|
| [6ff4507] | 797 |  | 
|---|
|  | 798 | // If this isn't the first monitor to test this, there is no reason to repeat the test. | 
|---|
|  | 799 | if( this != group[0] ) return group[0]->mask.accepted >= 0; | 
|---|
|  | 800 |  | 
|---|
|  | 801 | // For all acceptable functions check if this is the current function. | 
|---|
| [59a0bde] | 802 | for( __lock_size_t i = 0; i < count; i++, it++ ) { | 
|---|
| [6ff4507] | 803 | if( *it == group ) { | 
|---|
|  | 804 | *this->mask.accepted = i; | 
|---|
|  | 805 | return true; | 
|---|
|  | 806 | } | 
|---|
|  | 807 | } | 
|---|
|  | 808 |  | 
|---|
|  | 809 | // No function matched | 
|---|
|  | 810 | return false; | 
|---|
|  | 811 | } | 
|---|
|  | 812 |  | 
|---|
| [e84ab3d] | 813 | static inline void init( __lock_size_t count, monitor$ * monitors [], __condition_node_t & waiter, __condition_criterion_t criteria [] ) { | 
|---|
| [513daec] | 814 | for( __lock_size_t i = 0; i < count; i++) { | 
|---|
| [6b224a52] | 815 | (criteria[i]){ monitors[i], waiter }; | 
|---|
| [97e3296] | 816 | } | 
|---|
|  | 817 |  | 
|---|
| [8fc45b7] | 818 | waiter.criteria = criteria; | 
|---|
| [97e3296] | 819 | } | 
|---|
|  | 820 |  | 
|---|
| [e84ab3d] | 821 | static inline void init_push( __lock_size_t count, monitor$ * monitors [], __condition_node_t & waiter, __condition_criterion_t criteria [] ) { | 
|---|
| [513daec] | 822 | for( __lock_size_t i = 0; i < count; i++) { | 
|---|
| [6b224a52] | 823 | (criteria[i]){ monitors[i], waiter }; | 
|---|
| [36982fc] | 824 | __cfaabi_dbg_print_safe( "Kernel :  target %p = %p\n", criteria[i].target, &criteria[i] ); | 
|---|
| [8fc45b7] | 825 | push( criteria[i].target->signal_stack, &criteria[i] ); | 
|---|
| [97e3296] | 826 | } | 
|---|
|  | 827 |  | 
|---|
| [8fc45b7] | 828 | waiter.criteria = criteria; | 
|---|
| [97e3296] | 829 | } | 
|---|
|  | 830 |  | 
|---|
| [ea7d2b0] | 831 | static inline void lock_all( __spinlock_t * locks [], __lock_size_t count ) { | 
|---|
| [513daec] | 832 | for( __lock_size_t i = 0; i < count; i++ ) { | 
|---|
| [2e9aed4] | 833 | lock( *locks[i] __cfaabi_dbg_ctx2 ); | 
|---|
| [0c78741] | 834 | } | 
|---|
|  | 835 | } | 
|---|
|  | 836 |  | 
|---|
| [e84ab3d] | 837 | static inline void lock_all( monitor$ * source [], __spinlock_t * /*out*/ locks [], __lock_size_t count ) { | 
|---|
| [513daec] | 838 | for( __lock_size_t i = 0; i < count; i++ ) { | 
|---|
| [ea7d2b0] | 839 | __spinlock_t * l = &source[i]->lock; | 
|---|
| [2e9aed4] | 840 | lock( *l __cfaabi_dbg_ctx2 ); | 
|---|
| [0c78741] | 841 | if(locks) locks[i] = l; | 
|---|
|  | 842 | } | 
|---|
|  | 843 | } | 
|---|
|  | 844 |  | 
|---|
| [ea7d2b0] | 845 | static inline void unlock_all( __spinlock_t * locks [], __lock_size_t count ) { | 
|---|
| [513daec] | 846 | for( __lock_size_t i = 0; i < count; i++ ) { | 
|---|
| [ea7d2b0] | 847 | unlock( *locks[i] ); | 
|---|
| [0c78741] | 848 | } | 
|---|
|  | 849 | } | 
|---|
|  | 850 |  | 
|---|
| [e84ab3d] | 851 | static inline void unlock_all( monitor$ * locks [], __lock_size_t count ) { | 
|---|
| [513daec] | 852 | for( __lock_size_t i = 0; i < count; i++ ) { | 
|---|
| [ea7d2b0] | 853 | unlock( locks[i]->lock ); | 
|---|
| [0c78741] | 854 | } | 
|---|
|  | 855 | } | 
|---|
|  | 856 |  | 
|---|
| [8fc45b7] | 857 | static inline void save( | 
|---|
| [e84ab3d] | 858 | monitor$ * ctx [], | 
|---|
| [513daec] | 859 | __lock_size_t count, | 
|---|
| [ea7d2b0] | 860 | __attribute((unused)) __spinlock_t * locks [], | 
|---|
| [8fc45b7] | 861 | unsigned int /*out*/ recursions [], | 
|---|
|  | 862 | __waitfor_mask_t /*out*/ masks [] | 
|---|
|  | 863 | ) { | 
|---|
| [513daec] | 864 | for( __lock_size_t i = 0; i < count; i++ ) { | 
|---|
| [0c78741] | 865 | recursions[i] = ctx[i]->recursion; | 
|---|
| [6ff4507] | 866 | masks[i]      = ctx[i]->mask; | 
|---|
| [0c78741] | 867 | } | 
|---|
|  | 868 | } | 
|---|
|  | 869 |  | 
|---|
| [8fc45b7] | 870 | static inline void restore( | 
|---|
| [e84ab3d] | 871 | monitor$ * ctx [], | 
|---|
| [513daec] | 872 | __lock_size_t count, | 
|---|
| [ea7d2b0] | 873 | __spinlock_t * locks [], | 
|---|
| [8fc45b7] | 874 | unsigned int /*out*/ recursions [], | 
|---|
|  | 875 | __waitfor_mask_t /*out*/ masks [] | 
|---|
|  | 876 | ) { | 
|---|
| [6ff4507] | 877 | lock_all( locks, count ); | 
|---|
| [513daec] | 878 | for( __lock_size_t i = 0; i < count; i++ ) { | 
|---|
| [0c78741] | 879 | ctx[i]->recursion = recursions[i]; | 
|---|
| [6ff4507] | 880 | ctx[i]->mask      = masks[i]; | 
|---|
| [0c78741] | 881 | } | 
|---|
| [6ff4507] | 882 | unlock_all( locks, count ); | 
|---|
| [0c78741] | 883 | } | 
|---|
|  | 884 |  | 
|---|
|  | 885 | // Function has 2 different behavior | 
|---|
|  | 886 | // 1 - Marks a monitors as being ready to run | 
|---|
|  | 887 | // 2 - Checks if all the monitors are ready to run | 
|---|
|  | 888 | //     if so return the thread to run | 
|---|
| [e84ab3d] | 889 | static inline thread$ * check_condition( __condition_criterion_t * target ) { | 
|---|
| [0c78741] | 890 | __condition_node_t * node = target->owner; | 
|---|
|  | 891 | unsigned short count = node->count; | 
|---|
|  | 892 | __condition_criterion_t * criteria = node->criteria; | 
|---|
|  | 893 |  | 
|---|
|  | 894 | bool ready2run = true; | 
|---|
|  | 895 |  | 
|---|
|  | 896 | for(    int i = 0; i < count; i++ ) { | 
|---|
| [44264c5] | 897 |  | 
|---|
| [36982fc] | 898 | // __cfaabi_dbg_print_safe( "Checking %p for %p\n", &criteria[i], target ); | 
|---|
| [0c78741] | 899 | if( &criteria[i] == target ) { | 
|---|
|  | 900 | criteria[i].ready = true; | 
|---|
| [36982fc] | 901 | // __cfaabi_dbg_print_safe( "True\n" ); | 
|---|
| [0c78741] | 902 | } | 
|---|
|  | 903 |  | 
|---|
|  | 904 | ready2run = criteria[i].ready && ready2run; | 
|---|
|  | 905 | } | 
|---|
|  | 906 |  | 
|---|
| [504a7dc] | 907 | __cfaabi_dbg_print_safe( "Kernel :  Runing %i (%p)\n", ready2run, ready2run ? (thread*)node->waiting_thread : (thread*)0p ); | 
|---|
| [121be3e] | 908 | return ready2run ? node->waiting_thread : 0p; | 
|---|
| [0c78741] | 909 | } | 
|---|
|  | 910 |  | 
|---|
| [4cedd9f] | 911 | static inline void brand_condition( condition & this ) { | 
|---|
| [e84ab3d] | 912 | thread$ * thrd = active_thread(); | 
|---|
| [4cedd9f] | 913 | if( !this.monitors ) { | 
|---|
| [169d944] | 914 | // __cfaabi_dbg_print_safe( "Branding\n" ); | 
|---|
| [121be3e] | 915 | assertf( thrd->monitors.data != 0p, "No current monitor to brand condition %p", thrd->monitors.data ); | 
|---|
| [4cedd9f] | 916 | this.monitor_count = thrd->monitors.size; | 
|---|
| [a933dcf4] | 917 |  | 
|---|
| [e84ab3d] | 918 | this.monitors = (monitor$ **)malloc( this.monitor_count * sizeof( *this.monitors ) ); | 
|---|
| [4cedd9f] | 919 | for( int i = 0; i < this.monitor_count; i++ ) { | 
|---|
| [0cf5b79] | 920 | this.monitors[i] = thrd->monitors[i]; | 
|---|
| [a933dcf4] | 921 | } | 
|---|
| [0c78741] | 922 | } | 
|---|
|  | 923 | } | 
|---|
|  | 924 |  | 
|---|
| [e84ab3d] | 925 | static inline [thread$ *, int] search_entry_queue( const __waitfor_mask_t & mask, monitor$ * monitors [], __lock_size_t count ) { | 
|---|
| [90c4df0] | 926 |  | 
|---|
| [e84ab3d] | 927 | __queue_t(thread$) & entry_queue = monitors[0]->entry_queue; | 
|---|
| [90c4df0] | 928 |  | 
|---|
|  | 929 | // For each thread in the entry-queue | 
|---|
| [e84ab3d] | 930 | for(    thread$ ** thrd_it = &entry_queue.head; | 
|---|
| [1b143de] | 931 | (*thrd_it) != 1p; | 
|---|
| [b798713] | 932 | thrd_it = &(*thrd_it)->link.next | 
|---|
| [4cc9b13] | 933 | ) { | 
|---|
| [90c4df0] | 934 | // For each acceptable check if it matches | 
|---|
| [4cc9b13] | 935 | int i = 0; | 
|---|
| [0cf5b79] | 936 | __acceptable_t * end   = end  (mask); | 
|---|
|  | 937 | __acceptable_t * begin = begin(mask); | 
|---|
|  | 938 | for( __acceptable_t * it = begin; it != end; it++, i++ ) { | 
|---|
| [90c4df0] | 939 | // Check if we have a match | 
|---|
| [aaa4f93] | 940 | if( *it == (*thrd_it)->monitors ) { | 
|---|
| [90c4df0] | 941 |  | 
|---|
|  | 942 | // If we have a match return it | 
|---|
|  | 943 | // after removeing it from the entry queue | 
|---|
| [b18830e] | 944 | return [remove( entry_queue, thrd_it ), i]; | 
|---|
| [90c4df0] | 945 | } | 
|---|
|  | 946 | } | 
|---|
|  | 947 | } | 
|---|
|  | 948 |  | 
|---|
| [b18830e] | 949 | return [0, -1]; | 
|---|
|  | 950 | } | 
|---|
|  | 951 |  | 
|---|
| [fd54fef] | 952 | forall(T & | sized( T )) | 
|---|
| [59a0bde] | 953 | static inline __lock_size_t insert_unique( T * array [], __lock_size_t & size, T * val ) { | 
|---|
| [6ff4507] | 954 | if( !val ) return size; | 
|---|
|  | 955 |  | 
|---|
| [59a0bde] | 956 | for( __lock_size_t i = 0; i <= size; i++) { | 
|---|
| [6ff4507] | 957 | if( array[i] == val ) return size; | 
|---|
|  | 958 | } | 
|---|
|  | 959 |  | 
|---|
|  | 960 | array[size] = val; | 
|---|
|  | 961 | size = size + 1; | 
|---|
|  | 962 | return size; | 
|---|
|  | 963 | } | 
|---|
|  | 964 |  | 
|---|
| [59a0bde] | 965 | static inline __lock_size_t count_max( const __waitfor_mask_t & mask ) { | 
|---|
|  | 966 | __lock_size_t max = 0; | 
|---|
|  | 967 | for( __lock_size_t i = 0; i < mask.size; i++ ) { | 
|---|
| [0cf5b79] | 968 | __acceptable_t & accepted = mask[i]; | 
|---|
|  | 969 | max += accepted.size; | 
|---|
| [b18830e] | 970 | } | 
|---|
|  | 971 | return max; | 
|---|
| [97e3296] | 972 | } | 
|---|
| [b18830e] | 973 |  | 
|---|
| [e84ab3d] | 974 | static inline __lock_size_t aggregate( monitor$ * storage [], const __waitfor_mask_t & mask ) { | 
|---|
| [59a0bde] | 975 | __lock_size_t size = 0; | 
|---|
|  | 976 | for( __lock_size_t i = 0; i < mask.size; i++ ) { | 
|---|
| [0cf5b79] | 977 | __acceptable_t & accepted = mask[i]; | 
|---|
|  | 978 | __libcfa_small_sort( accepted.data, accepted.size ); | 
|---|
|  | 979 | for( __lock_size_t j = 0; j < accepted.size; j++) { | 
|---|
|  | 980 | insert_unique( storage, size, accepted[j] ); | 
|---|
| [6ff4507] | 981 | } | 
|---|
| [b18830e] | 982 | } | 
|---|
| [6a5be52] | 983 | // TODO insertion sort instead of this | 
|---|
| [de737c8] | 984 | __libcfa_small_sort( storage, size ); | 
|---|
| [6ff4507] | 985 | return size; | 
|---|
| [b18830e] | 986 | } | 
|---|
|  | 987 |  | 
|---|
| [6b0b624] | 988 | // Local Variables: // | 
|---|
|  | 989 | // mode: c // | 
|---|
|  | 990 | // tab-width: 4 // | 
|---|
|  | 991 | // End: // | 
|---|