pa_memorybarrier.h 5.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129
  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. * The text above constitutes the entire PortAudio license; however,
  33. * the PortAudio community also makes the following non-binding requests:
  34. *
  35. * Any person wishing to distribute modifications to the Software is
  36. * requested to send the modifications to the original developer so that
  37. * they can be incorporated into the canonical version. It is also
  38. * requested that these non-binding requests be included along with the
  39. * license above.
  40. */
  41. /**
  42. @file pa_memorybarrier.h
  43. @ingroup common_src
  44. */
  45. /****************
  46. * Some memory barrier primitives based on the system.
  47. * right now only OS X, FreeBSD, and Linux are supported. In addition to providing
  48. * memory barriers, these functions should ensure that data cached in registers
  49. * is written out to cache where it can be snooped by other CPUs. (ie, the volatile
  50. * keyword should not be required)
  51. *
  52. * the primitives that must be defined are:
  53. *
  54. * PaUtil_FullMemoryBarrier()
  55. * PaUtil_ReadMemoryBarrier()
  56. * PaUtil_WriteMemoryBarrier()
  57. *
  58. ****************/
  59. #if defined(__APPLE__)
  60. # include <libkern/OSAtomic.h>
  61. /* Here are the memory barrier functions. Mac OS X only provides
  62. full memory barriers, so the three types of barriers are the same,
  63. however, these barriers are superior to compiler-based ones. */
  64. # define PaUtil_FullMemoryBarrier() OSMemoryBarrier()
  65. # define PaUtil_ReadMemoryBarrier() OSMemoryBarrier()
  66. # define PaUtil_WriteMemoryBarrier() OSMemoryBarrier()
  67. #elif defined(__GNUC__)
  68. /* GCC >= 4.1 has built-in intrinsics. We'll use those */
  69. # if (__GNUC__ > 4) || (__GNUC__ == 4 && __GNUC_MINOR__ >= 1)
  70. # define PaUtil_FullMemoryBarrier() __sync_synchronize()
  71. # define PaUtil_ReadMemoryBarrier() __sync_synchronize()
  72. # define PaUtil_WriteMemoryBarrier() __sync_synchronize()
  73. /* as a fallback, GCC understands volatile asm and "memory" to mean it
  74. * should not reorder memory read/writes */
  75. /* Note that it is not clear that any compiler actually defines __PPC__,
  76. * it can probably removed safely. */
  77. # elif defined( __ppc__ ) || defined( __powerpc__) || defined( __PPC__ )
  78. # define PaUtil_FullMemoryBarrier() asm volatile("sync":::"memory")
  79. # define PaUtil_ReadMemoryBarrier() asm volatile("sync":::"memory")
  80. # define PaUtil_WriteMemoryBarrier() asm volatile("sync":::"memory")
  81. # elif defined( __i386__ ) || defined( __i486__ ) || defined( __i586__ ) || \
  82. defined( __i686__ ) || defined( __x86_64__ )
  83. # define PaUtil_FullMemoryBarrier() asm volatile("mfence":::"memory")
  84. # define PaUtil_ReadMemoryBarrier() asm volatile("lfence":::"memory")
  85. # define PaUtil_WriteMemoryBarrier() asm volatile("sfence":::"memory")
  86. # else
  87. # ifdef ALLOW_SMP_DANGERS
  88. # warning Memory barriers not defined on this system or system unknown
  89. # warning For SMP safety, you should fix this.
  90. # define PaUtil_FullMemoryBarrier()
  91. # define PaUtil_ReadMemoryBarrier()
  92. # define PaUtil_WriteMemoryBarrier()
  93. # else
  94. # 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.
  95. # endif
  96. # endif
  97. #elif (_MSC_VER >= 1400) && !defined(_WIN32_WCE)
  98. # include <intrin.h>
  99. # pragma intrinsic(_ReadWriteBarrier)
  100. # pragma intrinsic(_ReadBarrier)
  101. # pragma intrinsic(_WriteBarrier)
  102. /* note that MSVC intrinsics _ReadWriteBarrier(), _ReadBarrier(), _WriteBarrier() are just compiler barriers *not* memory barriers */
  103. # define PaUtil_FullMemoryBarrier() _ReadWriteBarrier()
  104. # define PaUtil_ReadMemoryBarrier() _ReadBarrier()
  105. # define PaUtil_WriteMemoryBarrier() _WriteBarrier()
  106. #elif defined(_WIN32_WCE)
  107. # define PaUtil_FullMemoryBarrier()
  108. # define PaUtil_ReadMemoryBarrier()
  109. # define PaUtil_WriteMemoryBarrier()
  110. #elif defined(_MSC_VER) || defined(__BORLANDC__)
  111. # define PaUtil_FullMemoryBarrier() _asm { lock add [esp], 0 }
  112. # define PaUtil_ReadMemoryBarrier() _asm { lock add [esp], 0 }
  113. # define PaUtil_WriteMemoryBarrier() _asm { lock add [esp], 0 }
  114. #else
  115. # ifdef ALLOW_SMP_DANGERS
  116. # warning Memory barriers not defined on this system or system unknown
  117. # warning For SMP safety, you should fix this.
  118. # define PaUtil_FullMemoryBarrier()
  119. # define PaUtil_ReadMemoryBarrier()
  120. # define PaUtil_WriteMemoryBarrier()
  121. # else
  122. # 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.
  123. # endif
  124. #endif