waitqueue.h 2.4 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273
  1. /*
  2. * File : waitqueue.h
  3. * This file is part of RT-Thread RTOS
  4. * COPYRIGHT (C) 2006 - 2018, 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. * 2018/06/26 Bernard Fix the wait queue issue when wakeup a soon
  23. * to blocked thread.
  24. */
  25. #ifndef WAITQUEUE_H__
  26. #define WAITQUEUE_H__
  27. #include <rtthread.h>
  28. #define RT_WQ_FLAG_CLEAN 0x00
  29. #define RT_WQ_FLAG_WAKEUP 0x01
  30. struct rt_wqueue_node;
  31. typedef int (*rt_wqueue_func_t)(struct rt_wqueue_node *wait, void *key);
  32. struct rt_wqueue_node
  33. {
  34. rt_thread_t polling_thread;
  35. rt_list_t list;
  36. rt_wqueue_func_t wakeup;
  37. rt_uint32_t key;
  38. };
  39. typedef struct rt_wqueue_node rt_wqueue_node_t;
  40. int __wqueue_default_wake(struct rt_wqueue_node *wait, void *key);
  41. rt_inline void rt_wqueue_init(rt_wqueue_t *queue)
  42. {
  43. RT_ASSERT(queue != RT_NULL);
  44. queue->flag = RT_WQ_FLAG_CLEAN;
  45. rt_list_init(&(queue->waiting_list));
  46. }
  47. void rt_wqueue_add(rt_wqueue_t *queue, struct rt_wqueue_node *node);
  48. void rt_wqueue_remove(struct rt_wqueue_node *node);
  49. int rt_wqueue_wait(rt_wqueue_t *queue, int condition, int timeout);
  50. void rt_wqueue_wakeup(rt_wqueue_t *queue, void *key);
  51. #define DEFINE_WAIT_FUNC(name, function) \
  52. struct rt_wqueue_node name = { \
  53. rt_current_thread, \
  54. RT_LIST_OBJECT_INIT(((name).list)), \
  55. \
  56. function, \
  57. 0 \
  58. }
  59. #define DEFINE_WAIT(name) DEFINE_WAIT_FUNC(name, __wqueue_default_wake)
  60. #endif