completion.c 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131
  1. /*
  2. * Copyright (c) 2006-2018, RT-Thread Development Team
  3. *
  4. * SPDX-License-Identifier: Apache-2.0
  5. *
  6. * Change Logs:
  7. * Date Author Notes
  8. * 2012-09-30 Bernard first version.
  9. */
  10. #include <rthw.h>
  11. #include <rtthread.h>
  12. #include <rtdevice.h>
  13. #define RT_COMPLETED 1
  14. #define RT_UNCOMPLETED 0
  15. void rt_completion_init(struct rt_completion *completion)
  16. {
  17. rt_base_t level;
  18. RT_ASSERT(completion != RT_NULL);
  19. level = rt_hw_interrupt_disable();
  20. completion->flag = RT_UNCOMPLETED;
  21. rt_list_init(&completion->suspended_list);
  22. rt_hw_interrupt_enable(level);
  23. }
  24. RTM_EXPORT(rt_completion_init);
  25. rt_err_t rt_completion_wait(struct rt_completion *completion,
  26. rt_int32_t timeout)
  27. {
  28. rt_err_t result;
  29. rt_base_t level;
  30. rt_thread_t thread;
  31. RT_ASSERT(completion != RT_NULL);
  32. result = RT_EOK;
  33. thread = rt_thread_self();
  34. level = rt_hw_interrupt_disable();
  35. if (completion->flag != RT_COMPLETED)
  36. {
  37. /* only one thread can suspend on complete */
  38. RT_ASSERT(rt_list_isempty(&(completion->suspended_list)));
  39. if (timeout == 0)
  40. {
  41. result = -RT_ETIMEOUT;
  42. goto __exit;
  43. }
  44. else
  45. {
  46. /* reset thread error number */
  47. thread->error = RT_EOK;
  48. /* suspend thread */
  49. rt_thread_suspend(thread);
  50. /* add to suspended list */
  51. rt_list_insert_before(&(completion->suspended_list),
  52. &(thread->tlist));
  53. /* current context checking */
  54. RT_DEBUG_NOT_IN_INTERRUPT;
  55. /* start timer */
  56. if (timeout > 0)
  57. {
  58. /* reset the timeout of thread timer and start it */
  59. rt_timer_control(&(thread->thread_timer),
  60. RT_TIMER_CTRL_SET_TIME,
  61. &timeout);
  62. rt_timer_start(&(thread->thread_timer));
  63. }
  64. /* enable interrupt */
  65. rt_hw_interrupt_enable(level);
  66. /* do schedule */
  67. rt_schedule();
  68. /* thread is waked up */
  69. result = thread->error;
  70. level = rt_hw_interrupt_disable();
  71. }
  72. }
  73. /* clean completed flag */
  74. completion->flag = RT_UNCOMPLETED;
  75. __exit:
  76. rt_hw_interrupt_enable(level);
  77. return result;
  78. }
  79. RTM_EXPORT(rt_completion_wait);
  80. void rt_completion_done(struct rt_completion *completion)
  81. {
  82. rt_base_t level;
  83. RT_ASSERT(completion != RT_NULL);
  84. if (completion->flag == RT_COMPLETED)
  85. return;
  86. level = rt_hw_interrupt_disable();
  87. completion->flag = RT_COMPLETED;
  88. if (!rt_list_isempty(&(completion->suspended_list)))
  89. {
  90. /* there is one thread in suspended list */
  91. struct rt_thread *thread;
  92. /* get thread entry */
  93. thread = rt_list_entry(completion->suspended_list.next,
  94. struct rt_thread,
  95. tlist);
  96. /* resume it */
  97. rt_thread_resume(thread);
  98. rt_hw_interrupt_enable(level);
  99. /* perform a schedule */
  100. rt_schedule();
  101. }
  102. else
  103. {
  104. rt_hw_interrupt_enable(level);
  105. }
  106. }
  107. RTM_EXPORT(rt_completion_done);