completion.c 5.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169
  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. * 2012-09-30 Bernard first version.
  9. * 2021-08-18 chenyingchun add comments
  10. * 2023-09-15 xqyjlj perf rt_hw_interrupt_disable/enable
  11. * 2024-01-25 Shell reduce resource usage in completion for better synchronization
  12. * and smaller footprint.
  13. */
  14. #define DBG_TAG "drivers.ipc"
  15. #define DBG_LVL DBG_INFO
  16. #include <rtdbg.h>
  17. #include <rthw.h>
  18. #include <rtdevice.h>
  19. #define RT_COMPLETED 1
  20. #define RT_UNCOMPLETED 0
  21. #define RT_COMPLETION_FLAG(comp) ((comp)->susp_thread_n_flag & 1)
  22. #define RT_COMPLETION_THREAD(comp) ((rt_thread_t)((comp)->susp_thread_n_flag & ~1))
  23. #define RT_COMPLETION_NEW_STAT(thread, flag) (((flag) & 1) | (((rt_base_t)thread) & ~1))
  24. static struct rt_spinlock _completion_lock = RT_SPINLOCK_INIT;
  25. /**
  26. * @brief This function will initialize a completion object.
  27. *
  28. * @param completion is a pointer to a completion object.
  29. */
  30. void rt_completion_init(struct rt_completion *completion)
  31. {
  32. RT_ASSERT(completion != RT_NULL);
  33. completion->susp_thread_n_flag = RT_COMPLETION_NEW_STAT(RT_NULL, RT_UNCOMPLETED);
  34. }
  35. RTM_EXPORT(rt_completion_init);
  36. /**
  37. * @brief This function will wait for a completion, if the completion is unavailable, the thread shall wait for
  38. * the completion up to a specified time.
  39. *
  40. * @param completion is a pointer to a completion object.
  41. *
  42. * @param timeout is a timeout period (unit: OS ticks). If the completion is unavailable, the thread will wait for
  43. * the completion done up to the amount of time specified by the argument.
  44. * NOTE: Generally, we use the macro RT_WAITING_FOREVER to set this parameter, which means that when the
  45. * completion is unavailable, the thread will be waitting forever.
  46. *
  47. * @return Return the operation status. ONLY when the return value is RT_EOK, the operation is successful.
  48. * If the return value is any other values, it means that the completion wait failed.
  49. *
  50. * @warning This function can ONLY be called in the thread context. It MUST NOT be called in interrupt context.
  51. */
  52. rt_err_t rt_completion_wait(struct rt_completion *completion,
  53. rt_int32_t timeout)
  54. {
  55. rt_err_t result;
  56. rt_base_t level;
  57. rt_thread_t thread;
  58. RT_ASSERT(completion != RT_NULL);
  59. /* current context checking */
  60. RT_DEBUG_SCHEDULER_AVAILABLE(timeout != 0);
  61. result = RT_EOK;
  62. thread = rt_thread_self();
  63. level = rt_spin_lock_irqsave(&_completion_lock);
  64. if (RT_COMPLETION_FLAG(completion) != RT_COMPLETED)
  65. {
  66. /* only one thread can suspend on complete */
  67. RT_ASSERT(RT_COMPLETION_THREAD(completion) == RT_NULL);
  68. if (timeout == 0)
  69. {
  70. result = -RT_ETIMEOUT;
  71. goto __exit;
  72. }
  73. else
  74. {
  75. /* reset thread error number */
  76. thread->error = RT_EOK;
  77. /* suspend thread */
  78. result = rt_thread_suspend_with_flag(thread, RT_UNINTERRUPTIBLE);
  79. if (result == RT_EOK)
  80. {
  81. /* add to suspended thread */
  82. completion->susp_thread_n_flag = RT_COMPLETION_NEW_STAT(thread, RT_UNCOMPLETED);
  83. /* current context checking */
  84. RT_DEBUG_NOT_IN_INTERRUPT;
  85. /* start timer */
  86. if (timeout > 0)
  87. {
  88. /* reset the timeout of thread timer and start it */
  89. rt_timer_control(&(thread->thread_timer),
  90. RT_TIMER_CTRL_SET_TIME,
  91. &timeout);
  92. rt_timer_start(&(thread->thread_timer));
  93. }
  94. /* enable interrupt */
  95. rt_spin_unlock_irqrestore(&_completion_lock, level);
  96. /* do schedule */
  97. rt_schedule();
  98. /* thread is waked up */
  99. result = thread->error;
  100. level = rt_spin_lock_irqsave(&_completion_lock);
  101. }
  102. }
  103. }
  104. /* clean completed flag & remove susp_thread on the case of waking by timeout */
  105. completion->susp_thread_n_flag = RT_COMPLETION_NEW_STAT(RT_NULL, RT_UNCOMPLETED);
  106. __exit:
  107. rt_spin_unlock_irqrestore(&_completion_lock, level);
  108. return result;
  109. }
  110. RTM_EXPORT(rt_completion_wait);
  111. /**
  112. * @brief This function indicates a completion has done.
  113. *
  114. * @param completion is a pointer to a completion object.
  115. */
  116. void rt_completion_done(struct rt_completion *completion)
  117. {
  118. rt_base_t level;
  119. rt_err_t error;
  120. rt_thread_t suspend_thread;
  121. RT_ASSERT(completion != RT_NULL);
  122. level = rt_spin_lock_irqsave(&_completion_lock);
  123. if (RT_COMPLETION_FLAG(completion) == RT_COMPLETED)
  124. {
  125. rt_spin_unlock_irqrestore(&_completion_lock, level);
  126. return;
  127. }
  128. suspend_thread = RT_COMPLETION_THREAD(completion);
  129. if (suspend_thread)
  130. {
  131. /* there is one thread in suspended list */
  132. /* resume it */
  133. error = rt_thread_resume(suspend_thread);
  134. if (error)
  135. {
  136. LOG_D("%s: failed to resume thread", __func__);
  137. }
  138. }
  139. completion->susp_thread_n_flag = RT_COMPLETION_NEW_STAT(RT_NULL, RT_COMPLETED);
  140. rt_spin_unlock_irqrestore(&_completion_lock, level);
  141. }
  142. RTM_EXPORT(rt_completion_done);