source: libcfa/src/concurrency/monitor.cfa@ 9082e0f1

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 9082e0f1 was 3ea8ad1, checked in by Thierry Delisle <tdelisle@…>, 5 years ago

Added more checks for thread termination synchronization

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