xref: /freebsd/share/man/man9/sx.9 (revision c5e7e03a143ca33bf523994e766d62892780abcf)
1.\"
2.\" Copyright (C) 2001 Jason Evans <jasone@FreeBSD.org>.  All rights reserved.
3.\"
4.\" Redistribution and use in source and binary forms, with or without
5.\" modification, are permitted provided that the following conditions
6.\" are met:
7.\" 1. Redistributions of source code must retain the above copyright
8.\"    notice(s), this list of conditions and the following disclaimer as
9.\"    the first lines of this file unmodified other than the possible
10.\"    addition of one or more copyright notices.
11.\" 2. Redistributions in binary form must reproduce the above copyright
12.\"    notice(s), this list of conditions and the following disclaimer in the
13.\"    documentation and/or other materials provided with the distribution.
14.\"
15.\" THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDER(S) ``AS IS'' AND ANY
16.\" EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
17.\" WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
18.\" DISCLAIMED.  IN NO EVENT SHALL THE COPYRIGHT HOLDER(S) BE LIABLE FOR ANY
19.\" DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
20.\" (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
21.\" SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
22.\" CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
23.\" LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
24.\" OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH
25.\" DAMAGE.
26.\"
27.\" $FreeBSD$
28.\"
29.Dd February 26, 2001
30.Dt SX 9
31.Os
32.Sh NAME
33.Nm sx ,
34.Nm sx_init ,
35.Nm sx_destroy ,
36.Nm sx_slock ,
37.Nm sx_xlock ,
38.Nm sx_try_slock ,
39.Nm sx_try_xlock ,
40.Nm sx_sunlock ,
41.Nm sx_xunlock
42.Nd kernel shared/exclusive lock
43.Sh SYNOPSIS
44.Fd #include <sys/types.h>
45.Fd #include <sys/lock.h>
46.Fd #include <sys/mutex.h>
47.Fd #include <sys/sx.h>
48.Ft void
49.Fn sx_init "struct sx *sx" "const char *description"
50.Ft void
51.Fn sx_destroy "struct sx *sx"
52.Ft void
53.Fn sx_slock "struct sx *sx"
54.Ft void
55.Fn sx_xlock "struct sx *sx"
56.Ft int
57.Fn sx_try_slock "struct sx *sx"
58.Ft int
59.Fn sx_try_xlock "struct sx *sx"
60.Ft void
61.Fn sx_sunlock "struct sx *sx"
62.Ft void
63.Fn sx_xunlock "struct sx *sx"
64.Sh DESCRIPTION
65Shared/exclusive locks are used to protect data that are read far more often
66than they are written.
67Mutexes are inherently more efficient than shared/exclusive locks, so
68shared/exclusive locks should be used prudently.
69.Pp
70Shared/exclusive locks are created with
71.Fn sx_init ,
72where
73.Fa sx
74is a pointer to space for a
75.Vt struct sx ,
76and
77.Fa description
78is a pointer to a null-terminated character string that describes the
79shared/exclusive lock.
80Shared/exclusive locks are destroyed with
81.Fn sx_destroy .
82Threads acquire and release a shared lock by calling
83.Fn sx_slock
84or
85.Fn sx_try_slock
86and
87.Fn sx_sunlock .
88Threads acquire and release an exclusive lock by calling
89.Fn sx_xlock
90or
91.Fn sx_try_xlock
92and
93.Fn sx_xunlock .
94.Pp
95.Fn sx_try_slock
96and
97.Fn sx_try_xlock
98will return 0 if the shared/exclusive lock cannot be acquired immediately;
99otherwise the shared/exclusive lock will be acquired and a non-zero value will
100be returned.
101.Pp
102A thread may not own a shared lock and an exclusive lock simultaneously;
103attempting to do so will result in deadlock.
104.Sh SEE ALSO
105.Xr mutex 9
106