• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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 #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 #define PaUtil_FullMemoryBarrier() OSMemoryBarrier()
72 #define PaUtil_ReadMemoryBarrier() OSMemoryBarrier()
73 #define PaUtil_WriteMemoryBarrier() OSMemoryBarrier()
74 #elif defined(__GNUC__)
75 /* GCC >= 4.1 has built-in intrinsics. We'll use those */
76 #if (__GNUC__ > 4) || (__GNUC__ == 4 && __GNUC_MINOR__ >= 1)
77 #define PaUtil_FullMemoryBarrier() __sync_synchronize()
78 #define PaUtil_ReadMemoryBarrier() __sync_synchronize()
79 #define PaUtil_WriteMemoryBarrier() __sync_synchronize()
80 /* as a fallback, GCC understands volatile asm and "memory" to mean it
81  * should not reorder memory read/writes */
82 /* Note that it is not clear that any compiler actually defines __PPC__,
83  * it can probably removed safely. */
84 #elif defined(__ppc__) || defined(__powerpc__) || defined(__PPC__)
85 #define PaUtil_FullMemoryBarrier() asm volatile("sync" ::: "memory")
86 #define PaUtil_ReadMemoryBarrier() asm volatile("sync" ::: "memory")
87 #define PaUtil_WriteMemoryBarrier() asm volatile("sync" ::: "memory")
88 #elif defined(__i386__) || defined(__i486__) || defined(__i586__) || \
89     defined(__i686__) || defined(__x86_64__)
90 #define PaUtil_FullMemoryBarrier() asm volatile("mfence" ::: "memory")
91 #define PaUtil_ReadMemoryBarrier() asm volatile("lfence" ::: "memory")
92 #define PaUtil_WriteMemoryBarrier() asm volatile("sfence" ::: "memory")
93 #else
94 #ifdef ALLOW_SMP_DANGERS
95 #warning Memory barriers not defined on this system or system unknown
96 #warning For SMP safety, you should fix this.
97 #define PaUtil_FullMemoryBarrier()
98 #define PaUtil_ReadMemoryBarrier()
99 #define PaUtil_WriteMemoryBarrier()
100 #else
101 #         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.
102 #endif
103 #endif
104 #elif (_MSC_VER >= 1400) && !defined(_WIN32_WCE)
105 #include <intrin.h>
106 #pragma intrinsic(_ReadWriteBarrier)
107 #pragma intrinsic(_ReadBarrier)
108 #pragma intrinsic(_WriteBarrier)
109 #define PaUtil_FullMemoryBarrier() _ReadWriteBarrier()
110 #define PaUtil_ReadMemoryBarrier() _ReadBarrier()
111 #define PaUtil_WriteMemoryBarrier() _WriteBarrier()
112 #elif defined(_WIN32_WCE)
113 #define PaUtil_FullMemoryBarrier()
114 #define PaUtil_ReadMemoryBarrier()
115 #define PaUtil_WriteMemoryBarrier()
116 #elif defined(_MSC_VER) || defined(__BORLANDC__)
117 #define PaUtil_FullMemoryBarrier() _asm { lock add    [esp], 0}
118 #define PaUtil_ReadMemoryBarrier() _asm { lock add    [esp], 0}
119 #define PaUtil_WriteMemoryBarrier() _asm { lock add    [esp], 0}
120 #else
121 #ifdef ALLOW_SMP_DANGERS
122 #warning Memory barriers not defined on this system or system unknown
123 #warning For SMP safety, you should fix this.
124 #define PaUtil_FullMemoryBarrier()
125 #define PaUtil_ReadMemoryBarrier()
126 #define PaUtil_WriteMemoryBarrier()
127 #else
128 #      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.
129 #endif
130 #endif
131 
132 #endif /* MODULES_THIRD_PARTY_PORTAUDIO_PA_MEMORYBARRIER_H_ */
133