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