/* * Copyright (c) 2006-2022, RT-Thread Development Team * * SPDX-License-Identifier: Apache-2.0 * */ #include #include #include #define DBG_TAG "kernel.thread" #define DBG_LVL DBG_INFO #include #ifndef __on_rt_thread_inited_hook #define __on_rt_thread_inited_hook(thread) __ON_HOOK_ARGS(rt_thread_inited_hook, (thread)) #endif #ifndef __on_rt_thread_suspend_hook #define __on_rt_thread_suspend_hook(thread) __ON_HOOK_ARGS(rt_thread_suspend_hook, (thread)) #endif #ifndef __on_rt_thread_resume_hook #define __on_rt_thread_resume_hook(thread) __ON_HOOK_ARGS(rt_thread_resume_hook, (thread)) #endif static void _thread_exit(void) { struct rt_thread *thread; rt_base_t level; /* get current thread */ LOG_D("line:%d thread:%s exit\n",__LINE__,rt_thread_self()->parent.name); thread = rt_thread_self(); rt_get_thread_struct(thread); rt_thread_defunct_enqueue(thread); level = rt_spin_lock_irqsave(&(thread->spinlock)); rt_timer_detach(&thread->thread_timer); /* insert to defunct thread list */ rt_spin_unlock_irqrestore(&(thread->spinlock), level); LOG_D("line:%d thread:%s exit\n",__LINE__,rt_thread_self()->parent.name); rt_put_thread_struct(thread); thread->stat = RT_THREAD_CLOSE; /* switch to next task */ rt_schedule(); } static void _thread_timeout(void *parameter) { struct rt_thread *thread; rt_base_t level; thread = (struct rt_thread *)parameter; /* parameter check */ RT_ASSERT(thread != RT_NULL); RT_ASSERT((thread->stat & RT_THREAD_SUSPEND_MASK) == RT_THREAD_SUSPEND_MASK); RT_ASSERT(rt_object_get_type((rt_object_t)thread) == RT_Object_Class_Thread); level = rt_spin_lock_irqsave(&(thread->spinlock)); /* set error number */ thread->error = -RT_ETIMEOUT; /* remove from suspend list */ rt_list_remove(&(thread->tlist)); rt_spin_unlock_irqrestore(&(thread->spinlock), level); /* insert to schedule ready list */ rt_schedule_insert_thread(thread); /* do schedule */ rt_schedule(); } static rt_err_t _thread_init(struct rt_thread *thread, const char *name, void (*entry)(void *parameter), void *parameter, void *stack_start, rt_uint32_t stack_size, rt_uint8_t priority, rt_uint32_t tick) { /* init thread list */ rt_list_init(&(thread->tlist)); rt_list_init(&(thread->tlist_schedule)); #ifdef RT_USING_MEM_PROTECTION thread->mem_regions = RT_NULL; #endif #ifdef RT_USING_SMART thread->wakeup.func = RT_NULL; #endif thread->entry = (void *)entry; thread->parameter = parameter; /* stack init */ thread->stack_addr = stack_start; thread->stack_size = stack_size; /* init thread stack */ rt_memset(thread->stack_addr, '#', thread->stack_size); #ifdef RT_USING_HW_STACK_GUARD rt_hw_stack_guard_init(thread); #endif #ifdef ARCH_CPU_STACK_GROWS_UPWARD thread->sp = (void *)rt_hw_stack_init(thread->entry, thread->parameter, (void *)((char *)thread->stack_addr), (void *)_thread_exit); #else thread->sp = (void *)rt_hw_stack_init(thread->entry, thread->parameter, (rt_uint8_t *)((char *)thread->stack_addr + thread->stack_size - sizeof(rt_ubase_t)), (void *)_thread_exit); #endif /* ARCH_CPU_STACK_GROWS_UPWARD */ /* priority init */ RT_ASSERT(priority < RT_THREAD_PRIORITY_MAX); thread->init_priority = priority; thread->current_priority = priority; thread->number_mask = 0; #ifdef RT_USING_MUTEX rt_list_init(&thread->taken_object_list); thread->pending_object = RT_NULL; #endif #ifdef RT_USING_EVENT thread->event_set = 0; thread->event_info = 0; #endif /* RT_USING_EVENT */ #if RT_THREAD_PRIORITY_MAX > 32 thread->number = 0; thread->high_mask = 0; #endif /* RT_THREAD_PRIORITY_MAX > 32 */ /* tick init */ rt_atomic_store(&thread->init_tick, tick); rt_atomic_store(&thread->remaining_tick, tick); /* error and flags */ thread->error = RT_EOK; thread->stat = RT_THREAD_INIT; #ifdef RT_USING_SMP /* not bind on any cpu */ thread->bind_cpu = RT_CPUS_NR; thread->oncpu = RT_CPU_DETACHED; /* lock init */ rt_atomic_store(&thread->cpus_lock_nest, 0); rt_atomic_store(&thread->critical_lock_nest, 0); #endif /* RT_USING_SMP */ /* initialize cleanup function and user data */ thread->cleanup = 0; thread->user_data = 0; /* initialize thread timer */ rt_timer_init(&(thread->thread_timer), thread->parent.name, _thread_timeout, thread, 0, RT_TIMER_FLAG_ONE_SHOT); /* initialize signal */ #ifdef RT_USING_SIGNALS thread->sig_mask = 0x00; thread->sig_pending = 0x00; #ifndef RT_USING_SMP thread->sig_ret = RT_NULL; #endif /* RT_USING_SMP */ thread->sig_vectors = RT_NULL; thread->si_list = RT_NULL; #endif /* RT_USING_SIGNALS */ #ifdef RT_USING_SMART thread->tid_ref_count = 0; thread->lwp = RT_NULL; thread->susp_recycler = RT_NULL; rt_list_init(&(thread->sibling)); /* lwp thread-signal init */ rt_memset(&thread->signal.sigset_mask, 0, sizeof(lwp_sigset_t)); rt_memset(&thread->signal.sig_queue.sigset_pending, 0, sizeof(lwp_sigset_t)); rt_list_init(&thread->signal.sig_queue.siginfo_list); rt_memset(&thread->user_ctx, 0, sizeof thread->user_ctx); /* initialize user_time and system_time */ thread->user_time = 0; thread->system_time = 0; #endif #ifdef RT_USING_CPU_USAGE thread->duration_tick = 0; #endif /* RT_USING_CPU_USAGE */ #ifdef RT_USING_PTHREADS thread->pthread_data = RT_NULL; #endif /* RT_USING_PTHREADS */ #ifdef RT_USING_MODULE thread->parent.module_id = 0; #endif /* RT_USING_MODULE */ rt_atomic_store(&thread->ref_count, 0); rt_spin_lock_init(&thread->spinlock); RT_OBJECT_HOOK_CALL(rt_thread_inited_hook, (thread)); return RT_EOK; } rt_err_t rt_thread_init(struct rt_thread *thread, const char *name, void (*entry)(void *parameter), void *parameter, void *stack_start, rt_uint32_t stack_size, rt_uint8_t priority, rt_uint32_t tick) { /* parameter check */ RT_ASSERT(thread != RT_NULL); RT_ASSERT(stack_start != RT_NULL); /* initialize thread object */ rt_object_init((rt_object_t)thread, RT_Object_Class_Thread, name); return _thread_init(thread, name, entry, parameter, stack_start, stack_size, priority, tick); } RTM_EXPORT(rt_thread_init); rt_thread_t rt_thread_self(void) { #ifdef RT_USING_SMP rt_base_t lock; rt_thread_t self; lock = rt_hw_local_irq_disable(); self = rt_cpu_self()->current_thread; rt_hw_local_irq_enable(lock); return self; #else extern rt_thread_t rt_current_thread; return rt_current_thread; #endif /* RT_USING_SMP */ } RTM_EXPORT(rt_thread_self); rt_err_t rt_thread_startup(rt_thread_t thread) { /* parameter check */ RT_ASSERT(thread != RT_NULL); RT_ASSERT((thread->stat & RT_THREAD_STAT_MASK) == RT_THREAD_INIT); RT_ASSERT(rt_object_get_type((rt_object_t)thread) == RT_Object_Class_Thread); /* calculate priority attribute */ #if RT_THREAD_PRIORITY_MAX > 32 thread->number = thread->current_priority >> 3; /* 5bit */ thread->number_mask = 1L << thread->number; thread->high_mask = 1L << (thread->current_priority & 0x07); /* 3bit */ #else thread->number_mask = 1L << thread->current_priority; #endif /* RT_THREAD_PRIORITY_MAX > 32 */ LOG_D("startup a thread:%s with priority:%d", thread->parent.name, thread->current_priority); /* change thread stat */ thread->stat = RT_THREAD_SUSPEND; /* then resume it */ rt_thread_resume(thread); if (rt_thread_self() != RT_NULL) { /* do a scheduling */ rt_schedule(); } return RT_EOK; } RTM_EXPORT(rt_thread_startup); rt_err_t rt_thread_sleep(rt_tick_t tick) { rt_base_t level, level_local; struct rt_thread *thread; int err; if (tick == 0) { return -RT_EINVAL; } /* set to current thread */ thread = rt_thread_self(); RT_ASSERT(thread != RT_NULL); RT_ASSERT(rt_object_get_type((rt_object_t)thread) == RT_Object_Class_Thread); /* current context checking */ RT_DEBUG_SCHEDULER_AVAILABLE(RT_TRUE); /* reset thread error */ thread->error = RT_EOK; level_local = rt_hw_local_irq_disable(); /* suspend thread */ err = rt_thread_suspend_with_flag(thread, RT_INTERRUPTIBLE); level = rt_spin_lock_irqsave(&(thread->spinlock)); /* reset the timeout of thread timer and start it */ if (err == RT_EOK) { rt_timer_control(&(thread->thread_timer), RT_TIMER_CTRL_SET_TIME, &tick); rt_timer_start(&(thread->thread_timer)); /* enable interrupt */ rt_spin_unlock_irqrestore(&(thread->spinlock), level); rt_hw_local_irq_enable(level_local); thread->error = -RT_EINTR; rt_schedule(); /* clear error number of this thread to RT_EOK */ if (thread->error == -RT_ETIMEOUT) thread->error = RT_EOK; } else { rt_spin_unlock_irqrestore(&(thread->spinlock), level); rt_hw_local_irq_enable(level_local); } return err; } rt_err_t rt_thread_delay(rt_tick_t tick) { return rt_thread_sleep(tick); } RTM_EXPORT(rt_thread_delay); static void rt_thread_set_suspend_state(struct rt_thread *thread, int suspend_flag) { rt_uint8_t stat = RT_THREAD_SUSPEND_UNINTERRUPTIBLE; RT_ASSERT(thread != RT_NULL); switch (suspend_flag) { case RT_INTERRUPTIBLE: stat = RT_THREAD_SUSPEND_INTERRUPTIBLE; break; case RT_KILLABLE: stat = RT_THREAD_SUSPEND_KILLABLE; break; case RT_UNINTERRUPTIBLE: stat = RT_THREAD_SUSPEND_UNINTERRUPTIBLE; break; default: RT_ASSERT(0); break; } thread->stat = stat | (thread->stat & ~RT_THREAD_STAT_MASK); } rt_err_t rt_thread_suspend_with_flag(rt_thread_t thread, int suspend_flag) { rt_base_t stat; rt_base_t level; /* parameter check */ RT_ASSERT(thread != RT_NULL); RT_ASSERT(rt_object_get_type((rt_object_t)thread) == RT_Object_Class_Thread); RT_ASSERT(thread == rt_thread_self()); LOG_D("thread suspend: %s", thread->parent.name); level = rt_spin_lock_irqsave(&(thread->spinlock)); stat = thread->stat & RT_THREAD_STAT_MASK; if ((stat != RT_THREAD_READY) && (stat != RT_THREAD_RUNNING)) { LOG_D("thread suspend: thread disorder, 0x%2x", thread->stat); rt_spin_unlock_irqrestore(&(thread->spinlock), level); return -RT_ERROR; } if (stat == RT_THREAD_RUNNING) { /* not suspend running status thread on other core */ RT_ASSERT(thread == rt_thread_self()); } #ifdef RT_USING_SMART if (lwp_thread_signal_suspend_check(thread, suspend_flag) == 0) { /* not to suspend */ rt_spin_unlock_irqrestore(&(thread->spinlock), level); return -RT_EINTR; } #endif rt_spin_unlock_irqrestore(&(thread->spinlock), level); rt_schedule_remove_thread(thread); level = rt_spin_lock_irqsave(&(thread->spinlock)); rt_thread_set_suspend_state(thread, suspend_flag); /* stop thread timer anyway */ rt_timer_stop(&(thread->thread_timer)); rt_spin_unlock_irqrestore(&(thread->spinlock), level); RT_OBJECT_HOOK_CALL(rt_thread_suspend_hook, (thread)); return RT_EOK; } RTM_EXPORT(rt_thread_suspend_with_flag); rt_err_t rt_thread_suspend(rt_thread_t thread) { return rt_thread_suspend_with_flag(thread, RT_UNINTERRUPTIBLE); } RTM_EXPORT(rt_thread_suspend); rt_err_t rt_thread_resume(rt_thread_t thread) { rt_base_t level; /* parameter check */ RT_ASSERT(thread != RT_NULL); RT_ASSERT(rt_object_get_type((rt_object_t)thread) == RT_Object_Class_Thread); LOG_D("thread resume: %s", thread->parent.name); level = rt_spin_lock_irqsave(&(thread->spinlock)); //TODO need lock for cpu if ((thread->stat & RT_THREAD_SUSPEND_MASK) != RT_THREAD_SUSPEND_MASK) { rt_spin_unlock_irqrestore(&(thread->spinlock), level); LOG_D("thread resume: thread disorder, %d", thread->stat); return -RT_ERROR; } /* remove from suspend list */ rt_list_remove(&(thread->tlist)); rt_timer_stop(&thread->thread_timer); #ifdef RT_USING_SMART thread->wakeup.func = RT_NULL; #endif rt_spin_unlock_irqrestore(&(thread->spinlock), level); /* insert to schedule ready list */ rt_schedule_insert_thread(thread); RT_OBJECT_HOOK_CALL(rt_thread_resume_hook, (thread)); return RT_EOK; } RTM_EXPORT(rt_thread_resume);