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