kservice.h 1.9 KB

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