Example #1
0
int
_pthread_spin_lock(pthread_spinlock_t *lock)
{
	struct pthread *curthread = _get_curthread();
	struct pthread_spinlock	*lck;
	int ret, count;

	if (lock == NULL || (lck = *lock) == NULL)
		ret = EINVAL;
	else {
		count = SPIN_COUNT;
		while ((ret = THR_UMUTEX_TRYLOCK(curthread, &lck->s_lock)) != 0) {
			while (lck->s_lock.m_owner) {
				if (!_thr_is_smp) {
					_pthread_yield();
				} else {
					CPU_SPINWAIT;

					if (--count <= 0) {
						count = SPIN_COUNT;
						_pthread_yield();
					}
				}
			}
		}
		ret = 0;
	}

	return (ret);
}
Example #2
0
int
_pthread_spin_lock(pthread_spinlock_t *lock)
{
	struct pthread *curthread;
	struct pthread_spinlock	*lck;
	int count;

	if (lock == NULL)
		return (EINVAL);
	lck = *lock == THR_PSHARED_PTR ? __thr_pshared_offpage(lock, 0) : *lock;
	if (lck == NULL)
		return (EINVAL);

	curthread = _get_curthread();
	count = SPIN_COUNT;
	while (THR_UMUTEX_TRYLOCK(curthread, &lck->s_lock) != 0) {
		while (lck->s_lock.m_owner) {
			if (!_thr_is_smp) {
				_pthread_yield();
			} else {
				CPU_SPINWAIT;
				if (--count <= 0) {
					count = SPIN_COUNT;
					_pthread_yield();
				}
			}
		}
	}
	return (0);
}
Example #3
0
int
_pthread_spin_lock(pthread_spinlock_t *lock)
{
	struct pthread_spinlock	*lck;
	struct pthread *self = _pthread_self();
	int count, oldval, ret;

	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();
				}
			}
			atomic_swap_int(&(lck)->s_lock, 1, &oldval);
		} while (oldval);

		lck->s_owner = self;
		ret = 0;
	}

	return (ret);
}