Ignore:
Timestamp:
May 3, 2018, 5:51:22 PM (4 years ago)
Author:
alain
Message:

1/ Fix a bug in the Multithreaded "sort" applicationr:
The pthread_create() arguments must be declared as global variables.
2/ The exit syscall can be called by any thread of a process..

File:
1 edited

Legend:

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

    r438 r440  
    125125            thread = LIST_ELEMENT( current , thread_t , sched_list );
    126126
    127             // execute RPC thread if non blocked
    128             if( (thread->blocked == 0)  &&
    129                 (thread->type == THREAD_RPC) )
    130             {
    131                 spinlock_unlock( &sched->lock );
    132                 return thread;
    133             }
    134 
    135             // execute DEV thread if non blocked and waiting queue non empty
    136             if( (thread->blocked == 0)  &&
    137                 (thread->type == THREAD_DEV) &&
    138                 (xlist_is_empty( XPTR( local_cxy , &thread->chdev->wait_root)) == 0) )
     127            // select kernel thread if non blocked and non IDLE
     128            if( (thread->blocked == 0)  && (thread->type != THREAD_IDLE) )
    139129            {
    140130                spinlock_unlock( &sched->lock );
     
    186176
    187177    list_entry_t * iter;
     178    list_entry_t * root;
    188179    thread_t     * thread;
    189180    process_t    * process;
    190181
     182    // get pointer on scheduler
    191183    scheduler_t  * sched = &core->scheduler;
     184
     185    // get pointer on user threads root
     186    root = &sched->u_root;
    192187
    193188    // take lock protecting threads lists
    194189    spinlock_lock( &sched->lock );
    195190
     191    // We use a while to scan the user threads, to control the iterator increment,
     192    // because some threads will be destroyed, and we cannot use a LIST_FOREACH()
     193
     194    // initialise list iterator
     195    iter = root->next;
     196
    196197    // scan all user threads
    197     LIST_FOREACH( &sched->u_root , iter )
    198     {
     198    while( iter != root )
     199    {
     200        // get pointer on thread
    199201        thread = LIST_ELEMENT( iter , thread_t , sched_list );
     202
     203        // increment iterator
     204        iter = iter->next;
    200205
    201206        // handle REQ_ACK
     
    219224            process = thread->process;
    220225
     226                // release FPU if required
     227                if( thread->core->fpu_owner == thread )  thread->core->fpu_owner = NULL;
     228
     229            // remove thread from scheduler (scheduler lock already taken)
     230            uint32_t threads_nr = sched->u_threads_nr;
     231
     232            assert( (threads_nr != 0) , __FUNCTION__ , "u_threads_nr cannot be 0\n" );
     233
     234            sched->u_threads_nr = threads_nr - 1;
     235            list_unlink( &thread->sched_list );
     236            if( threads_nr == 1 ) sched->u_last = NULL;
     237
     238            // delete thread
     239            thread_destroy( thread );
     240
    221241#if DEBUG_SCHED_HANDLE_SIGNALS
    222242uint32_t cycle = (uint32_t)hal_get_cycles();
    223243if( DEBUG_SCHED_HANDLE_SIGNALS < cycle )
    224 printk("\n[DBG] %s : thread %x in proces %x must be deleted / cycle %d\n",
    225 __FUNCTION__ , thread , process->pid , cycle );
    226 #endif
    227                 // release FPU if required
    228                 if( thread->core->fpu_owner == thread )  thread->core->fpu_owner = NULL;
    229 
    230             // detach thread from parent if attached
    231             if( (thread->flags & THREAD_FLAG_DETACHED) == 0 )
    232             thread_child_parent_unlink( thread->parent , XPTR( local_cxy , thread ) );
    233 
    234             // remove thread from scheduler (scheduler lock already taken)
    235             uint32_t threads_nr = sched->u_threads_nr;
    236             assert( (threads_nr != 0) , __FUNCTION__ , "u_threads_nr cannot be 0\n" );
    237             sched->u_threads_nr = threads_nr - 1;
    238             list_unlink( &thread->sched_list );
    239             if( threads_nr == 1 ) sched->u_last = NULL;
    240 
    241             // delete thread
    242             thread_destroy( thread );
    243 
    244 #if DEBUG_SCHED_HANDLE_SIGNALS
    245 cycle = (uint32_t)hal_get_cycles();
    246 if( DEBUG_SCHED_HANDLE_SIGNALS < cycle )
    247 printk("\n[DBG] %s : thread %x in process %x has been deleted / cycle %d\n",
    248 __FUNCTION__ , thread , process->pid , cycle );
     244printk("\n[DBG] %s : thread %x in proces %x (%x) deleted / cycle %d\n",
     245__FUNCTION__ , thread , process->pid , process , cycle );
    249246#endif
    250247            // destroy process descriptor if no more threads
     
    314311    {
    315312
     313if( (local_cxy == 0X1) && (core->lid == 1) && ((uint32_t)current == 0xcc000) )
     314printk("\n@@@@@ cc000 exit at cycle %d\n", (uint32_t)hal_get_cycles() );
     315
     316if( (local_cxy == 0X1) && (core->lid == 1) && ((uint32_t)next == 0xcc000) )
     317printk("\n@@@@@ cc000 enter at cycle %d\n", (uint32_t)hal_get_cycles() );
     318
    316319#if DEBUG_SCHED_YIELD
    317320uint32_t cycle = (uint32_t)hal_get_cycles();
Note: See TracChangeset for help on using the changeset viewer.