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
|
|
|
|
2011-03-18 11:29:54 +01:00
|
|
|
#ifdef MTHREAD_STRICT
|
2012-03-25 20:25:53 +02:00
|
|
|
static struct __mthread_cond *vc_front, *vc_rear;
|
|
|
|
static void mthread_cond_add(mthread_cond_t *c);
|
|
|
|
static void mthread_cond_remove(mthread_cond_t *c);
|
|
|
|
static int mthread_cond_valid(mthread_cond_t *c);
|
2011-03-18 11:29:54 +01:00
|
|
|
#else
|
|
|
|
# define mthread_cond_add(c) ((*c)->mc_magic = MTHREAD_INIT_MAGIC)
|
|
|
|
# define mthread_cond_remove(c) ((*c)->mc_magic = MTHREAD_NOT_INUSE)
|
|
|
|
# define mthread_cond_valid(c) ((*c)->mc_magic == MTHREAD_INIT_MAGIC)
|
|
|
|
#endif
|
2010-09-30 15:44:13 +02:00
|
|
|
#define MAIN_COND mainthread.m_cond
|
2010-09-21 14:22:38 +02:00
|
|
|
|
|
|
|
/*===========================================================================*
|
|
|
|
* mthread_init_valid_conditions *
|
|
|
|
*===========================================================================*/
|
2012-03-25 20:25:53 +02:00
|
|
|
void mthread_init_valid_conditions(void)
|
2010-09-21 14:22:38 +02:00
|
|
|
{
|
2011-07-27 11:30:26 +02:00
|
|
|
#ifdef MTHREAD_STRICT
|
2010-09-21 14:22:38 +02:00
|
|
|
/* Initialize condition variable list */
|
|
|
|
vc_front = vc_rear = NULL;
|
2011-07-27 11:30:26 +02:00
|
|
|
#endif
|
2010-09-21 14:22:38 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/*===========================================================================*
|
|
|
|
* mthread_cond_add *
|
|
|
|
*===========================================================================*/
|
2011-03-18 11:29:54 +01:00
|
|
|
#ifdef MTHREAD_STRICT
|
2012-03-25 20:25:53 +02:00
|
|
|
static void mthread_cond_add(c)
|
2010-09-21 14:22:38 +02:00
|
|
|
mthread_cond_t *c;
|
|
|
|
{
|
|
|
|
/* Add condition to list of valid, initialized conditions */
|
|
|
|
|
|
|
|
if (vc_front == NULL) { /* Empty list */
|
|
|
|
vc_front = *c;
|
2011-03-18 11:29:54 +01:00
|
|
|
(*c)->mc_prev = NULL;
|
2010-09-21 14:22:38 +02:00
|
|
|
} else {
|
2011-03-18 11:29:54 +01:00
|
|
|
vc_rear->mc_next = *c;
|
|
|
|
(*c)->mc_prev = vc_rear;
|
2010-09-21 14:22:38 +02:00
|
|
|
}
|
|
|
|
|
2011-03-18 11:29:54 +01:00
|
|
|
(*c)->mc_next = NULL;
|
2010-09-21 14:22:38 +02:00
|
|
|
vc_rear = *c;
|
|
|
|
}
|
2011-03-18 11:29:54 +01:00
|
|
|
#endif
|
2010-09-21 14:22:38 +02:00
|
|
|
|
|
|
|
/*===========================================================================*
|
|
|
|
* mthread_cond_broadcast *
|
|
|
|
*===========================================================================*/
|
2012-03-25 20:25:53 +02:00
|
|
|
int mthread_cond_broadcast(cond)
|
2010-09-21 14:22:38 +02:00
|
|
|
mthread_cond_t *cond;
|
|
|
|
{
|
|
|
|
/* Signal all threads waiting for condition 'cond'. */
|
2010-09-30 15:44:13 +02:00
|
|
|
mthread_thread_t t;
|
|
|
|
mthread_tcb_t *tcb;
|
2010-09-21 14:22:38 +02:00
|
|
|
|
2012-05-01 11:44:49 +02:00
|
|
|
MTHREAD_CHECK_INIT(); /* Make sure libmthread is initialized */
|
2010-09-21 14:22:38 +02:00
|
|
|
|
2011-04-13 16:01:13 +02:00
|
|
|
if (cond == NULL)
|
|
|
|
return(EINVAL);
|
|
|
|
else if (!mthread_cond_valid(cond))
|
|
|
|
return(EINVAL);
|
2010-09-21 14:22:38 +02:00
|
|
|
|
2010-09-30 15:44:13 +02:00
|
|
|
tcb = mthread_find_tcb(MAIN_THREAD);
|
|
|
|
if (tcb->m_state == MS_CONDITION && tcb->m_cond == *cond)
|
|
|
|
mthread_unsuspend(MAIN_THREAD);
|
|
|
|
|
|
|
|
for (t = (mthread_thread_t) 0; t < no_threads; t++) {
|
|
|
|
tcb = mthread_find_tcb(t);
|
|
|
|
if (tcb->m_state == MS_CONDITION && tcb->m_cond == *cond)
|
|
|
|
mthread_unsuspend(t);
|
|
|
|
}
|
2010-09-21 14:22:38 +02:00
|
|
|
|
|
|
|
return(0);
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/*===========================================================================*
|
|
|
|
* mthread_cond_destroy *
|
|
|
|
*===========================================================================*/
|
2012-03-25 20:25:53 +02:00
|
|
|
int mthread_cond_destroy(cond)
|
2010-09-21 14:22:38 +02:00
|
|
|
mthread_cond_t *cond;
|
|
|
|
{
|
|
|
|
/* Destroy a condition variable. Make sure it's not in use */
|
2010-09-30 15:44:13 +02:00
|
|
|
mthread_thread_t t;
|
|
|
|
mthread_tcb_t *tcb;
|
2010-09-21 14:22:38 +02:00
|
|
|
|
2012-05-01 11:44:49 +02:00
|
|
|
MTHREAD_CHECK_INIT(); /* Make sure libmthread is initialized */
|
2010-09-21 14:22:38 +02:00
|
|
|
|
2011-04-13 16:01:13 +02:00
|
|
|
if (cond == NULL)
|
|
|
|
return(EINVAL);
|
|
|
|
else if (!mthread_cond_valid(cond))
|
|
|
|
return(EINVAL);
|
2010-09-21 14:22:38 +02:00
|
|
|
|
|
|
|
/* Is another thread currently using this condition variable? */
|
2010-09-30 15:44:13 +02:00
|
|
|
tcb = mthread_find_tcb(MAIN_THREAD);
|
2011-04-13 16:01:13 +02:00
|
|
|
if (tcb->m_state == MS_CONDITION && tcb->m_cond == *cond)
|
|
|
|
return(EBUSY);
|
2010-09-30 15:44:13 +02:00
|
|
|
|
|
|
|
for (t = (mthread_thread_t) 0; t < no_threads; t++) {
|
|
|
|
tcb = mthread_find_tcb(t);
|
2011-04-13 16:01:13 +02:00
|
|
|
if (tcb->m_state == MS_CONDITION && tcb->m_cond == *cond)
|
|
|
|
return(EBUSY);
|
2010-09-21 14:22:38 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
/* Not in use; invalidate it. */
|
|
|
|
mthread_cond_remove(cond);
|
|
|
|
free(*cond);
|
|
|
|
*cond = NULL;
|
|
|
|
|
|
|
|
return(0);
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/*===========================================================================*
|
|
|
|
* mthread_cond_init *
|
|
|
|
*===========================================================================*/
|
2012-03-25 20:25:53 +02:00
|
|
|
int mthread_cond_init(cond, cattr)
|
2010-09-21 14:22:38 +02:00
|
|
|
mthread_cond_t *cond;
|
|
|
|
mthread_condattr_t *cattr;
|
|
|
|
{
|
|
|
|
/* Initialize condition variable to a known state. cattr is ignored */
|
|
|
|
struct __mthread_cond *c;
|
|
|
|
|
2012-05-01 11:44:49 +02:00
|
|
|
MTHREAD_CHECK_INIT(); /* Make sure libmthread is initialized */
|
2010-09-21 14:22:38 +02:00
|
|
|
|
2011-04-13 16:01:13 +02:00
|
|
|
if (cond == NULL)
|
|
|
|
return(EINVAL);
|
|
|
|
else if (cattr != NULL)
|
|
|
|
return(ENOSYS);
|
|
|
|
|
2011-03-18 11:29:54 +01:00
|
|
|
#ifdef MTHREAD_STRICT
|
2011-04-13 16:01:13 +02:00
|
|
|
else if (mthread_cond_valid(cond))
|
2010-09-21 14:22:38 +02:00
|
|
|
/* Already initialized */
|
2011-04-13 16:01:13 +02:00
|
|
|
return(EBUSY);
|
2011-03-18 11:29:54 +01:00
|
|
|
#endif
|
2011-04-13 16:01:13 +02:00
|
|
|
else if ((c = malloc(sizeof(struct __mthread_cond))) == NULL)
|
|
|
|
return(ENOMEM);
|
2010-09-21 14:22:38 +02:00
|
|
|
|
2011-03-18 11:29:54 +01:00
|
|
|
c->mc_mutex = NULL;
|
2010-09-21 14:22:38 +02:00
|
|
|
*cond = (mthread_cond_t) c;
|
|
|
|
mthread_cond_add(cond);
|
|
|
|
|
|
|
|
return(0);
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/*===========================================================================*
|
|
|
|
* mthread_cond_remove *
|
|
|
|
*===========================================================================*/
|
2011-03-18 11:29:54 +01:00
|
|
|
#ifdef MTHREAD_STRICT
|
2012-03-25 20:25:53 +02:00
|
|
|
static void mthread_cond_remove(c)
|
2010-09-21 14:22:38 +02:00
|
|
|
mthread_cond_t *c;
|
|
|
|
{
|
|
|
|
/* Remove condition from list of valid, initialized conditions */
|
|
|
|
|
2011-03-18 11:29:54 +01:00
|
|
|
if ((*c)->mc_prev == NULL)
|
|
|
|
vc_front = (*c)->mc_next;
|
2010-09-21 14:22:38 +02:00
|
|
|
else
|
2011-03-18 11:29:54 +01:00
|
|
|
(*c)->mc_prev->mc_next = (*c)->mc_next;
|
2010-09-21 14:22:38 +02:00
|
|
|
|
2011-03-18 11:29:54 +01:00
|
|
|
if ((*c)->mc_next == NULL)
|
|
|
|
vc_rear = (*c)->mc_prev;
|
2010-09-21 14:22:38 +02:00
|
|
|
else
|
2011-03-18 11:29:54 +01:00
|
|
|
(*c)->mc_next->mc_prev = (*c)->mc_prev;
|
2010-09-21 14:22:38 +02:00
|
|
|
|
|
|
|
}
|
2011-03-18 11:29:54 +01:00
|
|
|
#endif
|
2010-09-21 14:22:38 +02:00
|
|
|
|
|
|
|
/*===========================================================================*
|
|
|
|
* mthread_cond_signal *
|
|
|
|
*===========================================================================*/
|
2012-03-25 20:25:53 +02:00
|
|
|
int mthread_cond_signal(cond)
|
2010-09-21 14:22:38 +02:00
|
|
|
mthread_cond_t *cond;
|
|
|
|
{
|
|
|
|
/* Signal a thread that condition 'cond' was met. Just a single thread. */
|
2010-09-30 15:44:13 +02:00
|
|
|
mthread_thread_t t;
|
|
|
|
mthread_tcb_t *tcb;
|
2010-09-21 14:22:38 +02:00
|
|
|
|
2012-05-01 11:44:49 +02:00
|
|
|
MTHREAD_CHECK_INIT(); /* Make sure libmthread is initialized */
|
2010-09-21 14:22:38 +02:00
|
|
|
|
2011-04-13 16:01:13 +02:00
|
|
|
if (cond == NULL)
|
|
|
|
return(EINVAL);
|
|
|
|
else if (!mthread_cond_valid(cond))
|
|
|
|
return(EINVAL);
|
2010-09-21 14:22:38 +02:00
|
|
|
|
2010-09-30 15:44:13 +02:00
|
|
|
tcb = mthread_find_tcb(MAIN_THREAD);
|
|
|
|
if (tcb->m_state == MS_CONDITION && tcb->m_cond == *cond)
|
|
|
|
mthread_unsuspend(MAIN_THREAD);
|
|
|
|
|
|
|
|
for (t = (mthread_thread_t) 0; t < no_threads; t++) {
|
|
|
|
tcb = mthread_find_tcb(t);
|
2011-03-18 11:29:54 +01:00
|
|
|
if (tcb->m_state == MS_CONDITION && tcb->m_cond == *cond){
|
2010-09-30 15:44:13 +02:00
|
|
|
mthread_unsuspend(t);
|
2010-09-21 14:22:38 +02:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return(0);
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/*===========================================================================*
|
|
|
|
* mthread_cond_valid *
|
|
|
|
*===========================================================================*/
|
2011-03-18 11:29:54 +01:00
|
|
|
#ifdef MTHREAD_STRICT
|
2012-03-25 20:25:53 +02:00
|
|
|
static int mthread_cond_valid(c)
|
2010-09-21 14:22:38 +02:00
|
|
|
mthread_cond_t *c;
|
|
|
|
{
|
|
|
|
/* Check to see if cond is on the list of valid conditions */
|
|
|
|
struct __mthread_cond *loopitem;
|
|
|
|
|
2012-05-01 11:44:49 +02:00
|
|
|
MTHREAD_CHECK_INIT();
|
2011-03-18 11:29:54 +01:00
|
|
|
|
2010-09-21 14:22:38 +02:00
|
|
|
loopitem = vc_front;
|
|
|
|
|
|
|
|
while (loopitem != NULL) {
|
|
|
|
if (loopitem == *c)
|
|
|
|
return(1);
|
|
|
|
|
2011-03-18 11:29:54 +01:00
|
|
|
loopitem = loopitem->mc_next;
|
2010-09-21 14:22:38 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
return(0);
|
|
|
|
}
|
2011-03-18 11:29:54 +01:00
|
|
|
#endif
|
2010-09-21 14:22:38 +02:00
|
|
|
|
|
|
|
/*===========================================================================*
|
|
|
|
* mthread_cond_verify *
|
|
|
|
*===========================================================================*/
|
|
|
|
#ifdef MDEBUG
|
2012-03-25 20:25:53 +02:00
|
|
|
int mthread_cond_verify(void)
|
2010-09-21 14:22:38 +02:00
|
|
|
{
|
|
|
|
/* Return true in case no condition variables are in use. */
|
|
|
|
|
2012-05-01 11:44:49 +02:00
|
|
|
MTHREAD_CHECK_INIT(); /* Make sure libmthread is initialized */
|
2010-09-21 14:22:38 +02:00
|
|
|
|
|
|
|
return(vc_front == NULL);
|
|
|
|
}
|
|
|
|
#endif
|
|
|
|
|
|
|
|
|
|
|
|
/*===========================================================================*
|
|
|
|
* mthread_cond_wait *
|
|
|
|
*===========================================================================*/
|
2012-03-25 20:25:53 +02:00
|
|
|
int mthread_cond_wait(cond, mutex)
|
2010-09-21 14:22:38 +02:00
|
|
|
mthread_cond_t *cond;
|
|
|
|
mthread_mutex_t *mutex;
|
|
|
|
{
|
|
|
|
/* Wait for a condition to be signaled */
|
2010-09-30 15:44:13 +02:00
|
|
|
mthread_tcb_t *tcb;
|
2010-09-21 14:22:38 +02:00
|
|
|
struct __mthread_cond *c;
|
|
|
|
struct __mthread_mutex *m;
|
|
|
|
|
2012-05-01 11:44:49 +02:00
|
|
|
MTHREAD_CHECK_INIT(); /* Make sure libmthread is initialized */
|
2010-09-21 14:22:38 +02:00
|
|
|
|
2011-04-13 16:01:13 +02:00
|
|
|
if (cond == NULL || mutex == NULL)
|
|
|
|
return(EINVAL);
|
2010-09-21 14:22:38 +02:00
|
|
|
|
|
|
|
c = (struct __mthread_cond *) *cond;
|
|
|
|
m = (struct __mthread_mutex *) *mutex;
|
|
|
|
|
2011-04-13 16:01:13 +02:00
|
|
|
if (!mthread_cond_valid(cond) || !mthread_mutex_valid(mutex))
|
|
|
|
return(EINVAL);
|
2010-09-21 14:22:38 +02:00
|
|
|
|
2011-03-18 11:29:54 +01:00
|
|
|
c->mc_mutex = m; /* Remember we're using this mutex in a cond_wait */
|
2010-09-21 14:22:38 +02:00
|
|
|
if (mthread_mutex_unlock(mutex) != 0) /* Fails when we're not the owner */
|
|
|
|
return(-1);
|
|
|
|
|
2010-09-30 15:44:13 +02:00
|
|
|
tcb = mthread_find_tcb(current_thread);
|
|
|
|
tcb->m_cond = c; /* Register condition variable. */
|
|
|
|
mthread_suspend(MS_CONDITION);
|
2010-09-21 14:22:38 +02:00
|
|
|
|
|
|
|
/* When execution returns here, the condition was met. Lock mutex again. */
|
2011-03-18 11:29:54 +01:00
|
|
|
c->mc_mutex = NULL; /* Forget about this mutex */
|
2010-09-30 15:44:13 +02:00
|
|
|
tcb->m_cond = NULL; /* ... and condition var */
|
2010-09-21 14:22:38 +02:00
|
|
|
if (mthread_mutex_lock(mutex) != 0)
|
|
|
|
return(-1);
|
|
|
|
|
|
|
|
return(0);
|
|
|
|
}
|
|
|
|
|
|
|
|
|