Пример #1
0
/*
 * Lock a location for the running thread. Yield to allow other
 * threads to run if this thread is blocked because the lock is
 * not available. Note that this function does not sleep. It
 * assumes that the lock will be available very soon.
 *
 * This function checks if the running thread has already locked the
 * location, warns if this occurs and creates a thread dump before
 * returning.
 */
void
_spinlock_debug(spinlock_t *lck, char *fname, int lineno)
{
	struct pthread	*curthread = _get_curthread();
	int cnt = 0;

	/*
	 * Try to grab the lock and loop if another thread grabs
	 * it before we do.
	 */
	while(_atomic_lock(&lck->access_lock)) {
		cnt++;
		if (cnt > 100) {
			char str[256];
			snprintf(str, sizeof(str), "%s - Warning: Thread %p attempted to lock %p from %s (%d) was left locked from %s (%d)\n", _getprogname(), curthread, lck, fname, lineno, lck->fname, lck->lineno);
			__sys_extpwrite(2, str, strlen(str), O_FBLOCKING, -1);
			__sleep(1);
			cnt = 0;
		}

		/* Block the thread until the lock. */
		curthread->data.spinlock = lck;
		_thread_kern_sched_state(PS_SPINBLOCK, fname, lineno);
	}

	/* The running thread now owns the lock: */
	lck->lock_owner = (long) curthread;
	lck->fname = fname;
	lck->lineno = lineno;
}
Пример #2
0
/*
 * internal support functions
 */
void
_spinlock(_spinlock_lock_t *lock)
{

	while (_atomic_lock(lock))
		pthread_yield();
}
Пример #3
0
/*
 * Lock a location for the running thread. Yield to allow other
 * threads to run if this thread is blocked because the lock is
 * not available. Note that this function does not sleep. It
 * assumes that the lock will be available very soon.
 */
void
_spinlock(spinlock_t *lck)
{
	struct pthread	*curthread = _get_curthread();

	/*
	 * Try to grab the lock and loop if another thread grabs
	 * it before we do.
	 */
	while(_atomic_lock(&lck->access_lock)) {
		/* Block the thread until the lock. */
		curthread->data.spinlock = lck;
		_thread_kern_sched_state(PS_SPINBLOCK, __FILE__, __LINE__);
	}

	/* The running thread now owns the lock: */
	lck->lock_owner = (long) curthread;
}