VM: munmap used by VM for itself is no longer used
[minix.git] / lib / libmthread / queue.c
blob333d5458e65e7975be930b2c7abf40d62ed7b3fe
1 #include <minix/mthread.h>
2 #include "global.h"
3 #include "proto.h"
5 /*===========================================================================*
6 * mthread_queue_add *
7 *===========================================================================*/
8 void mthread_queue_add(queue, thread)
9 mthread_queue_t *queue; /* Queue we want thread to append to */
10 mthread_thread_t thread;
12 /* Append a thread to the tail of the queue. As a process can be present on
13 * only one queue at the same time, we can use the threads array's 'next'
14 * pointer to point to the next thread on the queue.
16 mthread_tcb_t *last;
18 if (!isokthreadid(thread))
19 mthread_panic("Can't append invalid thread ID to a queue");
21 last = mthread_find_tcb(thread);
23 if (mthread_queue_isempty(queue)) {
24 queue->mq_head = queue->mq_tail = last;
25 } else {
26 queue->mq_tail->m_next = last;
27 queue->mq_tail = last; /* 'last' is the new last in line */
32 /*===========================================================================*
33 * mthread_queue_init *
34 *===========================================================================*/
35 void mthread_queue_init(queue)
36 mthread_queue_t *queue; /* Queue that has to be initialized */
38 /* Initialize queue to a known state */
40 queue->mq_head = queue->mq_tail = NULL;
44 /*===========================================================================*
45 * mthread_queue_isempty *
46 *===========================================================================*/
47 int mthread_queue_isempty(queue)
48 mthread_queue_t *queue;
50 return(queue->mq_head == NULL);
54 /*===========================================================================*
55 * mthread_dump_queue *
56 *===========================================================================*/
57 #ifdef MDEBUG
58 void mthread_dump_queue(queue)
59 mthread_queue_t *queue;
61 int threshold, count = 0;
62 mthread_tcb_t *t;
63 mthread_thread_t tid;
64 threshold = no_threads;
65 printf("Dumping queue: ");
67 if(queue->mq_head != NULL) {
68 t = queue->mq_head;
69 if (t == &mainthread) tid = MAIN_THREAD;
70 else tid = t->m_tid;
71 printf("%d ", tid);
72 count++;
73 t = t->m_next;
74 while (t != NULL) {
75 if (t == &mainthread) tid = MAIN_THREAD;
76 else tid = t->m_tid;
77 printf("%d ", tid);
78 t = t->m_next;
79 count++;
80 if (count > threshold) break;
82 } else {
83 printf("[empty]");
86 printf("\n");
88 #endif
90 /*===========================================================================*
91 * mthread_queue_remove *
92 *===========================================================================*/
93 mthread_thread_t mthread_queue_remove(queue)
94 mthread_queue_t *queue; /* Queue we want a thread from */
96 /* Get the first thread in this queue, if there is one. */
97 mthread_thread_t thread;
98 mthread_tcb_t *tcb;
100 /* Calculate thread id from queue head */
101 if (queue->mq_head == NULL) thread = NO_THREAD;
102 else if (queue->mq_head == &mainthread) thread = MAIN_THREAD;
103 else thread = (queue->mq_head->m_tid);
105 if (thread != NO_THREAD) { /* i.e., this queue is not empty */
106 tcb = queue->mq_head;
107 if (queue->mq_head == queue->mq_tail) {
108 /* Queue holds only one thread */
109 queue->mq_head = queue->mq_tail = NULL; /* So mark thread empty */
110 } else {
111 /* Second thread in line is the new first */
112 queue->mq_head = queue->mq_head->m_next;
115 tcb->m_next = NULL; /* This thread is no longer part of a queue */
118 return(thread);