queue.c 2.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132
  1. /*
  2. * Mach Operating System
  3. * Copyright (c) 1991,1990,1989,1988,1987 Carnegie Mellon University
  4. * All Rights Reserved.
  5. *
  6. * Permission to use, copy, modify and distribute this software and its
  7. * documentation is hereby granted, provided that both the copyright
  8. * notice and this permission notice appear in all copies of the
  9. * software, derivative works or modified versions, and any portions
  10. * thereof, and that both notices appear in supporting documentation.
  11. *
  12. * CARNEGIE MELLON ALLOWS FREE USE OF THIS SOFTWARE IN ITS "AS IS"
  13. * CONDITION. CARNEGIE MELLON DISCLAIMS ANY LIABILITY OF ANY KIND FOR
  14. * ANY DAMAGES WHATSOEVER RESULTING FROM THE USE OF THIS SOFTWARE.
  15. *
  16. * Carnegie Mellon requests users of this software to return to
  17. *
  18. * Software Distribution Coordinator or Software.Distribution@CS.CMU.EDU
  19. * School of Computer Science
  20. * Carnegie Mellon University
  21. * Pittsburgh PA 15213-3890
  22. *
  23. * any improvements or extensions that they make and grant Carnegie Mellon
  24. * the rights to redistribute these changes.
  25. */
  26. /*
  27. * Routines to implement queue package.
  28. */
  29. #include <kern/queue.h>
  30. /*
  31. * Insert element at head of queue.
  32. */
  33. void enqueue_head(
  34. queue_t que,
  35. queue_entry_t elt)
  36. {
  37. elt->next = que->next;
  38. elt->prev = que;
  39. elt->next->prev = elt;
  40. que->next = elt;
  41. }
  42. /*
  43. * Insert element at tail of queue.
  44. */
  45. void enqueue_tail(
  46. queue_t que,
  47. queue_entry_t elt)
  48. {
  49. elt->next = que;
  50. elt->prev = que->prev;
  51. elt->prev->next = elt;
  52. que->prev = elt;
  53. }
  54. /*
  55. * Remove and return element at head of queue.
  56. */
  57. queue_entry_t dequeue_head(
  58. queue_t que)
  59. {
  60. queue_entry_t elt;
  61. if (que->next == que)
  62. return((queue_entry_t)0);
  63. elt = que->next;
  64. elt->next->prev = que;
  65. que->next = elt->next;
  66. return(elt);
  67. }
  68. /*
  69. * Remove and return element at tail of queue.
  70. */
  71. queue_entry_t dequeue_tail(
  72. queue_t que)
  73. {
  74. queue_entry_t elt;
  75. if (que->prev == que)
  76. return((queue_entry_t)0);
  77. elt = que->prev;
  78. elt->prev->next = que;
  79. que->prev = elt->prev;
  80. return(elt);
  81. }
  82. /*
  83. * Remove arbitrary element from queue.
  84. * Does not check whether element is on queue - the world
  85. * will go haywire if it isn't.
  86. */
  87. /*ARGSUSED*/
  88. void remqueue(
  89. queue_t que,
  90. queue_entry_t elt)
  91. {
  92. elt->next->prev = elt->prev;
  93. elt->prev->next = elt->next;
  94. }
  95. /*
  96. * Routines to directly imitate the VAX hardware queue
  97. * package.
  98. */
  99. void insque(
  100. struct queue_entry *entry,
  101. struct queue_entry *pred)
  102. {
  103. entry->next = pred->next;
  104. entry->prev = pred;
  105. (pred->next)->prev = entry;
  106. pred->next = entry;
  107. }
  108. struct queue_entry
  109. *remque(
  110. struct queue_entry *elt)
  111. {
  112. (elt->next)->prev = elt->prev;
  113. (elt->prev)->next = elt->next;
  114. return(elt);
  115. }