s_lock.h 28 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024
  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-2016, 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. #endif /* HAVE_GCC__SYNC_INT32_TAS */
  295. #endif /* __arm__ || __arm || __aarch64__ || __aarch64 */
  296. /* S/390 and S/390x Linux (32- and 64-bit zSeries) */
  297. #if defined(__s390__) || defined(__s390x__)
  298. #define HAS_TEST_AND_SET
  299. typedef unsigned int slock_t;
  300. #define TAS(lock) tas(lock)
  301. static __inline__ int
  302. tas(volatile slock_t *lock)
  303. {
  304. int _res = 0;
  305. __asm__ __volatile__(
  306. " cs %0,%3,0(%2) \n"
  307. : "+d"(_res), "+m"(*lock)
  308. : "a"(lock), "d"(1)
  309. : "memory", "cc");
  310. return _res;
  311. }
  312. #endif /* __s390__ || __s390x__ */
  313. #if defined(__sparc__) /* Sparc */
  314. /*
  315. * Solaris has always run sparc processors in TSO (total store) mode, but
  316. * linux didn't use to and the *BSDs still don't. So, be careful about
  317. * acquire/release semantics. The CPU will treat superfluous membars as
  318. * NOPs, so it's just code space.
  319. */
  320. #define HAS_TEST_AND_SET
  321. typedef unsigned char slock_t;
  322. #define TAS(lock) tas(lock)
  323. static __inline__ int
  324. tas(volatile slock_t *lock)
  325. {
  326. register slock_t _res;
  327. /*
  328. * See comment in /pg/backend/port/tas/solaris_sparc.s for why this
  329. * uses "ldstub", and that file uses "cas". gcc currently generates
  330. * sparcv7-targeted binaries, so "cas" use isn't possible.
  331. */
  332. __asm__ __volatile__(
  333. " ldstub [%2], %0 \n"
  334. : "=r"(_res), "+m"(*lock)
  335. : "r"(lock)
  336. : "memory");
  337. #if defined(__sparcv7) || defined(__sparc_v7__)
  338. /*
  339. * No stbar or membar available, luckily no actually produced hardware
  340. * requires a barrier.
  341. */
  342. #elif defined(__sparcv8) || defined(__sparc_v8__)
  343. /* stbar is available (and required for both PSO, RMO), membar isn't */
  344. __asm__ __volatile__ ("stbar \n":::"memory");
  345. #else
  346. /*
  347. * #LoadStore (RMO) | #LoadLoad (RMO) together are the appropriate acquire
  348. * barrier for sparcv8+ upwards.
  349. */
  350. __asm__ __volatile__ ("membar #LoadStore | #LoadLoad \n":::"memory");
  351. #endif
  352. return (int) _res;
  353. }
  354. #if defined(__sparcv7) || defined(__sparc_v7__)
  355. /*
  356. * No stbar or membar available, luckily no actually produced hardware
  357. * requires a barrier. We fall through to the default gcc definition of
  358. * S_UNLOCK in this case.
  359. */
  360. #elif defined(__sparcv8) || defined(__sparc_v8__)
  361. /* stbar is available (and required for both PSO, RMO), membar isn't */
  362. #define S_UNLOCK(lock) \
  363. do \
  364. { \
  365. __asm__ __volatile__ ("stbar \n":::"memory"); \
  366. *((volatile slock_t *) (lock)) = 0; \
  367. } while (0)
  368. #else
  369. /*
  370. * #LoadStore (RMO) | #StoreStore (RMO, PSO) together are the appropriate
  371. * release barrier for sparcv8+ upwards.
  372. */
  373. #define S_UNLOCK(lock) \
  374. do \
  375. { \
  376. __asm__ __volatile__ ("membar #LoadStore | #StoreStore \n":::"memory"); \
  377. *((volatile slock_t *) (lock)) = 0; \
  378. } while (0)
  379. #endif
  380. #endif /* __sparc__ */
  381. /* PowerPC */
  382. #if defined(__ppc__) || defined(__powerpc__) || defined(__ppc64__) || defined(__powerpc64__)
  383. #define HAS_TEST_AND_SET
  384. typedef unsigned int slock_t;
  385. #define TAS(lock) tas(lock)
  386. /* On PPC, it's a win to use a non-locking test before the lwarx */
  387. #define TAS_SPIN(lock) (*(lock) ? 1 : TAS(lock))
  388. /*
  389. * NOTE: per the Enhanced PowerPC Architecture manual, v1.0 dated 7-May-2002,
  390. * an isync is a sufficient synchronization barrier after a lwarx/stwcx loop.
  391. * On newer machines, we can use lwsync instead for better performance.
  392. *
  393. * Ordinarily, we'd code the branches here using GNU-style local symbols, that
  394. * is "1f" referencing "1:" and so on. But some people run gcc on AIX with
  395. * IBM's assembler as backend, and IBM's assembler doesn't do local symbols.
  396. * So hand-code the branch offsets; fortunately, all PPC instructions are
  397. * exactly 4 bytes each, so it's not too hard to count.
  398. */
  399. static __inline__ int
  400. tas(volatile slock_t *lock)
  401. {
  402. slock_t _t;
  403. int _res;
  404. __asm__ __volatile__(
  405. #ifdef USE_PPC_LWARX_MUTEX_HINT
  406. " lwarx %0,0,%3,1 \n"
  407. #else
  408. " lwarx %0,0,%3 \n"
  409. #endif
  410. " cmpwi %0,0 \n"
  411. " bne $+16 \n" /* branch to li %1,1 */
  412. " addi %0,%0,1 \n"
  413. " stwcx. %0,0,%3 \n"
  414. " beq $+12 \n" /* branch to lwsync/isync */
  415. " li %1,1 \n"
  416. " b $+12 \n" /* branch to end of asm sequence */
  417. #ifdef USE_PPC_LWSYNC
  418. " lwsync \n"
  419. #else
  420. " isync \n"
  421. #endif
  422. " li %1,0 \n"
  423. : "=&r"(_t), "=r"(_res), "+m"(*lock)
  424. : "r"(lock)
  425. : "memory", "cc");
  426. return _res;
  427. }
  428. /*
  429. * PowerPC S_UNLOCK is almost standard but requires a "sync" instruction.
  430. * On newer machines, we can use lwsync instead for better performance.
  431. */
  432. #ifdef USE_PPC_LWSYNC
  433. #define S_UNLOCK(lock) \
  434. do \
  435. { \
  436. __asm__ __volatile__ (" lwsync \n" ::: "memory"); \
  437. *((volatile slock_t *) (lock)) = 0; \
  438. } while (0)
  439. #else
  440. #define S_UNLOCK(lock) \
  441. do \
  442. { \
  443. __asm__ __volatile__ (" sync \n" ::: "memory"); \
  444. *((volatile slock_t *) (lock)) = 0; \
  445. } while (0)
  446. #endif /* USE_PPC_LWSYNC */
  447. #endif /* powerpc */
  448. /* Linux Motorola 68k */
  449. #if (defined(__mc68000__) || defined(__m68k__)) && defined(__linux__)
  450. #define HAS_TEST_AND_SET
  451. typedef unsigned char slock_t;
  452. #define TAS(lock) tas(lock)
  453. static __inline__ int
  454. tas(volatile slock_t *lock)
  455. {
  456. register int rv;
  457. __asm__ __volatile__(
  458. " clrl %0 \n"
  459. " tas %1 \n"
  460. " sne %0 \n"
  461. : "=d"(rv), "+m"(*lock)
  462. : /* no inputs */
  463. : "memory", "cc");
  464. return rv;
  465. }
  466. #endif /* (__mc68000__ || __m68k__) && __linux__ */
  467. /*
  468. * VAXen -- even multiprocessor ones
  469. * (thanks to Tom Ivar Helbekkmo)
  470. */
  471. #if defined(__vax__)
  472. #define HAS_TEST_AND_SET
  473. typedef unsigned char slock_t;
  474. #define TAS(lock) tas(lock)
  475. static __inline__ int
  476. tas(volatile slock_t *lock)
  477. {
  478. register int _res;
  479. __asm__ __volatile__(
  480. " movl $1, %0 \n"
  481. " bbssi $0, (%2), 1f \n"
  482. " clrl %0 \n"
  483. "1: \n"
  484. : "=&r"(_res), "+m"(*lock)
  485. : "r"(lock)
  486. : "memory");
  487. return _res;
  488. }
  489. #endif /* __vax__ */
  490. #if defined(__mips__) && !defined(__sgi) /* non-SGI MIPS */
  491. /* Note: on SGI we use the OS' mutex ABI, see below */
  492. /* Note: R10000 processors require a separate SYNC */
  493. #define HAS_TEST_AND_SET
  494. typedef unsigned int slock_t;
  495. #define TAS(lock) tas(lock)
  496. static __inline__ int
  497. tas(volatile slock_t *lock)
  498. {
  499. register volatile slock_t *_l = lock;
  500. register int _res;
  501. register int _tmp;
  502. __asm__ __volatile__(
  503. " .set push \n"
  504. " .set mips2 \n"
  505. " .set noreorder \n"
  506. " .set nomacro \n"
  507. " ll %0, %2 \n"
  508. " or %1, %0, 1 \n"
  509. " sc %1, %2 \n"
  510. " xori %1, 1 \n"
  511. " or %0, %0, %1 \n"
  512. " sync \n"
  513. " .set pop "
  514. : "=&r" (_res), "=&r" (_tmp), "+R" (*_l)
  515. : /* no inputs */
  516. : "memory");
  517. return _res;
  518. }
  519. /* MIPS S_UNLOCK is almost standard but requires a "sync" instruction */
  520. #define S_UNLOCK(lock) \
  521. do \
  522. { \
  523. __asm__ __volatile__( \
  524. " .set push \n" \
  525. " .set mips2 \n" \
  526. " .set noreorder \n" \
  527. " .set nomacro \n" \
  528. " sync \n" \
  529. " .set pop " \
  530. : /* no outputs */ \
  531. : /* no inputs */ \
  532. : "memory"); \
  533. *((volatile slock_t *) (lock)) = 0; \
  534. } while (0)
  535. #endif /* __mips__ && !__sgi */
  536. #if defined(__m32r__) && defined(HAVE_SYS_TAS_H) /* Renesas' M32R */
  537. #define HAS_TEST_AND_SET
  538. #include <sys/tas.h>
  539. typedef int slock_t;
  540. #define TAS(lock) tas(lock)
  541. #endif /* __m32r__ */
  542. #if defined(__sh__) /* Renesas' SuperH */
  543. #define HAS_TEST_AND_SET
  544. typedef unsigned char slock_t;
  545. #define TAS(lock) tas(lock)
  546. static __inline__ int
  547. tas(volatile slock_t *lock)
  548. {
  549. register int _res;
  550. /*
  551. * This asm is coded as if %0 could be any register, but actually SuperH
  552. * restricts the target of xor-immediate to be R0. That's handled by
  553. * the "z" constraint on _res.
  554. */
  555. __asm__ __volatile__(
  556. " tas.b @%2 \n"
  557. " movt %0 \n"
  558. " xor #1,%0 \n"
  559. : "=z"(_res), "+m"(*lock)
  560. : "r"(lock)
  561. : "memory", "t");
  562. return _res;
  563. }
  564. #endif /* __sh__ */
  565. /* These live in s_lock.c, but only for gcc */
  566. #if defined(__m68k__) && !defined(__linux__) /* non-Linux Motorola 68k */
  567. #define HAS_TEST_AND_SET
  568. typedef unsigned char slock_t;
  569. #endif
  570. /*
  571. * Default implementation of S_UNLOCK() for gcc/icc.
  572. *
  573. * Note that this implementation is unsafe for any platform that can reorder
  574. * a memory access (either load or store) after a following store. That
  575. * happens not to be possible on x86 and most legacy architectures (some are
  576. * single-processor!), but many modern systems have weaker memory ordering.
  577. * Those that do must define their own version of S_UNLOCK() rather than
  578. * relying on this one.
  579. */
  580. #if !defined(S_UNLOCK)
  581. #define S_UNLOCK(lock) \
  582. do { __asm__ __volatile__("" : : : "memory"); *(lock) = 0; } while (0)
  583. #endif
  584. #endif /* defined(__GNUC__) || defined(__INTEL_COMPILER) */
  585. /*
  586. * ---------------------------------------------------------------------
  587. * Platforms that use non-gcc inline assembly:
  588. * ---------------------------------------------------------------------
  589. */
  590. #if !defined(HAS_TEST_AND_SET) /* We didn't trigger above, let's try here */
  591. #if defined(USE_UNIVEL_CC) /* Unixware compiler */
  592. #define HAS_TEST_AND_SET
  593. typedef unsigned char slock_t;
  594. #define TAS(lock) tas(lock)
  595. asm int
  596. tas(volatile slock_t *s_lock)
  597. {
  598. /* UNIVEL wants %mem in column 1, so we don't pgindent this file */
  599. %mem s_lock
  600. pushl %ebx
  601. movl s_lock, %ebx
  602. movl $255, %eax
  603. lock
  604. xchgb %al, (%ebx)
  605. popl %ebx
  606. }
  607. #endif /* defined(USE_UNIVEL_CC) */
  608. #if defined(__hppa) || defined(__hppa__) /* HP PA-RISC, GCC and HP compilers */
  609. /*
  610. * HP's PA-RISC
  611. *
  612. * See src/backend/port/hpux/tas.c.template for details about LDCWX. Because
  613. * LDCWX requires a 16-byte-aligned address, we declare slock_t as a 16-byte
  614. * struct. The active word in the struct is whichever has the aligned address;
  615. * the other three words just sit at -1.
  616. *
  617. * When using gcc, we can inline the required assembly code.
  618. */
  619. #define HAS_TEST_AND_SET
  620. typedef struct
  621. {
  622. int sema[4];
  623. } slock_t;
  624. #define TAS_ACTIVE_WORD(lock) ((volatile int *) (((uintptr_t) (lock) + 15) & ~15))
  625. #if defined(__GNUC__)
  626. static __inline__ int
  627. tas(volatile slock_t *lock)
  628. {
  629. volatile int *lockword = TAS_ACTIVE_WORD(lock);
  630. register int lockval;
  631. __asm__ __volatile__(
  632. " ldcwx 0(0,%2),%0 \n"
  633. : "=r"(lockval), "+m"(*lockword)
  634. : "r"(lockword)
  635. : "memory");
  636. return (lockval == 0);
  637. }
  638. /*
  639. * The hppa implementation doesn't follow the rules of this files and provides
  640. * a gcc specific implementation outside of the above defined(__GNUC__). It
  641. * does so to avoid duplication between the HP compiler and gcc. So undefine
  642. * the generic fallback S_UNLOCK from above.
  643. */
  644. #ifdef S_UNLOCK
  645. #undef S_UNLOCK
  646. #endif
  647. #define S_UNLOCK(lock) \
  648. do { \
  649. __asm__ __volatile__("" : : : "memory"); \
  650. *TAS_ACTIVE_WORD(lock) = -1; \
  651. } while (0)
  652. #endif /* __GNUC__ */
  653. #define S_INIT_LOCK(lock) \
  654. do { \
  655. volatile slock_t *lock_ = (lock); \
  656. lock_->sema[0] = -1; \
  657. lock_->sema[1] = -1; \
  658. lock_->sema[2] = -1; \
  659. lock_->sema[3] = -1; \
  660. } while (0)
  661. #define S_LOCK_FREE(lock) (*TAS_ACTIVE_WORD(lock) != 0)
  662. #endif /* __hppa || __hppa__ */
  663. #if defined(__hpux) && defined(__ia64) && !defined(__GNUC__)
  664. /*
  665. * HP-UX on Itanium, non-gcc/icc compiler
  666. *
  667. * We assume that the compiler enforces strict ordering of loads/stores on
  668. * volatile data (see comments on the gcc-version earlier in this file).
  669. * Note that this assumption does *not* hold if you use the
  670. * +Ovolatile=__unordered option on the HP-UX compiler, so don't do that.
  671. *
  672. * See also Implementing Spinlocks on the Intel Itanium Architecture and
  673. * PA-RISC, by Tor Ekqvist and David Graves, for more information. As of
  674. * this writing, version 1.0 of the manual is available at:
  675. * http://h21007.www2.hp.com/portal/download/files/unprot/itanium/spinlocks.pdf
  676. */
  677. #define HAS_TEST_AND_SET
  678. typedef unsigned int slock_t;
  679. #include <ia64/sys/inline.h>
  680. #define TAS(lock) _Asm_xchg(_SZ_W, lock, 1, _LDHINT_NONE)
  681. /* On IA64, it's a win to use a non-locking test before the xchg proper */
  682. #define TAS_SPIN(lock) (*(lock) ? 1 : TAS(lock))
  683. #define S_UNLOCK(lock) \
  684. do { _Asm_mf(); (*(lock)) = 0; } while (0)
  685. #endif /* HPUX on IA64, non gcc/icc */
  686. #if defined(_AIX) /* AIX */
  687. /*
  688. * AIX (POWER)
  689. */
  690. #define HAS_TEST_AND_SET
  691. #include <sys/atomic_op.h>
  692. typedef int slock_t;
  693. #define TAS(lock) _check_lock((slock_t *) (lock), 0, 1)
  694. #define S_UNLOCK(lock) _clear_lock((slock_t *) (lock), 0)
  695. #endif /* _AIX */
  696. /* These are in sunstudio_(sparc|x86).s */
  697. #if defined(__SUNPRO_C) && (defined(__i386) || defined(__x86_64__) || defined(__sparc__) || defined(__sparc))
  698. #define HAS_TEST_AND_SET
  699. #if defined(__i386) || defined(__x86_64__) || defined(__sparcv9) || defined(__sparcv8plus)
  700. typedef unsigned int slock_t;
  701. #else
  702. typedef unsigned char slock_t;
  703. #endif
  704. extern slock_t pg_atomic_cas(volatile slock_t *lock, slock_t with,
  705. slock_t cmp);
  706. #define TAS(a) (pg_atomic_cas((a), 1, 0) != 0)
  707. #endif
  708. #ifdef WIN32_ONLY_COMPILER
  709. typedef LONG slock_t;
  710. #define HAS_TEST_AND_SET
  711. #define TAS(lock) (InterlockedCompareExchange(lock, 1, 0))
  712. #define SPIN_DELAY() spin_delay()
  713. /* If using Visual C++ on Win64, inline assembly is unavailable.
  714. * Use a _mm_pause intrinsic instead of rep nop.
  715. */
  716. #if defined(_WIN64)
  717. static __forceinline void
  718. spin_delay(void)
  719. {
  720. _mm_pause();
  721. }
  722. #else
  723. static __forceinline void
  724. spin_delay(void)
  725. {
  726. /* See comment for gcc code. Same code, MASM syntax */
  727. __asm rep nop;
  728. }
  729. #endif
  730. #include <intrin.h>
  731. #pragma intrinsic(_ReadWriteBarrier)
  732. #define S_UNLOCK(lock) \
  733. do { _ReadWriteBarrier(); (*(lock)) = 0; } while (0)
  734. #endif
  735. #endif /* !defined(HAS_TEST_AND_SET) */
  736. /* Blow up if we didn't have any way to do spinlocks */
  737. #ifndef HAS_TEST_AND_SET
  738. #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 pgsql-bugs@postgresql.org.
  739. #endif
  740. #else /* !HAVE_SPINLOCKS */
  741. /*
  742. * Fake spinlock implementation using semaphores --- slow and prone
  743. * to fall foul of kernel limits on number of semaphores, so don't use this
  744. * unless you must! The subroutines appear in spin.c.
  745. */
  746. typedef int slock_t;
  747. extern bool s_lock_free_sema(volatile slock_t *lock);
  748. extern void s_unlock_sema(volatile slock_t *lock);
  749. extern void s_init_lock_sema(volatile slock_t *lock, bool nested);
  750. extern int tas_sema(volatile slock_t *lock);
  751. #define S_LOCK_FREE(lock) s_lock_free_sema(lock)
  752. #define S_UNLOCK(lock) s_unlock_sema(lock)
  753. #define S_INIT_LOCK(lock) s_init_lock_sema(lock, false)
  754. #define TAS(lock) tas_sema(lock)
  755. #endif /* HAVE_SPINLOCKS */
  756. /*
  757. * Default Definitions - override these above as needed.
  758. */
  759. #if !defined(S_LOCK)
  760. #define S_LOCK(lock) \
  761. (TAS(lock) ? s_lock((lock), __FILE__, __LINE__, PG_FUNCNAME_MACRO) : 0)
  762. #endif /* S_LOCK */
  763. #if !defined(S_LOCK_FREE)
  764. #define S_LOCK_FREE(lock) (*(lock) == 0)
  765. #endif /* S_LOCK_FREE */
  766. #if !defined(S_UNLOCK)
  767. /*
  768. * Our default implementation of S_UNLOCK is essentially *(lock) = 0. This
  769. * is unsafe if the platform can reorder a memory access (either load or
  770. * store) after a following store; platforms where this is possible must
  771. * define their own S_UNLOCK. But CPU reordering is not the only concern:
  772. * if we simply defined S_UNLOCK() as an inline macro, the compiler might
  773. * reorder instructions from inside the critical section to occur after the
  774. * lock release. Since the compiler probably can't know what the external
  775. * function s_unlock is doing, putting the same logic there should be adequate.
  776. * A sufficiently-smart globally optimizing compiler could break that
  777. * assumption, though, and the cost of a function call for every spinlock
  778. * release may hurt performance significantly, so we use this implementation
  779. * only for platforms where we don't know of a suitable intrinsic. For the
  780. * most part, those are relatively obscure platform/compiler combinations to
  781. * which the PostgreSQL project does not have access.
  782. */
  783. #define USE_DEFAULT_S_UNLOCK
  784. extern void s_unlock(volatile slock_t *lock);
  785. #define S_UNLOCK(lock) s_unlock(lock)
  786. #endif /* S_UNLOCK */
  787. #if !defined(S_INIT_LOCK)
  788. #define S_INIT_LOCK(lock) S_UNLOCK(lock)
  789. #endif /* S_INIT_LOCK */
  790. #if !defined(SPIN_DELAY)
  791. #define SPIN_DELAY() ((void) 0)
  792. #endif /* SPIN_DELAY */
  793. #if !defined(TAS)
  794. extern int tas(volatile slock_t *lock); /* in port/.../tas.s, or
  795. * s_lock.c */
  796. #define TAS(lock) tas(lock)
  797. #endif /* TAS */
  798. #if !defined(TAS_SPIN)
  799. #define TAS_SPIN(lock) TAS(lock)
  800. #endif /* TAS_SPIN */
  801. extern slock_t dummy_spinlock;
  802. /*
  803. * Platform-independent out-of-line support routines
  804. */
  805. extern int s_lock(volatile slock_t *lock, const char *file, int line, const char *func);
  806. /* Support for dynamic adjustment of spins_per_delay */
  807. #define DEFAULT_SPINS_PER_DELAY 100
  808. extern void set_spins_per_delay(int shared_spins_per_delay);
  809. extern int update_spins_per_delay(int shared_spins_per_delay);
  810. /*
  811. * Support for spin delay which is useful in various places where
  812. * spinlock-like procedures take place.
  813. */
  814. typedef struct
  815. {
  816. int spins;
  817. int delays;
  818. int cur_delay;
  819. const char *file;
  820. int line;
  821. const char *func;
  822. } SpinDelayStatus;
  823. static inline void
  824. init_spin_delay(SpinDelayStatus *status,
  825. const char *file, int line, const char *func)
  826. {
  827. status->spins = 0;
  828. status->delays = 0;
  829. status->cur_delay = 0;
  830. status->file = file;
  831. status->line = line;
  832. status->func = func;
  833. }
  834. #define init_local_spin_delay(status) init_spin_delay(status, __FILE__, __LINE__, PG_FUNCNAME_MACRO)
  835. void perform_spin_delay(SpinDelayStatus *status);
  836. void finish_spin_delay(SpinDelayStatus *status);
  837. #endif /* S_LOCK_H */