threadlist.h 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101
  1. /*
  2. * Copyright (c) 2009
  3. * The President and Fellows of Harvard College.
  4. *
  5. * Redistribution and use in source and binary forms, with or without
  6. * modification, are permitted provided that the following conditions
  7. * are met:
  8. * 1. Redistributions of source code must retain the above copyright
  9. * notice, this list of conditions and the following disclaimer.
  10. * 2. Redistributions in binary form must reproduce the above copyright
  11. * notice, this list of conditions and the following disclaimer in the
  12. * documentation and/or other materials provided with the distribution.
  13. * 3. Neither the name of the University nor the names of its contributors
  14. * may be used to endorse or promote products derived from this software
  15. * without specific prior written permission.
  16. *
  17. * THIS SOFTWARE IS PROVIDED BY THE UNIVERSITY AND CONTRIBUTORS ``AS IS'' AND
  18. * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
  19. * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
  20. * ARE DISCLAIMED. IN NO EVENT SHALL THE UNIVERSITY OR CONTRIBUTORS BE LIABLE
  21. * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
  22. * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
  23. * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
  24. * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
  25. * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
  26. * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
  27. * SUCH DAMAGE.
  28. */
  29. #ifndef _THREADLIST_H_
  30. #define _THREADLIST_H_
  31. struct thread; /* from <thread.h> */
  32. /*
  33. * AmigaOS-style linked list of threads.
  34. *
  35. * The two threadlistnodes in the threadlist structure are always on
  36. * the list, as bookends; this removes all the special cases in the
  37. * list handling code. However, note how THREADLIST_FOREACH works: you
  38. * iterate by starting with tl_head.tln_next, and stop when
  39. * itervar->tln_next is null, not when itervar itself becomes null.
  40. *
  41. * ->tln_self always points to the thread that contains the
  42. * threadlistnode. We could avoid this if we wanted to instead use
  43. *
  44. * (struct thread *)((char *)node - offsetof(struct thread, t_listnode))
  45. *
  46. * to get the thread pointer. But that's gross.
  47. */
  48. struct threadlistnode {
  49. struct threadlistnode *tln_prev;
  50. struct threadlistnode *tln_next;
  51. struct thread *tln_self;
  52. };
  53. struct threadlist {
  54. struct threadlistnode tl_head;
  55. struct threadlistnode tl_tail;
  56. unsigned tl_count;
  57. };
  58. /* Initialize and clean up a thread list node. */
  59. void threadlistnode_init(struct threadlistnode *tln, struct thread *self);
  60. void threadlistnode_cleanup(struct threadlistnode *tln);
  61. /* Initialize and clean up a thread list. Must be empty at cleanup. */
  62. void threadlist_init(struct threadlist *tl);
  63. void threadlist_cleanup(struct threadlist *tl);
  64. /* Check if it's empty */
  65. bool threadlist_isempty(struct threadlist *tl);
  66. /* Add and remove: at ends */
  67. void threadlist_addhead(struct threadlist *tl, struct thread *t);
  68. void threadlist_addtail(struct threadlist *tl, struct thread *t);
  69. struct thread *threadlist_remhead(struct threadlist *tl);
  70. struct thread *threadlist_remtail(struct threadlist *tl);
  71. /* Add and remove: in middle. (TL is needed to maintain ->tl_count.) */
  72. void threadlist_insertafter(struct threadlist *tl,
  73. struct thread *onlist, struct thread *addee);
  74. void threadlist_insertbefore(struct threadlist *tl,
  75. struct thread *addee, struct thread *onlist);
  76. void threadlist_remove(struct threadlist *tl, struct thread *t);
  77. /* Iteration; itervar should previously be declared as (struct thread *) */
  78. #define THREADLIST_FORALL(itervar, tl) \
  79. for ((itervar) = (tl).tl_head.tln_next->tln_self; \
  80. (itervar)->t_listnode.tln_next != NULL; \
  81. (itervar) = (itervar)->t_listnode.tln_next->tln_self)
  82. #define THREADLIST_FORALL_REV(itervar, tl) \
  83. for ((itervar) = (tl).tl_tail.tln_prev->tln_self; \
  84. (itervar)->t_listnode.tln_prev != NULL; \
  85. (itervar) = (itervar)->t_listnode.tln_prev->tln_self)
  86. #endif /* _THREADLIST_H_ */