completion.c 3.3 KB

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