Changeset 3ea8ad1 for libcfa/src


Ignore:
Timestamp:
Nov 18, 2020, 2:28:02 PM (3 years ago)
Author:
Thierry Delisle <tdelisle@…>
Branches:
ADT, arm-eh, ast-experimental, enum, forall-pointer-decay, jacob/cs343-translation, master, new-ast-unique-expr, pthread-emulation, qualifiedEnum
Children:
69d1748
Parents:
9d6e1b8a
Message:

Added more checks for thread termination synchronization

Location:
libcfa/src/concurrency
Files:
4 edited

Legend:

Unmodified
Added
Removed
  • libcfa/src/concurrency/invoke.h

    r9d6e1b8a r3ea8ad1  
    6868        };
    6969
    70         enum __Coroutine_State { Halted, Start, Primed, Blocked, Ready, Active, Cancelled };
     70        enum __Coroutine_State { Halted, Start, Primed, Blocked, Ready, Active, Cancelled, Halting };
    7171
    7272        struct $coroutine {
  • libcfa/src/concurrency/kernel.cfa

    r9d6e1b8a r3ea8ad1  
    288288                }
    289289
    290                 if(unlikely(thrd_dst->state == Halted)) {
     290                if(unlikely(thrd_dst->state == Halting)) {
    291291                        // The thread has halted, it should never be scheduled/run again
    292292                        // finish the thread
     
    484484                /* paranoid */ verifyf( ((uintptr_t)thrd->context.SP) < ((uintptr_t)__get_stack(thrd->curr_cor)->base ), "ERROR : $thread %p has been corrupted.\n StackPointer too small.\n", thrd );
    485485
    486                 thrd->state = Halted;
     486                thrd->state = Halting;
    487487                if( TICKET_RUNNING != thrd->ticket ) { abort( "Thread terminated with pending unpark" ); }
    488488                if( thrd != this->owner ) { abort( "Thread internal monitor has incorrect owner" ); }
  • libcfa/src/concurrency/monitor.cfa

    r9d6e1b8a r3ea8ad1  
    142142static void __dtor_enter( $monitor * this, fptr_t func, bool join ) {
    143143        $thread * thrd = active_thread();
     144        #if defined( __CFA_WITH_VERIFY__ )
     145                bool is_thrd = this == &thrd->self_mon;
     146        #endif
    144147
    145148        // Lock the monitor spinlock
     
    155158                __set_owner( this, thrd );
    156159
    157                 verifyf( active_thread() == this->owner, "Expected owner to be %p, got %p (r: %i, m: %p)", active_thread(), this->owner, this->recursion, this );
     160                /* paranoid */ verifyf( active_thread() == this->owner, "Expected owner to be %p, got %p (r: %i, m: %p)", active_thread(), this->owner, this->recursion, this );
     161                /* paranoid */ verify( !is_thrd || thrd->state == Halted || thrd->state == Cancelled );
    158162
    159163                unlock( this->lock );
     
    175179                __set_owner( this, thrd );
    176180
    177                 verifyf( active_thread() == this->owner, "Expected owner to be %p, got %p (r: %i, m: %p)", active_thread(), this->owner, this->recursion, this );
     181                /* paranoid */ verifyf( active_thread() == this->owner, "Expected owner to be %p, got %p (r: %i, m: %p)", active_thread(), this->owner, this->recursion, this );
     182                /* paranoid */ verify( !is_thrd || thrd->state == Halted || thrd->state == Cancelled );
    178183
    179184                unlock( this->lock );
    180185                return;
    181186        }
     187
     188        // The monitor is busy, if this is a thread and the thread owns itself, it better be active
     189        /* paranoid */ verify( !is_thrd || this->owner != thrd || (thrd->state != Halted && thrd->state != Cancelled) );
    182190
    183191        __lock_size_t count = 1;
     
    209217                // Some one was waiting for us, enter
    210218                /* paranoid */ verifyf( active_thread() == this->owner, "Expected owner to be %p, got %p (r: %i, m: %p)", active_thread(), this->owner, this->recursion, this );
     219
     220                __cfaabi_dbg_print_safe( "Kernel : Destroying %p\n", this);
     221                return;
    211222        }
    212223        else {
     
    228239                return;
    229240        }
    230 
    231         __cfaabi_dbg_print_safe( "Kernel : Destroying %p\n", this);
    232 
    233241}
    234242
     
    294302
    295303        /* paranoid */ verifyf( thrd == this->owner, "Expected owner to be %p, got %p (r: %i, m: %p)", thrd, this->owner, this->recursion, this );
    296         /* paranoid */ verify( thrd->state == Halted );
     304        /* paranoid */ verify( thrd->state == Halting );
    297305        /* paranoid */ verify( this->recursion == 1 );
    298306
     
    303311        // Fetch the next thread, can be null
    304312        $thread * new_owner = next_thread( this );
     313
     314        // Mark the state as fully halted
     315        thrd->state = Halted;
    305316
    306317        // Release the monitor lock
  • libcfa/src/concurrency/thread.cfa

    r9d6e1b8a r3ea8ad1  
    8282                T & thrd, void(*defaultResumptionHandler)(ThreadCancelled(T) &)) {
    8383        $monitor * m = get_monitor(thrd);
     84        $thread * desc = get_thread(thrd);
     85
     86        // Setup the monitor guard
    8487        void (*dtor)(T& mutex this) = ^?{};
    8588        bool join = defaultResumptionHandler != (void(*)(ThreadCancelled(T)&))0;
    8689        (this.mg){&m, (void(*)())dtor, join};
    8790
     91
     92        /* paranoid */ verifyf( Halted == desc->state || Cancelled == desc->state, "Expected thread to be Halted or Cancelled, was %d\n", (int)desc->state );
     93
    8894        // After the guard set-up and any wait, check for cancellation.
    89         $thread * desc = get_thread(thrd);
    9095        struct _Unwind_Exception * cancellation = desc->self_cor.cancellation;
    9196        if ( likely( 0p == cancellation ) ) {
Note: See TracChangeset for help on using the changeset viewer.