Changeset 5afb49a for libcfa


Ignore:
Timestamp:
Oct 16, 2020, 3:31:02 PM (4 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:
8da7421f
Parents:
d21dd3cb
Message:

Split thread_leave so backend is called from the kernel once the kernel no longer needs the thread.
This hopefully solves the non-deterministic crashes in the build.

Location:
libcfa/src/concurrency
Files:
5 edited

Legend:

Unmodified
Added
Removed
  • libcfa/src/concurrency/kernel.cfa

    rd21dd3cb r5afb49a  
    252252                /* paranoid */ verify( kernelTLS.this_thread == thrd_dst );
    253253                /* paranoid */ verify( thrd_dst->context.SP );
     254                /* paranoid */ verify( thrd_dst->state != Halted );
    254255                /* paranoid */ verifyf( ((uintptr_t)thrd_dst->context.SP) < ((uintptr_t)__get_stack(thrd_dst->curr_cor)->base ) || thrd_dst->curr_cor == proc_cor, "ERROR : Destination $thread %p has been corrupted.\n StackPointer too small.\n", thrd_dst ); // add escape condition if we are setting up the processor
    255256                /* paranoid */ verifyf( ((uintptr_t)thrd_dst->context.SP) > ((uintptr_t)__get_stack(thrd_dst->curr_cor)->limit) || thrd_dst->curr_cor == proc_cor, "ERROR : Destination $thread %p has been corrupted.\n StackPointer too large.\n", thrd_dst ); // add escape condition if we are setting up the processor
     
    287288                if(unlikely(thrd_dst->state == Halted)) {
    288289                        // The thread has halted, it should never be scheduled/run again
    289                         // We may need to wake someone up here since
    290                         unpark( this->destroyer );
    291                         this->destroyer = 0p;
     290                        // finish the thread
     291                        __thread_finish( thrd_dst );
    292292                        break RUNNING;
    293293                }
     
    448448}
    449449
    450 // KERNEL ONLY
    451 void __leave_thread() {
    452         /* paranoid */ verify( ! kernelTLS.preemption_state.enabled );
    453         returnToKernel();
    454         abort();
     450extern "C" {
     451        // Leave the thread monitor
     452        // last routine called by a thread.
     453        // Should never return
     454        void __cfactx_thrd_leave() {
     455                $thread * thrd = TL_GET( this_thread );
     456                $monitor * this = &thrd->self_mon;
     457
     458                // Lock the monitor now
     459                lock( this->lock __cfaabi_dbg_ctx2 );
     460
     461                disable_interrupts();
     462
     463                thrd->state = Halted;
     464
     465                if( thrd != this->owner || this->recursion != 1) { abort( "Thread internal monitor has unbalanced recursion" ); }
     466
     467                // Leave the thread
     468                /* paranoid */ verify( ! kernelTLS.preemption_state.enabled );
     469                returnToKernel();
     470                abort();
     471
     472                // Control flow should never reach here!
     473        }
    455474}
    456475
  • libcfa/src/concurrency/kernel.hfa

    rd21dd3cb r5afb49a  
    7979        // Handle to pthreads
    8080        pthread_t kernel_thread;
    81 
    82         // RunThread data
    83         // Action to do after a thread is ran
    84         $thread * destroyer;
    8581
    8682        // Preemption data
  • libcfa/src/concurrency/kernel/startup.cfa

    rd21dd3cb r5afb49a  
    474474        this.cltr = &_cltr;
    475475        full_proc = true;
    476         destroyer = 0p;
    477476        do_terminate = false;
    478477        preemption_alarm = 0p;
  • libcfa/src/concurrency/kernel_private.hfa

    rd21dd3cb r5afb49a  
    3939;
    4040
    41 //Block current thread and release/wake-up the following resources
    42 void __leave_thread() __attribute__((noreturn));
     41//release/wake-up the following resources
     42void __thread_finish( $thread * thrd );
    4343
    4444//-----------------------------------------------------------------------------
  • libcfa/src/concurrency/monitor.cfa

    rd21dd3cb r5afb49a  
    281281}
    282282
    283 extern "C" {
    284         // Leave the thread monitor
    285         // last routine called by a thread.
    286         // Should never return
    287         void __cfactx_thrd_leave() {
    288                 $thread * thrd = TL_GET( this_thread );
    289                 $monitor * this = &thrd->self_mon;
    290 
    291                 // Lock the monitor now
    292                 lock( this->lock __cfaabi_dbg_ctx2 );
    293 
    294                 disable_interrupts();
    295 
    296                 thrd->state = Halted;
    297 
    298                 /* paranoid */ verifyf( thrd == this->owner, "Expected owner to be %p, got %p (r: %i, m: %p)", thrd, this->owner, this->recursion, this );
    299 
    300                 // Leaving a recursion level, decrement the counter
    301                 this->recursion -= 1;
    302 
    303                 // If we haven't left the last level of recursion
    304                 // it must mean there is an error
    305                 if( this->recursion != 0) { abort( "Thread internal monitor has unbalanced recursion" ); }
    306 
    307                 // Fetch the next thread, can be null
    308                 $thread * new_owner = next_thread( this );
    309 
    310                 // Release the monitor lock
    311                 unlock( this->lock );
    312 
    313                 // Unpark the next owner if needed
    314                 /* paranoid */ verifyf( !new_owner || new_owner == this->owner, "Expected owner to be %p, got %p (m: %p)", new_owner, this->owner, this );
    315                 /* paranoid */ verify( ! kernelTLS.preemption_state.enabled );
    316                 /* paranoid */ verify( ! kernelTLS.this_processor->destroyer );
    317                 /* paranoid */ verify( thrd->state == Halted );
    318 
    319                 kernelTLS.this_processor->destroyer = new_owner;
    320 
    321                 // Leave the thread
    322                 __leave_thread();
    323 
    324                 // Control flow should never reach here!
    325         }
     283void __thread_finish( $thread * thrd ) {
     284        $monitor * this = &thrd->self_mon;
     285
     286        // Lock the monitor now
     287        /* paranoid */ verify( this->lock.lock );
     288        /* paranoid */ verifyf( thrd == this->owner, "Expected owner to be %p, got %p (r: %i, m: %p)", thrd, this->owner, this->recursion, this );
     289        /* paranoid */ verify( ! kernelTLS.preemption_state.enabled );
     290        /* paranoid */ verify( thrd->state == Halted );
     291        /* paranoid */ verify( this->recursion == 1 );
     292
     293        // Leaving a recursion level, decrement the counter
     294        this->recursion -= 1;
     295        this->owner = 0p;
     296
     297        // Fetch the next thread, can be null
     298        $thread * new_owner = next_thread( this );
     299
     300        // Release the monitor lock
     301        unlock( this->lock );
     302
     303        // Unpark the next owner if needed
     304        /* paranoid */ verifyf( !new_owner || new_owner == this->owner, "Expected owner to be %p, got %p (m: %p)", new_owner, this->owner, this );
     305        /* paranoid */ verify( ! kernelTLS.preemption_state.enabled );
     306        /* paranoid */ verify( thrd->state == Halted );
     307        unpark( new_owner );
    326308}
    327309
Note: See TracChangeset for help on using the changeset viewer.