2001-11-13 21:55:13 +00:00
|
|
|
/*-
|
2003-06-13 19:39:21 +00:00
|
|
|
* Copyright (c) 2001 Matthew Dillon. All Rights Reserved. Copyright
|
2001-11-13 21:55:13 +00:00
|
|
|
* terms are as specified in the COPYRIGHT file at the base of the source
|
|
|
|
* tree.
|
|
|
|
*
|
|
|
|
* Mutex pool routines. These routines are designed to be used as short
|
|
|
|
* term leaf mutexes (e.g. the last mutex you might aquire other then
|
|
|
|
* calling msleep()). They operate using a shared pool. A mutex is chosen
|
|
|
|
* from the pool based on the supplied pointer (which may or may not be
|
|
|
|
* valid).
|
|
|
|
*
|
|
|
|
* Advantages:
|
|
|
|
* - no structural overhead. Mutexes can be associated with structures
|
|
|
|
* without adding bloat to the structures.
|
|
|
|
* - mutexes can be obtained for invalid pointers, useful when uses
|
|
|
|
* mutexes to interlock destructor ops.
|
2003-06-13 19:39:21 +00:00
|
|
|
* - no initialization/destructor overhead.
|
2001-11-13 21:55:13 +00:00
|
|
|
* - can be used with msleep.
|
|
|
|
*
|
|
|
|
* Disadvantages:
|
2003-06-13 19:39:21 +00:00
|
|
|
* - should generally only be used as leaf mutexes.
|
2001-11-13 21:55:13 +00:00
|
|
|
* - pool/pool dependancy ordering cannot be depended on.
|
2003-06-13 19:39:21 +00:00
|
|
|
* - possible L1 cache mastersip contention between cpus.
|
2001-11-13 21:55:13 +00:00
|
|
|
*/
|
|
|
|
|
2003-06-11 00:56:59 +00:00
|
|
|
#include <sys/cdefs.h>
|
|
|
|
__FBSDID("$FreeBSD$");
|
|
|
|
|
2001-11-13 21:55:13 +00:00
|
|
|
#include <sys/param.h>
|
|
|
|
#include <sys/proc.h>
|
|
|
|
#include <sys/kernel.h>
|
|
|
|
#include <sys/ktr.h>
|
|
|
|
#include <sys/lock.h>
|
|
|
|
#include <sys/malloc.h>
|
|
|
|
#include <sys/mutex.h>
|
|
|
|
#include <sys/systm.h>
|
|
|
|
|
|
|
|
#ifndef MTX_POOL_SIZE
|
|
|
|
#define MTX_POOL_SIZE 128
|
|
|
|
#endif
|
2003-06-13 19:39:21 +00:00
|
|
|
#define MTX_POOL_MASK (MTX_POOL_SIZE - 1)
|
2001-11-13 21:55:13 +00:00
|
|
|
|
|
|
|
static struct mtx mtx_pool_ary[MTX_POOL_SIZE];
|
|
|
|
|
|
|
|
int mtx_pool_valid = 0;
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Inline version of mtx_pool_find(), used to streamline our main API
|
|
|
|
* function calls.
|
|
|
|
*/
|
2003-06-13 19:39:21 +00:00
|
|
|
static __inline struct mtx *
|
2001-11-13 21:55:13 +00:00
|
|
|
_mtx_pool_find(void *ptr)
|
|
|
|
{
|
2001-11-19 00:20:36 +00:00
|
|
|
int p;
|
2001-11-17 00:42:02 +00:00
|
|
|
|
2001-11-19 00:20:36 +00:00
|
|
|
p = (int)(uintptr_t)ptr;
|
2003-06-13 19:39:21 +00:00
|
|
|
return (&mtx_pool_ary[(p ^ (p >> 6)) & MTX_POOL_MASK]);
|
2001-11-13 21:55:13 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
static void
|
|
|
|
mtx_pool_setup(void *dummy __unused)
|
|
|
|
{
|
|
|
|
int i;
|
|
|
|
|
|
|
|
for (i = 0; i < MTX_POOL_SIZE; ++i)
|
2003-06-13 19:39:21 +00:00
|
|
|
mtx_init(&mtx_pool_ary[i], "pool mutex", NULL,
|
|
|
|
MTX_DEF | MTX_NOWITNESS | MTX_QUIET);
|
2001-11-13 21:55:13 +00:00
|
|
|
mtx_pool_valid = 1;
|
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Obtain a (shared) mutex from the pool. The returned mutex is a leaf
|
|
|
|
* level mutex, meaning that if you obtain it you cannot obtain any other
|
|
|
|
* mutexes until you release it. You can legally msleep() on the mutex.
|
|
|
|
*/
|
|
|
|
struct mtx *
|
|
|
|
mtx_pool_alloc(void)
|
|
|
|
{
|
|
|
|
static int si;
|
2003-06-13 19:39:21 +00:00
|
|
|
|
|
|
|
return (&mtx_pool_ary[si++ & MTX_POOL_MASK]);
|
2001-11-13 21:55:13 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Return the (shared) pool mutex associated with the specified address.
|
|
|
|
* The returned mutex is a leaf level mutex, meaning that if you obtain it
|
|
|
|
* you cannot obtain any other mutexes until you release it. You can
|
|
|
|
* legally msleep() on the mutex.
|
|
|
|
*/
|
|
|
|
struct mtx *
|
|
|
|
mtx_pool_find(void *ptr)
|
|
|
|
{
|
2003-06-13 19:39:21 +00:00
|
|
|
|
|
|
|
return (_mtx_pool_find(ptr));
|
2001-11-13 21:55:13 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Combined find/lock operation. Lock the pool mutex associated with
|
|
|
|
* the specified address.
|
|
|
|
*/
|
2003-06-13 19:39:21 +00:00
|
|
|
void
|
2001-11-13 21:55:13 +00:00
|
|
|
mtx_pool_lock(void *ptr)
|
|
|
|
{
|
2003-06-13 19:39:21 +00:00
|
|
|
|
2001-11-13 21:55:13 +00:00
|
|
|
mtx_lock(_mtx_pool_find(ptr));
|
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Combined find/unlock operation. Unlock the pool mutex associated with
|
|
|
|
* the specified address.
|
|
|
|
*/
|
|
|
|
void
|
|
|
|
mtx_pool_unlock(void *ptr)
|
|
|
|
{
|
2003-06-13 19:39:21 +00:00
|
|
|
|
2001-11-13 21:55:13 +00:00
|
|
|
mtx_unlock(_mtx_pool_find(ptr));
|
|
|
|
}
|
|
|
|
|
2003-06-13 19:39:21 +00:00
|
|
|
SYSINIT(mtxpooli, SI_SUB_MTX_POOL, SI_ORDER_FIRST, mtx_pool_setup, NULL);
|