completion.c 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145
  1. /*
  2. * File : completion.c
  3. * This file is part of RT-Thread RTOS
  4. * COPYRIGHT (C) 2012, RT-Thread Development Team
  5. *
  6. * This program is free software; you can redistribute it and/or modify
  7. * it under the terms of the GNU General Public License as published by
  8. * the Free Software Foundation; either version 2 of the License, or
  9. * (at your option) any later version.
  10. *
  11. * This program is distributed in the hope that it will be useful,
  12. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  13. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  14. * GNU General Public License for more details.
  15. *
  16. * You should have received a copy of the GNU General Public License along
  17. * with this program; if not, write to the Free Software Foundation, Inc.,
  18. * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
  19. *
  20. * Change Logs:
  21. * Date Author Notes
  22. * 2012-09-30 Bernard first version.
  23. */
  24. #include <rthw.h>
  25. #include <rtthread.h>
  26. #include <rtdevice.h>
  27. #define RT_COMPLETED 1
  28. #define RT_UNCOMPLETED 0
  29. void rt_completion_init(struct rt_completion *completion)
  30. {
  31. rt_base_t level;
  32. RT_ASSERT(completion != RT_NULL);
  33. level = rt_hw_interrupt_disable();
  34. completion->flag = RT_UNCOMPLETED;
  35. rt_list_init(&completion->suspended_list);
  36. rt_hw_interrupt_enable(level);
  37. }
  38. RTM_EXPORT(rt_completion_init);
  39. rt_err_t rt_completion_wait(struct rt_completion *completion,
  40. rt_int32_t timeout)
  41. {
  42. rt_err_t result;
  43. rt_base_t level;
  44. rt_thread_t thread;
  45. RT_ASSERT(completion != RT_NULL);
  46. result = RT_EOK;
  47. thread = rt_thread_self();
  48. level = rt_hw_interrupt_disable();
  49. if (completion->flag != RT_COMPLETED)
  50. {
  51. /* only one thread can suspend on complete */
  52. RT_ASSERT(rt_list_isempty(&(completion->suspended_list)));
  53. if (timeout == 0)
  54. {
  55. result = -RT_ETIMEOUT;
  56. goto __exit;
  57. }
  58. else
  59. {
  60. /* reset thread error number */
  61. thread->error = RT_EOK;
  62. /* suspend thread */
  63. rt_thread_suspend(thread);
  64. /* add to suspended list */
  65. rt_list_insert_before(&(completion->suspended_list),
  66. &(thread->tlist));
  67. /* current context checking */
  68. RT_DEBUG_NOT_IN_INTERRUPT;
  69. /* start timer */
  70. if (timeout > 0)
  71. {
  72. /* reset the timeout of thread timer and start it */
  73. rt_timer_control(&(thread->thread_timer),
  74. RT_TIMER_CTRL_SET_TIME,
  75. &timeout);
  76. rt_timer_start(&(thread->thread_timer));
  77. }
  78. /* enable interrupt */
  79. rt_hw_interrupt_enable(level);
  80. /* do schedule */
  81. rt_schedule();
  82. /* thread is waked up */
  83. result = thread->error;
  84. level = rt_hw_interrupt_disable();
  85. }
  86. }
  87. /* clean completed flag */
  88. completion->flag = RT_UNCOMPLETED;
  89. __exit:
  90. rt_hw_interrupt_enable(level);
  91. return result;
  92. }
  93. RTM_EXPORT(rt_completion_wait);
  94. void rt_completion_done(struct rt_completion *completion)
  95. {
  96. rt_base_t level;
  97. RT_ASSERT(completion != RT_NULL);
  98. if (completion->flag == RT_COMPLETED)
  99. return;
  100. level = rt_hw_interrupt_disable();
  101. completion->flag = RT_COMPLETED;
  102. if (!rt_list_isempty(&(completion->suspended_list)))
  103. {
  104. /* there is one thread in suspended list */
  105. struct rt_thread *thread;
  106. /* get thread entry */
  107. thread = rt_list_entry(completion->suspended_list.next,
  108. struct rt_thread,
  109. tlist);
  110. /* resume it */
  111. rt_thread_resume(thread);
  112. rt_hw_interrupt_enable(level);
  113. /* perform a schedule */
  114. rt_schedule();
  115. }
  116. else
  117. {
  118. rt_hw_interrupt_enable(level);
  119. }
  120. }
  121. RTM_EXPORT(rt_completion_done);