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