PortAudio 2.0
pa_memorybarrier.h
Go to the documentation of this file.
1/*
2 * $Id: pa_memorybarrier.h 1240 2007-07-17 13:05:07Z bjornroche $
3 * Portable Audio I/O Library
4 * Memory barrier utilities
5 *
6 * Author: Bjorn Roche, XO Audio, LLC
7 *
8 * This program uses the PortAudio Portable Audio Library.
9 * For more information see: http://www.portaudio.com
10 * Copyright (c) 1999-2000 Ross Bencina and Phil Burk
11 *
12 * Permission is hereby granted, free of charge, to any person obtaining
13 * a copy of this software and associated documentation files
14 * (the "Software"), to deal in the Software without restriction,
15 * including without limitation the rights to use, copy, modify, merge,
16 * publish, distribute, sublicense, and/or sell copies of the Software,
17 * and to permit persons to whom the Software is furnished to do so,
18 * subject to the following conditions:
19 *
20 * The above copyright notice and this permission notice shall be
21 * included in all copies or substantial portions of the Software.
22 *
23 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
24 * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
25 * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
26 * IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR
27 * ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF
28 * CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
29 * WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
30 */
31
32/*
33 * The text above constitutes the entire PortAudio license; however,
34 * the PortAudio community also makes the following non-binding requests:
35 *
36 * Any person wishing to distribute modifications to the Software is
37 * requested to send the modifications to the original developer so that
38 * they can be incorporated into the canonical version. It is also
39 * requested that these non-binding requests be included along with the
40 * license above.
41 */
42
48/****************
49 * Some memory barrier primitives based on the system.
50 * right now only OS X, FreeBSD, and Linux are supported. In addition to providing
51 * memory barriers, these functions should ensure that data cached in registers
52 * is written out to cache where it can be snooped by other CPUs. (ie, the volatile
53 * keyword should not be required)
54 *
55 * the primitives that must be defined are:
56 *
57 * PaUtil_FullMemoryBarrier()
58 * PaUtil_ReadMemoryBarrier()
59 * PaUtil_WriteMemoryBarrier()
60 *
61 ****************/
62
63#if defined(__APPLE__)
64/* Support for the atomic library was added in C11.
65 */
66# if (__STDC_VERSION__ < 201112L) || defined(__STDC_NO_ATOMICS__)
67# include <libkern/OSAtomic.h>
68 /* Here are the memory barrier functions. Mac OS X only provides
69 full memory barriers, so the three types of barriers are the same,
70 however, these barriers are superior to compiler-based ones.
71 These were deprecated in MacOS 10.12. */
72# define PaUtil_FullMemoryBarrier() OSMemoryBarrier()
73# define PaUtil_ReadMemoryBarrier() OSMemoryBarrier()
74# define PaUtil_WriteMemoryBarrier() OSMemoryBarrier()
75# else
76# include <stdatomic.h>
77# define PaUtil_FullMemoryBarrier() atomic_thread_fence(memory_order_seq_cst)
78# define PaUtil_ReadMemoryBarrier() atomic_thread_fence(memory_order_acquire)
79# define PaUtil_WriteMemoryBarrier() atomic_thread_fence(memory_order_release)
80# endif
81#elif defined(__GNUC__)
82 /* GCC >= 4.1 has built-in intrinsics. We'll use those */
83# if (__GNUC__ > 4) || (__GNUC__ == 4 && __GNUC_MINOR__ >= 1)
84# define PaUtil_FullMemoryBarrier() __sync_synchronize()
85# define PaUtil_ReadMemoryBarrier() __sync_synchronize()
86# define PaUtil_WriteMemoryBarrier() __sync_synchronize()
87 /* as a fallback, GCC understands volatile asm and "memory" to mean it
88 * should not reorder memory read/writes */
89 /* Note that it is not clear that any compiler actually defines __PPC__,
90 * it can probably removed safely. */
91# elif defined( __ppc__ ) || defined( __powerpc__) || defined( __PPC__ )
92# define PaUtil_FullMemoryBarrier() asm volatile("sync":::"memory")
93# define PaUtil_ReadMemoryBarrier() asm volatile("sync":::"memory")
94# define PaUtil_WriteMemoryBarrier() asm volatile("sync":::"memory")
95# elif defined( __i386__ ) || defined( __i486__ ) || defined( __i586__ ) || \
96 defined( __i686__ ) || defined( __x86_64__ )
97# define PaUtil_FullMemoryBarrier() asm volatile("mfence":::"memory")
98# define PaUtil_ReadMemoryBarrier() asm volatile("lfence":::"memory")
99# define PaUtil_WriteMemoryBarrier() asm volatile("sfence":::"memory")
100# else
101# ifdef ALLOW_SMP_DANGERS
102# warning Memory barriers not defined on this system or system unknown
103# warning For SMP safety, you should fix this.
104# define PaUtil_FullMemoryBarrier()
105# define PaUtil_ReadMemoryBarrier()
106# define PaUtil_WriteMemoryBarrier()
107# else
108# error Memory barriers are not defined on this system. You can still compile by defining ALLOW_SMP_DANGERS, but SMP safety will not be guaranteed.
109# endif
110# endif
111#elif (_MSC_VER >= 1400) && !defined(_WIN32_WCE)
112# include <intrin.h>
113# pragma intrinsic(_ReadWriteBarrier)
114# pragma intrinsic(_ReadBarrier)
115# pragma intrinsic(_WriteBarrier)
116/* note that MSVC intrinsics _ReadWriteBarrier(), _ReadBarrier(), _WriteBarrier() are just compiler barriers *not* memory barriers */
117# define PaUtil_FullMemoryBarrier() _ReadWriteBarrier()
118# define PaUtil_ReadMemoryBarrier() _ReadBarrier()
119# define PaUtil_WriteMemoryBarrier() _WriteBarrier()
120#elif defined(_WIN32_WCE)
121# define PaUtil_FullMemoryBarrier()
122# define PaUtil_ReadMemoryBarrier()
123# define PaUtil_WriteMemoryBarrier()
124#elif defined(_MSC_VER) || defined(__BORLANDC__)
125# define PaUtil_FullMemoryBarrier() _asm { lock add [esp], 0 }
126# define PaUtil_ReadMemoryBarrier() _asm { lock add [esp], 0 }
127# define PaUtil_WriteMemoryBarrier() _asm { lock add [esp], 0 }
128#else
129# ifdef ALLOW_SMP_DANGERS
130# warning Memory barriers not defined on this system or system unknown
131# warning For SMP safety, you should fix this.
132# define PaUtil_FullMemoryBarrier()
133# define PaUtil_ReadMemoryBarrier()
134# define PaUtil_WriteMemoryBarrier()
135# else
136# error Memory barriers are not defined on this system. You can still compile by defining ALLOW_SMP_DANGERS, but SMP safety will not be guaranteed.
137# endif
138#endif