Changeset 2125443a


Ignore:
Timestamp:
Feb 3, 2023, 1:28:43 PM (14 months ago)
Author:
Peter A. Buhr <pabuhr@…>
Branches:
ADT, ast-experimental, master
Children:
2f61765
Parents:
8a97248 (diff), db9d7a9 (diff)
Note: this is a merge changeset, the changes displayed below correspond to the merge itself.
Use the (diff) links above to see all the changes relative to each parent.
Message:

Merge branch 'master' of plg.uwaterloo.ca:software/cfa/cfa-cc

Files:
6 added
7 edited

Legend:

Unmodified
Added
Removed
  • libcfa/src/concurrency/actor.hfa

    r8a97248 r2125443a  
     1#pragma once
     2
    13#include <locks.hfa>
    24#include <limits.hfa>
    35#include <list.hfa>
     6#include <kernel.hfa>
    47
    58#ifdef __CFA_DEBUG__
     
    2124// Define if executor is created in a separate cluster
    2225#define __DEFAULT_EXECUTOR_SEPCLUS__ false
     26
     27// when you flip this make sure to recompile compiler and flip the appropriate flag there too in Actors.cpp
     28#define __ALLOC 0
    2329
    2430// forward decls
     
    3844P9_EMBEDDED( request, dlink(request) )
    3945
    40 void ?{}( request & this ) { this.stop = true; } // default ctor makes a sentinel
    41 void ?{}( request & this, actor * receiver, message * msg, __receive_fn fn ) {
     46static inline void ?{}( request & this ) { this.stop = true; } // default ctor makes a sentinel
     47static inline void ?{}( request & this, actor * receiver, message * msg, __receive_fn fn ) {
    4248    this.receiver = receiver;
    4349    this.msg = msg;
     
    4551    this.stop = false;
    4652}
    47 
     53static inline void ?{}( request & this, request & copy ) {
     54    this.receiver = copy.receiver;
     55    this.msg = copy.msg;
     56    this.fn = copy.fn;
     57    this.stop = copy.stop;
     58}
     59
     60// hybrid data structure. Copies until buffer is full and then allocates for intrusive list
     61struct copy_queue {
     62    dlist( request ) list;
     63    #if ! __ALLOC
     64    request * buffer;
     65    size_t count, buffer_size, index;
     66    #endif
     67};
     68static inline void ?{}( copy_queue & this ) {}
     69static inline void ?{}( copy_queue & this, size_t buf_size ) with(this) {
     70    list{};
     71    #if ! __ALLOC
     72    buffer_size = buf_size;
     73    buffer = aalloc( buffer_size );
     74    count = 0;
     75    index = 0;
     76    #endif
     77}
     78static inline void ^?{}( copy_queue & this ) with(this) {
     79    #if ! __ALLOC
     80    adelete(buffer);
     81    #endif
     82}
     83
     84static inline void insert( copy_queue & this, request & elem ) with(this) {
     85    #if ! __ALLOC
     86    if ( count < buffer_size ) { // fast path ( no alloc )
     87        buffer[count]{ elem };
     88        count++;
     89        return;
     90    }
     91    request * new_elem = alloc();
     92    (*new_elem){ elem };
     93    insert_last( list, *new_elem );
     94    #else
     95    insert_last( list, elem );
     96    #endif
     97}
     98
     99// once you start removing you need to remove all elements
     100// it is not supported to call insert() before the list is fully empty
     101// should_delete is an output param
     102static inline request & remove( copy_queue & this, bool & should_delete ) with(this) {
     103    #if ! __ALLOC
     104    if ( count > 0 ) {
     105        count--;
     106        should_delete = false;
     107        size_t old_idx = index;
     108        index = count == 0 ? 0 : index + 1;
     109        return buffer[old_idx];
     110    }
     111    #endif
     112    should_delete = true;
     113    return try_pop_front( list );
     114}
     115
     116static inline bool isEmpty( copy_queue & this ) with(this) {
     117    #if ! __ALLOC
     118    return count == 0 && list`isEmpty;
     119    #else
     120    return list`isEmpty;
     121    #endif
     122}
     123
     124static size_t __buffer_size = 10; // C_TODO: rework this to be passed from executor through ctors (no need for global)
    48125struct work_queue {
    49     futex_mutex mutex_lock;
    50     dlist( request ) input;                                             // unbounded list of work requests
     126    __spinlock_t mutex_lock;
     127    copy_queue owned_queue;
     128    copy_queue * c_queue; // C_TODO: try putting this on the stack with ptr juggling
     129
    51130}; // work_queue
    52 void ?{}( work_queue & this ) with(this) { input{}; mutex_lock{}; }
    53 
    54 void insert( work_queue & this, request & elem ) with(this) {
    55     lock( mutex_lock );
    56     insert_last( input, elem );
     131static inline void ?{}( work_queue & this ) with(this) {
     132    // c_queue = alloc();
     133    // (*c_queue){ __buffer_size };
     134    owned_queue{ __buffer_size };
     135    c_queue = &owned_queue;
     136}
     137// static inline void ^?{}( work_queue & this ) with(this) { delete( c_queue ); }
     138
     139static inline void insert( work_queue & this, request & elem ) with(this) {
     140    lock( mutex_lock __cfaabi_dbg_ctx2 );
     141    insert( *c_queue, elem );
    57142    unlock( mutex_lock );
    58143} // insert
    59144
    60 void transfer( work_queue & this, dlist(request) & transferTo ) with(this) {
    61     lock( mutex_lock );
    62 
    63     //C_TODO CHANGE
    64     // transferTo->transfer( input );              // transfer input to output
    65 
    66     // this is awfully inefficient but Ill use it until transfer is implemented
    67     request * r;
    68     while ( ! input`isEmpty ) {
    69         r = &try_pop_front( input );
    70         if ( r ) insert_last( transferTo, *r );
    71     }
    72 
    73     // transfer( input, transferTo );
    74 
     145static inline void transfer( work_queue & this, copy_queue ** transfer_to ) with(this) {
     146    lock( mutex_lock __cfaabi_dbg_ctx2 );
     147    // swap copy queue ptrs
     148    copy_queue * temp = *transfer_to;
     149    *transfer_to = c_queue;
     150    c_queue = temp;
    75151    unlock( mutex_lock );
    76152} // transfer
    77153
    78154thread worker {
     155    copy_queue owned_queue;
    79156    work_queue * request_queues;
    80     dlist( request ) current_queue;
     157    copy_queue * current_queue;
    81158        request & req;
    82159    unsigned int start, range;
     
    86163    ((thread &)this){ clu };
    87164    this.request_queues = request_queues;
    88     this.current_queue{};
     165    // this.current_queue = alloc();
     166    // (*this.current_queue){ __buffer_size };
     167    this.owned_queue{ __buffer_size };
     168    this.current_queue = &this.owned_queue;
    89169    this.start = start;
    90170    this.range = range;
    91171}
     172// static inline void ^?{}( worker & mutex this ) with(this) { delete( current_queue ); }
    92173
    93174struct executor {
     
    100181}; // executor
    101182
    102 static inline void ?{}( executor & this, unsigned int nprocessors, unsigned int nworkers, unsigned int nrqueues, bool seperate_clus ) with(this) {
     183static inline void ?{}( executor & this, unsigned int nprocessors, unsigned int nworkers, unsigned int nrqueues, bool seperate_clus, size_t buf_size ) with(this) {
    103184    if ( nrqueues < nworkers ) abort( "nrqueues needs to be >= nworkers\n" );
     185    __buffer_size = buf_size;
    104186    this.nprocessors = nprocessors;
    105187    this.nworkers = nworkers;
     
    127209    } // for
    128210}
    129 
     211static inline void ?{}( executor & this, unsigned int nprocessors, unsigned int nworkers, unsigned int nrqueues, bool seperate_clus ) { this{ nprocessors, nworkers, nrqueues, seperate_clus, __buffer_size }; }
    130212static inline void ?{}( executor & this, unsigned int nprocessors, unsigned int nworkers, unsigned int nrqueues ) { this{ nprocessors, nworkers, nrqueues, __DEFAULT_EXECUTOR_SEPCLUS__ }; }
    131213static inline void ?{}( executor & this, unsigned int nprocessors, unsigned int nworkers ) { this{ nprocessors, nworkers, __DEFAULT_EXECUTOR_RQUEUES__ }; }
     
    147229    } // for
    148230
    149     delete( workers );
    150     delete( request_queues );
    151     delete( processors );
     231    adelete( workers );
     232    adelete( request_queues );
     233    adelete( processors );
    152234    if ( seperate_clus ) delete( cluster );
    153235}
     
    170252};
    171253
    172 void ?{}( actor & this ) {
     254static inline void ?{}( actor & this ) {
    173255    // Once an actor is allocated it must be sent a message or the actor system cannot stop. Hence, its receive
    174256    // member must be called to end it
     
    178260    __atomic_fetch_add( &__num_actors_, 1, __ATOMIC_SEQ_CST );
    179261}
    180 void ^?{}( actor & this ) {}
     262static inline void ^?{}( actor & this ) {}
    181263
    182264static inline void check_actor( actor & this ) {
     
    204286};
    205287
    206 void ?{}( message & this ) { this.allocation_ = Nodelete; }
    207 void ?{}( message & this, Allocation allocation ) { this.allocation_ = allocation; }
    208 void ^?{}( message & this ) {}
     288static inline void ?{}( message & this ) { this.allocation_ = Nodelete; }
     289static inline void ?{}( message & this, Allocation allocation ) { this.allocation_ = allocation; }
     290static inline void ^?{}( message & this ) {}
    209291
    210292static inline void check_message( message & this ) {
     
    217299}
    218300
    219 void deliver_request( request & this ) {
     301static inline void deliver_request( request & this ) {
    220302    Allocation actor_allocation = this.fn( *this.receiver, *this.msg );
    221303    this.receiver->allocation_ = actor_allocation;
     
    225307
    226308void main( worker & this ) with(this) {
     309    bool should_delete;
    227310    Exit:
    228311    for ( unsigned int i = 0;; i = (i + 1) % range ) { // cycle through set of request buffers
    229         transfer( request_queues[i + start], current_queue );
    230         while ( ! current_queue`isEmpty ) {
    231             &req = &try_pop_front( current_queue );
     312        // C_TODO: potentially check queue count instead of immediately trying to transfer
     313        transfer( request_queues[i + start], &current_queue );
     314        while ( ! isEmpty( *current_queue ) ) {
     315            &req = &remove( *current_queue, should_delete );
    232316            if ( !&req ) continue; // possibly add some work stealing/idle sleep here
    233317            if ( req.stop ) break Exit;
    234318            deliver_request( req );
    235319
    236             delete( &req );
     320            if ( should_delete ) delete( &req );
    237321        } // while
    238322    } // for
     
    250334    __actor_executor_thd = active_thread();
    251335    __actor_executor_ = alloc();
    252     (*__actor_executor_){ 0, num_thds, num_thds * 16 };
     336    (*__actor_executor_){ 0, num_thds, num_thds == 1 ? 1 : num_thds * 16 };
    253337}
    254338
  • src/Common/ScopedMap.h

    r8a97248 r2125443a  
    3737                template<typename N>
    3838                Scope(N && n) : map(), note(std::forward<N>(n)) {}
    39                
     39
    4040                Scope() = default;
    4141                Scope(const Scope &) = default;
     
    4646        typedef std::vector< Scope > ScopeList;
    4747
    48         ScopeList scopes; ///< scoped list of maps
     48        /// Scoped list of maps.
     49        ScopeList scopes;
    4950public:
    5051        typedef typename MapType::key_type key_type;
     
    5859        typedef typename MapType::const_pointer const_pointer;
    5960
    60         class iterator : public std::iterator< std::bidirectional_iterator_tag, value_type > {
    61         friend class ScopedMap;
    62         friend class const_iterator;
    63                 typedef typename ScopedMap::MapType::iterator wrapped_iterator;
    64                 typedef typename ScopedMap::ScopeList scope_list;
    65                 typedef typename scope_list::size_type size_type;
    66 
    67                 /// Checks if this iterator points to a valid item
    68                 bool is_valid() const {
    69                         return it != (*scopes)[level].map.end();
    70                 }
    71 
    72                 /// Increments on invalid
    73                 iterator & next_valid() {
    74                         if ( ! is_valid() ) { ++(*this); }
    75                         return *this;
    76                 }
    77 
    78                 /// Decrements on invalid
    79                 iterator & prev_valid() {
    80                         if ( ! is_valid() ) { --(*this); }
    81                         return *this;
    82                 }
    83 
    84                 iterator(scope_list & _scopes, const wrapped_iterator & _it, size_type inLevel)
    85                         : scopes(&_scopes), it(_it), level(inLevel) {}
    86         public:
    87                 iterator(const iterator & that) : scopes(that.scopes), it(that.it), level(that.level) {}
    88                 iterator & operator= (const iterator & that) {
    89                         scopes = that.scopes; level = that.level; it = that.it;
    90                         return *this;
    91                 }
    92 
    93                 reference operator* () { return *it; }
    94                 pointer operator-> () const { return it.operator->(); }
    95 
    96                 iterator & operator++ () {
    97                         if ( it == (*scopes)[level].map.end() ) {
    98                                 if ( level == 0 ) return *this;
    99                                 --level;
    100                                 it = (*scopes)[level].map.begin();
    101                         } else {
    102                                 ++it;
    103                         }
    104                         return next_valid();
    105                 }
    106                 iterator operator++ (int) { iterator tmp = *this; ++(*this); return tmp; }
    107 
    108                 iterator & operator-- () {
    109                         // may fail if this is the begin iterator; allowed by STL spec
    110                         if ( it == (*scopes)[level].map.begin() ) {
    111                                 ++level;
    112                                 it = (*scopes)[level].map.end();
    113                         }
    114                         --it;
    115                         return prev_valid();
    116                 }
    117                 iterator operator-- (int) { iterator tmp = *this; --(*this); return tmp; }
    118 
    119                 bool operator== (const iterator & that) const {
    120                         return scopes == that.scopes && level == that.level && it == that.it;
    121                 }
    122                 bool operator!= (const iterator & that) const { return !( *this == that ); }
    123 
    124                 size_type get_level() const { return level; }
    125 
    126                 Note & get_note() { return (*scopes)[level].note; }
    127                 const Note & get_note() const { return (*scopes)[level].note; }
    128 
    129         private:
    130                 scope_list *scopes;
    131                 wrapped_iterator it;
    132                 size_type level;
    133         };
    134 
    135         class const_iterator : public std::iterator< std::bidirectional_iterator_tag,
    136                                                      value_type > {
    137         friend class ScopedMap;
    138                 typedef typename ScopedMap::MapType::iterator wrapped_iterator;
    139                 typedef typename ScopedMap::MapType::const_iterator wrapped_const_iterator;
    140                 typedef typename ScopedMap::ScopeList scope_list;
    141                 typedef typename scope_list::size_type size_type;
    142 
    143                 /// Checks if this iterator points to a valid item
    144                 bool is_valid() const {
    145                         return it != (*scopes)[level].map.end();
    146                 }
    147 
    148                 /// Increments on invalid
    149                 const_iterator & next_valid() {
    150                         if ( ! is_valid() ) { ++(*this); }
    151                         return *this;
    152                 }
    153 
    154                 /// Decrements on invalid
    155                 const_iterator & prev_valid() {
    156                         if ( ! is_valid() ) { --(*this); }
    157                         return *this;
    158                 }
    159 
    160                 const_iterator(scope_list const & _scopes, const wrapped_const_iterator & _it, size_type inLevel)
    161                         : scopes(&_scopes), it(_it), level(inLevel) {}
    162         public:
    163                 const_iterator(const iterator & that) : scopes(that.scopes), it(that.it), level(that.level) {}
    164                 const_iterator(const const_iterator & that) : scopes(that.scopes), it(that.it), level(that.level) {}
    165                 const_iterator & operator= (const iterator & that) {
    166                         scopes = that.scopes; level = that.level; it = that.it;
    167                         return *this;
    168                 }
    169                 const_iterator & operator= (const const_iterator & that) {
    170                         scopes = that.scopes; level = that.level; it = that.it;
    171                         return *this;
    172                 }
    173 
    174                 const_reference operator* () { return *it; }
    175                 const_pointer operator-> () { return it.operator->(); }
    176 
    177                 const_iterator & operator++ () {
    178                         if ( it == (*scopes)[level].map.end() ) {
    179                                 if ( level == 0 ) return *this;
    180                                 --level;
    181                                 it = (*scopes)[level].map.begin();
    182                         } else {
    183                                 ++it;
    184                         }
    185                         return next_valid();
    186                 }
    187                 const_iterator operator++ (int) { const_iterator tmp = *this; ++(*this); return tmp; }
    188 
    189                 const_iterator & operator-- () {
    190                         // may fail if this is the begin iterator; allowed by STL spec
    191                         if ( it == (*scopes)[level].map.begin() ) {
    192                                 ++level;
    193                                 it = (*scopes)[level].map.end();
    194                         }
    195                         --it;
    196                         return prev_valid();
    197                 }
    198                 const_iterator operator-- (int) { const_iterator tmp = *this; --(*this); return tmp; }
    199 
    200                 bool operator== (const const_iterator & that) const {
    201                         return scopes == that.scopes && level == that.level && it == that.it;
    202                 }
    203                 bool operator!= (const const_iterator & that) const { return !( *this == that ); }
    204 
    205                 size_type get_level() const { return level; }
    206 
    207                 const Note & get_note() const { return (*scopes)[level].note; }
    208 
    209         private:
    210                 scope_list const *scopes;
    211                 wrapped_const_iterator it;
    212                 size_type level;
    213         };
     61        // Both iterator types are complete bidrectional iterators, see below.
     62        class iterator;
     63        class const_iterator;
    21464
    21565        /// Starts a new scope
     
    297147        }
    298148
    299         template< typename value_type_t >
    300         std::pair< iterator, bool > insert( iterator at, value_type_t && value ) {
    301                 MapType & scope = (*at.scopes)[ at.level ].map;
    302                 std::pair< typename MapType::iterator, bool > res = scope.insert( std::forward<value_type_t>( value ) );
    303                 return std::make_pair( iterator(scopes, std::move( res.first ), at.level), std::move( res.second ) );
    304         }
    305 
    306149        template< typename value_t >
    307150        std::pair< iterator, bool > insert( const Key & key, value_t && value ) { return insert( std::make_pair( key, std::forward<value_t>( value ) ) ); }
     
    324167        }
    325168
    326         iterator erase( iterator pos ) {
    327                 MapType & scope = (*pos.scopes)[ pos.level ].map;
    328                 const typename iterator::wrapped_iterator & new_it = scope.erase( pos.it );
    329                 iterator it( *pos.scopes, new_it, pos.level );
    330                 return it.next_valid();
    331         }
     169        iterator erase( iterator pos );
    332170
    333171        size_type count( const Key & key ) const {
     
    344182        }
    345183};
     184
     185template<typename Key, typename Value, typename Note>
     186class ScopedMap<Key, Value, Note>::iterator :
     187                public std::iterator< std::bidirectional_iterator_tag, value_type > {
     188        friend class ScopedMap;
     189        friend class const_iterator;
     190        typedef typename ScopedMap::MapType::iterator wrapped_iterator;
     191        typedef typename ScopedMap::ScopeList scope_list;
     192        typedef typename scope_list::size_type size_type;
     193
     194        /// Checks if this iterator points to a valid item
     195        bool is_valid() const {
     196                return it != (*scopes)[level].map.end();
     197        }
     198
     199        /// Increments on invalid
     200        iterator & next_valid() {
     201                if ( ! is_valid() ) { ++(*this); }
     202                return *this;
     203        }
     204
     205        /// Decrements on invalid
     206        iterator & prev_valid() {
     207                if ( ! is_valid() ) { --(*this); }
     208                return *this;
     209        }
     210
     211        iterator(scope_list & _scopes, const wrapped_iterator & _it, size_type inLevel)
     212                : scopes(&_scopes), it(_it), level(inLevel) {}
     213public:
     214        iterator(const iterator & that) : scopes(that.scopes), it(that.it), level(that.level) {}
     215        iterator & operator= (const iterator & that) {
     216                scopes = that.scopes; level = that.level; it = that.it;
     217                return *this;
     218        }
     219
     220        reference operator* () { return *it; }
     221        pointer operator-> () const { return it.operator->(); }
     222
     223        iterator & operator++ () {
     224                if ( it == (*scopes)[level].map.end() ) {
     225                        if ( level == 0 ) return *this;
     226                        --level;
     227                        it = (*scopes)[level].map.begin();
     228                } else {
     229                        ++it;
     230                }
     231                return next_valid();
     232        }
     233        iterator operator++ (int) { iterator tmp = *this; ++(*this); return tmp; }
     234
     235        iterator & operator-- () {
     236                // may fail if this is the begin iterator; allowed by STL spec
     237                if ( it == (*scopes)[level].map.begin() ) {
     238                        ++level;
     239                        it = (*scopes)[level].map.end();
     240                }
     241                --it;
     242                return prev_valid();
     243        }
     244        iterator operator-- (int) { iterator tmp = *this; --(*this); return tmp; }
     245
     246        bool operator== (const iterator & that) const {
     247                return scopes == that.scopes && level == that.level && it == that.it;
     248        }
     249        bool operator!= (const iterator & that) const { return !( *this == that ); }
     250
     251        size_type get_level() const { return level; }
     252
     253        Note & get_note() { return (*scopes)[level].note; }
     254        const Note & get_note() const { return (*scopes)[level].note; }
     255
     256private:
     257        scope_list *scopes;
     258        wrapped_iterator it;
     259        size_type level;
     260};
     261
     262template<typename Key, typename Value, typename Note>
     263class ScopedMap<Key, Value, Note>::const_iterator :
     264                public std::iterator< std::bidirectional_iterator_tag, value_type > {
     265        friend class ScopedMap;
     266        typedef typename ScopedMap::MapType::iterator wrapped_iterator;
     267        typedef typename ScopedMap::MapType::const_iterator wrapped_const_iterator;
     268        typedef typename ScopedMap::ScopeList scope_list;
     269        typedef typename scope_list::size_type size_type;
     270
     271        /// Checks if this iterator points to a valid item
     272        bool is_valid() const {
     273                return it != (*scopes)[level].map.end();
     274        }
     275
     276        /// Increments on invalid
     277        const_iterator & next_valid() {
     278                if ( ! is_valid() ) { ++(*this); }
     279                return *this;
     280        }
     281
     282        /// Decrements on invalid
     283        const_iterator & prev_valid() {
     284                if ( ! is_valid() ) { --(*this); }
     285                return *this;
     286        }
     287
     288        const_iterator(scope_list const & _scopes, const wrapped_const_iterator & _it, size_type inLevel)
     289                : scopes(&_scopes), it(_it), level(inLevel) {}
     290public:
     291        const_iterator(const iterator & that) : scopes(that.scopes), it(that.it), level(that.level) {}
     292        const_iterator(const const_iterator & that) : scopes(that.scopes), it(that.it), level(that.level) {}
     293        const_iterator & operator= (const iterator & that) {
     294                scopes = that.scopes; level = that.level; it = that.it;
     295                return *this;
     296        }
     297        const_iterator & operator= (const const_iterator & that) {
     298                scopes = that.scopes; level = that.level; it = that.it;
     299                return *this;
     300        }
     301
     302        const_reference operator* () { return *it; }
     303        const_pointer operator-> () { return it.operator->(); }
     304
     305        const_iterator & operator++ () {
     306                if ( it == (*scopes)[level].map.end() ) {
     307                        if ( level == 0 ) return *this;
     308                        --level;
     309                        it = (*scopes)[level].map.begin();
     310                } else {
     311                        ++it;
     312                }
     313                return next_valid();
     314        }
     315        const_iterator operator++ (int) { const_iterator tmp = *this; ++(*this); return tmp; }
     316
     317        const_iterator & operator-- () {
     318                // may fail if this is the begin iterator; allowed by STL spec
     319                if ( it == (*scopes)[level].map.begin() ) {
     320                        ++level;
     321                        it = (*scopes)[level].map.end();
     322                }
     323                --it;
     324                return prev_valid();
     325        }
     326        const_iterator operator-- (int) { const_iterator tmp = *this; --(*this); return tmp; }
     327
     328        bool operator== (const const_iterator & that) const {
     329                return scopes == that.scopes && level == that.level && it == that.it;
     330        }
     331        bool operator!= (const const_iterator & that) const { return !( *this == that ); }
     332
     333        size_type get_level() const { return level; }
     334
     335        const Note & get_note() const { return (*scopes)[level].note; }
     336
     337private:
     338        scope_list const *scopes;
     339        wrapped_const_iterator it;
     340        size_type level;
     341};
     342
     343template<typename Key, typename Value, typename Note>
     344typename ScopedMap<Key, Value, Note>::iterator
     345                ScopedMap<Key, Value, Note>::erase( iterator pos ) {
     346        MapType & scope = (*pos.scopes)[ pos.level ].map;
     347        const typename iterator::wrapped_iterator & new_it = scope.erase( pos.it );
     348        iterator it( *pos.scopes, new_it, pos.level );
     349        return it.next_valid();
     350}
    346351
    347352// Local Variables: //
  • src/Concurrency/Actors.cpp

    r8a97248 r2125443a  
    2121#include "AST/TranslationUnit.hpp"
    2222#include "AST/Expr.hpp"
     23#include <algorithm>
    2324using namespace ast;
     25using namespace std;
    2426
    2527namespace Concurrency {
    2628
    2729struct CollectactorStructDecls : public ast::WithGuards {
    28     std::map<const StructDecl *, int> & actorStructDecls;
    29     std::map<const StructDecl *, int>  & messageStructDecls;
     30    unordered_set<const StructDecl *> & actorStructDecls;
     31    unordered_set<const StructDecl *>  & messageStructDecls;
    3032    const StructDecl ** requestDecl;
    3133    const EnumDecl ** allocationDecl;
     
    3436    StructDecl * parentDecl;
    3537    bool insideStruct = false;
    36 
     38    bool namedDecl = false;
     39
     40    // finds and sets a ptr to the Allocation enum, which is needed in the next pass
    3741    void previsit( const EnumDecl * decl ) {
    3842        if( decl->name == "Allocation" ) *allocationDecl = decl;
    3943    }
    4044
     45    // finds and sets a ptr to the actor, message, and request structs, which are needed in the next pass
    4146    void previsit( const StructDecl * decl ) {
    4247        GuardValue(insideStruct);
     
    4550        if( decl->name == "actor" ) *actorDecl = decl;
    4651        if( decl->name == "message" ) *msgDecl = decl;
    47         if( decl->name == "request" ) *requestDecl = decl;       
     52        if( decl->name == "request" ) *requestDecl = decl;
    4853        }
    4954
     55    // this catches structs of the form:
     56    //     struct dummy_actor { actor a; };
     57    // since they should be:
     58    //     struct dummy_actor { inline actor; };
     59    void previsit ( const ObjectDecl * decl ) {
     60        if ( insideStruct && ! decl->name.empty() ) {
     61            GuardValue(namedDecl);
     62            namedDecl = true;
     63        }
     64    }
     65
     66    // this collects the valid actor and message struct decl pts
    5067    void postvisit( const StructInstType * node ) {
    5168        if ( ! *actorDecl || ! *msgDecl ) return;
    52         if ( insideStruct ) {
     69        if ( insideStruct && !namedDecl ) {
    5370            if ( node->aggr() == *actorDecl ) {
    54                 actorStructDecls.insert({parentDecl, 1});
     71                actorStructDecls.insert( parentDecl );
    5572            } else if ( node->aggr() == *msgDecl ) {
    56                 messageStructDecls.insert({parentDecl, 1});
     73                messageStructDecls.insert( parentDecl );
    5774            }
    5875        }
     
    6077
    6178  public:
    62     CollectactorStructDecls( std::map<const StructDecl *, int> & actorStructDecls, std::map<const StructDecl *, int> & messageStructDecls,
     79    CollectactorStructDecls( unordered_set<const StructDecl *> & actorStructDecls, unordered_set<const StructDecl *> & messageStructDecls,
    6380        const StructDecl ** requestDecl, const EnumDecl ** allocationDecl, const StructDecl ** actorDecl, const StructDecl ** msgDecl )
    6481        : actorStructDecls( actorStructDecls ), messageStructDecls( messageStructDecls ), requestDecl( requestDecl ),
     
    6683};
    6784
     85// keeps track of all fwdDecls of message routines so that we can hoist them to right after the appropriate decls
     86class FwdDeclTable {
     87
     88    // tracks which decls we have seen so that we can hoist the FunctionDecl to the highest point possible
     89    struct FwdDeclData {
     90        const StructDecl * actorDecl;
     91        const StructDecl * msgDecl;
     92        FunctionDecl * fwdDecl;
     93        bool actorFound;
     94        bool msgFound;
     95
     96        bool readyToInsert() { return actorFound && msgFound; }
     97        bool foundActor() { actorFound = true; return readyToInsert(); }
     98        bool foundMsg() { msgFound = true; return readyToInsert(); }
     99
     100        FwdDeclData( const StructDecl * actorDecl, const StructDecl * msgDecl, FunctionDecl * fwdDecl ) :
     101            actorDecl(actorDecl), msgDecl(msgDecl), fwdDecl(fwdDecl), actorFound(false), msgFound(false) {}
     102    };
     103
     104    // map indexed by actor struct ptr
     105    // value is map of all FwdDeclData that contains said actor struct ptr
     106    // inner map is indexed by the message struct ptr of FwdDeclData
     107    unordered_map<const StructDecl *, unordered_map<const StructDecl *, FwdDeclData *>> actorMap;
     108
     109    // this map is the same except the outer map is indexed by message ptr and the inner is indexed by actor ptr
     110    unordered_map<const StructDecl *, unordered_map<const StructDecl *, FwdDeclData *>> msgMap;
     111
     112    void insert( const StructDecl * decl, const StructDecl * otherDecl, unordered_map<const StructDecl *, unordered_map<const StructDecl *, FwdDeclData *>> & map, FwdDeclData * data ) {
     113        auto iter = map.find( decl );
     114        if ( iter != map.end() ) { // if decl exists in map append data to existing inner map
     115            iter->second.emplace( make_pair( otherDecl, data ) );
     116        } else { // else create inner map for key
     117            map.emplace( make_pair( decl, unordered_map<const StructDecl *, FwdDeclData *>( { make_pair( otherDecl, data ) } ) ) );
     118        }
     119    }
     120
     121  public:
     122    // insert decl into table so that we can fwd declare it later (average cost: O(1))
     123    void insertDecl( const StructDecl * actorDecl, const StructDecl * msgDecl, FunctionDecl * fwdDecl ) {
     124        FwdDeclData * declToInsert = new FwdDeclData( actorDecl, msgDecl, fwdDecl );
     125        insert( actorDecl, msgDecl, actorMap, declToInsert );
     126        insert( msgDecl, actorDecl, msgMap, declToInsert );
     127    }
     128
     129    // returns list of decls to insert after current struct decl
     130    // Over the entire pass the runtime of this routine is O(r) where r is the # of receive routines
     131    list<FunctionDecl *> updateDecl( const StructDecl * decl, bool isMsg ) {
     132        unordered_map<const StructDecl *, unordered_map<const StructDecl *, FwdDeclData *>> & map = isMsg ? msgMap : actorMap;
     133        unordered_map<const StructDecl *, unordered_map<const StructDecl *, FwdDeclData *>> & otherMap =  isMsg ? actorMap : msgMap;
     134        auto iter = map.find( decl );
     135        list<FunctionDecl *> toInsertAfter; // this is populated with decls that are ready to insert
     136        if ( iter == map.end() ) return toInsertAfter;
     137       
     138        // iterate over inner map
     139        unordered_map<const StructDecl *, FwdDeclData *> & currInnerMap = iter->second;
     140        for ( auto innerIter = currInnerMap.begin(); innerIter != currInnerMap.end(); ) {
     141            FwdDeclData * currentDatum = innerIter->second;
     142            bool readyToInsert = isMsg ? currentDatum->foundMsg() : currentDatum->foundActor();
     143            if ( ! readyToInsert ) { ++innerIter; continue; }
     144           
     145            // readyToInsert is true so we are good to insert the forward decl of the message fn
     146            toInsertAfter.push_back( currentDatum->fwdDecl );
     147
     148            // need to remove from other map before deleting
     149            // find inner map in other map ( other map is actor map if original is msg map and vice versa )
     150            const StructDecl * otherDecl = isMsg ? currentDatum->actorDecl : currentDatum->msgDecl;
     151            auto otherMapIter = otherMap.find( otherDecl );
     152
     153            unordered_map<const StructDecl *, FwdDeclData *> & otherInnerMap = otherMapIter->second;
     154
     155            // find the FwdDeclData we need to remove in the other inner map
     156            auto otherInnerIter = otherInnerMap.find( decl );
     157
     158            // remove references to deleted FwdDeclData from current inner map
     159            innerIter = currInnerMap.erase( innerIter ); // this does the increment so no explicit inc needed
     160
     161            // remove references to deleted FwdDeclData from other inner map
     162            otherInnerMap.erase( otherInnerIter );
     163           
     164            // if other inner map is now empty, remove key from other outer map
     165            if ( otherInnerMap.empty() )
     166                otherMap.erase( otherDecl );
     167
     168            // now we are safe to delete the FwdDeclData since we are done with it
     169            // and we have removed all references to it from our data structures
     170            delete currentDatum;
     171        }
     172
     173        // if current inner map is now empty, remove key from outer map.
     174        // Have to do this after iterating for safety
     175        if ( currInnerMap.empty() )
     176            map.erase( decl );
     177
     178        return toInsertAfter;
     179    }
     180};
     181
     182#define __ALLOC 0 // C_TODO: complete swap to no-alloc version
     183
    68184struct GenReceiveDecls : public ast::WithDeclsToAdd<> {
    69     std::map<const StructDecl *, int> & actorStructDecls;
    70     std::map<const StructDecl *, int>  & messageStructDecls;
     185    unordered_set<const StructDecl *> & actorStructDecls;
     186    unordered_set<const StructDecl *>  & messageStructDecls;
    71187    const StructDecl ** requestDecl;
    72188    const EnumDecl ** allocationDecl;
    73189    const StructDecl ** actorDecl;
    74190    const StructDecl ** msgDecl;
    75     std::vector<FunctionDecl *> & forwardDecls;
     191    FwdDeclTable & forwardDecls;
    76192
    77193        void postvisit( const FunctionDecl * decl ) {
     
    90206
    91207        // If the struct instances are derived actor and message types then generate the message send routine
    92         if ( actorStructDecls.count( arg1InstType->aggr() ) && messageStructDecls.count( arg2InstType->aggr() ) ) {
     208        auto actorIter = actorStructDecls.find( arg1InstType->aggr() );
     209        auto messageIter = messageStructDecls.find( arg2InstType->aggr() );
     210        if ( actorIter != actorStructDecls.end() && messageIter != messageStructDecls.end() ) {
    93211
    94212            // check that we have found all the decls we need from <actor.hfa>
     
    107225                    return receiver;
    108226                }
    109             */
     227            */ // C_TODO: update this with new no alloc version
    110228            CompoundStmt * sendBody = new CompoundStmt( decl->location );
    111229
     230            #if __ALLOC
    112231            // Generates: request * new_req = alloc();
    113232            sendBody->push_back( new DeclStmt(
     
    120239                )
    121240            ));
     241            #else
     242            // Generates: request new_req;
     243            sendBody->push_back( new DeclStmt(
     244                decl->location,
     245                new ObjectDecl(
     246                    decl->location,
     247                    "new_req",
     248                    new StructInstType( *requestDecl )
     249                )
     250            ));
     251            #endif
    122252           
    123253            // Function type is: Allocation (*)( derived_actor &, derived_msg & )
     
    160290            ));
    161291
     292            #if __ALLOC
    162293            // Generates: (*new_req){ &receiver, &msg, fn };
    163294            sendBody->push_back( new ExprStmt(
     
    189320                                )
    190321                        ));
     322            #else
     323            // Generates: new_req{ &receiver, &msg, fn };
     324            sendBody->push_back( new ExprStmt(
     325                decl->location,
     326                                new UntypedExpr (
     327                    decl->location,
     328                                        new NameExpr( decl->location, "?{}" ),
     329                                        {
     330                                                new NameExpr( decl->location, "new_req" ),
     331                        new AddressExpr( new NameExpr( decl->location, "receiver" ) ),
     332                        new AddressExpr( new NameExpr( decl->location, "msg" ) ),
     333                        new NameExpr( decl->location, "fn" )
     334                                        }
     335                                )
     336                        ));
     337
     338            // Generates: send( receiver, new_req );
     339            sendBody->push_back( new ExprStmt(
     340                decl->location,
     341                                new UntypedExpr (
     342                    decl->location,
     343                                        new NameExpr( decl->location, "send" ),
     344                                        {
     345                                                {
     346                            new NameExpr( decl->location, "receiver" ),
     347                            new NameExpr( decl->location, "new_req" )
     348                        }
     349                                        }
     350                                )
     351                        ));
     352            #endif
    191353           
    192354            // Generates: return receiver;
     
    225387           
    226388            // forward decls to resolve use before decl problem for '|' routines
    227             forwardDecls.push_back( ast::deepCopy( sendOperatorFunction ) );
     389            forwardDecls.insertDecl( *actorIter, *messageIter , ast::deepCopy( sendOperatorFunction ) );
     390            // forwardDecls.push_back( ast::deepCopy( sendOperatorFunction ) );
    228391
    229392            sendOperatorFunction->stmts = sendBody;
     
    233396
    234397  public:
    235     GenReceiveDecls( std::map<const StructDecl *, int> & actorStructDecls, std::map<const StructDecl *, int> & messageStructDecls,
     398    GenReceiveDecls( unordered_set<const StructDecl *> & actorStructDecls, unordered_set<const StructDecl *> & messageStructDecls,
    236399        const StructDecl ** requestDecl, const EnumDecl ** allocationDecl, const StructDecl ** actorDecl, const StructDecl ** msgDecl,
    237         std::vector<FunctionDecl *> & forwardDecls ) : actorStructDecls(actorStructDecls), messageStructDecls(messageStructDecls),
     400        FwdDeclTable & forwardDecls ) : actorStructDecls(actorStructDecls), messageStructDecls(messageStructDecls),
    238401        requestDecl(requestDecl), allocationDecl(allocationDecl), actorDecl(actorDecl), msgDecl(msgDecl), forwardDecls(forwardDecls) {}
    239402};
    240403
    241404struct GenFwdDecls : public ast::WithDeclsToAdd<> {
    242     std::map<const StructDecl *, int> & actorStructDecls;
    243     std::map<const StructDecl *, int>  & messageStructDecls;
    244     std::vector<FunctionDecl *> & forwardDecls;
    245     bool done;
    246 
    247     void postvisit( const FunctionDecl * decl ) {
    248         if ( done ) return;
    249         // return if not of the form receive( param1, param2 ) or if it is a forward decl
    250         if ( decl->name != "receive" || decl->params.size() != 2 || !decl->stmts ) return;
    251 
    252         // the params should be references
    253         const ReferenceType * derivedActorRef = dynamic_cast<const ReferenceType *>(decl->params.at(0)->get_type());
    254         const ReferenceType * derivedMsgRef = dynamic_cast<const ReferenceType *>(decl->params.at(1)->get_type());
    255         if ( !derivedActorRef || !derivedMsgRef ) return;
    256 
    257         // the references should be to struct instances
    258         const StructInstType * arg1InstType = dynamic_cast<const StructInstType *>(derivedActorRef->base.get());
    259         const StructInstType * arg2InstType = dynamic_cast<const StructInstType *>(derivedMsgRef->base.get());
    260         if ( !arg1InstType || !arg2InstType ) return;
    261 
    262         // If the struct instances are derived actor and message types then generate the message send routine
    263         if ( actorStructDecls.count( arg1InstType->aggr() ) && messageStructDecls.count( arg2InstType->aggr() ) ) {
    264             done = true;
    265             for ( const auto & func : forwardDecls ) {
    266                 declsToAddBefore.push_back( func );
    267             }
     405    unordered_set<const StructDecl *> & actorStructDecls;
     406    unordered_set<const StructDecl *>  & messageStructDecls;
     407    FwdDeclTable & forwardDecls;
     408
     409    void postvisit( const StructDecl * decl ) {
     410        list<FunctionDecl *> toAddAfter;
     411        auto actorIter = actorStructDecls.find( decl );
     412        if ( actorIter != actorStructDecls.end() ) { // this is a derived actor decl
     413            // get list of fwd decls that we can now insert
     414            toAddAfter = forwardDecls.updateDecl( decl, false );
     415
     416            // get rid of decl from actorStructDecls since we no longer need it
     417            actorStructDecls.erase( actorIter );
     418        } else {
     419            auto messageIter = messageStructDecls.find( decl );
     420            if ( messageIter == messageStructDecls.end() ) return;
     421
     422            toAddAfter = forwardDecls.updateDecl( decl, true );
     423
     424            // get rid of decl from messageStructDecls since we no longer need it
     425            messageStructDecls.erase( messageIter );
     426        }
     427
     428        // add the fwd decls to declsToAddAfter
     429        for ( FunctionDecl * func : toAddAfter ) {
     430            declsToAddAfter.push_back( func );
    268431        }
    269432    }
    270433
    271434  public:
    272     GenFwdDecls( std::map<const StructDecl *, int> & actorStructDecls, std::map<const StructDecl *, int> & messageStructDecls,
    273         std::vector<FunctionDecl *> & forwardDecls ) : actorStructDecls(actorStructDecls), messageStructDecls(messageStructDecls),
    274         forwardDecls(forwardDecls), done(false) {}
     435    GenFwdDecls( unordered_set<const StructDecl *> & actorStructDecls, unordered_set<const StructDecl *> & messageStructDecls,
     436        FwdDeclTable & forwardDecls ) : actorStructDecls(actorStructDecls), messageStructDecls(messageStructDecls),
     437        forwardDecls(forwardDecls) {}
    275438};
    276439
    277440void implementActors( TranslationUnit & translationUnit ) {
    278     // maps to collect all derived actor and message types
    279     std::map<const StructDecl *, int> actorStructDecls;
    280     std::map<const StructDecl *, int> messageStructDecls;
    281     std::vector<FunctionDecl *> forwardDecls;
     441    // unordered_maps to collect all derived actor and message types
     442    unordered_set<const StructDecl *> actorStructDecls;
     443    unordered_set<const StructDecl *> messageStructDecls;
     444    FwdDeclTable forwardDecls;
    282445
    283446    // for storing through the passes
  • src/GenPoly/ErasableScopedMap.h

    r8a97248 r2125443a  
    5151        typedef typename Scope::const_pointer const_pointer;
    5252
    53         // Both iterator types are complete bidirection iterators, defined below.
     53        // Both iterator types are complete bidirectional iterators, see below.
    5454        class iterator;
    5555        class const_iterator;
  • src/GenPoly/ScopedSet.h

    r8a97248 r2125443a  
    2121
    2222namespace GenPoly {
    23         /// A set where the items are placed into nested scopes;
    24         /// inserted items are placed into the innermost scope, lookup looks from the innermost scope outward
    25         template<typename Value>
    26         class ScopedSet {
    27                 typedef std::set< Value > Scope;
    28                 typedef std::vector< Scope > ScopeList;
    29 
    30                 ScopeList scopes; ///< scoped list of sets
    31         public:
    32                 typedef typename Scope::key_type key_type;
    33                 typedef typename Scope::value_type value_type;
    34                 typedef typename ScopeList::size_type size_type;
    35                 typedef typename ScopeList::difference_type difference_type;
    36                 typedef typename Scope::reference reference;
    37                 typedef typename Scope::const_reference const_reference;
    38                 typedef typename Scope::pointer pointer;
    39                 typedef typename Scope::const_pointer const_pointer;
    40 
    41                 class iterator : public std::iterator< std::bidirectional_iterator_tag,
    42                                                        value_type > {
    43                 friend class ScopedSet;
    44                 friend class const_iterator;
    45                         typedef typename std::set< Value >::iterator wrapped_iterator;
    46                         typedef typename std::vector< std::set< Value > > scope_list;
    47                         typedef typename scope_list::size_type size_type;
    48 
    49                         /// Checks if this iterator points to a valid item
    50                         bool is_valid() const {
    51                                 return it != (*scopes)[i].end();
    52                         }
    53 
    54                         /// Increments on invalid
    55                         iterator& next_valid() {
    56                                 if ( ! is_valid() ) { ++(*this); }
    57                                 return *this;
    58                         }
    59 
    60                         /// Decrements on invalid
    61                         iterator& prev_valid() {
    62                                 if ( ! is_valid() ) { --(*this); }
    63                                 return *this;
    64                         }
    65 
    66                         iterator(scope_list const &_scopes, const wrapped_iterator &_it, size_type _i)
    67                                 : scopes(&_scopes), it(_it), i(_i) {}
    68                 public:
    69                         iterator(const iterator &that) : scopes(that.scopes), it(that.it), i(that.i) {}
    70                         iterator& operator= (const iterator &that) {
    71                                 scopes = that.scopes; i = that.i; it = that.it;
    72                                 return *this;
    73                         }
    74 
    75                         reference operator* () { return *it; }
    76                         pointer operator-> () { return it.operator->(); }
    77 
    78                         iterator& operator++ () {
    79                                 if ( it == (*scopes)[i].end() ) {
    80                                         if ( i == 0 ) return *this;
    81                                         --i;
    82                                         it = (*scopes)[i].begin();
    83                                 } else {
    84                                         ++it;
    85                                 }
    86                                 return next_valid();
    87                         }
    88                         iterator operator++ (int) { iterator tmp = *this; ++(*this); return tmp; }
    89 
    90                         iterator& operator-- () {
    91                                 // may fail if this is the begin iterator; allowed by STL spec
    92                                 if ( it == (*scopes)[i].begin() ) {
    93                                         ++i;
    94                                         it = (*scopes)[i].end();
    95                                 }
    96                                 --it;
    97                                 return prev_valid();
    98                         }
    99                         iterator operator-- (int) { iterator tmp = *this; --(*this); return tmp; }
    100 
    101                         bool operator== (const iterator &that) {
    102                                 return scopes == that.scopes && i == that.i && it == that.it;
    103                         }
    104                         bool operator!= (const iterator &that) { return !( *this == that ); }
    105 
    106                         size_type get_level() const { return i; }
    107 
    108                 private:
    109                         scope_list const *scopes;
    110                         wrapped_iterator it;
    111                         size_type i;
    112                 };
    113 
    114                 class const_iterator : public std::iterator< std::bidirectional_iterator_tag,
    115                                                              value_type > {
    116                 friend class ScopedSet;
    117                         typedef typename std::set< Value >::iterator wrapped_iterator;
    118                         typedef typename std::set< Value >::const_iterator wrapped_const_iterator;
    119                         typedef typename std::vector< std::set< Value > > scope_list;
    120                         typedef typename scope_list::size_type size_type;
    121 
    122                         /// Checks if this iterator points to a valid item
    123                         bool is_valid() const {
    124                                 return it != (*scopes)[i].end();
    125                         }
    126 
    127                         /// Increments on invalid
    128                         const_iterator& next_valid() {
    129                                 if ( ! is_valid() ) { ++(*this); }
    130                                 return *this;
    131                         }
    132 
    133                         /// Decrements on invalid
    134                         const_iterator& prev_valid() {
    135                                 if ( ! is_valid() ) { --(*this); }
    136                                 return *this;
    137                         }
    138 
    139                         const_iterator(scope_list const &_scopes, const wrapped_const_iterator &_it, size_type _i)
    140                                 : scopes(&_scopes), it(_it), i(_i) {}
    141                 public:
    142                         const_iterator(const iterator &that) : scopes(that.scopes), it(that.it), i(that.i) {}
    143                         const_iterator(const const_iterator &that) : scopes(that.scopes), it(that.it), i(that.i) {}
    144                         const_iterator& operator= (const iterator &that) {
    145                                 scopes = that.scopes; i = that.i; it = that.it;
    146                                 return *this;
    147                         }
    148                         const_iterator& operator= (const const_iterator &that) {
    149                                 scopes = that.scopes; i = that.i; it = that.it;
    150                                 return *this;
    151                         }
    152 
    153                         const_reference operator* () { return *it; }
    154                         const_pointer operator-> () { return it.operator->(); }
    155 
    156                         const_iterator& operator++ () {
    157                                 if ( it == (*scopes)[i].end() ) {
    158                                         if ( i == 0 ) return *this;
    159                                         --i;
    160                                         it = (*scopes)[i].begin();
    161                                 } else {
    162                                         ++it;
    163                                 }
    164                                 return next_valid();
    165                         }
    166                         const_iterator operator++ (int) { const_iterator tmp = *this; ++(*this); return tmp; }
    167 
    168                         const_iterator& operator-- () {
    169                                 // may fail if this is the begin iterator; allowed by STL spec
    170                                 if ( it == (*scopes)[i].begin() ) {
    171                                         ++i;
    172                                         it = (*scopes)[i].end();
    173                                 }
    174                                 --it;
    175                                 return prev_valid();
    176                         }
    177                         const_iterator operator-- (int) { const_iterator tmp = *this; --(*this); return tmp; }
    178 
    179                         bool operator== (const const_iterator &that) {
    180                                 return scopes == that.scopes && i == that.i && it == that.it;
    181                         }
    182                         bool operator!= (const const_iterator &that) { return !( *this == that ); }
    183 
    184                         size_type get_level() const { return i; }
    185 
    186                 private:
    187                         scope_list const *scopes;
    188                         wrapped_const_iterator it;
    189                         size_type i;
    190                 };
    191 
    192                 /// Starts a new scope
    193                 void beginScope() {
    194                         Scope scope;
    195                         scopes.push_back(scope);
    196                 }
    197 
    198                 /// Ends a scope; invalidates any iterators pointing to elements of that scope
    199                 void endScope() {
    200                         scopes.pop_back();
    201                 }
    202 
    203                 /// Default constructor initializes with one scope
    204                 ScopedSet() { beginScope(); }
    205 
    206                 iterator begin() { return iterator(scopes, scopes.back().begin(), scopes.size()-1).next_valid(); }
    207                 const_iterator begin() const { return const_iterator(scopes, scopes.back().begin(), scopes.size()-1).next_valid(); }
    208                 const_iterator cbegin() const { return const_iterator(scopes, scopes.back().begin(), scopes.size()-1).next_valid(); }
    209                 iterator end() { return iterator(scopes, scopes[0].end(), 0); }
    210                 const_iterator end() const { return const_iterator(scopes, scopes[0].end(), 0); }
    211                 const_iterator cend() const { return const_iterator(scopes, scopes[0].end(), 0); }
    212 
    213                 /// Gets the index of the current scope (counted from 1)
    214                 size_type currentScope() const { return scopes.size(); }
    215 
    216                 /// Finds the given key in the outermost scope it occurs; returns end() for none such
    217                 iterator find( const Value &key ) {
    218                         for ( size_type i = scopes.size() - 1; ; --i ) {
    219                                 typename Scope::iterator val = scopes[i].find( key );
    220                                 if ( val != scopes[i].end() ) return iterator( scopes, val, i );
    221                                 if ( i == 0 ) break;
    222                         }
    223                         return end();
    224                 }
    225                 const_iterator find( const Value &key ) const {
    226                         return const_iterator( const_cast< ScopedSet< Value >* >(this)->find( key ) );
    227                 }
    228 
    229                 /// Finds the given key in the outermost scope inside the given scope where it occurs
    230                 iterator findNext( const_iterator &it, const Value &key ) {
    231                         if ( it.i == 0 ) return end();
     23
     24/// A set where the items are placed into nested scopes;
     25/// inserted items are placed into the innermost scope, lookup looks from the innermost scope outward
     26template<typename Value>
     27class ScopedSet {
     28        typedef std::set< Value > Scope;
     29        typedef std::vector< Scope > ScopeList;
     30
     31        ScopeList scopes; ///< scoped list of sets
     32public:
     33        typedef typename Scope::key_type key_type;
     34        typedef typename Scope::value_type value_type;
     35        typedef typename ScopeList::size_type size_type;
     36        typedef typename ScopeList::difference_type difference_type;
     37        typedef typename Scope::reference reference;
     38        typedef typename Scope::const_reference const_reference;
     39        typedef typename Scope::pointer pointer;
     40        typedef typename Scope::const_pointer const_pointer;
     41
     42        // Both iterator types are complete bidirectional iterators, see below.
     43        class iterator;
     44        class const_iterator;
     45
     46        /// Starts a new scope
     47        void beginScope() {
     48                Scope scope;
     49                scopes.push_back(scope);
     50        }
     51
     52        /// Ends a scope; invalidates any iterators pointing to elements of that scope
     53        void endScope() {
     54                scopes.pop_back();
     55        }
     56
     57        /// Default constructor initializes with one scope
     58        ScopedSet() { beginScope(); }
     59
     60        iterator begin() { return iterator(scopes, scopes.back().begin(), scopes.size()-1).next_valid(); }
     61        const_iterator begin() const { return const_iterator(scopes, scopes.back().begin(), scopes.size()-1).next_valid(); }
     62        const_iterator cbegin() const { return const_iterator(scopes, scopes.back().begin(), scopes.size()-1).next_valid(); }
     63        iterator end() { return iterator(scopes, scopes[0].end(), 0); }
     64        const_iterator end() const { return const_iterator(scopes, scopes[0].end(), 0); }
     65        const_iterator cend() const { return const_iterator(scopes, scopes[0].end(), 0); }
     66
     67        /// Gets the index of the current scope (counted from 1)
     68        size_type currentScope() const { return scopes.size(); }
     69
     70        /// Finds the given key in the outermost scope it occurs; returns end() for none such
     71        iterator find( const Value &key ) {
     72                for ( size_type i = scopes.size() - 1; ; --i ) {
     73                        typename Scope::iterator val = scopes[i].find( key );
     74                        if ( val != scopes[i].end() ) return iterator( scopes, val, i );
     75                        if ( i == 0 ) break;
     76                }
     77                return end();
     78        }
     79        const_iterator find( const Value &key ) const {
     80                return const_iterator( const_cast< ScopedSet< Value >* >(this)->find( key ) );
     81        }
     82
     83        /// Finds the given key in the outermost scope inside the given scope where it occurs
     84        iterator findNext( const_iterator &it, const Value &key ) {
     85                if ( it.i == 0 ) return end();
    23286                        for ( size_type i = it.i - 1; ; --i ) {
    233                                 typename Scope::iterator val = scopes[i].find( key );
    234                                 if ( val != scopes[i].end() ) return iterator( scopes, val, i );
    235                                 if ( i == 0 ) break;
    236                         }
    237                         return end();
    238                 }
    239                 const_iterator findNext( const_iterator &it, const Value &key ) const {
    240                         return const_iterator( const_cast< ScopedSet< Value >* >(this)->findNext( it, key ) );
    241                 }
    242 
    243                 /// Inserts the given value into the outermost scope
    244                 std::pair< iterator, bool > insert( const value_type &value ) {
    245                         std::pair< typename Scope::iterator, bool > res = scopes.back().insert( value );
    246                         return std::make_pair( iterator(scopes, res.first, scopes.size()-1), res.second );
    247                 }
    248 
    249         };
     87                        typename Scope::iterator val = scopes[i].find( key );
     88                        if ( val != scopes[i].end() ) return iterator( scopes, val, i );
     89                        if ( i == 0 ) break;
     90                }
     91                return end();
     92        }
     93        const_iterator findNext( const_iterator &it, const Value &key ) const {
     94                return const_iterator( const_cast< ScopedSet< Value >* >(this)->findNext( it, key ) );
     95        }
     96
     97        /// Inserts the given value into the outermost scope
     98        std::pair< iterator, bool > insert( const value_type &value ) {
     99                std::pair< typename Scope::iterator, bool > res = scopes.back().insert( value );
     100                return std::make_pair( iterator(scopes, res.first, scopes.size()-1), res.second );
     101        }
     102};
     103
     104template<typename Value>
     105class ScopedSet<Value>::iterator :
     106                public std::iterator< std::bidirectional_iterator_tag, value_type > {
     107        friend class ScopedSet;
     108        friend class const_iterator;
     109        typedef typename std::set< Value >::iterator wrapped_iterator;
     110        typedef typename std::vector< std::set< Value > > scope_list;
     111        typedef typename scope_list::size_type size_type;
     112
     113        /// Checks if this iterator points to a valid item
     114        bool is_valid() const {
     115                return it != (*scopes)[i].end();
     116        }
     117
     118        /// Increments on invalid
     119        iterator& next_valid() {
     120                if ( ! is_valid() ) { ++(*this); }
     121                return *this;
     122        }
     123
     124        /// Decrements on invalid
     125        iterator& prev_valid() {
     126                if ( ! is_valid() ) { --(*this); }
     127                return *this;
     128        }
     129
     130        iterator(scope_list const &_scopes, const wrapped_iterator &_it, size_type _i)
     131                : scopes(&_scopes), it(_it), i(_i) {}
     132public:
     133        iterator(const iterator &that) : scopes(that.scopes), it(that.it), i(that.i) {}
     134        iterator& operator= (const iterator &that) {
     135                scopes = that.scopes; i = that.i; it = that.it;
     136                return *this;
     137        }
     138
     139        reference operator* () { return *it; }
     140        pointer operator-> () { return it.operator->(); }
     141
     142        iterator& operator++ () {
     143                if ( it == (*scopes)[i].end() ) {
     144                        if ( i == 0 ) return *this;
     145                        --i;
     146                        it = (*scopes)[i].begin();
     147                } else {
     148                        ++it;
     149                }
     150                return next_valid();
     151        }
     152        iterator operator++ (int) { iterator tmp = *this; ++(*this); return tmp; }
     153
     154        iterator& operator-- () {
     155                // may fail if this is the begin iterator; allowed by STL spec
     156                if ( it == (*scopes)[i].begin() ) {
     157                        ++i;
     158                        it = (*scopes)[i].end();
     159                }
     160                --it;
     161                return prev_valid();
     162        }
     163        iterator operator-- (int) { iterator tmp = *this; --(*this); return tmp; }
     164
     165        bool operator== (const iterator &that) {
     166                return scopes == that.scopes && i == that.i && it == that.it;
     167        }
     168        bool operator!= (const iterator &that) { return !( *this == that ); }
     169
     170        size_type get_level() const { return i; }
     171
     172private:
     173        scope_list const *scopes;
     174        wrapped_iterator it;
     175        size_type i;
     176};
     177
     178template<typename Value>
     179class ScopedSet<Value>::const_iterator :
     180                public std::iterator< std::bidirectional_iterator_tag, value_type > {
     181        friend class ScopedSet;
     182        typedef typename std::set< Value >::iterator wrapped_iterator;
     183        typedef typename std::set< Value >::const_iterator wrapped_const_iterator;
     184        typedef typename std::vector< std::set< Value > > scope_list;
     185        typedef typename scope_list::size_type size_type;
     186
     187        /// Checks if this iterator points to a valid item
     188        bool is_valid() const {
     189                return it != (*scopes)[i].end();
     190        }
     191
     192        /// Increments on invalid
     193        const_iterator& next_valid() {
     194                if ( ! is_valid() ) { ++(*this); }
     195                return *this;
     196        }
     197
     198        /// Decrements on invalid
     199        const_iterator& prev_valid() {
     200                if ( ! is_valid() ) { --(*this); }
     201                return *this;
     202        }
     203
     204        const_iterator(scope_list const &_scopes, const wrapped_const_iterator &_it, size_type _i)
     205                : scopes(&_scopes), it(_it), i(_i) {}
     206public:
     207        const_iterator(const iterator &that) : scopes(that.scopes), it(that.it), i(that.i) {}
     208        const_iterator(const const_iterator &that) : scopes(that.scopes), it(that.it), i(that.i) {}
     209        const_iterator& operator= (const iterator &that) {
     210                scopes = that.scopes; i = that.i; it = that.it;
     211                return *this;
     212        }
     213        const_iterator& operator= (const const_iterator &that) {
     214                scopes = that.scopes; i = that.i; it = that.it;
     215                return *this;
     216        }
     217
     218        const_reference operator* () { return *it; }
     219        const_pointer operator-> () { return it.operator->(); }
     220
     221        const_iterator& operator++ () {
     222                if ( it == (*scopes)[i].end() ) {
     223                        if ( i == 0 ) return *this;
     224                        --i;
     225                        it = (*scopes)[i].begin();
     226                } else {
     227                        ++it;
     228                }
     229                return next_valid();
     230        }
     231        const_iterator operator++ (int) { const_iterator tmp = *this; ++(*this); return tmp; }
     232
     233        const_iterator& operator-- () {
     234                // may fail if this is the begin iterator; allowed by STL spec
     235                if ( it == (*scopes)[i].begin() ) {
     236                        ++i;
     237                        it = (*scopes)[i].end();
     238                }
     239                --it;
     240                return prev_valid();
     241        }
     242        const_iterator operator-- (int) { const_iterator tmp = *this; --(*this); return tmp; }
     243
     244        bool operator== (const const_iterator &that) {
     245                return scopes == that.scopes && i == that.i && it == that.it;
     246        }
     247        bool operator!= (const const_iterator &that) { return !( *this == that ); }
     248
     249        size_type get_level() const { return i; }
     250
     251private:
     252        scope_list const *scopes;
     253        wrapped_const_iterator it;
     254        size_type i;
     255};
     256
    250257} // namespace GenPoly
    251258
  • tests/concurrent/actors/matrix.cfa

    r8a97248 r2125443a  
    55#include <stdio.h>
    66
    7 unsigned int xr = 100, xc = 100, yc = 100, Processors = 1; // default values
     7unsigned int xr = 500, xc = 500, yc = 500, Processors = 1; // default values
    88
    99struct derived_actor {
  • tests/concurrent/actors/types.cfa

    r8a97248 r2125443a  
    55#include <stdio.h>
    66#include <mutex_stmt.hfa>
     7
     8struct dummy_actor { actor a; }; // this won't work since the actor isn't inlined
    79
    810struct derived_actor {
     
    2628}
    2729
     30Allocation receive( derived_actor & receiver, d_msg & msg ) {
     31    return receive( receiver, msg.num );
     32}
     33
    2834struct derived_actor2 {
     35    struct nested { int i; }; // testing nested before inline
    2936    inline actor;
    3037};
    3138static inline void ?{}( derived_actor2 & this ) { ((actor &)this){}; }
     39
     40Allocation receive( derived_actor2 & receiver, d_msg & msg ) {
     41    mutex(sout) sout | msg.num;
     42    return Finished;
     43}
    3244
    3345struct derived_actor3 {
     
    4153};
    4254static inline void ?{}( d_msg2 & this ) { ((message &)this){}; }
    43 
    44 Allocation receive( derived_actor2 & receiver, d_msg & msg ) {
    45     mutex(sout) sout | msg.num;
    46     return Finished;
    47 }
    48 
    49 Allocation receive( derived_actor & receiver, d_msg & msg ) {
    50     return receive( receiver, msg.num );
    51 }
    5255
    5356Allocation receive( derived_actor3 & receiver, d_msg & msg ) {
Note: See TracChangeset for help on using the changeset viewer.