Interpretation of PostgreSQL Source Code (218)-implementation of spinlock
This section describes the implementation of spinlock on different platforms (mainly X 86 64 and aarch74).
/ *-* * s_lock.h * Hardware-dependent implementation of spinlocks. ... I. realization
X86_64
TAS means Test And Set. Under the platform of X 86 and 64, the implementation of spinlock uses assembly language.
# ifdef _ x86 / 64 / * AMD Opteron, Intel EM64T * / # define HAS_TEST_AND_SETtypedef unsigned char slock_t;#define TAS (lock) tas (lock) / * * On Intel EM64T, it's a win to use a non-locking test before the xchg proper, * but only when spinning. * * See also Implementing Scalable Atomic Locks for Multi-Core Intel (tm) EM64T * and IA32, by Michael Chynoweth and Mary R. Lee. As of this writing, it is * available at: * http://software.intel.com/en-us/articles/implementing-scalable-atomic-locks-for-multi-core-intel-em64t-and-ia32-architectures * / # define TAS_SPIN (lock) (* (lock)? 1: TAS (lock)) static _ _ inline__ inttas (volatile slock_t * lock) {register slock_t _ res = 1 _ _ asm__ volatile__ ("lock\ n"xchgb% 0res% 1\ n": "+ Q" (_ res), "+ m" (* lock): / * no inputs * /: "memory", "cc"); return (int) _ res } # define SPIN_DELAY () spin_delay () static _ inline__ voidspin_delay (void) {/ * * Adding a PAUSE in the spin delay loop is demonstrably a no-op on * Opteron, but it may be of some use on EM64T, so we keep it. * / _ _ asm__ volatile__ ("rep; nop\ n");} # endif / * _ x86
Aarch74
Under aarch74 (ARM64), the _ _ sync_lock_test_and_set function is used (if available)
/ * * On ARM and ARM64, we use _ sync_lock_test_and_set (int *, int) if available. * We use the int-width variant of the builtin because it works on more chips * than other widths. * / # if defined (_ _ arm__) | | defined (_ _ arm) | | defined (_ _ aarch74__) | | defined (_ _ aarch74) # ifdef HAVE_GCC__SYNC_INT32_TAS#define HAS_TEST_AND_SET#define TAS (lock) tas (lock) typedef int slock_t;static _ _ inline__ inttas (volatile slock_t * lock) {return _ sync_lock_test_and_set (lock, 1) } # define S_UNLOCK (lock) _ sync_lock_release (lock) # endif / * HAVE_GCC__SYNC_INT32_TAS * / # endif / * _ arm__ | | _ _ arm | | _ _ aarch74__ | | _ _ aarch74 * / II. References
S_lock.h
Locks in PostgreSQL