2010-09-21 14:22:38 +02:00
|
|
|
#include <minix/mthread.h>
|
|
|
|
#include "global.h"
|
2010-09-30 15:44:13 +02:00
|
|
|
#include "proto.h"
|
2010-09-21 14:22:38 +02:00
|
|
|
|
|
|
|
/*===========================================================================*
|
|
|
|
* mthread_queue_add *
|
|
|
|
*===========================================================================*/
|
|
|
|
PUBLIC void mthread_queue_add(queue, thread)
|
|
|
|
mthread_queue_t *queue; /* Queue we want thread to append to */
|
|
|
|
mthread_thread_t thread;
|
|
|
|
{
|
|
|
|
/* Append a thread to the tail of the queue. As a process can be present on
|
|
|
|
* only one queue at the same time, we can use the threads array's 'next'
|
|
|
|
* pointer to point to the next thread on the queue.
|
|
|
|
*/
|
2010-09-30 15:44:13 +02:00
|
|
|
mthread_tcb_t *last;
|
|
|
|
|
|
|
|
if (!isokthreadid(thread))
|
|
|
|
mthread_panic("Can't append invalid thread ID to a queue");
|
|
|
|
|
|
|
|
last = mthread_find_tcb(thread);
|
2010-09-21 14:22:38 +02:00
|
|
|
|
|
|
|
if (mthread_queue_isempty(queue)) {
|
2011-03-18 11:29:54 +01:00
|
|
|
queue->mq_head = queue->mq_tail = last;
|
2010-09-30 15:44:13 +02:00
|
|
|
} else {
|
2011-03-18 11:29:54 +01:00
|
|
|
queue->mq_tail->m_next = last;
|
|
|
|
queue->mq_tail = last; /* 'last' is the new last in line */
|
2010-09-21 14:22:38 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/*===========================================================================*
|
|
|
|
* mthread_queue_init *
|
|
|
|
*===========================================================================*/
|
|
|
|
PUBLIC void mthread_queue_init(queue)
|
|
|
|
mthread_queue_t *queue; /* Queue that has to be initialized */
|
|
|
|
{
|
|
|
|
/* Initialize queue to a known state */
|
|
|
|
|
2011-03-18 11:29:54 +01:00
|
|
|
queue->mq_head = queue->mq_tail = NULL;
|
2010-09-21 14:22:38 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/*===========================================================================*
|
|
|
|
* mthread_queue_isempty *
|
|
|
|
*===========================================================================*/
|
|
|
|
PUBLIC int mthread_queue_isempty(queue)
|
|
|
|
mthread_queue_t *queue;
|
|
|
|
{
|
2011-03-18 11:29:54 +01:00
|
|
|
return(queue->mq_head == NULL);
|
2010-09-21 14:22:38 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/*===========================================================================*
|
|
|
|
* mthread_dump_queue *
|
|
|
|
*===========================================================================*/
|
|
|
|
PUBLIC void mthread_dump_queue(queue)
|
|
|
|
mthread_queue_t *queue;
|
|
|
|
{
|
|
|
|
int threshold, count = 0;
|
2010-09-30 15:44:13 +02:00
|
|
|
mthread_tcb_t *t;
|
|
|
|
mthread_thread_t tid;
|
2010-09-21 14:22:38 +02:00
|
|
|
threshold = no_threads;
|
|
|
|
#ifdef MDEBUG
|
|
|
|
printf("Dumping queue: ");
|
|
|
|
#endif
|
2011-03-18 11:29:54 +01:00
|
|
|
if(queue->mq_head != NULL) {
|
|
|
|
t = queue->mq_head;
|
2010-09-30 15:44:13 +02:00
|
|
|
if (t == &mainthread) tid = MAIN_THREAD;
|
|
|
|
else tid = t->m_tid;
|
2010-09-21 14:22:38 +02:00
|
|
|
#ifdef MDEBUG
|
2010-09-30 15:44:13 +02:00
|
|
|
printf("%d ", tid);
|
2010-09-21 14:22:38 +02:00
|
|
|
#endif
|
|
|
|
count++;
|
2010-09-30 15:44:13 +02:00
|
|
|
t = t->m_next;
|
|
|
|
while (t != NULL) {
|
|
|
|
if (t == &mainthread) tid = MAIN_THREAD;
|
|
|
|
else tid = t->m_tid;
|
2010-09-21 14:22:38 +02:00
|
|
|
#ifdef MDEBUG
|
2010-09-30 15:44:13 +02:00
|
|
|
printf("%d ", tid);
|
2010-09-21 14:22:38 +02:00
|
|
|
#endif
|
2010-09-30 15:44:13 +02:00
|
|
|
t = t->m_next;
|
2010-09-21 14:22:38 +02:00
|
|
|
count++;
|
|
|
|
if (count > threshold) break;
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
#ifdef MDEBUG
|
|
|
|
printf("[empty]");
|
|
|
|
#endif
|
|
|
|
}
|
|
|
|
|
|
|
|
#ifdef MDEBUG
|
|
|
|
printf("\n");
|
|
|
|
#endif
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/*===========================================================================*
|
|
|
|
* mthread_queue_remove *
|
|
|
|
*===========================================================================*/
|
|
|
|
PUBLIC mthread_thread_t mthread_queue_remove(queue)
|
|
|
|
mthread_queue_t *queue; /* Queue we want a thread from */
|
|
|
|
{
|
|
|
|
/* Get the first thread in this queue, if there is one. */
|
2010-09-30 15:44:13 +02:00
|
|
|
mthread_thread_t thread;
|
|
|
|
mthread_tcb_t *tcb;
|
|
|
|
|
|
|
|
/* Calculate thread id from queue head */
|
2011-03-18 11:29:54 +01:00
|
|
|
if (queue->mq_head == NULL) thread = NO_THREAD;
|
|
|
|
else if (queue->mq_head == &mainthread) thread = MAIN_THREAD;
|
|
|
|
else thread = (queue->mq_head->m_tid);
|
2010-09-21 14:22:38 +02:00
|
|
|
|
|
|
|
if (thread != NO_THREAD) { /* i.e., this queue is not empty */
|
2011-03-18 11:29:54 +01:00
|
|
|
tcb = queue->mq_head;
|
|
|
|
if (queue->mq_head == queue->mq_tail) {
|
2010-09-30 15:44:13 +02:00
|
|
|
/* Queue holds only one thread */
|
2011-03-18 11:29:54 +01:00
|
|
|
queue->mq_head = queue->mq_tail = NULL; /* So mark thread empty */
|
2010-09-30 15:44:13 +02:00
|
|
|
} else {
|
2010-09-21 14:22:38 +02:00
|
|
|
/* Second thread in line is the new first */
|
2011-03-18 11:29:54 +01:00
|
|
|
queue->mq_head = queue->mq_head->m_next;
|
2010-09-30 15:44:13 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
tcb->m_next = NULL; /* This thread is no longer part of a queue */
|
2010-09-21 14:22:38 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
return(thread);
|
|
|
|
}
|
|
|
|
|