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