mthread_init(); /* Make sure mthreads is initialized */
- if (proc == NULL) {
- errno = EINVAL;
- return(-1);
- }
+ if (proc == NULL)
+ return(EINVAL);
if (!mthread_queue_isempty(&free_threads)) {
thread = mthread_queue_remove(&free_threads);
#endif
return(0);
} else {
- if (mthread_increase_thread_pool() == -1) {
- errno = EAGAIN;
- return(-1);
- }
+ if (mthread_increase_thread_pool() == -1)
+ return(EAGAIN);
return mthread_create(threadid, tattr, proc, arg);
}
mthread_tcb_t *tcb;
mthread_init(); /* Make sure libmthread is initialized */
- if (!isokthreadid(detach)) {
- errno = ESRCH;
- return(-1);
- }
+ if (!isokthreadid(detach))
+ return(ESRCH);
tcb = mthread_find_tcb(detach);
if (tcb->m_state == MS_DEAD) {
- errno = ESRCH;
- return(-1);
+ return(ESRCH);
} else if (tcb->m_attr.ma_detachstate != MTHREAD_CREATE_DETACHED) {
if (tcb->m_state == MS_EXITING)
mthread_thread_stop(detach);
mthread_init(); /* Make sure libmthread is initialized */
- if (!isokthreadid(join)) {
- errno = ESRCH;
- return(-1);
- } else if (join == current_thread) {
- errno = EDEADLK;
- return(-1);
- }
+ if (!isokthreadid(join))
+ return(ESRCH);
+ else if (join == current_thread)
+ return(EDEADLK);
tcb = mthread_find_tcb(join);
- if (tcb->m_state == MS_DEAD) {
- errno = ESRCH;
- return(-1);
- } else if (tcb->m_attr.ma_detachstate == MTHREAD_CREATE_DETACHED) {
- errno = EINVAL;
- return(-1);
- }
+ if (tcb->m_state == MS_DEAD)
+ return(ESRCH);
+ else if (tcb->m_attr.ma_detachstate == MTHREAD_CREATE_DETACHED)
+ return(EINVAL);
/* When the thread hasn't exited yet, we have to wait for that to happen */
if (tcb->m_state != MS_EXITING) {
mthread_init(); /* Make sure libmthread is initialized */
- if (once == NULL || proc == NULL) {
- errno = EINVAL;
- return(-1);
- }
+ if (once == NULL || proc == NULL)
+ return(EINVAL);
if (*once != 1) proc();
*once = 1;
mthread_init(); /* Make sure mthreads is initialized */
- if (attr == NULL) {
- errno = EINVAL;
- return(-1);
- }
+ if (attr == NULL)
+ return(EINVAL);
- if (!mthread_attr_valid(attr)) {
- errno = EINVAL;
- return(-1);
- }
+ if (!mthread_attr_valid(attr))
+ return(EINVAL);
/* Valide attribute; invalidate it */
mthread_attr_remove(attr);
mthread_init(); /* Make sure mthreads is initialized */
- if (attr == NULL) {
- errno = EAGAIN;
- return(-1);
- } else if (mthread_attr_valid(attr)) {
- errno = EBUSY;
- return(-1);
- }
+ if (attr == NULL)
+ return(EAGAIN);
+ else if (mthread_attr_valid(attr))
+ return(EBUSY);
if ((a = malloc(sizeof(struct __mthread_attr))) == NULL)
return(-1);
mthread_init(); /* Make sure mthreads is initialized */
- if (attr == NULL) {
- errno = EINVAL;
- return(-1);
- }
+ if (attr == NULL)
+ return(EINVAL);
a = (struct __mthread_attr *) *attr;
- if (!mthread_attr_valid(attr)) {
- errno = EINVAL;
- return(-1);
- }
+ if (!mthread_attr_valid(attr))
+ return(EINVAL);
*detachstate = a->ma_detachstate;
mthread_init(); /* Make sure mthreads is initialized */
- if (attr == NULL) {
- errno = EINVAL;
- return(-1);
- }
+ if (attr == NULL)
+ return(EINVAL);
a = (struct __mthread_attr *) *attr;
- if (!mthread_attr_valid(attr)) {
- errno = EINVAL;
- return(-1);
- } else if(detachstate != MTHREAD_CREATE_JOINABLE &&
- detachstate != MTHREAD_CREATE_DETACHED) {
- errno = EINVAL;
- return(-1);
- }
+ if (!mthread_attr_valid(attr))
+ return(EINVAL);
+ else if(detachstate != MTHREAD_CREATE_JOINABLE &&
+ detachstate != MTHREAD_CREATE_DETACHED)
+ return(EINVAL);
a->ma_detachstate = detachstate;
mthread_init(); /* Make sure mthreads is initialized */
- if (attr == NULL) {
- errno = EINVAL;
- return(-1);
- }
+ if (attr == NULL)
+ return(EINVAL);
a = (struct __mthread_attr *) *attr;
- if (!mthread_attr_valid(attr)) {
- errno = EINVAL;
- return(-1);
- }
+ if (!mthread_attr_valid(attr))
+ return(EINVAL);
*stackaddr = a->ma_stackaddr;
*stacksize = a->ma_stacksize;
mthread_init(); /* Make sure mthreads is initialized */
- if (attr == NULL) {
- errno = EINVAL;
- return(-1);
- }
+ if (attr == NULL)
+ return(EINVAL);
a = (struct __mthread_attr *) *attr;
- if (!mthread_attr_valid(attr)) {
- errno = EINVAL;
- return(-1);
- }
+ if (!mthread_attr_valid(attr))
+ return(EINVAL);
*stacksize = a->ma_stacksize;
mthread_init(); /* Make sure mthreads is initialized */
- if (attr == NULL) {
- errno = EINVAL;
- return(-1);
- }
+ if (attr == NULL)
+ return(EINVAL);
a = (struct __mthread_attr *) *attr;
- if (!mthread_attr_valid(attr) || stacksize < MTHREAD_STACK_MIN) {
- errno = EINVAL;
- return(-1);
- }
+ if (!mthread_attr_valid(attr) || stacksize < MTHREAD_STACK_MIN)
+ return(EINVAL);
+
/* We don't care about address alignment (POSIX standard). The ucontext
* system calls will make sure that the provided stack will be aligned (at
* the cost of some memory if needed).
mthread_init(); /* Make sure mthreads is initialized */
- if (attr == NULL) {
- errno = EINVAL;
- return(-1);
- }
+ if (attr == NULL)
+ return(EINVAL);
a = (struct __mthread_attr *) *attr;
- if (!mthread_attr_valid(attr) || stacksize < MTHREAD_STACK_MIN) {
- errno = EINVAL;
- return(-1);
- }
+ if (!mthread_attr_valid(attr) || stacksize < MTHREAD_STACK_MIN)
+ return(EINVAL);
a->ma_stacksize = stacksize;
mthread_init(); /* Make sure libmthread is initialized */
- if (cond == NULL) {
- errno = EINVAL;
- return(-1);
- } else if (!mthread_cond_valid(cond)) {
- errno = EINVAL;
- return(-1);
- }
+ if (cond == NULL)
+ return(EINVAL);
+ else if (!mthread_cond_valid(cond))
+ return(EINVAL);
tcb = mthread_find_tcb(MAIN_THREAD);
if (tcb->m_state == MS_CONDITION && tcb->m_cond == *cond)
mthread_init(); /* Make sure libmthread is initialized */
- if (cond == NULL) {
- errno = EINVAL;
- return(-1);
- } else if (!mthread_cond_valid(cond)) {
- errno = EINVAL;
- return(-1);
- }
+ if (cond == NULL)
+ return(EINVAL);
+ else if (!mthread_cond_valid(cond))
+ return(EINVAL);
/* Is another thread currently using this condition variable? */
tcb = mthread_find_tcb(MAIN_THREAD);
- if (tcb->m_state == MS_CONDITION && tcb->m_cond == *cond) {
- errno = EBUSY;
- return(-1);
- }
+ if (tcb->m_state == MS_CONDITION && tcb->m_cond == *cond)
+ return(EBUSY);
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){
- errno = EBUSY;
- return(-1);
- }
+ if (tcb->m_state == MS_CONDITION && tcb->m_cond == *cond)
+ return(EBUSY);
}
/* Not in use; invalidate it. */
mthread_init(); /* Make sure libmthread is initialized */
- if (cond == NULL) {
- errno = EINVAL;
- return(-1);
- } else if (cattr != NULL) {
- errno = ENOSYS;
- return(-1);
- }
+ if (cond == NULL)
+ return(EINVAL);
+ else if (cattr != NULL)
+ return(ENOSYS);
+
#ifdef MTHREAD_STRICT
- else if (mthread_cond_valid(cond)) {
+ else if (mthread_cond_valid(cond))
/* Already initialized */
- errno = EBUSY;
- return(-1);
- }
+ return(EBUSY);
#endif
- else if ((c = malloc(sizeof(struct __mthread_cond))) == NULL) {
- errno = ENOMEM;
- return(-1);
- }
+ else if ((c = malloc(sizeof(struct __mthread_cond))) == NULL)
+ return(ENOMEM);
c->mc_mutex = NULL;
*cond = (mthread_cond_t) c;
mthread_init(); /* Make sure libmthread is initialized */
- if (cond == NULL) {
- errno = EINVAL;
- return(-1);
- } else if (!mthread_cond_valid(cond)) {
- errno = EINVAL;
- return(-1);
- }
+ if (cond == NULL)
+ return(EINVAL);
+ else if (!mthread_cond_valid(cond))
+ return(EINVAL);
tcb = mthread_find_tcb(MAIN_THREAD);
if (tcb->m_state == MS_CONDITION && tcb->m_cond == *cond)
mthread_init(); /* Make sure libmthread is initialized */
- if (cond == NULL || mutex == NULL) {
- errno = EINVAL;
- return(-1);
- }
+ if (cond == NULL || mutex == NULL)
+ return(EINVAL);
c = (struct __mthread_cond *) *cond;
m = (struct __mthread_mutex *) *mutex;
- if (!mthread_cond_valid(cond) || !mthread_mutex_valid(mutex)) {
- errno = EINVAL;
- return(-1);
- }
+ if (!mthread_cond_valid(cond) || !mthread_mutex_valid(mutex))
+ return(EINVAL);
c->mc_mutex = m; /* Remember we're using this mutex in a cond_wait */
if (mthread_mutex_unlock(mutex) != 0) /* Fails when we're not the owner */
mthread_init(); /* Make sure mthreads is initialized */
- if (mutex == NULL) {
- errno = EINVAL;
- return(-1);
- }
+ if (mutex == NULL)
+ return(EINVAL);
- if (!mthread_mutex_valid(mutex)) {
- errno = EINVAL;
- return(-1);
- } else if ((*mutex)->mm_owner != NO_THREAD) {
- errno = EBUSY;
- return(-1);
- }
+ if (!mthread_mutex_valid(mutex))
+ return(EINVAL);
+ else if ((*mutex)->mm_owner != NO_THREAD)
+ return(EBUSY);
/* Check if this mutex is not associated with a condition */
for (t = (mthread_thread_t) 0; t < no_threads; t++) {
tcb = mthread_find_tcb(t);
if (tcb->m_state == MS_CONDITION) {
- if (tcb->m_cond != NULL && tcb->m_cond->mc_mutex == *mutex) {
- errno = EBUSY;
- return(-1);
- }
+ if (tcb->m_cond != NULL && tcb->m_cond->mc_mutex == *mutex)
+ return(EBUSY);
}
}
mthread_init(); /* Make sure mthreads is initialized */
- if (mutex == NULL) {
- errno = EAGAIN;
- return(-1);
- } else if (mattr != NULL) {
- errno = ENOSYS;
- return(-1);
- }
+ if (mutex == NULL)
+ return(EAGAIN);
+ else if (mattr != NULL)
+ return(ENOSYS);
#ifdef MTHREAD_STRICT
- else if (mthread_mutex_valid(mutex)) {
- errno = EBUSY;
- return(-1);
- }
+ else if (mthread_mutex_valid(mutex))
+ return(EBUSY);
#endif
- else if ((m = malloc(sizeof(struct __mthread_mutex))) == NULL) {
- errno = ENOMEM;
- return(-1);
- }
+ else if ((m = malloc(sizeof(struct __mthread_mutex))) == NULL)
+ return(ENOMEM);
mthread_queue_init(&m->mm_queue);
m->mm_owner = NO_THREAD;
mthread_init(); /* Make sure mthreads is initialized */
- if (mutex == NULL) {
- errno = EINVAL;
- return(-1);
- }
+ if (mutex == NULL)
+ return(EINVAL);
m = (struct __mthread_mutex *) *mutex;
- if (!mthread_mutex_valid(&m)) {
- errno = EINVAL;
- return(-1);
- } else if (m->mm_owner == NO_THREAD) { /* Not locked */
+ if (!mthread_mutex_valid(&m))
+ return(EINVAL);
+ else if (m->mm_owner == NO_THREAD) { /* Not locked */
m->mm_owner = current_thread;
if (current_thread == MAIN_THREAD)
mthread_debug("MAIN_THREAD now mutex owner\n");
} else if (m->mm_owner == current_thread) {
- errno = EDEADLK;
- return(-1);
+ return(EDEADLK);
} else {
mthread_queue_add(&m->mm_queue, current_thread);
if (m->mm_owner == MAIN_THREAD)
mthread_init(); /* Make sure mthreads is initialized */
- if (mutex == NULL) {
- errno = EINVAL;
- return(-1);
- }
+ if (mutex == NULL)
+ return(EINVAL);
m = (struct __mthread_mutex *) *mutex;
- if (!mthread_mutex_valid(&m)) {
- errno = EINVAL;
- return(-1);
- } else if (m->mm_owner == NO_THREAD) {
+ if (!mthread_mutex_valid(&m))
+ return(EINVAL);
+ else if (m->mm_owner == NO_THREAD) {
m->mm_owner = current_thread;
return(0);
}
- errno = EBUSY;
- return(-1);
+ return(EBUSY);
}
mthread_init(); /* Make sure mthreads is initialized */
- if (mutex == NULL) {
- errno = EINVAL;
- return(-1);
- }
+ if (mutex == NULL)
+ return(EINVAL);
m = (struct __mthread_mutex *) *mutex;
- if (!mthread_mutex_valid(&m)) {
- errno = EINVAL;
- return(-1);
- } else if (m->mm_owner != current_thread) {
- errno = EPERM;
- return(-1); /* Can't unlock a mutex locked by another thread. */
- }
+ 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. */
m->mm_owner = mthread_queue_remove(&m->mm_queue);
if (m->mm_owner != NO_THREAD) mthread_unsuspend(m->mm_owner);
if (mthread_mutex_lock(&mu[0]) != 0) err(3, 2);
/* Trying to acquire lock again should fail with EDEADLK */
- if (mthread_mutex_lock(&mu[0]) != -1) err(3, 2);
- if (errno != EDEADLK) err(3, 3);
+ if (mthread_mutex_lock(&mu[0]) != EDEADLK) err(3, 2);
#ifdef MTHREAD_STRICT
/* Try to acquire lock on uninitialized mutex; should fail with EINVAL */
/* Note: this check only works when libmthread is compiled with
* MTHREAD_STRICT turned on. In POSIX this situation is a MAY fail if... */
- if (mthread_mutex_lock(&mu2) != -1) {
+ if (mthread_mutex_lock(&mu2) != EINVAL) {
err(3, 4);
mthread_mutex_unlock(&mu2);
}
- if (errno != EINVAL) err(3, 5);
- errno = 0;
- if (mthread_mutex_trylock(&mu2) != -1) {
+ if (mthread_mutex_trylock(&mu2) != EINVAL) {
err(3, 6);
mthread_mutex_unlock(&mu2);
}
- if (errno != EINVAL) err(3, 7);
#endif
if (mthread_mutex_trylock(&mu[1]) != 0) err(3, 8);
mutex_a_step = 1;
mthread_yield();
VERIFY_MUTEX(1, 0, 0, 3, 9);
- errno = 0;
- if (mthread_mutex_trylock(&mu[2]) != -1) err(3, 10);
- if (errno != EBUSY) err(3, 11);
+ if (mthread_mutex_trylock(&mu[2]) != EBUSY) err(3, 10);
if (mthread_mutex_lock(&mu[2]) != 0) err(3, 12); /* Transfer control to main
* loop.
*/
*/
VERIFY_MUTEX(1, 0, 0, 4, 1);
- if (mthread_mutex_unlock(&mu[0]) != -1) err(4, 2);
- if (errno != EPERM) err(4, 3);
+ if (mthread_mutex_unlock(&mu[0]) != EPERM) err(4, 2);
/* Probing mu[0] to lock it should tell us it's locked */
- if (mthread_mutex_trylock(&mu[0]) == 0) err(4, 4);
- if (errno != EBUSY) err(4, 5);
+ if (mthread_mutex_trylock(&mu[0]) != EBUSY) err(4, 4);
if (mthread_mutex_lock(&mu[0]) != 0) err(4, 5);
mutex_b_step = 1;
stacksize = 0;
if (mthread_attr_init(&tattr) != 0) err(11, 17);
if ((newstackaddr = malloc(newstacksize)) == NULL) err(11, 18);
- if (mthread_attr_setstack(&tattr, newstackaddr, newstacksize) == 0)
+ if (mthread_attr_setstack(&tattr, newstackaddr, newstacksize) != EINVAL)
err(11, 19);
- if (errno != EINVAL) err(11, 20);
if (mthread_attr_getstack(&tattr, &stackaddr, &stacksize) != 0) err(11, 21);
if (stackaddr == newstackaddr) err(11, 22);
if (stacksize == newstacksize) err(11, 23);