smp_thread_preemption_tc.c 2.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687
  1. /*
  2. * Copyright (c) 2006-2024, RT-Thread Development Team
  3. *
  4. * SPDX-License-Identifier: Apache-2.0
  5. *
  6. * Change Logs:
  7. * Date Author Notes
  8. * 2024-08-10 RV the first version
  9. */
  10. #include <rtthread.h>
  11. #include "utest.h"
  12. /**
  13. * @brief Thread Preemption Test with Different Priorities.
  14. *
  15. * @note Create multiple threads, low-priority threads run first,
  16. * high-priority threads preempt low-priority threads, and
  17. * print the current status of each core in the thread's entry function.
  18. */
  19. #define THREAD_PRIORITY_HIGH 21
  20. #define THREAD_PRIORITY_LOW 30
  21. #define THREAD_STACK_SIZE UTEST_THR_STACK_SIZE
  22. static rt_thread_t threads[2];
  23. static struct rt_spinlock lock;
  24. /* High Priority Thread */
  25. static void thread_high_entry(void *parameter)
  26. {
  27. uassert_true(1);
  28. rt_spin_lock(&lock);
  29. rt_kprintf("High priority thread is running\n");
  30. extern long list_thread(void);
  31. list_thread();
  32. rt_spin_unlock(&lock);
  33. }
  34. /* Low Priority Thread */
  35. static void thread_low_entry(void *parameter)
  36. {
  37. uassert_true(1);
  38. rt_spin_lock(&lock);
  39. rt_kprintf("Low priority thread is running\n");
  40. extern long list_thread(void);
  41. list_thread();
  42. rt_spin_unlock(&lock);
  43. }
  44. static void thread_preemptions_tc(void)
  45. {
  46. /* Creating low-priority thread */
  47. threads[0] = rt_thread_create("tlow", thread_low_entry, RT_NULL, THREAD_STACK_SIZE, THREAD_PRIORITY_LOW, 10);
  48. if (threads[0] != RT_NULL)
  49. {
  50. uassert_true(1);
  51. rt_thread_startup(threads[0] );
  52. }
  53. rt_thread_delay(5);
  54. /* Creating high-priority thread */
  55. threads[1] = rt_thread_create("thigh", thread_high_entry, RT_NULL, THREAD_STACK_SIZE, THREAD_PRIORITY_HIGH, 10);
  56. if (threads[1] != RT_NULL)
  57. {
  58. uassert_true(1);
  59. rt_thread_startup(threads[1]);
  60. }
  61. rt_thread_delay(50);
  62. }
  63. static rt_err_t utest_tc_init(void)
  64. {
  65. rt_spin_lock_init(&lock);
  66. return RT_EOK;
  67. }
  68. static rt_err_t utest_tc_cleanup(void)
  69. {
  70. return RT_EOK;
  71. }
  72. static void testcase(void)
  73. {
  74. UTEST_UNIT_RUN(thread_preemptions_tc);
  75. }
  76. UTEST_TC_EXPORT(testcase, "testcases.smp.thread_preemptions_tc", utest_tc_init, utest_tc_cleanup, 10);