From: Thomas Veerman Date: Wed, 13 Apr 2011 14:01:13 +0000 (+0000) Subject: Return errors directly instead of using errno X-Git-Tag: v3.2.0~578 X-Git-Url: http://zhaoyanbai.com/repos/%22../static/icons/mdoc.3.ps?a=commitdiff_plain;h=b1a60476c0c2d51b2fa06f804d7e1403abcfc702;p=minix.git Return errors directly instead of using errno --- diff --git a/lib/libmthread/allocate.c b/lib/libmthread/allocate.c index 853a86e99..e8ecede7b 100644 --- a/lib/libmthread/allocate.c +++ b/lib/libmthread/allocate.c @@ -53,10 +53,8 @@ void *arg; 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); @@ -69,10 +67,8 @@ void *arg; #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); } @@ -91,15 +87,12 @@ mthread_thread_t detach; 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); @@ -319,22 +312,16 @@ void **value; 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) { @@ -381,10 +368,8 @@ void (*proc)(void); 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; diff --git a/lib/libmthread/attribute.c b/lib/libmthread/attribute.c index 123260617..4c58db641 100644 --- a/lib/libmthread/attribute.c +++ b/lib/libmthread/attribute.c @@ -48,15 +48,11 @@ mthread_attr_t *attr; 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); @@ -78,13 +74,10 @@ mthread_attr_t *attr; /* Attribute */ 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); @@ -111,16 +104,12 @@ int *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); *detachstate = a->ma_detachstate; @@ -140,20 +129,15 @@ int 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; @@ -174,16 +158,12 @@ size_t *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); *stackaddr = a->ma_stackaddr; *stacksize = a->ma_stacksize; @@ -204,16 +184,12 @@ size_t *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; @@ -234,16 +210,13 @@ size_t 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). @@ -268,16 +241,12 @@ size_t 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); a->ma_stacksize = stacksize; diff --git a/lib/libmthread/condition.c b/lib/libmthread/condition.c index a542a56d3..3a2b48d04 100644 --- a/lib/libmthread/condition.c +++ b/lib/libmthread/condition.c @@ -58,13 +58,10 @@ mthread_cond_t *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); tcb = mthread_find_tcb(MAIN_THREAD); if (tcb->m_state == MS_CONDITION && tcb->m_cond == *cond) @@ -92,27 +89,20 @@ mthread_cond_t *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. */ @@ -136,24 +126,18 @@ mthread_condattr_t *cattr; 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; @@ -197,13 +181,10 @@ mthread_cond_t *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); tcb = mthread_find_tcb(MAIN_THREAD); if (tcb->m_state == MS_CONDITION && tcb->m_cond == *cond) @@ -275,18 +256,14 @@ mthread_mutex_t *mutex; 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 */ diff --git a/lib/libmthread/mutex.c b/lib/libmthread/mutex.c index 0d427ed5d..5aacbcf03 100644 --- a/lib/libmthread/mutex.c +++ b/lib/libmthread/mutex.c @@ -56,27 +56,20 @@ mthread_mutex_t *mutex; 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); } } @@ -102,23 +95,16 @@ mthread_mutexattr_t *mattr; /* Mutex attribute */ 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; @@ -141,22 +127,18 @@ mthread_mutex_t *mutex; /* Mutex that is to be locked */ 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) @@ -202,22 +184,18 @@ mthread_mutex_t *mutex; /* Mutex that is to be locked */ 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); } @@ -234,19 +212,14 @@ mthread_mutex_t *mutex; /* Mutex that is to be unlocked */ 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); diff --git a/test/test59.c b/test/test59.c index d3583099c..745c6b186 100644 --- a/test/test59.c +++ b/test/test59.c @@ -220,34 +220,28 @@ PRIVATE void mutex_a(void *arg) 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. */ @@ -282,12 +276,10 @@ PRIVATE void mutex_b(void *arg) */ 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; @@ -612,9 +604,8 @@ PRIVATE void test_attributes(void) 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);