PERFORCE change 65528 for review

David Xu davidxu at FreeBSD.org
Fri Nov 19 21:06:43 PST 2004


http://perforce.freebsd.org/chv.cgi?CH=65528

Change 65528 by davidxu at davidxu_alona on 2004/11/20 05:05:45

	use umtx lock.

Affected files ...

.. //depot/projects/davidxu_thread/src/lib/libthread/thread/thr_pspinlock.c#2 edit

Differences ...

==== //depot/projects/davidxu_thread/src/lib/libthread/thread/thr_pspinlock.c#2 (text+ko) ====

@@ -29,7 +29,6 @@
 #include <errno.h>
 #include <stdlib.h>
 #include <pthread.h>
-#include <atomic_ops.h>
 #include "thr_private.h"
 
 #define SPIN_COUNT 10000
@@ -51,8 +50,7 @@
 	else if ((lck = malloc(sizeof(struct pthread_spinlock))) == NULL)
 		ret = ENOMEM;
 	else {
-		lck->s_lock = 0;
-		lck->s_owner= NULL;
+		_lock_init(&lck->s_lock);
 		*lock = lck;
 		ret = 0;
 	}
@@ -67,9 +65,8 @@
 
 	if (lock == NULL || *lock == NULL)
 		ret = EINVAL;
-	else if ((*lock)->s_owner != NULL)
-		ret = EBUSY;
 	else {
+		_lock_destroy(&(*lock)->s_lock);
 		free(*lock);
 		*lock = NULL;
 		ret = 0;
@@ -83,23 +80,12 @@
 {
 	struct pthread_spinlock	*lck;
 	struct pthread *self = _pthread_self();
-	int oldval, ret;
+	int ret;
 
 	if (lock == NULL || (lck = *lock) == NULL)
 		ret = EINVAL;
-	else if (lck->s_owner == self)
-		ret = EDEADLK;
-	else if (lck->s_lock != 0)
-		ret = EBUSY;
-	else {
-		atomic_swap_int((int *)&(lck)->s_lock, 1, &oldval);
-		if (oldval)
-			ret = EBUSY;
-		else {
-			lck->s_owner = _pthread_self();
-			ret = 0;
-		}
-	}
+	else
+		ret = _lock_trylock(&lck->s_lock, self->tid);
 	return (ret);
 }
 
@@ -108,30 +94,20 @@
 {
 	struct pthread_spinlock	*lck;
 	struct pthread *self = _pthread_self();
-	int count, oldval, ret;
+	int ret, count;
 
 	if (lock == NULL || (lck = *lock) == NULL)
 		ret = EINVAL;
-	else if (lck->s_owner == self)
-		ret = EDEADLK;
 	else {
-		do {
-			count = SPIN_COUNT;
-			while (lck->s_lock) {
-#ifdef __i386__
-				/* tell cpu we are spinning */
-				__asm __volatile("pause");
-#endif
-				if (--count <= 0) {
-					count = SPIN_COUNT;
-					_pthread_yield();
-				}
+		count = SPIN_COUNT;
+		while ((ret = _lock_trylock(&lck->s_lock, self->tid)) != 0) {
+			if (ret != EINTR && ret != EAGAIN && ret != EBUSY)
+				break;
+			if (--count <= 0) {
+				count = SPIN_COUNT;
+				_pthread_yield();
 			}
-			atomic_swap_int((int *)&(lck)->s_lock, 1, &oldval);
-		} while (oldval);
-
-		lck->s_owner = self;
-		ret = 0;
+		}
 	}
 
 	return (ret);
@@ -141,20 +117,14 @@
 _pthread_spin_unlock(pthread_spinlock_t *lock)
 {
 	struct pthread_spinlock	*lck;
+	struct pthread *self = _pthread_self();
 	int ret;
 
 	if (lock == NULL || (lck = *lock) == NULL)
 		ret = EINVAL;
 	else {
-		if (lck->s_owner != _pthread_self())
-			ret = EPERM;
-		else {
-			lck->s_owner = NULL;
-			atomic_swap_int((int *)&lck->s_lock, 0, &ret);
-			ret = 0;
-		}
+		_lock_release(&lck->s_lock, self->tid);
+		ret = 0;
 	}
-
 	return (ret);
 }
-


More information about the p4-projects mailing list