View | Details | Raw Unified | Return to bug 36350
Collapse All | Expand All

(-)mtx_pool.9 (+134 lines)
Added Link Here
1
.\"
2
.\" Copyright (C) 2002 Garrett Rooney <rooneg@electricjellyfish.net>. All 
3
.\" rights reserved.
4
.\"
5
.\" Redistribution and use in source and binary forms, with or without
6
.\" modification, are permitted provided that the following conditions
7
.\" are met:
8
.\" 1. Redistributions of source code must retain the above copyright
9
.\"    notice(s), this list of conditions and the following disclaimer as
10
.\"    the first lines of this file unmodified other than the possible
11
.\"    addition of one or more copyright notices.
12
.\" 2. Redistributions in binary form must reproduce the above copyright
13
.\"    notice(s), this list of conditions and the following disclaimer in the
14
.\"    documentation and/or other materials provided with the distribution.
15
.\"
16
.\" THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDER(S) ``AS IS'' AND ANY
17
.\" EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
18
.\" WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
19
.\" DISCLAIMED.  IN NO EVENT SHALL THE COPYRIGHT HOLDER(S) BE LIABLE FOR ANY
20
.\" DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
21
.\" (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
22
.\" SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
23
.\" CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
24
.\" LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
25
.\" OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH
26
.\" DAMAGE.
27
.\"
28
.\" $FreeBSD$
29
.\"
30
.Dd March 25, 2002
31
.Dt MTX_POOL 9
32
.Os
33
.Sh NAME
34
.Nm mtx_pool ,
35
.Nm mtx_pool_alloc ,
36
.Nm mtx_pool_find ,
37
.Nm mtx_lock_lock ,
38
.Nm mtx_lock_unlock
39
.Nd mutex pool routines
40
.Sh SYNOPSIS
41
.In sys/mutex.h
42
.Ft struct mtx *
43
.Fn mtx_pool_alloc "void"
44
.Ft struct mtx *
45
.Fn mtx_pool_find "void *ptr"
46
.Ft void
47
.Fn mtx_pool_lock "void *ptr"
48
.Ft void
49
.Fn mtx_pool_unlock "void *ptr"
50
.Sh DESCRIPTION
51
Mutex pools are designed to be used as short term leaf mutexes, e.g. the 
52
last mutex you might acquire other than calling
53
.Fn msleep .
54
They operate using a shared pool of mutexes.
55
A mutex is chosen from the pool based on the supplied pointer (which may or 
56
may not be valid).
57
.Pp
58
The shared mutex managed by the pool module are standard, non-recursive, 
59
blockable mutexes, and should only be used in appropriate situations.
60
.Pp
61
The caller can lock and unlock mutexes returned by the pool routines, but 
62
since the mutexes are shared, the caller should not attempt to destroy them 
63
or modify their characteristics.
64
While pool mutexes are normally 'leaf' mutexes, meaning that you cannot 
65
depend on any ordering guarantees after obtaining one, you can still obtain 
66
other mutexes under carefully controlled circumstances.
67
Specifically, if you have a private mutex (that you allocate and 
68
.Fn mtx_init
69
yourself), and you carefully manage the ordering issues, you can obtain your 
70
private mutex after obtaining the pool mutex.
71
In these cases the private mute winds up being the true 'leaf' mutex.
72
.Pp
73
Mutex pools have the following advantages:
74
.Bl -enum
75
.It
76
No structural overhead.
77
Mutexes can be associated with structures without adding bloat to the structures.
78
.It
79
Mutexes can be obtained for invalid pointers, which is useful when one uses 
80
mutexes to interlock destructor operations.
81
.It
82
No initialization/destruction overhead.
83
.It
84
Can be used with msleep.
85
.El
86
.Pp
87
And the following disadvantages:
88
.Bl -enum
89
.It
90
Should generally only be used as leaf mutexes.
91
.It
92
Pool/pool dependency ordering cannot be depended on.
93
.It
94
Possible L1 cache mastership contention between CPUs.
95
.El
96
.Pp
97
.Fn mtx_pool_alloc
98
obtains a shared mutex from the pool.
99
This routine uses a simple rover to choose one of the shared mutexes managed 
100
by the module.
101
.Pp
102
.Fn mtx_pool_find
103
returns the shared mutex associated with the specified address.
104
This routine will create a hash out of the arbitrary pointer passed into it
105
and will choose a shared mutex based on the hash.
106
The pointer need not point to anything real.
107
.Pp
108
.Fn mtx_pool_lock
109
locks the shared mutex associated with the specified address.
110
This routine provides an easy-to-use combination of
111
.Fn mtx_pool_find
112
and
113
.Fn mtx_pool_lock .
114
Note that because it must hash the pointer passed to it, this will not be 
115
as fast as storing the pointer returned by a previous
116
.Fn mtx_pool_find .
117
.Pp
118
.Fn mtx_pool_unlock
119
unlocks the shared mutex associated with the specified address.
120
This routine provides an easy-to-use combination of 
121
.Fn mtx_pool_find
122
and
123
.Fn mtx_pool_unlock .
124
Note that because it must hash the pointer passed to it, this will not be 
125
as fast as storing the pointer returned by a previous 
126
.Fn mtx_pool_find .
127
.Pp
128
.Sh SEE ALSO
129
.Xr mutex 9 ,
130
.Xr msleep 9
131
.Sh HISTORY
132
These
133
functions appeared in
134
.Fx 5.0 .
(-)Makefile (-1 / +4 lines)
Lines 49-55 Link Here
49
	lock.9 \
