Skip site navigation (1)Skip section navigation (2)
Date:      Fri, 7 Jan 2005 04:16:19 GMT
From:      David Xu <davidxu@FreeBSD.org>
To:        Perforce Change Reviews <perforce@freebsd.org>
Subject:   PERFORCE change 68450 for review
Message-ID:  <200501070416.j074GJWH086169@repoman.freebsd.org>

next in thread | raw e-mail | index | archive | help
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



Want to link to this message? Use this URL: <https://mail-archive.FreeBSD.org/cgi/mid.cgi?200501070416.j074GJWH086169>