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

    r438 r440  
    22 * sys_thread_exit.c - terminates the execution of calling thread
    33 *
    4  * Authors   Alain Greiner (2016,2017)
     4 * Authors   Alain Greiner (2016,2017,2018)
    55 *
    66 * Copyright (c) UPMC Sorbonne Universites
     
    2323
    2424#include <hal_types.h>
     25#include <hal_irqmask.h>
    2526#include <thread.h>
     27#include <process.h>
    2628#include <core.h>
    2729#include <vmm.h>
     
    3234int sys_thread_exit( void * exit_value )
    3335{
    34         thread_t  * this    = CURRENT_THREAD;
    35     process_t * process = this->process;
     36    reg_t       save_sr;    // required to enable IRQs
     37    xptr_t      owner_xp;   // extended pointer on owner process
     38 
     39        thread_t  * this      = CURRENT_THREAD;
     40    trdid_t     trdid     = this->trdid;
     41    process_t * process   = this->process;
     42    pid_t       pid       = process->pid;
     43    cxy_t       owner_cxy = CXY_FROM_PID( pid );
    3644
    3745    // check exit_value argument
     
    4048
    4149#if DEBUG_SYSCALLS_ERROR
    42 printk("\n[ERROR] in %s : exit_value argument must be NULL for thread %x in process %x\n",
    43 __FUNCTION__ , exit_value, this->trdid , process->pid );
     50printk("\n[ERROR] in %s : exit_value argument must be NULL / thread %x in process %x\n",
     51__FUNCTION__ , this , pid );
    4452#endif
    4553        this->errno = EINVAL;
     
    5361if( DEBUG_SYS_THREAD_EXIT < tm_start )
    5462printk("\n[DBG] %s : thread %x enter / process %x / cycle %d\n",
    55 __FUNCTION__ , this, process->pid , (uint32_t)tm_start );
     63__FUNCTION__ , this, pid , (uint32_t)tm_start );
    5664#endif
    5765
    58     // cal the relevant kernel function
    59     thread_kill( XPTR( local_cxy , this ),
    60                  1,           // is_exit
    61                  0 );         // is forced
     66    // If calling thread is the main thread, the process must be deleted.
     67    // This require to delete all process threads and synchronise with parent process
     68    if( (local_cxy == owner_cxy) && (LTID_FROM_TRDID(trdid) == 0) )
     69    {
     70        // get extended pointer on owner cluster
     71        owner_xp = cluster_get_owner_process_from_pid( pid );
     72
     73        // mark for delete all threads but the main
     74        hal_enable_irq( &save_sr );
     75        process_sigaction( pid , DELETE_ALL_THREADS );
     76        hal_restore_irq( save_sr );
     77
     78        // remove process from TXT list
     79        process_txt_detach( owner_xp );
     80
     81        // block the main thread
     82        thread_block( XPTR( local_cxy , this ) , THREAD_BLOCKED_GLOBAL );
     83
     84        // atomically update owner process descriptor term_state to ask
     85        // the parent process sys_wait() function to delete the main thread
     86        hal_remote_atomic_or( XPTR( local_cxy , &process->term_state ) ,
     87                              PROCESS_TERM_EXIT );
     88    }
     89    else
     90    {
     91        // block calling thread and mark it for delete,
     92        thread_delete( XPTR( local_cxy , this ) , pid , false );
     93    }
    6294
    6395#if DEBUG_SYS_THREAD_EXIT
     
    6597if( DEBUG_SYS_THREAD_EXIT < tm_end )
    6698printk("\n[DBG] %s : thread %x exit / process %x / cost %d / cycle %d\n",
    67 __FUNCTION__, this, this->process->pid, (uint32_t)(tm_end - tm_start), (uint32_t)tm_end );
     99__FUNCTION__, this, pid, (uint32_t)(tm_end - tm_start), (uint32_t)tm_end );
    68100#endif
    69101
    70     // deschedule <=> suicide, because blocked by thread_kill()
     102    // deschedule <=> suicide, because blocked by thread_delete()
    71103    sched_yield( "suicide after thread_exit" );
    72104   
Note: See TracChangeset for help on using the changeset viewer.