2
0

s_lock.h 31 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110
  1. /*-------------------------------------------------------------------------
  2. *
  3. * s_lock.h
  4. * Hardware-dependent implementation of spinlocks.
  5. *
  6. * NOTE: none of the macros in this file are intended to be called directly.
  7. * Call them through the hardware-independent macros in spin.h.
  8. *
  9. * The following hardware-dependent macros must be provided for each
  10. * supported platform:
  11. *
  12. * void S_INIT_LOCK(slock_t *lock)
  13. * Initialize a spinlock (to the unlocked state).
  14. *
  15. * int S_LOCK(slock_t *lock)
  16. * Acquire a spinlock, waiting if necessary.
  17. * Time out and abort() if unable to acquire the lock in a
  18. * "reasonable" amount of time --- typically ~ 1 minute.
  19. * Should return number of "delays"; see s_lock.c
  20. *
  21. * void S_UNLOCK(slock_t *lock)
  22. * Unlock a previously acquired lock.
  23. *
  24. * bool S_LOCK_FREE(slock_t *lock)
  25. * Tests if the lock is free. Returns true if free, false if locked.
  26. * This does *not* change the state of the lock.
  27. *
  28. * void SPIN_DELAY(void)
  29. * Delay operation to occur inside spinlock wait loop.
  30. *
  31. * Note to implementors: there are default implementations for all these
  32. * macros at the bottom of the file. Check if your platform can use
  33. * these or needs to override them.
  34. *
  35. * Usually, S_LOCK() is implemented in terms of even lower-level macros
  36. * TAS() and TAS_SPIN():
  37. *
  38. * int TAS(slock_t *lock)
  39. * Atomic test-and-set instruction. Attempt to acquire the lock,
  40. * but do *not* wait. Returns 0 if successful, nonzero if unable
  41. * to acquire the lock.
  42. *
  43. * int TAS_SPIN(slock_t *lock)
  44. * Like TAS(), but this version is used when waiting for a lock
  45. * previously found to be contended. By default, this is the
  46. * same as TAS(), but on some architectures it's better to poll a
  47. * contended lock using an unlocked instruction and retry the
  48. * atomic test-and-set only when it appears free.
  49. *
  50. * TAS() and TAS_SPIN() are NOT part of the API, and should never be called
  51. * directly.
  52. *
  53. * CAUTION: on some platforms TAS() and/or TAS_SPIN() may sometimes report
  54. * failure to acquire a lock even when the lock is not locked. For example,
  55. * on Alpha TAS() will "fail" if interrupted. Therefore a retry loop must
  56. * always be used, even if you are certain the lock is free.
  57. *
  58. * It is the responsibility of these macros to make sure that the compiler
  59. * does not re-order accesses to shared memory to precede the actual lock
  60. * acquisition, or follow the lock release. Prior to PostgreSQL 9.5, this
  61. * was the caller's responsibility, which meant that callers had to use
  62. * volatile-qualified pointers to refer to both the spinlock itself and the
  63. * shared data being accessed within the spinlocked critical section. This
  64. * was notationally awkward, easy to forget (and thus error-prone), and
  65. * prevented some useful compiler optimizations. For these reasons, we
  66. * now require that the macros themselves prevent compiler re-ordering,
  67. * so that the caller doesn't need to take special precautions.
  68. *
  69. * On platforms with weak memory ordering, the TAS(), TAS_SPIN(), and
  70. * S_UNLOCK() macros must further include hardware-level memory fence
  71. * instructions to prevent similar re-ordering at the hardware level.
  72. * TAS() and TAS_SPIN() must guarantee that loads and stores issued after
  73. * the macro are not executed until the lock has been obtained. Conversely,
  74. * S_UNLOCK() must guarantee that loads and stores issued before the macro
  75. * have been executed before the lock is released.
  76. *
  77. * On most supported platforms, TAS() uses a tas() function written
  78. * in assembly language to execute a hardware atomic-test-and-set
  79. * instruction. Equivalent OS-supplied mutex routines could be used too.
  80. *
  81. * If no system-specific TAS() is available (ie, HAVE_SPINLOCKS is not
  82. * defined), then we fall back on an emulation that uses SysV semaphores
  83. * (see spin.c). This emulation will be MUCH MUCH slower than a proper TAS()
  84. * implementation, because of the cost of a kernel call per lock or unlock.
  85. * An old report is that Postgres spends around 40% of its time in semop(2)
  86. * when using the SysV semaphore code.
  87. *
  88. *
  89. * Portions Copyright (c) 1996-2022, PostgreSQL Global Development Group
  90. * Portions Copyright (c) 1994, Regents of the University of California
  91. *
  92. * src/include/storage/s_lock.h
  93. *
  94. *-------------------------------------------------------------------------
  95. */
  96. #ifndef S_LOCK_H
  97. #define S_LOCK_H
  98. #ifdef FRONTEND
  99. #error "s_lock.h may not be included from frontend code"
  100. #endif
  101. #ifdef HAVE_SPINLOCKS /* skip spinlocks if requested */
  102. #if defined(__GNUC__) || defined(__INTEL_COMPILER)
  103. /*************************************************************************
  104. * All the gcc inlines
  105. * Gcc consistently defines the CPU as __cpu__.
  106. * Other compilers use __cpu or __cpu__ so we test for both in those cases.
  107. */
  108. /*----------
  109. * Standard gcc asm format (assuming "volatile slock_t *lock"):
  110. __asm__ __volatile__(
  111. " instruction \n"
  112. " instruction \n"
  113. " instruction \n"
  114. : "=r"(_res), "+m"(*lock) // return register, in/out lock value
  115. : "r"(lock) // lock pointer, in input register
  116. : "memory", "cc"); // show clobbered registers here
  117. * The output-operands list (after first colon) should always include
  118. * "+m"(*lock), whether or not the asm code actually refers to this
  119. * operand directly. This ensures that gcc believes the value in the
  120. * lock variable is used and set by the asm code. Also, the clobbers
  121. * list (after third colon) should always include "memory"; this prevents
  122. * gcc from thinking it can cache the values of shared-memory fields
  123. * across the asm code. Add "cc" if your asm code changes the condition
  124. * code register, and also list any temp registers the code uses.
  125. *----------
  126. */
  127. #ifdef __i386__ /* 32-bit i386 */
  128. #define HAS_TEST_AND_SET
  129. typedef unsigned char slock_t;
  130. #define TAS(lock) tas(lock)
  131. static __inline__ int
  132. tas(volatile slock_t *lock)
  133. {
  134. register slock_t _res = 1;
  135. /*
  136. * Use a non-locking test before asserting the bus lock. Note that the
  137. * extra test appears to be a small loss on some x86 platforms and a small
  138. * win on others; it's by no means clear that we should keep it.
  139. *
  140. * When this was last tested, we didn't have separate TAS() and TAS_SPIN()
  141. * macros. Nowadays it probably would be better to do a non-locking test
  142. * in TAS_SPIN() but not in TAS(), like on x86_64, but no-one's done the
  143. * testing to verify that. Without some empirical evidence, better to
  144. * leave it alone.
  145. */
  146. __asm__ __volatile__(
  147. " cmpb $0,%1 \n"
  148. " jne 1f \n"
  149. " lock \n"
  150. " xchgb %0,%1 \n"
  151. "1: \n"
  152. : "+q"(_res), "+m"(*lock)
  153. : /* no inputs */
  154. : "memory", "cc");
  155. return (int) _res;
  156. }
  157. #define SPIN_DELAY() spin_delay()
  158. static __inline__ void
  159. spin_delay(void)
  160. {
  161. /*
  162. * This sequence is equivalent to the PAUSE instruction ("rep" is
  163. * ignored by old IA32 processors if the following instruction is
  164. * not a string operation); the IA-32 Architecture Software
  165. * Developer's Manual, Vol. 3, Section 7.7.2 describes why using
  166. * PAUSE in the inner loop of a spin lock is necessary for good
  167. * performance:
  168. *
  169. * The PAUSE instruction improves the performance of IA-32
  170. * processors supporting Hyper-Threading Technology when
  171. * executing spin-wait loops and other routines where one
  172. * thread is accessing a shared lock or semaphore in a tight
  173. * polling loop. When executing a spin-wait loop, the
  174. * processor can suffer a severe performance penalty when
  175. * exiting the loop because it detects a possible memory order
  176. * violation and flushes the core processor's pipeline. The
  177. * PAUSE instruction provides a hint to the processor that the
  178. * code sequence is a spin-wait loop. The processor uses this
  179. * hint to avoid the memory order violation and prevent the
  180. * pipeline flush. In addition, the PAUSE instruction
  181. * de-pipelines the spin-wait loop to prevent it from
  182. * consuming execution resources excessively.
  183. */
  184. __asm__ __volatile__(
  185. " rep; nop \n");
  186. }
  187. #endif /* __i386__ */
  188. #ifdef __x86_64__ /* AMD Opteron, Intel EM64T */
  189. #define HAS_TEST_AND_SET
  190. typedef unsigned char slock_t;
  191. #define TAS(lock) tas(lock)
  192. /*
  193. * On Intel EM64T, it's a win to use a non-locking test before the xchg proper,
  194. * but only when spinning.
  195. *
  196. * See also Implementing Scalable Atomic Locks for Multi-Core Intel(tm) EM64T
  197. * and IA32, by Michael Chynoweth and Mary R. Lee. As of this writing, it is
  198. * available at:
  199. * http://software.intel.com/en-us/articles/implementing-scalable-atomic-locks-for-multi-core-intel-em64t-and-ia32-architectures
  200. */
  201. #define TAS_SPIN(lock) (*(lock) ? 1 : TAS(lock))
  202. static __inline__ int
  203. tas(volatile slock_t *lock)
  204. {
  205. register slock_t _res = 1;
  206. __asm__ __volatile__(
  207. " lock \n"
  208. " xchgb %0,%1 \n"
  209. : "+q"(_res), "+m"(*lock)
  210. : /* no inputs */
  211. : "memory", "cc");
  212. return (int) _res;
  213. }
  214. #define SPIN_DELAY() spin_delay()
  215. static __inline__ void
  216. spin_delay(void)
  217. {
  218. /*
  219. * Adding a PAUSE in the spin delay loop is demonstrably a no-op on
  220. * Opteron, but it may be of some use on EM64T, so we keep it.
  221. */
  222. __asm__ __volatile__(
  223. " rep; nop \n");
  224. }
  225. #endif /* __x86_64__ */
  226. #if defined(__ia64__) || defined(__ia64)
  227. /*
  228. * Intel Itanium, gcc or Intel's compiler.
  229. *
  230. * Itanium has weak memory ordering, but we rely on the compiler to enforce
  231. * strict ordering of accesses to volatile data. In particular, while the
  232. * xchg instruction implicitly acts as a memory barrier with 'acquire'
  233. * semantics, we do not have an explicit memory fence instruction in the
  234. * S_UNLOCK macro. We use a regular assignment to clear the spinlock, and
  235. * trust that the compiler marks the generated store instruction with the
  236. * ".rel" opcode.
  237. *
  238. * Testing shows that assumption to hold on gcc, although I could not find
  239. * any explicit statement on that in the gcc manual. In Intel's compiler,
  240. * the -m[no-]serialize-volatile option controls that, and testing shows that
  241. * it is enabled by default.
  242. *
  243. * While icc accepts gcc asm blocks on x86[_64], this is not true on ia64
  244. * (at least not in icc versions before 12.x). So we have to carry a separate
  245. * compiler-intrinsic-based implementation for it.
  246. */
  247. #define HAS_TEST_AND_SET
  248. typedef unsigned int slock_t;
  249. #define TAS(lock) tas(lock)
  250. /* On IA64, it's a win to use a non-locking test before the xchg proper */
  251. #define TAS_SPIN(lock) (*(lock) ? 1 : TAS(lock))
  252. #ifndef __INTEL_COMPILER
  253. static __inline__ int
  254. tas(volatile slock_t *lock)
  255. {
  256. long int ret;
  257. __asm__ __volatile__(
  258. " xchg4 %0=%1,%2 \n"
  259. : "=r"(ret), "+m"(*lock)
  260. : "r"(1)
  261. : "memory");
  262. return (int) ret;
  263. }
  264. #else /* __INTEL_COMPILER */
  265. static __inline__ int
  266. tas(volatile slock_t *lock)
  267. {
  268. int ret;
  269. ret = _InterlockedExchange(lock,1); /* this is a xchg asm macro */
  270. return ret;
  271. }
  272. /* icc can't use the regular gcc S_UNLOCK() macro either in this case */
  273. #define S_UNLOCK(lock) \
  274. do { __memory_barrier(); *(lock) = 0; } while (0)
  275. #endif /* __INTEL_COMPILER */
  276. #endif /* __ia64__ || __ia64 */
  277. /*
  278. * On ARM and ARM64, we use __sync_lock_test_and_set(int *, int) if available.
  279. *
  280. * We use the int-width variant of the builtin because it works on more chips
  281. * than other widths.
  282. */
  283. #if defined(__arm__) || defined(__arm) || defined(__aarch64__) || defined(__aarch64)
  284. #ifdef HAVE_GCC__SYNC_INT32_TAS
  285. #define HAS_TEST_AND_SET
  286. #define TAS(lock) tas(lock)
  287. typedef int slock_t;
  288. static __inline__ int
  289. tas(volatile slock_t *lock)
  290. {
  291. return __sync_lock_test_and_set(lock, 1);
  292. }
  293. #define S_UNLOCK(lock) __sync_lock_release(lock)
  294. /*
  295. * Using an ISB instruction to delay in spinlock loops appears beneficial on
  296. * high-core-count ARM64 processors. It seems mostly a wash for smaller gear,
  297. * and ISB doesn't exist at all on pre-v7 ARM chips.
  298. */
  299. #if defined(__aarch64__) || defined(__aarch64)
  300. #define SPIN_DELAY() spin_delay()
  301. static __inline__ void
  302. spin_delay(void)
  303. {
  304. __asm__ __volatile__(
  305. " isb; \n");
  306. }
  307. #endif /* __aarch64__ || __aarch64 */
  308. #endif /* HAVE_GCC__SYNC_INT32_TAS */
  309. #endif /* __arm__ || __arm || __aarch64__ || __aarch64 */
  310. /* S/390 and S/390x Linux (32- and 64-bit zSeries) */
  311. #if defined(__s390__) || defined(__s390x__)
  312. #define HAS_TEST_AND_SET
  313. typedef unsigned int slock_t;
  314. #define TAS(lock) tas(lock)
  315. static __inline__ int
  316. tas(volatile slock_t *lock)
  317. {
  318. int _res = 0;
  319. __asm__ __volatile__(
  320. " cs %0,%3,0(%2) \n"
  321. : "+d"(_res), "+m"(*lock)
  322. : "a"(lock), "d"(1)
  323. : "memory", "cc");
  324. return _res;
  325. }
  326. #endif /* __s390__ || __s390x__ */
  327. #if defined(__sparc__) /* Sparc */
  328. /*
  329. * Solaris has always run sparc processors in TSO (total store) mode, but
  330. * linux didn't use to and the *BSDs still don't. So, be careful about
  331. * acquire/release semantics. The CPU will treat superfluous membars as
  332. * NOPs, so it's just code space.
  333. */
  334. #define HAS_TEST_AND_SET
  335. typedef unsigned char slock_t;
  336. #define TAS(lock) tas(lock)
  337. static __inline__ int
  338. tas(volatile slock_t *lock)
  339. {
  340. register slock_t _res;
  341. /*
  342. * See comment in src/backend/port/tas/sunstudio_sparc.s for why this
  343. * uses "ldstub", and that file uses "cas". gcc currently generates
  344. * sparcv7-targeted binaries, so "cas" use isn't possible.
  345. */
  346. __asm__ __volatile__(
  347. " ldstub [%2], %0 \n"
  348. : "=r"(_res), "+m"(*lock)
  349. : "r"(lock)
  350. : "memory");
  351. #if defined(__sparcv7) || defined(__sparc_v7__)
  352. /*
  353. * No stbar or membar available, luckily no actually produced hardware
  354. * requires a barrier.
  355. */
  356. #elif defined(__sparcv8) || defined(__sparc_v8__)
  357. /* stbar is available (and required for both PSO, RMO), membar isn't */
  358. __asm__ __volatile__ ("stbar \n":::"memory");
  359. #else
  360. /*
  361. * #LoadStore (RMO) | #LoadLoad (RMO) together are the appropriate acquire
  362. * barrier for sparcv8+ upwards.
  363. */
  364. __asm__ __volatile__ ("membar #LoadStore | #LoadLoad \n":::"memory");
  365. #endif
  366. return (int) _res;
  367. }
  368. #if defined(__sparcv7) || defined(__sparc_v7__)
  369. /*
  370. * No stbar or membar available, luckily no actually produced hardware
  371. * requires a barrier. We fall through to the default gcc definition of
  372. * S_UNLOCK in this case.
  373. */
  374. #elif defined(__sparcv8) || defined(__sparc_v8__)
  375. /* stbar is available (and required for both PSO, RMO), membar isn't */
  376. #define S_UNLOCK(lock) \
  377. do \
  378. { \
  379. __asm__ __volatile__ ("stbar \n":::"memory"); \
  380. *((volatile slock_t *) (lock)) = 0; \
  381. } while (0)
  382. #else
  383. /*
  384. * #LoadStore (RMO) | #StoreStore (RMO, PSO) together are the appropriate
  385. * release barrier for sparcv8+ upwards.
  386. */
  387. #define S_UNLOCK(lock) \
  388. do \
  389. { \
  390. __asm__ __volatile__ ("membar #LoadStore | #StoreStore \n":::"memory"); \
  391. *((volatile slock_t *) (lock)) = 0; \
  392. } while (0)
  393. #endif
  394. #endif /* __sparc__ */
  395. /* PowerPC */
  396. #if defined(__ppc__) || defined(__powerpc__) || defined(__ppc64__) || defined(__powerpc64__)
  397. #define HAS_TEST_AND_SET
  398. typedef unsigned int slock_t;
  399. #define TAS(lock) tas(lock)
  400. /* On PPC, it's a win to use a non-locking test before the lwarx */
  401. #define TAS_SPIN(lock) (*(lock) ? 1 : TAS(lock))
  402. /*
  403. * The second operand of addi can hold a constant zero or a register number,
  404. * hence constraint "=&b" to avoid allocating r0. "b" stands for "address
  405. * base register"; most operands having this register-or-zero property are
  406. * address bases, e.g. the second operand of lwax.
  407. *
  408. * NOTE: per the Enhanced PowerPC Architecture manual, v1.0 dated 7-May-2002,
  409. * an isync is a sufficient synchronization barrier after a lwarx/stwcx loop.
  410. * On newer machines, we can use lwsync instead for better performance.
  411. *
  412. * Ordinarily, we'd code the branches here using GNU-style local symbols, that
  413. * is "1f" referencing "1:" and so on. But some people run gcc on AIX with
  414. * IBM's assembler as backend, and IBM's assembler doesn't do local symbols.
  415. * So hand-code the branch offsets; fortunately, all PPC instructions are
  416. * exactly 4 bytes each, so it's not too hard to count.
  417. */
  418. static __inline__ int
  419. tas(volatile slock_t *lock)
  420. {
  421. slock_t _t;
  422. int _res;
  423. __asm__ __volatile__(
  424. #ifdef USE_PPC_LWARX_MUTEX_HINT
  425. " lwarx %0,0,%3,1 \n"
  426. #else
  427. " lwarx %0,0,%3 \n"
  428. #endif
  429. " cmpwi %0,0 \n"
  430. " bne $+16 \n" /* branch to li %1,1 */
  431. " addi %0,%0,1 \n"
  432. " stwcx. %0,0,%3 \n"
  433. " beq $+12 \n" /* branch to lwsync/isync */
  434. " li %1,1 \n"
  435. " b $+12 \n" /* branch to end of asm sequence */
  436. #ifdef USE_PPC_LWSYNC
  437. " lwsync \n"
  438. #else
  439. " isync \n"
  440. #endif
  441. " li %1,0 \n"
  442. : "=&b"(_t), "=r"(_res), "+m"(*lock)
  443. : "r"(lock)
  444. : "memory", "cc");
  445. return _res;
  446. }
  447. /*
  448. * PowerPC S_UNLOCK is almost standard but requires a "sync" instruction.
  449. * On newer machines, we can use lwsync instead for better performance.
  450. */
  451. #ifdef USE_PPC_LWSYNC
  452. #define S_UNLOCK(lock) \
  453. do \
  454. { \
  455. __asm__ __volatile__ (" lwsync \n" ::: "memory"); \
  456. *((volatile slock_t *) (lock)) = 0; \
  457. } while (0)
  458. #else
  459. #define S_UNLOCK(lock) \
  460. do \
  461. { \
  462. __asm__ __volatile__ (" sync \n" ::: "memory"); \
  463. *((volatile slock_t *) (lock)) = 0; \
  464. } while (0)
  465. #endif /* USE_PPC_LWSYNC */
  466. #endif /* powerpc */
  467. /* Linux Motorola 68k */
  468. #if (defined(__mc68000__) || defined(__m68k__)) && defined(__linux__)
  469. #define HAS_TEST_AND_SET
  470. typedef unsigned char slock_t;
  471. #define TAS(lock) tas(lock)
  472. static __inline__ int
  473. tas(volatile slock_t *lock)
  474. {
  475. register int rv;
  476. __asm__ __volatile__(
  477. " clrl %0 \n"
  478. " tas %1 \n"
  479. " sne %0 \n"
  480. : "=d"(rv), "+m"(*lock)
  481. : /* no inputs */
  482. : "memory", "cc");
  483. return rv;
  484. }
  485. #endif /* (__mc68000__ || __m68k__) && __linux__ */
  486. /* Motorola 88k */
  487. #if defined(__m88k__)
  488. #define HAS_TEST_AND_SET
  489. typedef unsigned int slock_t;
  490. #define TAS(lock) tas(lock)
  491. static __inline__ int
  492. tas(volatile slock_t *lock)
  493. {
  494. register slock_t _res = 1;
  495. __asm__ __volatile__(
  496. " xmem %0, %2, %%r0 \n"
  497. : "+r"(_res), "+m"(*lock)
  498. : "r"(lock)
  499. : "memory");
  500. return (int) _res;
  501. }
  502. #endif /* __m88k__ */
  503. /*
  504. * VAXen -- even multiprocessor ones
  505. * (thanks to Tom Ivar Helbekkmo)
  506. */
  507. #if defined(__vax__)
  508. #define HAS_TEST_AND_SET
  509. typedef unsigned char slock_t;
  510. #define TAS(lock) tas(lock)
  511. static __inline__ int
  512. tas(volatile slock_t *lock)
  513. {
  514. register int _res;
  515. __asm__ __volatile__(
  516. " movl $1, %0 \n"
  517. " bbssi $0, (%2), 1f \n"
  518. " clrl %0 \n"
  519. "1: \n"
  520. : "=&r"(_res), "+m"(*lock)
  521. : "r"(lock)
  522. : "memory");
  523. return _res;
  524. }
  525. #endif /* __vax__ */
  526. #if defined(__mips__) && !defined(__sgi) /* non-SGI MIPS */
  527. #define HAS_TEST_AND_SET
  528. typedef unsigned int slock_t;
  529. #define TAS(lock) tas(lock)
  530. /*
  531. * Original MIPS-I processors lacked the LL/SC instructions, but if we are
  532. * so unfortunate as to be running on one of those, we expect that the kernel
  533. * will handle the illegal-instruction traps and emulate them for us. On
  534. * anything newer (and really, MIPS-I is extinct) LL/SC is the only sane
  535. * choice because any other synchronization method must involve a kernel
  536. * call. Unfortunately, many toolchains still default to MIPS-I as the
  537. * codegen target; if the symbol __mips shows that that's the case, we
  538. * have to force the assembler to accept LL/SC.
  539. *
  540. * R10000 and up processors require a separate SYNC, which has the same
  541. * issues as LL/SC.
  542. */
  543. #if __mips < 2
  544. #define MIPS_SET_MIPS2 " .set mips2 \n"
  545. #else
  546. #define MIPS_SET_MIPS2
  547. #endif
  548. static __inline__ int
  549. tas(volatile slock_t *lock)
  550. {
  551. register volatile slock_t *_l = lock;
  552. register int _res;
  553. register int _tmp;
  554. __asm__ __volatile__(
  555. " .set push \n"
  556. MIPS_SET_MIPS2
  557. " .set noreorder \n"
  558. " .set nomacro \n"
  559. " ll %0, %2 \n"
  560. " or %1, %0, 1 \n"
  561. " sc %1, %2 \n"
  562. " xori %1, 1 \n"
  563. " or %0, %0, %1 \n"
  564. " sync \n"
  565. " .set pop "
  566. : "=&r" (_res), "=&r" (_tmp), "+R" (*_l)
  567. : /* no inputs */
  568. : "memory");
  569. return _res;
  570. }
  571. /* MIPS S_UNLOCK is almost standard but requires a "sync" instruction */
  572. #define S_UNLOCK(lock) \
  573. do \
  574. { \
  575. __asm__ __volatile__( \
  576. " .set push \n" \
  577. MIPS_SET_MIPS2 \
  578. " .set noreorder \n" \
  579. " .set nomacro \n" \
  580. " sync \n" \
  581. " .set pop " \
  582. : /* no outputs */ \
  583. : /* no inputs */ \
  584. : "memory"); \
  585. *((volatile slock_t *) (lock)) = 0; \
  586. } while (0)
  587. #endif /* __mips__ && !__sgi */
  588. #if defined(__m32r__) && defined(HAVE_SYS_TAS_H) /* Renesas' M32R */
  589. #define HAS_TEST_AND_SET
  590. #include <sys/tas.h>
  591. typedef int slock_t;
  592. #define TAS(lock) tas(lock)
  593. #endif /* __m32r__ */
  594. #if defined(__sh__) /* Renesas' SuperH */
  595. #define HAS_TEST_AND_SET
  596. typedef unsigned char slock_t;
  597. #define TAS(lock) tas(lock)
  598. static __inline__ int
  599. tas(volatile slock_t *lock)
  600. {
  601. register int _res;
  602. /*
  603. * This asm is coded as if %0 could be any register, but actually SuperH
  604. * restricts the target of xor-immediate to be R0. That's handled by
  605. * the "z" constraint on _res.
  606. */
  607. __asm__ __volatile__(
  608. " tas.b @%2 \n"
  609. " movt %0 \n"
  610. " xor #1,%0 \n"
  611. : "=z"(_res), "+m"(*lock)
  612. : "r"(lock)
  613. : "memory", "t");
  614. return _res;
  615. }
  616. #endif /* __sh__ */
  617. /* These live in s_lock.c, but only for gcc */
  618. #if defined(__m68k__) && !defined(__linux__) /* non-Linux Motorola 68k */
  619. #define HAS_TEST_AND_SET
  620. typedef unsigned char slock_t;
  621. #endif
  622. /*
  623. * If we have no platform-specific knowledge, but we found that the compiler
  624. * provides __sync_lock_test_and_set(), use that. Prefer the int-width
  625. * version over the char-width version if we have both, on the rather dubious
  626. * grounds that that's known to be more likely to work in the ARM ecosystem.
  627. * (But we dealt with ARM above.)
  628. */
  629. #if !defined(HAS_TEST_AND_SET)
  630. #if defined(HAVE_GCC__SYNC_INT32_TAS)
  631. #define HAS_TEST_AND_SET
  632. #define TAS(lock) tas(lock)
  633. typedef int slock_t;
  634. static __inline__ int
  635. tas(volatile slock_t *lock)
  636. {
  637. return __sync_lock_test_and_set(lock, 1);
  638. }
  639. #define S_UNLOCK(lock) __sync_lock_release(lock)
  640. #elif defined(HAVE_GCC__SYNC_CHAR_TAS)
  641. #define HAS_TEST_AND_SET
  642. #define TAS(lock) tas(lock)
  643. typedef char slock_t;
  644. static __inline__ int
  645. tas(volatile slock_t *lock)
  646. {
  647. return __sync_lock_test_and_set(lock, 1);
  648. }
  649. #define S_UNLOCK(lock) __sync_lock_release(lock)
  650. #endif /* HAVE_GCC__SYNC_INT32_TAS */
  651. #endif /* !defined(HAS_TEST_AND_SET) */
  652. /*
  653. * Default implementation of S_UNLOCK() for gcc/icc.
  654. *
  655. * Note that this implementation is unsafe for any platform that can reorder
  656. * a memory access (either load or store) after a following store. That
  657. * happens not to be possible on x86 and most legacy architectures (some are
  658. * single-processor!), but many modern systems have weaker memory ordering.
  659. * Those that do must define their own version of S_UNLOCK() rather than
  660. * relying on this one.
  661. */
  662. #if !defined(S_UNLOCK)
  663. #define S_UNLOCK(lock) \
  664. do { __asm__ __volatile__("" : : : "memory"); *(lock) = 0; } while (0)
  665. #endif
  666. #endif /* defined(__GNUC__) || defined(__INTEL_COMPILER) */
  667. /*
  668. * ---------------------------------------------------------------------
  669. * Platforms that use non-gcc inline assembly:
  670. * ---------------------------------------------------------------------
  671. */
  672. #if !defined(HAS_TEST_AND_SET) /* We didn't trigger above, let's try here */
  673. #if defined(__hppa) || defined(__hppa__) /* HP PA-RISC, GCC and HP compilers */
  674. /*
  675. * HP's PA-RISC
  676. *
  677. * See src/backend/port/hpux/tas.c.template for details about LDCWX. Because
  678. * LDCWX requires a 16-byte-aligned address, we declare slock_t as a 16-byte
  679. * struct. The active word in the struct is whichever has the aligned address;
  680. * the other three words just sit at -1.
  681. *
  682. * When using gcc, we can inline the required assembly code.
  683. */
  684. #define HAS_TEST_AND_SET
  685. typedef struct
  686. {
  687. int sema[4];
  688. } slock_t;
  689. #define TAS_ACTIVE_WORD(lock) ((volatile int *) (((uintptr_t) (lock) + 15) & ~15))
  690. #if defined(__GNUC__)
  691. static __inline__ int
  692. tas(volatile slock_t *lock)
  693. {
  694. volatile int *lockword = TAS_ACTIVE_WORD(lock);
  695. register int lockval;
  696. __asm__ __volatile__(
  697. " ldcwx 0(0,%2),%0 \n"
  698. : "=r"(lockval), "+m"(*lockword)
  699. : "r"(lockword)
  700. : "memory");
  701. return (lockval == 0);
  702. }
  703. /*
  704. * The hppa implementation doesn't follow the rules of this files and provides
  705. * a gcc specific implementation outside of the above defined(__GNUC__). It
  706. * does so to avoid duplication between the HP compiler and gcc. So undefine
  707. * the generic fallback S_UNLOCK from above.
  708. */
  709. #ifdef S_UNLOCK
  710. #undef S_UNLOCK
  711. #endif
  712. #define S_UNLOCK(lock) \
  713. do { \
  714. __asm__ __volatile__("" : : : "memory"); \
  715. *TAS_ACTIVE_WORD(lock) = -1; \
  716. } while (0)
  717. #endif /* __GNUC__ */
  718. #define S_INIT_LOCK(lock) \
  719. do { \
  720. volatile slock_t *lock_ = (lock); \
  721. lock_->sema[0] = -1; \
  722. lock_->sema[1] = -1; \
  723. lock_->sema[2] = -1; \
  724. lock_->sema[3] = -1; \
  725. } while (0)
  726. #define S_LOCK_FREE(lock) (*TAS_ACTIVE_WORD(lock) != 0)
  727. #endif /* __hppa || __hppa__ */
  728. #if defined(__hpux) && defined(__ia64) && !defined(__GNUC__)
  729. /*
  730. * HP-UX on Itanium, non-gcc/icc compiler
  731. *
  732. * We assume that the compiler enforces strict ordering of loads/stores on
  733. * volatile data (see comments on the gcc-version earlier in this file).
  734. * Note that this assumption does *not* hold if you use the
  735. * +Ovolatile=__unordered option on the HP-UX compiler, so don't do that.
  736. *
  737. * See also Implementing Spinlocks on the Intel Itanium Architecture and
  738. * PA-RISC, by Tor Ekqvist and David Graves, for more information. As of
  739. * this writing, version 1.0 of the manual is available at:
  740. * http://h21007.www2.hp.com/portal/download/files/unprot/itanium/spinlocks.pdf
  741. */
  742. #define HAS_TEST_AND_SET
  743. typedef unsigned int slock_t;
  744. #include <ia64/sys/inline.h>
  745. #define TAS(lock) _Asm_xchg(_SZ_W, lock, 1, _LDHINT_NONE)
  746. /* On IA64, it's a win to use a non-locking test before the xchg proper */
  747. #define TAS_SPIN(lock) (*(lock) ? 1 : TAS(lock))
  748. #define S_UNLOCK(lock) \
  749. do { _Asm_mf(); (*(lock)) = 0; } while (0)
  750. #endif /* HPUX on IA64, non gcc/icc */
  751. #if defined(_AIX) /* AIX */
  752. /*
  753. * AIX (POWER)
  754. */
  755. #define HAS_TEST_AND_SET
  756. #include <sys/atomic_op.h>
  757. typedef int slock_t;
  758. #define TAS(lock) _check_lock((slock_t *) (lock), 0, 1)
  759. #define S_UNLOCK(lock) _clear_lock((slock_t *) (lock), 0)
  760. #endif /* _AIX */
  761. /* These are in sunstudio_(sparc|x86).s */
  762. #if defined(__SUNPRO_C) && (defined(__i386) || defined(__x86_64__) || defined(__sparc__) || defined(__sparc))
  763. #define HAS_TEST_AND_SET
  764. #if defined(__i386) || defined(__x86_64__) || defined(__sparcv9) || defined(__sparcv8plus)
  765. typedef unsigned int slock_t;
  766. #else
  767. typedef unsigned char slock_t;
  768. #endif
  769. extern slock_t pg_atomic_cas(volatile slock_t *lock, slock_t with,
  770. slock_t cmp);
  771. #define TAS(a) (pg_atomic_cas((a), 1, 0) != 0)
  772. #endif
  773. #ifdef _MSC_VER
  774. typedef LONG slock_t;
  775. #define HAS_TEST_AND_SET
  776. #define TAS(lock) (InterlockedCompareExchange(lock, 1, 0))
  777. #define SPIN_DELAY() spin_delay()
  778. /* If using Visual C++ on Win64, inline assembly is unavailable.
  779. * Use a _mm_pause intrinsic instead of rep nop.
  780. */
  781. #if defined(_WIN64)
  782. static __forceinline void
  783. spin_delay(void)
  784. {
  785. _mm_pause();
  786. }
  787. #else
  788. static __forceinline void
  789. spin_delay(void)
  790. {
  791. /* See comment for gcc code. Same code, MASM syntax */
  792. __asm rep nop;
  793. }
  794. #endif
  795. #include <intrin.h>
  796. #pragma intrinsic(_ReadWriteBarrier)
  797. #define S_UNLOCK(lock) \
  798. do { _ReadWriteBarrier(); (*(lock)) = 0; } while (0)
  799. #endif
  800. #endif /* !defined(HAS_TEST_AND_SET) */
  801. /* Blow up if we didn't have any way to do spinlocks */
  802. #ifndef HAS_TEST_AND_SET
  803. #error PostgreSQL does not have native spinlock support on this platform. To continue the compilation, rerun configure using --disable-spinlocks. However, performance will be poor. Please report this to [email protected].
  804. #endif
  805. #else /* !HAVE_SPINLOCKS */
  806. /*
  807. * Fake spinlock implementation using semaphores --- slow and prone
  808. * to fall foul of kernel limits on number of semaphores, so don't use this
  809. * unless you must! The subroutines appear in spin.c.
  810. */
  811. typedef int slock_t;
  812. extern bool s_lock_free_sema(volatile slock_t *lock);
  813. extern void s_unlock_sema(volatile slock_t *lock);
  814. extern void s_init_lock_sema(volatile slock_t *lock, bool nested);
  815. extern int tas_sema(volatile slock_t *lock);
  816. #define S_LOCK_FREE(lock) s_lock_free_sema(lock)
  817. #define S_UNLOCK(lock) s_unlock_sema(lock)
  818. #define S_INIT_LOCK(lock) s_init_lock_sema(lock, false)
  819. #define TAS(lock) tas_sema(lock)
  820. #endif /* HAVE_SPINLOCKS */
  821. /*
  822. * Default Definitions - override these above as needed.
  823. */
  824. #if !defined(S_LOCK)
  825. #define S_LOCK(lock) \
  826. (TAS(lock) ? s_lock((lock), __FILE__, __LINE__, PG_FUNCNAME_MACRO) : 0)
  827. #endif /* S_LOCK */
  828. #if !defined(S_LOCK_FREE)
  829. #define S_LOCK_FREE(lock) (*(lock) == 0)
  830. #endif /* S_LOCK_FREE */
  831. #if !defined(S_UNLOCK)
  832. /*
  833. * Our default implementation of S_UNLOCK is essentially *(lock) = 0. This
  834. * is unsafe if the platform can reorder a memory access (either load or
  835. * store) after a following store; platforms where this is possible must
  836. * define their own S_UNLOCK. But CPU reordering is not the only concern:
  837. * if we simply defined S_UNLOCK() as an inline macro, the compiler might
  838. * reorder instructions from inside the critical section to occur after the
  839. * lock release. Since the compiler probably can't know what the external
  840. * function s_unlock is doing, putting the same logic there should be adequate.
  841. * A sufficiently-smart globally optimizing compiler could break that
  842. * assumption, though, and the cost of a function call for every spinlock
  843. * release may hurt performance significantly, so we use this implementation
  844. * only for platforms where we don't know of a suitable intrinsic. For the
  845. * most part, those are relatively obscure platform/compiler combinations to
  846. * which the PostgreSQL project does not have access.
  847. */
  848. #define USE_DEFAULT_S_UNLOCK
  849. extern void s_unlock(volatile slock_t *lock);
  850. #define S_UNLOCK(lock) s_unlock(lock)
  851. #endif /* S_UNLOCK */
  852. #if !defined(S_INIT_LOCK)
  853. #define S_INIT_LOCK(lock) S_UNLOCK(lock)
  854. #endif /* S_INIT_LOCK */
  855. #if !defined(SPIN_DELAY)
  856. #define SPIN_DELAY() ((void) 0)
  857. #endif /* SPIN_DELAY */
  858. #if !defined(TAS)
  859. extern int tas(volatile slock_t *lock); /* in port/.../tas.s, or
  860. * s_lock.c */
  861. #define TAS(lock) tas(lock)
  862. #endif /* TAS */
  863. #if !defined(TAS_SPIN)
  864. #define TAS_SPIN(lock) TAS(lock)
  865. #endif /* TAS_SPIN */
  866. extern PGDLLIMPORT slock_t dummy_spinlock;
  867. /*
  868. * Platform-independent out-of-line support routines
  869. */
  870. extern int s_lock(volatile slock_t *lock, const char *file, int line, const char *func);
  871. /* Support for dynamic adjustment of spins_per_delay */
  872. #define DEFAULT_SPINS_PER_DELAY 100
  873. extern void set_spins_per_delay(int shared_spins_per_delay);
  874. extern int update_spins_per_delay(int shared_spins_per_delay);
  875. /*
  876. * Support for spin delay which is useful in various places where
  877. * spinlock-like procedures take place.
  878. */
  879. typedef struct
  880. {
  881. int spins;
  882. int delays;
  883. int cur_delay;
  884. const char *file;
  885. int line;
  886. const char *func;
  887. } SpinDelayStatus;
  888. static inline void
  889. init_spin_delay(SpinDelayStatus *status,
  890. const char *file, int line, const char *func)
  891. {
  892. status->spins = 0;
  893. status->delays = 0;
  894. status->cur_delay = 0;
  895. status->file = file;
  896. status->line = line;
  897. status->func = func;
  898. }
  899. #define init_local_spin_delay(status) init_spin_delay(status, __FILE__, __LINE__, PG_FUNCNAME_MACRO)
  900. extern void perform_spin_delay(SpinDelayStatus *status);
  901. extern void finish_spin_delay(SpinDelayStatus *status);
  902. #endif /* S_LOCK_H */