* @brief Create a new lightweight process (LWP) * * @param[in] flags Creation flags that control LWP behavior * - LWP_CREATE_FLAG_NOTRACE_EXEC: Don't trace exec operations * - LWP_CREATE_FLAG_ALLOC_PID: Allocate a PID for the LWP * - LWP_CREATE_FLAG_INIT_USPACE: Initialize user space * * @return Pointer to newly created LWP structure on success * - RT_NULL on fa
| 626 | * - RT_NULL on failure (pid allocation failed or user space init failed) |
| 627 | */ |
| 628 | rt_lwp_t lwp_create(rt_base_t flags) |
| 629 | { |
| 630 | pid_t pid; |
| 631 | rt_lwp_t new_lwp = rt_calloc(1, sizeof(struct rt_lwp)); |
| 632 | |
| 633 | if (new_lwp) |
| 634 | { |
| 635 | /* minimal setup of lwp object */ |
| 636 | new_lwp->ref = 1; |
| 637 | #ifdef RT_USING_SMP |
| 638 | new_lwp->bind_cpu = RT_CPUS_NR; |
| 639 | #endif |
| 640 | new_lwp->exe_file = RT_NULL; |
| 641 | rt_list_init(&new_lwp->t_grp); |
| 642 | rt_list_init(&new_lwp->pgrp_node); |
| 643 | rt_list_init(&new_lwp->timer); |
| 644 | lwp_user_object_lock_init(new_lwp); |
| 645 | rt_wqueue_init(&new_lwp->wait_queue); |
| 646 | rt_wqueue_init(&new_lwp->waitpid_waiters); |
| 647 | lwp_signal_init(&new_lwp->signal); |
| 648 | rt_mutex_init(&new_lwp->lwp_lock, "lwp_lock", RT_IPC_FLAG_PRIO); |
| 649 | |
| 650 | if (flags & LWP_CREATE_FLAG_NOTRACE_EXEC) |
| 651 | new_lwp->did_exec = RT_TRUE; |
| 652 | |
| 653 | /* lwp with pid */ |
| 654 | if (flags & LWP_CREATE_FLAG_ALLOC_PID) |
| 655 | { |
| 656 | lwp_pid_lock_take(); |
| 657 | pid = lwp_pid_get_locked(); |
| 658 | if (pid == 0) |
| 659 | { |
| 660 | lwp_user_object_lock_destroy(new_lwp); |
| 661 | rt_free(new_lwp); |
| 662 | new_lwp = RT_NULL; |
| 663 | LOG_E("%s: pid slot fulled", __func__); |
| 664 | } |
| 665 | else |
| 666 | { |
| 667 | new_lwp->pid = pid; |
| 668 | lwp_pid_set_lwp_locked(pid, new_lwp); |
| 669 | } |
| 670 | lwp_pid_lock_release(); |
| 671 | } |
| 672 | rt_memset(&new_lwp->rt_rusage,0, sizeof(new_lwp->rt_rusage)); |
| 673 | |
| 674 | if (flags & LWP_CREATE_FLAG_INIT_USPACE) |
| 675 | { |
| 676 | rt_err_t error = lwp_user_space_init(new_lwp, 0); |
| 677 | if (error) |
| 678 | { |
| 679 | lwp_pid_put(new_lwp); |
| 680 | lwp_user_object_lock_destroy(new_lwp); |
| 681 | rt_free(new_lwp); |
| 682 | new_lwp = RT_NULL; |
| 683 | LOG_E("%s: failed to initialize user space", __func__); |
| 684 | } |
| 685 | } |