completion.c 2.7 KB

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