9
3

generic-msvc.h 4.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135
  1. /* Copyright (c) 2006-2008 MySQL AB, 2009 Sun Microsystems, Inc.
  2. Use is subject to license terms.
  3. This program is free software; you can redistribute it and/or modify
  4. it under the terms of the GNU General Public License as published by
  5. the Free Software Foundation; version 2 of the License.
  6. This program is distributed in the hope that it will be useful,
  7. but WITHOUT ANY WARRANTY; without even the implied warranty of
  8. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  9. GNU General Public License for more details.
  10. You should have received a copy of the GNU General Public License
  11. along with this program; if not, write to the Free Software
  12. Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA */
  13. #ifndef _atomic_h_cleanup_
  14. #define _atomic_h_cleanup_ "atomic/generic-msvc.h"
  15. /*
  16. We don't implement anything specific for MY_ATOMIC_MODE_DUMMY, always use
  17. intrinsics.
  18. 8 and 16-bit atomics are not implemented, but it can be done if necessary.
  19. */
  20. #undef MY_ATOMIC_HAS_8_16
  21. #include <windows.h>
  22. /*
  23. x86 compilers (both VS2003 or VS2005) never use instrinsics, but generate
  24. function calls to kernel32 instead, even in the optimized build.
  25. We force intrinsics as described in MSDN documentation for
  26. _InterlockedCompareExchange.
  27. */
  28. #ifdef _M_IX86
  29. #if (_MSC_VER >= 1500)
  30. #include <intrin.h>
  31. #else
  32. C_MODE_START
  33. /*Visual Studio 2003 and earlier do not have prototypes for atomic intrinsics*/
  34. LONG _InterlockedCompareExchange (LONG volatile *Target, LONG Value, LONG Comp);
  35. LONGLONG _InterlockedCompareExchange64 (LONGLONG volatile *Target,
  36. LONGLONG Value, LONGLONG Comp);
  37. C_MODE_END
  38. #pragma intrinsic(_InterlockedCompareExchange)
  39. #pragma intrinsic(_InterlockedCompareExchange64)
  40. #endif
  41. #define InterlockedCompareExchange _InterlockedCompareExchange
  42. #define InterlockedCompareExchange64 _InterlockedCompareExchange64
  43. /*
  44. No need to do something special for InterlockedCompareExchangePointer
  45. as it is a #define to InterlockedCompareExchange. The same applies to
  46. InterlockedExchangePointer.
  47. */
  48. #endif /*_M_IX86*/
  49. #define MY_ATOMIC_MODE "msvc-intrinsics"
  50. /* Implement using CAS on WIN32 */
  51. #define IL_COMP_EXCHG32(X,Y,Z) \
  52. InterlockedCompareExchange((volatile LONG *)(X),(Y),(Z))
  53. #define IL_COMP_EXCHG64(X,Y,Z) \
  54. InterlockedCompareExchange64((volatile LONGLONG *)(X), \
  55. (LONGLONG)(Y),(LONGLONG)(Z))
  56. #define IL_COMP_EXCHGptr InterlockedCompareExchangePointer
  57. #define make_atomic_cas_body(S) \
  58. int ## S initial_cmp= *cmp; \
  59. int ## S initial_a= IL_COMP_EXCHG ## S (a, set, initial_cmp); \
  60. if (!(ret= (initial_a == initial_cmp))) *cmp= initial_a;
  61. #ifndef _M_IX86
  62. /* Use full set of optimised functions on WIN64 */
  63. #define IL_EXCHG_ADD32(X,Y) \
  64. InterlockedExchangeAdd((volatile LONG *)(X),(Y))
  65. #define IL_EXCHG_ADD64(X,Y) \
  66. InterlockedExchangeAdd64((volatile LONGLONG *)(X),(LONGLONG)(Y))
  67. #define IL_EXCHG32(X,Y) \
  68. InterlockedExchange((volatile LONG *)(X),(Y))
  69. #define IL_EXCHG64(X,Y) \
  70. InterlockedExchange64((volatile LONGLONG *)(X),(LONGLONG)(Y))
  71. #define IL_EXCHGptr InterlockedExchangePointer
  72. #define make_atomic_add_body(S) \
  73. v= IL_EXCHG_ADD ## S (a, v)
  74. #define make_atomic_swap_body(S) \
  75. v= IL_EXCHG ## S (a, v)
  76. #define make_atomic_load_body(S) \
  77. ret= 0; /* avoid compiler warning */ \
  78. ret= IL_COMP_EXCHG ## S (a, ret, ret);
  79. #endif
  80. /*
  81. my_yield_processor (equivalent of x86 PAUSE instruction) should be used
  82. to improve performance on hyperthreaded CPUs. Intel recommends to use it in
  83. spin loops also on non-HT machines to reduce power consumption (see e.g
  84. http://softwarecommunity.intel.com/articles/eng/2004.htm)
  85. Running benchmarks for spinlocks implemented with InterlockedCompareExchange
  86. and YieldProcessor shows that much better performance is achieved by calling
  87. YieldProcessor in a loop - that is, yielding longer. On Intel boxes setting
  88. loop count in the range 200-300 brought best results.
  89. */
  90. #ifndef YIELD_LOOPS
  91. #define YIELD_LOOPS 200
  92. #endif
  93. static __inline int my_yield_processor()
  94. {
  95. int i;
  96. for(i=0; i<YIELD_LOOPS; i++)
  97. {
  98. #if (_MSC_VER <= 1310)
  99. /* On older compilers YieldProcessor is not available, use inline assembly*/
  100. __asm { rep nop }
  101. #else
  102. YieldProcessor();
  103. #endif
  104. }
  105. return 1;
  106. }
  107. #define LF_BACKOFF my_yield_processor()
  108. #else /* cleanup */
  109. #undef IL_EXCHG_ADD32
  110. #undef IL_EXCHG_ADD64
  111. #undef IL_COMP_EXCHG32
  112. #undef IL_COMP_EXCHG64
  113. #undef IL_COMP_EXCHGptr
  114. #undef IL_EXCHG32
  115. #undef IL_EXCHG64
  116. #undef IL_EXCHGptr
  117. #endif