source: libcfa/src/concurrency/monitor.cfa @ 50ff1d0

ADTast-experimentalpthread-emulation
Last change on this file since 50ff1d0 was c18bf9e, checked in by Thierry Delisle <tdelisle@…>, 2 years ago

Visibility concurrency

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