source: libcfa/src/concurrency/monitor.cfa@ b544afa

ADT arm-eh ast-experimental enum forall-pointer-decay jacob/cs343-translation jenkins-sandbox new-ast new-ast-unique-expr pthread-emulation qualifiedEnum
Last change on this file since b544afa was 2026bb6, checked in by Thierry Delisle <tdelisle@…>, 6 years ago

More robust fix for optionally linking threads

  • Property mode set to 100644
File size: 28.9 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
[b10affd]12// Last Modified On : Fri Mar 30 14:30:26 2018
13// Update Count : 9
[f07e037]14//
15
[2026bb6]16#define __cforall_thread__
17
[58b6d1b]18#include "monitor.hfa"
[f07e037]19
[73abe95]20#include <stdlib.hfa>
[2f6a7e93]21#include <inttypes.h>
[a933dcf4]22
[73abe95]23#include "kernel_private.hfa"
[f07e037]24
[58b6d1b]25#include "bits/algorithm.hfa"
[de737c8]26
[0c78741]27//-----------------------------------------------------------------------------
28// Forward declarations
[daacf82]29static inline void set_owner ( monitor_desc * this, thread_desc * owner );
[513daec]30static inline void set_owner ( monitor_desc * storage [], __lock_size_t count, thread_desc * owner );
31static inline void set_mask ( monitor_desc * storage [], __lock_size_t count, const __waitfor_mask_t & mask );
[daacf82]32static inline void reset_mask( monitor_desc * this );
[6ff4507]33
[0c78741]34static inline thread_desc * next_thread( monitor_desc * this );
[6ae8c92]35static inline bool is_accepted( monitor_desc * this, const __monitor_group_t & monitors );
[0c78741]36
[ea7d2b0]37static inline void lock_all ( __spinlock_t * locks [], __lock_size_t count );
38static inline void lock_all ( monitor_desc * source [], __spinlock_t * /*out*/ locks [], __lock_size_t count );
39static inline void unlock_all( __spinlock_t * locks [], __lock_size_t count );
[513daec]40static inline void unlock_all( monitor_desc * locks [], __lock_size_t count );
[0c78741]41
[ea7d2b0]42static inline void save ( monitor_desc * ctx [], __lock_size_t count, __spinlock_t * locks [], unsigned int /*out*/ recursions [], __waitfor_mask_t /*out*/ masks [] );
43static inline void restore( monitor_desc * ctx [], __lock_size_t count, __spinlock_t * locks [], unsigned int /*in */ recursions [], __waitfor_mask_t /*in */ masks [] );
[0c78741]44
[513daec]45static inline void init ( __lock_size_t count, monitor_desc * monitors [], __condition_node_t & waiter, __condition_criterion_t criteria [] );
46static inline void init_push( __lock_size_t count, monitor_desc * monitors [], __condition_node_t & waiter, __condition_criterion_t criteria [] );
[97e3296]47
[6ff4507]48static inline thread_desc * check_condition ( __condition_criterion_t * );
[4cedd9f]49static inline void brand_condition ( condition & );
[59a0bde]50static inline [thread_desc *, int] search_entry_queue( const __waitfor_mask_t &, monitor_desc * monitors [], __lock_size_t count );
[b18830e]51
[6ff4507]52forall(dtype T | sized( T ))
[59a0bde]53static inline __lock_size_t insert_unique( T * array [], __lock_size_t & size, T * val );
54static inline __lock_size_t count_max ( const __waitfor_mask_t & mask );
55static inline __lock_size_t aggregate ( monitor_desc * storage [], const __waitfor_mask_t & mask );
[97e3296]56
57//-----------------------------------------------------------------------------
58// Useful defines
[6ff4507]59#define wait_ctx(thrd, user_info) /* Create the necessary information to use the signaller stack */ \
60 __condition_node_t waiter = { thrd, count, user_info }; /* Create the node specific to this wait operation */ \
61 __condition_criterion_t criteria[count]; /* Create the creteria this wait operation needs to wake up */ \
[8fc45b7]62 init( count, monitors, waiter, criteria ); /* Link everything together */ \
[6ff4507]63
64#define wait_ctx_primed(thrd, user_info) /* Create the necessary information to use the signaller stack */ \
65 __condition_node_t waiter = { thrd, count, user_info }; /* Create the node specific to this wait operation */ \
66 __condition_criterion_t criteria[count]; /* Create the creteria this wait operation needs to wake up */ \
[8fc45b7]67 init_push( count, monitors, waiter, criteria ); /* Link everything together and push it to the AS-Stack */ \
[6ff4507]68
69#define monitor_ctx( mons, cnt ) /* Define that create the necessary struct for internal/external scheduling operations */ \
70 monitor_desc ** monitors = mons; /* Save the targeted monitors */ \
[59a0bde]71 __lock_size_t count = cnt; /* Save the count to a local variable */ \
[6ff4507]72 unsigned int recursions[ count ]; /* Save the current recursion levels to restore them later */ \
[4cedd9f]73 __waitfor_mask_t masks [ count ]; /* Save the current waitfor masks to restore them later */ \
[ea7d2b0]74 __spinlock_t * locks [ count ]; /* We need to pass-in an array of locks to BlockInternal */ \
[6ff4507]75
76#define monitor_save save ( monitors, count, locks, recursions, masks )
77#define monitor_restore restore( monitors, count, locks, recursions, masks )
78
[97e3296]79
[0c78741]80//-----------------------------------------------------------------------------
81// Enter/Leave routines
[690f13c]82
83
[cb0e6de]84extern "C" {
[97e3296]85 // Enter single monitor
[a843067]86 static void __enter_monitor_desc( monitor_desc * this, const __monitor_group_t & group ) {
[34c6c767]87 // Lock the monitor spinlock
[2e9aed4]88 lock( this->lock __cfaabi_dbg_ctx2 );
[14a61b5]89 // Interrupts disable inside critical section
90 thread_desc * thrd = kernelTLS.this_thread;
[f07e037]91
[169d944]92 __cfaabi_dbg_print_safe( "Kernel : %10p Entering mon %p (%p)\n", thrd, this, this->owner);
[90c4df0]93
[cb0e6de]94 if( !this->owner ) {
[97e3296]95 // No one has the monitor, just take it
[cd348e7]96 set_owner( this, thrd );
[90c4df0]97
[169d944]98 __cfaabi_dbg_print_safe( "Kernel : mon is free \n" );
[cb0e6de]99 }
100 else if( this->owner == thrd) {
[549c006]101 // We already have the monitor, just note how many times we took it
[cb0e6de]102 this->recursion += 1;
[90c4df0]103
[169d944]104 __cfaabi_dbg_print_safe( "Kernel : mon already owned \n" );
[cb0e6de]105 }
[6ae8c92]106 else if( is_accepted( this, group) ) {
[97e3296]107 // Some one was waiting for us, enter
108 set_owner( this, thrd );
[90c4df0]109
[daacf82]110 // Reset mask
111 reset_mask( this );
112
[169d944]113 __cfaabi_dbg_print_safe( "Kernel : mon accepts \n" );
[97e3296]114 }
[cb0e6de]115 else {
[169d944]116 __cfaabi_dbg_print_safe( "Kernel : blocking \n" );
[90c4df0]117
[97e3296]118 // Some one else has the monitor, wait in line for it
[8fc45b7]119 append( this->entry_queue, thrd );
[2e9aed4]120
[82ff5845]121 BlockInternal( &this->lock );
[cc7f4b1]122
[169d944]123 __cfaabi_dbg_print_safe( "Kernel : %10p Entered mon %p\n", thrd, this);
[90c4df0]124
[97e3296]125 // BlockInternal will unlock spinlock, no need to unlock ourselves
[2ac095d]126 return;
[cb0e6de]127 }
[f07e037]128
[169d944]129 __cfaabi_dbg_print_safe( "Kernel : %10p Entered mon %p\n", thrd, this);
[90c4df0]130
[97e3296]131 // Release the lock and leave
[ea7d2b0]132 unlock( this->lock );
[5ea06d6]133 return;
[cb0e6de]134 }
[f07e037]135
[549c006]136 static void __enter_monitor_dtor( monitor_desc * this, fptr_t func ) {
[34c6c767]137 // Lock the monitor spinlock
[2e9aed4]138 lock( this->lock __cfaabi_dbg_ctx2 );
[14a61b5]139 // Interrupts disable inside critical section
140 thread_desc * thrd = kernelTLS.this_thread;
[549c006]141
[169d944]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 ) {
[169d944]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!
[2fdbb3b]157 abort( "Attempt to destroy monitor %p by thread \"%.256s\" (%p) in nested mutex.", this, thrd->self_cor.name, thrd );
[549c006]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) ) {
[169d944]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
[14a61b5]174 wait_ctx_primed( thrd, 0 )
[549c006]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 {
[169d944]183 __cfaabi_dbg_print_safe( "Kernel : blocking \n" );
[549c006]184
[14a61b5]185 wait_ctx( thrd, 0 )
[549c006]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
[169d944]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
[14a61b5]205 __cfaabi_dbg_print_safe( "Kernel : %10p Leaving mon %p (%p)\n", kernelTLS.this_thread, this, this->owner);
[a843067]206
[14a61b5]207 verifyf( kernelTLS.this_thread == this->owner, "Expected owner to be %p, got %p (r: %i, m: %p)", kernelTLS.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) {
[169d944]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(
[b10affd]233 if( TL_GET( this_thread ) != this->owner ) {
234 abort( "Destroyed monitor %p has inconsistent owner, expected %p got %p.\n", this, TL_GET( this_thread ), this->owner);
[549c006]235 }
236 if( this->recursion != 1 ) {
[169d944]237 abort( "Destroyed monitor %p has %d outstanding nested calls.\n", this, this->recursion - 1);
[549c006]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
[169d944]262 if( this->recursion != 0) { abort( "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 ) {
[14a61b5]295 thread_desc * thrd = TL_GET( this_thread );
296
[97e3296]297 // Store current array
[242a902]298 this.m = m;
299 this.count = count;
[97e3296]300
[09800e9]301 // Sort monitors based on address
[de737c8]302 __libcfa_small_sort(this.m, count);
[5ea06d6]303
[97e3296]304 // Save previous thread context
[14a61b5]305 this.prev = thrd->monitors;
[5ea06d6]306
[97e3296]307 // Update thread context (needed for conditions)
[14a61b5]308 (thrd->monitors){m, count, func};
[90c4df0]309
[169d944]310 // __cfaabi_dbg_print_safe( "MGUARD : enter %d\n", count);
[a843067]311
[90c4df0]312 // Enter the monitors in order
[6ae8c92]313 __monitor_group_t group = {this.m, this.count, func};
[b18830e]314 enter( group );
[a843067]315
[169d944]316 // __cfaabi_dbg_print_safe( "MGUARD : entered\n" );
[5ea06d6]317}
318
[6b224a52]319
[97e3296]320// Dtor for monitor guard
[242a902]321void ^?{}( monitor_guard_t & this ) {
[169d944]322 // __cfaabi_dbg_print_safe( "MGUARD : leaving %d\n", this.count);
[a843067]323
[97e3296]324 // Leave the monitors in order
[242a902]325 leave( this.m, this.count );
[5ea06d6]326
[169d944]327 // __cfaabi_dbg_print_safe( "MGUARD : left\n" );
[a843067]328
[97e3296]329 // Restore thread context
[b10affd]330 TL_GET( this_thread )->monitors = this.prev;
[5ea06d6]331}
332
[549c006]333// Ctor for monitor guard
334// Sorts monitors before entering
[8fc45b7]335void ?{}( monitor_dtor_guard_t & this, monitor_desc * m [], fptr_t func ) {
[14a61b5]336 // optimization
337 thread_desc * thrd = TL_GET( this_thread );
338
[549c006]339 // Store current array
340 this.m = *m;
341
342 // Save previous thread context
[14a61b5]343 this.prev = thrd->monitors;
[549c006]344
345 // Update thread context (needed for conditions)
[14a61b5]346 (thrd->monitors){m, 1, func};
[549c006]347
348 __enter_monitor_dtor( this.m, func );
349}
350
351// Dtor for monitor guard
352void ^?{}( monitor_dtor_guard_t & this ) {
353 // Leave the monitors in order
354 __leave_dtor_monitor_desc( this.m );
355
356 // Restore thread context
[b10affd]357 TL_GET( this_thread )->monitors = this.prev;
[549c006]358}
359
[97e3296]360//-----------------------------------------------------------------------------
361// Internal scheduling types
[59a0bde]362void ?{}(__condition_node_t & this, thread_desc * waiting_thread, __lock_size_t count, uintptr_t user_info ) {
[242a902]363 this.waiting_thread = waiting_thread;
364 this.count = count;
365 this.next = NULL;
366 this.user_info = user_info;
[be3d020]367}
368
[c40e7c5]369void ?{}(__condition_criterion_t & this ) with( this ) {
370 ready = false;
371 target = NULL;
372 owner = NULL;
373 next = NULL;
[be3d020]374}
375
[8fc45b7]376void ?{}(__condition_criterion_t & this, monitor_desc * target, __condition_node_t & owner ) {
[242a902]377 this.ready = false;
378 this.target = target;
[8fc45b7]379 this.owner = &owner;
[242a902]380 this.next = NULL;
[ad1a8dd]381}
382
[5ea06d6]383//-----------------------------------------------------------------------------
384// Internal scheduling
[4cedd9f]385void wait( condition & this, uintptr_t user_info = 0 ) {
[0c78741]386 brand_condition( this );
[5ea06d6]387
[97e3296]388 // Check that everything is as expected
[4cedd9f]389 assertf( this.monitors != NULL, "Waiting with no monitors (%p)", this.monitors );
[2f6a7e93]390 verifyf( this.monitor_count != 0, "Waiting with 0 monitors (%"PRIiFAST16")", this.monitor_count );
391 verifyf( this.monitor_count < 32u, "Excessive monitor count (%"PRIiFAST16")", this.monitor_count );
[5ea06d6]392
[97e3296]393 // Create storage for monitor context
[4cedd9f]394 monitor_ctx( this.monitors, this.monitor_count );
[0c78741]395
[97e3296]396 // Create the node specific to this wait operation
[b10affd]397 wait_ctx( TL_GET( this_thread ), user_info );
[0c78741]398
[97e3296]399 // Append the current wait operation to the ones already queued on the condition
400 // We don't need locks for that since conditions must always be waited on inside monitor mutual exclusion
[8fc45b7]401 append( this.blocked, &waiter );
[0c78741]402
[6ff4507]403 // Lock all monitors (aggregates the locks as well)
[97e3296]404 lock_all( monitors, locks, count );
[5ea06d6]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(
[b10affd]436 thread_desc * this_thrd = TL_GET( this_thread );
[4cedd9f]437 if ( this.monitor_count != this_thrd->monitors.size ) {
[c2ca04d]438 abort( "Signal on condition %p made with different number of monitor(s), expected %zi got %zi", &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] ) {
[2fdbb3b]443 abort( "Signal on condition %p made with different monitor, expected %p got %p", &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
[97e3296]479 // Lock all monitors (aggregates the locks them as well)
480 lock_all( monitors, locks, count );
[44264c5]481
[2e9aed4]482
[97e3296]483 // Create the node specific to this wait operation
[afd550c]484 wait_ctx_primed( kernelTLS.this_thread, 0 )
[44264c5]485
486 //save contexts
[6ff4507]487 monitor_save;
[44264c5]488
489 //Find the thread to run
[8fc45b7]490 thread_desc * signallee = pop_head( this.blocked )->waiting_thread;
[6ff4507]491 set_owner( monitors, count, signallee );
[44264c5]492
[36982fc]493 __cfaabi_dbg_print_buffer_decl( "Kernel : signal_block condition %p (s: %p)\n", &this, signallee );
[de737c8]494
[44264c5]495 //Everything is ready to go to sleep
[82ff5845]496 BlockInternal( locks, count, &signallee, 1 );
[44264c5]497
[c81ebf9]498
[97e3296]499 // WE WOKE UP
[c81ebf9]500
501
[36982fc]502 __cfaabi_dbg_print_buffer_local( "Kernel : signal_block returned\n" );
[de737c8]503
[6ff4507]504 //We are back, restore the masks and recursions
505 monitor_restore;
[be3d020]506
507 return true;
508}
509
[97e3296]510// Access the user_info of the thread waiting at the front of the queue
[4cedd9f]511uintptr_t front( condition & this ) {
[2ac095d]512 verifyf( !is_empty(this),
[4aa2fb2]513 "Attempt to access user data on an empty condition.\n"
514 "Possible cause is not checking if the condition is empty before reading stored data."
[be3d020]515 );
[0cf5b79]516 return ((typeof(this.blocked.head))this.blocked.head)->user_info;
[44264c5]517}
518
[c81ebf9]519//-----------------------------------------------------------------------------
[b18830e]520// External scheduling
521// cases to handle :
522// - target already there :
523// block and wake
524// - dtor already there
525// put thread on signaller stack
526// - non-blocking
527// return else
528// - timeout
529// return timeout
530// - block
531// setup mask
532// block
[6ae8c92]533void __waitfor_internal( const __waitfor_mask_t & mask, int duration ) {
[b18830e]534 // This statment doesn't have a contiguous list of monitors...
535 // Create one!
[59a0bde]536 __lock_size_t max = count_max( mask );
[b18830e]537 monitor_desc * mon_storage[max];
[66298de]538 __builtin_memset( mon_storage, 0, sizeof( mon_storage ) );
[59a0bde]539 __lock_size_t actual_count = aggregate( mon_storage, mask );
[97e3296]540
[523232d]541 __cfaabi_dbg_print_buffer_decl( "Kernel : waitfor %"PRIdFAST16" (s: %"PRIdFAST16", m: %"PRIdFAST16")\n", actual_count, mask.size, (__lock_size_t)max);
[66298de]542
[daacf82]543 if(actual_count == 0) return;
[19c43b7]544
[169d944]545 __cfaabi_dbg_print_buffer_local( "Kernel : waitfor internal proceeding\n" );
[4cc9b13]546
[97e3296]547 // Create storage for monitor context
[b18830e]548 monitor_ctx( mon_storage, actual_count );
[c81ebf9]549
[6ff4507]550 // Lock all monitors (aggregates the locks as well)
[97e3296]551 lock_all( monitors, locks, count );
[c81ebf9]552
[b18830e]553 {
554 // Check if the entry queue
[6ae8c92]555 thread_desc * next; int index;
556 [next, index] = search_entry_queue( mask, monitors, count );
[b18830e]557
558 if( next ) {
[19c43b7]559 *mask.accepted = index;
[0cf5b79]560 __acceptable_t& accepted = mask[index];
561 if( accepted.is_dtor ) {
[169d944]562 __cfaabi_dbg_print_buffer_local( "Kernel : dtor already there\n" );
[0cf5b79]563 verifyf( accepted.size == 1, "ERROR: Accepted dtor has more than 1 mutex parameter." );
[549c006]564
[0cf5b79]565 monitor_desc * mon2dtor = accepted[0];
[549c006]566 verifyf( mon2dtor->dtor_node, "ERROR: Accepted monitor has no dtor_node." );
567
568 __condition_criterion_t * dtor_crit = mon2dtor->dtor_node->criteria;
[8fc45b7]569 push( mon2dtor->signal_stack, dtor_crit );
[549c006]570
571 unlock_all( locks, count );
[b18830e]572 }
573 else {
[169d944]574 __cfaabi_dbg_print_buffer_local( "Kernel : thread present, baton-passing\n" );
[19c43b7]575
576 // Create the node specific to this wait operation
[14a61b5]577 wait_ctx_primed( kernelTLS.this_thread, 0 );
[19c43b7]578
579 // Save monitor states
580 monitor_save;
581
[523232d]582 __cfaabi_dbg_print_buffer_local( "Kernel : baton of %"PRIdFAST16" monitors : ", count );
[6a5be52]583 #ifdef __CFA_DEBUG_PRINT__
584 for( int i = 0; i < count; i++) {
[36982fc]585 __cfaabi_dbg_print_buffer_local( "%p %p ", monitors[i], monitors[i]->signal_stack.top );
[6a5be52]586 }
587 #endif
[169d944]588 __cfaabi_dbg_print_buffer_local( "\n" );
[6a5be52]589
[19c43b7]590 // Set the owners to be the next thread
591 set_owner( monitors, count, next );
592
593 // Everything is ready to go to sleep
594 BlockInternal( locks, count, &next, 1 );
595
596 // We are back, restore the owners and recursions
597 monitor_restore;
598
[169d944]599 __cfaabi_dbg_print_buffer_local( "Kernel : thread present, returned\n" );
[b18830e]600 }
601
[36982fc]602 __cfaabi_dbg_print_buffer_local( "Kernel : accepted %d\n", *mask.accepted);
[19c43b7]603 return;
[90c4df0]604 }
605 }
606
[c81ebf9]607
[4cc9b13]608 if( duration == 0 ) {
[169d944]609 __cfaabi_dbg_print_buffer_local( "Kernel : non-blocking, exiting\n" );
[19c43b7]610
[4cc9b13]611 unlock_all( locks, count );
[19c43b7]612
[36982fc]613 __cfaabi_dbg_print_buffer_local( "Kernel : accepted %d\n", *mask.accepted);
[4cc9b13]614 return;
615 }
[b18830e]616
617
[169d944]618 verifyf( duration < 0, "Timeout on waitfor statments not supported yet." );
[b18830e]619
[169d944]620 __cfaabi_dbg_print_buffer_local( "Kernel : blocking waitfor\n" );
[19c43b7]621
622 // Create the node specific to this wait operation
[14a61b5]623 wait_ctx_primed( kernelTLS.this_thread, 0 );
[b18830e]624
[6ff4507]625 monitor_save;
[6ae8c92]626 set_mask( monitors, count, mask );
[c81ebf9]627
[59a0bde]628 for( __lock_size_t i = 0; i < count; i++) {
[14a61b5]629 verify( monitors[i]->owner == kernelTLS.this_thread );
[daacf82]630 }
631
[19c43b7]632 //Everything is ready to go to sleep
633 BlockInternal( locks, count );
634
635
636 // WE WOKE UP
637
638
639 //We are back, restore the masks and recursions
640 monitor_restore;
641
[169d944]642 __cfaabi_dbg_print_buffer_local( "Kernel : exiting\n" );
[19c43b7]643
[36982fc]644 __cfaabi_dbg_print_buffer_local( "Kernel : accepted %d\n", *mask.accepted);
[c81ebf9]645}
646
[0c78741]647//-----------------------------------------------------------------------------
648// Utilities
649
650static inline void set_owner( monitor_desc * this, thread_desc * owner ) {
[169d944]651 // __cfaabi_dbg_print_safe( "Kernal : Setting owner of %p to %p ( was %p)\n", this, owner, this->owner );
[a843067]652
[0c78741]653 //Pass the monitor appropriately
654 this->owner = owner;
655
656 //We are passing the monitor to someone else, which means recursion level is not 0
657 this->recursion = owner ? 1 : 0;
658}
659
[513daec]660static inline void set_owner( monitor_desc * monitors [], __lock_size_t count, thread_desc * owner ) {
[6a5be52]661 monitors[0]->owner = owner;
662 monitors[0]->recursion = 1;
[513daec]663 for( __lock_size_t i = 1; i < count; i++ ) {
[6a5be52]664 monitors[i]->owner = owner;
665 monitors[i]->recursion = 0;
[6ff4507]666 }
667}
668
[513daec]669static inline void set_mask( monitor_desc * storage [], __lock_size_t count, const __waitfor_mask_t & mask ) {
670 for( __lock_size_t i = 0; i < count; i++) {
[6ff4507]671 storage[i]->mask = mask;
672 }
673}
674
[daacf82]675static inline void reset_mask( monitor_desc * this ) {
676 this->mask.accepted = NULL;
[0cf5b79]677 this->mask.data = NULL;
[daacf82]678 this->mask.size = 0;
679}
680
[0c78741]681static inline thread_desc * next_thread( monitor_desc * this ) {
682 //Check the signaller stack
[169d944]683 __cfaabi_dbg_print_safe( "Kernel : mon %p AS-stack top %p\n", this, this->signal_stack.top);
[8fc45b7]684 __condition_criterion_t * urgent = pop( this->signal_stack );
[0c78741]685 if( urgent ) {
686 //The signaller stack is not empty,
687 //regardless of if we are ready to baton pass,
688 //we need to set the monitor as in use
689 set_owner( this, urgent->owner->waiting_thread );
690
691 return check_condition( urgent );
692 }
693
694 // No signaller thread
695 // Get the next thread in the entry_queue
[8fc45b7]696 thread_desc * new_owner = pop_head( this->entry_queue );
[0c78741]697 set_owner( this, new_owner );
698
699 return new_owner;
700}
701
[6ff4507]702static inline bool is_accepted( monitor_desc * this, const __monitor_group_t & group ) {
[0cf5b79]703 __acceptable_t * it = this->mask.data; // Optim
[59a0bde]704 __lock_size_t count = this->mask.size;
[6ff4507]705
706 // Check if there are any acceptable functions
[a843067]707 if( !it ) return false;
[6ff4507]708
709 // If this isn't the first monitor to test this, there is no reason to repeat the test.
710 if( this != group[0] ) return group[0]->mask.accepted >= 0;
711
712 // For all acceptable functions check if this is the current function.
[59a0bde]713 for( __lock_size_t i = 0; i < count; i++, it++ ) {
[6ff4507]714 if( *it == group ) {
715 *this->mask.accepted = i;
716 return true;
717 }
718 }
719
720 // No function matched
721 return false;
722}
723
[513daec]724static inline void init( __lock_size_t count, monitor_desc * monitors [], __condition_node_t & waiter, __condition_criterion_t criteria [] ) {
725 for( __lock_size_t i = 0; i < count; i++) {
[6b224a52]726 (criteria[i]){ monitors[i], waiter };
[97e3296]727 }
728
[8fc45b7]729 waiter.criteria = criteria;
[97e3296]730}
731
[513daec]732static inline void init_push( __lock_size_t count, monitor_desc * monitors [], __condition_node_t & waiter, __condition_criterion_t criteria [] ) {
733 for( __lock_size_t i = 0; i < count; i++) {
[6b224a52]734 (criteria[i]){ monitors[i], waiter };
[36982fc]735 __cfaabi_dbg_print_safe( "Kernel : target %p = %p\n", criteria[i].target, &criteria[i] );
[8fc45b7]736 push( criteria[i].target->signal_stack, &criteria[i] );
[97e3296]737 }
738
[8fc45b7]739 waiter.criteria = criteria;
[97e3296]740}
741
[ea7d2b0]742static inline void lock_all( __spinlock_t * locks [], __lock_size_t count ) {
[513daec]743 for( __lock_size_t i = 0; i < count; i++ ) {
[2e9aed4]744 lock( *locks[i] __cfaabi_dbg_ctx2 );
[0c78741]745 }
746}
747
[ea7d2b0]748static inline void lock_all( monitor_desc * source [], __spinlock_t * /*out*/ locks [], __lock_size_t count ) {
[513daec]749 for( __lock_size_t i = 0; i < count; i++ ) {
[ea7d2b0]750 __spinlock_t * l = &source[i]->lock;
[2e9aed4]751 lock( *l __cfaabi_dbg_ctx2 );
[0c78741]752 if(locks) locks[i] = l;
753 }
754}
755
[ea7d2b0]756static inline void unlock_all( __spinlock_t * locks [], __lock_size_t count ) {
[513daec]757 for( __lock_size_t i = 0; i < count; i++ ) {
[ea7d2b0]758 unlock( *locks[i] );
[0c78741]759 }
760}
761
[513daec]762static inline void unlock_all( monitor_desc * locks [], __lock_size_t count ) {
763 for( __lock_size_t i = 0; i < count; i++ ) {
[ea7d2b0]764 unlock( locks[i]->lock );
[0c78741]765 }
766}
767
[8fc45b7]768static inline void save(
769 monitor_desc * ctx [],
[513daec]770 __lock_size_t count,
[ea7d2b0]771 __attribute((unused)) __spinlock_t * locks [],
[8fc45b7]772 unsigned int /*out*/ recursions [],
773 __waitfor_mask_t /*out*/ masks []
774) {
[513daec]775 for( __lock_size_t i = 0; i < count; i++ ) {
[0c78741]776 recursions[i] = ctx[i]->recursion;
[6ff4507]777 masks[i] = ctx[i]->mask;
[0c78741]778 }
779}
780
[8fc45b7]781static inline void restore(
782 monitor_desc * ctx [],
[513daec]783 __lock_size_t count,
[ea7d2b0]784 __spinlock_t * locks [],
[8fc45b7]785 unsigned int /*out*/ recursions [],
786 __waitfor_mask_t /*out*/ masks []
787) {
[6ff4507]788 lock_all( locks, count );
[513daec]789 for( __lock_size_t i = 0; i < count; i++ ) {
[0c78741]790 ctx[i]->recursion = recursions[i];
[6ff4507]791 ctx[i]->mask = masks[i];
[0c78741]792 }
[6ff4507]793 unlock_all( locks, count );
[0c78741]794}
795
796// Function has 2 different behavior
797// 1 - Marks a monitors as being ready to run
798// 2 - Checks if all the monitors are ready to run
799// if so return the thread to run
800static inline thread_desc * check_condition( __condition_criterion_t * target ) {
801 __condition_node_t * node = target->owner;
802 unsigned short count = node->count;
803 __condition_criterion_t * criteria = node->criteria;
804
805 bool ready2run = true;
806
807 for( int i = 0; i < count; i++ ) {
[44264c5]808
[36982fc]809 // __cfaabi_dbg_print_safe( "Checking %p for %p\n", &criteria[i], target );
[0c78741]810 if( &criteria[i] == target ) {
811 criteria[i].ready = true;
[36982fc]812 // __cfaabi_dbg_print_safe( "True\n" );
[0c78741]813 }
814
815 ready2run = criteria[i].ready && ready2run;
816 }
817
[36982fc]818 __cfaabi_dbg_print_safe( "Kernel : Runing %i (%p)\n", ready2run, ready2run ? node->waiting_thread : NULL );
[0c78741]819 return ready2run ? node->waiting_thread : NULL;
820}
821
[4cedd9f]822static inline void brand_condition( condition & this ) {
[b10affd]823 thread_desc * thrd = TL_GET( this_thread );
[4cedd9f]824 if( !this.monitors ) {
[169d944]825 // __cfaabi_dbg_print_safe( "Branding\n" );
[0cf5b79]826 assertf( thrd->monitors.data != NULL, "No current monitor to brand condition %p", thrd->monitors.data );
[4cedd9f]827 this.monitor_count = thrd->monitors.size;
[a933dcf4]828
[cdbfab0]829 this.monitors = (monitor_desc **)malloc( this.monitor_count * sizeof( *this.monitors ) );
[4cedd9f]830 for( int i = 0; i < this.monitor_count; i++ ) {
[0cf5b79]831 this.monitors[i] = thrd->monitors[i];
[a933dcf4]832 }
[0c78741]833 }
834}
835
[59a0bde]836static inline [thread_desc *, int] search_entry_queue( const __waitfor_mask_t & mask, monitor_desc * monitors [], __lock_size_t count ) {
[90c4df0]837
[0cf5b79]838 __queue_t(thread_desc) & entry_queue = monitors[0]->entry_queue;
[90c4df0]839
840 // For each thread in the entry-queue
[8fc45b7]841 for( thread_desc ** thrd_it = &entry_queue.head;
[90c4df0]842 *thrd_it;
[4cc9b13]843 thrd_it = &(*thrd_it)->next
844 ) {
[90c4df0]845 // For each acceptable check if it matches
[4cc9b13]846 int i = 0;
[0cf5b79]847 __acceptable_t * end = end (mask);
848 __acceptable_t * begin = begin(mask);
849 for( __acceptable_t * it = begin; it != end; it++, i++ ) {
[90c4df0]850 // Check if we have a match
[aaa4f93]851 if( *it == (*thrd_it)->monitors ) {
[90c4df0]852
853 // If we have a match return it
854 // after removeing it from the entry queue
[b18830e]855 return [remove( entry_queue, thrd_it ), i];
[90c4df0]856 }
857 }
858 }
859
[b18830e]860 return [0, -1];
861}
862
[6ff4507]863forall(dtype T | sized( T ))
[59a0bde]864static inline __lock_size_t insert_unique( T * array [], __lock_size_t & size, T * val ) {
[6ff4507]865 if( !val ) return size;
866
[59a0bde]867 for( __lock_size_t i = 0; i <= size; i++) {
[6ff4507]868 if( array[i] == val ) return size;
869 }
870
871 array[size] = val;
872 size = size + 1;
873 return size;
874}
875
[59a0bde]876static inline __lock_size_t count_max( const __waitfor_mask_t & mask ) {
877 __lock_size_t max = 0;
878 for( __lock_size_t i = 0; i < mask.size; i++ ) {
[0cf5b79]879 __acceptable_t & accepted = mask[i];
880 max += accepted.size;
[b18830e]881 }
882 return max;
[97e3296]883}
[b18830e]884
[59a0bde]885static inline __lock_size_t aggregate( monitor_desc * storage [], const __waitfor_mask_t & mask ) {
886 __lock_size_t size = 0;
887 for( __lock_size_t i = 0; i < mask.size; i++ ) {
[0cf5b79]888 __acceptable_t & accepted = mask[i];
889 __libcfa_small_sort( accepted.data, accepted.size );
890 for( __lock_size_t j = 0; j < accepted.size; j++) {
891 insert_unique( storage, size, accepted[j] );
[6ff4507]892 }
[b18830e]893 }
[6a5be52]894 // TODO insertion sort instead of this
[de737c8]895 __libcfa_small_sort( storage, size );
[6ff4507]896 return size;
[b18830e]897}
898
[6b0b624]899// Local Variables: //
900// mode: c //
901// tab-width: 4 //
902// End: //
Note: See TracBrowser for help on using the repository browser.