completion.c 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141
  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. rt_err_t rt_completion_wait(struct rt_completion *completion,
  39. rt_int32_t timeout)
  40. {
  41. rt_err_t result;
  42. rt_base_t level;
  43. rt_thread_t thread;
  44. RT_ASSERT(completion != RT_NULL);
  45. result = RT_EOK;
  46. thread = rt_thread_self();
  47. level = rt_hw_interrupt_disable();
  48. if (completion->flag != RT_COMPLETED)
  49. {
  50. /* only one thread can suspend on complete */
  51. RT_ASSERT(rt_list_isempty(&(completion->suspended_list)));
  52. if (timeout == 0)
  53. {
  54. result = -RT_ETIMEOUT;
  55. goto __exit;
  56. }
  57. else
  58. {
  59. /* reset thread error number */
  60. thread->error = RT_EOK;
  61. /* suspend thread */
  62. rt_thread_suspend(thread);
  63. /* add to suspended list */
  64. rt_list_insert_before(&(completion->suspended_list),
  65. &(thread->tlist));
  66. /* current context checking */
  67. RT_DEBUG_NOT_IN_INTERRUPT;
  68. /* start timer */
  69. if (timeout > 0)
  70. {
  71. /* reset the timeout of thread timer and start it */
  72. rt_timer_control(&(thread->thread_timer),
  73. RT_TIMER_CTRL_SET_TIME,
  74. &timeout);
  75. rt_timer_start(&(thread->thread_timer));
  76. }
  77. /* enable interrupt */
  78. rt_hw_interrupt_enable(level);
  79. /* do schedule */
  80. rt_schedule();
  81. /* thread is waked up */
  82. result = thread->error;
  83. level = rt_hw_interrupt_disable();
  84. /* clean completed flag */
  85. completion->flag = RT_UNCOMPLETED;
  86. }
  87. }
  88. __exit:
  89. rt_hw_interrupt_enable(level);
  90. return result;
  91. }
  92. void rt_completion_done(struct rt_completion *completion)
  93. {
  94. rt_base_t level;
  95. RT_ASSERT(completion != RT_NULL);
  96. if (completion->flag == RT_COMPLETED)
  97. return;
  98. level = rt_hw_interrupt_disable();
  99. completion->flag = RT_COMPLETED;
  100. if (!rt_list_isempty(&(completion->suspended_list)))
  101. {
  102. /* there is one thread in suspended list */
  103. struct rt_thread *thread;
  104. /* get thread entry */
  105. thread = rt_list_entry(completion->suspended_list.next,
  106. struct rt_thread,
  107. tlist);
  108. /* resume it */
  109. rt_thread_resume(thread);
  110. rt_hw_interrupt_enable(level);
  111. /* perform a schedule */
  112. rt_schedule();
  113. }
  114. else
  115. {
  116. rt_hw_interrupt_enable(level);
  117. }
  118. }