This commit is contained in:
zhujiale 2024-09-12 18:25:10 +08:00 committed by Rbb666
parent d468b93657
commit 8ce405f739
5 changed files with 61 additions and 61 deletions

View File

@ -5,6 +5,6 @@ src = []
if GetDepend("RT_USING_SMP"): if GetDepend("RT_USING_SMP"):
src += Glob('*.c') src += Glob('*.c')
CPPPATH = [cwd] CPPPATH = [cwd]
group = DefineGroup('mprotect', src, depend = [''], CPPPATH = CPPPATH) group = DefineGroup('smp', src, depend = [''], CPPPATH = CPPPATH)
Return('group') Return('group')

View File

@ -14,15 +14,15 @@
#define DBG_LVL DBG_INFO #define DBG_LVL DBG_INFO
#include <rtdbg.h> #include <rtdbg.h>
struct smp_call global_work[RT_CPUS_NR]; struct rt_smp_call rt_smp_work[RT_CPUS_NR];
rt_atomic_t wait; rt_atomic_t rt_smp_wait;
rt_err_t smp_call_handler(struct smp_event *event) rt_err_t smp_call_handler(struct rt_smp_event *event)
{ {
switch(event->event_id) switch (event->event_id)
{ {
case SMP_CALL_EVENT_FUNC: case SMP_CALL_EVENT_FUNC:
event->func(event->data); event->func(event->data);
rt_atomic_add(&wait,1); rt_atomic_add(&rt_smp_wait, 1);
break; break;
default: default:
LOG_E("error event id\n"); LOG_E("error event id\n");
@ -34,20 +34,20 @@ void rt_smp_call_ipi_handler(int vector, void *param)
{ {
int err; int err;
int cur_cpu = rt_hw_cpu_id(); int cur_cpu = rt_hw_cpu_id();
rt_spin_lock(&global_work[cur_cpu].lock); rt_spin_lock(&rt_smp_work[cur_cpu].lock);
if(global_work[cur_cpu].event.event_id)
if (rt_smp_work[cur_cpu].event.event_id)
{ {
err = smp_call_handler(&global_work[cur_cpu].event); err = smp_call_handler(&rt_smp_work[cur_cpu].event);
if(err) if (err)
{ {
LOG_E("Have no event\n"); LOG_E("Have no event\n");
rt_memset(&global_work[cur_cpu].event,0,sizeof(struct smp_event)); rt_memset(&rt_smp_work[cur_cpu].event, 0, sizeof(struct rt_smp_event));
rt_spin_unlock(&global_work[cur_cpu].lock); rt_spin_unlock(&rt_smp_work[cur_cpu].lock);
} }
rt_memset(&global_work[cur_cpu].event,0,sizeof(struct smp_event)); rt_memset(&rt_smp_work[cur_cpu].event, 0, sizeof(struct rt_smp_event));
} }
rt_spin_unlock(&global_work[cur_cpu].lock); rt_spin_unlock(&rt_smp_work[cur_cpu].lock);
} }
/** /**
@ -61,48 +61,48 @@ void rt_smp_call_ipi_handler(int vector, void *param)
* else it will call function on specified CPU and return immediately * else it will call function on specified CPU and return immediately
* @param cond the condition function pointer,if you set it then it will call function only when cond return true * @param cond the condition function pointer,if you set it then it will call function only when cond return true
*/ */
void rt_smp_call_func_cond(int cpu_mask, smp_call_func_back func, void *data,rt_uint8_t flag,smp_cond cond) void rt_smp_call_func_cond(int cpu_mask, smp_call_func_back func, void *data, rt_uint8_t flag, smp_cond cond)
{ {
RT_DEBUG_NOT_IN_INTERRUPT; RT_DEBUG_NOT_IN_INTERRUPT;
struct smp_event event; struct rt_smp_event event;
rt_bool_t need_call = RT_TRUE,need_wait = RT_FALSE; rt_bool_t need_call = RT_TRUE, need_wait = RT_FALSE;
int cur_cpu = rt_hw_cpu_id(); int cur_cpu = rt_hw_cpu_id();
int cpuid = 1 << cur_cpu; int cpuid = 1 << cur_cpu;
int tmp_id = 0,cpu_nr = 0; int tmp_id = 0, cpu_nr = 0;
int tmp_mask; int tmp_mask;
if(flag == SMP_CALL_WAIT_ALL) if (flag == SMP_CALL_WAIT_ALL)
{ {
need_wait = RT_TRUE; need_wait = RT_TRUE;
rt_atomic_store(&wait,0); rt_atomic_store(&rt_smp_wait, 0);
} }
if(cpuid & cpu_mask) if (cpuid & cpu_mask)
{ {
func(data); func(data);
cpu_mask = cpu_mask & (~cpuid); cpu_mask = cpu_mask & (~cpuid);
} }
if(!cpu_mask) if (!cpu_mask)
need_call = RT_FALSE; need_call = RT_FALSE;
tmp_mask = cpu_mask; tmp_mask = cpu_mask;
if(need_call) if (need_call)
{ {
while(tmp_mask) while (tmp_mask)
{ {
if((tmp_mask & 1) && (tmp_id < RT_CPUS_NR)) if ((tmp_mask & 1) && (tmp_id < RT_CPUS_NR))
{ {
if(cond && !cond(tmp_id,data)) if (cond && !cond(tmp_id, data))
continue; continue;
cpu_nr++; cpu_nr++;
event.event_id = SMP_CALL_EVENT_FUNC; event.event_id = SMP_CALL_EVENT_FUNC;
event.func = func; event.func = func;
event.data = data; event.data = data;
event.cpu_mask = cpu_mask; event.cpu_mask = cpu_mask;
rt_spin_lock(&global_work[tmp_id].lock); rt_spin_lock(&rt_smp_work[tmp_id].lock);
global_work[tmp_id].event = event; rt_smp_work[tmp_id].event = event;
rt_spin_unlock(&global_work[tmp_id].lock); rt_spin_unlock(&rt_smp_work[tmp_id].lock);
} }
tmp_id++; tmp_id++;
tmp_mask = tmp_mask >> 1; tmp_mask = tmp_mask >> 1;
@ -110,36 +110,36 @@ void rt_smp_call_func_cond(int cpu_mask, smp_call_func_back func, void *data,rt_
rt_hw_ipi_send(RT_FUNC_IPI, cpu_mask); rt_hw_ipi_send(RT_FUNC_IPI, cpu_mask);
} }
if(need_wait) if (need_wait)
{ {
while(rt_atomic_load(&wait) != cpu_nr); while (rt_atomic_load(&rt_smp_wait) != cpu_nr);
} }
} }
void rt_call_each_cpu(smp_call_func_back func, void *data,rt_uint8_t flag) void rt_smp_call_each_cpu(smp_call_func_back func, void *data, rt_uint8_t flag)
{ {
rt_smp_call_func_cond(RT_ALL_CPU,func,data,flag,RT_NULL); rt_smp_call_func_cond(RT_ALL_CPU, func, data, flag, RT_NULL);
} }
void rt_call_each_cpu_cond(smp_call_func_back func, void *data,rt_uint8_t flag,smp_cond cond_func) void rt_smp_call_each_cpu_cond(smp_call_func_back func, void *data, rt_uint8_t flag, smp_cond cond_func)
{ {
rt_smp_call_func_cond(RT_ALL_CPU,func,data,flag,cond_func); rt_smp_call_func_cond(RT_ALL_CPU, func, data, flag, cond_func);
} }
void rt_call_any_cpu(int cpu_mask,smp_call_func_back func, void *data,rt_uint8_t flag) void rt_smp_call_any_cpu(int cpu_mask, smp_call_func_back func, void *data, rt_uint8_t flag)
{ {
rt_smp_call_func_cond(cpu_mask,func,data,flag,RT_NULL); rt_smp_call_func_cond(cpu_mask, func, data, flag, RT_NULL);
} }
void rt_call_any_cpu_cond(int cpu_mask,smp_call_func_back func, void *data,rt_uint8_t flag,smp_cond cond_func) void rt_smp_call_any_cpu_cond(int cpu_mask, smp_call_func_back func, void *data, rt_uint8_t flag, smp_cond cond_func)
{ {
rt_smp_call_func_cond(cpu_mask,func,data,flag,cond_func); rt_smp_call_func_cond(cpu_mask, func, data, flag, cond_func);
} }
void smp_init(void) void rt_smp_init(void)
{ {
for(int i = 0; i < RT_CPUS_NR; i++) for (int i = 0; i < RT_CPUS_NR; i++)
{ {
rt_memset(&global_work[i],0,sizeof(struct smp_call)); rt_memset(&rt_smp_work[i], 0, sizeof(struct rt_smp_call));
rt_spin_lock_init(&global_work[i].lock); rt_spin_lock_init(&rt_smp_work[i].lock);
} }
} }

View File

@ -10,25 +10,25 @@ typedef rt_bool_t (*smp_cond)(int cpu, void *info);
#define SMP_CALL_NO_WAIT (1 << 1) #define SMP_CALL_NO_WAIT (1 << 1)
#define RT_ALL_CPU ((1 << RT_CPUS_NR) - 1) #define RT_ALL_CPU ((1 << RT_CPUS_NR) - 1)
struct smp_event struct rt_smp_event
{ {
int cpu_mask; int cpu_mask;
int event_id; int event_id;
void *data; void *data;
smp_call_func_back func; smp_call_func_back func;
}; };
struct smp_call struct rt_smp_call
{ {
struct rt_spinlock lock; struct rt_spinlock lock;
struct smp_event event; struct rt_smp_event event;
}; };
void rt_smp_call_ipi_handler(int vector, void *param); void rt_smp_call_ipi_handler(int vector, void *param);
void rt_call_each_cpu(smp_call_func_back func, void *data,rt_uint8_t flag); void rt_smp_call_each_cpu(smp_call_func_back func, void *data,rt_uint8_t flag);
void rt_call_each_cpu_cond(smp_call_func_back func, void *data,rt_uint8_t flag,smp_cond cond_func); void rt_smp_call_each_cpu_cond(smp_call_func_back func, void *data,rt_uint8_t flag,smp_cond cond_func);
void rt_call_any_cpu(int cpu_mask,smp_call_func_back func, void *data,rt_uint8_t flag); void rt_smp_call_any_cpu(int cpu_mask,smp_call_func_back func, void *data,rt_uint8_t flag);
void rt_call_any_cpu_cond(int cpu_mask,smp_call_func_back func, void *data,rt_uint8_t flag,smp_cond cond_func); void rt_smp_call_any_cpu_cond(int cpu_mask,smp_call_func_back func, void *data,rt_uint8_t flag,smp_cond cond_func);
void smp_init(void); void rt_smp_init(void);
#endif #endif

View File

@ -26,7 +26,7 @@ void test1()
cpu_mask = rand() % 0xf; cpu_mask = rand() % 0xf;
if (cpu_mask == 0) if (cpu_mask == 0)
pass--; pass--;
rt_call_any_cpu(cpu_mask,test_call, &cpu_mask, SMP_CALL_WAIT_ALL); rt_smp_call_any_cpu(cpu_mask,test_call, &cpu_mask, SMP_CALL_WAIT_ALL);
if (i % 20 == 0) if (i % 20 == 0)
rt_kprintf("#"); rt_kprintf("#");
} }
@ -46,11 +46,11 @@ void test_call2(void *data)
void test2(void) void test2(void)
{ {
int data = 0; int data = 0;
rt_call_each_cpu(test_call2, &data, SMP_CALL_WAIT_ALL); rt_smp_call_each_cpu(test_call2, &data, SMP_CALL_WAIT_ALL);
uassert_true(data == RT_CPUS_NR); uassert_true(data == RT_CPUS_NR);
rt_thread_mdelay(10); rt_thread_mdelay(10);
data = 0; data = 0;
rt_call_each_cpu(test_call2, &data, SMP_CALL_NO_WAIT); rt_smp_call_each_cpu(test_call2, &data, SMP_CALL_NO_WAIT);
uassert_true(data != RT_CPUS_NR); uassert_true(data != RT_CPUS_NR);
} }

View File

@ -302,7 +302,7 @@ void rt_hw_common_setup(void)
rt_thread_idle_sethook(rt_hw_idle_wfi); rt_thread_idle_sethook(rt_hw_idle_wfi);
#ifdef RT_USING_SMP #ifdef RT_USING_SMP
smp_init(); rt_smp_init();
/* Install the IPI handle */ /* Install the IPI handle */
rt_hw_ipi_handler_install(RT_SCHEDULE_IPI, rt_scheduler_ipi_handler); rt_hw_ipi_handler_install(RT_SCHEDULE_IPI, rt_scheduler_ipi_handler);
rt_hw_ipi_handler_install(RT_STOP_IPI, rt_scheduler_ipi_handler); rt_hw_ipi_handler_install(RT_STOP_IPI, rt_scheduler_ipi_handler);