MCPcopy Index your code
hub / github.com/RT-Thread/rt-thread / sys_thread_create

Function sys_thread_create

components/lwp/lwp_syscall.c:3238–3332  ·  view source on GitHub ↗

* @brief Creates a new thread. * * This system call creates a new thread within the calling process. The newly created * thread starts executing the function specified by the `arg` argument, which typically * contains the necessary arguments or function pointer for the thread's execution. * * @param[in] arg An array of arguments that will be passed to the function executed *

Source from the content-addressed store, hash-verified

3236 * are unavailable.
3237 */
3238rt_thread_t sys_thread_create(void *arg[])
3239{
3240 void *user_stack = 0;
3241 struct rt_lwp *lwp = 0;
3242 rt_thread_t thread = RT_NULL;
3243 int tid = 0;
3244
3245 lwp = rt_thread_self()->lwp;
3246 lwp_ref_inc(lwp);
3247#ifdef ARCH_MM_MMU
3248 user_stack = lwp_map_user(lwp, 0, (size_t)arg[3], 0);
3249 if (!user_stack)
3250 {
3251 goto fail;
3252 }
3253 if ((tid = lwp_tid_get()) == 0)
3254 {
3255 goto fail;
3256 }
3257 thread = rt_thread_create((const char *)arg[0],
3258 _crt_thread_entry,
3259 (void *)arg[2],
3260 ALLOC_KERNEL_STACK_SIZE,
3261 (rt_uint8_t)(size_t)arg[4],
3262 (rt_uint32_t)(rt_size_t)arg[5]);
3263 if (!thread)
3264 {
3265 goto fail;
3266 }
3267
3268#ifdef RT_USING_SMP
3269 RT_SCHED_CTX(thread).bind_cpu = lwp->bind_cpu;
3270#endif
3271 thread->cleanup = lwp_cleanup;
3272 thread->user_entry = (void (*)(void *))arg[1];
3273 thread->user_stack = (void *)user_stack;
3274 thread->user_stack_size = (rt_size_t)arg[3];
3275#else
3276 rt_uint32_t kstack_size = (rt_uint32_t)arg[7];
3277 if (kstack_size < ALLOC_KERNEL_STACK_SIZE_MIN)
3278 {
3279 /* When kstack size is 0, the default size of the kernel stack is used */
3280 kstack_size = kstack_size ? ALLOC_KERNEL_STACK_SIZE_MIN : ALLOC_KERNEL_STACK_SIZE;
3281 }
3282 else if (kstack_size > ALLOC_KERNEL_STACK_SIZE_MAX)
3283 {
3284 kstack_size = ALLOC_KERNEL_STACK_SIZE_MAX;
3285 }
3286
3287 user_stack = (void *)arg[3];
3288 if ((!user_stack) || ((rt_uint32_t)arg[6] == RT_NULL))
3289 {
3290 goto fail;
3291 }
3292
3293 if ((tid = lwp_tid_get()) == 0)
3294 {
3295 goto fail;

Callers

nothing calls this directly

Calls 11

rt_thread_selfFunction · 0.85
lwp_ref_incFunction · 0.85
lwp_map_userFunction · 0.85
lwp_tid_getFunction · 0.85
rt_thread_createFunction · 0.85
rt_memsetFunction · 0.85
lwp_tid_set_threadFunction · 0.85
rt_thread_controlFunction · 0.85
rt_list_insert_afterFunction · 0.85
lwp_tid_putFunction · 0.85
lwp_ref_decFunction · 0.85

Tested by

no test coverage detected