Ignore:
Timestamp:
Jan 4, 2018, 10:05:47 AM (4 years ago)
Author:
alain
Message:

Improve sys_exec.

File:
1 edited

Legend:

Unmodified
Added
Removed
  • trunk/kernel/kern/scheduler.c

    r409 r416  
    5858    list_root_init( &sched->k_root );
    5959
    60     sched->sig_pending    = false;            // no pending signal
     60    sched->req_ack_pending = false;           // no pending request
    6161
    6262}  // end sched_init()
     
    8989
    9090}  // end sched_register_thread()
    91 
    92 /////////////////////////////////////////////
    93 void sched_remove_thread( thread_t * thread )
    94 {
    95     scheduler_t * sched = &thread->core->scheduler;
    96     thread_type_t type  = thread->type;
    97 
    98     // take lock protecting sheduler lists
    99     spinlock_lock( &sched->lock );
    100 
    101     if( type == THREAD_USER )
    102     {
    103         list_unlink( &thread->sched_list );
    104         sched->u_threads_nr--;
    105         if( sched->u_threads_nr == 0 ) sched->u_last = NULL;
    106     }
    107     else // kernel thread
    108     {
    109         list_unlink( &thread->sched_list );
    110         sched->k_threads_nr--;
    111         if( sched->k_threads_nr == 0 ) sched->k_last = NULL;
    112     }
    113 
    114     // release lock
    115     spinlock_unlock( &sched->lock );
    116 
    117 }  // end sched_remove_thread()
    11891
    11992//////////////////////////////////////////////
     
    203176}  // end sched_select()
    204177
    205 //////////////////////////////////////////
    206 void sched_handle_signals( core_t * core )
     178///////////////////////////////////////////
     179void sched_handle_requests( core_t * core )
    207180{
    208181    list_entry_t * iter;
    209182    thread_t     * thread;
    210183
     184// printk("\n@@@ %s : current thread %x enter at cycle %d\n",
     185// __FUNCTION__ , CURRENT_THREAD , hal_time_stamp() );
     186
    211187    scheduler_t  * sched = &core->scheduler;
    212188
     
    214190    spinlock_lock( &sched->lock );
    215191
    216     // handle user threads
     192    // scan all user threads
    217193    LIST_FOREACH( &sched->u_root , iter )
    218194    {
    219195        thread = LIST_ELEMENT( iter , thread_t , sched_list );
    220196
    221         if( thread->flags & THREAD_FLAG_SIGNAL )  // thread has signal
    222         {
    223             // decrement response counter to acknowledge signal
    224             hal_atomic_add( thread->sig_rsp_count , -1 );
    225 
    226             // reset signal
    227             thread_reset_signal( thread );
     197        // handle REQ_ACK
     198        if( thread->flags & THREAD_FLAG_REQ_ACK )
     199        {
     200            // check thread blocked
     201            assert( (thread->blocked & THREAD_BLOCKED_GLOBAL) ,
     202            __FUNCTION__ , "thread not blocked" );
     203 
     204            // decrement response counter
     205            hal_atomic_add( thread->ack_rsp_count , -1 );
     206
     207            // reset REQ_ACK in thread descriptor
     208            thread_reset_req_ack( thread );
     209        }
     210
     211        // handle REQ_DELETE
     212        if( thread->flags & THREAD_FLAG_REQ_DELETE )
     213        {
     214
     215sched_dmsg("\n[DBG] %s : current thread %x delete thread %x at cycle %d\n",
     216__FUNCTION__ , CURRENT_THREAD , thread , hal_time_stamp() );
     217
     218                // release FPU if required
     219                if( thread->core->fpu_owner == thread )  thread->core->fpu_owner = NULL;
     220
     221            // detach thread from parent if attached
     222            if( (thread->flags & THREAD_FLAG_DETACHED) == 0 )
     223            thread_child_parent_unlink( thread->parent , XPTR( local_cxy , thread ) );
     224
     225            // detach thread from process
     226            process_remove_thread( thread );
     227
     228            // remove thread from scheduler
     229            list_unlink( &thread->sched_list );
     230            sched->u_threads_nr--;
     231            if( sched->u_threads_nr == 0 ) sched->u_last = NULL;
     232
     233            // release memory allocated to thread
     234            thread_destroy( thread );
     235
     236            // destroy process descriptor if no more threads
     237            if (thread->process->th_nr == 0) process_destroy( thread->process );
    228238        }
    229239    }
     
    232242    spinlock_unlock( &sched->lock );
    233243
    234 } // end sched_handle_signals()
     244// printk("\n@@@ %s : current thread %x exit at cycle %d\n",
     245// __FUNCTION__ , CURRENT_THREAD , hal_time_stamp() );
     246
     247} // end sched_handle_requests()
    235248
    236249////////////////////////////////
     
    305318    }
    306319
    307     // handle signals for all threads executing on this core.
    308     sched_handle_signals( core );
     320    // handle pending requests for all threads executing on this core.
     321    sched_handle_requests( core );
    309322
    310323    // exit critical section / restore SR from next thread context
     
    354367        if (thread->type == THREAD_DEV)
    355368        {
    356             nolock_printk(" - %s / pid %X / trdid %X / desc %X / blocked %X / %s\n",
     369            nolock_printk(" - %s / pid %X / trdid %X / desc %X / block %X / flags %X / %s\n",
    357370            thread_type_str( thread->type ), thread->process->pid, thread->trdid,
    358             thread, thread->blocked , thread->chdev->name );
     371            thread, thread->blocked, thread->flags, thread->chdev->name );
    359372        }
    360373        else
    361374        {
    362             nolock_printk(" - %s / pid %X / trdid %X / desc %X / blocked %X\n",
     375            nolock_printk(" - %s / pid %X / trdid %X / desc %X / block %X / flags %X \n",
    363376            thread_type_str( thread->type ), thread->process->pid, thread->trdid,
    364             thread, thread->blocked );
     377            thread, thread->blocked, thread->flags );
    365378        }
    366379    }
     
    370383    {
    371384        thread = LIST_ELEMENT( iter , thread_t , sched_list );
    372         nolock_printk(" - %s / pid %X / trdid %X / desc %X / blocked %X\n",
     385        nolock_printk(" - %s / pid %X / trdid %X / desc %X / block %X / flags %X\n",
    373386        thread_type_str( thread->type ), thread->process->pid, thread->trdid,
    374         thread, thread->blocked );
     387        thread, thread->blocked, thread->flags );
    375388    }
    376389
Note: See TracChangeset for help on using the changeset viewer.