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