xref: /netbsd-src/lib/libc/atomic/atomic_cas.3 (revision b78992537496bc71ee3d761f9fe0be0fc0a9a001)
1.\"	$NetBSD: atomic_cas.3,v 1.1 2008/06/23 10:22:40 ad Exp $
2.\"
3.\" Copyright (c) 2007 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 April 11, 2007
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.Nd atomic compare-and-swap operations
41.\" .Sh LIBRARY
42.\" .Lb libc
43.Sh SYNOPSIS
44.In sys/atomic.h
45.Ft uint32_t
46.Fn atomic_cas_32 "volatile uint32_t *ptr" "uint32_t old" "uint32_t new"
47.Ft unsigned int
48.Fn atomic_cas_uint "volatile unsigned int *ptr" "unsigned int old" \
49    "unsigned int new"
50.Ft unsigned long
51.Fn atomic_cas_ulong "volatile unsigned long *ptr" "unsigned long old" \
52    "unsigned long new"
53.Ft void *
54.Fn atomic_cas_ptr "volatile void *ptr" "void *old" "void *new"
55.Ft uint64_t
56.Fn atomic_cas_64 "volatile uint64_t *ptr" "uint64_t old" "uint64_t new"
57.Sh DESCRIPTION
58The
59.Nm atomic_cas
60family of functions perform a compare-and-swap operation in an atomic fashion.
61The value of the variable referenced by
62.Fa ptr
63is compared against
64.Fa old .
65If the values are equal,
66.Fa new
67is stored in the variable referenced by
68.Fa ptr .
69.Pp
70The old value of the variable referenced by
71.Fa ptr
72is always returned regardless of whether or not the new value was stored.
73Applications can test for success of the operation by comparing the
74return value to the value passed as
75.Fa old ;
76if they are equal then the new value was stored.
77.Pp
78The 64-bit variants of these functions are available only on platforms
79that can support atomic 64-bit memory access.
80Applications can check for the availability of 64-bit atomic memory
81operations by testing if the pre-processor macro
82.Dv __HAVE_ATOMIC64_OPS
83is defined.
84.Sh SEE ALSO
85.Xr atomic_ops 3
86.Sh HISTORY
87The
88.Nm atomic_cas
89functions first appeared in
90.Nx 5.0 .
91