smp.c 1.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475
  1. #include <rtdevice.h>
  2. #include "utest.h"
  3. #include "utest_assert.h"
  4. #include "smp.h"
  5. static int pass_count = 0;
  6. static int pass = 1000;
  7. static struct rt_spinlock lock;
  8. static void test_call(void *data)
  9. {
  10. rt_spin_lock(&lock);
  11. int *i = (int *)data;
  12. int id = rt_hw_cpu_id();
  13. *i &= ~(1 << id);
  14. if (*i == 0)
  15. pass_count++;
  16. rt_spin_unlock(&lock);
  17. }
  18. static void test1()
  19. {
  20. int cpu_mask = 0xf;
  21. for (int i = 0; i < 1000; i++)
  22. {
  23. cpu_mask = rand() % 0xf;
  24. if (cpu_mask == 0)
  25. pass--;
  26. rt_smp_call_any_cpu(cpu_mask,test_call, &cpu_mask, SMP_CALL_WAIT_ALL);
  27. if (i % 20 == 0)
  28. rt_kprintf("#");
  29. }
  30. rt_kprintf("\n");
  31. uassert_true(pass_count == pass);
  32. }
  33. static void test_call2(void *data)
  34. {
  35. rt_spin_lock(&lock);
  36. int a = 100000;
  37. while (a--);
  38. int *i = (int *)data;
  39. (*i)++;
  40. rt_spin_unlock(&lock);
  41. }
  42. static void test2(void)
  43. {
  44. int data = 0;
  45. rt_smp_call_each_cpu(test_call2, &data, SMP_CALL_WAIT_ALL);
  46. uassert_true(data == RT_CPUS_NR);
  47. rt_thread_mdelay(10);
  48. data = 0;
  49. rt_smp_call_each_cpu(test_call2, &data, SMP_CALL_NO_WAIT);
  50. uassert_true(data != RT_CPUS_NR);
  51. }
  52. static rt_err_t utest_tc_init(void)
  53. {
  54. pass_count = 0;
  55. pass = 1000;
  56. rt_spin_lock_init(&lock);
  57. return RT_EOK;
  58. }
  59. static rt_err_t utest_tc_cleanup(void)
  60. {
  61. return RT_EOK;
  62. }
  63. static void testcase(void)
  64. {
  65. UTEST_UNIT_RUN(test1);
  66. UTEST_UNIT_RUN(test2);
  67. }
  68. UTEST_TC_EXPORT(testcase, "testcase.smp.smp", utest_tc_init, utest_tc_cleanup, 10);