xref: /openbsd-src/share/man/man9/mutex.9 (revision 2b0358df1d88d06ef4139321dd05bd5e05d91eaf)
1.\" $OpenBSD: mutex.9,v 1.7 2007/12/09 20:54:01 jmc Exp $
2.\"
3.\" Copyright (c) 2005 Pedro Martelletto <pedro@ambientworks.net>
4.\" All rights reserved.
5.\"
6.\" Permission to use, copy, modify, and distribute this software for any
7.\" purpose with or without fee is hereby granted, provided that the above
8.\" copyright notice and this permission notice appear in all copies.
9.\"
10.\" THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
11.\" WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
12.\" MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
13.\" ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
14.\" WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
15.\" ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
16.\" OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
17.\"
18.Dd $Mdocdate: December 9 2007 $
19.Dt MUTEX 9
20.Os
21.Sh NAME
22.Nm mutex ,
23.Nm mtx_init ,
24.Nm mtx_enter ,
25.Nm mtx_leave
26.Nd interface to CPU mutexes
27.Sh SYNOPSIS
28.Fd #include <sys/mutex.h>
29.Ft void
30.Fn mtx_init "struct mutex *mtxp" "int wantipl"
31.Ft void
32.Fn mtx_enter "struct mutex *mtxp"
33.Ft void
34.Fn mtx_leave "struct mutex *mtxp"
35.Sh DESCRIPTION
36The
37.Nm
38set of functions provides a non-recursive, interrupt-aware spinning mechanism
39to ensure mutual exclusion between different CPUs.
40.Pp
41The
42.Fn mtx_init
43function is used to initiate the mutex pointed to by
44.Fa mtxp .
45When acquired, the mutex will cause the processor interrupt level to be raised
46to
47.Fa wantipl
48if necessary.
49.Pp
50The
51.Fn mtx_enter
52function acquires a mutex, spinning if necessary.
53.Pp
54The
55.Fn mtx_leave
56function releases a mutex.
57In case the acquisition of the mutex caused the interrupt level to be changed,
58it is then restored.
59.Sh SEE ALSO
60.Xr lockmgr 9 ,
61.Xr rwlock 9 ,
62.Xr spl 9
63.Sh HISTORY
64The
65.Nm
66functions first appeared in
67.Ox 3.6 .
68.Sh AUTHORS
69The
70.Nm
71functions were written by
72.An Artur Grabowski
73.Aq art@openbsd.org .
74.Sh CAVEATS
75As these are spinning locks, don't sleep while holding one.
76.Pp
77If using a mutex in an interrupt handler, locks must be initialised
78with the correct IPL or deadlock will occur.
79.Pp
80Multiple mutexes may be nested, but not interleaved.
81This is okay:
82.Bd -literal -offset indent
83mtx_enter(foo);
84mtx_enter(bar);
85mtx_leave(bar);
86mtx_leave(foo);
87.Ed
88.Pp
89While this is
90.Fa not :
91.Bd -literal -offset indent
92mtx_enter(foo);
93mtx_enter(bar);
94mtx_leave(foo);
95mtx_leave(bar);
96.Ed
97