completion_comm.c 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081
  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-04-26 Shell lockless rt_completion
  9. */
  10. #include <rtthread.h>
  11. #include <rthw.h>
  12. #include <rtdevice.h>
  13. /**
  14. * @brief This function indicates a completion has done.
  15. *
  16. * @param completion is a pointer to a completion object.
  17. */
  18. void rt_completion_done(struct rt_completion *completion)
  19. {
  20. rt_completion_wakeup_by_errno(completion, -1);
  21. }
  22. RTM_EXPORT(rt_completion_done);
  23. /**
  24. * @brief This function indicates a completion has done and wakeup the thread
  25. *
  26. * @param completion is a pointer to a completion object.
  27. * @return RT_EOK if wakeup succeed.
  28. * RT_EEMPTY if wakeup failure and the completion is set to completed.
  29. * RT_EBUSY if the completion is still in completed state
  30. */
  31. rt_err_t rt_completion_wakeup(struct rt_completion *completion)
  32. {
  33. return rt_completion_wakeup_by_errno(completion, -1);
  34. }
  35. /**
  36. * @brief This is same as rt_completion_wait(), except that this API is NOT
  37. * ISR-safe (you can NOT call completion_done() on isr routine).
  38. *
  39. * @param completion is a pointer to a completion object.
  40. *
  41. * @param timeout is a timeout period (unit: OS ticks). If the completion is unavailable, the thread will wait for
  42. * the completion done up to the amount of time specified by the argument.
  43. * NOTE: Generally, we use the macro RT_WAITING_FOREVER to set this parameter, which means that when the
  44. * completion is unavailable, the thread will be waitting forever.
  45. *
  46. * @return Return the operation status. ONLY when the return value is RT_EOK, the operation is successful.
  47. * If the return value is any other values, it means that the completion wait failed.
  48. *
  49. * @warning This function can ONLY be called in the thread context. It MUST NOT be called in interrupt context.
  50. */
  51. rt_err_t rt_completion_wait_noisr(struct rt_completion *completion,
  52. rt_int32_t timeout)
  53. {
  54. return rt_completion_wait_flags_noisr(completion, timeout, RT_UNINTERRUPTIBLE);
  55. }
  56. /**
  57. * @brief This function will wait for a completion, if the completion is unavailable, the thread shall wait for
  58. * the completion up to a specified time.
  59. *
  60. * @param completion is a pointer to a completion object.
  61. *
  62. * @param timeout is a timeout period (unit: OS ticks). If the completion is unavailable, the thread will wait for
  63. * the completion done up to the amount of time specified by the argument.
  64. * NOTE: Generally, we use the macro RT_WAITING_FOREVER to set this parameter, which means that when the
  65. * completion is unavailable, the thread will be waitting forever.
  66. *
  67. * @return Return the operation status. ONLY when the return value is RT_EOK, the operation is successful.
  68. * If the return value is any other values, it means that the completion wait failed.
  69. *
  70. * @warning This function can ONLY be called in the thread context. It MUST NOT be called in interrupt context.
  71. */
  72. rt_err_t rt_completion_wait(struct rt_completion *completion,
  73. rt_int32_t timeout)
  74. {
  75. return rt_completion_wait_flags(completion, timeout, RT_UNINTERRUPTIBLE);
  76. }
  77. RTM_EXPORT(rt_completion_wait);