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/syscalls/sys_exit.c

    r438 r440  
    22 * sys_exit.c - Kernel function implementing the "exit" system call.
    33 *
    4  * Author    Alain Greiner (2016,2017)
     4 * Author    Alain Greiner (2016,2017,2018)
    55 *
    66 * Copyright (c)  UPMC Sorbonne Universites
     
    4141    process_t * process = this->process;
    4242    pid_t       pid     = process->pid;
    43     trdid_t     trdid   = this->trdid;
    4443
    4544#if DEBUG_SYS_EXIT
     
    5251#endif
    5352
    54     // get owner cluster
    55     cxy_t  owner_cxy = CXY_FROM_PID( pid );
     53    // get owner process descriptor pointers an cluster
     54    xptr_t      owner_xp  = cluster_get_owner_process_from_pid( pid );
     55    cxy_t       owner_cxy = GET_CXY( owner_xp );
     56    process_t * owner_ptr = GET_PTR( owner_xp );
    5657
    57     // exit must be called by the main thread
    58     if( (owner_cxy != local_cxy) || (LTID_FROM_TRDID( trdid ) != 0) )
    59     {
    60 
    61 #if DEBUG_SYSCALLS_ERROR
    62 printk("\n[ERROR] in %s : calling thread %x is not thread 0 in owner cluster %x\n",
    63 __FUNCTION__, trdid, owner_cxy );
    64 #endif
    65          this->errno = EINVAL;
    66          return -1;
    67     }
     58    // get pointers on the process main thread
     59    thread_t * main    = hal_remote_lpt( XPTR( owner_cxy , &owner_ptr->th_tbl[0] ) );
    6860
    6961    // enable IRQs
    7062    hal_enable_irq( &save_sr );
    7163
    72     // register exit_status in owner process descriptor
    73     process->term_state = status;
     64    // mark for delete all process threads in all clusters
     65    // (but the main thread and this calling thread)
     66    process_sigaction( pid , DELETE_ALL_THREADS );
     67
     68    // disable IRQs
     69    hal_restore_irq( save_sr );
    7470
    7571#if( DEBUG_SYS_EXIT & 1)
    76 printk("\n[DBG] %s : set exit status in process term_state\n", __FUNCTION__);
     72if( tm_start > DEBUG_SYS_EXIT )
     73printk("\n[DBG] %s : thread %x deleted threads / process %x\n",
     74__FUNCTION__ , this, pid );
    7775#endif
    7876
    79     // remove process from TXT list
    80     process_txt_detach( XPTR( local_cxy , process ) );
     77    // mark for delete this calling thread when it is not the main
     78    if( (owner_cxy != local_cxy) || (main != this) )
     79    {
    8180
    8281#if( DEBUG_SYS_EXIT & 1)
    83 printk("\n[DBG] %s : removed from TXT list\n", __FUNCTION__);
     82if( tm_start > DEBUG_SYS_EXIT )
     83printk("\n[DBG] %s : calling thread %x deleted itself / process %x\n",
     84__FUNCTION__ , this, pid );
     85#endif
     86        thread_delete( XPTR( local_cxy , this ) , pid , true );
     87    }
     88         
     89    // remove process from TXT list
     90    process_txt_detach( owner_xp );
     91
     92#if( DEBUG_SYS_EXIT & 1)
     93if( tm_start > DEBUG_SYS_EXIT )
     94printk("\n[DBG] %s : thread %x removed process %x from TXT list\n",
     95__FUNCTION__ , this, pid );
    8496#endif
    8597
    86     // mark for delete all process threads in all clusters (but the main)
    87     process_sigaction( pid , DELETE_ALL_THREADS );
     98    // block the main thread
     99    thread_block( XPTR( owner_cxy , main ) , THREAD_BLOCKED_GLOBAL );
    88100
    89101#if( DEBUG_SYS_EXIT & 1)
    90 printk("\n[DBG] %s : deleted all other threads than main\n", __FUNCTION__);
    91 #endif
    92 
    93     // restore IRQs
    94     hal_restore_irq( save_sr );
    95 
    96     // block the main thread itself
    97     thread_block( XPTR( local_cxy , this ) , THREAD_BLOCKED_GLOBAL );
    98 
    99 #if( DEBUG_SYS_EXIT & 1)
    100 printk("\n[DBG] %s : blocked the main thread\n", __FUNCTION__);
     102if( tm_start > DEBUG_SYS_EXIT )
     103printk("\n[DBG] %s : thread %x blocked main thread for process %x\n",
     104__FUNCTION__, this , pid );
    101105#endif
    102106
    103107    // atomically update owner process descriptor term_state to ask
    104     // the parent process sys_wait() function to delete this main thread
    105     hal_remote_atomic_or( XPTR( local_cxy , &process->term_state ) ,
    106                           PROCESS_TERM_EXIT );
     108    // the parent process sys_wait() function to delete the main thread
     109    hal_remote_atomic_or( XPTR( owner_cxy , &process->term_state ) ,
     110                          PROCESS_TERM_EXIT | (status & 0xFF) );
    107111
    108112#if( DEBUG_SYS_EXIT & 1)
    109 printk("\n[DBG] %s : set EXIT flag in process term_state\n", __FUNCTION__);
     113if( tm_start > DEBUG_SYS_EXIT )
     114printk("\n[DBG] %s : thread %x set exit status in process %x term_state\n",
     115__FUNCTION__ , this, pid );
    110116#endif
    111117
     
    119125#endif
    120126
    121     // main thread deschedule
     127    // this thread deschedule
    122128    sched_yield( "process exit" );
    123129
Note: See TracChangeset for help on using the changeset viewer.