sched_timed_sem_tc.c 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149
  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-01-25 Shell init ver.
  9. */
  10. #define __RT_KERNEL_SOURCE__
  11. #include <rtthread.h>
  12. #include <stdlib.h>
  13. #include "utest.h"
  14. #define TEST_SECONDS 10
  15. #define TEST_LOOP_TICKS (TEST_SECONDS * RT_TICK_PER_SECOND)
  16. #define TEST_PROGRESS_COUNTS (36)
  17. #define TEST_PROGRESS_ON (TEST_LOOP_TICKS*2/TEST_PROGRESS_COUNTS)
  18. static struct rt_semaphore _thr_exit_sem;
  19. static struct rt_semaphore _ipc_sem;
  20. static rt_atomic_t _progress_counter;
  21. static rt_base_t _timedout_failed_times = 0;
  22. /**
  23. * Test on timedout IPC with racing condition where timedout routine and producer
  24. * thread may race to wakeup sleeper.
  25. *
  26. * This test will fork 2 thread, one producer and one consumer. The producer will
  27. * looping and trigger the IPC on the edge of new tick arrives. The consumer will
  28. * wait on IPC with a timedout of 1 tick.
  29. */
  30. static void _wait_until_edge(void)
  31. {
  32. rt_tick_t entry_level, current;
  33. rt_base_t random_latency;
  34. entry_level = rt_tick_get();
  35. do
  36. {
  37. current = rt_tick_get();
  38. }
  39. while (current == entry_level);
  40. /* give a random latency for test */
  41. random_latency = rand();
  42. entry_level = current;
  43. for (size_t i = 0; i < random_latency; i++)
  44. {
  45. current = rt_tick_get();
  46. if (current != entry_level)
  47. break;
  48. }
  49. }
  50. static void _producer_entry(void *param)
  51. {
  52. for (size_t i = 0; i < TEST_LOOP_TICKS; i++)
  53. {
  54. _wait_until_edge();
  55. rt_sem_release(&_ipc_sem);
  56. if (rt_atomic_add(&_progress_counter, 1) % TEST_PROGRESS_ON == 0)
  57. uassert_true(1);
  58. }
  59. rt_sem_release(&_thr_exit_sem);
  60. return;
  61. }
  62. static void _consumer_entry(void *param)
  63. {
  64. int error;
  65. for (size_t i = 0; i < TEST_LOOP_TICKS; i++)
  66. {
  67. error = rt_sem_take_interruptible(&_ipc_sem, 1);
  68. if (error == -RT_ETIMEOUT)
  69. {
  70. _timedout_failed_times++;
  71. }
  72. else
  73. {
  74. if (error != RT_EOK)
  75. uassert_true(0);
  76. }
  77. if (rt_atomic_add(&_progress_counter, 1) % TEST_PROGRESS_ON == 0)
  78. uassert_true(1);
  79. }
  80. rt_sem_release(&_thr_exit_sem);
  81. return;
  82. }
  83. static void timed_sem_tc(void)
  84. {
  85. rt_thread_t prod = rt_thread_create(
  86. "prod",
  87. _producer_entry,
  88. (void *)0,
  89. UTEST_THR_STACK_SIZE,
  90. UTEST_THR_PRIORITY + 1,
  91. 4);
  92. rt_thread_t cons = rt_thread_create(
  93. "cons",
  94. _consumer_entry,
  95. (void *)0,
  96. UTEST_THR_STACK_SIZE,
  97. UTEST_THR_PRIORITY + 1,
  98. 100);
  99. rt_thread_startup(prod);
  100. rt_thread_startup(cons);
  101. for (size_t i = 0; i < 2; i++)
  102. {
  103. rt_sem_take(&_thr_exit_sem, RT_WAITING_FOREVER);
  104. }
  105. /* Summary */
  106. LOG_I("Total failed times: %ld(in %d)\n", _timedout_failed_times, TEST_LOOP_TICKS);
  107. }
  108. static rt_err_t utest_tc_init(void)
  109. {
  110. int *pseed = rt_malloc(sizeof(int));
  111. srand(*(int *)pseed);
  112. rt_free(pseed);
  113. rt_sem_init(&_ipc_sem, "ipc", 0, RT_IPC_FLAG_PRIO);
  114. rt_sem_init(&_thr_exit_sem, "test", 0, RT_IPC_FLAG_PRIO);
  115. return RT_EOK;
  116. }
  117. static rt_err_t utest_tc_cleanup(void)
  118. {
  119. rt_sem_detach(&_ipc_sem);
  120. rt_sem_detach(&_thr_exit_sem);
  121. return RT_EOK;
  122. }
  123. static void testcase(void)
  124. {
  125. UTEST_UNIT_RUN(timed_sem_tc);
  126. }
  127. UTEST_TC_EXPORT(testcase, "testcases.kernel.scheduler.timed_sem", utest_tc_init, utest_tc_cleanup, TEST_SECONDS * 2);