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_mutex *vm_front, *vm_rear;
|
|
|
|
static void mthread_mutex_add(mthread_mutex_t *m);
|
|
|
|
static void mthread_mutex_remove(mthread_mutex_t *m);
|
2011-03-18 11:29:54 +01:00
|
|
|
#else
|
|
|
|
# define mthread_mutex_add(m) ((*m)->mm_magic = MTHREAD_INIT_MAGIC)
|
|
|
|
# define mthread_mutex_remove(m) ((*m)->mm_magic = MTHREAD_NOT_INUSE)
|
|
|
|
#endif
|
2010-09-21 14:22:38 +02:00
|
|
|
|
|
|
|
/*===========================================================================*
|
|
|
|
* mthread_init_valid_mutexes *
|
|
|
|
*===========================================================================*/
|
2012-03-25 20:25:53 +02:00
|
|
|
void mthread_init_valid_mutexes(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 list of valid mutexes */
|
|
|
|
vm_front = vm_rear = NULL;
|
2011-07-27 11:30:26 +02:00
|
|
|
#endif
|
2010-09-21 14:22:38 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/*===========================================================================*
|
|
|
|
* mthread_mutex_add *
|
|
|
|
*===========================================================================*/
|
2011-03-18 11:29:54 +01:00
|
|
|
#ifdef MTHREAD_STRICT
|
2012-03-25 20:25:53 +02:00
|
|
|
static void mthread_mutex_add(m)
|
2010-09-21 14:22:38 +02:00
|
|
|
mthread_mutex_t *m;
|
|
|
|
{
|
|
|
|
/* Add mutex to list of valid, initialized mutexes */
|
|
|
|
|
|
|
|
if (vm_front == NULL) { /* Empty list */
|
|
|
|
vm_front = *m;
|
2011-03-18 11:29:54 +01:00
|
|
|
(*m)->mm_prev = NULL;
|
2010-09-21 14:22:38 +02:00
|
|
|
} else {
|
2011-03-18 11:29:54 +01:00
|
|
|
vm_rear->mm_next = *m;
|
|
|
|
(*m)->mm_prev = vm_rear;
|
2010-09-21 14:22:38 +02:00
|
|
|
}
|
|
|
|
|
2011-03-18 11:29:54 +01:00
|
|
|
(*m)->mm_next = NULL;
|
2010-09-21 14:22:38 +02:00
|
|
|
vm_rear = *m;
|
|
|
|
}
|
2011-03-18 11:29:54 +01:00
|
|
|
#endif
|
2010-09-21 14:22:38 +02:00
|
|
|
|
|
|
|
/*===========================================================================*
|
|
|
|
* mthread_mutex_destroy *
|
|
|
|
*===========================================================================*/
|
2012-03-25 20:25:53 +02:00
|
|
|
int mthread_mutex_destroy(mutex)
|
2010-09-21 14:22:38 +02:00
|
|
|
mthread_mutex_t *mutex;
|
|
|
|
{
|
|
|
|
/* Invalidate mutex and deallocate resources. */
|
|
|
|
|
2010-09-30 15:44:13 +02:00
|
|
|
mthread_thread_t t;
|
|
|
|
mthread_tcb_t *tcb;
|
2010-09-21 14:22:38 +02:00
|
|
|
|
|
|
|
mthread_init(); /* Make sure mthreads is initialized */
|
|
|
|
|
2011-04-13 16:01:13 +02:00
|
|
|
if (mutex == NULL)
|
|
|
|
return(EINVAL);
|
2010-09-21 14:22:38 +02:00
|
|
|
|
2011-04-13 16:01:13 +02:00
|
|
|
if (!mthread_mutex_valid(mutex))
|
|
|
|
return(EINVAL);
|
|
|
|
else if ((*mutex)->mm_owner != NO_THREAD)
|
|
|
|
return(EBUSY);
|
2010-09-21 14:22:38 +02:00
|
|
|
|
|
|
|
/* Check if this mutex is not associated with a condition */
|
2010-09-30 15:44:13 +02:00
|
|
|
for (t = (mthread_thread_t) 0; t < no_threads; t++) {
|
|
|
|
tcb = mthread_find_tcb(t);
|
|
|
|
if (tcb->m_state == MS_CONDITION) {
|
2011-04-13 16:01:13 +02:00
|
|
|
if (tcb->m_cond != NULL && tcb->m_cond->mc_mutex == *mutex)
|
|
|
|
return(EBUSY);
|
2010-09-21 14:22:38 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Not in use; invalidate it */
|
|
|
|
mthread_mutex_remove(mutex);
|
|
|
|
free(*mutex);
|
|
|
|
*mutex = NULL;
|
|
|
|
|
|
|
|
return(0);
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/*===========================================================================*
|
|
|
|
* mthread_mutex_init *
|
|
|
|
*===========================================================================*/
|
2012-03-25 20:25:53 +02:00
|
|
|
int mthread_mutex_init(mutex, mattr)
|
2010-09-21 14:22:38 +02:00
|
|
|
mthread_mutex_t *mutex; /* Mutex that is to be initialized */
|
|
|
|
mthread_mutexattr_t *mattr; /* Mutex attribute */
|
|
|
|
{
|
|
|
|
/* Initialize the mutex to a known state. Attributes are not supported */
|
|
|
|
|
|
|
|
struct __mthread_mutex *m;
|
|
|
|
|
|
|
|
mthread_init(); /* Make sure mthreads is initialized */
|
|
|
|
|
2011-04-13 16:01:13 +02:00
|
|
|
if (mutex == NULL)
|
|
|
|
return(EAGAIN);
|
|
|
|
else if (mattr != 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_mutex_valid(mutex))
|
|
|
|
return(EBUSY);
|
2011-03-18 11:29:54 +01:00
|
|
|
#endif
|
2011-04-13 16:01:13 +02:00
|
|
|
else if ((m = malloc(sizeof(struct __mthread_mutex))) == NULL)
|
|
|
|
return(ENOMEM);
|
2010-09-21 14:22:38 +02:00
|
|
|
|
2011-03-18 11:29:54 +01:00
|
|
|
mthread_queue_init(&m->mm_queue);
|
|
|
|
m->mm_owner = NO_THREAD;
|
2010-09-21 14:22:38 +02:00
|
|
|
*mutex = (mthread_mutex_t) m;
|
|
|
|
mthread_mutex_add(mutex); /* Validate mutex; mutex now in use */
|
|
|
|
|
|
|
|
return(0);
|
|
|
|
}
|
|
|
|
|
|
|
|
/*===========================================================================*
|
|
|
|
* mthread_mutex_lock *
|
|
|
|
*===========================================================================*/
|
2012-03-25 20:25:53 +02:00
|
|
|
int mthread_mutex_lock(mutex)
|
2010-09-21 14:22:38 +02:00
|
|
|
mthread_mutex_t *mutex; /* Mutex that is to be locked */
|
|
|
|
{
|
|
|
|
/* Try to lock this mutex. If already locked, append the current thread to
|
|
|
|
* FIFO queue associated with this mutex and suspend the thread. */
|
|
|
|
|
|
|
|
struct __mthread_mutex *m;
|
|
|
|
|
|
|
|
mthread_init(); /* Make sure mthreads is initialized */
|
|
|
|
|
2011-04-13 16:01:13 +02:00
|
|
|
if (mutex == NULL)
|
|
|
|
return(EINVAL);
|
2010-09-21 14:22:38 +02:00
|
|
|
|
|
|
|
m = (struct __mthread_mutex *) *mutex;
|
2011-04-13 16:01:13 +02:00
|
|
|
if (!mthread_mutex_valid(&m))
|
|
|
|
return(EINVAL);
|
|
|
|
else if (m->mm_owner == NO_THREAD) { /* Not locked */
|
2011-03-18 11:29:54 +01:00
|
|
|
m->mm_owner = current_thread;
|
|
|
|
} else if (m->mm_owner == current_thread) {
|
2011-04-13 16:01:13 +02:00
|
|
|
return(EDEADLK);
|
2010-09-21 14:22:38 +02:00
|
|
|
} else {
|
2011-03-18 11:29:54 +01:00
|
|
|
mthread_queue_add(&m->mm_queue, current_thread);
|
2010-09-30 15:44:13 +02:00
|
|
|
mthread_suspend(MS_MUTEX);
|
2010-09-21 14:22:38 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
/* When we get here we acquired the lock. */
|
|
|
|
return(0);
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/*===========================================================================*
|
|
|
|
* mthread_mutex_remove *
|
|
|
|
*===========================================================================*/
|
2011-03-18 11:29:54 +01:00
|
|
|
#ifdef MTHREAD_STRICT
|
2012-03-25 20:25:53 +02:00
|
|
|
static void mthread_mutex_remove(m)
|
2010-09-21 14:22:38 +02:00
|
|
|
mthread_mutex_t *m;
|
|
|
|
{
|
|
|
|
/* Remove mutex from list of valid, initialized mutexes */
|
|
|
|
|
2011-03-18 11:29:54 +01:00
|
|
|
if ((*m)->mm_prev == NULL)
|
|
|
|
vm_front = (*m)->mm_next;
|
2010-09-21 14:22:38 +02:00
|
|
|
else
|
2011-03-18 11:29:54 +01:00
|
|
|
(*m)->mm_prev->mm_next = (*m)->mm_next;
|
2010-09-21 14:22:38 +02:00
|
|
|
|
2011-03-18 11:29:54 +01:00
|
|
|
if ((*m)->mm_next == NULL)
|
|
|
|
vm_rear = (*m)->mm_prev;
|
2010-09-21 14:22:38 +02:00
|
|
|
else
|
2011-03-18 11:29:54 +01:00
|
|
|
(*m)->mm_next->mm_prev = (*m)->mm_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_mutex_trylock *
|
|
|
|
*===========================================================================*/
|
2012-03-25 20:25:53 +02:00
|
|
|
int mthread_mutex_trylock(mutex)
|
2010-09-21 14:22:38 +02:00
|
|
|
mthread_mutex_t *mutex; /* Mutex that is to be locked */
|
|
|
|
{
|
|
|
|
/* Try to lock this mutex and return OK. If already locked, return error. */
|
|
|
|
|
|
|
|
struct __mthread_mutex *m;
|
|
|
|
|
|
|
|
mthread_init(); /* Make sure mthreads is initialized */
|
|
|
|
|
2011-04-13 16:01:13 +02:00
|
|
|
if (mutex == NULL)
|
|
|
|
return(EINVAL);
|
2010-09-21 14:22:38 +02:00
|
|
|
|
|
|
|
m = (struct __mthread_mutex *) *mutex;
|
2011-04-13 16:01:13 +02:00
|
|
|
if (!mthread_mutex_valid(&m))
|
|
|
|
return(EINVAL);
|
2011-07-08 15:59:07 +02:00
|
|
|
else if (m->mm_owner == current_thread)
|
|
|
|
return(EDEADLK);
|
2011-04-13 16:01:13 +02:00
|
|
|
else if (m->mm_owner == NO_THREAD) {
|
2011-03-18 11:29:54 +01:00
|
|
|
m->mm_owner = current_thread;
|
2010-09-21 14:22:38 +02:00
|
|
|
return(0);
|
|
|
|
}
|
|
|
|
|
2011-04-13 16:01:13 +02:00
|
|
|
return(EBUSY);
|
2010-09-21 14:22:38 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/*===========================================================================*
|
|
|
|
* mthread_mutex_unlock *
|
|
|
|
*===========================================================================*/
|
2012-03-25 20:25:53 +02:00
|
|
|
int mthread_mutex_unlock(mutex)
|
2010-09-21 14:22:38 +02:00
|
|
|
mthread_mutex_t *mutex; /* Mutex that is to be unlocked */
|
|
|
|
{
|
|
|
|
/* Unlock a previously locked mutex. If there is a pending lock for this mutex
|
|
|
|
* by another thread, mark that thread runnable. */
|
|
|
|
|
|
|
|
struct __mthread_mutex *m;
|
|
|
|
|
|
|
|
mthread_init(); /* Make sure mthreads is initialized */
|
|
|
|
|
2011-04-13 16:01:13 +02:00
|
|
|
if (mutex == NULL)
|
|
|
|
return(EINVAL);
|
2010-09-21 14:22:38 +02:00
|
|
|
|
|
|
|
m = (struct __mthread_mutex *) *mutex;
|
2011-04-13 16:01:13 +02:00
|
|
|
if (!mthread_mutex_valid(&m))
|
|
|
|
return(EINVAL);
|
|
|
|
else if (m->mm_owner != current_thread)
|
|
|
|
return(EPERM); /* Can't unlock a mutex locked by another thread. */
|
2010-09-21 14:22:38 +02:00
|
|
|
|
2011-03-18 11:29:54 +01:00
|
|
|
m->mm_owner = mthread_queue_remove(&m->mm_queue);
|
|
|
|
if (m->mm_owner != NO_THREAD) mthread_unsuspend(m->mm_owner);
|
2010-09-21 14:22:38 +02:00
|
|
|
return(0);
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/*===========================================================================*
|
|
|
|
* mthread_mutex_valid *
|
|
|
|
*===========================================================================*/
|
2011-03-18 11:29:54 +01:00
|
|
|
#ifdef MTHREAD_STRICT
|
2012-03-25 20:25:53 +02:00
|
|
|
int mthread_mutex_valid(m)
|
2010-09-21 14:22:38 +02:00
|
|
|
mthread_mutex_t *m;
|
|
|
|
{
|
|
|
|
/* Check to see if mutex is on the list of valid mutexes */
|
|
|
|
struct __mthread_mutex *loopitem;
|
|
|
|
|
|
|
|
mthread_init(); /* Make sure mthreads is initialized */
|
|
|
|
|
|
|
|
loopitem = vm_front;
|
|
|
|
|
|
|
|
while (loopitem != NULL) {
|
2011-07-27 11:30:26 +02:00
|
|
|
if (loopitem == *m)
|
|
|
|
return(1);
|
2010-09-21 14:22:38 +02:00
|
|
|
|
2011-07-27 11:30:26 +02:00
|
|
|
loopitem = loopitem->mm_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_mutex_verify *
|
|
|
|
*===========================================================================*/
|
|
|
|
#ifdef MDEBUG
|
2012-03-25 20:25:53 +02:00
|
|
|
int mthread_mutex_verify(void)
|
2010-09-21 14:22:38 +02:00
|
|
|
{
|
|
|
|
/* Return true when no mutexes are in use */
|
|
|
|
int r = 1;
|
|
|
|
struct __mthread_mutex *loopitem;
|
|
|
|
|
|
|
|
mthread_init(); /* Make sure mthreads is initialized */
|
|
|
|
|
2011-07-27 11:30:26 +02:00
|
|
|
#ifdef MTHREAD_STRICT
|
2010-09-21 14:22:38 +02:00
|
|
|
loopitem = vm_front;
|
|
|
|
|
|
|
|
while (loopitem != NULL) {
|
2011-03-18 11:29:54 +01:00
|
|
|
printf("mutex corruption: owner: %d\n", loopitem->mm_owner);
|
2011-07-27 11:30:26 +02:00
|
|
|
loopitem = loopitem->mm_next;
|
2010-09-21 14:22:38 +02:00
|
|
|
r = 0;
|
|
|
|
}
|
2011-07-27 11:30:26 +02:00
|
|
|
#endif
|
2010-09-21 14:22:38 +02:00
|
|
|
|
|
|
|
return(r);
|
|
|
|
}
|
|
|
|
#endif
|
|
|
|
|
|
|
|
|