Ignore:
Timestamp:
Nov 2, 2017, 4:01:28 PM (4 years ago)
Author:
Thierry Delisle <tdelisle@…>
Branches:
aaron-thesis, arm-eh, cleanup-dtors, deferred_resn, demangler, jacob/cs343-translation, jenkins-sandbox, master, new-ast, new-ast-unique-expr, new-env, no_list, persistent-indexer, resolv-new, with_gc
Children:
025278e
Parents:
4cedd9f
Message:

Internal helper functions now use arrays and references instead of pointers

File:
1 edited

Legend:

Unmodified
Added
Removed
  • src/libcfa/concurrency/monitor.c

    r4cedd9f r8fc45b7  
    2626// Forward declarations
    2727static inline void set_owner ( monitor_desc * this, thread_desc * owner );
    28 static inline void set_owner ( monitor_desc ** storage, short count, thread_desc * owner );
    29 static inline void set_mask  ( monitor_desc ** storage, short count, const __waitfor_mask_t & mask );
     28static inline void set_owner ( monitor_desc * storage [], short count, thread_desc * owner );
     29static inline void set_mask  ( monitor_desc * storage [], short count, const __waitfor_mask_t & mask );
    3030static inline void reset_mask( monitor_desc * this );
    3131
     
    3333static inline bool is_accepted( monitor_desc * this, const __monitor_group_t & monitors );
    3434
    35 static inline void lock_all( spinlock ** locks, unsigned short count );
    36 static inline void lock_all( monitor_desc ** source, spinlock ** /*out*/ locks, unsigned short count );
    37 static inline void unlock_all( spinlock ** locks, unsigned short count );
    38 static inline void unlock_all( monitor_desc ** locks, unsigned short count );
    39 
    40 static inline void save   ( monitor_desc ** ctx, short count, spinlock ** locks, unsigned int * /*out*/ recursions, __waitfor_mask_t * /*out*/ masks );
    41 static inline void restore( monitor_desc ** ctx, short count, spinlock ** locks, unsigned int * /*in */ recursions, __waitfor_mask_t * /*in */ masks );
    42 
    43 static inline void init     ( int count, monitor_desc ** monitors, __condition_node_t * waiter, __condition_criterion_t * criteria );
    44 static inline void init_push( int count, monitor_desc ** monitors, __condition_node_t * waiter, __condition_criterion_t * criteria );
     35static inline void lock_all  ( spinlock * locks [], unsigned short count );
     36static inline void lock_all  ( monitor_desc * source [], spinlock * /*out*/ locks [], unsigned short count );
     37static inline void unlock_all( spinlock * locks [], unsigned short count );
     38static inline void unlock_all( monitor_desc * locks [], unsigned short count );
     39
     40static inline void save   ( monitor_desc * ctx [], short count, spinlock * locks [], unsigned int /*out*/ recursions [], __waitfor_mask_t /*out*/ masks [] );
     41static inline void restore( monitor_desc * ctx [], short count, spinlock * locks [], unsigned int /*in */ recursions [], __waitfor_mask_t /*in */ masks [] );
     42
     43static inline void init     ( int count, monitor_desc * monitors [], __condition_node_t & waiter, __condition_criterion_t criteria [] );
     44static inline void init_push( int count, monitor_desc * monitors [], __condition_node_t & waiter, __condition_criterion_t criteria [] );
    4545
    4646static inline thread_desc *        check_condition   ( __condition_criterion_t * );
    4747static inline void                 brand_condition   ( condition & );
    48 static inline [thread_desc *, int] search_entry_queue( const __waitfor_mask_t &, monitor_desc ** monitors, int count );
     48static inline [thread_desc *, int] search_entry_queue( const __waitfor_mask_t &, monitor_desc * monitors [], int count );
    4949
    5050forall(dtype T | sized( T ))
    51 static inline short insert_unique( T ** array, short & size, T * val );
     51static inline short insert_unique( T * array [], short & size, T * val );
    5252static inline short count_max    ( const __waitfor_mask_t & mask );
    53 static inline short aggregate    ( monitor_desc ** storage, const __waitfor_mask_t & mask );
     53static inline short aggregate    ( monitor_desc * storage [], const __waitfor_mask_t & mask );
    5454
    5555//-----------------------------------------------------------------------------
     
    5858        __condition_node_t waiter = { thrd, count, user_info };   /* Create the node specific to this wait operation                                     */ \
    5959        __condition_criterion_t criteria[count];                  /* Create the creteria this wait operation needs to wake up                            */ \
    60         init( count, monitors, &waiter, criteria );               /* Link everything together                                                            */ \
     60        init( count, monitors, waiter, criteria );                /* Link everything together                                                            */ \
    6161
    6262#define wait_ctx_primed(thrd, user_info)                        /* Create the necessary information to use the signaller stack                         */ \
    6363        __condition_node_t waiter = { thrd, count, user_info };   /* Create the node specific to this wait operation                                     */ \
    6464        __condition_criterion_t criteria[count];                  /* Create the creteria this wait operation needs to wake up                            */ \
    65         init_push( count, monitors, &waiter, criteria );          /* Link everything together and push it to the AS-Stack                                */ \
     65        init_push( count, monitors, waiter, criteria );           /* Link everything together and push it to the AS-Stack                                */ \
    6666
    6767#define monitor_ctx( mons, cnt )                                /* Define that create the necessary struct for internal/external scheduling operations */ \
     
    114114
    115115                        // Some one else has the monitor, wait in line for it
    116                         append( &this->entry_queue, thrd );
     116                        append( this->entry_queue, thrd );
    117117                        BlockInternal( &this->lock );
    118118
     
    160160
    161161                        // Wake the thread that is waiting for this
    162                         __condition_criterion_t * urgent = pop( &this->signal_stack );
     162                        __condition_criterion_t * urgent = pop( this->signal_stack );
    163163                        verify( urgent );
    164164
     
    182182
    183183                        // Some one else has the monitor, wait in line for it
    184                         append( &this->entry_queue, thrd );
     184                        append( this->entry_queue, thrd );
    185185                        BlockInternal( &this->lock );
    186186
     
    279279// Leave multiple monitor
    280280// relies on the monitor array being sorted
    281 static inline void leave(monitor_desc ** monitors, int count) {
     281static inline void leave(monitor_desc * monitors [], int count) {
    282282        for(int i = count - 1; i >= 0; i--) {
    283283                __leave_monitor_desc( monitors[i] );
     
    287287// Ctor for monitor guard
    288288// Sorts monitors before entering
    289 void ?{}( monitor_guard_t & this, monitor_desc ** m, int count, fptr_t func ) {
     289void ?{}( monitor_guard_t & this, monitor_desc * m [], int count, fptr_t func ) {
    290290        // Store current array
    291291        this.m = m;
     
    333333// Ctor for monitor guard
    334334// Sorts monitors before entering
    335 void ?{}( monitor_dtor_guard_t & this, monitor_desc ** m, fptr_t func ) {
     335void ?{}( monitor_dtor_guard_t & this, monitor_desc * m [], fptr_t func ) {
    336336        // Store current array
    337337        this.m = *m;
     
    378378}
    379379
    380 void ?{}(__condition_criterion_t & this, monitor_desc * target, __condition_node_t * owner ) {
     380void ?{}(__condition_criterion_t & this, monitor_desc * target, __condition_node_t & owner ) {
    381381        this.ready  = false;
    382382        this.target = target;
    383         this.owner  = owner;
     383        this.owner  = &owner;
    384384        this.next   = NULL;
    385385}
     
    403403        // Append the current wait operation to the ones already queued on the condition
    404404        // We don't need locks for that since conditions must always be waited on inside monitor mutual exclusion
    405         append( &this.blocked, &waiter );
     405        append( this.blocked, &waiter );
    406406
    407407        // Lock all monitors (aggregates the locks as well)
     
    456456
    457457        //Pop the head of the waiting queue
    458         __condition_node_t * node = pop_head( &this.blocked );
     458        __condition_node_t * node = pop_head( this.blocked );
    459459
    460460        //Add the thread to the proper AS stack
     
    462462                __condition_criterion_t * crit = &node->criteria[i];
    463463                assert( !crit->ready );
    464                 push( &crit->target->signal_stack, crit );
     464                push( crit->target->signal_stack, crit );
    465465        }
    466466
     
    491491
    492492        //Find the thread to run
    493         thread_desc * signallee = pop_head( &this.blocked )->waiting_thread;
     493        thread_desc * signallee = pop_head( this.blocked )->waiting_thread;
    494494        set_owner( monitors, count, signallee );
    495495
     
    569569
    570570                                __condition_criterion_t * dtor_crit = mon2dtor->dtor_node->criteria;
    571                                 push( &mon2dtor->signal_stack, dtor_crit );
     571                                push( mon2dtor->signal_stack, dtor_crit );
    572572
    573573                                unlock_all( locks, count );
     
    661661}
    662662
    663 static inline void set_owner( monitor_desc ** monitors, short count, thread_desc * owner ) {
     663static inline void set_owner( monitor_desc * monitors [], short count, thread_desc * owner ) {
    664664        monitors[0]->owner     = owner;
    665665        monitors[0]->recursion = 1;
     
    670670}
    671671
    672 static inline void set_mask( monitor_desc ** storage, short count, const __waitfor_mask_t & mask ) {
     672static inline void set_mask( monitor_desc * storage [], short count, const __waitfor_mask_t & mask ) {
    673673        for(int i = 0; i < count; i++) {
    674674                storage[i]->mask = mask;
     
    685685        //Check the signaller stack
    686686        LIB_DEBUG_PRINT_SAFE("Kernel :  mon %p AS-stack top %p\n", this, this->signal_stack.top);
    687         __condition_criterion_t * urgent = pop( &this->signal_stack );
     687        __condition_criterion_t * urgent = pop( this->signal_stack );
    688688        if( urgent ) {
    689689                //The signaller stack is not empty,
     
    697697        // No signaller thread
    698698        // Get the next thread in the entry_queue
    699         thread_desc * new_owner = pop_head( &this->entry_queue );
     699        thread_desc * new_owner = pop_head( this->entry_queue );
    700700        set_owner( this, new_owner );
    701701
     
    725725}
    726726
    727 static inline void init( int count, monitor_desc ** monitors, __condition_node_t * waiter, __condition_criterion_t * criteria ) {
     727static inline void init( int count, monitor_desc * monitors [], __condition_node_t & waiter, __condition_criterion_t criteria [] ) {
    728728        for(int i = 0; i < count; i++) {
    729729                (criteria[i]){ monitors[i], waiter };
    730730        }
    731731
    732         waiter->criteria = criteria;
    733 }
    734 
    735 static inline void init_push( int count, monitor_desc ** monitors, __condition_node_t * waiter, __condition_criterion_t * criteria ) {
     732        waiter.criteria = criteria;
     733}
     734
     735static inline void init_push( int count, monitor_desc * monitors [], __condition_node_t & waiter, __condition_criterion_t criteria [] ) {
    736736        for(int i = 0; i < count; i++) {
    737737                (criteria[i]){ monitors[i], waiter };
    738738                LIB_DEBUG_PRINT_SAFE( "Kernel :  target %p = %p\n", criteria[i].target, &criteria[i] );
    739                 push( &criteria[i].target->signal_stack, &criteria[i] );
    740         }
    741 
    742         waiter->criteria = criteria;
    743 }
    744 
    745 static inline void lock_all( spinlock ** locks, unsigned short count ) {
     739                push( criteria[i].target->signal_stack, &criteria[i] );
     740        }
     741
     742        waiter.criteria = criteria;
     743}
     744
     745static inline void lock_all( spinlock * locks [], unsigned short count ) {
    746746        for( int i = 0; i < count; i++ ) {
    747747                lock_yield( locks[i] DEBUG_CTX2 );
     
    749749}
    750750
    751 static inline void lock_all( monitor_desc ** source, spinlock ** /*out*/ locks, unsigned short count ) {
     751static inline void lock_all( monitor_desc * source [], spinlock * /*out*/ locks [], unsigned short count ) {
    752752        for( int i = 0; i < count; i++ ) {
    753753                spinlock * l = &source[i]->lock;
     
    757757}
    758758
    759 static inline void unlock_all( spinlock ** locks, unsigned short count ) {
     759static inline void unlock_all( spinlock * locks [], unsigned short count ) {
    760760        for( int i = 0; i < count; i++ ) {
    761761                unlock( locks[i] );
     
    763763}
    764764
    765 static inline void unlock_all( monitor_desc ** locks, unsigned short count ) {
     765static inline void unlock_all( monitor_desc * locks [], unsigned short count ) {
    766766        for( int i = 0; i < count; i++ ) {
    767767                unlock( &locks[i]->lock );
     
    769769}
    770770
    771 static inline void save( monitor_desc ** ctx, short count, __attribute((unused)) spinlock ** locks, unsigned int * /*out*/ recursions, __waitfor_mask_t * /*out*/ masks ) {
     771static inline void save(
     772        monitor_desc * ctx [],
     773        short count,
     774        __attribute((unused)) spinlock * locks [],
     775        unsigned int /*out*/ recursions [],
     776        __waitfor_mask_t /*out*/ masks []
     777) {
    772778        for( int i = 0; i < count; i++ ) {
    773779                recursions[i] = ctx[i]->recursion;
     
    776782}
    777783
    778 static inline void restore( monitor_desc ** ctx, short count, spinlock ** locks, unsigned int * /*out*/ recursions, __waitfor_mask_t * /*out*/ masks ) {
     784static inline void restore(
     785        monitor_desc * ctx [],
     786        short count,
     787        spinlock * locks [],
     788        unsigned int /*out*/ recursions [],
     789        __waitfor_mask_t /*out*/ masks []
     790) {
    779791        lock_all( locks, count );
    780792        for( int i = 0; i < count; i++ ) {
     
    825837}
    826838
    827 static inline [thread_desc *, int] search_entry_queue( const __waitfor_mask_t & mask, monitor_desc ** monitors, int count ) {
    828 
    829         __thread_queue_t * entry_queue = &monitors[0]->entry_queue;
     839static inline [thread_desc *, int] search_entry_queue( const __waitfor_mask_t & mask, monitor_desc * monitors [], int count ) {
     840
     841        __thread_queue_t & entry_queue = monitors[0]->entry_queue;
    830842
    831843        // For each thread in the entry-queue
    832         for(    thread_desc ** thrd_it = &entry_queue->head;
     844        for(    thread_desc ** thrd_it = &entry_queue.head;
    833845                *thrd_it;
    834846                thrd_it = &(*thrd_it)->next
     
    852864
    853865forall(dtype T | sized( T ))
    854 static inline short insert_unique( T ** array, short & size, T * val ) {
     866static inline short insert_unique( T * array [], short & size, T * val ) {
    855867        if( !val ) return size;
    856868
     
    872884}
    873885
    874 static inline short aggregate( monitor_desc ** storage, const __waitfor_mask_t & mask ) {
     886static inline short aggregate( monitor_desc * storage [], const __waitfor_mask_t & mask ) {
    875887        short size = 0;
    876888        for( int i = 0; i < mask.size; i++ ) {
     
    890902}
    891903
    892 void append( __condition_blocked_queue_t * this, __condition_node_t * c ) {
    893         verify(this->tail != NULL);
    894         *this->tail = c;
    895         this->tail = &c->next;
    896 }
    897 
    898 __condition_node_t * pop_head( __condition_blocked_queue_t * this ) {
    899         __condition_node_t * head = this->head;
     904void append( __condition_blocked_queue_t & this, __condition_node_t * c ) {
     905        verify(this.tail != NULL);
     906        *this.tail = c;
     907        this.tail = &c->next;
     908}
     909
     910__condition_node_t * pop_head( __condition_blocked_queue_t & this ) {
     911        __condition_node_t * head = this.head;
    900912        if( head ) {
    901                 this->head = head->next;
     913                this.head = head->next;
    902914                if( !head->next ) {
    903                         this->tail = &this->head;
     915                        this.tail = &this.head;
    904916                }
    905917                head->next = NULL;
Note: See TracChangeset for help on using the changeset viewer.