x86.h 5.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143
  1. /*
  2. * Copyright (c) 2003 Hewlett-Packard Development Company, L.P.
  3. *
  4. * Permission is hereby granted, free of charge, to any person obtaining a copy
  5. * of this software and associated documentation files (the "Software"), to deal
  6. * in the Software without restriction, including without limitation the rights
  7. * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
  8. * copies of the Software, and to permit persons to whom the Software is
  9. * furnished to do so, subject to the following conditions:
  10. *
  11. * The above copyright notice and this permission notice shall be included in
  12. * all copies or substantial portions of the Software.
  13. *
  14. * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  15. * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  16. * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
  17. * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
  18. * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
  19. * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
  20. * SOFTWARE.
  21. */
  22. /* If AO_ASSUME_WINDOWS98 is defined, we assume Windows 98 or newer. */
  23. /* If AO_ASSUME_VISTA is defined, we assume Windows Server 2003, Vista */
  24. /* or later. */
  25. #include "../all_aligned_atomic_load_store.h"
  26. #include "../test_and_set_t_is_char.h"
  27. #if defined(AO_ASSUME_VISTA) && !defined(AO_ASSUME_WINDOWS98)
  28. # define AO_ASSUME_WINDOWS98
  29. #endif
  30. #ifndef AO_USE_INTERLOCKED_INTRINSICS
  31. /* _Interlocked primitives (Inc, Dec, Xchg, Add) are always available */
  32. # define AO_USE_INTERLOCKED_INTRINSICS
  33. #endif
  34. #include "common32_defs.h"
  35. /* As far as we can tell, the lfence and sfence instructions are not */
  36. /* currently needed or useful for cached memory accesses. */
  37. /* Unfortunately mfence doesn't exist everywhere. */
  38. /* IsProcessorFeaturePresent(PF_COMPARE_EXCHANGE128) is */
  39. /* probably a conservative test for it? */
  40. #if defined(AO_USE_PENTIUM4_INSTRS)
  41. AO_INLINE void
  42. AO_nop_full(void)
  43. {
  44. __asm { mfence }
  45. }
  46. #define AO_HAVE_nop_full
  47. #else
  48. /* We could use the cpuid instruction. But that seems to be slower */
  49. /* than the default implementation based on test_and_set_full. Thus */
  50. /* we omit that bit of misinformation here. */
  51. #endif
  52. #ifndef AO_NO_ASM_XADD
  53. AO_INLINE unsigned char
  54. AO_char_fetch_and_add_full(volatile unsigned char *p, unsigned char incr)
  55. {
  56. __asm
  57. {
  58. mov al, incr
  59. mov ebx, p
  60. lock xadd byte ptr [ebx], al
  61. }
  62. /* Ignore possible "missing return value" warning here. */
  63. }
  64. # define AO_HAVE_char_fetch_and_add_full
  65. AO_INLINE unsigned short
  66. AO_short_fetch_and_add_full(volatile unsigned short *p, unsigned short incr)
  67. {
  68. __asm
  69. {
  70. mov ax, incr
  71. mov ebx, p
  72. lock xadd word ptr [ebx], ax
  73. }
  74. /* Ignore possible "missing return value" warning here. */
  75. }
  76. # define AO_HAVE_short_fetch_and_add_full
  77. #endif /* !AO_NO_ASM_XADD */
  78. AO_INLINE AO_TS_VAL_t
  79. AO_test_and_set_full(volatile AO_TS_t *addr)
  80. {
  81. __asm
  82. {
  83. mov eax,0xff ; /* AO_TS_SET */
  84. mov ebx,addr ;
  85. xchg byte ptr [ebx],al ;
  86. }
  87. /* Ignore possible "missing return value" warning here. */
  88. }
  89. #define AO_HAVE_test_and_set_full
  90. #ifdef _WIN64
  91. # error wrong architecture
  92. #endif
  93. #ifdef AO_ASSUME_VISTA
  94. # include "../standard_ao_double_t.h"
  95. /* Reading or writing a quadword aligned on a 64-bit boundary is */
  96. /* always carried out atomically (requires at least a Pentium). */
  97. # define AO_ACCESS_double_CHECK_ALIGNED
  98. # include "../loadstore/double_atomic_load_store.h"
  99. /* Whenever we run on a Pentium class machine, we have that certain */
  100. /* function. */
  101. # pragma intrinsic (_InterlockedCompareExchange64)
  102. /* Returns nonzero if the comparison succeeded. */
  103. AO_INLINE int
  104. AO_double_compare_and_swap_full(volatile AO_double_t *addr,
  105. AO_double_t old_val, AO_double_t new_val)
  106. {
  107. return (double_ptr_storage)_InterlockedCompareExchange64(
  108. (__int64 volatile *)addr,
  109. new_val.AO_whole /* exchange */,
  110. old_val.AO_whole) == old_val.AO_whole;
  111. }
  112. # define AO_HAVE_double_compare_and_swap_full
  113. #endif /* AO_ASSUME_VISTA */
  114. #define AO_T_IS_INT
  115. /* Real X86 implementations, except for some old WinChips, appear */
  116. /* to enforce ordering between memory operations, EXCEPT that a later */
  117. /* read can pass earlier writes, presumably due to the visible */
  118. /* presence of store buffers. */
  119. /* We ignore both the WinChips, and the fact that the official specs */
  120. /* seem to be much weaker (and arguably too weak to be usable). */
  121. #include "../ordered_except_wr.h"