xref: /netbsd-src/lib/libc/atomic/atomic_cas.3 (revision a5847cc334d9a7029f6352b847e9e8d71a0f9e0c)
1.\"	$NetBSD: atomic_cas.3,v 1.2 2010/02/12 22:23:17 dyoung Exp $
2.\"
3.\" Copyright (c) 2007, 2010 The NetBSD Foundation, Inc.
4.\" All rights reserved.
5.\"
6.\" This code is derived from software contributed to The NetBSD Foundation
7.\" by Jason R. Thorpe.
8.\"
9.\" Redistribution and use in source and binary forms, with or without
10.\" modification, are permitted provided that the following conditions
11.\" are met:
12.\" 1. Redistributions of source code must retain the above copyright
13.\" notice, this list of conditions and the following disclaimer.
14.\" 2. Redistributions in binary form must reproduce the above copyright
15.\" notice, this list of conditions and the following disclaimer in the
16.\" documentation and/or other materials provided with the distribution.
17.\"
18.\" THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
19.\" ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
20.\" TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
21.\" PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
22.\" BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
23.\" CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
24.\" SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
25.\" INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
26.\" CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
27.\" ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
28.\" POSSIBILITY OF SUCH DAMAGE.
29.\"
30.Dd February 11, 2010
31.Dt ATOMIC_CAS 3
32.Os
33.Sh NAME
34.Nm atomic_cas ,
35.Nm atomic_cas_32 ,
36.Nm atomic_cas_uint ,
37.Nm atomic_cas_ulong ,
38.Nm atomic_cas_ptr ,
39.Nm atomic_cas_64 ,
40.Nm atomic_cas_32_ni ,
41.Nm atomic_cas_uint_ni ,
42.Nm atomic_cas_ulong_ni ,
43.Nm atomic_cas_ptr_ni ,
44.Nm atomic_cas_64_ni
45.Nd atomic compare-and-swap operations
46.\" .Sh LIBRARY
47.\" .Lb libc
48.Sh SYNOPSIS
49.In sys/atomic.h
50.Ft uint32_t
51.Fn atomic_cas_32 "volatile uint32_t *ptr" "uint32_t old" "uint32_t new"
52.Ft unsigned int
53.Fn atomic_cas_uint "volatile unsigned int *ptr" "unsigned int old" \
54    "unsigned int new"
55.Ft unsigned long
56.Fn atomic_cas_ulong "volatile unsigned long *ptr" "unsigned long old" \
57    "unsigned long new"
58.Ft void *
59.Fn atomic_cas_ptr "volatile void *ptr" "void *old" "void *new"
60.Ft uint64_t
61.Fn atomic_cas_64 "volatile uint64_t *ptr" "uint64_t old" "uint64_t new"
62.Ft uint32_t
63.Fn atomic_cas_32_ni "volatile uint32_t *ptr" "uint32_t old" "uint32_t new"
64.Ft unsigned int
65.Fn atomic_cas_uint_ni "volatile unsigned int *ptr" "unsigned int old" \
66    "unsigned int new"
67.Ft unsigned long
68.Fn atomic_cas_ulong_ni "volatile unsigned long *ptr" "unsigned long old" \
69    "unsigned long new"
70.Ft void *
71.Fn atomic_cas_ptr_ni "volatile void *ptr" "void *old" "void *new"
72.Ft uint64_t
73.Fn atomic_cas_64_ni "volatile uint64_t *ptr" "uint64_t old" "uint64_t new"
74.Sh DESCRIPTION
75The
76.Nm atomic_cas
77family of functions perform a compare-and-swap operation in an atomic fashion.
78The value of the variable referenced by
79.Fa ptr
80is compared against
81.Fa old .
82If the values are equal,
83.Fa new
84is stored in the variable referenced by
85.Fa ptr .
86.Pp
87The old value of the variable referenced by
88.Fa ptr
89is always returned regardless of whether or not the new value was stored.
90Applications can test for success of the operation by comparing the
91return value to the value passed as
92.Fa old ;
93if they are equal then the new value was stored.
94.Pp
95The non-interlocked variants,
96.Fn *_ni ,
97guarantee atomicity within the same CPU with respect to
98interrupts and preemption.
99For example, they are suitable for synchronizing compare-and-swap
100operations on a variable shared by a thread and an interrupt
101that are bound to the same CPU.
102The
103.Fn *_ni
104variants are not atomic with respect to different CPUs.
105.Fn *_ni
106variants should avoid the interprocessor synchronization overhead
107of the standard compare-and-swap operations.
108.Pp
109The 64-bit variants of these functions are available only on platforms
110that can support atomic 64-bit memory access.
111Applications can check for the availability of 64-bit atomic memory
112operations by testing if the pre-processor macro
113.Dv __HAVE_ATOMIC64_OPS
114is defined.
115.Sh SEE ALSO
116.Xr atomic_ops 3
117.Sh HISTORY
118The
119.Nm atomic_cas
120functions first appeared in
121.Nx 5.0 .
122.Sh NOTES
123On some architectures, a
124.Fn *_ni
125variant is merely an alias for the corresponding standard
126compare-and-swap operation.
127While the non-interlocked variant behaves correctly on those
128architectures, it does not avoid the interprocessor synchronization
129overhead.
130