44#define DBG_TAG "kernel.thread"
45#define DBG_LVL DBG_INFO
48#if defined(RT_USING_HOOK) && defined(RT_HOOK_USING_FUNC_PTR)
49static void (*rt_thread_suspend_hook)(
rt_thread_t thread);
50static void (*rt_thread_resume_hook) (
rt_thread_t thread);
61 rt_thread_suspend_hook = hook;
73 rt_thread_resume_hook = hook;
80static void _thread_detach_from_mutex(
rt_thread_t thread)
114static void _thread_detach_from_mutex(
rt_thread_t thread) {}
117static void _thread_exit(
void)
129 _thread_detach_from_mutex(thread);
146static void _thread_timeout(
void *
parameter)
166 thread->
error = -RT_ETIMEOUT;
171 rt_sched_insert_thread(thread);
189#ifdef RT_USING_MEM_PROTECTION
194 thread->wakeup_handle.func =
RT_NULL;
206#ifdef RT_USING_HW_STACK_GUARD
207 rt_hw_stack_guard_init(thread);
209#ifdef ARCH_CPU_STACK_GROWS_UPWARD
212 (
void *)_thread_exit);
216 (
void *)_thread_exit);
230 thread->
error = RT_EOK;
250#ifdef RT_USING_SIGNALS
251 thread->sig_mask = 0x00;
252 thread->sig_pending = 0x00;
262 thread->tid_ref_count = 0;
264 thread->susp_recycler =
RT_NULL;
269 rt_memset(&thread->signal.sigset_mask, 0,
sizeof(lwp_sigset_t));
270 rt_memset(&thread->signal.sig_queue.sigset_pending, 0,
sizeof(lwp_sigset_t));
273 rt_memset(&thread->user_ctx, 0,
sizeof thread->user_ctx);
276 thread->user_time = 0;
277 thread->system_time = 0;
280#ifdef RT_USING_CPU_USAGE
281 thread->duration_tick = 0;
284#ifdef RT_USING_PTHREADS
285 thread->pthread_data =
RT_NULL;
288#ifdef RT_USING_MODULE
343 rt_memset(thread, 0x0,
sizeof(
struct rt_thread));
348 return _thread_init(thread,
369#elif defined (ARCH_USING_HW_THREAD_SELF)
370 return rt_hw_thread_self();
400 LOG_D(
"startup a thread:%s with priority:%d",
401 thread->
parent.
name, thread->current_priority);
404 rt_sched_thread_startup(thread);
442 rt_sched_remove_thread(thread);
477 return _thread_detach(thread);
494 _thread_detach_from_mutex(thread);
579 return _thread_detach(thread);
634 thread->
error = RT_EOK;
648 thread->
error = -RT_EINTR;
657 if (thread->
error == -RT_ETIMEOUT)
658 thread->
error = RT_EOK;
678 return _thread_sleep(tick);
706 thread->
error = RT_EOK;
712 if (cur_tick - *tick < inc_tick)
717 left_tick = *tick - cur_tick;
731 if (thread->
error == -RT_ETIMEOUT)
733 thread->
error = RT_EOK;
742 return thread->
error;
760 return _thread_sleep(tick);
833 return rt_sched_thread_bind_cpu(thread, cpu);
845#include <lwp_signal.h>
848static void _thread_set_suspend_state(
struct rt_thread *thread,
int suspend_flag)
853 switch (suspend_flag)
914 LOG_D(
"thread suspend: thread disorder, 0x%2x", thread->stat);
931 if (lwp_thread_signal_suspend_check(thread, suspend_flag) == 0)
953 rt_sched_remove_thread(thread);
954 _thread_set_suspend_state(thread, suspend_flag);
1034 if (
error == -RT_ESCHEDLOCKED)
1050#ifdef RT_USING_SMART
1062 rt_wakeup_func_t func =
RT_NULL;
1068 func = thread->wakeup_handle.func;
1069 thread->wakeup_handle.func =
RT_NULL;
1074 ret = func(thread->wakeup_handle.
user_data, thread);
1084void rt_thread_wakeup_set(
struct rt_thread *thread, rt_wakeup_func_t func,
void*
user_data)
1092 thread->wakeup_handle.func = func;
struct rt_cpu * rt_cpu_self(void)
This fucntion will return current cpu object.
rt_err_t rt_timer_detach(rt_timer_t timer)
This function will detach a timer from timer management.
rt_err_t rt_timer_control(rt_timer_t timer, int cmd, void *arg)
This function will get or set some options of the timer
rt_err_t rt_timer_start(rt_timer_t timer)
This function will start the timer
void rt_timer_init(rt_timer_t timer, const char *name, void(*timeout)(void *parameter), void *parameter, rt_tick_t time, rt_uint8_t flag)
This function will initialize a timer normally this function is used to initialize a static timer obj...
rt_tick_t rt_tick_get(void)
This function will return current tick from operating system startup.
#define RT_TIMER_CTRL_SET_TIME
rt_tick_t rt_tick_from_millisecond(rt_int32_t ms)
This function will calculate the tick from millisecond.
#define RT_TIMER_FLAG_THREAD_TIMER
#define RT_TIMER_FLAG_ONE_SHOT
int stat(const char *file, struct stat *buf)
void rt_thread_defunct_enqueue(rt_thread_t thread)
Enqueue a thread to defunct queue.
rt_base_t rt_spin_lock_irqsave(struct rt_spinlock *lock)
This function will disable the local interrupt and then lock the spinlock, will lock the thread sched...
void rt_spin_lock_init(struct rt_spinlock *lock)
Initialize a static spinlock object.
rt_err_t rt_susp_list_enqueue(rt_list_t *susp_list, rt_thread_t thread, int ipc_flags)
Add a thread to the suspend list
void rt_spin_unlock_irqrestore(struct rt_spinlock *lock, rt_base_t level)
This function will unlock the spinlock and then restore current cpu interrupt status,...
void rt_object_delete(rt_object_t object)
This function will delete an object and release object memory.
void rt_object_init(struct rt_object *object, enum rt_object_class_type type, const char *name)
This function will initialize an object and add it to object system management.
rt_bool_t rt_object_is_systemobject(rt_object_t object)
This function will judge the object is system object or not.
#define RT_OBJECT_HOOK_CALL(func, argv)
rt_err_t rt_object_get_name(rt_object_t object, char *name, rt_uint8_t name_size)
This function will return the name of the specified object container
rt_object_t rt_object_allocate(enum rt_object_class_type type, const char *name)
This function will allocate an object from object system.
rt_object_t rt_object_find(const char *name, rt_uint8_t type)
This function will find specified name object from object container.
#define RT_OBJECT_HOOKLIST_CALL(name, argv)
rt_uint8_t rt_object_get_type(rt_object_t object)
This function will return the type of object without RT_Object_Class_Static flag.
struct rt_object * rt_object_t
#define RT_OBJECT_HOOKLIST_DEFINE(name)
rt_inline void rt_list_remove(rt_list_t *n)
remove node from list.
#define rt_list_entry(node, type, member)
get the struct for this entry
#define rt_list_for_each_safe(pos, n, head)
rt_inline void rt_list_init(rt_list_t *l)
initialize a list
#define RT_DEBUG_SCHEDULER_AVAILABLE(need_check)
rt_err_t rt_thread_startup(rt_thread_t thread)
This function will start a thread and put it to system ready queue.
rt_err_t rt_thread_close(rt_thread_t thread)
This function will close a thread. The thread object will be removed from thread queue and detached/d...
rt_err_t rt_thread_get_name(rt_thread_t thread, char *name, rt_uint8_t name_size)
This function will return the name of the specified thread
void rt_exit_critical_safe(rt_base_t critical_level)
rt_err_t rt_thread_delay_until(rt_tick_t *tick, rt_tick_t inc_tick)
This function will let current thread delay until (*tick + inc_tick).
#define RT_THREAD_CTRL_BIND_CPU
rt_err_t rt_thread_control(rt_thread_t thread, int cmd, void *arg)
This function will control thread behaviors according to control command.
#define RT_THREAD_STAT_MASK
rt_err_t rt_thread_suspend_to_list(rt_thread_t thread, rt_list_t *susp_list, int ipc_flags, int suspend_flag)
This function will suspend the specified thread and change it to suspend state.
rt_err_t rt_thread_mdelay(rt_int32_t ms)
This function will let current thread delay for some milliseconds.
rt_err_t rt_thread_detach(rt_thread_t thread)
This function will detach a thread. The thread object will be removed from thread queue and detached/...
#define RT_THREAD_SUSPEND_INTERRUPTIBLE
rt_err_t rt_thread_suspend_with_flag(rt_thread_t thread, int suspend_flag)
This function will suspend the specified thread and change it to suspend state.
rt_err_t rt_thread_yield(void)
This function will let current thread yield processor, and scheduler will choose the highest thread t...
rt_thread_t rt_thread_self(void)
This function will return self thread object.
rt_err_t rt_thread_delay(rt_tick_t tick)
This function will let current thread delay for some ticks.
rt_err_t rt_thread_delete(rt_thread_t thread)
This function will delete a thread. The thread object will be removed from thread queue and deleted f...
#define RT_THREAD_SUSPEND_UNINTERRUPTIBLE
#define RT_THREAD_SUSPEND_KILLABLE
#define RT_THREAD_RUNNING
rt_thread_t rt_thread_find(char *name)
This function will find the specified thread.
void rt_thread_suspend_sethook(void(*hook)(rt_thread_t thread))
rt_err_t rt_thread_resume(rt_thread_t thread)
This function will resume a thread and put it to system ready queue.
rt_err_t rt_thread_suspend(rt_thread_t thread)
rt_base_t rt_enter_critical(void)
This function will lock the thread scheduler.
rt_thread_t rt_thread_create(const char *name, void(*entry)(void *parameter), void *parameter, rt_uint32_t stack_size, rt_uint8_t priority, rt_uint32_t tick)
This function will create a thread object and allocate thread object memory. and stack.
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)
This function will initialize a thread. It's used to initialize a static thread object.
#define RT_THREAD_CTRL_CHANGE_PRIORITY
struct rt_thread * rt_thread_t
#define RT_THREAD_CTRL_CLOSE
void rt_thread_resume_sethook(void(*hook)(rt_thread_t thread))
#define RT_THREAD_CTRL_STARTUP
void rt_schedule(void)
This function will perform one scheduling. It will select one thread with the highest priority level ...
rt_uint16_t rt_critical_level(void)
Get the scheduler lock level.
void rt_mutex_drop_thread(rt_mutex_t mutex, rt_thread_t thread)
drop a thread from the suspend list of mutex
rt_err_t rt_mutex_release(rt_mutex_t mutex)
This function will release a mutex. If there is thread suspended on the mutex, the thread will be res...
#define rt_atomic_store(ptr, v)
#define RT_KERNEL_MALLOC(sz)
#define rt_hw_local_irq_disable
rt_uint8_t * rt_hw_stack_init(void *entry, void *parameter, rt_uint8_t *stack_addr, void *exit)
#define rt_hw_local_irq_enable
#define RTM_EXPORT(symbol)
#define RT_SCHED_CTX(thread)
#define RT_THREAD_LIST_NODE(thread)
rt_ubase_t rt_sched_lock_level_t
struct rt_list_node rt_list_t
void rt_sched_thread_init_ctx(struct rt_thread *thread, rt_uint32_t tick, rt_uint8_t priority)
rt_uint8_t rt_sched_thread_get_stat(struct rt_thread *thread)
rt_err_t rt_sched_thread_timer_stop(struct rt_thread *thread)
rt_err_t rt_sched_thread_change_priority(struct rt_thread *thread, rt_uint8_t priority)
Update priority of the target thread
rt_err_t rt_sched_thread_close(struct rt_thread *thread)
rt_err_t rt_sched_thread_ready(struct rt_thread *thread)
rt_uint8_t rt_sched_thread_is_suspended(struct rt_thread *thread)
rt_err_t rt_sched_thread_yield(struct rt_thread *thread)
rt_err_t rt_sched_lock(rt_sched_lock_level_t *plvl)
rt_err_t rt_sched_unlock(rt_sched_lock_level_t level)
rt_err_t rt_sched_unlock_n_resched(rt_sched_lock_level_t level)
struct rt_thread * current_thread
struct rt_ipc_object parent
struct rt_spinlock spinlock
rt_object_t pending_object
rt_thread_cleanup_t cleanup
RT_SCHED_THREAD_CTX struct rt_timer thread_timer
rt_list_t taken_object_list