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