completion.c 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126
  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), &(thread->tlist));
  55. /* current context checking */
  56. RT_DEBUG_NOT_IN_INTERRUPT;
  57. /* start timer */
  58. if (timeout > 0)
  59. {
  60. /* reset the timeout of thread timer and start it */
  61. rt_timer_control(&(thread->thread_timer), RT_TIMER_CTRL_SET_TIME, &timeout);
  62. rt_timer_start(&(thread->thread_timer));
  63. }
  64. /* enable interrupt */
  65. rt_hw_interrupt_enable(level);
  66. /* do schedule */
  67. rt_schedule();
  68. /* thread is waked up */
  69. result = thread->error;
  70. level = rt_hw_interrupt_disable();
  71. /* clean completed flag */
  72. completion->flag = RT_UNCOMPLETED;
  73. }
  74. }
  75. __exit:
  76. rt_hw_interrupt_enable(level);
  77. return result;
  78. }
  79. void rt_completion_done(struct rt_completion *completion)
  80. {
  81. rt_base_t level;
  82. RT_ASSERT(completion != RT_NULL);
  83. if (completion->flag == RT_COMPLETED)
  84. return;
  85. level = rt_hw_interrupt_disable();
  86. completion->flag = RT_COMPLETED;
  87. if (!rt_list_isempty(&(completion->suspended_list)))
  88. {
  89. /* there is one thread in suspended list */
  90. struct rt_thread *thread;
  91. /* get thread entry */
  92. thread = rt_list_entry(completion->suspended_list.next, struct rt_thread, tlist);
  93. /* resume it */
  94. rt_thread_resume(thread);
  95. rt_hw_interrupt_enable(level);
  96. /* perform a schedule */
  97. rt_schedule();
  98. }
  99. else
  100. {
  101. rt_hw_interrupt_enable(level);
  102. }
  103. }