source: libcfa/src/concurrency/monitor.cfa@ 8c50aed

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 8c50aed was b0c7419, checked in by Thierry Delisle <tdelisle@…>, 6 years ago

Yield now uses force_yield instead of park/unpark.
Final ctxswitch of a thread now uses ad-hoc mechanism instead of park/unpark.

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