mtsafe_kprint_tc.c 1.4 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768
  1. /*
  2. * Copyright (c) 2006-2023, RT-Thread Development Team
  3. *
  4. * SPDX-License-Identifier: Apache-2.0
  5. *
  6. * Change Logs:
  7. * Date Author Notes
  8. * 2023-12-25 Shell the first version
  9. */
  10. #include <rtthread.h>
  11. #include "utest.h"
  12. #define TEST_LOOP_TIMES 20
  13. static struct rt_semaphore _thr_exit_sem;
  14. static void _thread_entry(void *param)
  15. {
  16. for (size_t i = 0; i < TEST_LOOP_TIMES; i++)
  17. {
  18. rt_kprintf("This is thread %p\n", rt_thread_self());
  19. rt_thread_mdelay(1);
  20. }
  21. rt_sem_release(&_thr_exit_sem);
  22. return;
  23. }
  24. #define TEST_THREAD_COUNT 16
  25. static void mtsafe_kprint_tc(void)
  26. {
  27. for (size_t i = 0; i < TEST_THREAD_COUNT; i++)
  28. {
  29. rt_thread_t new_thread =
  30. rt_thread_create(
  31. "test",
  32. _thread_entry,
  33. NULL,
  34. UTEST_THR_STACK_SIZE,
  35. UTEST_THR_PRIORITY,
  36. 100);
  37. rt_thread_startup(new_thread);
  38. }
  39. for (size_t i = 0; i < TEST_THREAD_COUNT; i++)
  40. {
  41. rt_sem_take(&_thr_exit_sem, RT_WAITING_FOREVER);
  42. }
  43. }
  44. static rt_err_t utest_tc_init(void)
  45. {
  46. rt_sem_init(&_thr_exit_sem, "test", 0, RT_IPC_FLAG_PRIO);
  47. return RT_EOK;
  48. }
  49. static rt_err_t utest_tc_cleanup(void)
  50. {
  51. rt_sem_detach(&_thr_exit_sem);
  52. return RT_EOK;
  53. }
  54. static void testcase(void)
  55. {
  56. UTEST_UNIT_RUN(mtsafe_kprint_tc);
  57. }
  58. UTEST_TC_EXPORT(testcase, "testcases.kernel.mtsafe_kprint", utest_tc_init, utest_tc_cleanup, 10);