1.\" $NetBSD: membar_ops.3,v 1.4 2015/01/08 22:27:17 riastradh Exp $ 2.\" 3.\" Copyright (c) 2007, 2008 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 November 20, 2014 31.Dt MEMBAR_OPS 3 32.Os 33.Sh NAME 34.Nm membar_ops , 35.Nm membar_enter , 36.Nm membar_exit , 37.Nm membar_producer , 38.Nm membar_consumer , 39.Nm membar_sync 40.Nd memory access barrier operations 41.\" .Sh LIBRARY 42.\" .Lb libc 43.Sh SYNOPSIS 44.In sys/atomic.h 45.\" 46.Ft void 47.Fn membar_enter "void" 48.Ft void 49.Fn membar_exit "void" 50.Ft void 51.Fn membar_producer "void" 52.Ft void 53.Fn membar_consumer "void" 54.Ft void 55.Fn membar_datadep_consumer "void" 56.Ft void 57.Fn membar_sync "void" 58.Sh DESCRIPTION 59The 60.Nm membar_ops 61family of functions provide memory access barrier operations necessary 62for synchronization in multiprocessor execution environments that have 63relaxed load and store order. 64.Pp 65.Bl -tag -width "mem" 66.It Fn membar_enter 67Any store preceding 68.Fn membar_enter 69will reach global visibility before all loads and stores following it. 70.Pp 71.Fn membar_enter 72is typically used in code that implements locking primitives to ensure 73that a lock protects its data. 74.It Fn membar_exit 75All loads and stores preceding 76.Fn membar_exit 77will reach global visibility before any store that follows it. 78.Pp 79.Fn membar_exit 80is typically used in code that implements locking primitives to ensure 81that a lock protects its data. 82.It Fn membar_producer 83All stores preceding the memory barrier will reach global visibility 84before any stores after the memory barrier reach global visibility. 85.It Fn membar_consumer 86All loads preceding the memory barrier will complete before any loads 87after the memory barrier complete. 88.It Fn membar_datadep_consumer 89Same as 90.Fn membar_consumer , 91but limited to loads of addresses dependent on prior loads, or 92.Sq data-dependent 93loads: 94.Bd -literal -offset indent 95int **pp, *p, v; 96 97p = *pp; 98membar_datadep_consumer(); 99v = *p; 100consume(v); 101.Ed 102.Pp 103Does not guarantee ordering of loads in branches, or 104.Sq control-dependent 105loads -- you must use 106.Fn membar_consumer 107instead: 108.Bd -literal -offset indent 109int *ok, *p, v; 110 111if (*ok) { 112 membar_consumer(); 113 v = *p; 114 consume(v); 115} 116.Ed 117.Pp 118Most CPUs do not reorder data-dependent loads (i.e., most CPUs 119guarantee that cached values are not stale in that case), so 120.Fn membar_datadep_consumer 121is a no-op on those CPUs. 122.It Fn membar_sync 123All loads and stores preceding the memory barrier will complete and 124reach global visibility before any loads and stores after the memory 125barrier complete and reach global visibility. 126.El 127.Sh SEE ALSO 128.Xr atomic_ops 3 129.Sh HISTORY 130The 131.Nm membar_ops 132functions first appeared in 133.Nx 5.0 . 134The data-dependent load barrier, 135.Fn membar_datadep_consumer , 136first appeared in 137.Nx 7.0 . 138