rtservice.h 2.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122
  1. /*
  2. * File : rtservice.h
  3. * This file is part of RT-Thread RTOS
  4. * COPYRIGHT (C) 2006 - 2012, 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. * 2006-03-16 Bernard the first version
  23. * 2006-09-07 Bernard move the kservice APIs to rtthread.h
  24. * 2007-06-27 Bernard fix the rt_list_remove bug
  25. * 2012-03-22 Bernard rename kservice.h to rtservice.h
  26. */
  27. #ifndef __RT_SERVICE_H__
  28. #define __RT_SERVICE_H__
  29. #ifdef __cplusplus
  30. extern "C" {
  31. #endif
  32. /**
  33. * @addtogroup KernelService
  34. */
  35. /*@{*/
  36. /**
  37. * @brief initialize a list object
  38. */
  39. #define RT_LIST_OBJECT_INIT(object) { &(object), &(object) }
  40. /**
  41. * @brief initialize a list
  42. *
  43. * @param l list to be initialized
  44. */
  45. rt_inline void rt_list_init(rt_list_t *l)
  46. {
  47. l->next = l->prev = l;
  48. }
  49. /**
  50. * @brief insert a node after a list
  51. *
  52. * @param l list to insert it
  53. * @param n new node to be inserted
  54. */
  55. rt_inline void rt_list_insert_after(rt_list_t *l, rt_list_t *n)
  56. {
  57. l->next->prev = n;
  58. n->next = l->next;
  59. l->next = n;
  60. n->prev = l;
  61. }
  62. /**
  63. * @brief insert a node before a list
  64. *
  65. * @param n new node to be inserted
  66. * @param l list to insert it
  67. */
  68. rt_inline void rt_list_insert_before(rt_list_t *l, rt_list_t *n)
  69. {
  70. l->prev->next = n;
  71. n->prev = l->prev;
  72. l->prev = n;
  73. n->next = l;
  74. }
  75. /**
  76. * @brief remove node from list.
  77. * @param n the node to remove from the list.
  78. */
  79. rt_inline void rt_list_remove(rt_list_t *n)
  80. {
  81. n->next->prev = n->prev;
  82. n->prev->next = n->next;
  83. n->next = n->prev = n;
  84. }
  85. /**
  86. * @brief tests whether a list is empty
  87. * @param l the list to test.
  88. */
  89. rt_inline int rt_list_isempty(const rt_list_t *l)
  90. {
  91. return l->next == l;
  92. }
  93. /**
  94. * @brief get the struct for this entry
  95. * @param node the entry point
  96. * @param type the type of structure
  97. * @param member the name of list in structure
  98. */
  99. #define rt_list_entry(node, type, member) \
  100. ((type *)((char *)(node) - (unsigned long)(&((type *)0)->member)))
  101. /*@}*/
  102. #ifdef __cplusplus
  103. }
  104. #endif
  105. #endif