Ignore:
Timestamp:
May 3, 2018, 5:51:22 PM (7 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_thread_cancel.c

    r438 r440  
    2323
    2424#include <hal_types.h>
     25#include <hal_irqmask.h>
    2526#include <hal_remote.h>
    2627#include <hal_special.h>
     
    3233int sys_thread_cancel( trdid_t trdid )
    3334{
     35    reg_t        save_sr;       // required to enable IRQs
    3436    xptr_t       target_xp;     // target thread extended pointer
     37    cxy_t        target_cxy;    // target thread cluster identifier
     38    ltid_t       target_ltid;   // target thread local index
     39    cxy_t        owner_cxy;     // process owner cluster identifier
     40    xptr_t       owner_xp;      // extended pointer on owner process
    3541
    3642    // get killer thread pointers
    3743        thread_t   * this    = CURRENT_THREAD;
    3844    process_t  * process = this->process;
     45    pid_t        pid     = process->pid;
    3946
    4047    // get extended pointer on target thread
    41         target_xp  = thread_get_xptr( process->pid , trdid );
     48        target_xp  = thread_get_xptr( pid , trdid );
    4249
    4350    // check target_xp
     
    6168#endif
    6269
    63     // cal the relevant kernel function
    64     thread_kill( target_xp,
    65                  0,           // is_exit
    66                  0 );         // is forced
     70    // get process owner cluster identifier
     71    owner_cxy = CXY_FROM_PID( pid );
     72
     73    // get target thread ltid and cluster
     74    target_cxy  = CXY_FROM_TRDID( trdid );
     75    target_ltid = LTID_FROM_TRDID( trdid );
     76
     77    // If target thread is the main thread, the process must be deleted,
     78    // This require synchronisation with parent process
     79    if( (target_cxy == owner_cxy) && (target_ltid == 0) )
     80    {
     81        // get extended pointer on owner cluster
     82        owner_xp = cluster_get_owner_process_from_pid( pid );
     83
     84        // mark for delete all threads but the main
     85        hal_enable_irq( &save_sr );
     86        process_sigaction( pid , DELETE_ALL_THREADS );
     87        hal_restore_irq( save_sr );
     88
     89        // remove process from TXT list
     90        process_txt_detach( owner_xp );
     91
     92        // block the main thread
     93        thread_block( XPTR( local_cxy ,this ) , THREAD_BLOCKED_GLOBAL );
     94
     95        // atomically update owner process descriptor term_state to ask
     96        // the parent process sys_wait() function to delete the main thread
     97        hal_remote_atomic_or( XPTR( local_cxy , &process->term_state ) ,
     98                              PROCESS_TERM_EXIT );
     99    }
     100    else
     101    {
     102        // block target thread and mark it for delete
     103        thread_delete( target_xp , pid , false );
     104    }
    67105
    68106#if DEBUG_SYS_THREAD_CANCEL
Note: See TracChangeset for help on using the changeset viewer.