source: src/libcfa/concurrency/monitor.c@ 05f4b85

ADT aaron-thesis arm-eh ast-experimental cleanup-dtors deferred_resn demangler enum forall-pointer-decay jacob/cs343-translation jenkins-sandbox new-ast new-ast-unique-expr new-env no_list persistent-indexer pthread-emulation qualifiedEnum resolv-new with_gc
Last change on this file since 05f4b85 was 2e9aed4, checked in by Thierry Delisle <tdelisle@…>, 8 years ago

Fixed non-preemptive locks

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