Changeset 171 for trunk


Ignore:
Timestamp:
Jul 11, 2017, 10:26:53 AM (7 years ago)
Author:
max@…
Message:

style

File:
1 edited

Legend:

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

    r170 r171  
    11/*
    22 * thread.c -  implementation of thread operations (user & kernel)
    3  * 
     3 *
    44 * Author  Ghassan Almaless (2008,2009,2010,2011,2012)
    55 *         Alain Greiner (2016,2017)
     
    7474{
    7575        page_t       * page;   // pointer on page descriptor containing thread descriptor
    76         kmem_req_t     req;    // kmem request 
     76        kmem_req_t     req;    // kmem request
    7777
    7878        // allocates memory for thread descriptor + kernel stack
     
    8585        if( page == NULL ) return NULL;
    8686    else               return (thread_t *)ppm_page2vaddr( page );
    87 } 
     87}
    8888
    8989/////////////////////////////////////////////////////////////////////////////////////
     
    141141    spinlock_unlock( &process->th_lock );
    142142
    143     if( error ) 
     143    if( error )
    144144    {
    145145        printk("\n[ERROR] in %s : cannot get TRDID\n", __FUNCTION__ );
     
    149149        // Initialize new thread descriptor
    150150    thread->trdid           = trdid;
    151         thread->type            = type; 
     151        thread->type            = type;
    152152    thread->quantum         = 0;            // TODO
    153153    thread->ticks_nr        = 0;            // TODO
     
    162162    xlist_root_init( XPTR( local_cxy , &thread->xlocks_root ) );
    163163
    164     thread->u_stack_base    = u_stack_base;     
     164    thread->u_stack_base    = u_stack_base;
    165165    thread->u_stack_size    = u_stack_size;
    166     thread->k_stack_base    = (intptr_t)thread;     
     166    thread->k_stack_base    = (intptr_t)thread;
    167167    thread->k_stack_size    = CONFIG_THREAD_DESC_SIZE;
    168168
    169169    thread->entry_func      = func;         // thread entry point
    170170    thread->entry_args      = args;         // thread function arguments
    171     thread->flags           = 0;            // all flags reset 
     171    thread->flags           = 0;            // all flags reset
    172172    thread->signals         = 0;            // no pending signal
    173173    thread->errno           = 0;            // no error detected
    174     thread->fork_user       = 0;            // no fork required 
     174    thread->fork_user       = 0;            // no fork required
    175175    thread->fork_cxy        = 0;
    176176
     
    195195    dqdt_local_update_threads( 1 );
    196196
    197     // register new thread in core scheduler 
     197    // register new thread in core scheduler
    198198    sched_register_thread( thread->core , thread );
    199199
    200200        return 0;
    201 
    202 } // end thread_init()
    203 
     201}
    204202
    205203/////////////////////////////////////////////////////////
     
    228226    }
    229227
    230     // select a target core in local cluster 
     228    // select a target core in local cluster
    231229    if( attr->attributes & PT_ATTR_CORE_DEFINED ) core_lid = attr->lid;
    232230    else                                          core_lid = cluster_select_local_core();
     
    237235            printk("\n[ERROR] in %s : illegal core index attribute = %d\n",
    238236               __FUNCTION__ , core_lid );
    239        
     237
    240238        return EINVAL;
    241239    }
    242240
    243     // allocate a stack from local VMM 
     241    // allocate a stack from local VMM
    244242    vseg = vmm_create_vseg( process, 0 , 0 , VSEG_TYPE_STACK );
    245243
     
    248246            printk("\n[ERROR] in %s : cannot create stack vseg\n", __FUNCTION__ );
    249247                return ENOMEM;
    250     } 
    251 
    252     // allocates memory tor thread descriptor
     248    }
     249
     250    // allocate memory for thread descriptor
    253251    thread = thread_alloc();
    254252
     
    260258    }
    261259
    262     // initializes thread descriptor
     260    // initialize thread descriptor
    263261    error = thread_init( thread,
    264262                         process,
     
    270268                         vseg->max - vseg->min );
    271269
    272     if( error ) 
     270    if( error )
    273271    {
    274272            printk("\n[ERROR] in %s : cannot initialize new thread\n", __FUNCTION__ );
     
    278276    }
    279277
    280     // set LOADABLE flag 
     278    // set LOADABLE flag
    281279    thread->flags = THREAD_FLAG_LOADABLE;
    282280
     
    284282    if( attr->attributes & PT_ATTR_DETACH ) thread->flags |= THREAD_FLAG_DETACHED;
    285283
    286     // allocate & initialise CPU context
    287         error = hal_cpu_context_create( thread ); 
    288 
    289     if( error ) 
     284    // allocate & initialize CPU context
     285        error = hal_cpu_context_create( thread );
     286
     287    if( error )
    290288    {
    291289            printk("\n[ERROR] in %s : cannot create CPU context\n", __FUNCTION__ );
     
    295293    }
    296294
    297     // allocate & initialise FPU context
    298     error = hal_fpu_context_create( thread ); 
     295    // allocate & initialize FPU context
     296    error = hal_fpu_context_create( thread );
    299297
    300298    if( error )
     
    306304    }
    307305
    308     thread_dmsg("\n[INFO] %s : exit / trdid = %x / process %x / core = %d\n", 
     306    thread_dmsg("\n[INFO] %s : exit / trdid = %x / process %x / core = %d\n",
    309307                __FUNCTION__ , thread->trdid , process->pid , core_lid );
    310308
    311309    *new_thread = thread;
    312310        return 0;
    313 
    314 } // end thread_user_create()
    315 
     311}
    316312
    317313//////////////////////////////////////////////
     
    326322    thread_dmsg("\n[INFO] %s : enters\n", __FUNCTION__ );
    327323
    328     // allocate a stack from local VMM 
     324    // allocate a stack from local VMM
    329325    vseg = vmm_create_vseg( process, 0 , 0 , VSEG_TYPE_STACK );
    330326
     
    333329            printk("\n[ERROR] in %s : cannot create stack vseg\n", __FUNCTION__ );
    334330                return ENOMEM;
    335     } 
     331    }
    336332
    337333    // select a target core in local cluster
     
    341337    thread_t * this = CURRENT_THREAD;
    342338
    343     // allocated memory for new thread descriptor
     339    // allocate memory for new thread descriptor
    344340    thread = thread_alloc();
    345341
     
    351347    }
    352348
    353     // initializes thread descriptor
     349    // initialize thread descriptor
    354350    error = thread_init( thread,
    355351                         process,
     
    372368    if( this->flags & THREAD_FLAG_DETACHED ) thread->flags = THREAD_FLAG_DETACHED;
    373369
    374     // allocate & initialise CPU context from calling thread
    375         error = hal_cpu_context_copy( thread , this ); 
     370    // allocate & initialize CPU context from calling thread
     371        error = hal_cpu_context_copy( thread , this );
    376372
    377373    if( error )
     
    383379    }
    384380
    385     // allocate & initialise FPU context from calling thread
    386         error = hal_fpu_context_copy( thread , this ); 
     381    // allocate & initialize FPU context from calling thread
     382        error = hal_fpu_context_copy( thread , this );
    387383
    388384    if( error )
     
    394390    }
    395391
    396     thread_dmsg("\n[INFO] %s : exit / thread %x for process %x on core %d in cluster %x\n", 
     392    thread_dmsg("\n[INFO] %s : exit / thread %x for process %x on core %d in cluster %x\n",
    397393                 __FUNCTION__, thread->trdid, process->pid, core_lid, local_cxy );
    398394
    399395    *new_thread = thread;
    400396        return 0;
    401 
    402 } // end thread_user_fork()
    403 
    404 
     397}
    405398
    406399/////////////////////////////////////////////////////////
    407400error_t thread_kernel_create( thread_t     ** new_thread,
    408401                              thread_type_t   type,
    409                               void          * func, 
    410                               void          * args, 
     402                              void          * func,
     403                              void          * args,
    411404                                              lid_t           core_lid )
    412405{
    413406    error_t        error;
    414407        thread_t     * thread;       // pointer on new thread descriptor
    415         kmem_req_t     req;          // kmem request (for release) 
     408        kmem_req_t     req;          // kmem request (for release)
    416409
    417410    thread_dmsg("\n[INFO] %s : enters for type %s in cluster %x\n",
    418411                __FUNCTION__ , thread_type_str( type ) , local_cxy );
    419412
    420     assert( ( (type == THREAD_KERNEL) || (type == THREAD_RPC) || 
     413    assert( ( (type == THREAD_KERNEL) || (type == THREAD_RPC) ||
    421414              (type == THREAD_IDLE)   || (type == THREAD_DEV) ) ,
    422415              __FUNCTION__ , "illegal thread type" );
    423416
    424     assert( (core_lid < LOCAL_CLUSTER->cores_nr) , 
     417    assert( (core_lid < LOCAL_CLUSTER->cores_nr) ,
    425418            __FUNCTION__ , "illegal core_lid" );
    426419
    427     // allocated memory for new thread descriptor
     420    // allocate memory for new thread descriptor
    428421    thread = thread_alloc();
    429422
    430423    if( thread == NULL ) return ENOMEM;
    431424
    432     // initializes thread descriptor
     425    // initialize thread descriptor
    433426    error = thread_init( thread,
    434427                         &process_zero,
     
    439432                         0 , 0 );  // no user stack for a kernel thread
    440433
    441     if( error ) // release allocated memory for thread descriptor 
     434    if( error ) // release allocated memory for thread descriptor
    442435    {
    443436            req.type  = KMEM_PAGE;
     
    447440    }
    448441
    449 
    450     // allocate & initialise CPU context
    451         hal_cpu_context_create( thread );
    452 
    453     thread_dmsg("\n[INFO] %s : exit in cluster %x / trdid = %x / core_lid = %d\n",
     442    // allocate & initialize CPU context
     443        hal_cpu_context_create( thread );
     444
     445    thread_dmsg("\n[INFO] %s : exit in cluster %x / trdid = %x / core_lid = %d\n",
    454446                 __FUNCTION__ , local_cxy , thread->trdid , core_lid );
    455447
    456     *new_thread = thread; 
     448    *new_thread = thread;
    457449        return 0;
    458 
    459 } // end thread_kernel_create()
     450}
    460451
    461452///////////////////////////////////////////////////
    462453error_t thread_kernel_init( thread_t      * thread,
    463454                            thread_type_t   type,
    464                             void          * func, 
    465                             void          * args, 
     455                            void          * func,
     456                            void          * args,
    466457                                            lid_t           core_lid )
    467458{
    468     assert( ( (type == THREAD_KERNEL) || (type == THREAD_RPC) || 
     459    assert( ( (type == THREAD_KERNEL) || (type == THREAD_RPC) ||
    469460              (type == THREAD_IDLE)   || (type == THREAD_DEV) ) ,
    470461              __FUNCTION__ , "illegal thread type" );
    471462
    472     if( core_lid >= LOCAL_CLUSTER->cores_nr ) 
    473     {
    474         printk("\n[PANIC] in %s : illegal core_lid / cores = %d / lid = %d / cxy = %x\n", 
     463    if( core_lid >= LOCAL_CLUSTER->cores_nr )
     464    {
     465        printk("\n[PANIC] in %s : illegal core_lid / cores = %d / lid = %d / cxy = %x\n",
    475466               __FUNCTION__ , LOCAL_CLUSTER->cores_nr , core_lid , local_cxy );
    476467        hal_core_sleep();
     
    487478    // allocate & initialize CPU context if success
    488479    if( error == 0 ) hal_cpu_context_create( thread );
    489      
     480
    490481    return error;
    491 
    492 }  // end thread_kernel_init()
     482}
    493483
    494484///////////////////////////////////////////////////////////////////////////////////////
     
    511501
    512502    assert( (thread->local_locks == 0) , __FUNCTION__ , "all local locks not released" );
    513    
     503
    514504    assert( (thread->remote_locks == 0) , __FUNCTION__ , "all remote locks not released" );
    515505
     
    539529        hal_restore_irq( state );
    540530
    541     // remove thread from process th_tbl[] 
     531    // remove thread from process th_tbl[]
    542532    // TODO This should be done before calling thread_destroy()
    543533    ltid_t ltid = LTID_FROM_TRDID( thread->trdid );
     
    561551        thread_dmsg("\n[INFO] %s : exit for thread %x in process %x / duration = %d\n",
    562552                       __FUNCTION__, thread->trdid , process->pid , tm_end - tm_start );
    563 
    564 }  // end thread_destroy()
    565 
     553}
    566554
    567555/////////////////////////////////////////////////
     
    569557                               xptr_t  xp_child )
    570558{
    571     // get extended pointers on children list root 
    572     cxy_t      parent_cxy = GET_CXY( xp_parent );   
     559    // get extended pointers on children list root
     560    cxy_t      parent_cxy = GET_CXY( xp_parent );
    573561    thread_t * parent_ptr = (thread_t *)GET_PTR( xp_parent );
    574562    xptr_t     root       = XPTR( parent_cxy , &parent_ptr->children_root );
    575563
    576     // get extended pointer on children list entry 
    577     cxy_t      child_cxy  = GET_CXY( xp_child );   
     564    // get extended pointer on children list entry
     565    cxy_t      child_cxy  = GET_CXY( xp_child );
    578566    thread_t * child_ptr  = (thread_t *)GET_PTR( xp_child );
    579567    xptr_t     entry      = XPTR( child_cxy , &child_ptr->brothers_list );
     
    582570    xlist_add_first( root , entry );
    583571    hal_remote_atomic_add( XPTR( parent_cxy , &parent_ptr->children_nr ) , 1 );
    584 } 
     572}
    585573
    586574///////////////////////////////////////////////////
     
    589577{
    590578    // get extended pointer on children list lock
    591     cxy_t      parent_cxy = GET_CXY( xp_parent );   
     579    cxy_t      parent_cxy = GET_CXY( xp_parent );
    592580    thread_t * parent_ptr = (thread_t *)GET_PTR( xp_parent );
    593581    xptr_t     lock       = XPTR( parent_cxy , &parent_ptr->children_lock );
    594582
    595     // get extended pointer on children list entry 
    596     cxy_t      child_cxy  = GET_CXY( xp_child );   
     583    // get extended pointer on children list entry
     584    cxy_t      child_cxy  = GET_CXY( xp_child );
    597585    thread_t * child_ptr  = (thread_t *)GET_PTR( xp_child );
    598586    xptr_t     entry      = XPTR( child_cxy , &child_ptr->brothers_list );
     
    604592    xlist_unlink( entry );
    605593    hal_remote_atomic_add( XPTR( parent_cxy , &parent_ptr->children_nr ) , -1 );
    606    
     594
    607595    // release the lock
    608596    remote_spinlock_unlock( lock );
     
    615603    hal_atomic_or( &thread->signals , mask );
    616604}
    617  
     605
    618606///////////////////////////////////////////////////
    619607inline void thread_reset_signal( thread_t * thread,
     
    622610    hal_atomic_and( &thread->signals , ~mask );
    623611}
    624  
     612
    625613//////////////////////////////////
    626614inline bool_t thread_is_joinable()
     
    693681        hal_restore_irq( sr_save );
    694682    }
    695     else 
    696     {
    697         // if attached, set blocking cause 
     683    else
     684    {
     685        // if attached, set blocking cause
    698686        thread_block( this , THREAD_BLOCKED_EXIT );
    699687    }
     
    702690    sched_yield();
    703691    return 0;
    704 
    705 } // end thread_exit()
     692}
    706693
    707694/////////////////////////////////////
     
    709696                   uint32_t   cause )
    710697{
    711     // set blocking cause 
     698    // set blocking cause
    712699    hal_atomic_or( &thread->blocked , cause );
    713 
    714 }  // end thread_block()
     700}
    715701
    716702////////////////////////////////////
     
    719705{
    720706    // get thread cluster and local pointer
    721     cxy_t      cxy = GET_CXY( thread ); 
     707    cxy_t      cxy = GET_CXY( thread );
    722708    thread_t * ptr = (thread_t *)GET_PTR( thread );
    723709
    724710    // reset blocking cause
    725711    hal_remote_atomic_and( XPTR( cxy , &ptr->blocked ) , ~cause );
    726 
    727 }  // end thread_unblock()
     712}
    728713
    729714/////////////////////////////////////
     
    738723    // send an IPI to reschedule the target thread core.
    739724    dev_icu_send_ipi( local_cxy , target->core->lid );
    740 
    741 }  // end thread_kill()
    742 
     725}
    743726
    744727///////////////////////
    745728void thread_idle_func()
    746729{
    747 
    748730#if CONFIG_IDLE_DEBUG
    749731    lid_t  lid = CURRENT_CORE->lid;
     
    767749        sched_yield();
    768750   }
    769 }  // end thread_idle()
     751}
    770752
    771753/////////////////////////////////////////////////
     
    796778    cxy_t         target_cxy;          // target thread cluster identifier
    797779    ltid_t        target_thread_ltid;  // target thread local index
    798     thread_t    * target_thread_ptr;   // target thread local pointer           
     780    thread_t    * target_thread_ptr;   // target thread local pointer
    799781    xptr_t        target_process_xp;   // extended pointer on target process descriptor
    800     process_t   * target_process_ptr;  // local pointer on target process descriptor 
     782    process_t   * target_process_ptr;  // local pointer on target process descriptor
    801783    pid_t         target_process_pid;  // target process identifier
    802784    xlist_entry_t root;                // root of list of process in target cluster
     
    812794                       sizeof(xlist_entry_t) );
    813795
    814     // get extended pointer on lock protecting the list of processes 
     796    // get extended pointer on lock protecting the list of processes
    815797    lock_xp = XPTR( target_cxy , &LOCAL_CLUSTER->pmgr.local_lock );
    816798
     
    844826    // get target thread local pointer
    845827    xptr_t xp = XPTR( target_cxy , &target_process_ptr->th_tbl[target_thread_ltid] );
    846     target_thread_ptr = (thread_t *)hal_remote_lpt( xp );   
     828    target_thread_ptr = (thread_t *)hal_remote_lpt( xp );
    847829
    848830    if( target_thread_ptr == NULL )
     
    852834
    853835    return XPTR( target_cxy , target_thread_ptr );
    854 
    855 }  // end thread_get_xptr()
    856 
     836}
     837
Note: See TracChangeset for help on using the changeset viewer.