kern_mtxpool.c revision 116182
1/*-
2 * Copyright (c) 2001 Matthew Dillon.  All Rights Reserved.  Copyright
3 * terms are as specified in the COPYRIGHT file at the base of the source
4 * tree.
5 *
6 * Mutex pool routines.  These routines are designed to be used as short
7 * term leaf mutexes (e.g. the last mutex you might aquire other then
8 * calling msleep()).  They operate using a shared pool.  A mutex is chosen
9 * from the pool based on the supplied pointer (which may or may not be
10 * valid).
11 *
12 * Advantages:
13 *	- no structural overhead.  Mutexes can be associated with structures
14 *	  without adding bloat to the structures.
15 *	- mutexes can be obtained for invalid pointers, useful when uses
16 *	  mutexes to interlock destructor ops.
17 *	- no initialization/destructor overhead
18 *	- can be used with msleep.
19 *
20 * Disadvantages:
21 *	- should generally only be used as leaf mutexes
22 *	- pool/pool dependancy ordering cannot be depended on.
23 *	- possible L1 cache mastersip contention between cpus
24 */
25
26#include <sys/cdefs.h>
27__FBSDID("$FreeBSD: head/sys/kern/kern_mtxpool.c 116182 2003-06-11 00:56:59Z obrien $");
28
29#include <sys/param.h>
30#include <sys/proc.h>
31#include <sys/kernel.h>
32#include <sys/ktr.h>
33#include <sys/lock.h>
34#include <sys/malloc.h>
35#include <sys/mutex.h>
36#include <sys/systm.h>
37
38#ifndef MTX_POOL_SIZE
39#define MTX_POOL_SIZE	128
40#endif
41#define MTX_POOL_MASK	(MTX_POOL_SIZE-1)
42
43static struct mtx mtx_pool_ary[MTX_POOL_SIZE];
44
45int mtx_pool_valid = 0;
46
47/*
48 * Inline version of mtx_pool_find(), used to streamline our main API
49 * function calls.
50 */
51static __inline
52struct mtx *
53_mtx_pool_find(void *ptr)
54{
55    int p;
56
57    p = (int)(uintptr_t)ptr;
58    return(&mtx_pool_ary[(p ^ (p >> 6)) & MTX_POOL_MASK]);
59}
60
61static void
62mtx_pool_setup(void *dummy __unused)
63{
64    int i;
65
66    for (i = 0; i < MTX_POOL_SIZE; ++i)
67	mtx_init(&mtx_pool_ary[i], "pool mutex", NULL, MTX_DEF | MTX_NOWITNESS | MTX_QUIET);
68    mtx_pool_valid = 1;
69}
70
71/*
72 * Obtain a (shared) mutex from the pool.  The returned mutex is a leaf
73 * level mutex, meaning that if you obtain it you cannot obtain any other
74 * mutexes until you release it.  You can legally msleep() on the mutex.
75 */
76struct mtx *
77mtx_pool_alloc(void)
78{
79    static int si;
80    return(&mtx_pool_ary[si++ & MTX_POOL_MASK]);
81}
82
83/*
84 * Return the (shared) pool mutex associated with the specified address.
85 * The returned mutex is a leaf level mutex, meaning that if you obtain it
86 * you cannot obtain any other mutexes until you release it.  You can
87 * legally msleep() on the mutex.
88 */
89struct mtx *
90mtx_pool_find(void *ptr)
91{
92    return(_mtx_pool_find(ptr));
93}
94
95/*
96 * Combined find/lock operation.  Lock the pool mutex associated with
97 * the specified address.
98 */
99void
100mtx_pool_lock(void *ptr)
101{
102    mtx_lock(_mtx_pool_find(ptr));
103}
104
105/*
106 * Combined find/unlock operation.  Unlock the pool mutex associated with
107 * the specified address.
108 */
109void
110mtx_pool_unlock(void *ptr)
111{
112    mtx_unlock(_mtx_pool_find(ptr));
113}
114
115SYSINIT(mtxpooli, SI_SUB_MTX_POOL, SI_ORDER_FIRST, mtx_pool_setup, NULL)
116
117