source: trunk/kernel/syscalls/sys_thread_create.c @ 6

Last change on this file since 6 was 1, checked in by alain, 8 years ago

First import

File size: 5.3 KB
RevLine 
[1]1/*
2 * sys_thread_create.c - creates a new user thread
3 *
4 * Author  Ghassan Almaless (2008,2009,2010,2011,2012)
5 *         Mohamed lamine Karaoui (2015)
6 *         Alain Greiner (2016)
7 *
8 * Copyright (c) UPMC Sorbonne Universites
9 *
10 * This file is part of ALMOS-MKH.
11 *
12 * ALMOS-MKH is free software; you can redistribute it and/or modify it
13 * under the terms of the GNU General Public License as published by
14 * the Free Software Foundation; version 2.0 of the License.
15 *
16 * ALMOS-MKH is distributed in the hope that it will be useful, but
17 * WITHOUT ANY WARRANTY; without even the implied warranty of
18 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
19 * General Public License for more details.
20 *
21 * You should have received a copy of the GNU General Public License
22 * along with ALMOS-MKH; if not, write to the Free Software Foundation,
23 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
24 */
25
26#include <almos_config.h>
27#include <hal_types.h>
28#include <printk.h>
29#include <errno.h>
30#include <core.h>
31#include <cluster.h>
32#include <list.h>
33#include <xlist.h>
34#include <thread.h>
35#include <scheduler.h>
36#include <kmem.h>
37#include <process.h>
38#include <spinlock.h>
39#include <dqdt.h>
40
41
42//////////////////////////////////////////////////////////////////////////////////////////
43// This function implements the pthread_create system call
44//////////////////////////////////////////////////////////////////////////////////////////
45int sys_thread_create ( thread_t       * new_thread,    // [out] argument
46                        pthread_attr_t * user_attr,     // [in] argument
47                        void           * start_func,    // [in] argument
48                        void           * start_args )   // [in] argument
49{
50        pthread_attr_t   attr;             // copy of pthread attributes in kernel space
51        thread_t       * parent;           // pointer on thread executing the pthread_create
52    xptr_t           xp_parent;        // extended pointer on created thread
53    thread_t       * child;            // pointer on created child thread
54    xptr_t           xp_child;         // extended pointer on created thread
55    trdid_t          trdid;            // created thread identifier
56    process_t      * process;          // pointer on local process descriptor
57    error_t          error;
58
59        uint32_t         tm_start;
60        uint32_t         tm_end;
61
62        tm_start = hal_time_stamp();
63
64    // get parent thead pointer, extended pointer, and process pointer
65        parent     = CURRENT_THREAD;
66    xp_parent  = XPTR( local_cxy , parent );   
67        process    = parent->process;
68
69    // check user_attr & start_func arguments
70        if( user_attr == NULL )
71        {
72                printk("\n[ERROR] in %s : user_attr is NULL\n", __FUNCTION__ );
73                return EINVAL;
74        }
75        if( start_func== NULL )
76        {
77                printk("\n[ERROR] in %s : start_func is NULL\n", __FUNCTION__ );
78                return EINVAL;
79        }
80
81    // copy user_attr structure to kernel space
82        hal_copy_from_uspace( &attr , user_attr , sizeof(pthread_attr_t) );
83
84    // check/set "cxy" attribute
85        if( attr.flags & PT_FLAG_CLUSTER_DEFINED ) 
86    {
87        if( cluster_is_undefined( attr.cxy ) )
88        {
89                    printk("\n[ERROR] in %s : illegal target cluster = %x\n",
90                   __FUNCTION__ , attr.cxy );
91                    return = EINVAL;
92        } 
93    }
94    else
95    {
96        attr.cxy = dqdt_get_cluster_for_process();
97    }
98
99    // set "pid", "start_func" & "start_args" attributes
100    attr.pid        = process->pid;
101    attr.start_func = start_func;
102    attr.start_args = start_args;
103
104    // create the thread, using a RPC if required
105    // this returns "error", "child", and "xp_child"
106    if( attr.cxy == local_cxy )                 // target cluster is local
107    {
108        // allocate a stack from local VMM
109        vseg_t * vseg = vmm_create_vseg( process, 
110                                         0,
111                                         0,
112                                         VSEG_TYPE_STACK,
113                                         local_cxy );
114        if( vseg == NULL )
115        {
116                    printk("\n[ERROR] in %s for : cannot create stack vseg\n", __FUNCTION__ );
117                    return = EINVAL;
118        } 
119
120        // create thread in local cluster
121        error = thread_user_create( &child,
122                                    &attr,
123                                    vseg->min,
124                                    vseg->max - vseg->min );
125
126        xp_child = XPTR( local_cxy , thread );
127    }
128    else                                                 // target cluster is remote
129    {
130        rpc_thread_user_create( attr.cxy , &attr , &error , &xp_child );
131        child = (thread_t *)GET_PTR( xp_child );
132    }
133
134    // check successful thread creation
135    if( error ) 
136    {
137                printk("\n[ERROR] in %s : cannot create thread\n", __FUNCTION__ );
138        return ENOMEM;
139    }
140
141    // returns trdid to user space
142    trdid = hal_remote_lw( XPTR( attr.cxy , &child->trdid ) );   
143        hal_copy_to_uspace( new_thread , &trdid , sizeof(pthread_t) );
144   
145    // register new-thread in parent-thread children list if required
146    if( attr.flags & PT_FLAG_DETACH == 0 ) thread_child_parent_link( xp_parent , xp_child );
147
148        tm_end = hal_time_stamp();
149
150        thread_dmsg("\n[INFO] %s created thread %x for process %x in cluster %x\n"
151                "  start_cycle = %d / end_cycle = %d\n",
152                   trdid , process->pid , attr.cxy , tm_start , tm_end );
153        return 0;
154
155}  // end sys_thread_create()
156
157
Note: See TracBrowser for help on using the repository browser.