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

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

Split thread_leave so backend is called from the kernel once the kernel no longer needs the thread.
This hopefully solves the non-deterministic crashes in the build.

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