xref: /aosp_15_r20/external/webrtc/modules/third_party/portaudio/pa_memorybarrier.h (revision d9f758449e529ab9291ac668be2861e7a55c2422)
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 
43 /**
44  @file pa_memorybarrier.h
45  @ingroup common_src
46 */
47 
48 /****************
49  * Some memory barrier primitives based on the system.
50  * right now only OS X, FreeBSD, and Linux are supported. In addition to
51  *providing memory barriers, these functions should ensure that data cached in
52  *registers is written out to cache where it can be snooped by other CPUs. (ie,
53  *the volatile 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 #ifndef MODULES_THIRD_PARTY_PORTAUDIO_PA_MEMORYBARRIER_H_
64 #define MODULES_THIRD_PARTY_PORTAUDIO_PA_MEMORYBARRIER_H_
65 
66 #if defined(__APPLE__)
67 /* Support for the atomic library was added in C11.
68  */
69 #if (__STDC_VERSION__ < 201112L) || defined(__STDC_NO_ATOMICS__)
70 #include <libkern/OSAtomic.h>
71 /* Here are the memory barrier functions. Mac OS X only provides
72    full memory barriers, so the three types of barriers are the same,
73    however, these barriers are superior to compiler-based ones.
74    These were deprecated in MacOS 10.12. */
75 #define PaUtil_FullMemoryBarrier() OSMemoryBarrier()
76 #define PaUtil_ReadMemoryBarrier() OSMemoryBarrier()
77 #define PaUtil_WriteMemoryBarrier() OSMemoryBarrier()
78 #else
79 #include <stdatomic.h>
80 #define PaUtil_FullMemoryBarrier() atomic_thread_fence(memory_order_seq_cst)
81 #define PaUtil_ReadMemoryBarrier() atomic_thread_fence(memory_order_acquire)
82 #define PaUtil_WriteMemoryBarrier() atomic_thread_fence(memory_order_release)
83 #endif
84 #elif defined(__GNUC__)
85 /* GCC >= 4.1 has built-in intrinsics. We'll use those */
86 #if (__GNUC__ > 4) || (__GNUC__ == 4 && __GNUC_MINOR__ >= 1)
87 #define PaUtil_FullMemoryBarrier() __sync_synchronize()
88 #define PaUtil_ReadMemoryBarrier() __sync_synchronize()
89 #define PaUtil_WriteMemoryBarrier() __sync_synchronize()
90 /* as a fallback, GCC understands volatile asm and "memory" to mean it
91  * should not reorder memory read/writes */
92 /* Note that it is not clear that any compiler actually defines __PPC__,
93  * it can probably removed safely. */
94 #elif defined(__ppc__) || defined(__powerpc__) || defined(__PPC__)
95 #define PaUtil_FullMemoryBarrier() asm volatile("sync" ::: "memory")
96 #define PaUtil_ReadMemoryBarrier() asm volatile("sync" ::: "memory")
97 #define PaUtil_WriteMemoryBarrier() asm volatile("sync" ::: "memory")
98 #elif defined(__i386__) || defined(__i486__) || defined(__i586__) || \
99     defined(__i686__) || defined(__x86_64__)
100 #define PaUtil_FullMemoryBarrier() asm volatile("mfence" ::: "memory")
101 #define PaUtil_ReadMemoryBarrier() asm volatile("lfence" ::: "memory")
102 #define PaUtil_WriteMemoryBarrier() asm volatile("sfence" ::: "memory")
103 #else
104 #ifdef ALLOW_SMP_DANGERS
105 #warning Memory barriers not defined on this system or system unknown
106 #warning For SMP safety, you should fix this.
107 #define PaUtil_FullMemoryBarrier()
108 #define PaUtil_ReadMemoryBarrier()
109 #define PaUtil_WriteMemoryBarrier()
110 #else
111 #           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.
112 #endif
113 #endif
114 #elif (_MSC_VER >= 1400) && !defined(_WIN32_WCE)
115 #include <intrin.h>
116 #pragma intrinsic(_ReadWriteBarrier)
117 #pragma intrinsic(_ReadBarrier)
118 #pragma intrinsic(_WriteBarrier)
119 /* note that MSVC intrinsics _ReadWriteBarrier(), _ReadBarrier(),
120  * _WriteBarrier() are just compiler barriers *not* memory barriers */
121 #define PaUtil_FullMemoryBarrier() _ReadWriteBarrier()
122 #define PaUtil_ReadMemoryBarrier() _ReadBarrier()
123 #define PaUtil_WriteMemoryBarrier() _WriteBarrier()
124 #elif defined(_WIN32_WCE)
125 #define PaUtil_FullMemoryBarrier()
126 #define PaUtil_ReadMemoryBarrier()
127 #define PaUtil_WriteMemoryBarrier()
128 #elif defined(_MSC_VER) || defined(__BORLANDC__)
129 #define PaUtil_FullMemoryBarrier() _asm { lock add    [esp], 0}
130 #define PaUtil_ReadMemoryBarrier() _asm { lock add    [esp], 0}
131 #define PaUtil_WriteMemoryBarrier() _asm { lock add    [esp], 0}
132 #else
133 #ifdef ALLOW_SMP_DANGERS
134 #warning Memory barriers not defined on this system or system unknown
135 #warning For SMP safety, you should fix this.
136 #define PaUtil_FullMemoryBarrier()
137 #define PaUtil_ReadMemoryBarrier()
138 #define PaUtil_WriteMemoryBarrier()
139 #else
140 #       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.
141 #endif
142 #endif
143 
144 #endif /* MODULES_THIRD_PARTY_PORTAUDIO_PA_MEMORYBARRIER_H_ */
145