threadlist.c 5.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240
  1. /*
  2. * Copyright (c) 2000, 2001, 2002, 2003, 2004, 2005, 2008, 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. /*
  30. * Thread list functions, rather dull.
  31. */
  32. #include <types.h>
  33. #include <lib.h>
  34. #include <thread.h>
  35. #include <threadlist.h>
  36. void
  37. threadlistnode_init(struct threadlistnode *tln, struct thread *t)
  38. {
  39. DEBUGASSERT(tln != NULL);
  40. KASSERT(t != NULL);
  41. tln->tln_next = NULL;
  42. tln->tln_prev = NULL;
  43. tln->tln_self = t;
  44. }
  45. void
  46. threadlistnode_cleanup(struct threadlistnode *tln)
  47. {
  48. DEBUGASSERT(tln != NULL);
  49. KASSERT(tln->tln_next == NULL);
  50. KASSERT(tln->tln_prev == NULL);
  51. KASSERT(tln->tln_self != NULL);
  52. }
  53. void
  54. threadlist_init(struct threadlist *tl)
  55. {
  56. DEBUGASSERT(tl != NULL);
  57. tl->tl_head.tln_next = &tl->tl_tail;
  58. tl->tl_head.tln_prev = NULL;
  59. tl->tl_tail.tln_next = NULL;
  60. tl->tl_tail.tln_prev = &tl->tl_head;
  61. tl->tl_head.tln_self = NULL;
  62. tl->tl_tail.tln_self = NULL;
  63. tl->tl_count = 0;
  64. }
  65. void
  66. threadlist_cleanup(struct threadlist *tl)
  67. {
  68. DEBUGASSERT(tl != NULL);
  69. DEBUGASSERT(tl->tl_head.tln_next == &tl->tl_tail);
  70. DEBUGASSERT(tl->tl_head.tln_prev == NULL);
  71. DEBUGASSERT(tl->tl_tail.tln_next == NULL);
  72. DEBUGASSERT(tl->tl_tail.tln_prev == &tl->tl_head);
  73. DEBUGASSERT(tl->tl_head.tln_self == NULL);
  74. DEBUGASSERT(tl->tl_tail.tln_self == NULL);
  75. KASSERT(threadlist_isempty(tl));
  76. KASSERT(tl->tl_count == 0);
  77. /* nothing (else) to do */
  78. }
  79. bool
  80. threadlist_isempty(struct threadlist *tl)
  81. {
  82. DEBUGASSERT(tl != NULL);
  83. return (tl->tl_count == 0);
  84. }
  85. ////////////////////////////////////////////////////////////
  86. // internal
  87. /*
  88. * Do insertion. Doesn't update tl_count.
  89. */
  90. static
  91. void
  92. threadlist_insertafternode(struct threadlistnode *onlist, struct thread *t)
  93. {
  94. struct threadlistnode *addee;
  95. addee = &t->t_listnode;
  96. DEBUGASSERT(addee->tln_prev == NULL);
  97. DEBUGASSERT(addee->tln_next == NULL);
  98. addee->tln_prev = onlist;
  99. addee->tln_next = onlist->tln_next;
  100. addee->tln_prev->tln_next = addee;
  101. addee->tln_next->tln_prev = addee;
  102. }
  103. /*
  104. * Do insertion. Doesn't update tl_count.
  105. */
  106. static
  107. void
  108. threadlist_insertbeforenode(struct thread *t, struct threadlistnode *onlist)
  109. {
  110. struct threadlistnode *addee;
  111. addee = &t->t_listnode;
  112. DEBUGASSERT(addee->tln_prev == NULL);
  113. DEBUGASSERT(addee->tln_next == NULL);
  114. addee->tln_prev = onlist->tln_prev;
  115. addee->tln_next = onlist;
  116. addee->tln_prev->tln_next = addee;
  117. addee->tln_next->tln_prev = addee;
  118. }
  119. /*
  120. * Do removal. Doesn't update tl_count.
  121. */
  122. static
  123. void
  124. threadlist_removenode(struct threadlistnode *tln)
  125. {
  126. DEBUGASSERT(tln != NULL);
  127. DEBUGASSERT(tln->tln_prev != NULL);
  128. DEBUGASSERT(tln->tln_next != NULL);
  129. tln->tln_prev->tln_next = tln->tln_next;
  130. tln->tln_next->tln_prev = tln->tln_prev;
  131. tln->tln_prev = NULL;
  132. tln->tln_next = NULL;
  133. }
  134. ////////////////////////////////////////////////////////////
  135. // public
  136. void
  137. threadlist_addhead(struct threadlist *tl, struct thread *t)
  138. {
  139. DEBUGASSERT(tl != NULL);
  140. DEBUGASSERT(t != NULL);
  141. threadlist_insertafternode(&tl->tl_head, t);
  142. tl->tl_count++;
  143. }
  144. void
  145. threadlist_addtail(struct threadlist *tl, struct thread *t)
  146. {
  147. DEBUGASSERT(tl != NULL);
  148. DEBUGASSERT(t != NULL);
  149. threadlist_insertbeforenode(t, &tl->tl_tail);
  150. tl->tl_count++;
  151. }
  152. struct thread *
  153. threadlist_remhead(struct threadlist *tl)
  154. {
  155. struct threadlistnode *tln;
  156. DEBUGASSERT(tl != NULL);
  157. tln = tl->tl_head.tln_next;
  158. if (tln->tln_next == NULL) {
  159. /* list was empty */
  160. return NULL;
  161. }
  162. threadlist_removenode(tln);
  163. DEBUGASSERT(tl->tl_count > 0);
  164. tl->tl_count--;
  165. return tln->tln_self;
  166. }
  167. struct thread *
  168. threadlist_remtail(struct threadlist *tl)
  169. {
  170. struct threadlistnode *tln;
  171. DEBUGASSERT(tl != NULL);
  172. tln = tl->tl_tail.tln_prev;
  173. if (tln->tln_prev == NULL) {
  174. /* list was empty */
  175. return NULL;
  176. }
  177. threadlist_removenode(tln);
  178. DEBUGASSERT(tl->tl_count > 0);
  179. tl->tl_count--;
  180. return tln->tln_self;
  181. }
  182. void
  183. threadlist_insertafter(struct threadlist *tl,
  184. struct thread *onlist, struct thread *addee)
  185. {
  186. threadlist_insertafternode(&onlist->t_listnode, addee);
  187. tl->tl_count++;
  188. }
  189. void
  190. threadlist_insertbefore(struct threadlist *tl,
  191. struct thread *addee, struct thread *onlist)
  192. {
  193. threadlist_insertbeforenode(addee, &onlist->t_listnode);
  194. tl->tl_count++;
  195. }
  196. void
  197. threadlist_remove(struct threadlist *tl, struct thread *t)
  198. {
  199. threadlist_removenode(&t->t_listnode);
  200. DEBUGASSERT(tl->tl_count > 0);
  201. tl->tl_count--;
  202. }