condvar_timedwait_tc.c 1.6 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576
  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-11-20 Shell add test suites
  9. */
  10. #include "common.h"
  11. #include "utest_assert.h"
  12. #include <rtdevice.h>
  13. #include <rtdef.h>
  14. static struct rt_mutex _local_mtx;
  15. static struct rt_condvar _local_cv;
  16. static void condvar_timedwait_tc(void)
  17. {
  18. rt_mutex_t mutex = &_local_mtx;
  19. rt_condvar_t cond = &_local_cv;
  20. int err;
  21. if (rt_mutex_take(mutex, RT_WAITING_FOREVER) != 0)
  22. {
  23. LOG_E("pthread_mutex_lock() error");
  24. uassert_false(1);
  25. return;
  26. }
  27. err = rt_condvar_timedwait(cond, mutex, RT_KILLABLE, 100);
  28. if (err != 0)
  29. {
  30. if (err == -EINTR || err == -ETIMEDOUT)
  31. {
  32. puts("wait timed out");
  33. uassert_false(0);
  34. }
  35. else
  36. {
  37. LOG_E("errno=%d, ret=%d\n", errno, err);
  38. LOG_E("pthread_cond_timedwait() error");
  39. uassert_false(1);
  40. }
  41. }
  42. return ;
  43. }
  44. static rt_err_t utest_tc_init(void)
  45. {
  46. if (rt_mutex_init(&_local_mtx, "utest", RT_IPC_FLAG_PRIO) != 0)
  47. {
  48. perror("pthread_mutex_init() error");
  49. uassert_false(1);
  50. return -1;
  51. }
  52. rt_condvar_init(&_local_cv, NULL);
  53. return RT_EOK;
  54. }
  55. static rt_err_t utest_tc_cleanup(void)
  56. {
  57. rt_mutex_detach(&_local_mtx);
  58. rt_condvar_detach(&_local_cv);
  59. return RT_EOK;
  60. }
  61. static void testcase(void)
  62. {
  63. UTEST_UNIT_RUN(condvar_timedwait_tc);
  64. }
  65. UTEST_TC_EXPORT(testcase, "testcases.ipc.condvar.timedwait", utest_tc_init, utest_tc_cleanup, 10);