Ignore:
File:
1 edited

Legend:

Unmodified
Added
Removed
  • kernel/generic/src/proc/thread.c

    rdfa4be62 r597fa24  
    11/*
     2 * Copyright (c) 2025 Jiri Svoboda
    23 * Copyright (c) 2010 Jakub Jermar
    3  * Copyright (c) 2018 Jiri Svoboda
    44 * All rights reserved.
    55 *
     
    338338        irq_spinlock_unlock(&thread->task->lock, false);
    339339
    340         assert((atomic_get_unordered(&thread->state) == Exiting) || (atomic_get_unordered(&thread->state) == Lingering));
     340        assert((atomic_get_unordered(&thread->state) == Entering) ||
     341            (atomic_get_unordered(&thread->state) == Exiting) ||
     342            (atomic_get_unordered(&thread->state) == Lingering));
    341343
    342344        /* Clear cpu->fpu_owner if set to this thread. */
     
    665667void thread_usleep(uint32_t usec)
    666668{
    667         waitq_t wq;
    668 
    669         waitq_initialize(&wq);
    670 
     669        WAITQ_INITIALIZE(wq);
    671670        (void) waitq_sleep_timeout(&wq, usec);
    672671}
     
    944943
    945944/** Process syscall to create new thread.
    946  *
    947  */
    948 sys_errno_t sys_thread_create(uspace_ptr_uspace_arg_t uspace_uarg, uspace_ptr_char uspace_name,
    949     size_t name_len, uspace_ptr_thread_id_t uspace_thread_id)
     945 * The started thread will have initial pc and sp set to the exact values passed
     946 * to the syscall. The kernel will not touch any stack data below the stack
     947 * pointer, but some architectures may require some space to be available
     948 * for use above it. See userspace() in kernel, and <libarch/thread.h> in libc.
     949 *
     950 */
     951sys_errno_t sys_thread_create(sysarg_t pc, sysarg_t sp,
     952    uspace_ptr_char uspace_name, size_t name_len)
    950953{
    951954        if (name_len > THREAD_NAME_BUFLEN - 1)
     
    963966         * In case of success, kernel_uarg will be freed in uinit().
    964967         */
    965         uspace_arg_t *kernel_uarg =
    966             (uspace_arg_t *) malloc(sizeof(uspace_arg_t));
     968        uinit_arg_t *kernel_uarg = malloc(sizeof(uinit_arg_t));
    967969        if (!kernel_uarg)
    968970                return (sys_errno_t) ENOMEM;
    969971
    970         rc = copy_from_uspace(kernel_uarg, uspace_uarg, sizeof(uspace_arg_t));
    971         if (rc != EOK) {
    972                 free(kernel_uarg);
    973                 return (sys_errno_t) rc;
    974         }
     972        kernel_uarg->pc = pc;
     973        kernel_uarg->sp = sp;
     974
     975        // TODO: fix some unnecessary inconsistencies between architectures
    975976
    976977        thread_t *thread = thread_create(uinit, kernel_uarg, TASK,
    977978            THREAD_FLAG_USPACE | THREAD_FLAG_NOATTACH, namebuf);
    978         if (thread) {
    979                 if (uspace_thread_id) {
    980                         rc = copy_to_uspace(uspace_thread_id, &thread->tid,
    981                             sizeof(thread->tid));
    982                         if (rc != EOK) {
    983                                 /*
    984                                  * We have encountered a failure, but the thread
    985                                  * has already been created. We need to undo its
    986                                  * creation now.
    987                                  */
    988 
    989                                 /*
    990                                  * The new thread structure is initialized, but
    991                                  * is still not visible to the system.
    992                                  * We can safely deallocate it.
    993                                  */
    994                                 slab_free(thread_cache, thread);
    995                                 free(kernel_uarg);
    996 
    997                                 return (sys_errno_t) rc;
    998                         }
    999                 }
     979        if (!thread) {
     980                free(kernel_uarg);
     981                return (sys_errno_t) ENOMEM;
     982        }
    1000983
    1001984#ifdef CONFIG_UDEBUG
    1002                 /*
    1003                 * Generate udebug THREAD_B event and attach the thread.
    1004                 * This must be done atomically (with the debug locks held),
    1005                 * otherwise we would either miss some thread or receive
    1006                 * THREAD_B events for threads that already existed
    1007                 * and could be detected with THREAD_READ before.
    1008                 */
    1009                 udebug_thread_b_event_attach(thread, TASK);
     985        /*
     986        * Generate udebug THREAD_B event and attach the thread.
     987        * This must be done atomically (with the debug locks held),
     988        * otherwise we would either miss some thread or receive
     989        * THREAD_B events for threads that already existed
     990        * and could be detected with THREAD_READ before.
     991        */
     992        udebug_thread_b_event_attach(thread, TASK);
    1010993#else
    1011                 thread_attach(thread, TASK);
     994        thread_attach(thread, TASK);
    1012995#endif
    1013                 thread_start(thread);
    1014                 thread_put(thread);
    1015 
    1016                 return 0;
    1017         } else
    1018                 free(kernel_uarg);
    1019 
    1020         return (sys_errno_t) ENOMEM;
     996        thread_start(thread);
     997        thread_put(thread);
     998
     999        return (sys_errno_t) EOK;
    10211000}
    10221001
Note: See TracChangeset for help on using the changeset viewer.