rtservice.h 2.2 KB

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