Changeset 7a2c6b18 for libcfa/src


Ignore:
Timestamp:
Jul 20, 2023, 2:09:15 PM (11 months ago)
Author:
caparsons <caparson@…>
Branches:
master
Children:
0e8f4c6
Parents:
a09552d
Message:

did some cleanup in locks.hfa and fixed seg fault bug in channel waituntil remove()

Location:
libcfa/src/concurrency
Files:
2 edited

Legend:

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

    ra09552d r7a2c6b18  
    6868    #endif
    6969};
     70static inline void ?{}( channel(T) & this, channel(T) this2 ) = void;
     71static inline void ?=?( channel(T) & this, channel(T) this2 ) = void;
    7072
    7173static inline void ?{}( channel(T) &c, size_t _size ) with(c) {
     
    372374// type used by select statement to capture a chan read as the selected operation
    373375struct chan_read {
    374     T & ret;
    375     channel(T) & chan;
     376    T * ret;
     377    channel(T) * chan;
    376378};
    377379__CFA_SELECT_GET_TYPE( chan_read(T) );
    378380
    379 static inline void ?{}( chan_read(T) & cr, channel(T) & chan, T & ret ) {
    380     &cr.chan = &chan;
    381     &cr.ret = &ret;
    382 }
    383 static inline chan_read(T) ?<<?( T & ret, channel(T) & chan ) { chan_read(T) cr{ chan, ret }; return cr; }
    384 
    385 static inline void __handle_select_closed_read( chan_read(T) & this, select_node & node ) with(this.chan, this) {
    386     __closed_remove( chan, ret );
     381static inline void ?{}( chan_read(T) & cr, channel(T) * chan, T * ret ) {
     382    cr.chan = chan;
     383    cr.ret = ret;
     384}
     385static inline chan_read(T) ?<<?( T & ret, channel(T) & chan ) { chan_read(T) cr{ &chan, &ret }; return cr; }
     386
     387static inline void __handle_select_closed_read( chan_read(T) & this, select_node & node ) with(*this.chan, this) {
     388    __closed_remove( *chan, *ret );
    387389    // if we get here then the insert succeeded
    388390    __make_select_node_available( node );
    389391}
    390392
    391 static inline bool register_select( chan_read(T) & this, select_node & node ) with(this.chan, this) {
    392     lock( mutex_lock );
    393     node.extra = &ret; // set .extra so that if it == 0p later in on_selected it is due to channel close
     393static inline bool register_select( chan_read(T) & this, select_node & node ) with(*this.chan, this) {
     394    lock( mutex_lock );
     395    node.extra = ret; // set .extra so that if it == 0p later in on_selected it is due to channel close
    394396
    395397    #ifdef CHAN_STATS
     
    406408
    407409            if ( __handle_pending( prods, node ) ) {
    408                 __prods_handoff( chan, ret );
     410                __prods_handoff( *chan, *ret );
    409411                __make_select_node_sat( node ); // need to to mark SAT now that we know operation is done or else threads could get stuck in __mark_select_node
    410412                unlock( mutex_lock );
     
    432434    ZeroSize: if ( size == 0 && !prods`isEmpty ) {
    433435        if ( !__handle_waituntil_OR( prods ) ) break ZeroSize;
    434         __prods_handoff( chan, ret );
     436        __prods_handoff( *chan, *ret );
    435437        __set_avail_then_unlock( node, mutex_lock );
    436438        return true;
     
    449451
    450452    // Remove from buffer
    451     __do_remove( chan, ret );
     453    __do_remove( *chan, *ret );
    452454    __set_avail_then_unlock( node, mutex_lock );
    453455    return true;
    454456}
    455 static inline bool unregister_select( chan_read(T) & this, select_node & node ) { return unregister_chan( this.chan, node ); }
     457static inline bool unregister_select( chan_read(T) & this, select_node & node ) { return unregister_chan( *this.chan, node ); }
    456458static inline bool on_selected( chan_read(T) & this, select_node & node ) with(this) {
    457459    if ( unlikely(node.extra == 0p) ) {
    458         if ( !exception_in_flight() ) __closed_remove( chan, ret ); // check if woken up due to closed channel
     460        if ( !exception_in_flight() ) __closed_remove( *chan, *ret ); // check if woken up due to closed channel
    459461        else return false;
    460462    }
     
    465467// type used by select statement to capture a chan read as the selected operation that doesn't have a param to read to
    466468struct chan_read_no_ret {
    467     T ret;
    468     chan_read( T ) cr;
     469    T retval;
     470    chan_read( T ) c_read;
    469471};
    470472__CFA_SELECT_GET_TYPE( chan_read_no_ret(T) );
    471473
    472474static inline void ?{}( chan_read_no_ret(T) & this, channel(T) & chan ) {
    473     this.cr{ chan, this.ret };
    474 }
    475 static inline chan_read_no_ret(T) remove( channel(T) & chan ) { chan_read_no_ret(T) cr{ chan }; return cr; }
    476 static inline bool register_select( chan_read_no_ret(T) & this, select_node & node ) { return register_select( this.cr, node ); }
    477 static inline bool unregister_select( chan_read_no_ret(T) & this, select_node & node ) { return unregister_select( this.cr, node ); }
    478 static inline bool on_selected( chan_read_no_ret(T) & this, select_node & node ) { return on_selected( this.cr, node ); }
     475    this.c_read{ &chan, &this.retval };
     476}
     477
     478static inline chan_read_no_ret(T) remove( channel(T) & chan ) { chan_read_no_ret(T) c_read{ chan }; return c_read; }
     479static inline bool register_select( chan_read_no_ret(T) & this, select_node & node ) {
     480    this.c_read.ret = &this.retval;
     481    return register_select( this.c_read, node );
     482}
     483static inline bool unregister_select( chan_read_no_ret(T) & this, select_node & node ) { return unregister_select( this.c_read, node ); }
     484static inline bool on_selected( chan_read_no_ret(T) & this, select_node & node ) { return on_selected( this.c_read, node ); }
    479485
    480486// type used by select statement to capture a chan write as the selected operation
    481487struct chan_write {
    482488    T elem;
    483     channel(T) & chan;
     489    channel(T) * chan;
    484490};
    485491__CFA_SELECT_GET_TYPE( chan_write(T) );
    486492
    487 static inline void ?{}( chan_write(T) & cw, channel(T) & chan, T elem ) {
    488     &cw.chan = &chan;
     493static inline void ?{}( chan_write(T) & cw, channel(T) * chan, T elem ) {
     494    cw.chan = chan;
    489495    memcpy( (void *)&cw.elem, (void *)&elem, sizeof(T) );
    490496}
    491 static inline chan_write(T) ?<<?( channel(T) & chan, T elem ) { chan_write(T) cw{ chan, elem }; return cw; }
    492 static inline chan_write(T) insert( T elem, channel(T) & chan) { chan_write(T) cw{ chan, elem }; return cw; }
    493 
    494 static inline void __handle_select_closed_write( chan_write(T) & this, select_node & node ) with(this.chan, this) {
    495     __closed_insert( chan, elem );
     497static inline chan_write(T) ?<<?( channel(T) & chan, T elem ) { chan_write(T) cw{ &chan, elem }; return cw; }
     498static inline chan_write(T) insert( T elem, channel(T) & chan) { chan_write(T) cw{ &chan, elem }; return cw; }
     499
     500static inline void __handle_select_closed_write( chan_write(T) & this, select_node & node ) with(*this.chan, this) {
     501    __closed_insert( *chan, elem );
    496502    // if we get here then the insert succeeded
    497503    __make_select_node_available( node );
    498504}
    499505
    500 static inline bool register_select( chan_write(T) & this, select_node & node ) with(this.chan, this) {
     506static inline bool register_select( chan_write(T) & this, select_node & node ) with(*this.chan, this) {
    501507    lock( mutex_lock );
    502508    node.extra = &elem; // set .extra so that if it == 0p later in on_selected it is due to channel close
     
    516522
    517523            if ( __handle_pending( cons, node ) ) {
    518                 __cons_handoff( chan, elem );
     524                __cons_handoff( *chan, elem );
    519525                __make_select_node_sat( node ); // need to to mark SAT now that we know operation is done or else threads could get stuck in __mark_select_node
    520526                unlock( mutex_lock );
     
    543549    ConsEmpty: if ( !cons`isEmpty ) {
    544550        if ( !__handle_waituntil_OR( cons ) ) break ConsEmpty;
    545         __cons_handoff( chan, elem );
     551        __cons_handoff( *chan, elem );
    546552        __set_avail_then_unlock( node, mutex_lock );
    547553        return true;
     
    560566
    561567    // otherwise carry out write either via normal insert
    562     __buf_insert( chan, elem );
     568    __buf_insert( *chan, elem );
    563569    __set_avail_then_unlock( node, mutex_lock );
    564570    return true;
    565571}
    566 static inline bool unregister_select( chan_write(T) & this, select_node & node ) { return unregister_chan( this.chan, node ); }
     572static inline bool unregister_select( chan_write(T) & this, select_node & node ) { return unregister_chan( *this.chan, node ); }
    567573
    568574static inline bool on_selected( chan_write(T) & this, select_node & node ) with(this) {
    569575    if ( unlikely(node.extra == 0p) ) {
    570         if ( !exception_in_flight() ) __closed_insert( chan, elem ); // check if woken up due to closed channel
     576        if ( !exception_in_flight() ) __closed_insert( *chan, elem ); // check if woken up due to closed channel
    571577        else return false;
    572578    }
  • libcfa/src/concurrency/locks.hfa

    ra09552d r7a2c6b18  
    140140};
    141141
    142 static inline void ?{}(mcs_node & this) { this.next = 0p; }
     142static inline void ?{}( mcs_node & this ) { this.next = 0p; }
    143143
    144144static inline mcs_node * volatile & ?`next ( mcs_node * node ) {
     
    150150};
    151151
    152 static inline void lock(mcs_lock & l, mcs_node & n) {
     152static inline void lock( mcs_lock & l, mcs_node & n ) {
    153153        if(push(l.queue, &n))
    154154                wait(n.sem);
     
    174174};
    175175
    176 static inline void ?{}(mcs_spin_node & this) { this.next = 0p; this.locked = true; }
     176static inline void ?{}( mcs_spin_node & this ) { this.next = 0p; this.locked = true; }
    177177
    178178struct mcs_spin_lock {
     
    180180};
    181181
    182 static inline void lock(mcs_spin_lock & l, mcs_spin_node & n) {
     182static inline void lock( mcs_spin_lock & l, mcs_spin_node & n ) {
    183183    n.locked = true;
    184184        mcs_spin_node * prev = __atomic_exchange_n(&l.queue.tail, &n, __ATOMIC_SEQ_CST);
     
    273273};
    274274static inline void  ?{}( go_mutex & this ) with(this) { val = 0; }
    275 // static inline void ?{}( go_mutex & this, go_mutex this2 ) = void; // these don't compile correctly at the moment so they should be omitted
    276 // static inline void ?=?( go_mutex & this, go_mutex this2 ) = void;
     275static inline void ?{}( go_mutex & this, go_mutex this2 ) = void;
     276static inline void ?=?( go_mutex & this, go_mutex this2 ) = void;
    277277
    278278static inline bool internal_try_lock(go_mutex & this, int & compare_val, int new_val ) with(this) {
Note: See TracChangeset for help on using the changeset viewer.