Ignore:
Timestamp:
Jun 18, 2017, 10:06:41 PM (5 years ago)
Author:
alain
Message:

Introduce syscalls.

File:
1 edited

Legend:

Unmodified
Added
Removed
  • trunk/kernel/syscalls/sys_thread_detach.c

    r1 r23  
    11/*
    2  * kern/sys_thread_detach.c - detach a joinable thread
     2 * sys_thread_detach.c - detach a joinable thread
    33 *
    4  * Copyright (c) 2008,2009,2010,2011,2012 Ghassan Almaless
     4 * Authors   Alain Greiner (2016,2017)
     5 *
    56 * Copyright (c) 2011,2012 UPMC Sorbonne Universites
    67 *
    7  * This file is part of ALMOS-kernel.
     8 * This file is part of ALMOS-MKH.
    89 *
    9  * ALMOS-kernel is free software; you can redistribute it and/or modify it
     10 * ALMOS-MKH is free software; you can redistribute it and/or modify it
    1011 * under the terms of the GNU General Public License as published by
    1112 * the Free Software Foundation; version 2.0 of the License.
    1213 *
    13  * ALMOS-kernel is distributed in the hope that it will be useful, but
     14 * ALMOS-MKH is distributed in the hope that it will be useful, but
    1415 * WITHOUT ANY WARRANTY; without even the implied warranty of
    1516 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
     
    1718 *
    1819 * You should have received a copy of the GNU General Public License
    19  * along with ALMOS-kernel; if not, write to the Free Software Foundation,
     20 * along with ALMOS-MKH; if not, write to the Free Software Foundation,
    2021 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
    2122 */
    2223
    23 #include <list.h>
     24#include <hal_types.h>
     25#include <hal_remote.h>
     26#include <hal_special.h>
    2427#include <thread.h>
    25 #include <kmem.h>
    26 #include <kmagics.h>
    2728#include <errno.h>
    28 #include <task.h>
    29 #include <spinlock.h>
     29#include <printk.h>
    3030
    31 int sys_thread_detach (pthread_t tid)
     31//////////////////////////////////////
     32int sys_thread_detach( trdid_t trdid )
    3233{
    33         register struct task_s *task;
    34         struct thread_s *target_th;
    35         uint_t state;
    36         error_t err;
     34    xptr_t       target_xp;
     35        thread_t   * target_ptr;
     36    cxy_t        target_cxy;
     37    ltid_t       target_ltid;
     38    uint32_t     flags;
    3739
    38         task = current_task;
     40        thread_t   * this    = CURRENT_THREAD;
     41    process_t  * process = this->process;
    3942
    40         if(tid > task->max_order)
     43    // get target thread ltid and cxy
     44    target_ltid = LTID_FROM_TRDID( trdid );
     45    target_cxy  = CXY_FROM_TRDID( trdid );
     46
     47    // check trdid argument
     48        if( (target_ltid >= CONFIG_THREAD_MAX_PER_CLUSTER) || cluster_is_undefined( target_cxy ) ) 
    4149        {
    42                 err = EINVAL;
    43                 goto fail_arg;
    44         }
    45  
    46         spinlock_lock(&task->th_lock);
    47 
    48         target_th = task->th_tbl[tid];
    49    
    50         if((target_th == NULL)                 ||
    51            (target_th->signature != THREAD_ID) ||
    52            (target_th->info.attr.key != tid))
    53         {
    54                 err = ESRCH;
    55                 goto fail_srch;
    56         }
    57  
    58         if(!(thread_isJoinable(target_th)))
    59         {
    60                 err = EINVAL;
    61                 goto fail_not_joinable;
    62         }
    63  
    64         err = 0;
    65 
    66         spinlock_lock(&target_th->lock);
    67 
    68         thread_clear_joinable(target_th);
    69 
    70         if((target_th->info.join == NULL) &&
    71            !(state = wait_queue_isEmpty(&target_th->info.wait_queue)))
    72         {
    73                 wakeup_one(&target_th->info.wait_queue, WAIT_ANY);
     50        printk("\n[ERROR] in %s : illegal trdid argument\n", __FUNCTION__ );
     51                this->errno = EINVAL;
     52                return -1;
    7453        }
    7554
    76         spinlock_unlock(&target_th->lock);
    77  
    78 fail_not_joinable:
    79 fail_srch:
    80         spinlock_unlock(&task->lock);
     55    // get extended pointer on target thread
     56        target_xp  = thread_get_xptr( process->pid , trdid );
    8157
    82 fail_arg:
    83         current_thread->info.errno = err;
    84         return err;
    85 }
     58    if( target_xp == XPTR_NULL )
     59    {
     60        printk("\n[ERROR] in %s : target thread not found\n", __FUNCTION__ );
     61        this->errno = ESRCH;
     62        return -1;
     63    }
     64
     65    // get local pointer on target thread
     66    target_ptr = (thread_t *)GET_PTR( target_xp );
     67
     68    // get target thread flags
     69    flags = hal_remote_lw( XPTR( target_cxy , &target_ptr->flags ) );
     70
     71    // check target thread joinable
     72    if( flags & THREAD_FLAG_DETACHED )
     73    {
     74        printk("\n[ERROR] in %s : target thread not joinable\n", __FUNCTION__ );
     75        this->errno = EINVAL;
     76        return -1;
     77    }
     78
     79    // atomically set the thread DETACHED flag
     80    hal_remote_atomic_or( XPTR( target_cxy , &target_ptr->flags ) , THREAD_FLAG_DETACHED );
     81
     82        return 0;
     83
     84}  // end sys_thread_detach()
Note: See TracChangeset for help on using the changeset viewer.