49
	lock.9 \
50
	make_dev.9 malloc.9 mbchain.9 mbuf.9 mdchain.9 \
50
	make_dev.9 malloc.9 mbchain.9 mbuf.9 mdchain.9 \
51
	mi_switch.9 microseq.9 microtime.9 microuptime.9 \
51
	mi_switch.9 microseq.9 microtime.9 microuptime.9 \
52
	module.9 mutex.9 \
52
	module.9 mtx_pool.9 mutex.9 \
53
	namei.9 \
53
	namei.9 \
54
	panic.9 pbuf.9 pfil.9 pfind.9 pgfind.9 \
54
	panic.9 pbuf.9 pfil.9 pfind.9 pgfind.9 \
55
	physio.9 posix4.9 printf.9 pseudofs.9 psignal.9 \
55
	physio.9 posix4.9 printf.9 pseudofs.9 psignal.9 \
Lines 309-314 Link Here
309
MLINKS+=microtime.9 getnanotime.9
309
MLINKS+=microtime.9 getnanotime.9
310
MLINKS+=microuptime.9 getmicrouptime.9 microuptime.9 nanouptime.9
310
MLINKS+=microuptime.9 getmicrouptime.9 microuptime.9 nanouptime.9
311
MLINKS+=microuptime.9 getnanouptime.9
311
MLINKS+=microuptime.9 getnanouptime.9
312
313
MLINKS+=mtx_pool.9 mtx_pool_alloc.9 mtx_pool.9 mtx_pool_find.9
314
MLINKS+=mtx_pool.9 mtx_pool_lock.9 mtx_pool.9 mtx_pool_unlock.9
312
315
313
MLINKS+=mutex.9 mtx_init.9
316
MLINKS+=mutex.9 mtx_init.9
314
MLINKS+=mutex.9 mtx_lock.9 mutex.9 mtx_lock_flags.9
317
MLINKS+=mutex.9 mtx_lock.9 mutex.9 mtx_lock_flags.9
(-)condvar.9 (+1 lines)
Lines 195-200 Link Here
195
.El
195
.El
196
.Sh SEE ALSO
196
.Sh SEE ALSO
197
.Xr msleep 9 ,
197
.Xr msleep 9 ,
198
.Xr mtx_pool 9 ,
198
.Xr mutex 9 ,
199
.Xr mutex 9 ,
199
.Xr sema 9 ,
200
.Xr sema 9 ,
200
.Xr sx 9
201
.Xr sx 9
(-)mutex.9 (+1 lines)
Lines 465-470 Link Here
465
.Sh SEE ALSO
465
.Sh SEE ALSO
466
.Xr condvar 9 ,
466
.Xr condvar 9 ,
467
.Xr msleep 9 ,
467
.Xr msleep 9 ,
468
.Xr mtx_pool 9 ,
468
.Xr sema 9 ,
469
.Xr sema 9 ,
469
.Xr sx 9
470
.Xr sx 9
470
.Sh HISTORY
471
.Sh HISTORY
(-)sema.9 (+1 lines)
Lines 107-111 Link Here
107
will be returned to indicate success.
107
will be returned to indicate success.
108
.Sh SEE ALSO
108
.Sh SEE ALSO
109
.Xr condvar 9 ,
109
.Xr condvar 9 ,
110
.Xr mtx_pool 9 ,
110
.Xr mutex 9 ,
111
.Xr mutex 9 ,
111
.Xr sx 9
112
.Xr sx 9
(-)sx.9 (+1 lines)
Lines 146-151 Link Here
146
attempting to do so will result in deadlock.
146
attempting to do so will result in deadlock.
147
.Sh SEE ALSO
147
.Sh SEE ALSO
148
.Xr condvar 9 ,
148
.Xr condvar 9 ,
149
.Xr mtx_pool 9 ,
149
.Xr mutex 9 ,
150
.Xr mutex 9 ,
150
.Xr sema 9
151
.Xr sema 9
151
.Sh BUGS
152
.Sh BUGS

Return to bug 36350