From owner-svn-src-all@FreeBSD.ORG Tue Oct 26 09:25:29 2010 Return-Path: Delivered-To: svn-src-all@freebsd.org Received: from mx1.freebsd.org (mx1.freebsd.org [IPv6:2001:4f8:fff6::34]) by hub.freebsd.org (Postfix) with ESMTP id 8460C1065693; Tue, 26 Oct 2010 09:25:29 +0000 (UTC) (envelope-from davidxu@FreeBSD.org) Received: from svn.freebsd.org (svn.freebsd.org [IPv6:2001:4f8:fff6::2c]) by mx1.freebsd.org (Postfix) with ESMTP id 70F7D8FC18; Tue, 26 Oct 2010 09:25:29 +0000 (UTC) Received: from svn.freebsd.org (localhost [127.0.0.1]) by svn.freebsd.org (8.14.3/8.14.3) with ESMTP id o9Q9PT6H010232; Tue, 26 Oct 2010 09:25:29 GMT (envelope-from davidxu@svn.freebsd.org) Received: (from davidxu@localhost) by svn.freebsd.org (8.14.3/8.14.3/Submit) id o9Q9PTr2010224; Tue, 26 Oct 2010 09:25:29 GMT (envelope-from davidxu@svn.freebsd.org) Message-Id: <201010260925.o9Q9PTr2010224@svn.freebsd.org> From: David Xu Date: Tue, 26 Oct 2010 09:25:29 +0000 (UTC) To: src-committers@freebsd.org, svn-src-all@freebsd.org, svn-src-stable@freebsd.org, svn-src-stable-8@freebsd.org X-SVN-Group: stable-8 MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Cc: Subject: svn commit: r214371 - in stable/8: include lib/libthr/thread X-BeenThere: svn-src-all@freebsd.org X-Mailman-Version: 2.1.5 Precedence: list List-Id: "SVN commit messages for the entire src tree \(except for " user" and " projects" \)" List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , X-List-Received-Date: Tue, 26 Oct 2010 09:25:29 -0000 Author: davidxu Date: Tue Oct 26 09:25:29 2010 New Revision: 214371 URL: http://svn.freebsd.org/changeset/base/214371 Log: MFC r213241, r213257: In current code, statically initialized and destroyed object have same null value, the code can not distinguish between them, to fix the problem, now a destroyed object is assigned to a non-null value, and it will be rejected by some pthread functions. PTHREAD_ADAPTIVE_MUTEX_INITIALIZER_NP is changed to number 1, so that adaptive mutex can be statically initialized correctly. Modified: stable/8/include/pthread.h stable/8/lib/libthr/thread/thr_cond.c stable/8/lib/libthr/thread/thr_init.c stable/8/lib/libthr/thread/thr_mutex.c stable/8/lib/libthr/thread/thr_private.h stable/8/lib/libthr/thread/thr_rwlock.c Directory Properties: stable/8/include/ (props changed) stable/8/lib/libthr/ (props changed) Modified: stable/8/include/pthread.h ============================================================================== --- stable/8/include/pthread.h Tue Oct 26 08:50:09 2010 (r214370) +++ stable/8/include/pthread.h Tue Oct 26 09:25:29 2010 (r214371) @@ -98,7 +98,7 @@ * Static initialization values. */ #define PTHREAD_MUTEX_INITIALIZER NULL -#define PTHREAD_ADAPTIVE_MUTEX_INITIALIZER_NP NULL +#define PTHREAD_ADAPTIVE_MUTEX_INITIALIZER_NP ((pthread_mutex_t)1) #define PTHREAD_COND_INITIALIZER NULL #define PTHREAD_RWLOCK_INITIALIZER NULL Modified: stable/8/lib/libthr/thread/thr_cond.c ============================================================================== --- stable/8/lib/libthr/thread/thr_cond.c Tue Oct 26 08:50:09 2010 (r214370) +++ stable/8/lib/libthr/thread/thr_cond.c Tue Oct 26 09:25:29 2010 (r214371) @@ -104,6 +104,19 @@ init_static(struct pthread *thread, pthr return (ret); } +#define CHECK_AND_INIT_COND \ + if (__predict_false((cv = (*cond)) <= THR_COND_DESTROYED)) { \ + if (cv == THR_COND_INITIALIZER) { \ + int ret; \ + ret = init_static(_get_curthread(), cond); \ + if (ret) \ + return (ret); \ + } else if (cv == THR_COND_DESTROYED) { \ + return (EINVAL); \ + } \ + cv = *cond; \ + } + int _pthread_cond_init(pthread_cond_t *cond, const pthread_condattr_t *cond_attr) { @@ -119,16 +132,14 @@ _pthread_cond_destroy(pthread_cond_t *co struct pthread_cond *cv; int rval = 0; - if (*cond == NULL) + if ((cv = *cond) == THR_COND_INITIALIZER) + rval = 0; + else if (cv == THR_COND_DESTROYED) rval = EINVAL; else { cv = *cond; THR_UMUTEX_LOCK(curthread, &cv->c_lock); - /* - * NULL the caller's pointer now that the condition - * variable has been destroyed: - */ - *cond = NULL; + *cond = THR_COND_DESTROYED; THR_UMUTEX_UNLOCK(curthread, &cv->c_lock); /* @@ -137,7 +148,6 @@ _pthread_cond_destroy(pthread_cond_t *co */ free(cv); } - /* Return the completion status: */ return (rval); } @@ -170,20 +180,18 @@ cond_wait_common(pthread_cond_t *cond, p struct timespec ts, ts2, *tsp; struct cond_cancel_info info; pthread_cond_t cv; - int ret = 0; + int ret; /* * If the condition variable is statically initialized, * perform the dynamic initialization: */ - if (__predict_false(*cond == NULL && - (ret = init_static(curthread, cond)) != 0)) - return (ret); + CHECK_AND_INIT_COND cv = *cond; THR_UMUTEX_LOCK(curthread, &cv->c_lock); ret = _mutex_cv_unlock(mutex, &info.count); - if (ret) { + if (__predict_false(ret != 0)) { THR_UMUTEX_UNLOCK(curthread, &cv->c_lock); return (ret); } @@ -263,11 +271,8 @@ cond_signal_common(pthread_cond_t *cond, * If the condition variable is statically initialized, perform dynamic * initialization. */ - if (__predict_false(*cond == NULL && - (ret = init_static(curthread, cond)) != 0)) - return (ret); + CHECK_AND_INIT_COND - cv = *cond; THR_UMUTEX_LOCK(curthread, &cv->c_lock); if (!broadcast) ret = _thr_ucond_signal(&cv->c_kerncv); Modified: stable/8/lib/libthr/thread/thr_init.c ============================================================================== --- stable/8/lib/libthr/thread/thr_init.c Tue Oct 26 08:50:09 2010 (r214370) +++ stable/8/lib/libthr/thread/thr_init.c Tue Oct 26 09:25:29 2010 (r214371) @@ -92,6 +92,12 @@ struct pthread_mutex_attr _pthread_mutex .m_ceiling = 0 }; +struct pthread_mutex_attr _pthread_mutexattr_adaptive_default = { + .m_type = PTHREAD_MUTEX_ADAPTIVE_NP, + .m_protocol = PTHREAD_PRIO_NONE, + .m_ceiling = 0 +}; + /* Default condition variable attributes: */ struct pthread_cond_attr _pthread_condattr_default = { .c_pshared = PTHREAD_PROCESS_PRIVATE, Modified: stable/8/lib/libthr/thread/thr_mutex.c ============================================================================== --- stable/8/lib/libthr/thread/thr_mutex.c Tue Oct 26 08:50:09 2010 (r214370) +++ stable/8/lib/libthr/thread/thr_mutex.c Tue Oct 26 09:25:29 2010 (r214371) @@ -124,7 +124,7 @@ __weak_reference(_pthread_mutex_isowned_ static int mutex_init(pthread_mutex_t *mutex, - const pthread_mutexattr_t *mutex_attr, + const struct pthread_mutex_attr *mutex_attr, void *(calloc_cb)(size_t, size_t)) { const struct pthread_mutex_attr *attr; @@ -133,7 +133,7 @@ mutex_init(pthread_mutex_t *mutex, if (mutex_attr == NULL) { attr = &_pthread_mutexattr_default; } else { - attr = *mutex_attr; + attr = mutex_attr; if (attr->m_type < PTHREAD_MUTEX_ERRORCHECK || attr->m_type >= PTHREAD_MUTEX_TYPE_MAX) return (EINVAL); @@ -153,6 +153,10 @@ mutex_init(pthread_mutex_t *mutex, pmutex->m_yieldloops = 0; MUTEX_INIT_LINK(pmutex); switch(attr->m_protocol) { + case PTHREAD_PRIO_NONE: + pmutex->m_lock.m_owner = UMUTEX_UNOWNED; + pmutex->m_lock.m_flags = 0; + break; case PTHREAD_PRIO_INHERIT: pmutex->m_lock.m_owner = UMUTEX_UNOWNED; pmutex->m_lock.m_flags = UMUTEX_PRIO_INHERIT; @@ -162,9 +166,6 @@ mutex_init(pthread_mutex_t *mutex, pmutex->m_lock.m_flags = UMUTEX_PRIO_PROTECT; pmutex->m_lock.m_ceilings[0] = attr->m_ceiling; break; - case PTHREAD_PRIO_NONE: - pmutex->m_lock.m_owner = UMUTEX_UNOWNED; - pmutex->m_lock.m_flags = 0; } if (pmutex->m_type == PTHREAD_MUTEX_ADAPTIVE_NP) { @@ -184,11 +185,12 @@ init_static(struct pthread *thread, pthr THR_LOCK_ACQUIRE(thread, &_mutex_static_lock); - if (*mutex == NULL) - ret = mutex_init(mutex, NULL, calloc); + if (*mutex == THR_MUTEX_INITIALIZER) + ret = mutex_init(mutex, &_pthread_mutexattr_default, calloc); + else if (*mutex == THR_ADAPTIVE_MUTEX_INITIALIZER) + ret = mutex_init(mutex, &_pthread_mutexattr_adaptive_default, calloc); else ret = 0; - THR_LOCK_RELEASE(thread, &_mutex_static_lock); return (ret); @@ -210,7 +212,7 @@ int __pthread_mutex_init(pthread_mutex_t *mutex, const pthread_mutexattr_t *mutex_attr) { - return mutex_init(mutex, mutex_attr, calloc); + return mutex_init(mutex, mutex_attr ? *mutex_attr : NULL, calloc); } /* This function is used internally by malloc. */ @@ -223,9 +225,8 @@ _pthread_mutex_init_calloc_cb(pthread_mu .m_protocol = PTHREAD_PRIO_NONE, .m_ceiling = 0 }; - static const struct pthread_mutex_attr *pattr = &attr; - return mutex_init(mutex, (pthread_mutexattr_t *)&pattr, calloc_cb); + return mutex_init(mutex, &attr, calloc_cb); } void @@ -257,19 +258,21 @@ _pthread_mutex_destroy(pthread_mutex_t * uint32_t id; int ret = 0; - if (__predict_false(*mutex == NULL)) + m = *mutex; + if (m < THR_MUTEX_DESTROYED) { + ret = 0; + } else if (m == THR_MUTEX_DESTROYED) { ret = EINVAL; - else { + } else { id = TID(curthread); /* * Try to lock the mutex structure, we only need to * try once, if failed, the mutex is in used. */ - ret = _thr_umutex_trylock(&(*mutex)->m_lock, id); + ret = _thr_umutex_trylock(&m->m_lock, id); if (ret) return (ret); - m = *mutex; /* * Check mutex other fields to see if this mutex is * in use. Mostly for prority mutex types, or there @@ -281,11 +284,7 @@ _pthread_mutex_destroy(pthread_mutex_t * _thr_umutex_unlock(&m->m_lock, id); ret = EBUSY; } else { - /* - * Save a pointer to the mutex so it can be free'd - * and set the caller's pointer to NULL. - */ - *mutex = NULL; + *mutex = THR_MUTEX_DESTROYED; if (m->m_lock.m_flags & UMUTEX_PRIO_PROTECT) set_inherited_priority(curthread, m); @@ -310,17 +309,28 @@ _pthread_mutex_destroy(pthread_mutex_t * TAILQ_INSERT_TAIL(&curthread->pp_mutexq, (m), m_qe);\ } while (0) +#define CHECK_AND_INIT_MUTEX \ + if (__predict_false((m = *mutex) <= THR_MUTEX_DESTROYED)) { \ + if (m == THR_MUTEX_DESTROYED) \ + return (EINVAL); \ + int ret; \ + ret = init_static(_get_curthread(), mutex); \ + if (ret) \ + return (ret); \ + m = *mutex; \ + } + static int -mutex_trylock_common(struct pthread *curthread, pthread_mutex_t *mutex) +mutex_trylock_common(pthread_mutex_t *mutex) { - struct pthread_mutex *m; + struct pthread *curthread = _get_curthread(); + struct pthread_mutex *m = *mutex; uint32_t id; int ret; id = TID(curthread); - m = *mutex; ret = _thr_umutex_trylock(&m->m_lock, id); - if (ret == 0) { + if (__predict_true(ret == 0)) { ENQUEUE_MUTEX(curthread, m); } else if (m->m_owner == curthread) { ret = mutex_self_trylock(m); @@ -332,19 +342,11 @@ mutex_trylock_common(struct pthread *cur int __pthread_mutex_trylock(pthread_mutex_t *mutex) { - struct pthread *curthread = _get_curthread(); - int ret; + struct pthread_mutex *m; - /* - * If the mutex is statically initialized, perform the dynamic - * initialization: - */ - if (__predict_false(*mutex == NULL)) { - ret = init_static(curthread, mutex); - if (__predict_false(ret)) - return (ret); - } - return (mutex_trylock_common(curthread, mutex)); + CHECK_AND_INIT_MUTEX + + return (mutex_trylock_common(mutex)); } static int @@ -365,8 +367,10 @@ mutex_lock_sleep(struct pthread *curthre * the lock is likely to be released quickly and it is * faster than entering the kernel */ - if (m->m_lock.m_flags & (UMUTEX_PRIO_PROTECT | UMUTEX_PRIO_INHERIT)) - goto sleep_in_kernel; + if (__predict_false( + (m->m_lock.m_flags & + (UMUTEX_PRIO_PROTECT | UMUTEX_PRIO_INHERIT)) != 0)) + goto sleep_in_kernel; if (!_thr_is_smp) goto yield_loop; @@ -414,9 +418,10 @@ done: } static inline int -mutex_lock_common(struct pthread *curthread, struct pthread_mutex *m, +mutex_lock_common(struct pthread_mutex *m, const struct timespec *abstime) { + struct pthread *curthread = _get_curthread(); if (_thr_umutex_trylock2(&m->m_lock, TID(curthread)) == 0) { ENQUEUE_MUTEX(curthread, m); @@ -429,50 +434,25 @@ mutex_lock_common(struct pthread *curthr int __pthread_mutex_lock(pthread_mutex_t *mutex) { - struct pthread *curthread; - struct pthread_mutex *m; - int ret; + struct pthread_mutex *m; _thr_check_init(); - curthread = _get_curthread(); - - /* - * If the mutex is statically initialized, perform the dynamic - * initialization: - */ - if (__predict_false((m = *mutex) == NULL)) { - ret = init_static(curthread, mutex); - if (__predict_false(ret)) - return (ret); - m = *mutex; - } + CHECK_AND_INIT_MUTEX - return (mutex_lock_common(curthread, m, NULL)); + return (mutex_lock_common(m, NULL)); } int __pthread_mutex_timedlock(pthread_mutex_t *mutex, const struct timespec *abstime) { - struct pthread *curthread; - struct pthread_mutex *m; - int ret; + struct pthread_mutex *m; _thr_check_init(); - curthread = _get_curthread(); + CHECK_AND_INIT_MUTEX - /* - * If the mutex is statically initialized, perform the dynamic - * initialization: - */ - if (__predict_false((m = *mutex) == NULL)) { - ret = init_static(curthread, mutex); - if (__predict_false(ret)) - return (ret); - m = *mutex; - } - return (mutex_lock_common(curthread, m, abstime)); + return (mutex_lock_common(m, abstime)); } int @@ -482,20 +462,22 @@ _pthread_mutex_unlock(pthread_mutex_t *m } int -_mutex_cv_lock(pthread_mutex_t *m, int count) +_mutex_cv_lock(pthread_mutex_t *mutex, int count) { + struct pthread_mutex *m; int ret; - ret = mutex_lock_common(_get_curthread(), *m, NULL); + m = *mutex; + ret = mutex_lock_common(m, NULL); if (ret == 0) { - (*m)->m_refcount--; - (*m)->m_count += count; + m->m_refcount--; + m->m_count += count; } return (ret); } static int -mutex_self_trylock(pthread_mutex_t m) +mutex_self_trylock(struct pthread_mutex *m) { int ret; @@ -523,7 +505,7 @@ mutex_self_trylock(pthread_mutex_t m) } static int -mutex_self_lock(pthread_mutex_t m, const struct timespec *abstime) +mutex_self_lock(struct pthread_mutex *m, const struct timespec *abstime) { struct timespec ts1, ts2; int ret; @@ -598,8 +580,12 @@ mutex_unlock_common(pthread_mutex_t *mut struct pthread_mutex *m; uint32_t id; - if (__predict_false((m = *mutex) == NULL)) - return (EINVAL); + m = *mutex; + if (__predict_false(m <= THR_MUTEX_DESTROYED)) { + if (m == THR_MUTEX_DESTROYED) + return (EINVAL); + return (EPERM); + } /* * Check if the running thread is not the owner of the mutex. @@ -616,7 +602,7 @@ mutex_unlock_common(pthread_mutex_t *mut m->m_owner = NULL; /* Remove the mutex from the threads queue. */ MUTEX_ASSERT_IS_OWNED(m); - if ((m->m_lock.m_flags & UMUTEX_PRIO_PROTECT) == 0) + if (__predict_true((m->m_lock.m_flags & UMUTEX_PRIO_PROTECT) == 0)) TAILQ_REMOVE(&curthread->mutexq, m, m_qe); else { TAILQ_REMOVE(&curthread->pp_mutexq, m, m_qe); @@ -634,8 +620,12 @@ _mutex_cv_unlock(pthread_mutex_t *mutex, struct pthread *curthread = _get_curthread(); struct pthread_mutex *m; - if (__predict_false((m = *mutex) == NULL)) - return (EINVAL); + m = *mutex; + if (__predict_false(m <= THR_MUTEX_DESTROYED)) { + if (m == THR_MUTEX_DESTROYED) + return (EINVAL); + return (EPERM); + } /* * Check if the running thread is not the owner of the mutex. @@ -652,7 +642,7 @@ _mutex_cv_unlock(pthread_mutex_t *mutex, m->m_owner = NULL; /* Remove the mutex from the threads queue. */ MUTEX_ASSERT_IS_OWNED(m); - if ((m->m_lock.m_flags & UMUTEX_PRIO_PROTECT) == 0) + if (__predict_true((m->m_lock.m_flags & UMUTEX_PRIO_PROTECT) == 0)) TAILQ_REMOVE(&curthread->mutexq, m, m_qe); else { TAILQ_REMOVE(&curthread->pp_mutexq, m, m_qe); @@ -667,18 +657,19 @@ int _pthread_mutex_getprioceiling(pthread_mutex_t *mutex, int *prioceiling) { + struct pthread_mutex *m; int ret; - if (*mutex == NULL) - ret = EINVAL; - else if (((*mutex)->m_lock.m_flags & UMUTEX_PRIO_PROTECT) == 0) + m = *mutex; + if ((m <= THR_MUTEX_DESTROYED) || + (m->m_lock.m_flags & UMUTEX_PRIO_PROTECT) == 0) ret = EINVAL; else { - *prioceiling = (*mutex)->m_lock.m_ceilings[0]; + *prioceiling = m->m_lock.m_ceilings[0]; ret = 0; } - return(ret); + return (ret); } int @@ -690,7 +681,8 @@ _pthread_mutex_setprioceiling(pthread_mu int ret; m = *mutex; - if (m == NULL || (m->m_lock.m_flags & UMUTEX_PRIO_PROTECT) == 0) + if ((m <= THR_MUTEX_DESTROYED) || + (m->m_lock.m_flags & UMUTEX_PRIO_PROTECT) == 0) return (EINVAL); ret = __thr_umutex_set_ceiling(&m->m_lock, ceiling, old_ceiling); @@ -719,61 +711,54 @@ _pthread_mutex_setprioceiling(pthread_mu int _pthread_mutex_getspinloops_np(pthread_mutex_t *mutex, int *count) { - if (*mutex == NULL) - return (EINVAL); - *count = (*mutex)->m_spinloops; + struct pthread_mutex *m; + + CHECK_AND_INIT_MUTEX + + *count = m->m_spinloops; return (0); } int __pthread_mutex_setspinloops_np(pthread_mutex_t *mutex, int count) { - struct pthread *curthread = _get_curthread(); - int ret; + struct pthread_mutex *m; - if (__predict_false(*mutex == NULL)) { - ret = init_static(curthread, mutex); - if (__predict_false(ret)) - return (ret); - } - (*mutex)->m_spinloops = count; + CHECK_AND_INIT_MUTEX + + m->m_spinloops = count; return (0); } int _pthread_mutex_getyieldloops_np(pthread_mutex_t *mutex, int *count) { - if (*mutex == NULL) - return (EINVAL); - *count = (*mutex)->m_yieldloops; + struct pthread_mutex *m; + + CHECK_AND_INIT_MUTEX + + *count = m->m_yieldloops; return (0); } int __pthread_mutex_setyieldloops_np(pthread_mutex_t *mutex, int count) { - struct pthread *curthread = _get_curthread(); - int ret; + struct pthread_mutex *m; - if (__predict_false(*mutex == NULL)) { - ret = init_static(curthread, mutex); - if (__predict_false(ret)) - return (ret); - } - (*mutex)->m_yieldloops = count; + CHECK_AND_INIT_MUTEX + + m->m_yieldloops = count; return (0); } int _pthread_mutex_isowned_np(pthread_mutex_t *mutex) { - struct pthread *curthread = _get_curthread(); - int ret; + struct pthread_mutex *m; - if (__predict_false(*mutex == NULL)) { - ret = init_static(curthread, mutex); - if (__predict_false(ret)) - return (ret); - } - return ((*mutex)->m_owner == curthread); + m = *mutex; + if (m <= THR_MUTEX_DESTROYED) + return (0); + return (m->m_owner == _get_curthread()); } Modified: stable/8/lib/libthr/thread/thr_private.h ============================================================================== --- stable/8/lib/libthr/thread/thr_private.h Tue Oct 26 08:50:09 2010 (r214370) +++ stable/8/lib/libthr/thread/thr_private.h Tue Oct 26 09:25:29 2010 (r214371) @@ -107,6 +107,15 @@ TAILQ_HEAD(mutex_queue, pthread_mutex); } \ } while (0) +/* XXX These values should be same as those defined in pthread.h */ +#define THR_MUTEX_INITIALIZER ((struct pthread_mutex *)NULL) +#define THR_ADAPTIVE_MUTEX_INITIALIZER ((struct pthread_mutex *)1) +#define THR_MUTEX_DESTROYED ((struct pthread_mutex *)2) +#define THR_COND_INITIALIZER ((struct pthread_cond *)NULL) +#define THR_COND_DESTROYED ((struct pthread_cond *)1) +#define THR_RWLOCK_INITIALIZER ((struct pthread_rwlock *)NULL) +#define THR_RWLOCK_DESTROYED ((struct pthread_rwlock *)1) + struct pthread_mutex { /* * Lock for accesses to this structure. @@ -571,6 +580,7 @@ extern struct pthread_attr _pthread_attr /* Default mutex attributes: */ extern struct pthread_mutex_attr _pthread_mutexattr_default __hidden; +extern struct pthread_mutex_attr _pthread_mutexattr_adaptive_default __hidden; /* Default condition variable attributes: */ extern struct pthread_cond_attr _pthread_condattr_default __hidden; Modified: stable/8/lib/libthr/thread/thr_rwlock.c ============================================================================== --- stable/8/lib/libthr/thread/thr_rwlock.c Tue Oct 26 08:50:09 2010 (r214370) +++ stable/8/lib/libthr/thread/thr_rwlock.c Tue Oct 26 09:25:29 2010 (r214371) @@ -45,6 +45,19 @@ __weak_reference(_pthread_rwlock_unlock, __weak_reference(_pthread_rwlock_wrlock, pthread_rwlock_wrlock); __weak_reference(_pthread_rwlock_timedwrlock, pthread_rwlock_timedwrlock); +#define CHECK_AND_INIT_RWLOCK \ + if (__predict_false((prwlock = (*rwlock)) <= THR_RWLOCK_DESTROYED)) { \ + if (prwlock == THR_RWLOCK_INITIALIZER) { \ + int ret; \ + ret = init_static(_get_curthread(), rwlock); \ + if (ret) \ + return (ret); \ + } else if (prwlock == THR_RWLOCK_DESTROYED) { \ + return (EINVAL); \ + } \ + prwlock = *rwlock; \ + } + /* * Prototypes */ @@ -64,15 +77,16 @@ rwlock_init(pthread_rwlock_t *rwlock, co int _pthread_rwlock_destroy (pthread_rwlock_t *rwlock) { + pthread_rwlock_t prwlock; int ret; - if (rwlock == NULL) + prwlock = *rwlock; + if (prwlock == THR_RWLOCK_INITIALIZER) + ret = 0; + else if (prwlock == THR_RWLOCK_DESTROYED) ret = EINVAL; else { - pthread_rwlock_t prwlock; - - prwlock = *rwlock; - *rwlock = NULL; + *rwlock = THR_RWLOCK_DESTROYED; free(prwlock); ret = 0; @@ -87,7 +101,7 @@ init_static(struct pthread *thread, pthr THR_LOCK_ACQUIRE(thread, &_rwlock_static_lock); - if (*rwlock == NULL) + if (*rwlock == THR_RWLOCK_INITIALIZER) ret = rwlock_init(rwlock, NULL); else ret = 0; @@ -113,18 +127,7 @@ rwlock_rdlock_common(pthread_rwlock_t *r int flags; int ret; - if (__predict_false(rwlock == NULL)) - return (EINVAL); - - prwlock = *rwlock; - - /* check for static initialization */ - if (__predict_false(prwlock == NULL)) { - if ((ret = init_static(curthread, rwlock)) != 0) - return (ret); - - prwlock = *rwlock; - } + CHECK_AND_INIT_RWLOCK if (curthread->rdlock_count) { /* @@ -206,18 +209,7 @@ _pthread_rwlock_tryrdlock (pthread_rwloc int flags; int ret; - if (__predict_false(rwlock == NULL)) - return (EINVAL); - - prwlock = *rwlock; - - /* check for static initialization */ - if (__predict_false(prwlock == NULL)) { - if ((ret = init_static(curthread, rwlock)) != 0) - return (ret); - - prwlock = *rwlock; - } + CHECK_AND_INIT_RWLOCK if (curthread->rdlock_count) { /* @@ -250,18 +242,7 @@ _pthread_rwlock_trywrlock (pthread_rwloc pthread_rwlock_t prwlock; int ret; - if (__predict_false(rwlock == NULL)) - return (EINVAL); - - prwlock = *rwlock; - - /* check for static initialization */ - if (__predict_false(prwlock == NULL)) { - if ((ret = init_static(curthread, rwlock)) != 0) - return (ret); - - prwlock = *rwlock; - } + CHECK_AND_INIT_RWLOCK ret = _thr_rwlock_trywrlock(&prwlock->lock); if (ret == 0) @@ -277,18 +258,7 @@ rwlock_wrlock_common (pthread_rwlock_t * struct timespec ts, ts2, *tsp; int ret; - if (__predict_false(rwlock == NULL)) - return (EINVAL); - - prwlock = *rwlock; - - /* check for static initialization */ - if (__predict_false(prwlock == NULL)) { - if ((ret = init_static(curthread, rwlock)) != 0) - return (ret); - - prwlock = *rwlock; - } + CHECK_AND_INIT_RWLOCK /* * POSIX said the validity of the abstimeout parameter need @@ -356,12 +326,9 @@ _pthread_rwlock_unlock (pthread_rwlock_t int ret; int32_t state; - if (__predict_false(rwlock == NULL)) - return (EINVAL); - prwlock = *rwlock; - if (__predict_false(prwlock == NULL)) + if (__predict_false(prwlock <= THR_RWLOCK_DESTROYED)) return (EINVAL); state = prwlock->lock.rw_state;