1998-04-29 09:59:34 +00:00
|
|
|
/*
|
|
|
|
* Copyright (c) 1997 John Birrell <jb@cimlogic.com.au>.
|
|
|
|
* All rights reserved.
|
|
|
|
*
|
|
|
|
* Redistribution and use in source and binary forms, with or without
|
|
|
|
* modification, are permitted provided that the following conditions
|
|
|
|
* are met:
|
|
|
|
* 1. Redistributions of source code must retain the above copyright
|
|
|
|
* notice, this list of conditions and the following disclaimer.
|
|
|
|
* 2. Redistributions in binary form must reproduce the above copyright
|
|
|
|
* notice, this list of conditions and the following disclaimer in the
|
|
|
|
* documentation and/or other materials provided with the distribution.
|
|
|
|
* 3. All advertising materials mentioning features or use of this software
|
|
|
|
* must display the following acknowledgement:
|
|
|
|
* This product includes software developed by John Birrell.
|
|
|
|
* 4. Neither the name of the author nor the names of any co-contributors
|
|
|
|
* may be used to endorse or promote products derived from this software
|
|
|
|
* without specific prior written permission.
|
|
|
|
*
|
|
|
|
* THIS SOFTWARE IS PROVIDED BY JOHN BIRRELL AND CONTRIBUTORS ``AS IS'' AND
|
|
|
|
* ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
|
|
|
|
* IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
|
1999-08-05 12:15:30 +00:00
|
|
|
* ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
|
1998-04-29 09:59:34 +00:00
|
|
|
* FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
|
|
|
|
* DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
|
|
|
|
* OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
|
|
|
|
* HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
|
|
|
|
* LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
|
|
|
|
* OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
|
|
|
|
* SUCH DAMAGE.
|
|
|
|
*
|
1999-08-28 00:22:10 +00:00
|
|
|
* $FreeBSD$
|
1998-04-29 09:59:34 +00:00
|
|
|
*
|
|
|
|
*/
|
|
|
|
|
2003-04-28 23:56:12 +00:00
|
|
|
#include <sys/types.h>
|
|
|
|
#include <machine/atomic.h>
|
2002-03-29 22:43:43 +00:00
|
|
|
|
|
|
|
#include <libc_private.h>
|
2003-04-18 05:04:16 +00:00
|
|
|
#include "spinlock.h"
|
2002-09-16 08:45:36 +00:00
|
|
|
#include "thr_private.h"
|
1998-06-09 23:13:10 +00:00
|
|
|
|
2003-05-29 17:10:45 +00:00
|
|
|
#define MAX_SPINLOCKS 5
|
|
|
|
|
|
|
|
struct spinlock_extra {
|
2003-11-04 19:59:22 +00:00
|
|
|
spinlock_t *owner;
|
2003-12-09 02:37:40 +00:00
|
|
|
pthread_mutex_t lock;
|
2003-05-29 17:10:45 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
static void init_spinlock(spinlock_t *lck);
|
|
|
|
|
2004-12-18 18:07:37 +00:00
|
|
|
static struct pthread_mutex_attr static_mutex_attr =
|
|
|
|
PTHREAD_MUTEXATTR_STATIC_INITIALIZER;
|
|
|
|
static pthread_mutexattr_t static_mattr = &static_mutex_attr;
|
|
|
|
|
2003-12-09 02:37:40 +00:00
|
|
|
static pthread_mutex_t spinlock_static_lock;
|
2003-05-29 17:10:45 +00:00
|
|
|
static struct spinlock_extra extra[MAX_SPINLOCKS];
|
|
|
|
static int spinlock_count = 0;
|
|
|
|
static int initialized = 0;
|
|
|
|
|
2003-04-18 05:04:16 +00:00
|
|
|
/*
|
|
|
|
* These are for compatability only. Spinlocks of this type
|
|
|
|
* are deprecated.
|
|
|
|
*/
|
|
|
|
|
2003-03-26 04:02:24 +00:00
|
|
|
void
|
|
|
|
_spinunlock(spinlock_t *lck)
|
|
|
|
{
|
2003-05-29 17:10:45 +00:00
|
|
|
struct spinlock_extra *extra;
|
2003-04-28 23:56:12 +00:00
|
|
|
|
2003-05-29 17:10:45 +00:00
|
|
|
extra = (struct spinlock_extra *)lck->fname;
|
2004-12-18 18:07:37 +00:00
|
|
|
_pthread_mutex_unlock(&extra->lock);
|
2003-03-26 04:02:24 +00:00
|
|
|
}
|
|
|
|
|
1998-04-29 09:59:34 +00:00
|
|
|
/*
|
|
|
|
* 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
|
1998-06-09 23:13:10 +00:00
|
|
|
_spinlock(spinlock_t *lck)
|
1998-04-29 09:59:34 +00:00
|
|
|
{
|
2003-12-09 02:37:40 +00:00
|
|
|
struct spinlock_extra *extra;
|
2003-04-28 23:56:12 +00:00
|
|
|
|
2003-12-09 02:37:40 +00:00
|
|
|
if (!__isthreaded)
|
|
|
|
PANIC("Spinlock called when not threaded.");
|
|
|
|
if (!initialized)
|
|
|
|
PANIC("Spinlocks not initialized.");
|
1998-06-09 23:13:10 +00:00
|
|
|
/*
|
|
|
|
* Try to grab the lock and loop if another thread grabs
|
|
|
|
* it before we do.
|
|
|
|
*/
|
2003-05-29 17:10:45 +00:00
|
|
|
if (lck->fname == NULL)
|
|
|
|
init_spinlock(lck);
|
|
|
|
extra = (struct spinlock_extra *)lck->fname;
|
2004-12-18 18:07:37 +00:00
|
|
|
_pthread_mutex_lock(&extra->lock);
|
1998-06-09 23:13:10 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* 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)
|
|
|
|
{
|
2003-04-28 23:56:12 +00:00
|
|
|
_spinlock(lck);
|
1998-04-29 09:59:34 +00:00
|
|
|
}
|
2003-05-29 17:10:45 +00:00
|
|
|
|
|
|
|
static void
|
|
|
|
init_spinlock(spinlock_t *lck)
|
|
|
|
{
|
2004-12-18 18:07:37 +00:00
|
|
|
_pthread_mutex_lock(&spinlock_static_lock);
|
2003-05-29 17:10:45 +00:00
|
|
|
if ((lck->fname == NULL) && (spinlock_count < MAX_SPINLOCKS)) {
|
|
|
|
lck->fname = (char *)&extra[spinlock_count];
|
2003-11-04 19:59:22 +00:00
|
|
|
extra[spinlock_count].owner = lck;
|
2003-05-29 17:10:45 +00:00
|
|
|
spinlock_count++;
|
|
|
|
}
|
2004-12-18 18:07:37 +00:00
|
|
|
_pthread_mutex_unlock(&spinlock_static_lock);
|
2003-12-09 02:37:40 +00:00
|
|
|
if (lck->fname == NULL)
|
|
|
|
PANIC("Exceeded max spinlocks");
|
2003-05-29 17:10:45 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
void
|
|
|
|
_thr_spinlock_init(void)
|
|
|
|
{
|
|
|
|
int i;
|
|
|
|
|
|
|
|
if (initialized != 0) {
|
2003-12-09 02:37:40 +00:00
|
|
|
_thr_mutex_reinit(&spinlock_static_lock);
|
|
|
|
for (i = 0; i < spinlock_count; i++)
|
|
|
|
_thr_mutex_reinit(&extra[i].lock);
|
2003-11-04 19:59:22 +00:00
|
|
|
} else {
|
2004-12-18 18:07:37 +00:00
|
|
|
if (_pthread_mutex_init(&spinlock_static_lock, &static_mattr))
|
2003-11-04 19:59:22 +00:00
|
|
|
PANIC("Cannot initialize spinlock_static_lock");
|
|
|
|
for (i = 0; i < MAX_SPINLOCKS; i++) {
|
2004-12-18 18:07:37 +00:00
|
|
|
if (_pthread_mutex_init(&extra[i].lock, &static_mattr))
|
2003-11-04 19:59:22 +00:00
|
|
|
PANIC("Cannot initialize spinlock extra");
|
|
|
|
}
|
|
|
|
initialized = 1;
|
2003-05-29 17:10:45 +00:00
|
|
|
}
|
|
|
|
}
|