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