mirror of
https://github.com/RT-Thread/rt-thread.git
synced 2026-08-20 21:29:42 +08:00
Add smp support to RT-Thread 4.0
This commit is contained in:
+16
@@ -8,6 +8,22 @@ config RT_NAME_MAX
|
||||
Each kernel object, such as thread, timer, semaphore etc, has a name,
|
||||
the RT_NAME_MAX is the maximal size of this object name.
|
||||
|
||||
config RT_USING_SMP
|
||||
bool "Enable SMP(Symmetric multiprocessing)"
|
||||
default n
|
||||
help
|
||||
This option should be selected by machines which have an SMP-
|
||||
capable CPU.
|
||||
The only effect of this option is to make the SMP-related
|
||||
options available to the user for configuration.
|
||||
|
||||
config RT_CPUS_NR
|
||||
int "Number of CPUs"
|
||||
default 2
|
||||
depends on RT_USING_SMP
|
||||
help
|
||||
Number of CPUs in the system
|
||||
|
||||
config RT_ALIGN_SIZE
|
||||
int "Alignment size for CPU architecture data access"
|
||||
default 4
|
||||
|
||||
@@ -12,12 +12,17 @@
|
||||
* 2010-05-20 Bernard fix the tick exceeds the maximum limits
|
||||
* 2010-07-13 Bernard fix rt_tick_from_millisecond issue found by kuronca
|
||||
* 2011-06-26 Bernard add rt_tick_set function.
|
||||
* 2018-11-22 Jesven add per cpu tick
|
||||
*/
|
||||
|
||||
#include <rthw.h>
|
||||
#include <rtthread.h>
|
||||
|
||||
#ifdef RT_USING_SMP
|
||||
#define rt_tick rt_cpu_index(0)->tick
|
||||
#else
|
||||
static rt_tick_t rt_tick = 0;
|
||||
#endif
|
||||
|
||||
extern void rt_timer_check(void);
|
||||
|
||||
@@ -71,7 +76,11 @@ void rt_tick_increase(void)
|
||||
struct rt_thread *thread;
|
||||
|
||||
/* increase the global tick */
|
||||
#ifdef RT_USING_SMP
|
||||
rt_cpu_self()->tick ++;
|
||||
#else
|
||||
++ rt_tick;
|
||||
#endif
|
||||
|
||||
/* check time slice */
|
||||
thread = rt_thread_self();
|
||||
|
||||
@@ -14,6 +14,7 @@
|
||||
* 2015-05-04 Bernard Rename it to components.c because compiling issue
|
||||
* in some IDEs.
|
||||
* 2015-07-29 Arda.Fu Add support to use RT_USING_USER_MAIN with IAR
|
||||
* 2018-11-22 Jesven Add secondary cpu boot up
|
||||
*/
|
||||
|
||||
#include <rthw.h>
|
||||
@@ -180,6 +181,9 @@ void main_thread_entry(void *parameter)
|
||||
/* RT-Thread components initialization */
|
||||
rt_components_init();
|
||||
|
||||
#ifdef RT_USING_SMP
|
||||
rt_hw_secondary_cpu_up();
|
||||
#endif
|
||||
/* invoke system main function */
|
||||
#if defined(__CC_ARM) || defined(__CLANG_ARM)
|
||||
$Super$$main(); /* for ARMCC. */
|
||||
@@ -243,6 +247,10 @@ int rtthread_startup(void)
|
||||
/* idle thread initialization */
|
||||
rt_thread_idle_init();
|
||||
|
||||
#ifdef RT_USING_SMP
|
||||
rt_hw_spin_lock(&_cpus_lock);
|
||||
#endif /*RT_USING_SMP*/
|
||||
|
||||
/* start scheduler */
|
||||
rt_system_scheduler_start();
|
||||
|
||||
|
||||
@@ -0,0 +1,91 @@
|
||||
/*
|
||||
* Copyright (c) 2006-2018, RT-Thread Development Team
|
||||
*
|
||||
* SPDX-License-Identifier: Apache-2.0
|
||||
*
|
||||
* Change Logs:
|
||||
* Date Author Notes
|
||||
* 2018-10-30 Bernard The first version
|
||||
*/
|
||||
|
||||
#include <rtthread.h>
|
||||
#include <rthw.h>
|
||||
|
||||
#ifdef RT_USING_SMP
|
||||
|
||||
static struct rt_cpu rt_cpus[RT_CPUS_NR];
|
||||
rt_hw_spinlock_t _cpus_lock;
|
||||
|
||||
/**
|
||||
* This fucntion will return current cpu.
|
||||
*/
|
||||
struct rt_cpu *rt_cpu_self(void)
|
||||
{
|
||||
return &rt_cpus[rt_hw_cpu_id()];
|
||||
}
|
||||
|
||||
struct rt_cpu *rt_cpu_index(int index)
|
||||
{
|
||||
return &rt_cpus[index];
|
||||
}
|
||||
|
||||
/**
|
||||
* This function will lock all cpus's scheduler and disable local irq.
|
||||
*/
|
||||
rt_base_t rt_cpus_lock(void)
|
||||
{
|
||||
rt_base_t level;
|
||||
struct rt_cpu* pcpu;
|
||||
|
||||
level = rt_hw_local_irq_disable();
|
||||
|
||||
pcpu = rt_cpu_self();
|
||||
if (pcpu->current_thread != RT_NULL)
|
||||
{
|
||||
if (pcpu->current_thread->cpus_lock_nest++ == 0)
|
||||
{
|
||||
pcpu->current_thread->scheduler_lock_nest++;
|
||||
rt_hw_spin_lock(&_cpus_lock);
|
||||
}
|
||||
}
|
||||
return level;
|
||||
}
|
||||
RTM_EXPORT(rt_cpus_lock);
|
||||
|
||||
/**
|
||||
* This function will restore all cpus's scheduler and restore local irq.
|
||||
*/
|
||||
void rt_cpus_unlock(rt_base_t level)
|
||||
{
|
||||
struct rt_cpu* pcpu = rt_cpu_self();
|
||||
|
||||
if (pcpu->current_thread != RT_NULL)
|
||||
{
|
||||
if (--pcpu->current_thread->cpus_lock_nest == 0)
|
||||
{
|
||||
pcpu->current_thread->scheduler_lock_nest--;
|
||||
rt_hw_spin_unlock(&_cpus_lock);
|
||||
}
|
||||
}
|
||||
rt_hw_local_irq_enable(level);
|
||||
}
|
||||
RTM_EXPORT(rt_cpus_unlock);
|
||||
|
||||
/**
|
||||
* This function is invoked by scheduler.
|
||||
* It will restore the lock state to whatever the thread's counter expects.
|
||||
* If target thread not locked the cpus then unlock the cpus lock.
|
||||
*/
|
||||
void rt_cpus_lock_status_restore(struct rt_thread *thread)
|
||||
{
|
||||
struct rt_cpu* pcpu = rt_cpu_self();
|
||||
|
||||
pcpu->current_thread = thread;
|
||||
if (!thread->cpus_lock_nest)
|
||||
{
|
||||
rt_hw_spin_unlock(&_cpus_lock);
|
||||
}
|
||||
}
|
||||
RTM_EXPORT(rt_post_switch);
|
||||
|
||||
#endif
|
||||
+51
-24
@@ -13,6 +13,8 @@
|
||||
* 2016-08-09 ArdaFu add method to get the handler of the idle thread.
|
||||
* 2018-02-07 Bernard lock scheduler to protect tid->cleanup.
|
||||
* 2018-07-14 armink add idle hook list
|
||||
* 2018-11-22 Jesven add per cpu idle task
|
||||
* combine the code of primary and secondary cpu
|
||||
*/
|
||||
|
||||
#include <rthw.h>
|
||||
@@ -28,6 +30,11 @@
|
||||
#endif
|
||||
#endif
|
||||
|
||||
#ifdef RT_USING_IDLE_HOOK
|
||||
#ifndef RT_IDEL_HOOK_LIST_SIZE
|
||||
#define RT_IDEL_HOOK_LIST_SIZE 4
|
||||
#endif
|
||||
|
||||
#ifndef IDLE_THREAD_STACK_SIZE
|
||||
#if defined (RT_USING_IDLE_HOOK) || defined(RT_USING_HEAP)
|
||||
#define IDLE_THREAD_STACK_SIZE 256
|
||||
@@ -36,18 +43,17 @@
|
||||
#endif
|
||||
#endif
|
||||
|
||||
static struct rt_thread idle;
|
||||
ALIGN(RT_ALIGN_SIZE)
|
||||
static rt_uint8_t rt_thread_stack[IDLE_THREAD_STACK_SIZE];
|
||||
#ifdef RT_USING_SMP
|
||||
#define _CPUS_NR RT_CPUS_NR
|
||||
#else
|
||||
#define _CPUS_NR 1
|
||||
#endif
|
||||
|
||||
extern rt_list_t rt_thread_defunct;
|
||||
|
||||
#ifdef RT_USING_IDLE_HOOK
|
||||
|
||||
#ifndef RT_IDEL_HOOK_LIST_SIZE
|
||||
#define RT_IDEL_HOOK_LIST_SIZE 4
|
||||
#endif
|
||||
|
||||
static struct rt_thread idle[_CPUS_NR];
|
||||
ALIGN(RT_ALIGN_SIZE)
|
||||
static rt_uint8_t rt_thread_stack[_CPUS_NR][IDLE_THREAD_STACK_SIZE];
|
||||
static void (*idle_hook_list[RT_IDEL_HOOK_LIST_SIZE])();
|
||||
|
||||
/**
|
||||
@@ -221,14 +227,21 @@ void rt_thread_idle_excute(void)
|
||||
|
||||
static void rt_thread_idle_entry(void *parameter)
|
||||
{
|
||||
#ifdef RT_USING_IDLE_HOOK
|
||||
rt_size_t i;
|
||||
#ifdef RT_USING_SMP
|
||||
if (rt_hw_cpu_id() != 0)
|
||||
{
|
||||
while (1)
|
||||
{
|
||||
rt_hw_secondary_cpu_idle_exec();
|
||||
}
|
||||
}
|
||||
#endif
|
||||
|
||||
while (1)
|
||||
{
|
||||
|
||||
#ifdef RT_USING_IDLE_HOOK
|
||||
rt_size_t i;
|
||||
|
||||
for (i = 0; i < RT_IDEL_HOOK_LIST_SIZE; i++)
|
||||
{
|
||||
if (idle_hook_list[i] != RT_NULL)
|
||||
@@ -251,18 +264,26 @@ static void rt_thread_idle_entry(void *parameter)
|
||||
*/
|
||||
void rt_thread_idle_init(void)
|
||||
{
|
||||
/* initialize thread */
|
||||
rt_thread_init(&idle,
|
||||
"tidle",
|
||||
rt_thread_idle_entry,
|
||||
RT_NULL,
|
||||
&rt_thread_stack[0],
|
||||
sizeof(rt_thread_stack),
|
||||
RT_THREAD_PRIORITY_MAX - 1,
|
||||
32);
|
||||
int i;
|
||||
char tidle_name[RT_NAME_MAX];
|
||||
|
||||
/* startup */
|
||||
rt_thread_startup(&idle);
|
||||
for (i = 0; i < _CPUS_NR; i++)
|
||||
{
|
||||
rt_sprintf(tidle_name, "tidle%d", i);
|
||||
rt_thread_init(&idle[i],
|
||||
tidle_name,
|
||||
rt_thread_idle_entry,
|
||||
RT_NULL,
|
||||
&rt_thread_stack[i][0],
|
||||
sizeof(rt_thread_stack[i]),
|
||||
RT_THREAD_PRIORITY_MAX - 1,
|
||||
32);
|
||||
#ifdef RT_USING_SMP
|
||||
rt_thread_control(&idle[i], RT_THREAD_CTRL_BIND_CPU, (void*)i);
|
||||
#endif
|
||||
/* startup */
|
||||
rt_thread_startup(&idle[i]);
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
@@ -273,5 +294,11 @@ void rt_thread_idle_init(void)
|
||||
*/
|
||||
rt_thread_t rt_thread_idle_gethandler(void)
|
||||
{
|
||||
return (rt_thread_t)(&idle);
|
||||
#ifdef RT_USING_SMP
|
||||
register int id = rt_hw_cpu_id();
|
||||
#else
|
||||
register int id = 0;
|
||||
#endif
|
||||
|
||||
return (rt_thread_t)(&idle[id]);
|
||||
}
|
||||
|
||||
@@ -8,6 +8,7 @@
|
||||
* 2006-02-24 Bernard first version
|
||||
* 2006-05-03 Bernard add IRQ_DEBUG
|
||||
* 2016-08-09 ArdaFu add interrupt enter and leave hook.
|
||||
* 2018-11-22 Jesven rt_interrupt_get_nest function add disable irq
|
||||
*/
|
||||
|
||||
#include <rthw.h>
|
||||
@@ -48,7 +49,11 @@ void rt_interrupt_leave_sethook(void (*hook)(void))
|
||||
|
||||
/**@{*/
|
||||
|
||||
#ifdef RT_USING_SMP
|
||||
#define rt_interrupt_nest rt_cpu_self()->irq_nest
|
||||
#else
|
||||
volatile rt_uint8_t rt_interrupt_nest;
|
||||
#endif
|
||||
|
||||
/**
|
||||
* This function will be invoked by BSP, when enter interrupt service routine
|
||||
@@ -102,7 +107,13 @@ RTM_EXPORT(rt_interrupt_leave);
|
||||
*/
|
||||
rt_uint8_t rt_interrupt_get_nest(void)
|
||||
{
|
||||
return rt_interrupt_nest;
|
||||
rt_uint8_t ret;
|
||||
rt_base_t level;
|
||||
|
||||
level = rt_hw_interrupt_disable();
|
||||
ret = rt_interrupt_nest;
|
||||
rt_hw_interrupt_enable(level);
|
||||
return ret;
|
||||
}
|
||||
RTM_EXPORT(rt_interrupt_get_nest);
|
||||
|
||||
|
||||
+573
-111
File diff suppressed because it is too large
Load Diff
+9
-3
@@ -6,6 +6,8 @@
|
||||
* Change Logs:
|
||||
* Date Author Notes
|
||||
* 2017/10/5 Bernard the first version
|
||||
* 2018/09/17 Jesven fix: in _signal_deliver RT_THREAD_STAT_MASK to RT_THREAD_STAT_SIGNAL_MASK
|
||||
* 2018/11/22 Jesven in smp version rt_hw_context_switch_to add a param
|
||||
*/
|
||||
|
||||
#include <stdint.h>
|
||||
@@ -60,10 +62,14 @@ static void _signal_entry(void *parameter)
|
||||
tid->sp = tid->sig_ret;
|
||||
tid->sig_ret = RT_NULL;
|
||||
|
||||
LOG_D("switch back to: 0x%08x", tid->sp);
|
||||
LOG_D("switch back to: 0x%08x\n", tid->sp);
|
||||
tid->stat &= ~RT_THREAD_STAT_SIGNAL;
|
||||
|
||||
rt_hw_context_switch_to((rt_uint32_t) & (tid->sp));
|
||||
#ifdef RT_USING_SMP
|
||||
rt_hw_context_switch_to((rt_ubase_t)&(tid->sp), tid);
|
||||
#else
|
||||
rt_hw_context_switch_to((rt_ubase_t)&(tid->sp));
|
||||
#endif /*RT_USING_SMP*/
|
||||
}
|
||||
|
||||
/*
|
||||
@@ -108,7 +114,7 @@ static void _signal_deliver(rt_thread_t tid)
|
||||
/* do signal action in self thread context */
|
||||
rt_thread_handle_sig(RT_TRUE);
|
||||
}
|
||||
else if (!((tid->stat & RT_THREAD_STAT_MASK) & RT_THREAD_STAT_SIGNAL))
|
||||
else if (!((tid->stat & RT_THREAD_STAT_SIGNAL_MASK) & RT_THREAD_STAT_SIGNAL))
|
||||
{
|
||||
/* add signal state */
|
||||
tid->stat |= RT_THREAD_STAT_SIGNAL;
|
||||
|
||||
+52
-40
@@ -23,18 +23,17 @@
|
||||
* 2012-12-29 Bernard fixed compiling warning.
|
||||
* 2016-08-09 ArdaFu add thread suspend and resume hook.
|
||||
* 2017-04-10 armink fixed the rt_thread_delete and rt_thread_detach
|
||||
bug when thread has not startup.
|
||||
* bug when thread has not startup.
|
||||
* 2018-11-22 Jesven yield is same to rt_schedule
|
||||
* add support for tasks bound to cpu
|
||||
*/
|
||||
|
||||
#include <rtthread.h>
|
||||
#include <rthw.h>
|
||||
#include <rtthread.h>
|
||||
|
||||
extern rt_list_t rt_thread_priority_table[RT_THREAD_PRIORITY_MAX];
|
||||
extern struct rt_thread *rt_current_thread;
|
||||
extern rt_list_t rt_thread_defunct;
|
||||
|
||||
#ifdef RT_USING_HOOK
|
||||
|
||||
static void (*rt_thread_suspend_hook)(rt_thread_t thread);
|
||||
static void (*rt_thread_resume_hook) (rt_thread_t thread);
|
||||
static void (*rt_thread_inited_hook) (rt_thread_t thread);
|
||||
@@ -84,7 +83,7 @@ void rt_thread_exit(void)
|
||||
register rt_base_t level;
|
||||
|
||||
/* get current thread */
|
||||
thread = rt_current_thread;
|
||||
thread = rt_thread_self();
|
||||
|
||||
/* disable interrupt */
|
||||
level = rt_hw_interrupt_disable();
|
||||
@@ -165,6 +164,16 @@ static rt_err_t _rt_thread_init(struct rt_thread *thread,
|
||||
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 */
|
||||
thread->scheduler_lock_nest = 0;
|
||||
thread->cpus_lock_nest = 0;
|
||||
#endif /*RT_USING_SMP*/
|
||||
|
||||
/* initialize cleanup function and user data */
|
||||
thread->cleanup = 0;
|
||||
thread->user_data = 0;
|
||||
@@ -251,7 +260,19 @@ 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
|
||||
}
|
||||
RTM_EXPORT(rt_thread_self);
|
||||
|
||||
@@ -449,36 +470,7 @@ RTM_EXPORT(rt_thread_delete);
|
||||
*/
|
||||
rt_err_t rt_thread_yield(void)
|
||||
{
|
||||
register rt_base_t level;
|
||||
struct rt_thread *thread;
|
||||
|
||||
/* disable interrupt */
|
||||
level = rt_hw_interrupt_disable();
|
||||
|
||||
/* set to current thread */
|
||||
thread = rt_current_thread;
|
||||
|
||||
/* if the thread stat is READY and on ready queue list */
|
||||
if ((thread->stat & RT_THREAD_STAT_MASK) == RT_THREAD_READY &&
|
||||
thread->tlist.next != thread->tlist.prev)
|
||||
{
|
||||
/* remove thread from thread list */
|
||||
rt_list_remove(&(thread->tlist));
|
||||
|
||||
/* put thread to end of ready queue */
|
||||
rt_list_insert_before(&(rt_thread_priority_table[thread->current_priority]),
|
||||
&(thread->tlist));
|
||||
|
||||
/* enable interrupt */
|
||||
rt_hw_interrupt_enable(level);
|
||||
|
||||
rt_schedule();
|
||||
|
||||
return RT_EOK;
|
||||
}
|
||||
|
||||
/* enable interrupt */
|
||||
rt_hw_interrupt_enable(level);
|
||||
rt_schedule();
|
||||
|
||||
return RT_EOK;
|
||||
}
|
||||
@@ -496,13 +488,14 @@ rt_err_t rt_thread_sleep(rt_tick_t tick)
|
||||
register rt_base_t temp;
|
||||
struct rt_thread *thread;
|
||||
|
||||
/* disable interrupt */
|
||||
temp = rt_hw_interrupt_disable();
|
||||
/* set to current thread */
|
||||
thread = rt_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);
|
||||
|
||||
/* disable interrupt */
|
||||
temp = rt_hw_interrupt_disable();
|
||||
|
||||
/* suspend thread */
|
||||
rt_thread_suspend(thread);
|
||||
|
||||
@@ -559,7 +552,8 @@ RTM_EXPORT(rt_thread_mdelay);
|
||||
* @param cmd the control command, which includes
|
||||
* RT_THREAD_CTRL_CHANGE_PRIORITY for changing priority level of thread;
|
||||
* RT_THREAD_CTRL_STARTUP for starting a thread;
|
||||
* RT_THREAD_CTRL_CLOSE for delete a thread.
|
||||
* RT_THREAD_CTRL_CLOSE for delete a thread;
|
||||
* RT_THREAD_CTRL_BIND_CPU for bind the thread to a CPU.
|
||||
* @param arg the argument of control command
|
||||
*
|
||||
* @return RT_EOK
|
||||
@@ -625,6 +619,22 @@ rt_err_t rt_thread_control(rt_thread_t thread, int cmd, void *arg)
|
||||
return rt_thread_delete(thread);
|
||||
#endif
|
||||
|
||||
#ifdef RT_USING_SMP
|
||||
case RT_THREAD_CTRL_BIND_CPU:
|
||||
{
|
||||
rt_uint8_t cpu;
|
||||
|
||||
if ((thread->stat & RT_THREAD_STAT_MASK) != RT_THREAD_INIT)
|
||||
{
|
||||
/* we only support bind cpu before started phase. */
|
||||
return RT_ERROR;
|
||||
}
|
||||
|
||||
cpu = (rt_uint8_t)(size_t)arg;
|
||||
thread->bind_cpu = cpu > RT_CPUS_NR? RT_CPUS_NR : cpu;
|
||||
break;
|
||||
}
|
||||
#endif /*RT_USING_SMP*/
|
||||
default:
|
||||
break;
|
||||
}
|
||||
@@ -661,6 +671,8 @@ rt_err_t rt_thread_suspend(rt_thread_t thread)
|
||||
return -RT_ERROR;
|
||||
}
|
||||
|
||||
RT_ASSERT(thread == rt_thread_self());
|
||||
|
||||
/* disable interrupt */
|
||||
temp = rt_hw_interrupt_disable();
|
||||
|
||||
|
||||
Reference in New Issue
Block a user