| [f07e037] | 1 | //
|
|---|
| 2 | // Cforall Version 1.0.0 Copyright (C) 2016 University of Waterloo
|
|---|
| 3 | //
|
|---|
| 4 | // The contents of this file are covered under the licence agreement in the
|
|---|
| 5 | // file "LICENCE" distributed with Cforall.
|
|---|
| 6 | //
|
|---|
| [84c52a8] | 7 | // monitor_desc.c --
|
|---|
| [f07e037] | 8 | //
|
|---|
| 9 | // Author : Thierry Delisle
|
|---|
| 10 | // Created On : Thd Feb 23 12:27:26 2017
|
|---|
| [6b0b624] | 11 | // Last Modified By : Peter A. Buhr
|
|---|
| [38ef0de] | 12 | // Last Modified On : Mon Jul 31 14:59:05 2017
|
|---|
| 13 | // Update Count : 3
|
|---|
| [f07e037] | 14 | //
|
|---|
| 15 |
|
|---|
| 16 | #include "monitor"
|
|---|
| 17 |
|
|---|
| [a933dcf4] | 18 | #include <stdlib>
|
|---|
| 19 |
|
|---|
| [5ea06d6] | 20 | #include "libhdr.h"
|
|---|
| [2ac095d] | 21 | #include "kernel_private.h"
|
|---|
| [f07e037] | 22 |
|
|---|
| [0c78741] | 23 | //-----------------------------------------------------------------------------
|
|---|
| 24 | // Forward declarations
|
|---|
| 25 | static inline void set_owner( monitor_desc * this, thread_desc * owner );
|
|---|
| 26 | static inline thread_desc * next_thread( monitor_desc * this );
|
|---|
| [97e3296] | 27 | static inline int is_accepted( thread_desc * owner, monitor_desc * this, monitor_desc ** group, int group_cnt, void (*func)() );
|
|---|
| [0c78741] | 28 |
|
|---|
| 29 | static inline void lock_all( spinlock ** locks, unsigned short count );
|
|---|
| 30 | static inline void lock_all( monitor_desc ** source, spinlock ** /*out*/ locks, unsigned short count );
|
|---|
| 31 | static inline void unlock_all( spinlock ** locks, unsigned short count );
|
|---|
| 32 | static inline void unlock_all( monitor_desc ** locks, unsigned short count );
|
|---|
| 33 |
|
|---|
| 34 | static inline void save_recursion ( monitor_desc ** ctx, unsigned int * /*out*/ recursions, unsigned short count );
|
|---|
| 35 | static inline void restore_recursion( monitor_desc ** ctx, unsigned int * /*in */ recursions, unsigned short count );
|
|---|
| 36 |
|
|---|
| [97e3296] | 37 | static inline void init ( int count, monitor_desc ** monitors, __condition_node_t * waiter, __condition_criterion_t * criteria );
|
|---|
| 38 | static inline void init_push( int count, monitor_desc ** monitors, __condition_node_t * waiter, __condition_criterion_t * criteria );
|
|---|
| 39 |
|
|---|
| [0c78741] | 40 | static inline thread_desc * check_condition( __condition_criterion_t * );
|
|---|
| 41 | static inline void brand_condition( condition * );
|
|---|
| 42 | static inline unsigned short insert_unique( thread_desc ** thrds, unsigned short end, thread_desc * val );
|
|---|
| 43 |
|
|---|
| [97e3296] | 44 | static inline thread_desc * search_entry_queue( __acceptable_t * acceptables, int acc_count, monitor_desc ** monitors, int count );
|
|---|
| 45 |
|
|---|
| 46 | //-----------------------------------------------------------------------------
|
|---|
| 47 | // Useful defines
|
|---|
| 48 | #define wait_ctx(thrd, user_info) /* Create the necessary information to use the signaller stack */ \
|
|---|
| 49 | __condition_node_t waiter = { thrd, count, user_info }; /* Create the node specific to this wait operation */ \
|
|---|
| 50 | __condition_criterion_t criteria[count]; /* Create the creteria this wait operation needs to wake up */ \
|
|---|
| 51 | init( count, monitors, &waiter, criteria ); /* Link everything together */ \
|
|---|
| 52 |
|
|---|
| 53 | #define wait_ctx_primed(thrd, user_info) /* Create the necessary information to use the signaller stack */ \
|
|---|
| 54 | __condition_node_t waiter = { thrd, count, user_info }; /* Create the node specific to this wait operation */ \
|
|---|
| 55 | __condition_criterion_t criteria[count]; /* Create the creteria this wait operation needs to wake up */ \
|
|---|
| 56 | init_push( count, monitors, &waiter, criteria ); /* Link everything together and push it to the AS-Stack */ \
|
|---|
| 57 |
|
|---|
| 58 | #define monitor_ctx( mons, cnt ) /* Define that create the necessary struct for internal/external scheduling operations */ \
|
|---|
| 59 | monitor_desc ** monitors = mons; /* Save the targeted monitors */ \
|
|---|
| 60 | unsigned short count = cnt; /* Save the count to a local variable */ \
|
|---|
| 61 | unsigned int recursions[ count ]; /* Save the current recursion levels to restore them later */ \
|
|---|
| 62 | spinlock * locks [ count ]; /* We need to pass-in an array of locks to BlockInternal */ \
|
|---|
| 63 |
|
|---|
| [0c78741] | 64 | //-----------------------------------------------------------------------------
|
|---|
| 65 | // Enter/Leave routines
|
|---|
| [690f13c] | 66 |
|
|---|
| 67 |
|
|---|
| [cb0e6de] | 68 | extern "C" {
|
|---|
| [97e3296] | 69 | // Enter single monitor
|
|---|
| 70 | static void __enter_monitor_desc( monitor_desc * this, monitor_desc ** group, int group_cnt, void (*func)() ) {
|
|---|
| 71 | // Lock the monitor spinlock, lock_yield to reduce contention
|
|---|
| [b227f68] | 72 | lock_yield( &this->lock DEBUG_CTX2 );
|
|---|
| [1c273d0] | 73 | thread_desc * thrd = this_thread;
|
|---|
| [f07e037] | 74 |
|
|---|
| [97e3296] | 75 | this->accepted_index = -1;
|
|---|
| [cb0e6de] | 76 | if( !this->owner ) {
|
|---|
| [97e3296] | 77 | // No one has the monitor, just take it
|
|---|
| [cd348e7] | 78 | set_owner( this, thrd );
|
|---|
| [cb0e6de] | 79 | }
|
|---|
| 80 | else if( this->owner == thrd) {
|
|---|
| [97e3296] | 81 | // We already have the monitor, just not how many times we took it
|
|---|
| [4aa2fb2] | 82 | verify( this->recursion > 0 );
|
|---|
| [cb0e6de] | 83 | this->recursion += 1;
|
|---|
| 84 | }
|
|---|
| [97e3296] | 85 | else if( (this->accepted_index = is_accepted( thrd, this, group, group_cnt, func)) >= 0 ) {
|
|---|
| 86 | // Some one was waiting for us, enter
|
|---|
| 87 | set_owner( this, thrd );
|
|---|
| 88 | }
|
|---|
| [cb0e6de] | 89 | else {
|
|---|
| [97e3296] | 90 | // Some one else has the monitor, wait in line for it
|
|---|
| [cb0e6de] | 91 | append( &this->entry_queue, thrd );
|
|---|
| [82ff5845] | 92 | BlockInternal( &this->lock );
|
|---|
| [cc7f4b1] | 93 |
|
|---|
| [97e3296] | 94 | // BlockInternal will unlock spinlock, no need to unlock ourselves
|
|---|
| [2ac095d] | 95 | return;
|
|---|
| [cb0e6de] | 96 | }
|
|---|
| [f07e037] | 97 |
|
|---|
| [97e3296] | 98 | // Release the lock and leave
|
|---|
| [cb0e6de] | 99 | unlock( &this->lock );
|
|---|
| [5ea06d6] | 100 | return;
|
|---|
| [cb0e6de] | 101 | }
|
|---|
| [f07e037] | 102 |
|
|---|
| [97e3296] | 103 | // Leave single monitor
|
|---|
| [1c273d0] | 104 | void __leave_monitor_desc( monitor_desc * this ) {
|
|---|
| [97e3296] | 105 | // Lock the monitor spinlock, lock_yield to reduce contention
|
|---|
| [b227f68] | 106 | lock_yield( &this->lock DEBUG_CTX2 );
|
|---|
| [f07e037] | 107 |
|
|---|
| [1c273d0] | 108 | verifyf( this_thread == this->owner, "Expected owner to be %p, got %p (r: %i)", this_thread, this->owner, this->recursion );
|
|---|
| [cc7f4b1] | 109 |
|
|---|
| [97e3296] | 110 | // Leaving a recursion level, decrement the counter
|
|---|
| [cb0e6de] | 111 | this->recursion -= 1;
|
|---|
| [f07e037] | 112 |
|
|---|
| [97e3296] | 113 | // If we haven't left the last level of recursion
|
|---|
| 114 | // it means we don't need to do anything
|
|---|
| [690f13c] | 115 | if( this->recursion != 0) {
|
|---|
| 116 | unlock( &this->lock );
|
|---|
| 117 | return;
|
|---|
| 118 | }
|
|---|
| [f07e037] | 119 |
|
|---|
| [97e3296] | 120 | // Get the next thread, will be null on low contention monitor
|
|---|
| [0c78741] | 121 | thread_desc * new_owner = next_thread( this );
|
|---|
| [5ea06d6] | 122 |
|
|---|
| [97e3296] | 123 | // We can now let other threads in safely
|
|---|
| [cb0e6de] | 124 | unlock( &this->lock );
|
|---|
| [51f3798] | 125 |
|
|---|
| [690f13c] | 126 | //We need to wake-up the thread
|
|---|
| [1c273d0] | 127 | WakeThread( new_owner );
|
|---|
| 128 | }
|
|---|
| 129 |
|
|---|
| [97e3296] | 130 | // Leave the thread monitor
|
|---|
| 131 | // last routine called by a thread.
|
|---|
| 132 | // Should never return
|
|---|
| [1c273d0] | 133 | void __leave_thread_monitor( thread_desc * thrd ) {
|
|---|
| 134 | monitor_desc * this = &thrd->mon;
|
|---|
| [97e3296] | 135 |
|
|---|
| 136 | // Lock the monitor now
|
|---|
| [b227f68] | 137 | lock_yield( &this->lock DEBUG_CTX2 );
|
|---|
| [1c273d0] | 138 |
|
|---|
| 139 | disable_interrupts();
|
|---|
| 140 |
|
|---|
| 141 | thrd->cor.state = Halted;
|
|---|
| 142 |
|
|---|
| 143 | verifyf( thrd == this->owner, "Expected owner to be %p, got %p (r: %i)", thrd, this->owner, this->recursion );
|
|---|
| 144 |
|
|---|
| [97e3296] | 145 | // Leaving a recursion level, decrement the counter
|
|---|
| [1c273d0] | 146 | this->recursion -= 1;
|
|---|
| 147 |
|
|---|
| [97e3296] | 148 | // If we haven't left the last level of recursion
|
|---|
| 149 | // it must mean there is an error
|
|---|
| 150 | if( this->recursion != 0) { abortf("Thread internal monitor has unbalanced recursion"); }
|
|---|
| [1c273d0] | 151 |
|
|---|
| [97e3296] | 152 | // Fetch the next thread, can be null
|
|---|
| [1c273d0] | 153 | thread_desc * new_owner = next_thread( this );
|
|---|
| 154 |
|
|---|
| [97e3296] | 155 | // Leave the thread, this will unlock the spinlock
|
|---|
| 156 | // Use leave thread instead of BlockInternal which is
|
|---|
| 157 | // specialized for this case and supports null new_owner
|
|---|
| [f2b12406] | 158 | LeaveThread( &this->lock, new_owner );
|
|---|
| [97e3296] | 159 |
|
|---|
| 160 | // Control flow should never reach here!
|
|---|
| [cc7f4b1] | 161 | }
|
|---|
| [2781e65] | 162 | }
|
|---|
| 163 |
|
|---|
| [97e3296] | 164 | // Enter multiple monitor
|
|---|
| 165 | // relies on the monitor array being sorted
|
|---|
| 166 | static inline void enter(monitor_desc ** monitors, int count, void (*func)() ) {
|
|---|
| [0c78741] | 167 | for(int i = 0; i < count; i++) {
|
|---|
| [97e3296] | 168 | __enter_monitor_desc( monitors[i], monitors, count, func );
|
|---|
| 169 | }
|
|---|
| 170 |
|
|---|
| 171 | int acc_idx = monitors[0]->accepted_index;
|
|---|
| 172 | if( acc_idx >= 0 && monitors[0]->acceptables[ acc_idx ].run_preaccept ) {
|
|---|
| 173 | assert( monitors[0]->pre_accept );
|
|---|
| 174 | monitors[0]->pre_accept();
|
|---|
| [2781e65] | 175 | }
|
|---|
| 176 | }
|
|---|
| 177 |
|
|---|
| [97e3296] | 178 | // Leave multiple monitor
|
|---|
| 179 | // relies on the monitor array being sorted
|
|---|
| [5ea06d6] | 180 | static inline void leave(monitor_desc ** monitors, int count) {
|
|---|
| [0c78741] | 181 | for(int i = count - 1; i >= 0; i--) {
|
|---|
| 182 | __leave_monitor_desc( monitors[i] );
|
|---|
| [2781e65] | 183 | }
|
|---|
| [5ea06d6] | 184 | }
|
|---|
| 185 |
|
|---|
| [97e3296] | 186 | // Ctor for monitor guard
|
|---|
| 187 | // Sorts monitors before entering
|
|---|
| 188 | void ?{}( monitor_guard_t * this, monitor_desc ** m, int count, void (*func)() ) {
|
|---|
| 189 | // Store current array
|
|---|
| [5ea06d6] | 190 | this->m = m;
|
|---|
| 191 | this->count = count;
|
|---|
| [97e3296] | 192 |
|
|---|
| 193 | // Sort monitors based on address -> TODO use a sort specialized for small numbers
|
|---|
| [5ea06d6] | 194 | qsort(this->m, count);
|
|---|
| 195 |
|
|---|
| [97e3296] | 196 | // Enter the monitors in order
|
|---|
| 197 | enter( this->m, this->count, func );
|
|---|
| 198 |
|
|---|
| 199 | // Save previous thread context
|
|---|
| [1c273d0] | 200 | this->prev_mntrs = this_thread->current_monitors;
|
|---|
| 201 | this->prev_count = this_thread->current_monitor_count;
|
|---|
| [5ea06d6] | 202 |
|
|---|
| [97e3296] | 203 | // Update thread context (needed for conditions)
|
|---|
| [1c273d0] | 204 | this_thread->current_monitors = m;
|
|---|
| 205 | this_thread->current_monitor_count = count;
|
|---|
| [5ea06d6] | 206 | }
|
|---|
| 207 |
|
|---|
| [97e3296] | 208 | // Dtor for monitor guard
|
|---|
| [5ea06d6] | 209 | void ^?{}( monitor_guard_t * this ) {
|
|---|
| [97e3296] | 210 | // Leave the monitors in order
|
|---|
| [5ea06d6] | 211 | leave( this->m, this->count );
|
|---|
| 212 |
|
|---|
| [97e3296] | 213 | // Restore thread context
|
|---|
| [1c273d0] | 214 | this_thread->current_monitors = this->prev_mntrs;
|
|---|
| 215 | this_thread->current_monitor_count = this->prev_count;
|
|---|
| [5ea06d6] | 216 | }
|
|---|
| 217 |
|
|---|
| [97e3296] | 218 | //-----------------------------------------------------------------------------
|
|---|
| 219 | // Internal scheduling types
|
|---|
| 220 |
|
|---|
| [be3d020] | 221 | void ?{}(__condition_node_t * this, thread_desc * waiting_thread, unsigned short count, uintptr_t user_info ) {
|
|---|
| 222 | this->waiting_thread = waiting_thread;
|
|---|
| 223 | this->count = count;
|
|---|
| 224 | this->next = NULL;
|
|---|
| 225 | this->user_info = user_info;
|
|---|
| 226 | }
|
|---|
| 227 |
|
|---|
| 228 | void ?{}(__condition_criterion_t * this ) {
|
|---|
| 229 | this->ready = false;
|
|---|
| 230 | this->target = NULL;
|
|---|
| 231 | this->owner = NULL;
|
|---|
| 232 | this->next = NULL;
|
|---|
| 233 | }
|
|---|
| 234 |
|
|---|
| 235 | void ?{}(__condition_criterion_t * this, monitor_desc * target, __condition_node_t * owner ) {
|
|---|
| 236 | this->ready = false;
|
|---|
| 237 | this->target = target;
|
|---|
| 238 | this->owner = owner;
|
|---|
| 239 | this->next = NULL;
|
|---|
| [ad1a8dd] | 240 | }
|
|---|
| 241 |
|
|---|
| [5ea06d6] | 242 | //-----------------------------------------------------------------------------
|
|---|
| 243 | // Internal scheduling
|
|---|
| [be3d020] | 244 | void wait( condition * this, uintptr_t user_info = 0 ) {
|
|---|
| [0c78741] | 245 | brand_condition( this );
|
|---|
| [5ea06d6] | 246 |
|
|---|
| [97e3296] | 247 | // Check that everything is as expected
|
|---|
| [0c78741] | 248 | assertf( this->monitors != NULL, "Waiting with no monitors (%p)", this->monitors );
|
|---|
| [4aa2fb2] | 249 | verifyf( this->monitor_count != 0, "Waiting with 0 monitors (%i)", this->monitor_count );
|
|---|
| 250 | verifyf( this->monitor_count < 32u, "Excessive monitor count (%i)", this->monitor_count );
|
|---|
| [5ea06d6] | 251 |
|
|---|
| [97e3296] | 252 | // Create storage for monitor context
|
|---|
| 253 | monitor_ctx( this->monitors, this->monitor_count );
|
|---|
| [0c78741] | 254 |
|
|---|
| [97e3296] | 255 | // Create the node specific to this wait operation
|
|---|
| 256 | wait_ctx( this_thread, user_info );
|
|---|
| [0c78741] | 257 |
|
|---|
| [97e3296] | 258 | // Append the current wait operation to the ones already queued on the condition
|
|---|
| 259 | // We don't need locks for that since conditions must always be waited on inside monitor mutual exclusion
|
|---|
| 260 | append( &this->blocked, &waiter );
|
|---|
| [0c78741] | 261 |
|
|---|
| [97e3296] | 262 | // Lock all monitors (aggregates the lock them as well)
|
|---|
| 263 | lock_all( monitors, locks, count );
|
|---|
| [5ea06d6] | 264 |
|
|---|
| [97e3296] | 265 | // DON'T unlock, ask the kernel to do it
|
|---|
| [5ea06d6] | 266 |
|
|---|
| [97e3296] | 267 | // Save monitor state
|
|---|
| 268 | save_recursion( monitors, recursions, count );
|
|---|
| [5ea06d6] | 269 |
|
|---|
| [97e3296] | 270 | // Find the next thread(s) to run
|
|---|
| [ad1a8dd] | 271 | unsigned short thread_count = 0;
|
|---|
| [0c78741] | 272 | thread_desc * threads[ count ];
|
|---|
| [ad1a8dd] | 273 | for(int i = 0; i < count; i++) {
|
|---|
| 274 | threads[i] = 0;
|
|---|
| 275 | }
|
|---|
| 276 |
|
|---|
| [97e3296] | 277 | // Remove any duplicate threads
|
|---|
| [0c78741] | 278 | for( int i = 0; i < count; i++) {
|
|---|
| [97e3296] | 279 | thread_desc * new_owner = next_thread( monitors[i] );
|
|---|
| [ad1a8dd] | 280 | thread_count = insert_unique( threads, thread_count, new_owner );
|
|---|
| [5ea06d6] | 281 | }
|
|---|
| 282 |
|
|---|
| [9c59cd4] | 283 | // Everything is ready to go to sleep
|
|---|
| [82ff5845] | 284 | BlockInternal( locks, count, threads, thread_count );
|
|---|
| [5ea06d6] | 285 |
|
|---|
| [c81ebf9] | 286 |
|
|---|
| [97e3296] | 287 | // WE WOKE UP
|
|---|
| [5ea06d6] | 288 |
|
|---|
| 289 |
|
|---|
| [97e3296] | 290 | // We are back, restore the owners and recursions
|
|---|
| [9c59cd4] | 291 | lock_all( locks, count );
|
|---|
| [97e3296] | 292 | restore_recursion( monitors, recursions, count );
|
|---|
| [9c59cd4] | 293 | unlock_all( locks, count );
|
|---|
| [5ea06d6] | 294 | }
|
|---|
| 295 |
|
|---|
| [be3d020] | 296 | bool signal( condition * this ) {
|
|---|
| [97e3296] | 297 | if( is_empty( this ) ) { return false; }
|
|---|
| [5ea06d6] | 298 |
|
|---|
| 299 | //Check that everything is as expected
|
|---|
| [4aa2fb2] | 300 | verify( this->monitors );
|
|---|
| 301 | verify( this->monitor_count != 0 );
|
|---|
| [0c78741] | 302 |
|
|---|
| [44264c5] | 303 | //Some more checking in debug
|
|---|
| [5ea06d6] | 304 | LIB_DEBUG_DO(
|
|---|
| [1c273d0] | 305 | thread_desc * this_thrd = this_thread;
|
|---|
| [0c78741] | 306 | if ( this->monitor_count != this_thrd->current_monitor_count ) {
|
|---|
| 307 | abortf( "Signal on condition %p made with different number of monitor(s), expected %i got %i", this, this->monitor_count, this_thrd->current_monitor_count );
|
|---|
| [97e3296] | 308 | }
|
|---|
| [0c78741] | 309 |
|
|---|
| 310 | for(int i = 0; i < this->monitor_count; i++) {
|
|---|
| 311 | if ( this->monitors[i] != this_thrd->current_monitors[i] ) {
|
|---|
| 312 | abortf( "Signal on condition %p made with different monitor, expected %p got %i", this, this->monitors[i], this_thrd->current_monitors[i] );
|
|---|
| [97e3296] | 313 | }
|
|---|
| [0c78741] | 314 | }
|
|---|
| [5ea06d6] | 315 | );
|
|---|
| 316 |
|
|---|
| [97e3296] | 317 | unsigned short count = this->monitor_count;
|
|---|
| 318 |
|
|---|
| 319 | // Lock all monitors
|
|---|
| [0c78741] | 320 | lock_all( this->monitors, NULL, count );
|
|---|
| 321 |
|
|---|
| [44264c5] | 322 | //Pop the head of the waiting queue
|
|---|
| [0c78741] | 323 | __condition_node_t * node = pop_head( &this->blocked );
|
|---|
| [44264c5] | 324 |
|
|---|
| 325 | //Add the thread to the proper AS stack
|
|---|
| [0c78741] | 326 | for(int i = 0; i < count; i++) {
|
|---|
| 327 | __condition_criterion_t * crit = &node->criteria[i];
|
|---|
| 328 | assert( !crit->ready );
|
|---|
| 329 | push( &crit->target->signal_stack, crit );
|
|---|
| [5ea06d6] | 330 | }
|
|---|
| [0c78741] | 331 |
|
|---|
| [44264c5] | 332 | //Release
|
|---|
| [0c78741] | 333 | unlock_all( this->monitors, count );
|
|---|
| [be3d020] | 334 |
|
|---|
| 335 | return true;
|
|---|
| [5ea06d6] | 336 | }
|
|---|
| 337 |
|
|---|
| [be3d020] | 338 | bool signal_block( condition * this ) {
|
|---|
| [97e3296] | 339 | if( !this->blocked.head ) { return false; }
|
|---|
| [44264c5] | 340 |
|
|---|
| 341 | //Check that everything is as expected
|
|---|
| [4aa2fb2] | 342 | verifyf( this->monitors != NULL, "Waiting with no monitors (%p)", this->monitors );
|
|---|
| 343 | verifyf( this->monitor_count != 0, "Waiting with 0 monitors (%i)", this->monitor_count );
|
|---|
| [44264c5] | 344 |
|
|---|
| [97e3296] | 345 | // Create storage for monitor context
|
|---|
| 346 | monitor_ctx( this->monitors, this->monitor_count );
|
|---|
| [44264c5] | 347 |
|
|---|
| [97e3296] | 348 | // Lock all monitors (aggregates the locks them as well)
|
|---|
| 349 | lock_all( monitors, locks, count );
|
|---|
| [44264c5] | 350 |
|
|---|
| [97e3296] | 351 | // Create the node specific to this wait operation
|
|---|
| 352 | wait_ctx_primed( this_thread, 0 )
|
|---|
| [44264c5] | 353 |
|
|---|
| 354 | //save contexts
|
|---|
| [97e3296] | 355 | save_recursion( monitors, recursions, count );
|
|---|
| [44264c5] | 356 |
|
|---|
| 357 | //Find the thread to run
|
|---|
| 358 | thread_desc * signallee = pop_head( &this->blocked )->waiting_thread;
|
|---|
| 359 | for(int i = 0; i < count; i++) {
|
|---|
| [97e3296] | 360 | set_owner( monitors[i], signallee );
|
|---|
| [44264c5] | 361 | }
|
|---|
| 362 |
|
|---|
| 363 | //Everything is ready to go to sleep
|
|---|
| [82ff5845] | 364 | BlockInternal( locks, count, &signallee, 1 );
|
|---|
| [44264c5] | 365 |
|
|---|
| [c81ebf9] | 366 |
|
|---|
| [97e3296] | 367 | // WE WOKE UP
|
|---|
| [c81ebf9] | 368 |
|
|---|
| 369 |
|
|---|
| [44264c5] | 370 | //We are back, restore the owners and recursions
|
|---|
| 371 | lock_all( locks, count );
|
|---|
| [97e3296] | 372 | restore_recursion( monitors, recursions, count );
|
|---|
| [44264c5] | 373 | unlock_all( locks, count );
|
|---|
| [be3d020] | 374 |
|
|---|
| 375 | return true;
|
|---|
| 376 | }
|
|---|
| 377 |
|
|---|
| [97e3296] | 378 | // Access the user_info of the thread waiting at the front of the queue
|
|---|
| [be3d020] | 379 | uintptr_t front( condition * this ) {
|
|---|
| [2ac095d] | 380 | verifyf( !is_empty(this),
|
|---|
| [4aa2fb2] | 381 | "Attempt to access user data on an empty condition.\n"
|
|---|
| 382 | "Possible cause is not checking if the condition is empty before reading stored data."
|
|---|
| [be3d020] | 383 | );
|
|---|
| 384 | return this->blocked.head->user_info;
|
|---|
| [44264c5] | 385 | }
|
|---|
| 386 |
|
|---|
| [c81ebf9] | 387 | //-----------------------------------------------------------------------------
|
|---|
| 388 | // Internal scheduling
|
|---|
| [97e3296] | 389 | int __accept_internal( unsigned short acc_count, __acceptable_t * acceptables ) {
|
|---|
| 390 | thread_desc * thrd = this_thread;
|
|---|
| 391 |
|
|---|
| 392 | // Create storage for monitor context
|
|---|
| 393 | monitor_ctx( acceptables->monitors, acceptables->count );
|
|---|
| [c81ebf9] | 394 |
|
|---|
| [97e3296] | 395 | // Lock all monitors (aggregates the lock them as well)
|
|---|
| 396 | lock_all( monitors, locks, count );
|
|---|
| [c81ebf9] | 397 |
|
|---|
| [97e3296] | 398 | // Create the node specific to this wait operation
|
|---|
| 399 | wait_ctx_primed( thrd, 0 );
|
|---|
| [c81ebf9] | 400 |
|
|---|
| [97e3296] | 401 | // Check if the entry queue
|
|---|
| 402 | thread_desc * next = search_entry_queue( acceptables, acc_count, monitors, count );
|
|---|
| [c81ebf9] | 403 |
|
|---|
| [97e3296] | 404 | if( !next ) {
|
|---|
| 405 | // Update acceptables on the current monitors
|
|---|
| 406 | for(int i = 0; i < count; i++) {
|
|---|
| 407 | monitors[i]->acceptables = acceptables;
|
|---|
| 408 | monitors[i]->acceptable_count = acc_count;
|
|---|
| 409 | }
|
|---|
| 410 | }
|
|---|
| [c81ebf9] | 411 |
|
|---|
| [97e3296] | 412 | save_recursion( monitors, recursions, count );
|
|---|
| [c81ebf9] | 413 |
|
|---|
| [97e3296] | 414 | // Everything is ready to go to sleep
|
|---|
| 415 | BlockInternal( locks, count, &next, next ? 1 : 0 );
|
|---|
| [c81ebf9] | 416 |
|
|---|
| 417 |
|
|---|
| [97e3296] | 418 | //WE WOKE UP
|
|---|
| [c81ebf9] | 419 |
|
|---|
| 420 |
|
|---|
| [97e3296] | 421 | //We are back, restore the owners and recursions
|
|---|
| 422 | lock_all( locks, count );
|
|---|
| 423 | restore_recursion( monitors, recursions, count );
|
|---|
| 424 | int acc_idx = monitors[0]->accepted_index;
|
|---|
| 425 | unlock_all( locks, count );
|
|---|
| [c81ebf9] | 426 |
|
|---|
| [97e3296] | 427 | return acc_idx;
|
|---|
| [c81ebf9] | 428 | }
|
|---|
| 429 |
|
|---|
| [0c78741] | 430 | //-----------------------------------------------------------------------------
|
|---|
| 431 | // Utilities
|
|---|
| 432 |
|
|---|
| 433 | static inline void set_owner( monitor_desc * this, thread_desc * owner ) {
|
|---|
| 434 | //Pass the monitor appropriately
|
|---|
| 435 | this->owner = owner;
|
|---|
| 436 |
|
|---|
| 437 | //We are passing the monitor to someone else, which means recursion level is not 0
|
|---|
| 438 | this->recursion = owner ? 1 : 0;
|
|---|
| 439 | }
|
|---|
| 440 |
|
|---|
| 441 | static inline thread_desc * next_thread( monitor_desc * this ) {
|
|---|
| 442 | //Check the signaller stack
|
|---|
| 443 | __condition_criterion_t * urgent = pop( &this->signal_stack );
|
|---|
| 444 | if( urgent ) {
|
|---|
| 445 | //The signaller stack is not empty,
|
|---|
| 446 | //regardless of if we are ready to baton pass,
|
|---|
| 447 | //we need to set the monitor as in use
|
|---|
| 448 | set_owner( this, urgent->owner->waiting_thread );
|
|---|
| 449 |
|
|---|
| 450 | return check_condition( urgent );
|
|---|
| 451 | }
|
|---|
| 452 |
|
|---|
| 453 | // No signaller thread
|
|---|
| 454 | // Get the next thread in the entry_queue
|
|---|
| 455 | thread_desc * new_owner = pop_head( &this->entry_queue );
|
|---|
| 456 | set_owner( this, new_owner );
|
|---|
| 457 |
|
|---|
| 458 | return new_owner;
|
|---|
| 459 | }
|
|---|
| 460 |
|
|---|
| [97e3296] | 461 | static inline int is_accepted( thread_desc * owner, monitor_desc * this, monitor_desc ** group, int group_cnt, void (*func)() ) {
|
|---|
| 462 | __acceptable_t* accs = this->acceptables; // Optim
|
|---|
| 463 | int acc_cnt = this->acceptable_count;
|
|---|
| 464 |
|
|---|
| 465 | // Check if there are any acceptable functions
|
|---|
| 466 | if( !accs ) return -1;
|
|---|
| 467 |
|
|---|
| 468 | // If this isn't the first monitor to test this, there is no reason to repeat the test.
|
|---|
| 469 | if( this != group[0] ) return group[0]->accepted_index;
|
|---|
| 470 |
|
|---|
| 471 | // For all acceptable functions check if this is the current function.
|
|---|
| 472 | OUT_LOOP:
|
|---|
| 473 | for( int i = 0; i < acc_cnt; i++ ) {
|
|---|
| 474 | __acceptable_t * acc = &accs[i];
|
|---|
| 475 |
|
|---|
| 476 | // if function matches, check the monitors
|
|---|
| 477 | if( acc->func == func ) {
|
|---|
| 478 |
|
|---|
| 479 | // If the group count is different then it can't be a match
|
|---|
| 480 | if( acc->count != group_cnt ) return -1;
|
|---|
| 481 |
|
|---|
| 482 | // Check that all the monitors match
|
|---|
| 483 | for( int j = 0; j < group_cnt; j++ ) {
|
|---|
| 484 | // If not a match, check next function
|
|---|
| 485 | if( acc->monitors[j] != group[j] ) continue OUT_LOOP;
|
|---|
| 486 | }
|
|---|
| 487 |
|
|---|
| 488 | // It's a complete match, accept the call
|
|---|
| 489 | return i;
|
|---|
| 490 | }
|
|---|
| 491 | }
|
|---|
| 492 |
|
|---|
| 493 | // No function matched
|
|---|
| 494 | return -1;
|
|---|
| 495 | }
|
|---|
| 496 |
|
|---|
| 497 | static inline void init( int count, monitor_desc ** monitors, __condition_node_t * waiter, __condition_criterion_t * criteria ) {
|
|---|
| 498 | for(int i = 0; i < count; i++) {
|
|---|
| 499 | (&criteria[i]){ monitors[i], waiter };
|
|---|
| 500 | }
|
|---|
| 501 |
|
|---|
| 502 | waiter->criteria = criteria;
|
|---|
| 503 | }
|
|---|
| 504 |
|
|---|
| 505 | static inline void init_push( int count, monitor_desc ** monitors, __condition_node_t * waiter, __condition_criterion_t * criteria ) {
|
|---|
| 506 | for(int i = 0; i < count; i++) {
|
|---|
| 507 | (&criteria[i]){ monitors[i], waiter };
|
|---|
| 508 | push( &criteria[i].target->signal_stack, &criteria[i] );
|
|---|
| 509 | }
|
|---|
| 510 |
|
|---|
| 511 | waiter->criteria = criteria;
|
|---|
| 512 | }
|
|---|
| 513 |
|
|---|
| [0c78741] | 514 | static inline void lock_all( spinlock ** locks, unsigned short count ) {
|
|---|
| 515 | for( int i = 0; i < count; i++ ) {
|
|---|
| [b227f68] | 516 | lock_yield( locks[i] DEBUG_CTX2 );
|
|---|
| [0c78741] | 517 | }
|
|---|
| 518 | }
|
|---|
| 519 |
|
|---|
| 520 | static inline void lock_all( monitor_desc ** source, spinlock ** /*out*/ locks, unsigned short count ) {
|
|---|
| 521 | for( int i = 0; i < count; i++ ) {
|
|---|
| 522 | spinlock * l = &source[i]->lock;
|
|---|
| [b227f68] | 523 | lock_yield( l DEBUG_CTX2 );
|
|---|
| [0c78741] | 524 | if(locks) locks[i] = l;
|
|---|
| 525 | }
|
|---|
| 526 | }
|
|---|
| 527 |
|
|---|
| 528 | static inline void unlock_all( spinlock ** locks, unsigned short count ) {
|
|---|
| 529 | for( int i = 0; i < count; i++ ) {
|
|---|
| 530 | unlock( locks[i] );
|
|---|
| 531 | }
|
|---|
| 532 | }
|
|---|
| 533 |
|
|---|
| 534 | static inline void unlock_all( monitor_desc ** locks, unsigned short count ) {
|
|---|
| 535 | for( int i = 0; i < count; i++ ) {
|
|---|
| 536 | unlock( &locks[i]->lock );
|
|---|
| 537 | }
|
|---|
| 538 | }
|
|---|
| 539 |
|
|---|
| 540 |
|
|---|
| 541 | static inline void save_recursion ( monitor_desc ** ctx, unsigned int * /*out*/ recursions, unsigned short count ) {
|
|---|
| 542 | for( int i = 0; i < count; i++ ) {
|
|---|
| 543 | recursions[i] = ctx[i]->recursion;
|
|---|
| 544 | }
|
|---|
| 545 | }
|
|---|
| 546 |
|
|---|
| 547 | static inline void restore_recursion( monitor_desc ** ctx, unsigned int * /*in */ recursions, unsigned short count ) {
|
|---|
| 548 | for( int i = 0; i < count; i++ ) {
|
|---|
| 549 | ctx[i]->recursion = recursions[i];
|
|---|
| 550 | }
|
|---|
| 551 | }
|
|---|
| 552 |
|
|---|
| 553 | // Function has 2 different behavior
|
|---|
| 554 | // 1 - Marks a monitors as being ready to run
|
|---|
| 555 | // 2 - Checks if all the monitors are ready to run
|
|---|
| 556 | // if so return the thread to run
|
|---|
| 557 | static inline thread_desc * check_condition( __condition_criterion_t * target ) {
|
|---|
| 558 | __condition_node_t * node = target->owner;
|
|---|
| 559 | unsigned short count = node->count;
|
|---|
| 560 | __condition_criterion_t * criteria = node->criteria;
|
|---|
| 561 |
|
|---|
| 562 | bool ready2run = true;
|
|---|
| 563 |
|
|---|
| 564 | for( int i = 0; i < count; i++ ) {
|
|---|
| [44264c5] | 565 |
|
|---|
| [b227f68] | 566 | // LIB_DEBUG_PRINT_SAFE( "Checking %p for %p\n", &criteria[i], target );
|
|---|
| [0c78741] | 567 | if( &criteria[i] == target ) {
|
|---|
| 568 | criteria[i].ready = true;
|
|---|
| [b227f68] | 569 | // LIB_DEBUG_PRINT_SAFE( "True\n" );
|
|---|
| [0c78741] | 570 | }
|
|---|
| 571 |
|
|---|
| 572 | ready2run = criteria[i].ready && ready2run;
|
|---|
| 573 | }
|
|---|
| 574 |
|
|---|
| [b227f68] | 575 | // LIB_DEBUG_PRINT_SAFE( "Runing %i\n", ready2run );
|
|---|
| [0c78741] | 576 | return ready2run ? node->waiting_thread : NULL;
|
|---|
| 577 | }
|
|---|
| 578 |
|
|---|
| 579 | static inline void brand_condition( condition * this ) {
|
|---|
| [1c273d0] | 580 | thread_desc * thrd = this_thread;
|
|---|
| [0c78741] | 581 | if( !this->monitors ) {
|
|---|
| [b227f68] | 582 | // LIB_DEBUG_PRINT_SAFE("Branding\n");
|
|---|
| [38ef0de] | 583 | assertf( thrd->current_monitors != NULL, "No current monitor to brand condition %p", thrd->current_monitors );
|
|---|
| [0c78741] | 584 | this->monitor_count = thrd->current_monitor_count;
|
|---|
| [a933dcf4] | 585 |
|
|---|
| 586 | this->monitors = malloc( this->monitor_count * sizeof( *this->monitors ) );
|
|---|
| 587 | for( int i = 0; i < this->monitor_count; i++ ) {
|
|---|
| 588 | this->monitors[i] = thrd->current_monitors[i];
|
|---|
| 589 | }
|
|---|
| [0c78741] | 590 | }
|
|---|
| 591 | }
|
|---|
| 592 |
|
|---|
| 593 | static inline unsigned short insert_unique( thread_desc ** thrds, unsigned short end, thread_desc * val ) {
|
|---|
| [ad1a8dd] | 594 | if( !val ) return end;
|
|---|
| 595 |
|
|---|
| 596 | for(int i = 0; i <= end; i++) {
|
|---|
| [0c78741] | 597 | if( thrds[i] == val ) return end;
|
|---|
| 598 | }
|
|---|
| 599 |
|
|---|
| 600 | thrds[end] = val;
|
|---|
| 601 | return end + 1;
|
|---|
| 602 | }
|
|---|
| 603 |
|
|---|
| [97e3296] | 604 | static inline thread_desc * search_entry_queue( __acceptable_t * acceptables, int acc_count, monitor_desc ** monitors, int count ) {
|
|---|
| 605 | return NULL;
|
|---|
| 606 | }
|
|---|
| 607 |
|
|---|
| [0c78741] | 608 | void ?{}( __condition_blocked_queue_t * this ) {
|
|---|
| 609 | this->head = NULL;
|
|---|
| 610 | this->tail = &this->head;
|
|---|
| 611 | }
|
|---|
| 612 |
|
|---|
| 613 | void append( __condition_blocked_queue_t * this, __condition_node_t * c ) {
|
|---|
| [4aa2fb2] | 614 | verify(this->tail != NULL);
|
|---|
| [0c78741] | 615 | *this->tail = c;
|
|---|
| 616 | this->tail = &c->next;
|
|---|
| 617 | }
|
|---|
| 618 |
|
|---|
| 619 | __condition_node_t * pop_head( __condition_blocked_queue_t * this ) {
|
|---|
| 620 | __condition_node_t * head = this->head;
|
|---|
| 621 | if( head ) {
|
|---|
| 622 | this->head = head->next;
|
|---|
| 623 | if( !head->next ) {
|
|---|
| 624 | this->tail = &this->head;
|
|---|
| 625 | }
|
|---|
| 626 | head->next = NULL;
|
|---|
| 627 | }
|
|---|
| 628 | return head;
|
|---|
| [4aa2fb2] | 629 | }
|
|---|
| [6b0b624] | 630 |
|
|---|
| 631 | // Local Variables: //
|
|---|
| 632 | // mode: c //
|
|---|
| 633 | // tab-width: 4 //
|
|---|
| 634 | // End: //
|
|---|