2010-09-21 14:22:38 +02:00
|
|
|
#define ALLOCATE
|
|
|
|
#include <errno.h>
|
|
|
|
#include <minix/mthread.h>
|
2010-10-04 22:19:40 +02:00
|
|
|
#include <string.h>
|
2012-02-13 13:02:12 +01:00
|
|
|
#include <machine/param.h>
|
|
|
|
#include <sys/mman.h>
|
2010-09-21 14:22:38 +02:00
|
|
|
#include "global.h"
|
|
|
|
#include "proto.h"
|
|
|
|
|
2012-03-24 16:16:34 +01:00
|
|
|
FORWARD int mthread_increase_thread_pool(void);
|
|
|
|
FORWARD void mthread_thread_init(mthread_thread_t thread, mthread_attr_t
|
|
|
|
*tattr, void *(*proc)(void *), void *arg);
|
2010-09-21 14:22:38 +02:00
|
|
|
|
2012-03-24 16:16:34 +01:00
|
|
|
FORWARD void mthread_thread_stop(mthread_thread_t thread);
|
|
|
|
FORWARD void mthread_trampoline(void);
|
2010-09-21 14:22:38 +02:00
|
|
|
|
|
|
|
PRIVATE int initialized = 0;
|
2012-02-13 13:02:12 +01:00
|
|
|
#ifndef PGSHIFT
|
|
|
|
# define PGSHIFT 12 /* XXX: temporarily for ACK */
|
|
|
|
#endif
|
|
|
|
#define MTHREAD_GUARDSIZE (1 << PGSHIFT) /* 1 page */
|
2010-09-21 14:22:38 +02:00
|
|
|
|
|
|
|
PRIVATE struct __mthread_attr default_attr = { MTHREAD_STACK_MIN,
|
|
|
|
NULL,
|
|
|
|
MTHREAD_CREATE_JOINABLE,
|
|
|
|
NULL, NULL };
|
|
|
|
|
|
|
|
/*===========================================================================*
|
|
|
|
* mthread_equal *
|
|
|
|
*===========================================================================*/
|
|
|
|
PUBLIC int mthread_equal(l, r)
|
|
|
|
mthread_thread_t l;
|
|
|
|
mthread_thread_t r;
|
|
|
|
{
|
|
|
|
/* Compare two thread ids */
|
|
|
|
mthread_init(); /* Make sure mthreads is initialized */
|
|
|
|
|
|
|
|
return(l == r);
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/*===========================================================================*
|
|
|
|
* mthread_create *
|
|
|
|
*===========================================================================*/
|
|
|
|
PUBLIC int mthread_create(threadid, tattr, proc, arg)
|
|
|
|
mthread_thread_t *threadid;
|
|
|
|
mthread_attr_t *tattr;
|
2011-07-27 11:30:26 +02:00
|
|
|
void *(*proc)(void *);
|
2010-09-21 14:22:38 +02:00
|
|
|
void *arg;
|
|
|
|
{
|
|
|
|
/* Register procedure proc for execution in a thread. */
|
|
|
|
mthread_thread_t thread;
|
|
|
|
|
|
|
|
mthread_init(); /* Make sure mthreads is initialized */
|
|
|
|
|
2011-04-13 16:01:13 +02:00
|
|
|
if (proc == NULL)
|
|
|
|
return(EINVAL);
|
2010-09-21 14:22:38 +02:00
|
|
|
|
|
|
|
if (!mthread_queue_isempty(&free_threads)) {
|
|
|
|
thread = mthread_queue_remove(&free_threads);
|
|
|
|
mthread_thread_init(thread, tattr, proc, arg);
|
|
|
|
used_threads++;
|
|
|
|
if(threadid != NULL)
|
|
|
|
*threadid = (mthread_thread_t) thread;
|
|
|
|
#ifdef MDEBUG
|
|
|
|
printf("Inited thread %d\n", thread);
|
|
|
|
#endif
|
|
|
|
return(0);
|
|
|
|
} else {
|
2011-04-13 16:01:13 +02:00
|
|
|
if (mthread_increase_thread_pool() == -1)
|
|
|
|
return(EAGAIN);
|
2010-09-21 14:22:38 +02:00
|
|
|
|
|
|
|
return mthread_create(threadid, tattr, proc, arg);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/*===========================================================================*
|
|
|
|
* mthread_detach *
|
|
|
|
*===========================================================================*/
|
|
|
|
PUBLIC int mthread_detach(detach)
|
|
|
|
mthread_thread_t detach;
|
|
|
|
{
|
|
|
|
/* Mark a thread as detached. Consequently, upon exit, resources allocated for
|
|
|
|
* this thread are automatically freed.
|
|
|
|
*/
|
2010-09-30 15:44:13 +02:00
|
|
|
mthread_tcb_t *tcb;
|
|
|
|
mthread_init(); /* Make sure libmthread is initialized */
|
2010-09-21 14:22:38 +02:00
|
|
|
|
2011-04-13 16:01:13 +02:00
|
|
|
if (!isokthreadid(detach))
|
|
|
|
return(ESRCH);
|
2010-09-30 15:44:13 +02:00
|
|
|
|
|
|
|
tcb = mthread_find_tcb(detach);
|
|
|
|
if (tcb->m_state == MS_DEAD) {
|
2011-04-13 16:01:13 +02:00
|
|
|
return(ESRCH);
|
2011-03-18 11:29:54 +01:00
|
|
|
} else if (tcb->m_attr.ma_detachstate != MTHREAD_CREATE_DETACHED) {
|
2010-09-30 15:44:13 +02:00
|
|
|
if (tcb->m_state == MS_EXITING)
|
2010-09-21 14:22:38 +02:00
|
|
|
mthread_thread_stop(detach);
|
2010-09-30 15:44:13 +02:00
|
|
|
else
|
2011-03-18 11:29:54 +01:00
|
|
|
tcb->m_attr.ma_detachstate = MTHREAD_CREATE_DETACHED;
|
2010-09-21 14:22:38 +02:00
|
|
|
}
|
2010-09-30 15:44:13 +02:00
|
|
|
|
2010-09-21 14:22:38 +02:00
|
|
|
return(0);
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/*===========================================================================*
|
|
|
|
* mthread_exit *
|
|
|
|
*===========================================================================*/
|
|
|
|
PUBLIC void mthread_exit(value)
|
|
|
|
void *value;
|
|
|
|
{
|
|
|
|
/* Make a thread stop running and store the result value. */
|
2010-09-30 15:44:13 +02:00
|
|
|
mthread_tcb_t *tcb;
|
2010-09-21 14:22:38 +02:00
|
|
|
|
2010-09-30 15:44:13 +02:00
|
|
|
mthread_init(); /* Make sure libmthread is initialized */
|
2010-09-21 14:22:38 +02:00
|
|
|
|
2010-09-30 15:44:13 +02:00
|
|
|
tcb = mthread_find_tcb(current_thread);
|
2010-09-21 14:22:38 +02:00
|
|
|
|
2010-09-30 15:44:13 +02:00
|
|
|
if (tcb->m_state == MS_EXITING) /* Already stopping, nothing to do. */
|
2010-09-21 14:22:38 +02:00
|
|
|
return;
|
|
|
|
|
2011-04-14 13:54:43 +02:00
|
|
|
mthread_cleanup_values();
|
|
|
|
|
2010-09-30 15:44:13 +02:00
|
|
|
tcb->m_result = value;
|
|
|
|
tcb->m_state = MS_EXITING;
|
2010-09-21 14:22:38 +02:00
|
|
|
|
2011-03-18 11:29:54 +01:00
|
|
|
if (tcb->m_attr.ma_detachstate == MTHREAD_CREATE_DETACHED) {
|
2010-09-30 15:44:13 +02:00
|
|
|
mthread_thread_stop(current_thread);
|
2010-09-21 14:22:38 +02:00
|
|
|
} else {
|
|
|
|
/* Joinable thread; notify possibly waiting thread */
|
2010-09-30 15:44:13 +02:00
|
|
|
if (mthread_cond_signal(&(tcb->m_exited)) != 0)
|
2010-09-21 14:22:38 +02:00
|
|
|
mthread_panic("Couldn't signal exit");
|
|
|
|
|
|
|
|
/* The thread that's actually doing the join will eventually clean
|
|
|
|
* up this thread (i.e., call mthread_thread_stop).
|
|
|
|
*/
|
|
|
|
}
|
|
|
|
|
|
|
|
mthread_schedule();
|
2010-09-30 15:44:13 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
/*===========================================================================*
|
|
|
|
* mthread_find_tcb *
|
|
|
|
*===========================================================================*/
|
|
|
|
PUBLIC mthread_tcb_t * mthread_find_tcb(thread)
|
|
|
|
mthread_thread_t thread;
|
|
|
|
{
|
|
|
|
mthread_tcb_t *rt = NULL;
|
|
|
|
|
|
|
|
if (!isokthreadid(thread)) mthread_panic("Invalid thread id");
|
|
|
|
|
|
|
|
if (thread == MAIN_THREAD)
|
|
|
|
rt = &mainthread;
|
|
|
|
else
|
|
|
|
rt = threads[thread];
|
|
|
|
|
|
|
|
return(rt);
|
2010-09-21 14:22:38 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/*===========================================================================*
|
|
|
|
* mthread_increase_thread_pool *
|
|
|
|
*===========================================================================*/
|
|
|
|
PRIVATE int mthread_increase_thread_pool(void)
|
|
|
|
{
|
|
|
|
/* Increase thread pool. No fancy algorithms, just double the size. */
|
2010-09-30 15:44:13 +02:00
|
|
|
mthread_tcb_t **new_tcb;
|
2010-09-21 14:22:38 +02:00
|
|
|
int new_no_threads, old_no_threads, i;
|
|
|
|
|
|
|
|
old_no_threads = no_threads;
|
2010-09-30 15:44:13 +02:00
|
|
|
|
|
|
|
if (old_no_threads == 0)
|
|
|
|
new_no_threads = NO_THREADS;
|
|
|
|
else
|
|
|
|
new_no_threads = 2 * old_no_threads;
|
|
|
|
|
2010-09-21 14:22:38 +02:00
|
|
|
|
|
|
|
if (new_no_threads >= MAX_THREAD_POOL) {
|
|
|
|
mthread_debug("Reached max number of threads");
|
|
|
|
return(-1);
|
|
|
|
}
|
|
|
|
|
2010-09-30 15:44:13 +02:00
|
|
|
/* Allocate space to store pointers to thread control blocks */
|
|
|
|
if (old_no_threads == 0) /* No data yet: allocate space */
|
|
|
|
new_tcb = calloc(new_no_threads, sizeof(mthread_tcb_t *));
|
|
|
|
else /* Preserve existing data: reallocate space */
|
|
|
|
new_tcb = realloc(threads, new_no_threads * sizeof(mthread_tcb_t *));
|
|
|
|
|
2010-09-21 14:22:38 +02:00
|
|
|
if (new_tcb == NULL) {
|
|
|
|
mthread_debug("Can't increase thread pool");
|
|
|
|
return(-1);
|
|
|
|
}
|
|
|
|
|
2010-09-30 15:44:13 +02:00
|
|
|
/* Allocate space for thread control blocks itself */
|
|
|
|
for (i = old_no_threads; i < new_no_threads; i++) {
|
|
|
|
new_tcb[i] = malloc(sizeof(mthread_tcb_t));
|
|
|
|
if (new_tcb[i] == NULL) {
|
|
|
|
mthread_debug("Can't allocate space for tcb");
|
|
|
|
return(-1);
|
|
|
|
}
|
|
|
|
memset(new_tcb[i], '\0', sizeof(mthread_tcb_t)); /* Clear entry */
|
|
|
|
}
|
|
|
|
|
|
|
|
/* We can breath again, let's tell the others about the good news */
|
2010-09-21 14:22:38 +02:00
|
|
|
threads = new_tcb;
|
|
|
|
no_threads = new_no_threads;
|
|
|
|
|
|
|
|
/* Add newly available threads to free_threads */
|
|
|
|
for (i = old_no_threads; i < new_no_threads; i++) {
|
|
|
|
mthread_queue_add(&free_threads, i);
|
|
|
|
mthread_thread_reset(i);
|
|
|
|
}
|
|
|
|
|
|
|
|
#ifdef MDEBUG
|
2010-09-30 15:44:13 +02:00
|
|
|
printf("Increased thread pool from %d to %d threads\n", old_no_threads,
|
|
|
|
new_no_threads);
|
2010-09-21 14:22:38 +02:00
|
|
|
#endif
|
|
|
|
return(0);
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/*===========================================================================*
|
|
|
|
* mthread_init *
|
|
|
|
*===========================================================================*/
|
|
|
|
PUBLIC void mthread_init(void)
|
|
|
|
{
|
|
|
|
/* Initialize thread system; allocate thread structures and start creating
|
|
|
|
* threads.
|
|
|
|
*/
|
|
|
|
|
|
|
|
if (!initialized) {
|
2010-09-30 15:44:13 +02:00
|
|
|
no_threads = 0;
|
2010-09-21 14:22:38 +02:00
|
|
|
used_threads = 0;
|
2012-02-13 13:02:12 +01:00
|
|
|
need_reset = 0;
|
2010-09-21 14:22:38 +02:00
|
|
|
running_main_thread = 1;/* mthread_init can only be called from the
|
|
|
|
* main thread. Calling it from a thread will
|
|
|
|
* not enter this clause.
|
|
|
|
*/
|
|
|
|
|
2011-03-18 11:29:54 +01:00
|
|
|
if (mthread_getcontext(&(mainthread.m_context)) == -1)
|
2010-09-21 14:22:38 +02:00
|
|
|
mthread_panic("Couldn't save state for main thread");
|
2010-09-30 15:44:13 +02:00
|
|
|
current_thread = MAIN_THREAD;
|
2010-09-21 14:22:38 +02:00
|
|
|
|
|
|
|
mthread_init_valid_mutexes();
|
|
|
|
mthread_init_valid_conditions();
|
|
|
|
mthread_init_valid_attributes();
|
2011-04-14 13:54:43 +02:00
|
|
|
mthread_init_keys();
|
2010-09-21 14:22:38 +02:00
|
|
|
mthread_init_scheduler();
|
|
|
|
|
|
|
|
initialized = 1;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/*===========================================================================*
|
|
|
|
* mthread_join *
|
|
|
|
*===========================================================================*/
|
|
|
|
PUBLIC int mthread_join(join, value)
|
|
|
|
mthread_thread_t join;
|
|
|
|
void **value;
|
|
|
|
{
|
|
|
|
/* Wait for a thread to stop running and copy the result. */
|
|
|
|
|
2010-09-30 15:44:13 +02:00
|
|
|
mthread_tcb_t *tcb;
|
|
|
|
|
|
|
|
mthread_init(); /* Make sure libmthread is initialized */
|
2010-09-21 14:22:38 +02:00
|
|
|
|
2011-04-13 16:01:13 +02:00
|
|
|
if (!isokthreadid(join))
|
|
|
|
return(ESRCH);
|
|
|
|
else if (join == current_thread)
|
|
|
|
return(EDEADLK);
|
2010-09-30 15:44:13 +02:00
|
|
|
|
|
|
|
tcb = mthread_find_tcb(join);
|
2011-04-13 16:01:13 +02:00
|
|
|
if (tcb->m_state == MS_DEAD)
|
|
|
|
return(ESRCH);
|
|
|
|
else if (tcb->m_attr.ma_detachstate == MTHREAD_CREATE_DETACHED)
|
|
|
|
return(EINVAL);
|
2010-09-21 14:22:38 +02:00
|
|
|
|
|
|
|
/* When the thread hasn't exited yet, we have to wait for that to happen */
|
2010-09-30 15:44:13 +02:00
|
|
|
if (tcb->m_state != MS_EXITING) {
|
2010-09-21 14:22:38 +02:00
|
|
|
mthread_cond_t *c;
|
|
|
|
mthread_mutex_t *m;
|
|
|
|
|
2010-09-30 15:44:13 +02:00
|
|
|
c = &(tcb->m_exited);
|
|
|
|
m = &(tcb->m_exitm);
|
2010-09-21 14:22:38 +02:00
|
|
|
|
|
|
|
if (mthread_mutex_init(m, NULL) != 0)
|
|
|
|
mthread_panic("Couldn't initialize mutex to join\n");
|
|
|
|
|
|
|
|
if (mthread_mutex_lock(m) != 0)
|
|
|
|
mthread_panic("Couldn't lock mutex to join\n");
|
|
|
|
|
|
|
|
if (mthread_cond_wait(c, m) != 0)
|
|
|
|
mthread_panic("Couldn't wait for join condition\n");
|
|
|
|
|
|
|
|
if (mthread_mutex_unlock(m) != 0)
|
|
|
|
mthread_panic("Couldn't unlock mutex to join\n");
|
|
|
|
|
|
|
|
if (mthread_mutex_destroy(m) != 0)
|
|
|
|
mthread_panic("Couldn't destroy mutex to join\n");
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Thread has exited; copy results */
|
|
|
|
if(value != NULL)
|
2010-09-30 15:44:13 +02:00
|
|
|
*value = tcb->m_result;
|
2010-09-21 14:22:38 +02:00
|
|
|
|
|
|
|
/* Deallocate resources */
|
|
|
|
mthread_thread_stop(join);
|
|
|
|
return(0);
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/*===========================================================================*
|
|
|
|
* mthread_once *
|
|
|
|
*===========================================================================*/
|
|
|
|
PUBLIC int mthread_once(once, proc)
|
|
|
|
mthread_once_t *once;
|
|
|
|
void (*proc)(void);
|
|
|
|
{
|
|
|
|
/* Run procedure proc just once */
|
|
|
|
|
2010-09-30 15:44:13 +02:00
|
|
|
mthread_init(); /* Make sure libmthread is initialized */
|
2010-09-21 14:22:38 +02:00
|
|
|
|
2011-04-13 16:01:13 +02:00
|
|
|
if (once == NULL || proc == NULL)
|
|
|
|
return(EINVAL);
|
2010-09-21 14:22:38 +02:00
|
|
|
|
|
|
|
if (*once != 1) proc();
|
|
|
|
*once = 1;
|
|
|
|
return(0);
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/*===========================================================================*
|
|
|
|
* mthread_self *
|
|
|
|
*===========================================================================*/
|
|
|
|
PUBLIC mthread_thread_t mthread_self(void)
|
|
|
|
{
|
|
|
|
/* Return the thread id of the thread calling this function. */
|
|
|
|
|
2010-09-30 15:44:13 +02:00
|
|
|
mthread_init(); /* Make sure libmthread is initialized */
|
2010-09-21 14:22:38 +02:00
|
|
|
|
|
|
|
return(current_thread);
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/*===========================================================================*
|
|
|
|
* mthread_thread_init *
|
|
|
|
*===========================================================================*/
|
|
|
|
PRIVATE void mthread_thread_init(thread, tattr, proc, arg)
|
|
|
|
mthread_thread_t thread;
|
|
|
|
mthread_attr_t *tattr;
|
2011-07-27 11:30:26 +02:00
|
|
|
void *(*proc)(void *);
|
2010-09-21 14:22:38 +02:00
|
|
|
void *arg;
|
|
|
|
{
|
|
|
|
/* Initialize a thread so that it, when unsuspended, will run the given
|
|
|
|
* procedure with the given parameter. The thread is marked as runnable.
|
|
|
|
*/
|
|
|
|
|
2010-09-30 15:44:13 +02:00
|
|
|
#define THIS_CTX (&(threads[thread]->m_context))
|
|
|
|
mthread_tcb_t *tcb;
|
2010-09-21 14:22:38 +02:00
|
|
|
size_t stacksize;
|
|
|
|
char *stackaddr;
|
|
|
|
|
2010-09-30 15:44:13 +02:00
|
|
|
tcb = mthread_find_tcb(thread);
|
|
|
|
tcb->m_next = NULL;
|
|
|
|
tcb->m_state = MS_DEAD;
|
2011-07-27 11:30:26 +02:00
|
|
|
tcb->m_proc = proc;
|
2010-09-30 15:44:13 +02:00
|
|
|
tcb->m_arg = arg;
|
2010-09-21 14:22:38 +02:00
|
|
|
/* Threads use a copy of the provided attributes. This way, if another
|
|
|
|
* thread modifies the attributes (such as detach state), already running
|
|
|
|
* threads are not affected.
|
|
|
|
*/
|
|
|
|
if (tattr != NULL)
|
2010-09-30 15:44:13 +02:00
|
|
|
tcb->m_attr = *((struct __mthread_attr *) *tattr);
|
2010-09-21 14:22:38 +02:00
|
|
|
else {
|
2010-09-30 15:44:13 +02:00
|
|
|
tcb->m_attr = default_attr;
|
2010-09-21 14:22:38 +02:00
|
|
|
}
|
|
|
|
|
2010-09-30 15:44:13 +02:00
|
|
|
if (mthread_cond_init(&(tcb->m_exited), NULL) != 0)
|
2010-09-21 14:22:38 +02:00
|
|
|
mthread_panic("Could not initialize thread");
|
|
|
|
|
2011-07-27 11:30:26 +02:00
|
|
|
tcb->m_context.uc_link = NULL;
|
2010-09-21 14:22:38 +02:00
|
|
|
|
2011-07-27 11:30:26 +02:00
|
|
|
/* Construct this thread's context to run procedure proc. */
|
2011-03-18 11:29:54 +01:00
|
|
|
if (mthread_getcontext(&(tcb->m_context)) == -1)
|
2010-09-21 14:22:38 +02:00
|
|
|
mthread_panic("Failed to initialize context state");
|
|
|
|
|
2011-03-18 11:29:54 +01:00
|
|
|
stacksize = tcb->m_attr.ma_stacksize;
|
|
|
|
stackaddr = tcb->m_attr.ma_stackaddr;
|
2010-09-21 14:22:38 +02:00
|
|
|
|
2012-02-13 13:02:12 +01:00
|
|
|
if (stacksize == (size_t) 0) {
|
|
|
|
/* User provided too small a stack size. Forget about that stack and
|
|
|
|
* allocate a new one ourselves.
|
|
|
|
*/
|
2010-09-21 14:22:38 +02:00
|
|
|
stacksize = (size_t) MTHREAD_STACK_MIN;
|
2012-02-13 13:02:12 +01:00
|
|
|
tcb->m_attr.ma_stackaddr = stackaddr = NULL;
|
|
|
|
}
|
2010-09-21 14:22:38 +02:00
|
|
|
|
|
|
|
if (stackaddr == NULL) {
|
|
|
|
/* Allocate stack space */
|
2012-02-13 13:02:12 +01:00
|
|
|
size_t guarded_stacksize;
|
|
|
|
char *guard_start, *guard_end;
|
|
|
|
|
|
|
|
stacksize = round_page(stacksize + MTHREAD_GUARDSIZE);
|
|
|
|
stackaddr = minix_mmap(NULL, stacksize,
|
|
|
|
PROT_READ|PROT_WRITE, MAP_ANON|MAP_PRIVATE,
|
|
|
|
-1, 0);
|
|
|
|
if (stackaddr == NULL)
|
2010-09-21 14:22:38 +02:00
|
|
|
mthread_panic("Failed to allocate stack to thread");
|
2012-02-13 13:02:12 +01:00
|
|
|
|
|
|
|
#if (_MINIX_CHIP == _CHIP_INTEL)
|
|
|
|
guard_start = stackaddr;
|
|
|
|
guard_end = stackaddr + MTHREAD_GUARDSIZE;
|
|
|
|
guarded_stacksize = stackaddr + stacksize - guard_end;
|
|
|
|
|
|
|
|
/* The stack will be used from (stackaddr+stacksize) to stackaddr. That
|
|
|
|
* is, growing downwards. So the "top" of the stack may not grow into
|
2012-02-13 14:42:28 +01:00
|
|
|
* stackaddr+MTHREAD_GUARDSIZE.
|
2012-02-13 13:02:12 +01:00
|
|
|
*
|
|
|
|
* +-------+ stackaddr + stacksize
|
|
|
|
* | |
|
|
|
|
* | | |
|
|
|
|
* | \|/ |
|
|
|
|
* | |
|
2012-02-13 14:42:28 +01:00
|
|
|
* +-------+ stackaddr + MTHREAD_GUARDSIZE
|
2012-02-13 13:02:12 +01:00
|
|
|
* | GUARD |
|
|
|
|
* +-------+ stackaddr
|
|
|
|
*/
|
|
|
|
#else
|
|
|
|
# error "Unsupported platform"
|
|
|
|
#endif
|
|
|
|
stacksize = guarded_stacksize;
|
|
|
|
if (minix_munmap(guard_start, MTHREAD_GUARDSIZE) != 0)
|
|
|
|
mthread_panic("unable to unmap stack space for guard");
|
|
|
|
tcb->m_context.uc_stack.ss_sp = guard_end;
|
2010-09-21 14:22:38 +02:00
|
|
|
} else
|
2010-09-30 15:44:13 +02:00
|
|
|
tcb->m_context.uc_stack.ss_sp = stackaddr;
|
2010-09-21 14:22:38 +02:00
|
|
|
|
2010-09-30 15:44:13 +02:00
|
|
|
tcb->m_context.uc_stack.ss_size = stacksize;
|
|
|
|
makecontext(&(tcb->m_context), mthread_trampoline, 0);
|
2010-09-21 14:22:38 +02:00
|
|
|
|
|
|
|
mthread_unsuspend(thread); /* Make thread runnable */
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/*===========================================================================*
|
|
|
|
* mthread_thread_reset *
|
|
|
|
*===========================================================================*/
|
2012-02-13 13:02:12 +01:00
|
|
|
PUBLIC void mthread_thread_reset(thread)
|
2010-09-21 14:22:38 +02:00
|
|
|
mthread_thread_t thread;
|
|
|
|
{
|
|
|
|
/* Reset the thread to its default values. Free the allocated stack space. */
|
|
|
|
|
|
|
|
mthread_tcb_t *rt;
|
|
|
|
if (!isokthreadid(thread)) mthread_panic("Invalid thread id");
|
|
|
|
|
2010-09-30 15:44:13 +02:00
|
|
|
rt = mthread_find_tcb(thread);
|
|
|
|
rt->m_tid = thread;
|
|
|
|
rt->m_next = NULL;
|
|
|
|
rt->m_state = MS_DEAD;
|
2010-09-21 14:22:38 +02:00
|
|
|
rt->m_proc = NULL;
|
|
|
|
rt->m_arg = NULL;
|
|
|
|
rt->m_result = NULL;
|
|
|
|
rt->m_cond = NULL;
|
2012-02-10 10:16:00 +01:00
|
|
|
if (rt->m_attr.ma_stackaddr == NULL) { /* We allocated stack space */
|
2012-02-13 13:02:12 +01:00
|
|
|
if (rt->m_context.uc_stack.ss_sp) {
|
|
|
|
if (minix_munmap(rt->m_context.uc_stack.ss_sp,
|
|
|
|
rt->m_context.uc_stack.ss_size) != 0) {
|
|
|
|
mthread_panic("unable to unmap memory");
|
|
|
|
}
|
|
|
|
}
|
2012-02-10 10:16:00 +01:00
|
|
|
rt->m_context.uc_stack.ss_sp = NULL;
|
2011-03-18 11:29:54 +01:00
|
|
|
}
|
2010-09-21 14:22:38 +02:00
|
|
|
rt->m_context.uc_stack.ss_size = 0;
|
|
|
|
rt->m_context.uc_link = NULL;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/*===========================================================================*
|
|
|
|
* mthread_thread_stop *
|
|
|
|
*===========================================================================*/
|
|
|
|
PRIVATE void mthread_thread_stop(thread)
|
|
|
|
mthread_thread_t thread;
|
|
|
|
{
|
|
|
|
/* Stop thread from running. Deallocate resources. */
|
|
|
|
mthread_tcb_t *stop_thread;
|
|
|
|
|
|
|
|
if (!isokthreadid(thread)) mthread_panic("Invalid thread id");
|
|
|
|
|
2010-09-30 15:44:13 +02:00
|
|
|
stop_thread = mthread_find_tcb(thread);
|
2010-09-21 14:22:38 +02:00
|
|
|
|
2010-09-30 15:44:13 +02:00
|
|
|
if (stop_thread->m_state == MS_DEAD) {
|
|
|
|
/* Already dead, nothing to do */
|
2010-09-21 14:22:38 +02:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (mthread_cond_destroy(&(stop_thread->m_exited)) != 0)
|
|
|
|
mthread_panic("Could not destroy condition at thread deallocation\n");
|
|
|
|
|
2012-02-13 13:02:12 +01:00
|
|
|
/* Can't deallocate ourselves (i.e., we're a detached thread) */
|
|
|
|
if (thread == current_thread) {
|
|
|
|
stop_thread->m_state = MS_NEEDRESET;
|
|
|
|
need_reset++;
|
|
|
|
} else {
|
|
|
|
mthread_thread_reset(thread);
|
|
|
|
used_threads--;
|
|
|
|
mthread_queue_add(&free_threads, thread);
|
|
|
|
}
|
2010-09-21 14:22:38 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/*===========================================================================*
|
|
|
|
* mthread_trampoline *
|
|
|
|
*===========================================================================*/
|
|
|
|
PRIVATE void mthread_trampoline(void)
|
|
|
|
{
|
|
|
|
/* Execute the /current_thread's/ procedure. Store its result. */
|
|
|
|
|
2010-09-30 15:44:13 +02:00
|
|
|
mthread_tcb_t *tcb;
|
2010-09-21 14:22:38 +02:00
|
|
|
void *r;
|
|
|
|
|
2010-09-30 15:44:13 +02:00
|
|
|
tcb = mthread_find_tcb(current_thread);
|
|
|
|
|
|
|
|
r = (tcb->m_proc)(tcb->m_arg);
|
2010-09-21 14:22:38 +02:00
|
|
|
mthread_exit(r);
|
|
|
|
}
|
|
|
|
|