1.\" $NetBSD: atomic_cas.3,v 1.5 2014/02/02 18:06:33 dholland 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 2, 2014 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 expected" "uint32_t new" 52.Ft unsigned int 53.Fn atomic_cas_uint "volatile unsigned int *ptr" "unsigned int expected" \ 54 "unsigned int new" 55.Ft unsigned long 56.Fn atomic_cas_ulong "volatile unsigned long *ptr" "unsigned long expected" \ 57 "unsigned long new" 58.Ft void * 59.Fn atomic_cas_ptr "volatile void *ptr" "void *expected" "void *new" 60.Ft uint64_t 61.Fn atomic_cas_64 "volatile uint64_t *ptr" "uint64_t expected" "uint64_t new" 62.Ft uint32_t 63.Fn atomic_cas_32_ni "volatile uint32_t *ptr" "uint32_t expected" \ 64 "uint32_t new" 65.Ft unsigned int 66.Fn atomic_cas_uint_ni "volatile unsigned int *ptr" "unsigned int expected" \ 67 "unsigned int new" 68.Ft unsigned long 69.Fn atomic_cas_ulong_ni "volatile unsigned long *ptr" \ 70 "unsigned long expected" "unsigned long new" 71.Ft void * 72.Fn atomic_cas_ptr_ni "volatile void *ptr" "void *expected" "void *new" 73.Ft uint64_t 74.Fn atomic_cas_64_ni "volatile uint64_t *ptr" "uint64_t expected" \ 75 "uint64_t new" 76.Sh DESCRIPTION 77The 78.Nm atomic_cas 79family of functions perform an atomic conditional assignment. 80The value 81.Fa new 82is assigned to the variable referenced by 83.Fa ptr . 84The assignment succeeds 85if and only if its current value matches the value 86.Fa expected . 87If the value is different, the assignment fails and no change is 88made. 89This operation is sometimes known as 90.Dq compare-and-swap . 91These functions always return the value found via 92.Fa ptr . 93Callers test for success by comparing the return value to the value 94passed as 95.Fa expected ; 96if they are equal then the new value was stored; if they are not, the 97value was not changed. 98.Pp 99The non-interlocked variants, 100.Fn *_ni , 101guarantee atomicity within the same CPU with respect to 102interrupts and preemption. 103They are not atomic with respect to different CPUs. 104These can be used to avoid interprocessor synchronization overhead 105in some cases; for example, they are suitable for synchronized 106operations on a variable shared by a thread and an interrupt that are 107bound to the same CPU. 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