1 /*-------------------------------------------------------------------------
4 * Hardware-dependent implementation of spinlocks.
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.
9 * The following hardware-dependent macros must be provided for each
12 * void S_INIT_LOCK(slock_t *lock)
13 * Initialize a spinlock (to the unlocked state).
15 * void 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.
20 * void S_UNLOCK(slock_t *lock)
21 * Unlock a previously acquired lock.
23 * bool S_LOCK_FREE(slock_t *lock)
24 * Tests if the lock is free. Returns TRUE if free, FALSE if locked.
25 * This does *not* change the state of the lock.
27 * void SPIN_DELAY(void)
28 * Delay operation to occur inside spinlock wait loop.
30 * Note to implementors: there are default implementations for all these
31 * macros at the bottom of the file. Check if your platform can use
32 * these or needs to override them.
34 * Usually, S_LOCK() is implemented in terms of an even lower-level macro
37 * int TAS(slock_t *lock)
38 * Atomic test-and-set instruction. Attempt to acquire the lock,
39 * but do *not* wait. Returns 0 if successful, nonzero if unable
40 * to acquire the lock.
42 * TAS() is NOT part of the API, and should never be called directly.
44 * CAUTION: on some platforms TAS() may sometimes report failure to acquire
45 * a lock even when the lock is not locked. For example, on Alpha TAS()
46 * will "fail" if interrupted. Therefore TAS() should always be invoked
47 * in a retry loop, even if you are certain the lock is free.
49 * ANOTHER CAUTION: be sure that TAS() and S_UNLOCK() represent sequence
50 * points, ie, loads and stores of other values must not be moved across
51 * a lock or unlock. In most cases it suffices to make the operation be
52 * done through a "volatile" pointer.
54 * On most supported platforms, TAS() uses a tas() function written
55 * in assembly language to execute a hardware atomic-test-and-set
56 * instruction. Equivalent OS-supplied mutex routines could be used too.
58 * If no system-specific TAS() is available (ie, HAVE_SPINLOCKS is not
59 * defined), then we fall back on an emulation that uses SysV semaphores
60 * (see spin.c). This emulation will be MUCH MUCH slower than a proper TAS()
61 * implementation, because of the cost of a kernel call per lock or unlock.
62 * An old report is that Postgres spends around 40% of its time in semop(2)
63 * when using the SysV semaphore code.
66 * Portions Copyright (c) 1996-2004, PostgreSQL Global Development Group
67 * Portions Copyright (c) 1994, Regents of the University of California
69 * $PostgreSQL: pgsql/src/include/storage/s_lock.h,v 1.128 2004/08/30 22:49:07 tgl Exp $
71 *-------------------------------------------------------------------------
76 #include "storage/pg_sema.h"
78 #ifdef HAVE_SPINLOCKS /* skip spinlocks if requested */
81 #if defined(__GNUC__) || defined(__ICC)
82 /*************************************************************************
84 * Gcc consistently defines the CPU as __cpu__.
85 * Other compilers use __cpu or __cpu__ so we test for both in those cases.
89 * Standard gcc asm format (assuming "volatile slock_t *lock"):
95 : "=r"(_res), "+m"(*lock) // return register, in/out lock value
96 : "r"(lock) // lock pointer, in input register
97 : "memory", "cc"); // show clobbered registers here
99 * The output-operands list (after first colon) should always include
100 * "+m"(*lock), whether or not the asm code actually refers to this
101 * operand directly. This ensures that gcc believes the value in the
102 * lock variable is used and set by the asm code. Also, the clobbers
103 * list (after third colon) should always include "memory"; this prevents
104 * gcc from thinking it can cache the values of shared-memory fields
105 * across the asm code. Add "cc" if your asm code changes the condition
106 * code register, and also list any temp registers the code uses.
111 #if defined(__i386__) || defined(__x86_64__) /* AMD Opteron */
112 #define HAS_TEST_AND_SET
114 typedef unsigned char slock_t;
116 #define TAS(lock) tas(lock)
118 static __inline__ int
119 tas(volatile slock_t *lock)
121 register slock_t _res = 1;
123 /* Use a non-locking test before asserting the bus lock */
124 __asm__ __volatile__(
130 : "+q"(_res), "+m"(*lock)
136 #define SPIN_DELAY() spin_delay()
138 static __inline__ void
141 __asm__ __volatile__(
145 #endif /* __i386__ || __x86_64__ */
148 #if defined(__ia64__) || defined(__ia64) /* __ia64 used by ICC compiler? */
150 #define HAS_TEST_AND_SET
152 typedef unsigned int slock_t;
154 #define TAS(lock) tas(lock)
156 static __inline__ int
157 tas(volatile slock_t *lock)
161 __asm__ __volatile__(
163 : "=r"(ret), "+m"(*lock)
169 #endif /* __ia64__ || __ia64 */
172 #if defined(__arm__) || defined(__arm)
173 #define HAS_TEST_AND_SET
175 typedef unsigned char slock_t;
177 #define TAS(lock) tas(lock)
179 static __inline__ int
180 tas(volatile slock_t *lock)
182 register slock_t _res = 1;
184 __asm__ __volatile__(
185 " swpb %0, %0, [%2] \n"
186 : "+r"(_res), "+m"(*lock)
195 #if defined(__s390__) || defined(__s390x__)
196 /* S/390 and S/390x Linux (32- and 64-bit zSeries) */
197 #define HAS_TEST_AND_SET
199 typedef unsigned int slock_t;
201 #define TAS(lock) tas(lock)
203 static __inline__ int
204 tas(volatile slock_t *lock)
208 __asm__ __volatile__(
210 : "+d"(_res), "+m"(*lock)
216 #endif /* __s390__ || __s390x__ */
219 #if defined(__sparc__)
220 #define HAS_TEST_AND_SET
222 typedef unsigned char slock_t;
224 #define TAS(lock) tas(lock)
226 static __inline__ int
227 tas(volatile slock_t *lock)
229 register slock_t _res;
231 __asm__ __volatile__(
232 " ldstub [%2], %0 \n"
233 : "=r"(_res), "+m"(*lock)
239 #endif /* __sparc__ */
242 #if defined(__ppc__) || defined(__powerpc__) || defined(__powerpc64__)
243 #define HAS_TEST_AND_SET
245 #if defined(__powerpc64__)
246 typedef unsigned long slock_t;
248 typedef unsigned int slock_t;
251 #define TAS(lock) tas(lock)
253 * NOTE: per the Enhanced PowerPC Architecture manual, v1.0 dated 7-May-2002,
254 * an isync is a sufficient synchronization barrier after a lwarx/stwcx loop.
256 static __inline__ int
257 tas(volatile slock_t *lock)
262 __asm__ __volatile__(
276 : "=&r"(_t), "=r"(_res), "+m"(*lock)
282 /* PowerPC S_UNLOCK is almost standard but requires a "sync" instruction */
283 #define S_UNLOCK(lock) \
286 __asm__ __volatile__ (" sync \n"); \
287 *((volatile slock_t *) (lock)) = 0; \
293 #if defined(__mc68000__) && defined(__linux__)
294 #define HAS_TEST_AND_SET
296 typedef unsigned char slock_t;
298 #define TAS(lock) tas(lock)
300 static __inline__ int
301 tas(volatile slock_t *lock)
305 __asm__ __volatile__(
309 : "=d"(rv), "+m"(*lock)
315 #endif /* defined(__mc68000__) && defined(__linux__) */
320 * VAXen -- even multiprocessor ones
321 * (thanks to Tom Ivar Helbekkmo)
323 #define HAS_TEST_AND_SET
325 typedef unsigned char slock_t;
327 #define TAS(lock) tas(lock)
329 static __inline__ int
330 tas(volatile slock_t *lock)
334 __asm__ __volatile__(
336 " bbssi $0, (%2), 1f \n"
339 : "=&r"(_res), "+m"(*lock)
348 #if defined(__ns32k__)
349 #define HAS_TEST_AND_SET
351 typedef unsigned char slock_t;
353 #define TAS(lock) tas(lock)
355 static __inline__ int
356 tas(volatile slock_t *lock)
360 __asm__ __volatile__(
363 : "=r"(_res), "+m"(*lock)
369 #endif /* __ns32k__ */
372 #if defined(__alpha) || defined(__alpha__)
374 * Correct multi-processor locking methods are explained in section 5.5.3
375 * of the Alpha AXP Architecture Handbook, which at this writing can be
376 * found at ftp://ftp.netbsd.org/pub/NetBSD/misc/dec-docs/index.html.
377 * For gcc we implement the handbook's code directly with inline assembler.
379 #define HAS_TEST_AND_SET
381 typedef unsigned long slock_t;
383 #define TAS(lock) tas(lock)
385 static __inline__ int
386 tas(volatile slock_t *lock)
388 register slock_t _res;
390 __asm__ __volatile__(
402 : "=&r"(_res), "+m"(*lock)
408 #define S_UNLOCK(lock) \
411 __asm__ __volatile__ (" mb \n"); \
412 *((volatile slock_t *) (lock)) = 0; \
415 #endif /* __alpha || __alpha__ */
418 /* These live in s_lock.c, but only for gcc */
421 #if defined(__m68k__)
422 #define HAS_TEST_AND_SET
424 typedef unsigned char slock_t;
428 #if defined(__mips__) && !defined(__sgi)
429 #define HAS_TEST_AND_SET
431 typedef unsigned int slock_t;
435 #endif /* __GNUC__ */
439 /***************************************************************************
440 * Platforms that use non-gcc inline assembly:
443 #if !defined(HAS_TEST_AND_SET) /* We didn't trigger above, let's try here */
446 #if defined(USE_UNIVEL_CC)
447 #define HAS_TEST_AND_SET
449 typedef unsigned char slock_t;
451 #define TAS(lock) tas(lock)
454 tas(volatile slock_t *s_lock)
456 /* UNIVEL wants %mem in column 1, so we don't pg_indent this file */
466 #endif /* defined(USE_UNIVEL_CC) */
469 #if defined(__alpha) || defined(__alpha__)
471 * The Tru64 compiler doesn't support gcc-style inline asm, but it does
472 * have some builtin functions that accomplish much the same results.
473 * For simplicity, slock_t is defined as long (ie, quadword) on Alpha
474 * regardless of the compiler in use. LOCK_LONG and UNLOCK_LONG only
475 * operate on an int (ie, longword), but that's OK as long as we define
476 * S_INIT_LOCK to zero out the whole quadword.
478 #define HAS_TEST_AND_SET
480 typedef unsigned long slock_t;
482 #include <alpha/builtins.h>
483 #define S_INIT_LOCK(lock) (*(lock) = 0)
484 #define TAS(lock) (__LOCK_LONG_RETRY((lock), 1) == 0)
485 #define S_UNLOCK(lock) __UNLOCK_LONG(lock)
487 #endif /* __alpha || __alpha__ */
490 #if defined(__hppa) || defined(__hppa__)
494 * See src/backend/port/hpux/tas.c.template for details about LDCWX. Because
495 * LDCWX requires a 16-byte-aligned address, we declare slock_t as a 16-byte
496 * struct. The active word in the struct is whichever has the aligned address;
497 * the other three words just sit at -1.
499 * When using gcc, we can inline the required assembly code.
501 #define HAS_TEST_AND_SET
508 #define TAS_ACTIVE_WORD(lock) ((volatile int *) (((long) (lock) + 15) & ~15))
510 #if defined(__GNUC__)
512 static __inline__ int
513 tas(volatile slock_t *lock)
515 volatile int *lockword = TAS_ACTIVE_WORD(lock);
516 register int lockval;
518 __asm__ __volatile__(
519 " ldcwx 0(0,%2),%0 \n"
520 : "=r"(lockval), "+m"(*lockword)
523 return (lockval == 0);
526 #endif /* __GNUC__ */
528 #define S_UNLOCK(lock) (*TAS_ACTIVE_WORD(lock) = -1)
530 #define S_INIT_LOCK(lock) \
532 volatile slock_t *lock_ = (lock); \
533 lock_->sema[0] = -1; \
534 lock_->sema[1] = -1; \
535 lock_->sema[2] = -1; \
536 lock_->sema[3] = -1; \
539 #define S_LOCK_FREE(lock) (*TAS_ACTIVE_WORD(lock) != 0)
541 #endif /* __hppa || __hppa__ */
544 #if defined(__QNX__) && defined(__WATCOMC__)
546 * QNX 4 using WATCOM C
548 #define HAS_TEST_AND_SET
550 typedef unsigned char slock_t;
552 #define TAS(lock) wc_tas(lock)
553 extern slock_t wc_tas(volatile slock_t *lock);
554 #pragma aux wc_tas =\
556 " lock xchg al,[esi]" \
560 #endif /* __QNX__ and __WATCOMC__*/
566 * slock_t is defined as a unsigned long. We use the standard SGI
569 * The following comment is left for historical reasons, but is probably
570 * not a good idea since the mutex ABI is supported.
572 * This stuff may be supplemented in the future with Masato Kataoka's MIPS-II
573 * assembly from his NECEWS SVR4 port, but we probably ought to retain this
574 * for the R3000 chips out there.
576 #define HAS_TEST_AND_SET
578 typedef unsigned long slock_t;
581 #define TAS(lock) (test_and_set(lock,1))
582 #define S_UNLOCK(lock) (test_then_and(lock,0))
583 #define S_INIT_LOCK(lock) (test_then_and(lock,0))
584 #define S_LOCK_FREE(lock) (test_then_add(lock,0) == 0)
590 * SINIX / Reliant UNIX
591 * slock_t is defined as a struct abilock_t, which has a single unsigned long
592 * member. (Basically same as SGI)
594 #define HAS_TEST_AND_SET
596 #include "abi_mutex.h"
597 typedef abilock_t slock_t;
599 #define TAS(lock) (!acquire_lock(lock))
600 #define S_UNLOCK(lock) release_lock(lock)
601 #define S_INIT_LOCK(lock) init_lock(lock)
602 #define S_LOCK_FREE(lock) (stat_lock(lock) == UNLOCKED)
610 #define HAS_TEST_AND_SET
612 typedef unsigned int slock_t;
614 #define TAS(lock) _check_lock(lock, 0, 1)
615 #define S_UNLOCK(lock) _clear_lock(lock, 0)
619 #if defined (nextstep)
620 #define HAS_TEST_AND_SET
622 typedef struct mutex slock_t;
624 #define S_LOCK(lock) mutex_lock(lock)
625 #define S_UNLOCK(lock) mutex_unlock(lock)
626 #define S_INIT_LOCK(lock) mutex_init(lock)
627 /* For Mach, we have to delve inside the entrails of `struct mutex'. Ick! */
628 #define S_LOCK_FREE(alock) ((alock)->lock == 0)
629 #endif /* nextstep */
632 /* These are in s_lock.c */
636 #define HAS_TEST_AND_SET
638 typedef unsigned char slock_t;
642 #if defined(__sparc__) || defined(__sparc)
643 #define HAS_TEST_AND_SET
645 typedef unsigned char slock_t;
649 #endif /* !defined(HAS_TEST_AND_SET) */
652 /* Blow up if we didn't have any way to do spinlocks */
653 #ifndef HAS_TEST_AND_SET
654 #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.
658 #else /* !HAVE_SPINLOCKS */
662 * Fake spinlock implementation using semaphores --- slow and prone
663 * to fall foul of kernel limits on number of semaphores, so don't use this
664 * unless you must! The subroutines appear in spin.c.
666 typedef PGSemaphoreData slock_t;
668 extern bool s_lock_free_sema(volatile slock_t *lock);
669 extern void s_unlock_sema(volatile slock_t *lock);
670 extern void s_init_lock_sema(volatile slock_t *lock);
671 extern int tas_sema(volatile slock_t *lock);
673 #define S_LOCK_FREE(lock) s_lock_free_sema(lock)
674 #define S_UNLOCK(lock) s_unlock_sema(lock)
675 #define S_INIT_LOCK(lock) s_init_lock_sema(lock)
676 #define TAS(lock) tas_sema(lock)
679 #endif /* HAVE_SPINLOCKS */
683 * Default Definitions - override these above as needed.
687 #define S_LOCK(lock) \
690 s_lock((lock), __FILE__, __LINE__); \
694 #if !defined(S_LOCK_FREE)
695 #define S_LOCK_FREE(lock) (*(lock) == 0)
696 #endif /* S_LOCK_FREE */
698 #if !defined(S_UNLOCK)
699 #define S_UNLOCK(lock) (*((volatile slock_t *) (lock)) = 0)
700 #endif /* S_UNLOCK */
702 #if !defined(S_INIT_LOCK)
703 #define S_INIT_LOCK(lock) S_UNLOCK(lock)
704 #endif /* S_INIT_LOCK */
706 #if !defined(SPIN_DELAY)
707 #define SPIN_DELAY() ((void) 0)
708 #endif /* SPIN_DELAY */
711 extern int tas(volatile slock_t *lock); /* in port/.../tas.s, or
714 #define TAS(lock) tas(lock)
719 * Platform-independent out-of-line support routines
721 extern void s_lock(volatile slock_t *lock, const char *file, int line);
723 #endif /* S_LOCK_H */