waitqueue.h 3.0 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283
  1. /*****************************************************************************
  2. * Copyright (c) 2019, Nations Technologies Inc.
  3. *
  4. * All rights reserved.
  5. * ****************************************************************************
  6. *
  7. * Redistribution and use in source and binary forms, with or without
  8. * modification, are permitted provided that the following conditions are met:
  9. *
  10. * - Redistributions of source code must retain the above copyright notice,
  11. * this list of conditions and the disclaimer below.
  12. *
  13. * Nations' name may not be used to endorse or promote products derived from
  14. * this software without specific prior written permission.
  15. *
  16. * DISCLAIMER: THIS SOFTWARE IS PROVIDED BY NATIONS "AS IS" AND ANY EXPRESS OR
  17. * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
  18. * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NON-INFRINGEMENT ARE
  19. * DISCLAIMED. IN NO EVENT SHALL NATIONS BE LIABLE FOR ANY DIRECT, INDIRECT,
  20. * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
  21. * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA,
  22. * OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
  23. * LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
  24. * NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE,
  25. * EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  26. * ****************************************************************************/
  27. /**
  28. * @file waitqueue.h
  29. * @author Nations
  30. * @version v1.0.0
  31. *
  32. * @copyright Copyright (c) 2019, Nations Technologies Inc. All rights reserved.
  33. */
  34. #ifndef WAITQUEUE_H__
  35. #define WAITQUEUE_H__
  36. #include <rtthread.h>
  37. #define RT_WQ_FLAG_CLEAN 0x00
  38. #define RT_WQ_FLAG_WAKEUP 0x01
  39. struct rt_wqueue_node;
  40. typedef int (*rt_wqueue_func_t)(struct rt_wqueue_node *wait, void *key);
  41. struct rt_wqueue_node
  42. {
  43. rt_thread_t polling_thread;
  44. rt_list_t list;
  45. rt_wqueue_func_t wakeup;
  46. rt_uint32_t key;
  47. };
  48. typedef struct rt_wqueue_node rt_wqueue_node_t;
  49. int __wqueue_default_wake(struct rt_wqueue_node *wait, void *key);
  50. rt_inline void rt_wqueue_init(rt_wqueue_t *queue)
  51. {
  52. RT_ASSERT(queue != RT_NULL);
  53. queue->flag = RT_WQ_FLAG_CLEAN;
  54. rt_list_init(&(queue->waiting_list));
  55. }
  56. void rt_wqueue_add(rt_wqueue_t *queue, struct rt_wqueue_node *node);
  57. void rt_wqueue_remove(struct rt_wqueue_node *node);
  58. int rt_wqueue_wait(rt_wqueue_t *queue, int condition, int timeout);
  59. void rt_wqueue_wakeup(rt_wqueue_t *queue, void *key);
  60. #define DEFINE_WAIT_FUNC(name, function) \
  61. struct rt_wqueue_node name = { \
  62. rt_current_thread, \
  63. RT_LIST_OBJECT_INIT(((name).list)), \
  64. \
  65. function, \
  66. 0 \
  67. }
  68. #define DEFINE_WAIT(name) DEFINE_WAIT_FUNC(name, __wqueue_default_wake)
  69. #endif