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