source: trunk/kernel/syscalls/sys_thread_join.c @ 435

Last change on this file since 435 was 421, checked in by alain, 7 years ago

Introduce sys_fg() , sys_display() , sys_wait() syscalls.

File size: 4.9 KB
RevLine 
[1]1/*
[23]2 * sys_thread_join.c - passive wait on the end of a given thread.
[1]3 *
[23]4 * Authors    Alain Greiner (2016,2017)
5 *
[1]6 * Copyright (c) 2011,2012 UPMC Sorbonne Universites
7 *
[23]8 * This file is part of ALMOS-MKH.
[1]9 *
[23]10 * ALMOS-MKH is free software; you can redistribute it and/or modify it
[1]11 * under the terms of the GNU General Public License as published by
12 * the Free Software Foundation; version 2.0 of the License.
13 *
[23]14 * ALMOS-MKH is distributed in the hope that it will be useful, but
[1]15 * WITHOUT ANY WARRANTY; without even the implied warranty of
16 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
17 * General Public License for more details.
18 *
19 * You should have received a copy of the GNU General Public License
[23]20 * along with ALMOS-MKH; if not, write to the Free Software Foundation,
[1]21 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
22 */
23
[23]24#include <hal_types.h>
25#include <hal_remote.h>
26#include <hal_special.h>
[1]27#include <thread.h>
[23]28#include <vmm.h>
[1]29#include <scheduler.h>
30#include <errno.h>
[23]31#include <printk.h>
32#include <remote_spinlock.h>
[1]33
[23]34///////////////////////////////////////
35int sys_thread_join ( trdid_t    trdid,
36                      void    ** exit_value )
[1]37{
[23]38    xptr_t        target_xp;
39    thread_t    * target_ptr;
40    cxy_t         target_cxy;
41    ltid_t        target_ltid;
[409]42        uint32_t      target_blocked;   // target thread blocked bit-vector
43    uint32_t      target_flags;     // target thread flags bit-bector
44    paddr_t       paddr;            // required for vmm_v2p_translate()
[1]45
[409]46        thread_t    * this    = CURRENT_THREAD;
47    process_t   * process = this->process;
[1]48
[23]49    // get target thread ltid and cxy
50    target_ltid = LTID_FROM_TRDID( trdid );
51    target_cxy  = CXY_FROM_TRDID( trdid );
52
53    // check trdid argument
54        if( (target_ltid >= CONFIG_THREAD_MAX_PER_CLUSTER) || cluster_is_undefined( target_cxy ) ) 
[1]55        {
[23]56        printk("\n[ERROR] in %s : illegal trdid argument\n", __FUNCTION__ );
57                this->errno = EINVAL;
58                return -1;
[1]59        }
60
[23]61    // check exit_value argument
62        if( (exit_value != NULL) && (vmm_v2p_translate( false , exit_value , &paddr ) != 0 ) )
[1]63        {
[23]64        printk("\n[ERROR] in %s : illegal exit_value argument\n", __FUNCTION__ );
65                this->errno = EINVAL;
66                return -1;
[1]67        }
68
[23]69    // check target thread != this thread
70    if( this->trdid == trdid )
71    {
72        printk("\n[ERROR] in %s : this thread == target thread\n", __FUNCTION__ );
73        this->errno = EDEADLK;
74        return -1;
75    }
[1]76
[23]77    // get extended pointer on target thread
78        target_xp  = thread_get_xptr( process->pid , trdid );
[1]79
[23]80    if( target_xp == XPTR_NULL )
81    {
82        printk("\n[ERROR] in %s : target thread not found\n", __FUNCTION__ );
83        this->errno = ESRCH;
84        return -1;
85    }
[1]86
[23]87    // get cluster and local pointer on target thread
88    target_ptr = (thread_t *)GET_PTR( target_xp );
[1]89
[23]90    // check target thread joinable
[409]91    target_flags = hal_remote_lw( XPTR( target_cxy , &target_ptr->flags ) );
92    if( target_flags & THREAD_FLAG_DETACHED )
[23]93    {
94        printk("\n[ERROR] in %s : target thread not joinable\n", __FUNCTION__ );
95        this->errno = EINVAL; 
96        return -1;
97    }
[1]98
[23]99    // check kernel stack overflow
100    if( target_ptr->signature != THREAD_SIGNATURE )
101    {
[421]102        assert( false , __FUNCTION__ , "kernel stack overflow\n" );
[23]103    }
[1]104
[409]105    // get the lock protecting the join in target thread
106    remote_spinlock_lock( XPTR( target_cxy , &target_ptr->join_lock ) );
107
108    // get the blocked bit_vector from the target thread
109    target_blocked = hal_remote_lw( XPTR( target_cxy , &target_ptr->blocked ) );
110
111    if( target_blocked & THREAD_BLOCKED_JOIN )    // target thread arrived first
[23]112    {
[409]113        // unblock the target thread
114        thread_unblock( target_xp , THREAD_BLOCKED_JOIN );
[1]115
[409]116        // release the lock protecting flags     
117        remote_spinlock_unlock( XPTR( target_cxy , &target_ptr->join_lock ) );
[1]118
[409]119        // get the exit value from target thread
120        *exit_value = hal_remote_lpt( XPTR( target_cxy , &target_ptr->join_value ) );
121    }
122    else                                          // this thread arrived first
123    {
124        // register this thread extended pointer in target thread
125        hal_remote_swd( XPTR( target_cxy , &target_ptr->join_xp ) , 
126                              XPTR( local_cxy , this ) );
[23]127
[409]128        // set the JOIN_DONE flag in target thread
129        hal_remote_atomic_or( XPTR( target_cxy , &target_ptr->flags ) ,
130                              THREAD_FLAG_JOIN_DONE );
[23]131
[409]132        // block this thread on BLOCKED_EXIT
133        thread_block( this , THREAD_BLOCKED_EXIT ); 
[23]134
[409]135        // release the lock protecting flags     
136        remote_spinlock_unlock( XPTR( target_cxy , &target_ptr->join_lock ) );
[23]137
[409]138        // deschedule
139        sched_yield( "WAITING_EXIT" );
140   
141        // get the exit value from target thread when resume
142        *exit_value = hal_remote_lpt( XPTR( target_cxy , &target_ptr->join_value ) );
[23]143    }
144
145    return 0;
146
147}  // end sys_thread_join()
Note: See TracBrowser for help on using the repository browser.