1*0a6a1f1dSLionel Sambuc.\" $NetBSD: membar_ops.3,v 1.4 2015/01/08 22:27:17 riastradh Exp $ 22fe8fb19SBen Gras.\" 32fe8fb19SBen Gras.\" Copyright (c) 2007, 2008 The NetBSD Foundation, Inc. 42fe8fb19SBen Gras.\" All rights reserved. 52fe8fb19SBen Gras.\" 62fe8fb19SBen Gras.\" This code is derived from software contributed to The NetBSD Foundation 72fe8fb19SBen Gras.\" by Jason R. Thorpe. 82fe8fb19SBen Gras.\" 92fe8fb19SBen Gras.\" Redistribution and use in source and binary forms, with or without 102fe8fb19SBen Gras.\" modification, are permitted provided that the following conditions 112fe8fb19SBen Gras.\" are met: 122fe8fb19SBen Gras.\" 1. Redistributions of source code must retain the above copyright 132fe8fb19SBen Gras.\" notice, this list of conditions and the following disclaimer. 142fe8fb19SBen Gras.\" 2. Redistributions in binary form must reproduce the above copyright 152fe8fb19SBen Gras.\" notice, this list of conditions and the following disclaimer in the 162fe8fb19SBen Gras.\" documentation and/or other materials provided with the distribution. 172fe8fb19SBen Gras.\" 182fe8fb19SBen Gras.\" THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS 192fe8fb19SBen Gras.\" ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED 202fe8fb19SBen Gras.\" TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR 212fe8fb19SBen Gras.\" PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS 222fe8fb19SBen Gras.\" BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR 232fe8fb19SBen Gras.\" CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF 242fe8fb19SBen Gras.\" SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS 252fe8fb19SBen Gras.\" INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN 262fe8fb19SBen Gras.\" CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) 272fe8fb19SBen Gras.\" ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE 282fe8fb19SBen Gras.\" POSSIBILITY OF SUCH DAMAGE. 292fe8fb19SBen Gras.\" 30*0a6a1f1dSLionel Sambuc.Dd November 20, 2014 312fe8fb19SBen Gras.Dt MEMBAR_OPS 3 322fe8fb19SBen Gras.Os 332fe8fb19SBen Gras.Sh NAME 342fe8fb19SBen Gras.Nm membar_ops , 352fe8fb19SBen Gras.Nm membar_enter , 362fe8fb19SBen Gras.Nm membar_exit , 372fe8fb19SBen Gras.Nm membar_producer , 382fe8fb19SBen Gras.Nm membar_consumer , 392fe8fb19SBen Gras.Nm membar_sync 402fe8fb19SBen Gras.Nd memory access barrier operations 412fe8fb19SBen Gras.\" .Sh LIBRARY 422fe8fb19SBen Gras.\" .Lb libc 432fe8fb19SBen Gras.Sh SYNOPSIS 442fe8fb19SBen Gras.In sys/atomic.h 452fe8fb19SBen Gras.\" 462fe8fb19SBen Gras.Ft void 472fe8fb19SBen Gras.Fn membar_enter "void" 482fe8fb19SBen Gras.Ft void 492fe8fb19SBen Gras.Fn membar_exit "void" 502fe8fb19SBen Gras.Ft void 512fe8fb19SBen Gras.Fn membar_producer "void" 522fe8fb19SBen Gras.Ft void 532fe8fb19SBen Gras.Fn membar_consumer "void" 542fe8fb19SBen Gras.Ft void 55*0a6a1f1dSLionel Sambuc.Fn membar_datadep_consumer "void" 56*0a6a1f1dSLionel Sambuc.Ft void 572fe8fb19SBen Gras.Fn membar_sync "void" 582fe8fb19SBen Gras.Sh DESCRIPTION 592fe8fb19SBen GrasThe 602fe8fb19SBen Gras.Nm membar_ops 612fe8fb19SBen Grasfamily of functions provide memory access barrier operations necessary 622fe8fb19SBen Grasfor synchronization in multiprocessor execution environments that have 632fe8fb19SBen Grasrelaxed load and store order. 642fe8fb19SBen Gras.Pp 652fe8fb19SBen Gras.Bl -tag -width "mem" 662fe8fb19SBen Gras.It Fn membar_enter 67f14fb602SLionel SambucAny store preceding 682fe8fb19SBen Gras.Fn membar_enter 692fe8fb19SBen Graswill reach global visibility before all loads and stores following it. 702fe8fb19SBen Gras.Pp 712fe8fb19SBen Gras.Fn membar_enter 722fe8fb19SBen Grasis typically used in code that implements locking primitives to ensure 732fe8fb19SBen Grasthat a lock protects its data. 742fe8fb19SBen Gras.It Fn membar_exit 752fe8fb19SBen GrasAll loads and stores preceding 762fe8fb19SBen Gras.Fn membar_exit 772fe8fb19SBen Graswill reach global visibility before any store that follows it. 782fe8fb19SBen Gras.Pp 792fe8fb19SBen Gras.Fn membar_exit 802fe8fb19SBen Grasis typically used in code that implements locking primitives to ensure 812fe8fb19SBen Grasthat a lock protects its data. 822fe8fb19SBen Gras.It Fn membar_producer 832fe8fb19SBen GrasAll stores preceding the memory barrier will reach global visibility 842fe8fb19SBen Grasbefore any stores after the memory barrier reach global visibility. 852fe8fb19SBen Gras.It Fn membar_consumer 862fe8fb19SBen GrasAll loads preceding the memory barrier will complete before any loads 872fe8fb19SBen Grasafter the memory barrier complete. 88*0a6a1f1dSLionel Sambuc.It Fn membar_datadep_consumer 89*0a6a1f1dSLionel SambucSame as 90*0a6a1f1dSLionel Sambuc.Fn membar_consumer , 91*0a6a1f1dSLionel Sambucbut limited to loads of addresses dependent on prior loads, or 92*0a6a1f1dSLionel Sambuc.Sq data-dependent 93*0a6a1f1dSLionel Sambucloads: 94*0a6a1f1dSLionel Sambuc.Bd -literal -offset indent 95*0a6a1f1dSLionel Sambucint **pp, *p, v; 96*0a6a1f1dSLionel Sambuc 97*0a6a1f1dSLionel Sambucp = *pp; 98*0a6a1f1dSLionel Sambucmembar_datadep_consumer(); 99*0a6a1f1dSLionel Sambucv = *p; 100*0a6a1f1dSLionel Sambucconsume(v); 101*0a6a1f1dSLionel Sambuc.Ed 102*0a6a1f1dSLionel Sambuc.Pp 103*0a6a1f1dSLionel SambucDoes not guarantee ordering of loads in branches, or 104*0a6a1f1dSLionel Sambuc.Sq control-dependent 105*0a6a1f1dSLionel Sambucloads -- you must use 106*0a6a1f1dSLionel Sambuc.Fn membar_consumer 107*0a6a1f1dSLionel Sambucinstead: 108*0a6a1f1dSLionel Sambuc.Bd -literal -offset indent 109*0a6a1f1dSLionel Sambucint *ok, *p, v; 110*0a6a1f1dSLionel Sambuc 111*0a6a1f1dSLionel Sambucif (*ok) { 112*0a6a1f1dSLionel Sambuc membar_consumer(); 113*0a6a1f1dSLionel Sambuc v = *p; 114*0a6a1f1dSLionel Sambuc consume(v); 115*0a6a1f1dSLionel Sambuc} 116*0a6a1f1dSLionel Sambuc.Ed 117*0a6a1f1dSLionel Sambuc.Pp 118*0a6a1f1dSLionel SambucMost CPUs do not reorder data-dependent loads (i.e., most CPUs 119*0a6a1f1dSLionel Sambucguarantee that cached values are not stale in that case), so 120*0a6a1f1dSLionel Sambuc.Fn membar_datadep_consumer 121*0a6a1f1dSLionel Sambucis a no-op on those CPUs. 1222fe8fb19SBen Gras.It Fn membar_sync 1232fe8fb19SBen GrasAll loads and stores preceding the memory barrier will complete and 1242fe8fb19SBen Grasreach global visibility before any loads and stores after the memory 1252fe8fb19SBen Grasbarrier complete and reach global visibility. 1262fe8fb19SBen Gras.El 1272fe8fb19SBen Gras.Sh SEE ALSO 1282fe8fb19SBen Gras.Xr atomic_ops 3 1292fe8fb19SBen Gras.Sh HISTORY 1302fe8fb19SBen GrasThe 1312fe8fb19SBen Gras.Nm membar_ops 1322fe8fb19SBen Grasfunctions first appeared in 1332fe8fb19SBen Gras.Nx 5.0 . 134*0a6a1f1dSLionel SambucThe data-dependent load barrier, 135*0a6a1f1dSLionel Sambuc.Fn membar_datadep_consumer , 136*0a6a1f1dSLionel Sambucfirst appeared in 137*0a6a1f1dSLionel Sambuc.Nx 7.0 . 138