diff options
| author | zhujiale <[email protected]> | 2024-09-12 11:54:50 +0800 |
|---|---|---|
| committer | Rbb666 <[email protected]> | 2024-09-24 09:56:30 +0800 |
| commit | 5f83c51347788ab4414cb753485d71c5dc247b6a (patch) | |
| tree | 2ff8909b902e3b400201adf5341f004f5284e891 /examples | |
| parent | 45bc5a721f455a785bdf18f399203db67ec42f8a (diff) | |
SMP
Diffstat (limited to 'examples')
| -rw-r--r-- | examples/utest/testcases/smp/smp.c | 53 |
1 files changed, 36 insertions, 17 deletions
diff --git a/examples/utest/testcases/smp/smp.c b/examples/utest/testcases/smp/smp.c index f35e243912..1af161b72f 100644 --- a/examples/utest/testcases/smp/smp.c +++ b/examples/utest/testcases/smp/smp.c @@ -2,39 +2,58 @@ #include "utest.h" #include "utest_assert.h" #include "smp.h" -int pass_count = 0; -int pass = 1000; +int pass_count = 0; +int pass = 1000; struct rt_spinlock lock; void test_call(void *data) { rt_spin_lock(&lock); - int *i = (int *)data; - int id = rt_hw_cpu_id(); - *i &= ~(1 << id); - if(*i == 0) + int *i = (int *)data; + int id = rt_hw_cpu_id(); + *i &= ~(1 << id); + if (*i == 0) pass_count++; rt_spin_unlock(&lock); } -void test() +void test1() { int cpu_mask = 0xf; - for(int i =0 ;i < 1000 ;i++) + for (int i = 0; i < 1000; i++) { - cpu_mask = rand()% 0xf; - if (cpu_mask == 0) - pass--; - rt_smp_call_func_cond(cpu_mask,test_call, &cpu_mask); - if(i % 20 == 0) + cpu_mask = rand() % 0xf; + if (cpu_mask == 0) + pass--; + rt_call_each_cpu(test_call, &cpu_mask, SMP_CALL_NO_WAIT); + if (i % 20 == 0) rt_kprintf("#"); rt_thread_mdelay(1); - } + } rt_kprintf("\n"); + uassert_true(pass_count == pass); } - +void test_call2(void *data) +{ + rt_spin_lock(&lock); + int a = 100000; + while (a--); + int *i = (int *)data; + (*i)++; + rt_spin_unlock(&lock); +} +void test2(void) +{ + int data = 0; + rt_call_each_cpu(test_call2, &data, SMP_CALL_WAIT_ALL); + uassert_true(data == RT_CPUS_NR); + rt_thread_mdelay(10); + data = 0; + rt_call_each_cpu(test_call2, &data, SMP_CALL_NO_WAIT); + uassert_true(data != RT_CPUS_NR); +} static rt_err_t utest_tc_init(void) { @@ -44,12 +63,12 @@ static rt_err_t utest_tc_init(void) static rt_err_t utest_tc_cleanup(void) { - uassert_true(pass_count == pass); return RT_EOK; } static void testcase(void) { - UTEST_UNIT_RUN(test); + UTEST_UNIT_RUN(test1); + UTEST_UNIT_RUN(test2); } UTEST_TC_EXPORT(testcase, "testcase.smp.smp", utest_tc_init, utest_tc_cleanup, 10); |
