![]() |
Mixxx
|
00001 /* 00002 * $Id: pa_memorybarrier.h 1240 2007-07-17 13:05:07Z bjornroche $ 00003 * Portable Audio I/O Library 00004 * Memory barrier utilities 00005 * 00006 * Author: Bjorn Roche, XO Audio, LLC 00007 * 00008 * This program uses the PortAudio Portable Audio Library. 00009 * For more information see: http://www.portaudio.com 00010 * Copyright (c) 1999-2000 Ross Bencina and Phil Burk 00011 * 00012 * Permission is hereby granted, free of charge, to any person obtaining 00013 * a copy of this software and associated documentation files 00014 * (the "Software"), to deal in the Software without restriction, 00015 * including without limitation the rights to use, copy, modify, merge, 00016 * publish, distribute, sublicense, and/or sell copies of the Software, 00017 * and to permit persons to whom the Software is furnished to do so, 00018 * subject to the following conditions: 00019 * 00020 * The above copyright notice and this permission notice shall be 00021 * included in all copies or substantial portions of the Software. 00022 * 00023 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, 00024 * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF 00025 * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. 00026 * IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR 00027 * ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF 00028 * CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION 00029 * WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. 00030 */ 00031 00032 /* 00033 * The text above constitutes the entire PortAudio license; however, 00034 * the PortAudio community also makes the following non-binding requests: 00035 * 00036 * Any person wishing to distribute modifications to the Software is 00037 * requested to send the modifications to the original developer so that 00038 * they can be incorporated into the canonical version. It is also 00039 * requested that these non-binding requests be included along with the 00040 * license above. 00041 */ 00042 00048 /**************** 00049 * Some memory barrier primitives based on the system. 00050 * right now only OS X, FreeBSD, and Linux are supported. In addition to providing 00051 * memory barriers, these functions should ensure that data cached in registers 00052 * is written out to cache where it can be snooped by other CPUs. (ie, the volatile 00053 * keyword should not be required) 00054 * 00055 * the primitives that must be defined are: 00056 * 00057 * PaUtil_FullMemoryBarrier() 00058 * PaUtil_ReadMemoryBarrier() 00059 * PaUtil_WriteMemoryBarrier() 00060 * 00061 ****************/ 00062 00063 #if defined(__APPLE__) 00064 # include <libkern/OSAtomic.h> 00065 /* Here are the memory barrier functions. Mac OS X only provides 00066 full memory barriers, so the three types of barriers are the same, 00067 however, these barriers are superior to compiler-based ones. */ 00068 # define PaUtil_FullMemoryBarrier() OSMemoryBarrier() 00069 # define PaUtil_ReadMemoryBarrier() OSMemoryBarrier() 00070 # define PaUtil_WriteMemoryBarrier() OSMemoryBarrier() 00071 #elif defined(__GNUC__) 00072 /* GCC >= 4.1 has built-in intrinsics. We'll use those */ 00073 # if (__GNUC__ > 4) || (__GNUC__ == 4 && __GNUC_MINOR__ >= 1) 00074 # define PaUtil_FullMemoryBarrier() __sync_synchronize() 00075 # define PaUtil_ReadMemoryBarrier() __sync_synchronize() 00076 # define PaUtil_WriteMemoryBarrier() __sync_synchronize() 00077 /* as a fallback, GCC understands volatile asm and "memory" to mean it 00078 * should not reorder memory read/writes */ 00079 /* Note that it is not clear that any compiler actually defines __PPC__, 00080 * it can probably removed safely. */ 00081 # elif defined( __ppc__ ) || defined( __powerpc__) || defined( __PPC__ ) 00082 # define PaUtil_FullMemoryBarrier() asm volatile("sync":::"memory") 00083 # define PaUtil_ReadMemoryBarrier() asm volatile("sync":::"memory") 00084 # define PaUtil_WriteMemoryBarrier() asm volatile("sync":::"memory") 00085 # elif defined( __i386__ ) || defined( __i486__ ) || defined( __i586__ ) || \ 00086 defined( __i686__ ) || defined( __x86_64__ ) 00087 # define PaUtil_FullMemoryBarrier() asm volatile("mfence":::"memory") 00088 # define PaUtil_ReadMemoryBarrier() asm volatile("lfence":::"memory") 00089 # define PaUtil_WriteMemoryBarrier() asm volatile("sfence":::"memory") 00090 # else 00091 # ifdef ALLOW_SMP_DANGERS 00092 # warning Memory barriers not defined on this system or system unknown 00093 # warning For SMP safety, you should fix this. 00094 # define PaUtil_FullMemoryBarrier() 00095 # define PaUtil_ReadMemoryBarrier() 00096 # define PaUtil_WriteMemoryBarrier() 00097 # else 00098 # 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. 00099 # endif 00100 # endif 00101 #elif (_MSC_VER >= 1400) && !defined(_WIN32_WCE) 00102 # include <intrin.h> 00103 # pragma intrinsic(_ReadWriteBarrier) 00104 # pragma intrinsic(_ReadBarrier) 00105 # pragma intrinsic(_WriteBarrier) 00106 /* note that MSVC intrinsics _ReadWriteBarrier(), _ReadBarrier(), _WriteBarrier() are just compiler barriers *not* memory barriers */ 00107 # define PaUtil_FullMemoryBarrier() _ReadWriteBarrier() 00108 # define PaUtil_ReadMemoryBarrier() _ReadBarrier() 00109 # define PaUtil_WriteMemoryBarrier() _WriteBarrier() 00110 #elif defined(_WIN32_WCE) 00111 # define PaUtil_FullMemoryBarrier() 00112 # define PaUtil_ReadMemoryBarrier() 00113 # define PaUtil_WriteMemoryBarrier() 00114 #elif defined(_MSC_VER) || defined(__BORLANDC__) 00115 # define PaUtil_FullMemoryBarrier() _asm { lock add [esp], 0 } 00116 # define PaUtil_ReadMemoryBarrier() _asm { lock add [esp], 0 } 00117 # define PaUtil_WriteMemoryBarrier() _asm { lock add [esp], 0 } 00118 #else 00119 # ifdef ALLOW_SMP_DANGERS 00120 # warning Memory barriers not defined on this system or system unknown 00121 # warning For SMP safety, you should fix this. 00122 # define PaUtil_FullMemoryBarrier() 00123 # define PaUtil_ReadMemoryBarrier() 00124 # define PaUtil_WriteMemoryBarrier() 00125 # else 00126 # 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. 00127 # endif 00128 #endif