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