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