source: libcfa/src/concurrency/monitor.cfa@ 9b01ed1

Last change on this file since 9b01ed1 was e25ef8c, checked in by Peter A. Buhr <pabuhr@…>, 10 months ago

formatting, first attempt to add priority order to waitfor selection, change to for-loops to use CFA for-control

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