| 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 | //-----------------------------------------------------------------------------
|
|---|
| 24 | // Forward declarations
|
|---|
| 25 | static inline void set_owner( monitor_desc * this, thread_desc * owner );
|
|---|
| 26 | static inline void set_owner( monitor_desc ** storage, short count, thread_desc * owner );
|
|---|
| 27 | static inline void set_mask ( monitor_desc ** storage, short count, const __waitfor_mask_t & mask );
|
|---|
| 28 |
|
|---|
| 29 | static inline thread_desc * next_thread( monitor_desc * this );
|
|---|
| 30 | static inline bool is_accepted( monitor_desc * this, const __monitor_group_t & monitors );
|
|---|
| 31 |
|
|---|
| 32 | static inline void lock_all( spinlock ** locks, unsigned short count );
|
|---|
| 33 | static inline void lock_all( monitor_desc ** source, spinlock ** /*out*/ locks, unsigned short count );
|
|---|
| 34 | static inline void unlock_all( spinlock ** locks, unsigned short count );
|
|---|
| 35 | static inline void unlock_all( monitor_desc ** locks, unsigned short count );
|
|---|
| 36 |
|
|---|
| 37 | static inline void save ( monitor_desc ** ctx, short count, spinlock ** locks, unsigned int * /*out*/ recursions, __waitfor_mask_t * /*out*/ masks );
|
|---|
| 38 | static inline void restore( monitor_desc ** ctx, short count, spinlock ** locks, unsigned int * /*in */ recursions, __waitfor_mask_t * /*in */ masks );
|
|---|
| 39 |
|
|---|
| 40 | static inline void init ( int count, monitor_desc ** monitors, __condition_node_t * waiter, __condition_criterion_t * criteria );
|
|---|
| 41 | static inline void init_push( int count, monitor_desc ** monitors, __condition_node_t * waiter, __condition_criterion_t * criteria );
|
|---|
| 42 |
|
|---|
| 43 | static inline thread_desc * check_condition ( __condition_criterion_t * );
|
|---|
| 44 | static inline void brand_condition ( condition * );
|
|---|
| 45 | static inline [thread_desc *, int] search_entry_queue( const __waitfor_mask_t &, monitor_desc ** monitors, int count );
|
|---|
| 46 |
|
|---|
| 47 | forall(dtype T | sized( T ))
|
|---|
| 48 | static inline short insert_unique( T ** array, short & size, T * val );
|
|---|
| 49 | static inline short count_max ( const __waitfor_mask_t & mask );
|
|---|
| 50 | static inline short aggregate ( monitor_desc ** storage, const __waitfor_mask_t & mask );
|
|---|
| 51 |
|
|---|
| 52 | //-----------------------------------------------------------------------------
|
|---|
| 53 | // Useful defines
|
|---|
| 54 | #define wait_ctx(thrd, user_info) /* Create the necessary information to use the signaller stack */ \
|
|---|
| 55 | __condition_node_t waiter = { thrd, count, user_info }; /* Create the node specific to this wait operation */ \
|
|---|
| 56 | __condition_criterion_t criteria[count]; /* Create the creteria this wait operation needs to wake up */ \
|
|---|
| 57 | init( count, monitors, &waiter, criteria ); /* Link everything together */ \
|
|---|
| 58 |
|
|---|
| 59 | #define wait_ctx_primed(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 */ \
|
|---|
| 62 | init_push( count, monitors, &waiter, criteria ); /* Link everything together and push it to the AS-Stack */ \
|
|---|
| 63 |
|
|---|
| 64 | #define monitor_ctx( mons, cnt ) /* Define that create the necessary struct for internal/external scheduling operations */ \
|
|---|
| 65 | monitor_desc ** monitors = mons; /* Save the targeted monitors */ \
|
|---|
| 66 | unsigned short count = cnt; /* Save the count to a local variable */ \
|
|---|
| 67 | unsigned int recursions[ count ]; /* Save the current recursion levels to restore them later */ \
|
|---|
| 68 | __waitfor_mask_t masks[ count ]; /* Save the current waitfor masks to restore them later */ \
|
|---|
| 69 | spinlock * locks [ count ]; /* We need to pass-in an array of locks to BlockInternal */ \
|
|---|
| 70 |
|
|---|
| 71 | #define monitor_save save ( monitors, count, locks, recursions, masks )
|
|---|
| 72 | #define monitor_restore restore( monitors, count, locks, recursions, masks )
|
|---|
| 73 |
|
|---|
| 74 | #define blockAndWake( thrd, cnt ) /* Create the necessary information to use the signaller stack */ \
|
|---|
| 75 | monitor_save; /* Save monitor states */ \
|
|---|
| 76 | BlockInternal( locks, count, thrd, cnt ); /* Everything is ready to go to sleep */ \
|
|---|
| 77 | monitor_restore; /* We are back, restore the owners and recursions */ \
|
|---|
| 78 |
|
|---|
| 79 |
|
|---|
| 80 | //-----------------------------------------------------------------------------
|
|---|
| 81 | // Enter/Leave routines
|
|---|
| 82 |
|
|---|
| 83 |
|
|---|
| 84 | extern "C" {
|
|---|
| 85 | // Enter single monitor
|
|---|
| 86 | static void __enter_monitor_desc( monitor_desc * this, const __monitor_group_t & group ) {
|
|---|
| 87 | // Lock the monitor spinlock, lock_yield to reduce contention
|
|---|
| 88 | lock_yield( &this->lock DEBUG_CTX2 );
|
|---|
| 89 | thread_desc * thrd = this_thread;
|
|---|
| 90 |
|
|---|
| 91 | LIB_DEBUG_PRINT_SAFE("Kernel : %10p Entering mon %p (%p)\n", thrd, this, this->owner);
|
|---|
| 92 |
|
|---|
| 93 | if( !this->owner ) {
|
|---|
| 94 | // No one has the monitor, just take it
|
|---|
| 95 | set_owner( this, thrd );
|
|---|
| 96 |
|
|---|
| 97 | LIB_DEBUG_PRINT_SAFE("Kernel : mon is free \n");
|
|---|
| 98 | }
|
|---|
| 99 | else if( this->owner == thrd) {
|
|---|
| 100 | // We already have the monitor, just not how many times we took it
|
|---|
| 101 | verify( this->recursion > 0 );
|
|---|
| 102 | this->recursion += 1;
|
|---|
| 103 |
|
|---|
| 104 | LIB_DEBUG_PRINT_SAFE("Kernel : mon already owned \n");
|
|---|
| 105 | }
|
|---|
| 106 | else if( is_accepted( this, group) ) {
|
|---|
| 107 | // Some one was waiting for us, enter
|
|---|
| 108 | set_owner( this, thrd );
|
|---|
| 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 | // Leave single monitor
|
|---|
| 133 | void __leave_monitor_desc( monitor_desc * this ) {
|
|---|
| 134 | // Lock the monitor spinlock, lock_yield to reduce contention
|
|---|
| 135 | lock_yield( &this->lock DEBUG_CTX2 );
|
|---|
| 136 |
|
|---|
| 137 | LIB_DEBUG_PRINT_SAFE("Kernel : %10p Leaving mon %p (%p)\n", this_thread, this, this->owner);
|
|---|
| 138 |
|
|---|
| 139 | verifyf( this_thread == this->owner, "Expected owner to be %p, got %p (r: %i, m: %p)", this_thread, this->owner, this->recursion, this );
|
|---|
| 140 |
|
|---|
| 141 | // Leaving a recursion level, decrement the counter
|
|---|
| 142 | this->recursion -= 1;
|
|---|
| 143 |
|
|---|
| 144 | // If we haven't left the last level of recursion
|
|---|
| 145 | // it means we don't need to do anything
|
|---|
| 146 | if( this->recursion != 0) {
|
|---|
| 147 | unlock( &this->lock );
|
|---|
| 148 | return;
|
|---|
| 149 | }
|
|---|
| 150 |
|
|---|
| 151 | // Get the next thread, will be null on low contention monitor
|
|---|
| 152 | thread_desc * new_owner = next_thread( this );
|
|---|
| 153 |
|
|---|
| 154 | // We can now let other threads in safely
|
|---|
| 155 | unlock( &this->lock );
|
|---|
| 156 |
|
|---|
| 157 | //We need to wake-up the thread
|
|---|
| 158 | WakeThread( new_owner );
|
|---|
| 159 | }
|
|---|
| 160 |
|
|---|
| 161 | // Leave the thread monitor
|
|---|
| 162 | // last routine called by a thread.
|
|---|
| 163 | // Should never return
|
|---|
| 164 | void __leave_thread_monitor( thread_desc * thrd ) {
|
|---|
| 165 | monitor_desc * this = &thrd->self_mon;
|
|---|
| 166 |
|
|---|
| 167 | // Lock the monitor now
|
|---|
| 168 | lock_yield( &this->lock DEBUG_CTX2 );
|
|---|
| 169 |
|
|---|
| 170 | disable_interrupts();
|
|---|
| 171 |
|
|---|
| 172 | thrd->self_cor.state = Halted;
|
|---|
| 173 |
|
|---|
| 174 | verifyf( thrd == this->owner, "Expected owner to be %p, got %p (r: %i, m: %p)", thrd, this->owner, this->recursion, this );
|
|---|
| 175 |
|
|---|
| 176 | // Leaving a recursion level, decrement the counter
|
|---|
| 177 | this->recursion -= 1;
|
|---|
| 178 |
|
|---|
| 179 | // If we haven't left the last level of recursion
|
|---|
| 180 | // it must mean there is an error
|
|---|
| 181 | if( this->recursion != 0) { abortf("Thread internal monitor has unbalanced recursion"); }
|
|---|
| 182 |
|
|---|
| 183 | // Fetch the next thread, can be null
|
|---|
| 184 | thread_desc * new_owner = next_thread( this );
|
|---|
| 185 |
|
|---|
| 186 | // Leave the thread, this will unlock the spinlock
|
|---|
| 187 | // Use leave thread instead of BlockInternal which is
|
|---|
| 188 | // specialized for this case and supports null new_owner
|
|---|
| 189 | LeaveThread( &this->lock, new_owner );
|
|---|
| 190 |
|
|---|
| 191 | // Control flow should never reach here!
|
|---|
| 192 | }
|
|---|
| 193 | }
|
|---|
| 194 |
|
|---|
| 195 | // Enter multiple monitor
|
|---|
| 196 | // relies on the monitor array being sorted
|
|---|
| 197 | static inline void enter( __monitor_group_t monitors ) {
|
|---|
| 198 | for(int i = 0; i < monitors.size; i++) {
|
|---|
| 199 | __enter_monitor_desc( monitors.list[i], monitors );
|
|---|
| 200 | }
|
|---|
| 201 | }
|
|---|
| 202 |
|
|---|
| 203 | // Leave multiple monitor
|
|---|
| 204 | // relies on the monitor array being sorted
|
|---|
| 205 | static inline void leave(monitor_desc ** monitors, int count) {
|
|---|
| 206 | for(int i = count - 1; i >= 0; i--) {
|
|---|
| 207 | __leave_monitor_desc( monitors[i] );
|
|---|
| 208 | }
|
|---|
| 209 | }
|
|---|
| 210 |
|
|---|
| 211 | // Ctor for monitor guard
|
|---|
| 212 | // Sorts monitors before entering
|
|---|
| 213 | void ?{}( monitor_guard_t & this, monitor_desc ** m, int count, void (*func)() ) {
|
|---|
| 214 | // Store current array
|
|---|
| 215 | this.m = m;
|
|---|
| 216 | this.count = count;
|
|---|
| 217 |
|
|---|
| 218 | // Sort monitors based on address -> TODO use a sort specialized for small numbers
|
|---|
| 219 | qsort(this.m, count);
|
|---|
| 220 |
|
|---|
| 221 | // Save previous thread context
|
|---|
| 222 | this.prev_mntrs = this_thread->monitors.list;
|
|---|
| 223 | this.prev_count = this_thread->monitors.size;
|
|---|
| 224 | this.prev_func = this_thread->monitors.func;
|
|---|
| 225 |
|
|---|
| 226 | // Update thread context (needed for conditions)
|
|---|
| 227 | this_thread->monitors.list = m;
|
|---|
| 228 | this_thread->monitors.size = count;
|
|---|
| 229 | this_thread->monitors.func = func;
|
|---|
| 230 |
|
|---|
| 231 | LIB_DEBUG_PRINT_SAFE("MGUARD : enter %d\n", count);
|
|---|
| 232 |
|
|---|
| 233 | // Enter the monitors in order
|
|---|
| 234 | __monitor_group_t group = {this.m, this.count, func};
|
|---|
| 235 | enter( group );
|
|---|
| 236 |
|
|---|
| 237 | LIB_DEBUG_PRINT_SAFE("MGUARD : entered\n");
|
|---|
| 238 | }
|
|---|
| 239 |
|
|---|
| 240 |
|
|---|
| 241 | // Dtor for monitor guard
|
|---|
| 242 | void ^?{}( monitor_guard_t & this ) {
|
|---|
| 243 | LIB_DEBUG_PRINT_SAFE("MGUARD : leaving %d\n", this.count);
|
|---|
| 244 |
|
|---|
| 245 | // Leave the monitors in order
|
|---|
| 246 | leave( this.m, this.count );
|
|---|
| 247 |
|
|---|
| 248 | LIB_DEBUG_PRINT_SAFE("MGUARD : left\n");
|
|---|
| 249 |
|
|---|
| 250 | // Restore thread context
|
|---|
| 251 | this_thread->monitors.list = this.prev_mntrs;
|
|---|
| 252 | this_thread->monitors.size = this.prev_count;
|
|---|
| 253 | this_thread->monitors.func = this.prev_func;
|
|---|
| 254 | }
|
|---|
| 255 |
|
|---|
| 256 | //-----------------------------------------------------------------------------
|
|---|
| 257 | // Internal scheduling types
|
|---|
| 258 | void ?{}(__condition_node_t & this, thread_desc * waiting_thread, unsigned short count, uintptr_t user_info ) {
|
|---|
| 259 | this.waiting_thread = waiting_thread;
|
|---|
| 260 | this.count = count;
|
|---|
| 261 | this.next = NULL;
|
|---|
| 262 | this.user_info = user_info;
|
|---|
| 263 | }
|
|---|
| 264 |
|
|---|
| 265 | void ?{}(__condition_criterion_t & this ) {
|
|---|
| 266 | this.ready = false;
|
|---|
| 267 | this.target = NULL;
|
|---|
| 268 | this.owner = NULL;
|
|---|
| 269 | this.next = NULL;
|
|---|
| 270 | }
|
|---|
| 271 |
|
|---|
| 272 | void ?{}(__condition_criterion_t & this, monitor_desc * target, __condition_node_t * owner ) {
|
|---|
| 273 | this.ready = false;
|
|---|
| 274 | this.target = target;
|
|---|
| 275 | this.owner = owner;
|
|---|
| 276 | this.next = NULL;
|
|---|
| 277 | }
|
|---|
| 278 |
|
|---|
| 279 | //-----------------------------------------------------------------------------
|
|---|
| 280 | // Internal scheduling
|
|---|
| 281 | void wait( condition * this, uintptr_t user_info = 0 ) {
|
|---|
| 282 | brand_condition( this );
|
|---|
| 283 |
|
|---|
| 284 | // Check that everything is as expected
|
|---|
| 285 | assertf( this->monitors != NULL, "Waiting with no monitors (%p)", this->monitors );
|
|---|
| 286 | verifyf( this->monitor_count != 0, "Waiting with 0 monitors (%i)", this->monitor_count );
|
|---|
| 287 | verifyf( this->monitor_count < 32u, "Excessive monitor count (%i)", this->monitor_count );
|
|---|
| 288 |
|
|---|
| 289 | // Create storage for monitor context
|
|---|
| 290 | monitor_ctx( this->monitors, this->monitor_count );
|
|---|
| 291 |
|
|---|
| 292 | // Create the node specific to this wait operation
|
|---|
| 293 | wait_ctx( this_thread, user_info );
|
|---|
| 294 |
|
|---|
| 295 | // Append the current wait operation to the ones already queued on the condition
|
|---|
| 296 | // We don't need locks for that since conditions must always be waited on inside monitor mutual exclusion
|
|---|
| 297 | append( &this->blocked, &waiter );
|
|---|
| 298 |
|
|---|
| 299 | // Lock all monitors (aggregates the locks as well)
|
|---|
| 300 | lock_all( monitors, locks, count );
|
|---|
| 301 |
|
|---|
| 302 | // Find the next thread(s) to run
|
|---|
| 303 | short thread_count = 0;
|
|---|
| 304 | thread_desc * threads[ count ];
|
|---|
| 305 | for(int i = 0; i < count; i++) {
|
|---|
| 306 | threads[i] = 0;
|
|---|
| 307 | }
|
|---|
| 308 |
|
|---|
| 309 | // Save monitor states
|
|---|
| 310 | monitor_save;
|
|---|
| 311 |
|
|---|
| 312 | // Remove any duplicate threads
|
|---|
| 313 | for( int i = 0; i < count; i++) {
|
|---|
| 314 | thread_desc * new_owner = next_thread( monitors[i] );
|
|---|
| 315 | insert_unique( threads, thread_count, new_owner );
|
|---|
| 316 | }
|
|---|
| 317 |
|
|---|
| 318 | // Everything is ready to go to sleep
|
|---|
| 319 | BlockInternal( locks, count, threads, thread_count );
|
|---|
| 320 |
|
|---|
| 321 | // We are back, restore the owners and recursions
|
|---|
| 322 | monitor_restore;
|
|---|
| 323 | }
|
|---|
| 324 |
|
|---|
| 325 | bool signal( condition * this ) {
|
|---|
| 326 | if( is_empty( this ) ) { return false; }
|
|---|
| 327 |
|
|---|
| 328 | //Check that everything is as expected
|
|---|
| 329 | verify( this->monitors );
|
|---|
| 330 | verify( this->monitor_count != 0 );
|
|---|
| 331 |
|
|---|
| 332 | //Some more checking in debug
|
|---|
| 333 | LIB_DEBUG_DO(
|
|---|
| 334 | thread_desc * this_thrd = this_thread;
|
|---|
| 335 | if ( this->monitor_count != this_thrd->monitors.size ) {
|
|---|
| 336 | abortf( "Signal on condition %p made with different number of monitor(s), expected %i got %i", this, this->monitor_count, this_thrd->monitors.size );
|
|---|
| 337 | }
|
|---|
| 338 |
|
|---|
| 339 | for(int i = 0; i < this->monitor_count; i++) {
|
|---|
| 340 | if ( this->monitors[i] != this_thrd->monitors.list[i] ) {
|
|---|
| 341 | abortf( "Signal on condition %p made with different monitor, expected %p got %i", this, this->monitors[i], this_thrd->monitors.list[i] );
|
|---|
| 342 | }
|
|---|
| 343 | }
|
|---|
| 344 | );
|
|---|
| 345 |
|
|---|
| 346 | unsigned short count = this->monitor_count;
|
|---|
| 347 |
|
|---|
| 348 | // Lock all monitors
|
|---|
| 349 | lock_all( this->monitors, NULL, count );
|
|---|
| 350 |
|
|---|
| 351 | //Pop the head of the waiting queue
|
|---|
| 352 | __condition_node_t * node = pop_head( &this->blocked );
|
|---|
| 353 |
|
|---|
| 354 | //Add the thread to the proper AS stack
|
|---|
| 355 | for(int i = 0; i < count; i++) {
|
|---|
| 356 | __condition_criterion_t * crit = &node->criteria[i];
|
|---|
| 357 | assert( !crit->ready );
|
|---|
| 358 | push( &crit->target->signal_stack, crit );
|
|---|
| 359 | }
|
|---|
| 360 |
|
|---|
| 361 | //Release
|
|---|
| 362 | unlock_all( this->monitors, count );
|
|---|
| 363 |
|
|---|
| 364 | return true;
|
|---|
| 365 | }
|
|---|
| 366 |
|
|---|
| 367 | bool signal_block( condition * this ) {
|
|---|
| 368 | if( !this->blocked.head ) { return false; }
|
|---|
| 369 |
|
|---|
| 370 | //Check that everything is as expected
|
|---|
| 371 | verifyf( this->monitors != NULL, "Waiting with no monitors (%p)", this->monitors );
|
|---|
| 372 | verifyf( this->monitor_count != 0, "Waiting with 0 monitors (%i)", this->monitor_count );
|
|---|
| 373 |
|
|---|
| 374 | // Create storage for monitor context
|
|---|
| 375 | monitor_ctx( this->monitors, this->monitor_count );
|
|---|
| 376 |
|
|---|
| 377 | // Lock all monitors (aggregates the locks them as well)
|
|---|
| 378 | lock_all( monitors, locks, count );
|
|---|
| 379 |
|
|---|
| 380 | // Create the node specific to this wait operation
|
|---|
| 381 | wait_ctx_primed( this_thread, 0 )
|
|---|
| 382 |
|
|---|
| 383 | //save contexts
|
|---|
| 384 | monitor_save;
|
|---|
| 385 |
|
|---|
| 386 | //Find the thread to run
|
|---|
| 387 | thread_desc * signallee = pop_head( &this->blocked )->waiting_thread;
|
|---|
| 388 | set_owner( monitors, count, signallee );
|
|---|
| 389 |
|
|---|
| 390 | //Everything is ready to go to sleep
|
|---|
| 391 | BlockInternal( locks, count, &signallee, 1 );
|
|---|
| 392 |
|
|---|
| 393 |
|
|---|
| 394 | // WE WOKE UP
|
|---|
| 395 |
|
|---|
| 396 |
|
|---|
| 397 | //We are back, restore the masks and recursions
|
|---|
| 398 | monitor_restore;
|
|---|
| 399 |
|
|---|
| 400 | return true;
|
|---|
| 401 | }
|
|---|
| 402 |
|
|---|
| 403 | // Access the user_info of the thread waiting at the front of the queue
|
|---|
| 404 | uintptr_t front( condition * this ) {
|
|---|
| 405 | verifyf( !is_empty(this),
|
|---|
| 406 | "Attempt to access user data on an empty condition.\n"
|
|---|
| 407 | "Possible cause is not checking if the condition is empty before reading stored data."
|
|---|
| 408 | );
|
|---|
| 409 | return this->blocked.head->user_info;
|
|---|
| 410 | }
|
|---|
| 411 |
|
|---|
| 412 | //-----------------------------------------------------------------------------
|
|---|
| 413 | // External scheduling
|
|---|
| 414 | // cases to handle :
|
|---|
| 415 | // - target already there :
|
|---|
| 416 | // block and wake
|
|---|
| 417 | // - dtor already there
|
|---|
| 418 | // put thread on signaller stack
|
|---|
| 419 | // - non-blocking
|
|---|
| 420 | // return else
|
|---|
| 421 | // - timeout
|
|---|
| 422 | // return timeout
|
|---|
| 423 | // - block
|
|---|
| 424 | // setup mask
|
|---|
| 425 | // block
|
|---|
| 426 | void __waitfor_internal( const __waitfor_mask_t & mask, int duration ) {
|
|---|
| 427 | // This statment doesn't have a contiguous list of monitors...
|
|---|
| 428 | // Create one!
|
|---|
| 429 | short max = count_max( mask );
|
|---|
| 430 | monitor_desc * mon_storage[max];
|
|---|
| 431 | short actual_count = aggregate( mon_storage, mask );
|
|---|
| 432 |
|
|---|
| 433 | if(actual_count == 0) return;
|
|---|
| 434 |
|
|---|
| 435 | // Create storage for monitor context
|
|---|
| 436 | monitor_ctx( mon_storage, actual_count );
|
|---|
| 437 |
|
|---|
| 438 | // Lock all monitors (aggregates the locks as well)
|
|---|
| 439 | lock_all( monitors, locks, count );
|
|---|
| 440 |
|
|---|
| 441 | {
|
|---|
| 442 | // Check if the entry queue
|
|---|
| 443 | thread_desc * next; int index;
|
|---|
| 444 | [next, index] = search_entry_queue( mask, monitors, count );
|
|---|
| 445 |
|
|---|
| 446 | if( next ) {
|
|---|
| 447 | if( mask.clauses[index].is_dtor ) {
|
|---|
| 448 | #warning case not implemented
|
|---|
| 449 | }
|
|---|
| 450 | else {
|
|---|
| 451 | blockAndWake( &next, 1 );
|
|---|
| 452 | }
|
|---|
| 453 |
|
|---|
| 454 | return index;
|
|---|
| 455 | }
|
|---|
| 456 | }
|
|---|
| 457 |
|
|---|
| 458 |
|
|---|
| 459 | if( duration == 0 ) {
|
|---|
| 460 | unlock_all( locks, count );
|
|---|
| 461 | return;
|
|---|
| 462 | }
|
|---|
| 463 |
|
|---|
| 464 |
|
|---|
| 465 | verifyf( duration < 0, "Timeout on waitfor statments not supported yet.");
|
|---|
| 466 |
|
|---|
| 467 |
|
|---|
| 468 | monitor_save;
|
|---|
| 469 | set_mask( monitors, count, mask );
|
|---|
| 470 |
|
|---|
| 471 | BlockInternal( locks, count ); // Everything is ready to go to sleep
|
|---|
| 472 | //WE WOKE UP
|
|---|
| 473 | monitor_restore; //We are back, restore the masks and recursions
|
|---|
| 474 | }
|
|---|
| 475 |
|
|---|
| 476 | //-----------------------------------------------------------------------------
|
|---|
| 477 | // Utilities
|
|---|
| 478 |
|
|---|
| 479 | static inline void set_owner( monitor_desc * this, thread_desc * owner ) {
|
|---|
| 480 | LIB_DEBUG_PRINT_SAFE("Kernal : Setting owner of %p to %p ( was %p)\n", this, owner, this->owner );
|
|---|
| 481 |
|
|---|
| 482 | //Pass the monitor appropriately
|
|---|
| 483 | this->owner = owner;
|
|---|
| 484 |
|
|---|
| 485 | //We are passing the monitor to someone else, which means recursion level is not 0
|
|---|
| 486 | this->recursion = owner ? 1 : 0;
|
|---|
| 487 | }
|
|---|
| 488 |
|
|---|
| 489 | static inline void set_owner( monitor_desc ** monitors, short count, thread_desc * owner ) {
|
|---|
| 490 | for( int i = 0; i < count; i++ ) {
|
|---|
| 491 | set_owner( monitors[i], owner );
|
|---|
| 492 | }
|
|---|
| 493 | }
|
|---|
| 494 |
|
|---|
| 495 | static inline void set_mask( monitor_desc ** storage, short count, const __waitfor_mask_t & mask ) {
|
|---|
| 496 | for(int i = 0; i < count; i++) {
|
|---|
| 497 | storage[i]->mask = mask;
|
|---|
| 498 | }
|
|---|
| 499 | }
|
|---|
| 500 |
|
|---|
| 501 | static inline thread_desc * next_thread( monitor_desc * this ) {
|
|---|
| 502 | //Check the signaller stack
|
|---|
| 503 | __condition_criterion_t * urgent = pop( &this->signal_stack );
|
|---|
| 504 | if( urgent ) {
|
|---|
| 505 | //The signaller stack is not empty,
|
|---|
| 506 | //regardless of if we are ready to baton pass,
|
|---|
| 507 | //we need to set the monitor as in use
|
|---|
| 508 | set_owner( this, urgent->owner->waiting_thread );
|
|---|
| 509 |
|
|---|
| 510 | return check_condition( urgent );
|
|---|
| 511 | }
|
|---|
| 512 |
|
|---|
| 513 | // No signaller thread
|
|---|
| 514 | // Get the next thread in the entry_queue
|
|---|
| 515 | thread_desc * new_owner = pop_head( &this->entry_queue );
|
|---|
| 516 | set_owner( this, new_owner );
|
|---|
| 517 |
|
|---|
| 518 | return new_owner;
|
|---|
| 519 | }
|
|---|
| 520 |
|
|---|
| 521 | static inline bool is_accepted( monitor_desc * this, const __monitor_group_t & group ) {
|
|---|
| 522 | __acceptable_t * it = this->mask.clauses; // Optim
|
|---|
| 523 | int count = this->mask.size;
|
|---|
| 524 |
|
|---|
| 525 | // Check if there are any acceptable functions
|
|---|
| 526 | if( !it ) return false;
|
|---|
| 527 |
|
|---|
| 528 | // If this isn't the first monitor to test this, there is no reason to repeat the test.
|
|---|
| 529 | if( this != group[0] ) return group[0]->mask.accepted >= 0;
|
|---|
| 530 |
|
|---|
| 531 | // For all acceptable functions check if this is the current function.
|
|---|
| 532 | for( short i = 0; i < count; i++, it++ ) {
|
|---|
| 533 | if( *it == group ) {
|
|---|
| 534 | *this->mask.accepted = i;
|
|---|
| 535 | return true;
|
|---|
| 536 | }
|
|---|
| 537 | }
|
|---|
| 538 |
|
|---|
| 539 | // No function matched
|
|---|
| 540 | return false;
|
|---|
| 541 | }
|
|---|
| 542 |
|
|---|
| 543 | static inline void init( int count, monitor_desc ** monitors, __condition_node_t * waiter, __condition_criterion_t * criteria ) {
|
|---|
| 544 | for(int i = 0; i < count; i++) {
|
|---|
| 545 | (criteria[i]){ monitors[i], waiter };
|
|---|
| 546 | }
|
|---|
| 547 |
|
|---|
| 548 | waiter->criteria = criteria;
|
|---|
| 549 | }
|
|---|
| 550 |
|
|---|
| 551 | static inline void init_push( int count, monitor_desc ** monitors, __condition_node_t * waiter, __condition_criterion_t * criteria ) {
|
|---|
| 552 | for(int i = 0; i < count; i++) {
|
|---|
| 553 | (criteria[i]){ monitors[i], waiter };
|
|---|
| 554 | push( &criteria[i].target->signal_stack, &criteria[i] );
|
|---|
| 555 | }
|
|---|
| 556 |
|
|---|
| 557 | waiter->criteria = criteria;
|
|---|
| 558 | }
|
|---|
| 559 |
|
|---|
| 560 | static inline void lock_all( spinlock ** locks, unsigned short count ) {
|
|---|
| 561 | for( int i = 0; i < count; i++ ) {
|
|---|
| 562 | lock_yield( locks[i] DEBUG_CTX2 );
|
|---|
| 563 | }
|
|---|
| 564 | }
|
|---|
| 565 |
|
|---|
| 566 | static inline void lock_all( monitor_desc ** source, spinlock ** /*out*/ locks, unsigned short count ) {
|
|---|
| 567 | for( int i = 0; i < count; i++ ) {
|
|---|
| 568 | spinlock * l = &source[i]->lock;
|
|---|
| 569 | lock_yield( l DEBUG_CTX2 );
|
|---|
| 570 | if(locks) locks[i] = l;
|
|---|
| 571 | }
|
|---|
| 572 | }
|
|---|
| 573 |
|
|---|
| 574 | static inline void unlock_all( spinlock ** locks, unsigned short count ) {
|
|---|
| 575 | for( int i = 0; i < count; i++ ) {
|
|---|
| 576 | unlock( locks[i] );
|
|---|
| 577 | }
|
|---|
| 578 | }
|
|---|
| 579 |
|
|---|
| 580 | static inline void unlock_all( monitor_desc ** locks, unsigned short count ) {
|
|---|
| 581 | for( int i = 0; i < count; i++ ) {
|
|---|
| 582 | unlock( &locks[i]->lock );
|
|---|
| 583 | }
|
|---|
| 584 | }
|
|---|
| 585 |
|
|---|
| 586 | static inline void save ( monitor_desc ** ctx, short count, __attribute((unused)) spinlock ** locks, unsigned int * /*out*/ recursions, __waitfor_mask_t * /*out*/ masks ) {
|
|---|
| 587 | for( int i = 0; i < count; i++ ) {
|
|---|
| 588 | recursions[i] = ctx[i]->recursion;
|
|---|
| 589 | masks[i] = ctx[i]->mask;
|
|---|
| 590 | }
|
|---|
| 591 | }
|
|---|
| 592 |
|
|---|
| 593 | static inline void restore( monitor_desc ** ctx, short count, spinlock ** locks, unsigned int * /*out*/ recursions, __waitfor_mask_t * /*out*/ masks ) {
|
|---|
| 594 | lock_all( locks, count );
|
|---|
| 595 | for( int i = 0; i < count; i++ ) {
|
|---|
| 596 | ctx[i]->recursion = recursions[i];
|
|---|
| 597 | ctx[i]->mask = masks[i];
|
|---|
| 598 | }
|
|---|
| 599 | unlock_all( locks, count );
|
|---|
| 600 | }
|
|---|
| 601 |
|
|---|
| 602 | // Function has 2 different behavior
|
|---|
| 603 | // 1 - Marks a monitors as being ready to run
|
|---|
| 604 | // 2 - Checks if all the monitors are ready to run
|
|---|
| 605 | // if so return the thread to run
|
|---|
| 606 | static inline thread_desc * check_condition( __condition_criterion_t * target ) {
|
|---|
| 607 | __condition_node_t * node = target->owner;
|
|---|
| 608 | unsigned short count = node->count;
|
|---|
| 609 | __condition_criterion_t * criteria = node->criteria;
|
|---|
| 610 |
|
|---|
| 611 | bool ready2run = true;
|
|---|
| 612 |
|
|---|
| 613 | for( int i = 0; i < count; i++ ) {
|
|---|
| 614 |
|
|---|
| 615 | // LIB_DEBUG_PRINT_SAFE( "Checking %p for %p\n", &criteria[i], target );
|
|---|
| 616 | if( &criteria[i] == target ) {
|
|---|
| 617 | criteria[i].ready = true;
|
|---|
| 618 | // LIB_DEBUG_PRINT_SAFE( "True\n" );
|
|---|
| 619 | }
|
|---|
| 620 |
|
|---|
| 621 | ready2run = criteria[i].ready && ready2run;
|
|---|
| 622 | }
|
|---|
| 623 |
|
|---|
| 624 | // LIB_DEBUG_PRINT_SAFE( "Runing %i\n", ready2run );
|
|---|
| 625 | return ready2run ? node->waiting_thread : NULL;
|
|---|
| 626 | }
|
|---|
| 627 |
|
|---|
| 628 | static inline void brand_condition( condition * this ) {
|
|---|
| 629 | thread_desc * thrd = this_thread;
|
|---|
| 630 | if( !this->monitors ) {
|
|---|
| 631 | // LIB_DEBUG_PRINT_SAFE("Branding\n");
|
|---|
| 632 | assertf( thrd->monitors.list != NULL, "No current monitor to brand condition %p", thrd->monitors.list );
|
|---|
| 633 | this->monitor_count = thrd->monitors.size;
|
|---|
| 634 |
|
|---|
| 635 | this->monitors = malloc( this->monitor_count * sizeof( *this->monitors ) );
|
|---|
| 636 | for( int i = 0; i < this->monitor_count; i++ ) {
|
|---|
| 637 | this->monitors[i] = thrd->monitors.list[i];
|
|---|
| 638 | }
|
|---|
| 639 | }
|
|---|
| 640 | }
|
|---|
| 641 |
|
|---|
| 642 | static inline [thread_desc *, int] search_entry_queue( const __waitfor_mask_t & mask, monitor_desc ** monitors, int count ) {
|
|---|
| 643 |
|
|---|
| 644 | __thread_queue_t * entry_queue = &monitors[0]->entry_queue;
|
|---|
| 645 |
|
|---|
| 646 | // For each thread in the entry-queue
|
|---|
| 647 | for( thread_desc ** thrd_it = &entry_queue->head;
|
|---|
| 648 | *thrd_it;
|
|---|
| 649 | thrd_it = &(*thrd_it)->next
|
|---|
| 650 | ) {
|
|---|
| 651 | // For each acceptable check if it matches
|
|---|
| 652 | int i = 0;
|
|---|
| 653 | __acceptable_t * end = mask.clauses + mask.size;
|
|---|
| 654 | for( __acceptable_t * it = mask.clauses; it != end; it++, i++ ) {
|
|---|
| 655 | // Check if we have a match
|
|---|
| 656 | if( *it == (*thrd_it)->monitors ) {
|
|---|
| 657 |
|
|---|
| 658 | // If we have a match return it
|
|---|
| 659 | // after removeing it from the entry queue
|
|---|
| 660 | return [remove( entry_queue, thrd_it ), i];
|
|---|
| 661 | }
|
|---|
| 662 | }
|
|---|
| 663 | }
|
|---|
| 664 |
|
|---|
| 665 | return [0, -1];
|
|---|
| 666 | }
|
|---|
| 667 |
|
|---|
| 668 | forall(dtype T | sized( T ))
|
|---|
| 669 | static inline short insert_unique( T ** array, short & size, T * val ) {
|
|---|
| 670 | if( !val ) return size;
|
|---|
| 671 |
|
|---|
| 672 | for(int i = 0; i <= size; i++) {
|
|---|
| 673 | if( array[i] == val ) return size;
|
|---|
| 674 | }
|
|---|
| 675 |
|
|---|
| 676 | array[size] = val;
|
|---|
| 677 | size = size + 1;
|
|---|
| 678 | return size;
|
|---|
| 679 | }
|
|---|
| 680 |
|
|---|
| 681 | static inline short count_max( const __waitfor_mask_t & mask ) {
|
|---|
| 682 | short max = 0;
|
|---|
| 683 | for( int i = 0; i < mask.size; i++ ) {
|
|---|
| 684 | max += mask.clauses[i].size;
|
|---|
| 685 | }
|
|---|
| 686 | return max;
|
|---|
| 687 | }
|
|---|
| 688 |
|
|---|
| 689 | static inline short aggregate( monitor_desc ** storage, const __waitfor_mask_t & mask ) {
|
|---|
| 690 | short size = 0;
|
|---|
| 691 | for( int i = 0; i < mask.size; i++ ) {
|
|---|
| 692 | for( int j = 0; j < mask.clauses[i].size; j++) {
|
|---|
| 693 | insert_unique( storage, size, mask.clauses[i].list[j] );
|
|---|
| 694 | }
|
|---|
| 695 | }
|
|---|
| 696 | qsort( storage, size );
|
|---|
| 697 | return size;
|
|---|
| 698 | }
|
|---|
| 699 |
|
|---|
| 700 | void ?{}( __condition_blocked_queue_t & this ) {
|
|---|
| 701 | this.head = NULL;
|
|---|
| 702 | this.tail = &this.head;
|
|---|
| 703 | }
|
|---|
| 704 |
|
|---|
| 705 | void append( __condition_blocked_queue_t * this, __condition_node_t * c ) {
|
|---|
| 706 | verify(this->tail != NULL);
|
|---|
| 707 | *this->tail = c;
|
|---|
| 708 | this->tail = &c->next;
|
|---|
| 709 | }
|
|---|
| 710 |
|
|---|
| 711 | __condition_node_t * pop_head( __condition_blocked_queue_t * this ) {
|
|---|
| 712 | __condition_node_t * head = this->head;
|
|---|
| 713 | if( head ) {
|
|---|
| 714 | this->head = head->next;
|
|---|
| 715 | if( !head->next ) {
|
|---|
| 716 | this->tail = &this->head;
|
|---|
| 717 | }
|
|---|
| 718 | head->next = NULL;
|
|---|
| 719 | }
|
|---|
| 720 | return head;
|
|---|
| 721 | }
|
|---|
| 722 |
|
|---|
| 723 | // Local Variables: //
|
|---|
| 724 | // mode: c //
|
|---|
| 725 | // tab-width: 4 //
|
|---|
| 726 | // End: //
|
|---|