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