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

Last change on this file since bb9897c was e426c6f, checked in by Peter A. Buhr <pabuhr@…>, 2 weeks ago

fix signal_block on empty condition queue

  • Property mode set to 100644
File size: 35.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//
[e84ab3d]7// monitor.cfa --
[f07e037]8//
9// Author : Thierry Delisle
10// Created On : Thd Feb 23 12:27:26 2017
[6b33e89]11// Last Modified By : Peter A. Buhr
[e426c6f]12// Last Modified On : Thu Mar 26 22:37:42 2026
13// Update Count : 91
[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
[6b33e89]80#define monitor_save save ( monitors, count, locks, recursions, masks )
[6ff4507]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 );
[6b33e89]97 } else if ( ! this->owner ) {
[c7a900a]98 // No one has the monitor, just take it
99 __set_owner( this, thrd );
[f07e037]100
[6b33e89]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
[6b33e89]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
[6b33e89]114 __cfaabi_dbg_print_safe( "Kernel : mon accepts \n" );
[e25ef8c]115 } else {
[6b33e89]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
[6b33e89]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
[6b33e89]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
[6b33e89]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 );
[6b33e89]161 /* paranoid */ verify( ! is_thrd || thrd->state == Halted || thrd->state == Cancelled );
[549c006]162
[c7a900a]163 unlock( this->lock );
164 return;
[6b33e89]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 );
[6b33e89]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
[6b33e89]188 /* paranoid */ verify( ! is_thrd || this->owner != thrd || (thrd->state != Halted && thrd->state != Cancelled) );
[3ea8ad1]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) ) {
[6b33e89]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 {
[6b33e89]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) {
[6b33e89]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
[6b33e89]266 /* paranoid */ verifyf( ! new_owner || new_owner == this->owner, "Expected owner to be %p, got %p (m: %p)", new_owner, this->owner, this );
[c7a900a]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
[6b33e89]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 }
[6b33e89]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
[6b33e89]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 ) {
[6b33e89]426 ready = false;
[121be3e]427 target = 0p;
[6b33e89]428 owner = 0p;
429 this.next = 0p;
[be3d020]430}
431
[c18bf9e]432static void ?{}(__condition_criterion_t & this, monitor$ * target, __condition_node_t & owner ) {
[6b33e89]433 this.ready = false;
[242a902]434 this.target = target;
[6b33e89]435 this.owner = &owner;
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
[e426c6f]495bool signal( condition & this ) libcfa_public with( this ) {
496 if ( empty( this ) ) { return false; }
[5ea06d6]497
[e426c6f]498 verifyf( monitors != 0p, "Waiting with no monitors (%p)", monitors );
499 verifyf( monitor_count != 0, "Waiting with 0 monitors (%"PRIiFAST16")", monitor_count );
[0c78741]500
[e426c6f]501 __cfaabi_dbg_debug_do( // more checking in debug
[e84ab3d]502 thread$ * this_thrd = active_thread();
[e426c6f]503 if ( monitor_count != this_thrd->monitors.size ) {
504 abort( "Signal on condition %p made with different number of monitor(s), expected %zi got %zi",
505 &this, monitor_count, this_thrd->monitors.size );
506 } // if
507
508 for ( i; monitor_count ) {
509 if ( monitors[i] != this_thrd->monitors[i] ) {
510 abort( "Signal on condition %p made with different monitor, expected %p got %p",
511 &this, monitors[i], this_thrd->monitors[i] );
512 } // if
513 } // for
[5ea06d6]514 );
515
[e426c6f]516 __lock_size_t count = monitor_count;
517 lock_all( monitors, 0p, count ); // lock all monitors
518 __condition_node_t * node = pop_head( blocked ); // pop head of waiting queue
[97e3296]519
[e426c6f]520 for ( i; count ) { // add threads to the proper AS stack
[0c78741]521 __condition_criterion_t * crit = &node->criteria[i];
[6b33e89]522 assert( ! crit->ready );
[8fc45b7]523 push( crit->target->signal_stack, crit );
[e426c6f]524 } // for
[be3d020]525
[e426c6f]526 unlock_all( monitors, count ); // release
[be3d020]527 return true;
[e426c6f]528} // signal
[5ea06d6]529
[c18bf9e]530bool signal_block( condition & this ) libcfa_public {
[e426c6f]531 if ( empty( this ) ) { return false; }
[44264c5]532
[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
[e25ef8c]537 monitor_ctx( this.monitors, this.monitor_count ); // creates monitors, count, recursions, masks, locks
[44264c5]538
[97e3296]539 // Lock all monitors (aggregates the locks them as well)
540 lock_all( monitors, locks, count );
[44264c5]541
[97e3296]542 // Create the node specific to this wait operation
[8fc652e0]543 wait_ctx_primed( active_thread(), 0 )
[44264c5]544
[e426c6f]545 monitor_save; // save contexts
[44264c5]546
547 //Find the thread to run
[e84ab3d]548 thread$ * signallee = pop_head( this.blocked )->waiting_thread;
[c7a900a]549 __set_owner( monitors, count, signallee );
[44264c5]550
[36982fc]551 __cfaabi_dbg_print_buffer_decl( "Kernel : signal_block condition %p (s: %p)\n", &this, signallee );
[de737c8]552
[e426c6f]553 unlock_all( locks, count ); // unlock all the monitors
554 unpark( signallee ); // unpark the thread we signalled
555 park(); // everything is ready to go to sleep
[3381ed7]556
[e426c6f]557 // WOKE UP
[c81ebf9]558
[6b33e89]559 __cfaabi_dbg_print_buffer_local( "Kernel : signal_block returned\n" );
[de737c8]560
[e426c6f]561 monitor_restore; // restore the masks and recursions
[be3d020]562
563 return true;
[e426c6f]564} // signal_block
[be3d020]565
[97e3296]566// Access the user_info of the thread waiting at the front of the queue
[c18bf9e]567uintptr_t front( condition & this ) libcfa_public {
[b6de35e]568 verifyf( ! empty( this ),
[4aa2fb2]569 "Attempt to access user data on an empty condition.\n"
570 "Possible cause is not checking if the condition is empty before reading stored data."
[be3d020]571 );
[0cf5b79]572 return ((typeof(this.blocked.head))this.blocked.head)->user_info;
[44264c5]573}
574
[c81ebf9]575//-----------------------------------------------------------------------------
[b18830e]576// External scheduling
577// cases to handle :
578// - target already there :
579// block and wake
580// - dtor already there
581// put thread on signaller stack
582// - non-blocking
583// return else
584// - timeout
585// return timeout
586// - block
587// setup mask
588// block
[c18bf9e]589void __waitfor_internal( const __waitfor_mask_t & mask, int duration ) libcfa_public {
[b18830e]590 // This statment doesn't have a contiguous list of monitors...
591 // Create one!
[59a0bde]592 __lock_size_t max = count_max( mask );
[e84ab3d]593 monitor$ * mon_storage[max];
[66298de]594 __builtin_memset( mon_storage, 0, sizeof( mon_storage ) );
[59a0bde]595 __lock_size_t actual_count = aggregate( mon_storage, mask );
[97e3296]596
[523232d]597 __cfaabi_dbg_print_buffer_decl( "Kernel : waitfor %"PRIdFAST16" (s: %"PRIdFAST16", m: %"PRIdFAST16")\n", actual_count, mask.size, (__lock_size_t)max);
[66298de]598
[e25ef8c]599 if (actual_count == 0) return;
[19c43b7]600
[169d944]601 __cfaabi_dbg_print_buffer_local( "Kernel : waitfor internal proceeding\n" );
[4cc9b13]602
[97e3296]603 // Create storage for monitor context
[e25ef8c]604 monitor_ctx( mon_storage, actual_count ); // creates monitors, count, recursions, masks, locks
[c81ebf9]605
[6ff4507]606 // Lock all monitors (aggregates the locks as well)
[97e3296]607 lock_all( monitors, locks, count );
[c81ebf9]608
[b18830e]609 {
610 // Check if the entry queue
[6b33e89]611 thread$ * nxt; int index;
612 [nxt, index] = search_entry_queue( mask, monitors, count );
[b18830e]613
[6b33e89]614 if ( nxt ) {
[19c43b7]615 *mask.accepted = index;
[0cf5b79]616 __acceptable_t& accepted = mask[index];
[e25ef8c]617 if ( accepted.is_dtor ) {
[169d944]618 __cfaabi_dbg_print_buffer_local( "Kernel : dtor already there\n" );
[6b33e89]619 verifyf( accepted.size == 1, "ERROR: Accepted dtor has more than 1 mutex parameter." );
[549c006]620
[e84ab3d]621 monitor$ * mon2dtor = accepted[0];
[549c006]622 verifyf( mon2dtor->dtor_node, "ERROR: Accepted monitor has no dtor_node." );
623
624 __condition_criterion_t * dtor_crit = mon2dtor->dtor_node->criteria;
[8fc45b7]625 push( mon2dtor->signal_stack, dtor_crit );
[549c006]626
627 unlock_all( locks, count );
[b18830e]628 }
629 else {
[169d944]630 __cfaabi_dbg_print_buffer_local( "Kernel : thread present, baton-passing\n" );
[19c43b7]631
632 // Create the node specific to this wait operation
[8fc652e0]633 wait_ctx_primed( active_thread(), 0 );
[19c43b7]634
635 // Save monitor states
636 monitor_save;
637
[6b33e89]638 __cfaabi_dbg_print_buffer_local( "Kernel : baton of %"PRIdFAST16" monitors : ", count );
[6a5be52]639 #ifdef __CFA_DEBUG_PRINT__
[e25ef8c]640 for ( i; count ) {
[36982fc]641 __cfaabi_dbg_print_buffer_local( "%p %p ", monitors[i], monitors[i]->signal_stack.top );
[6a5be52]642 }
643 #endif
[169d944]644 __cfaabi_dbg_print_buffer_local( "\n" );
[6a5be52]645
[19c43b7]646 // Set the owners to be the next thread
[6b33e89]647 __set_owner( monitors, count, nxt );
[19c43b7]648
[3381ed7]649 // unlock all the monitors
650 unlock_all( locks, count );
[19c43b7]651
[3381ed7]652 // unpark the thread we signalled
[6b33e89]653 unpark( nxt );
[3381ed7]654
655 //Everything is ready to go to sleep
[e235429]656 park();
[19c43b7]657
658 // We are back, restore the owners and recursions
659 monitor_restore;
660
[169d944]661 __cfaabi_dbg_print_buffer_local( "Kernel : thread present, returned\n" );
[b18830e]662 }
663
[36982fc]664 __cfaabi_dbg_print_buffer_local( "Kernel : accepted %d\n", *mask.accepted);
[19c43b7]665 return;
[90c4df0]666 }
667 }
668
[c81ebf9]669
[e25ef8c]670 if ( duration == 0 ) {
[169d944]671 __cfaabi_dbg_print_buffer_local( "Kernel : non-blocking, exiting\n" );
[19c43b7]672
[4cc9b13]673 unlock_all( locks, count );
[19c43b7]674
[36982fc]675 __cfaabi_dbg_print_buffer_local( "Kernel : accepted %d\n", *mask.accepted);
[4cc9b13]676 return;
677 }
[b18830e]678
679
[169d944]680 verifyf( duration < 0, "Timeout on waitfor statments not supported yet." );
[b18830e]681
[169d944]682 __cfaabi_dbg_print_buffer_local( "Kernel : blocking waitfor\n" );
[19c43b7]683
684 // Create the node specific to this wait operation
[8fc652e0]685 wait_ctx_primed( active_thread(), 0 );
[b18830e]686
[6ff4507]687 monitor_save;
[6ae8c92]688 set_mask( monitors, count, mask );
[c81ebf9]689
[e25ef8c]690 for ( i; count ) {
[8fc652e0]691 verify( monitors[i]->owner == active_thread() );
[daacf82]692 }
693
[3381ed7]694 // unlock all the monitors
695 unlock_all( locks, count );
696
[19c43b7]697 //Everything is ready to go to sleep
[e235429]698 park();
[19c43b7]699
700
701 // WE WOKE UP
702
703
704 //We are back, restore the masks and recursions
705 monitor_restore;
706
[169d944]707 __cfaabi_dbg_print_buffer_local( "Kernel : exiting\n" );
[19c43b7]708
[36982fc]709 __cfaabi_dbg_print_buffer_local( "Kernel : accepted %d\n", *mask.accepted);
[c81ebf9]710}
711
[0c78741]712//-----------------------------------------------------------------------------
713// Utilities
714
[e84ab3d]715static inline void __set_owner( monitor$ * this, thread$ * owner ) {
[3381ed7]716 /* paranoid */ verify( this->lock.lock );
[a843067]717
[0c78741]718 //Pass the monitor appropriately
719 this->owner = owner;
720
721 //We are passing the monitor to someone else, which means recursion level is not 0
722 this->recursion = owner ? 1 : 0;
723}
724
[e84ab3d]725static inline void __set_owner( monitor$ * monitors [], __lock_size_t count, thread$ * owner ) {
[3381ed7]726 /* paranoid */ verify ( monitors[0]->lock.lock );
[8fc652e0]727 /* 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] );
[6b33e89]728 monitors[0]->owner = owner;
729 monitors[0]->recursion = 1;
[e25ef8c]730 for ( i; 1~count ) {
[3381ed7]731 /* paranoid */ verify ( monitors[i]->lock.lock );
[8fc652e0]732 /* 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] );
[6b33e89]733 monitors[i]->owner = owner;
734 monitors[i]->recursion = 0;
[6ff4507]735 }
736}
737
[e84ab3d]738static inline void set_mask( monitor$ * storage [], __lock_size_t count, const __waitfor_mask_t & mask ) {
[e25ef8c]739 for ( i; count) {
[6ff4507]740 storage[i]->mask = mask;
741 }
742}
743
[e84ab3d]744static inline void reset_mask( monitor$ * this ) {
[121be3e]745 this->mask.accepted = 0p;
746 this->mask.data = 0p;
[daacf82]747 this->mask.size = 0;
748}
749
[e84ab3d]750static inline thread$ * next_thread( monitor$ * this ) {
[0c78741]751 //Check the signaller stack
[6b33e89]752 __cfaabi_dbg_print_safe( "Kernel : mon %p AS-stack top %p\n", this, this->signal_stack.top);
[8fc45b7]753 __condition_criterion_t * urgent = pop( this->signal_stack );
[e25ef8c]754 if ( urgent ) {
[0c78741]755 //The signaller stack is not empty,
756 //regardless of if we are ready to baton pass,
757 //we need to set the monitor as in use
[6b33e89]758 /* 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 );
759 __set_owner( this, urgent->owner->waiting_thread );
[0c78741]760
761 return check_condition( urgent );
762 }
763
764 // No signaller thread
765 // Get the next thread in the entry_queue
[e84ab3d]766 thread$ * new_owner = pop_head( this->entry_queue );
[6b33e89]767 /* 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 );
768 /* paranoid */ verify( ! new_owner || new_owner->user_link.next == 0p );
[c7a900a]769 __set_owner( this, new_owner );
[0c78741]770
771 return new_owner;
772}
773
[e84ab3d]774static inline bool is_accepted( monitor$ * this, const __monitor_group_t & group ) {
[0cf5b79]775 __acceptable_t * it = this->mask.data; // Optim
[59a0bde]776 __lock_size_t count = this->mask.size;
[6ff4507]777
778 // Check if there are any acceptable functions
[6b33e89]779 if ( ! it ) return false;
[6ff4507]780
781 // If this isn't the first monitor to test this, there is no reason to repeat the test.
[e25ef8c]782 if ( this != group[0] ) return group[0]->mask.accepted >= 0;
[6ff4507]783
784 // For all acceptable functions check if this is the current function.
[e25ef8c]785 for ( __lock_size_t i = 0; i < count; i++, it++ ) {
786 if ( *it == group ) {
[6ff4507]787 *this->mask.accepted = i;
788 return true;
789 }
790 }
791
792 // No function matched
793 return false;
794}
795
[e84ab3d]796static inline void init( __lock_size_t count, monitor$ * monitors [], __condition_node_t & waiter, __condition_criterion_t criteria [] ) {
[e25ef8c]797 for ( i; count ) {
[6b224a52]798 (criteria[i]){ monitors[i], waiter };
[97e3296]799 }
800
[8fc45b7]801 waiter.criteria = criteria;
[97e3296]802}
803
[e84ab3d]804static inline void init_push( __lock_size_t count, monitor$ * monitors [], __condition_node_t & waiter, __condition_criterion_t criteria [] ) {
[e25ef8c]805 for ( i; count ) {
[6b224a52]806 (criteria[i]){ monitors[i], waiter };
[6b33e89]807 __cfaabi_dbg_print_safe( "Kernel : target %p = %p\n", criteria[i].target, &criteria[i] );
[8fc45b7]808 push( criteria[i].target->signal_stack, &criteria[i] );
[97e3296]809 }
810
[8fc45b7]811 waiter.criteria = criteria;
[97e3296]812}
813
[ea7d2b0]814static inline void lock_all( __spinlock_t * locks [], __lock_size_t count ) {
[e25ef8c]815 for ( i; count ) {
[2e9aed4]816 lock( *locks[i] __cfaabi_dbg_ctx2 );
[0c78741]817 }
818}
819
[e84ab3d]820static inline void lock_all( monitor$ * source [], __spinlock_t * /*out*/ locks [], __lock_size_t count ) {
[e25ef8c]821 for ( i; count ) {
[ea7d2b0]822 __spinlock_t * l = &source[i]->lock;
[2e9aed4]823 lock( *l __cfaabi_dbg_ctx2 );
[e25ef8c]824 if (locks) locks[i] = l;
[0c78741]825 }
826}
827
[ea7d2b0]828static inline void unlock_all( __spinlock_t * locks [], __lock_size_t count ) {
[e25ef8c]829 for ( i; count ) {
[ea7d2b0]830 unlock( *locks[i] );
[0c78741]831 }
832}
833
[e84ab3d]834static inline void unlock_all( monitor$ * locks [], __lock_size_t count ) {
[e25ef8c]835 for ( i; count ) {
[ea7d2b0]836 unlock( locks[i]->lock );
[0c78741]837 }
838}
839
[8fc45b7]840static inline void save(
[e84ab3d]841 monitor$ * ctx [],
[513daec]842 __lock_size_t count,
[ea7d2b0]843 __attribute((unused)) __spinlock_t * locks [],
[8fc45b7]844 unsigned int /*out*/ recursions [],
845 __waitfor_mask_t /*out*/ masks []
846) {
[e25ef8c]847 for ( i; count ) {
[0c78741]848 recursions[i] = ctx[i]->recursion;
[e25ef8c]849 masks[i] = ctx[i]->mask;
[0c78741]850 }
851}
852
[8fc45b7]853static inline void restore(
[e84ab3d]854 monitor$ * ctx [],
[513daec]855 __lock_size_t count,
[ea7d2b0]856 __spinlock_t * locks [],
[8fc45b7]857 unsigned int /*out*/ recursions [],
858 __waitfor_mask_t /*out*/ masks []
859) {
[6ff4507]860 lock_all( locks, count );
[e25ef8c]861 for ( i; count ) {
[0c78741]862 ctx[i]->recursion = recursions[i];
[e25ef8c]863 ctx[i]->mask = masks[i];
[0c78741]864 }
[6ff4507]865 unlock_all( locks, count );
[0c78741]866}
867
868// Function has 2 different behavior
869// 1 - Marks a monitors as being ready to run
870// 2 - Checks if all the monitors are ready to run
871// if so return the thread to run
[e84ab3d]872static inline thread$ * check_condition( __condition_criterion_t * target ) {
[0c78741]873 __condition_node_t * node = target->owner;
874 unsigned short count = node->count;
875 __condition_criterion_t * criteria = node->criteria;
876
877 bool ready2run = true;
878
[e25ef8c]879 for ( i; count ) {
[36982fc]880 // __cfaabi_dbg_print_safe( "Checking %p for %p\n", &criteria[i], target );
[e25ef8c]881 if ( &criteria[i] == target ) {
[0c78741]882 criteria[i].ready = true;
[36982fc]883 // __cfaabi_dbg_print_safe( "True\n" );
[0c78741]884 }
885
886 ready2run = criteria[i].ready && ready2run;
887 }
888
[6b33e89]889 __cfaabi_dbg_print_safe( "Kernel : Runing %i (%p)\n", ready2run, ready2run ? (thread*)node->waiting_thread : (thread*)0p );
[121be3e]890 return ready2run ? node->waiting_thread : 0p;
[0c78741]891}
892
[4cedd9f]893static inline void brand_condition( condition & this ) {
[e84ab3d]894 thread$ * thrd = active_thread();
[6b33e89]895 if ( ! this.monitors ) {
[169d944]896 // __cfaabi_dbg_print_safe( "Branding\n" );
[121be3e]897 assertf( thrd->monitors.data != 0p, "No current monitor to brand condition %p", thrd->monitors.data );
[4cedd9f]898 this.monitor_count = thrd->monitors.size;
[a933dcf4]899
[e84ab3d]900 this.monitors = (monitor$ **)malloc( this.monitor_count * sizeof( *this.monitors ) );
[e25ef8c]901 for ( i; this.monitor_count ) {
[0cf5b79]902 this.monitors[i] = thrd->monitors[i];
[a933dcf4]903 }
[0c78741]904 }
905}
906
[e84ab3d]907static inline [thread$ *, int] search_entry_queue( const __waitfor_mask_t & mask, monitor$ * monitors [], __lock_size_t count ) {
908 __queue_t(thread$) & entry_queue = monitors[0]->entry_queue;
[1cd2839]909 // For each acceptable (respect lexical priority in waitfor statement)
[f6f7b52]910 int i = 0;
911 __acceptable_t * end = end(mask);
912 __acceptable_t * begin = begin(mask);
913 for ( __acceptable_t * it = begin; it != end; it++, i++ ) {
[1cd2839]914 #if defined( __CFA_WITH_VERIFY__ )
[6b33e89]915 thread$ * prior = 0p;
[1cd2839]916 #endif // __CFA_WITH_VERIFY__
917
918 for ( thread$ ** thrd_it = &entry_queue.head; (*thrd_it) != 1p; thrd_it = &get_next(**thrd_it) ) {
919 thread$ * curr = *thrd_it;
[90c4df0]920
[6b33e89]921 /* paranoid */ verifyf( ! prior || prior->user_link.next == curr, "search not making progress, from %p (%p) to %p",
922 prior, prior->user_link.next, curr );
923 /* paranoid */ verifyf( curr != prior, "search not making progress, from %p to %p", prior, curr );
[1cd2839]924
925 // For each thread in the entry-queue check for a match
926 if ( *it == curr->monitors ) {
[e25ef8c]927 // If match, return it after removing from the entry queue
[1cd2839]928 return [remove( entry_queue, thrd_it ), i];
929 } // if
930
931 #if defined( __CFA_WITH_VERIFY__ )
[6b33e89]932 prior = curr;
[1cd2839]933 #endif
934 } // for
935 } // for
[b18830e]936 return [0, -1];
937}
938
[e25ef8c]939forall( T & | sized( T ) )
[59a0bde]940static inline __lock_size_t insert_unique( T * array [], __lock_size_t & size, T * val ) {
[e25ef8c]941 if ( ! val ) return size;
[6ff4507]942
[e25ef8c]943 for ( __lock_size_t i; ~= size ) {
944 if ( array[i] == val ) return size;
[6ff4507]945 }
946
947 array[size] = val;
[e25ef8c]948 return size += 1;
[6ff4507]949}
950
[59a0bde]951static inline __lock_size_t count_max( const __waitfor_mask_t & mask ) {
952 __lock_size_t max = 0;
[e25ef8c]953 for ( i; mask.size ) {
[0cf5b79]954 __acceptable_t & accepted = mask[i];
955 max += accepted.size;
[b18830e]956 }
957 return max;
[97e3296]958}
[b18830e]959
[e84ab3d]960static inline __lock_size_t aggregate( monitor$ * storage [], const __waitfor_mask_t & mask ) {
[59a0bde]961 __lock_size_t size = 0;
[e25ef8c]962 for ( i; mask.size ) {
[0cf5b79]963 __acceptable_t & accepted = mask[i];
964 __libcfa_small_sort( accepted.data, accepted.size );
[e25ef8c]965 for ( __lock_size_t j = 0; j < accepted.size; j++) {
[0cf5b79]966 insert_unique( storage, size, accepted[j] );
[6ff4507]967 }
[b18830e]968 }
[6a5be52]969 // TODO insertion sort instead of this
[de737c8]970 __libcfa_small_sort( storage, size );
[6ff4507]971 return size;
[b18830e]972}
973
[af67ee1]974//-----------------------------------------------------------------------------
975// Enter routine for mutex stmt
976// Can't be accepted since a mutex stmt is effectively an anonymous routine
977// Thus we do not need a monitor group
[c18bf9e]978void lock( monitor$ * this ) libcfa_public {
[af67ee1]979 thread$ * thrd = active_thread();
980
981 // Lock the monitor spinlock
982 lock( this->lock __cfaabi_dbg_ctx2 );
983
984 __cfaabi_dbg_print_safe( "Kernel : %10p Entering mon %p (%p)\n", thrd, this, this->owner);
985
[e25ef8c]986 if ( unlikely(0 != (0x1 & (uintptr_t)this->owner)) ) {
[af67ee1]987 abort( "Attempt by thread \"%.256s\" (%p) to access joined monitor %p.", thrd->self_cor.name, thrd, this );
[6b33e89]988 } else if ( ! this->owner ) {
[af67ee1]989 // No one has the monitor, just take it
990 __set_owner( this, thrd );
991
[6b33e89]992 __cfaabi_dbg_print_safe( "Kernel : mon is free \n" );
[e25ef8c]993 } else if ( this->owner == thrd) {
[af67ee1]994 // We already have the monitor, just note how many times we took it
995 this->recursion += 1;
996
[6b33e89]997 __cfaabi_dbg_print_safe( "Kernel : mon already owned \n" );
[e25ef8c]998 } else {
[6b33e89]999 __cfaabi_dbg_print_safe( "Kernel : blocking \n" );
[af67ee1]1000
1001 // Some one else has the monitor, wait in line for it
[be5f0a5]1002 /* paranoid */ verify( thrd->user_link.next == 0p );
[af67ee1]1003 append( this->entry_queue, thrd );
[be5f0a5]1004 /* paranoid */ verify( thrd->user_link.next == 1p );
[af67ee1]1005
1006 unlock( this->lock );
1007 park();
1008
[6b33e89]1009 __cfaabi_dbg_print_safe( "Kernel : %10p Entered mon %p\n", thrd, this);
[af67ee1]1010
1011 /* paranoid */ verifyf( active_thread() == this->owner, "Expected owner to be %p, got %p (r: %i, m: %p)", active_thread(), this->owner, this->recursion, this );
1012 return;
1013 }
1014
[6b33e89]1015 __cfaabi_dbg_print_safe( "Kernel : %10p Entered mon %p\n", thrd, this);
[af67ee1]1016
1017 /* paranoid */ verifyf( active_thread() == this->owner, "Expected owner to be %p, got %p (r: %i, m: %p)", active_thread(), this->owner, this->recursion, this );
1018 /* paranoid */ verify( this->lock.lock );
1019
1020 // Release the lock and leave
1021 unlock( this->lock );
1022 return;
1023}
1024
1025// Leave routine for mutex stmt
1026// Is just a wrapper around __leave for the is_lock trait to see
[c18bf9e]1027void unlock( monitor$ * this ) libcfa_public { __leave( this ); }
[af67ee1]1028
[6b0b624]1029// Local Variables: //
1030// mode: c //
1031// tab-width: 4 //
1032// End: //
Note: See TracBrowser for help on using the repository browser.