From owner-p4-projects@FreeBSD.ORG Fri Jan 7 04:16:20 2005 Return-Path: Delivered-To: p4-projects@freebsd.org Received: by hub.freebsd.org (Postfix, from userid 32767) id 459DC16A4DA; Fri, 7 Jan 2005 04:16:20 +0000 (GMT) Delivered-To: perforce@freebsd.org Received: from mx1.FreeBSD.org (mx1.freebsd.org [216.136.204.125]) by hub.freebsd.org (Postfix) with ESMTP id 016FF16A4CE for ; Fri, 7 Jan 2005 04:16:20 +0000 (GMT) Received: from repoman.freebsd.org (repoman.freebsd.org [216.136.204.115]) by mx1.FreeBSD.org (Postfix) with ESMTP id DB23743D39 for ; Fri, 7 Jan 2005 04:16:19 +0000 (GMT) (envelope-from davidxu@freebsd.org) Received: from repoman.freebsd.org (localhost [127.0.0.1]) by repoman.freebsd.org (8.13.1/8.13.1) with ESMTP id j074GJGH086172 for ; Fri, 7 Jan 2005 04:16:19 GMT (envelope-from davidxu@freebsd.org) Received: (from perforce@localhost) by repoman.freebsd.org (8.13.1/8.13.1/Submit) id j074GJWH086169 for perforce@freebsd.org; Fri, 7 Jan 2005 04:16:19 GMT (envelope-from davidxu@freebsd.org) Date: Fri, 7 Jan 2005 04:16:19 GMT Message-Id: <200501070416.j074GJWH086169@repoman.freebsd.org> X-Authentication-Warning: repoman.freebsd.org: perforce set sender to davidxu@freebsd.org using -f From: David Xu To: Perforce Change Reviews Subject: PERFORCE change 68450 for review X-BeenThere: p4-projects@freebsd.org X-Mailman-Version: 2.1.1 Precedence: list List-Id: p4 projects tree changes List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , X-List-Received-Date: Fri, 07 Jan 2005 04:16:21 -0000 http://perforce.freebsd.org/chv.cgi?CH=68450 Change 68450 by davidxu@davidxu_celeron on 2005/01/07 04:15:21 use atomic operation, remove static initializing lock. Affected files ... .. //depot/projects/davidxu_thread/src/lib/libthread/thread/thr_cond.c#9 edit Differences ... ==== //depot/projects/davidxu_thread/src/lib/libthread/thread/thr_cond.c#9 (text+ko) ==== @@ -37,10 +37,10 @@ /* * Prototypes */ -static int init_static(struct pthread *thread, pthread_cond_t *cond); -static int cond_wait_common(pthread_cond_t *cond, pthread_mutex_t *mutex, +static int cond_init(pthread_cond_t *cond, const pthread_condattr_t *attr); +static int cond_wait_common(pthread_cond_t *cond, pthread_mutex_t *mutex, const struct timespec *abstime, int cancel); -static int cond_signal_common(pthread_cond_t *cond, int broadcast); +static int cond_signal_common(pthread_cond_t *cond, int broadcast); /* * Double underscore versions are cancellation points. Single underscore @@ -55,47 +55,36 @@ __weak_reference(_pthread_cond_signal, pthread_cond_signal); __weak_reference(_pthread_cond_broadcast, pthread_cond_broadcast); -int -_pthread_cond_init(pthread_cond_t *cond, const pthread_condattr_t *cond_attr) +static int +cond_init(pthread_cond_t *cond, const pthread_condattr_t *cond_attr) { pthread_cond_t pcond; int rval = 0; - if (cond == NULL) - rval = EINVAL; - else { - if ((pcond = (pthread_cond_t) - malloc(sizeof(struct pthread_cond))) == NULL) { - rval = ENOMEM; - } else { - /* - * Initialise the condition variable structure: - */ - umtx_init(&pcond->c_lock); - pcond->c_seqno = 0; - pcond->c_waiters = 0; - pcond->c_wakeups = 0; - pcond->c_flags = 0; - *cond = pcond; - } + if ((pcond = (pthread_cond_t) + malloc(sizeof(struct pthread_cond))) == NULL) { + rval = ENOMEM; + } else { + /* + * Initialise the condition variable structure: + */ + umtx_init(&pcond->c_lock); + pcond->c_seqno = 0; + pcond->c_waiters = 0; + pcond->c_wakeups = 0; + pcond->c_flags = 0; + if (!atomic_cmpset_acq_ptr(cond, NULL, pcond)) + free(pcond); } /* Return the completion status: */ return (rval); } -static int -init_static(struct pthread *thread, pthread_cond_t *cond) +int +_pthread_cond_init(pthread_cond_t *cond, const pthread_condattr_t *cond_attr) { - int ret; - - THR_LOCK_ACQUIRE(thread, &_cond_static_lock); - if (*cond == NULL) - ret = pthread_cond_init(cond, NULL); - else - ret = 0; - THR_LOCK_RELEASE(thread, &_cond_static_lock); - - return (ret); + *cond = NULL; + return cond_init(cond, cond_attr); } int @@ -105,7 +94,7 @@ struct pthread *curthread = _get_curthread(); int rval = 0; - if (cond == NULL || *cond == NULL) + if (*cond == NULL) rval = EINVAL; else { /* Lock the condition variable structure: */ @@ -184,7 +173,7 @@ * perform the dynamic initialization: */ if (__predict_false(*cond == NULL && - (ret = init_static(curthread, cond)) != 0)) + (ret = cond_init(cond, NULL)) != 0)) return (ret); cv = *cond; @@ -284,15 +273,15 @@ { struct pthread *curthread = _get_curthread(); pthread_cond_t cv; - int rval = 0; + int ret = 0; /* * If the condition variable is statically initialized, perform dynamic * initialization. */ if (__predict_false(*cond == NULL && - (rval = init_static(curthread, cond)) != 0)) - return (rval); + (ret = cond_init(cond, NULL)) != 0)) + return (ret); cv = *cond; /* Lock the condition variable structure. */ @@ -311,7 +300,7 @@ } } THR_LOCK_RELEASE(curthread, &cv->c_lock); - return (rval); + return (ret); } int