fallback.h 4.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146
  1. /*-------------------------------------------------------------------------
  2. *
  3. * fallback.h
  4. * Fallback for platforms without spinlock and/or atomics support. Slower
  5. * than native atomics support, but not unusably slow.
  6. *
  7. * Portions Copyright (c) 1996-2016, PostgreSQL Global Development Group
  8. * Portions Copyright (c) 1994, Regents of the University of California
  9. *
  10. * src/include/port/atomics/fallback.h
  11. *
  12. *-------------------------------------------------------------------------
  13. */
  14. /* intentionally no include guards, should only be included by atomics.h */
  15. #ifndef INSIDE_ATOMICS_H
  16. # error "should be included via atomics.h"
  17. #endif
  18. #ifndef pg_memory_barrier_impl
  19. /*
  20. * If we have no memory barrier implementation for this architecture, we
  21. * fall back to acquiring and releasing a spinlock. This might, in turn,
  22. * fall back to the semaphore-based spinlock implementation, which will be
  23. * amazingly slow.
  24. *
  25. * It's not self-evident that every possible legal implementation of a
  26. * spinlock acquire-and-release would be equivalent to a full memory barrier.
  27. * For example, I'm not sure that Itanium's acq and rel add up to a full
  28. * fence. But all of our actual implementations seem OK in this regard.
  29. */
  30. #define PG_HAVE_MEMORY_BARRIER_EMULATION
  31. extern void pg_spinlock_barrier(void);
  32. #define pg_memory_barrier_impl pg_spinlock_barrier
  33. #endif
  34. #ifndef pg_compiler_barrier_impl
  35. /*
  36. * If the compiler/arch combination does not provide compiler barriers,
  37. * provide a fallback. The fallback simply consists of a function call into
  38. * an externally defined function. That should guarantee compiler barrier
  39. * semantics except for compilers that do inter translation unit/global
  40. * optimization - those better provide an actual compiler barrier.
  41. *
  42. * A native compiler barrier for sure is a lot faster than this...
  43. */
  44. #define PG_HAVE_COMPILER_BARRIER_EMULATION
  45. extern void pg_extern_compiler_barrier(void);
  46. #define pg_compiler_barrier_impl pg_extern_compiler_barrier
  47. #endif
  48. /*
  49. * If we have atomics implementation for this platform, fall back to providing
  50. * the atomics API using a spinlock to protect the internal state. Possibly
  51. * the spinlock implementation uses semaphores internally...
  52. *
  53. * We have to be a bit careful here, as it's not guaranteed that atomic
  54. * variables are mapped to the same address in every process (e.g. dynamic
  55. * shared memory segments). We can't just hash the address and use that to map
  56. * to a spinlock. Instead assign a spinlock on initialization of the atomic
  57. * variable.
  58. */
  59. #if !defined(PG_HAVE_ATOMIC_FLAG_SUPPORT) && !defined(PG_HAVE_ATOMIC_U32_SUPPORT)
  60. #define PG_HAVE_ATOMIC_FLAG_SIMULATION
  61. #define PG_HAVE_ATOMIC_FLAG_SUPPORT
  62. typedef struct pg_atomic_flag
  63. {
  64. /*
  65. * To avoid circular includes we can't use s_lock as a type here. Instead
  66. * just reserve enough space for all spinlock types. Some platforms would
  67. * be content with just one byte instead of 4, but that's not too much
  68. * waste.
  69. */
  70. #if defined(__hppa) || defined(__hppa__) /* HP PA-RISC, GCC and HP compilers */
  71. int sema[4];
  72. #else
  73. int sema;
  74. #endif
  75. } pg_atomic_flag;
  76. #endif /* PG_HAVE_ATOMIC_FLAG_SUPPORT */
  77. #if !defined(PG_HAVE_ATOMIC_U32_SUPPORT)
  78. #define PG_HAVE_ATOMIC_U32_SIMULATION
  79. #define PG_HAVE_ATOMIC_U32_SUPPORT
  80. typedef struct pg_atomic_uint32
  81. {
  82. /* Check pg_atomic_flag's definition above for an explanation */
  83. #if defined(__hppa) || defined(__hppa__) /* HP PA-RISC, GCC and HP compilers */
  84. int sema[4];
  85. #else
  86. int sema;
  87. #endif
  88. volatile uint32 value;
  89. } pg_atomic_uint32;
  90. #endif /* PG_HAVE_ATOMIC_U32_SUPPORT */
  91. #ifdef PG_HAVE_ATOMIC_FLAG_SIMULATION
  92. #define PG_HAVE_ATOMIC_INIT_FLAG
  93. extern void pg_atomic_init_flag_impl(volatile pg_atomic_flag *ptr);
  94. #define PG_HAVE_ATOMIC_TEST_SET_FLAG
  95. extern bool pg_atomic_test_set_flag_impl(volatile pg_atomic_flag *ptr);
  96. #define PG_HAVE_ATOMIC_CLEAR_FLAG
  97. extern void pg_atomic_clear_flag_impl(volatile pg_atomic_flag *ptr);
  98. #define PG_HAVE_ATOMIC_UNLOCKED_TEST_FLAG
  99. static inline bool
  100. pg_atomic_unlocked_test_flag_impl(volatile pg_atomic_flag *ptr)
  101. {
  102. /*
  103. * Can't do this efficiently in the semaphore based implementation - we'd
  104. * have to try to acquire the semaphore - so always return true. That's
  105. * correct, because this is only an unlocked test anyway. Do this in the
  106. * header so compilers can optimize the test away.
  107. */
  108. return true;
  109. }
  110. #endif /* PG_HAVE_ATOMIC_FLAG_SIMULATION */
  111. #ifdef PG_HAVE_ATOMIC_U32_SIMULATION
  112. #define PG_HAVE_ATOMIC_INIT_U32
  113. extern void pg_atomic_init_u32_impl(volatile pg_atomic_uint32 *ptr, uint32 val_);
  114. #define PG_HAVE_ATOMIC_WRITE_U32
  115. extern void pg_atomic_write_u32_impl(volatile pg_atomic_uint32 *ptr, uint32 val);
  116. #define PG_HAVE_ATOMIC_COMPARE_EXCHANGE_U32
  117. extern bool pg_atomic_compare_exchange_u32_impl(volatile pg_atomic_uint32 *ptr,
  118. uint32 *expected, uint32 newval);
  119. #define PG_HAVE_ATOMIC_FETCH_ADD_U32
  120. extern uint32 pg_atomic_fetch_add_u32_impl(volatile pg_atomic_uint32 *ptr, int32 add_);
  121. #endif /* PG_HAVE_ATOMIC_U32_SIMULATION */