rtservice.h 2.1 KB

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