[f07e037] | 1 | // -*- Mode: CFA -*- |
---|
| 2 | // |
---|
| 3 | // Cforall Version 1.0.0 Copyright (C) 2016 University of Waterloo |
---|
| 4 | // |
---|
| 5 | // The contents of this file are covered under the licence agreement in the |
---|
| 6 | // file "LICENCE" distributed with Cforall. |
---|
| 7 | // |
---|
[84c52a8] | 8 | // monitor_desc.c -- |
---|
[f07e037] | 9 | // |
---|
| 10 | // Author : Thierry Delisle |
---|
| 11 | // Created On : Thd Feb 23 12:27:26 2017 |
---|
| 12 | // Last Modified By : Thierry Delisle |
---|
| 13 | // Last Modified On : -- |
---|
| 14 | // Update Count : 0 |
---|
| 15 | // |
---|
| 16 | |
---|
| 17 | #include "monitor" |
---|
| 18 | |
---|
[a933dcf4] | 19 | #include <stdlib> |
---|
| 20 | |
---|
[f07e037] | 21 | #include "kernel_private.h" |
---|
[5ea06d6] | 22 | #include "libhdr.h" |
---|
[f07e037] | 23 | |
---|
[0c78741] | 24 | //----------------------------------------------------------------------------- |
---|
| 25 | // Forward declarations |
---|
| 26 | static inline void set_owner( monitor_desc * this, thread_desc * owner ); |
---|
| 27 | static inline thread_desc * next_thread( monitor_desc * this ); |
---|
| 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 thread_desc * check_condition( __condition_criterion_t * ); |
---|
| 38 | static inline void brand_condition( condition * ); |
---|
| 39 | static inline unsigned short insert_unique( thread_desc ** thrds, unsigned short end, thread_desc * val ); |
---|
| 40 | |
---|
| 41 | //----------------------------------------------------------------------------- |
---|
| 42 | // Enter/Leave routines |
---|
[690f13c] | 43 | |
---|
| 44 | |
---|
[cb0e6de] | 45 | extern "C" { |
---|
[0c78741] | 46 | void __enter_monitor_desc(monitor_desc * this) { |
---|
[cb0e6de] | 47 | lock( &this->lock ); |
---|
| 48 | thread_desc * thrd = this_thread(); |
---|
[f07e037] | 49 | |
---|
[0c78741] | 50 | LIB_DEBUG_PRINT_SAFE("%p Entering %p (o: %p, r: %i)\n", thrd, this, this->owner, this->recursion); |
---|
[5ea06d6] | 51 | |
---|
[cb0e6de] | 52 | if( !this->owner ) { |
---|
| 53 | //No one has the monitor, just take it |
---|
[cd348e7] | 54 | set_owner( this, thrd ); |
---|
[cb0e6de] | 55 | } |
---|
| 56 | else if( this->owner == thrd) { |
---|
| 57 | //We already have the monitor, just not how many times we took it |
---|
[4aa2fb2] | 58 | verify( this->recursion > 0 ); |
---|
[cb0e6de] | 59 | this->recursion += 1; |
---|
| 60 | } |
---|
| 61 | else { |
---|
| 62 | //Some one else has the monitor, wait in line for it |
---|
| 63 | append( &this->entry_queue, thrd ); |
---|
[44264c5] | 64 | LIB_DEBUG_PRINT_SAFE("%p Blocking on entry\n", thrd); |
---|
[cb0e6de] | 65 | ScheduleInternal( &this->lock ); |
---|
[cc7f4b1] | 66 | |
---|
[cb0e6de] | 67 | //ScheduleInternal will unlock spinlock, no need to unlock ourselves |
---|
| 68 | return; |
---|
| 69 | } |
---|
[f07e037] | 70 | |
---|
[cb0e6de] | 71 | unlock( &this->lock ); |
---|
[5ea06d6] | 72 | return; |
---|
[cb0e6de] | 73 | } |
---|
[f07e037] | 74 | |
---|
[690f13c] | 75 | // leave pseudo code : |
---|
[0c78741] | 76 | // TODO |
---|
| 77 | void __leave_monitor_desc(monitor_desc * this) { |
---|
[cb0e6de] | 78 | lock( &this->lock ); |
---|
[f07e037] | 79 | |
---|
[0c78741] | 80 | LIB_DEBUG_PRINT_SAFE("%p Leaving %p (o: %p, r: %i)\n", thrd, this, this->owner, this->recursion); |
---|
[4aa2fb2] | 81 | verifyf( this_thread() == this->owner, "Expected owner to be %p, got %p (r: %i)", this_thread(), this->owner, this->recursion ); |
---|
[cc7f4b1] | 82 | |
---|
[cb0e6de] | 83 | //Leaving a recursion level, decrement the counter |
---|
| 84 | this->recursion -= 1; |
---|
[f07e037] | 85 | |
---|
[690f13c] | 86 | //If we haven't left the last level of recursion |
---|
| 87 | //it means we don't need to do anything |
---|
| 88 | if( this->recursion != 0) { |
---|
| 89 | unlock( &this->lock ); |
---|
| 90 | return; |
---|
| 91 | } |
---|
[f07e037] | 92 | |
---|
[0c78741] | 93 | thread_desc * new_owner = next_thread( this ); |
---|
[5ea06d6] | 94 | |
---|
[690f13c] | 95 | //We can now let other threads in safely |
---|
[cb0e6de] | 96 | unlock( &this->lock ); |
---|
[51f3798] | 97 | |
---|
[44264c5] | 98 | LIB_DEBUG_PRINT_SAFE("Next owner is %p\n", new_owner); |
---|
| 99 | |
---|
[690f13c] | 100 | //We need to wake-up the thread |
---|
| 101 | ScheduleThread( new_owner ); |
---|
[cc7f4b1] | 102 | } |
---|
[2781e65] | 103 | } |
---|
| 104 | |
---|
[5ea06d6] | 105 | static inline void enter(monitor_desc ** monitors, int count) { |
---|
[0c78741] | 106 | for(int i = 0; i < count; i++) { |
---|
| 107 | __enter_monitor_desc( monitors[i] ); |
---|
[2781e65] | 108 | } |
---|
| 109 | } |
---|
| 110 | |
---|
[5ea06d6] | 111 | static inline void leave(monitor_desc ** monitors, int count) { |
---|
[0c78741] | 112 | for(int i = count - 1; i >= 0; i--) { |
---|
| 113 | __leave_monitor_desc( monitors[i] ); |
---|
[2781e65] | 114 | } |
---|
[5ea06d6] | 115 | } |
---|
| 116 | |
---|
| 117 | void ?{}( monitor_guard_t * this, monitor_desc ** m, int count ) { |
---|
| 118 | this->m = m; |
---|
| 119 | this->count = count; |
---|
| 120 | qsort(this->m, count); |
---|
| 121 | enter( this->m, this->count ); |
---|
| 122 | |
---|
| 123 | this->prev_mntrs = this_thread()->current_monitors; |
---|
| 124 | this->prev_count = this_thread()->current_monitor_count; |
---|
| 125 | |
---|
| 126 | this_thread()->current_monitors = m; |
---|
| 127 | this_thread()->current_monitor_count = count; |
---|
| 128 | } |
---|
| 129 | |
---|
| 130 | void ^?{}( monitor_guard_t * this ) { |
---|
| 131 | leave( this->m, this->count ); |
---|
| 132 | |
---|
| 133 | this_thread()->current_monitors = this->prev_mntrs; |
---|
| 134 | this_thread()->current_monitor_count = this->prev_count; |
---|
| 135 | } |
---|
| 136 | |
---|
[be3d020] | 137 | void ?{}(__condition_node_t * this, thread_desc * waiting_thread, unsigned short count, uintptr_t user_info ) { |
---|
| 138 | this->waiting_thread = waiting_thread; |
---|
| 139 | this->count = count; |
---|
| 140 | this->next = NULL; |
---|
| 141 | this->user_info = user_info; |
---|
| 142 | } |
---|
| 143 | |
---|
| 144 | void ?{}(__condition_criterion_t * this ) { |
---|
| 145 | this->ready = false; |
---|
| 146 | this->target = NULL; |
---|
| 147 | this->owner = NULL; |
---|
| 148 | this->next = NULL; |
---|
| 149 | } |
---|
| 150 | |
---|
| 151 | void ?{}(__condition_criterion_t * this, monitor_desc * target, __condition_node_t * owner ) { |
---|
| 152 | this->ready = false; |
---|
| 153 | this->target = target; |
---|
| 154 | this->owner = owner; |
---|
| 155 | this->next = NULL; |
---|
[ad1a8dd] | 156 | } |
---|
| 157 | |
---|
[5ea06d6] | 158 | //----------------------------------------------------------------------------- |
---|
| 159 | // Internal scheduling |
---|
[be3d020] | 160 | void wait( condition * this, uintptr_t user_info = 0 ) { |
---|
[0c78741] | 161 | LIB_DEBUG_PRINT_SAFE("Waiting\n"); |
---|
[5ea06d6] | 162 | |
---|
[0c78741] | 163 | brand_condition( this ); |
---|
[5ea06d6] | 164 | |
---|
| 165 | //Check that everything is as expected |
---|
[0c78741] | 166 | assertf( this->monitors != NULL, "Waiting with no monitors (%p)", this->monitors ); |
---|
[4aa2fb2] | 167 | verifyf( this->monitor_count != 0, "Waiting with 0 monitors (%i)", this->monitor_count ); |
---|
| 168 | verifyf( this->monitor_count < 32u, "Excessive monitor count (%i)", this->monitor_count ); |
---|
[5ea06d6] | 169 | |
---|
[0c78741] | 170 | unsigned short count = this->monitor_count; |
---|
[9c59cd4] | 171 | unsigned int recursions[ count ]; //Save the current recursion levels to restore them later |
---|
| 172 | spinlock * locks [ count ]; //We need to pass-in an array of locks to ScheduleInternal |
---|
[0c78741] | 173 | |
---|
| 174 | LIB_DEBUG_PRINT_SAFE("count %i\n", count); |
---|
| 175 | |
---|
[be3d020] | 176 | __condition_node_t waiter = { this_thread(), count, user_info }; |
---|
[0c78741] | 177 | |
---|
| 178 | __condition_criterion_t criteria[count]; |
---|
| 179 | for(int i = 0; i < count; i++) { |
---|
[be3d020] | 180 | (&criteria[i]){ this->monitors[i], &waiter }; |
---|
[0c78741] | 181 | LIB_DEBUG_PRINT_SAFE( "Criterion %p\n", &criteria[i] ); |
---|
| 182 | } |
---|
[5ea06d6] | 183 | |
---|
[0c78741] | 184 | waiter.criteria = criteria; |
---|
| 185 | append( &this->blocked, &waiter ); |
---|
[5ea06d6] | 186 | |
---|
[9c59cd4] | 187 | lock_all( this->monitors, locks, count ); |
---|
| 188 | save_recursion( this->monitors, recursions, count ); |
---|
[0c78741] | 189 | //DON'T unlock, ask the kernel to do it |
---|
[5ea06d6] | 190 | |
---|
[0c78741] | 191 | //Find the next thread(s) to run |
---|
[ad1a8dd] | 192 | unsigned short thread_count = 0; |
---|
[0c78741] | 193 | thread_desc * threads[ count ]; |
---|
[ad1a8dd] | 194 | for(int i = 0; i < count; i++) { |
---|
| 195 | threads[i] = 0; |
---|
| 196 | } |
---|
| 197 | |
---|
[0c78741] | 198 | for( int i = 0; i < count; i++) { |
---|
| 199 | thread_desc * new_owner = next_thread( this->monitors[i] ); |
---|
[ad1a8dd] | 200 | thread_count = insert_unique( threads, thread_count, new_owner ); |
---|
[5ea06d6] | 201 | } |
---|
| 202 | |
---|
[0c78741] | 203 | LIB_DEBUG_PRINT_SAFE("Will unblock: "); |
---|
| 204 | for(int i = 0; i < thread_count; i++) { |
---|
| 205 | LIB_DEBUG_PRINT_SAFE("%p ", threads[i]); |
---|
| 206 | } |
---|
| 207 | LIB_DEBUG_PRINT_SAFE("\n"); |
---|
[5ea06d6] | 208 | |
---|
[9c59cd4] | 209 | // Everything is ready to go to sleep |
---|
| 210 | ScheduleInternal( locks, count, threads, thread_count ); |
---|
[5ea06d6] | 211 | |
---|
[c81ebf9] | 212 | |
---|
[5ea06d6] | 213 | //WE WOKE UP |
---|
| 214 | |
---|
| 215 | |
---|
| 216 | //We are back, restore the owners and recursions |
---|
[9c59cd4] | 217 | lock_all( locks, count ); |
---|
| 218 | restore_recursion( this->monitors, recursions, count ); |
---|
| 219 | unlock_all( locks, count ); |
---|
[5ea06d6] | 220 | } |
---|
| 221 | |
---|
[be3d020] | 222 | bool signal( condition * this ) { |
---|
| 223 | if( is_empty( this ) ) { |
---|
[0c78741] | 224 | LIB_DEBUG_PRINT_SAFE("Nothing to signal\n"); |
---|
[be3d020] | 225 | return false; |
---|
[0c78741] | 226 | } |
---|
[5ea06d6] | 227 | |
---|
| 228 | //Check that everything is as expected |
---|
[4aa2fb2] | 229 | verify( this->monitors ); |
---|
| 230 | verify( this->monitor_count != 0 ); |
---|
[0c78741] | 231 | |
---|
| 232 | unsigned short count = this->monitor_count; |
---|
[5ea06d6] | 233 | |
---|
[44264c5] | 234 | //Some more checking in debug |
---|
[5ea06d6] | 235 | LIB_DEBUG_DO( |
---|
[0c78741] | 236 | thread_desc * this_thrd = this_thread(); |
---|
| 237 | if ( this->monitor_count != this_thrd->current_monitor_count ) { |
---|
| 238 | abortf( "Signal on condition %p made with different number of monitor(s), expected %i got %i", this, this->monitor_count, this_thrd->current_monitor_count ); |
---|
[5ea06d6] | 239 | } // if |
---|
[0c78741] | 240 | |
---|
| 241 | for(int i = 0; i < this->monitor_count; i++) { |
---|
| 242 | if ( this->monitors[i] != this_thrd->current_monitors[i] ) { |
---|
| 243 | abortf( "Signal on condition %p made with different monitor, expected %p got %i", this, this->monitors[i], this_thrd->current_monitors[i] ); |
---|
| 244 | } // if |
---|
| 245 | } |
---|
[5ea06d6] | 246 | ); |
---|
| 247 | |
---|
[44264c5] | 248 | //Lock all the monitors |
---|
[0c78741] | 249 | lock_all( this->monitors, NULL, count ); |
---|
| 250 | LIB_DEBUG_PRINT_SAFE("Signalling"); |
---|
| 251 | |
---|
[44264c5] | 252 | //Pop the head of the waiting queue |
---|
[0c78741] | 253 | __condition_node_t * node = pop_head( &this->blocked ); |
---|
[44264c5] | 254 | |
---|
| 255 | //Add the thread to the proper AS stack |
---|
[0c78741] | 256 | for(int i = 0; i < count; i++) { |
---|
| 257 | __condition_criterion_t * crit = &node->criteria[i]; |
---|
| 258 | LIB_DEBUG_PRINT_SAFE(" %p", crit->target); |
---|
| 259 | assert( !crit->ready ); |
---|
| 260 | push( &crit->target->signal_stack, crit ); |
---|
[5ea06d6] | 261 | } |
---|
[0c78741] | 262 | |
---|
| 263 | LIB_DEBUG_PRINT_SAFE("\n"); |
---|
| 264 | |
---|
[44264c5] | 265 | //Release |
---|
[0c78741] | 266 | unlock_all( this->monitors, count ); |
---|
[be3d020] | 267 | |
---|
| 268 | return true; |
---|
[5ea06d6] | 269 | } |
---|
| 270 | |
---|
[be3d020] | 271 | bool signal_block( condition * this ) { |
---|
[44264c5] | 272 | if( !this->blocked.head ) { |
---|
| 273 | LIB_DEBUG_PRINT_SAFE("Nothing to signal\n"); |
---|
[be3d020] | 274 | return false; |
---|
[44264c5] | 275 | } |
---|
| 276 | |
---|
| 277 | //Check that everything is as expected |
---|
[4aa2fb2] | 278 | verifyf( this->monitors != NULL, "Waiting with no monitors (%p)", this->monitors ); |
---|
| 279 | verifyf( this->monitor_count != 0, "Waiting with 0 monitors (%i)", this->monitor_count ); |
---|
[44264c5] | 280 | |
---|
| 281 | unsigned short count = this->monitor_count; |
---|
| 282 | unsigned int recursions[ count ]; //Save the current recursion levels to restore them later |
---|
| 283 | spinlock * locks [ count ]; //We need to pass-in an array of locks to ScheduleInternal |
---|
| 284 | |
---|
| 285 | lock_all( this->monitors, locks, count ); |
---|
| 286 | |
---|
| 287 | //create creteria |
---|
[be3d020] | 288 | __condition_node_t waiter = { this_thread(), count, 0 }; |
---|
[44264c5] | 289 | |
---|
| 290 | __condition_criterion_t criteria[count]; |
---|
| 291 | for(int i = 0; i < count; i++) { |
---|
[be3d020] | 292 | (&criteria[i]){ this->monitors[i], &waiter }; |
---|
[44264c5] | 293 | LIB_DEBUG_PRINT_SAFE( "Criterion %p\n", &criteria[i] ); |
---|
| 294 | push( &criteria[i].target->signal_stack, &criteria[i] ); |
---|
| 295 | } |
---|
| 296 | |
---|
| 297 | waiter.criteria = criteria; |
---|
| 298 | |
---|
| 299 | //save contexts |
---|
| 300 | save_recursion( this->monitors, recursions, count ); |
---|
| 301 | |
---|
| 302 | //Find the thread to run |
---|
| 303 | thread_desc * signallee = pop_head( &this->blocked )->waiting_thread; |
---|
| 304 | for(int i = 0; i < count; i++) { |
---|
| 305 | set_owner( this->monitors[i], signallee ); |
---|
| 306 | } |
---|
| 307 | |
---|
| 308 | LIB_DEBUG_PRINT_SAFE( "Waiting on signal block\n" ); |
---|
| 309 | |
---|
| 310 | //Everything is ready to go to sleep |
---|
| 311 | ScheduleInternal( locks, count, &signallee, 1 ); |
---|
| 312 | |
---|
[c81ebf9] | 313 | |
---|
| 314 | |
---|
| 315 | |
---|
[44264c5] | 316 | LIB_DEBUG_PRINT_SAFE( "Back from signal block\n" ); |
---|
| 317 | |
---|
| 318 | //We are back, restore the owners and recursions |
---|
| 319 | lock_all( locks, count ); |
---|
| 320 | restore_recursion( this->monitors, recursions, count ); |
---|
| 321 | unlock_all( locks, count ); |
---|
[be3d020] | 322 | |
---|
| 323 | return true; |
---|
| 324 | } |
---|
| 325 | |
---|
| 326 | uintptr_t front( condition * this ) { |
---|
[4aa2fb2] | 327 | verifyf( !is_empty(this), |
---|
| 328 | "Attempt to access user data on an empty condition.\n" |
---|
| 329 | "Possible cause is not checking if the condition is empty before reading stored data." |
---|
[be3d020] | 330 | ); |
---|
| 331 | return this->blocked.head->user_info; |
---|
[44264c5] | 332 | } |
---|
| 333 | |
---|
[c81ebf9] | 334 | //----------------------------------------------------------------------------- |
---|
| 335 | // Internal scheduling |
---|
| 336 | void __accept_internal( unsigned short count, __acceptable_t * acceptables, void (*func)(void) ) { |
---|
| 337 | // thread_desc * this = this_thread(); |
---|
| 338 | |
---|
| 339 | // unsigned short count = this->current_monitor_count; |
---|
| 340 | // unsigned int recursions[ count ]; //Save the current recursion levels to restore them later |
---|
| 341 | // spinlock * locks [ count ]; //We need to pass-in an array of locks to ScheduleInternal |
---|
| 342 | |
---|
| 343 | // lock_all( this->current_monitors, locks, count ); |
---|
| 344 | |
---|
| 345 | |
---|
| 346 | |
---|
| 347 | |
---|
| 348 | |
---|
| 349 | // // // Everything is ready to go to sleep |
---|
| 350 | // // ScheduleInternal( locks, count, threads, thread_count ); |
---|
| 351 | |
---|
| 352 | |
---|
| 353 | // //WE WOKE UP |
---|
| 354 | |
---|
| 355 | |
---|
| 356 | // //We are back, restore the owners and recursions |
---|
| 357 | // lock_all( locks, count ); |
---|
| 358 | // restore_recursion( this->monitors, recursions, count ); |
---|
| 359 | // unlock_all( locks, count ); |
---|
| 360 | } |
---|
| 361 | |
---|
[0c78741] | 362 | //----------------------------------------------------------------------------- |
---|
| 363 | // Utilities |
---|
| 364 | |
---|
| 365 | static inline void set_owner( monitor_desc * this, thread_desc * owner ) { |
---|
| 366 | //Pass the monitor appropriately |
---|
| 367 | this->owner = owner; |
---|
| 368 | |
---|
| 369 | //We are passing the monitor to someone else, which means recursion level is not 0 |
---|
| 370 | this->recursion = owner ? 1 : 0; |
---|
| 371 | } |
---|
| 372 | |
---|
| 373 | static inline thread_desc * next_thread( monitor_desc * this ) { |
---|
| 374 | //Check the signaller stack |
---|
| 375 | __condition_criterion_t * urgent = pop( &this->signal_stack ); |
---|
| 376 | if( urgent ) { |
---|
| 377 | //The signaller stack is not empty, |
---|
| 378 | //regardless of if we are ready to baton pass, |
---|
| 379 | //we need to set the monitor as in use |
---|
| 380 | set_owner( this, urgent->owner->waiting_thread ); |
---|
| 381 | |
---|
| 382 | return check_condition( urgent ); |
---|
| 383 | } |
---|
| 384 | |
---|
| 385 | // No signaller thread |
---|
| 386 | // Get the next thread in the entry_queue |
---|
| 387 | thread_desc * new_owner = pop_head( &this->entry_queue ); |
---|
| 388 | set_owner( this, new_owner ); |
---|
| 389 | |
---|
| 390 | return new_owner; |
---|
| 391 | } |
---|
| 392 | |
---|
| 393 | static inline void lock_all( spinlock ** locks, unsigned short count ) { |
---|
| 394 | for( int i = 0; i < count; i++ ) { |
---|
| 395 | lock( locks[i] ); |
---|
| 396 | } |
---|
| 397 | } |
---|
| 398 | |
---|
| 399 | static inline void lock_all( monitor_desc ** source, spinlock ** /*out*/ locks, unsigned short count ) { |
---|
| 400 | for( int i = 0; i < count; i++ ) { |
---|
| 401 | spinlock * l = &source[i]->lock; |
---|
| 402 | lock( l ); |
---|
| 403 | if(locks) locks[i] = l; |
---|
| 404 | } |
---|
| 405 | } |
---|
| 406 | |
---|
| 407 | static inline void unlock_all( spinlock ** locks, unsigned short count ) { |
---|
| 408 | for( int i = 0; i < count; i++ ) { |
---|
| 409 | unlock( locks[i] ); |
---|
| 410 | } |
---|
| 411 | } |
---|
| 412 | |
---|
| 413 | static inline void unlock_all( monitor_desc ** locks, unsigned short count ) { |
---|
| 414 | for( int i = 0; i < count; i++ ) { |
---|
| 415 | unlock( &locks[i]->lock ); |
---|
| 416 | } |
---|
| 417 | } |
---|
| 418 | |
---|
| 419 | |
---|
| 420 | static inline void save_recursion ( monitor_desc ** ctx, unsigned int * /*out*/ recursions, unsigned short count ) { |
---|
| 421 | for( int i = 0; i < count; i++ ) { |
---|
| 422 | recursions[i] = ctx[i]->recursion; |
---|
| 423 | } |
---|
| 424 | } |
---|
| 425 | |
---|
| 426 | static inline void restore_recursion( monitor_desc ** ctx, unsigned int * /*in */ recursions, unsigned short count ) { |
---|
| 427 | for( int i = 0; i < count; i++ ) { |
---|
| 428 | ctx[i]->recursion = recursions[i]; |
---|
| 429 | } |
---|
| 430 | } |
---|
| 431 | |
---|
| 432 | // Function has 2 different behavior |
---|
| 433 | // 1 - Marks a monitors as being ready to run |
---|
| 434 | // 2 - Checks if all the monitors are ready to run |
---|
| 435 | // if so return the thread to run |
---|
| 436 | static inline thread_desc * check_condition( __condition_criterion_t * target ) { |
---|
| 437 | __condition_node_t * node = target->owner; |
---|
| 438 | unsigned short count = node->count; |
---|
| 439 | __condition_criterion_t * criteria = node->criteria; |
---|
| 440 | |
---|
| 441 | bool ready2run = true; |
---|
| 442 | |
---|
| 443 | for( int i = 0; i < count; i++ ) { |
---|
[44264c5] | 444 | |
---|
[0c78741] | 445 | LIB_DEBUG_PRINT_SAFE( "Checking %p for %p\n", &criteria[i], target ); |
---|
| 446 | if( &criteria[i] == target ) { |
---|
| 447 | criteria[i].ready = true; |
---|
| 448 | LIB_DEBUG_PRINT_SAFE( "True\n" ); |
---|
| 449 | } |
---|
| 450 | |
---|
| 451 | ready2run = criteria[i].ready && ready2run; |
---|
| 452 | } |
---|
| 453 | |
---|
| 454 | LIB_DEBUG_PRINT_SAFE( "Runing %i\n", ready2run ); |
---|
| 455 | return ready2run ? node->waiting_thread : NULL; |
---|
| 456 | } |
---|
| 457 | |
---|
| 458 | static inline void brand_condition( condition * this ) { |
---|
| 459 | thread_desc * thrd = this_thread(); |
---|
| 460 | if( !this->monitors ) { |
---|
| 461 | LIB_DEBUG_PRINT_SAFE("Branding\n"); |
---|
| 462 | assertf( thrd->current_monitors != NULL, "No current monitor to brand condition", thrd->current_monitors ); |
---|
| 463 | this->monitor_count = thrd->current_monitor_count; |
---|
[a933dcf4] | 464 | |
---|
| 465 | this->monitors = malloc( this->monitor_count * sizeof( *this->monitors ) ); |
---|
| 466 | for( int i = 0; i < this->monitor_count; i++ ) { |
---|
| 467 | this->monitors[i] = thrd->current_monitors[i]; |
---|
| 468 | } |
---|
[0c78741] | 469 | } |
---|
| 470 | } |
---|
| 471 | |
---|
| 472 | static inline unsigned short insert_unique( thread_desc ** thrds, unsigned short end, thread_desc * val ) { |
---|
[ad1a8dd] | 473 | if( !val ) return end; |
---|
| 474 | |
---|
| 475 | for(int i = 0; i <= end; i++) { |
---|
[0c78741] | 476 | if( thrds[i] == val ) return end; |
---|
| 477 | } |
---|
| 478 | |
---|
| 479 | thrds[end] = val; |
---|
| 480 | return end + 1; |
---|
| 481 | } |
---|
| 482 | |
---|
| 483 | void ?{}( __condition_blocked_queue_t * this ) { |
---|
| 484 | this->head = NULL; |
---|
| 485 | this->tail = &this->head; |
---|
| 486 | } |
---|
| 487 | |
---|
| 488 | void append( __condition_blocked_queue_t * this, __condition_node_t * c ) { |
---|
[4aa2fb2] | 489 | verify(this->tail != NULL); |
---|
[0c78741] | 490 | *this->tail = c; |
---|
| 491 | this->tail = &c->next; |
---|
| 492 | } |
---|
| 493 | |
---|
| 494 | __condition_node_t * pop_head( __condition_blocked_queue_t * this ) { |
---|
| 495 | __condition_node_t * head = this->head; |
---|
| 496 | if( head ) { |
---|
| 497 | this->head = head->next; |
---|
| 498 | if( !head->next ) { |
---|
| 499 | this->tail = &this->head; |
---|
| 500 | } |
---|
| 501 | head->next = NULL; |
---|
| 502 | } |
---|
| 503 | return head; |
---|
[4aa2fb2] | 504 | } |
---|