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 * Note to implementors: there are default implementations for all these
28 * macros at the bottom of the file. Check if your platform can use
29 * these or needs to override them.
31 * Usually, S_LOCK() is implemented in terms of an even lower-level macro
34 * int TAS(slock_t *lock)
35 * Atomic test-and-set instruction. Attempt to acquire the lock,
36 * but do *not* wait. Returns 0 if successful, nonzero if unable
37 * to acquire the lock.
39 * TAS() is NOT part of the API, and should never be called directly.
41 * CAUTION: on some platforms TAS() may sometimes report failure to acquire
42 * a lock even when the lock is not locked. For example, on Alpha TAS()
43 * will "fail" if interrupted. Therefore TAS() should always be invoked
44 * in a retry loop, even if you are certain the lock is free.
46 * ANOTHER CAUTION: be sure that TAS() and S_UNLOCK() represent sequence
47 * points, ie, loads and stores of other values must not be moved across
48 * a lock or unlock. In most cases it suffices to make the operation be
49 * done through a "volatile" pointer.
51 * On most supported platforms, TAS() uses a tas() function written
52 * in assembly language to execute a hardware atomic-test-and-set
53 * instruction. Equivalent OS-supplied mutex routines could be used too.
55 * If no system-specific TAS() is available (ie, HAS_TEST_AND_SET is not
56 * defined), then we fall back on an emulation that uses SysV semaphores
57 * (see spin.c). This emulation will be MUCH MUCH slower than a proper TAS()
58 * implementation, because of the cost of a kernel call per lock or unlock.
59 * An old report is that Postgres spends around 40% of its time in semop(2)
60 * when using the SysV semaphore code.
63 * Portions Copyright (c) 1996-2003, PostgreSQL Global Development Group
64 * Portions Copyright (c) 1994, Regents of the University of California
66 * $Id: s_lock.h,v 1.114 2003/09/29 04:20:22 momjian Exp $
68 *-------------------------------------------------------------------------
73 #include "storage/pg_sema.h"
76 #if defined(HAS_TEST_AND_SET)
79 #if defined(__GNUC__) || defined(__ICC)
80 /*************************************************************************
85 * Standard gcc asm format:
91 : "=r"(_res) return value, in register
92 : "r"(lock) argument, 'lock pointer', in register
93 : "r0"); inline code uses this register
97 #if defined(__i386__) || defined(__x86_64__) /* AMD Opteron */
98 #define TAS(lock) tas(lock)
100 static __inline__ int
101 tas(volatile slock_t *lock)
103 register slock_t _res = 1;
105 __asm__ __volatile__(
108 : "=q"(_res), "=m"(*lock)
113 #endif /* __i386__ || __x86_64__ */
117 #if defined(__ia64__) || defined(__ia64)
118 #define TAS(lock) tas(lock)
120 static __inline__ int
121 tas(volatile slock_t *lock)
125 __asm__ __volatile__(
127 : "=r"(ret), "=m"(*lock)
134 #endif /* __ia64__ || __ia64 */
137 #if defined(__arm__) || defined(__arm__)
138 #define TAS(lock) tas(lock)
140 static __inline__ int
141 tas(volatile slock_t *lock)
143 register slock_t _res = 1;
145 __asm__ __volatile__(
146 " swpb %0, %0, [%3] \n"
147 : "=r"(_res), "=m"(*lock)
148 : "0"(_res), "r"(lock));
155 #if defined(__s390__) && !defined(__s390x__)
159 #define TAS(lock) tas(lock)
161 static __inline__ int
162 tas(volatile slock_t *lock)
166 __asm__ __volatile__(
172 : "=m"(lock), "=d"(_res)
179 #endif /* __s390__ */
181 #if defined(__s390x__)
183 * S/390x Linux (64-bit zSeries)
185 #define TAS(lock) tas(lock)
187 static __inline__ int
188 tas(volatile slock_t *lock)
192 __asm__ __volatile__(
198 : "=m"(lock), "=d"(_res)
205 #endif /* __s390x__ */
208 #if defined(__sparc__)
209 #define TAS(lock) tas(lock)
211 static __inline__ int
212 tas(volatile slock_t *lock)
214 register slock_t _res = 1;
216 __asm__ __volatile__(
217 " ldstub [%2], %0 \n"
218 : "=r"(_res), "=m"(*lock)
223 #endif /* __sparc__ */
225 #if defined(__ppc__) || defined(__powerpc__) || defined(__powerpc64__)
226 #define TAS(lock) tas(lock)
228 * NOTE: per the Enhanced PowerPC Architecture manual, v1.0 dated 7-May-2002,
229 * an isync is a sufficient synchronization barrier after a lwarx/stwcx loop.
231 static __inline__ int
232 tas(volatile slock_t *lock)
237 __asm__ __volatile__(
251 : "=&r" (_t), "=r" (_res)
261 #if defined(__mc68000__) && defined(__linux__)
262 #define TAS(lock) tas(lock)
264 static __inline__ int
265 tas(volatile slock_t *lock)
269 __asm__ __volatile__(
273 : "=d"(rv), "=m"(*lock)
280 #endif /* defined(__mc68000__) && defined(__linux__) */
283 #if defined(__ppc__) || defined(__powerpc__) || defined(__powerpc64__)
285 * PowerPC S_UNLOCK is almost standard but requires a "sync" instruction.
287 #define S_UNLOCK(lock) \
290 __asm__ __volatile__ (" sync \n"); \
291 *((volatile slock_t *) (lock)) = 0; \
297 #if defined(NEED_VAX_TAS_ASM)
299 * VAXen -- even multiprocessor ones
300 * (thanks to Tom Ivar Helbekkmo)
302 #define TAS(lock) tas(lock)
304 static __inline__ int
305 tas(volatile slock_t *lock)
309 __asm__ __volatile__(
311 " bbssi $0, (%1), 1f \n"
320 #endif /* NEED_VAX_TAS_ASM */
323 #if defined(NEED_NS32K_TAS_ASM)
324 #define TAS(lock) tas(lock)
326 static __inline__ int
327 tas(volatile slock_t *lock)
331 __asm__ __volatile__(
334 : "=m"(*lock), "=r"(_res));
338 #endif /* NEED_NS32K_TAS_ASM */
342 #else /* !__GNUC__ */
344 /***************************************************************************
345 * All non-gcc inlines
348 #if defined(NEED_I386_TAS_ASM) && defined(USE_UNIVEL_CC)
349 #define TAS(lock) tas(lock)
352 tas(volatile slock_t *s_lock)
354 /* UNIVEL wants %mem in column 1, so we don't pg_indent this file */
364 #endif /* defined(NEED_I386_TAS_ASM) && defined(USE_UNIVEL_CC) */
366 #endif /* defined(__GNUC__) */
370 /*************************************************************************
371 * These are the platforms that have only one compiler, or do not use inline
372 * assembler (and hence have common code for gcc and non-gcc compilers,
373 * if both are available).
380 * Correct multi-processor locking methods are explained in section 5.5.3
381 * of the Alpha AXP Architecture Handbook, which at this writing can be
382 * found at ftp://ftp.netbsd.org/pub/NetBSD/misc/dec-docs/index.html.
383 * For gcc we implement the handbook's code directly with inline assembler.
385 #if defined(__GNUC__)
387 #define TAS(lock) tas(lock)
388 #define S_UNLOCK(lock) \
391 __asm__ __volatile__ (" mb \n"); \
392 *((volatile slock_t *) (lock)) = 0; \
395 static __inline__ int
396 tas(volatile slock_t *lock)
398 register slock_t _res;
400 __asm__ __volatile__(
412 : "=m"(*lock), "=r"(_res)
419 #else /* !defined(__GNUC__) */
422 * The Tru64 compiler doesn't support gcc-style inline asm, but it does
423 * have some builtin functions that accomplish much the same results.
424 * For simplicity, slock_t is defined as long (ie, quadword) on Alpha
425 * regardless of the compiler in use. LOCK_LONG and UNLOCK_LONG only
426 * operate on an int (ie, longword), but that's OK as long as we define
427 * S_INIT_LOCK to zero out the whole quadword.
430 #include <alpha/builtins.h>
432 #define S_INIT_LOCK(lock) (*(lock) = 0)
433 #define TAS(lock) (__LOCK_LONG_RETRY((lock), 1) == 0)
434 #define S_UNLOCK(lock) __UNLOCK_LONG(lock)
436 #endif /* defined(__GNUC__) */
445 * Note that slock_t on PA-RISC is a structure instead of char
446 * (see include/port/hpux.h).
448 * a "set" slock_t has a single word cleared. a "clear" slock_t has
449 * all words set to non-zero. tas() is in tas.s
452 #define S_UNLOCK(lock) \
454 volatile slock_t *lock_ = (volatile slock_t *) (lock); \
455 lock_->sema[0] = -1; \
456 lock_->sema[1] = -1; \
457 lock_->sema[2] = -1; \
458 lock_->sema[3] = -1; \
461 #define S_LOCK_FREE(lock) ( *(int *) (((long) (lock) + 15) & ~15) != 0)
465 #if defined(__QNX__) && defined(__WATCOMC__)
467 * QNX 4 using WATCOM C
469 #define TAS(lock) wc_tas(lock)
470 extern slock_t wc_tas(volatile slock_t *lock);
471 #pragma aux wc_tas =\
473 " lock xchg al,[esi]" \
477 #endif /* __QNX__ and __WATCOMC__*/
483 * slock_t is defined as a unsigned long. We use the standard SGI
486 * The following comment is left for historical reasons, but is probably
487 * not a good idea since the mutex ABI is supported.
489 * This stuff may be supplemented in the future with Masato Kataoka's MIPS-II
490 * assembly from his NECEWS SVR4 port, but we probably ought to retain this
491 * for the R3000 chips out there.
494 #define TAS(lock) (test_and_set(lock,1))
495 #define S_UNLOCK(lock) (test_then_and(lock,0))
496 #define S_INIT_LOCK(lock) (test_then_and(lock,0))
497 #define S_LOCK_FREE(lock) (test_then_add(lock,0) == 0)
502 * SINIX / Reliant UNIX
503 * slock_t is defined as a struct abilock_t, which has a single unsigned long
504 * member. (Basically same as SGI)
507 #define TAS(lock) (!acquire_lock(lock))
508 #define S_UNLOCK(lock) release_lock(lock)
509 #define S_INIT_LOCK(lock) init_lock(lock)
510 #define S_LOCK_FREE(lock) (stat_lock(lock) == UNLOCKED)
518 * Note that slock_t on POWER/POWER2/PowerPC is int instead of char
520 #define TAS(lock) _check_lock(lock, 0, 1)
521 #define S_UNLOCK(lock) _clear_lock(lock, 0)
525 #if defined (nextstep)
528 * slock_t is defined as a struct mutex.
531 #define S_LOCK(lock) mutex_lock(lock)
532 #define S_UNLOCK(lock) mutex_unlock(lock)
533 #define S_INIT_LOCK(lock) mutex_init(lock)
534 /* For Mach, we have to delve inside the entrails of `struct mutex'. Ick! */
535 #define S_LOCK_FREE(alock) ((alock)->lock == 0)
536 #endif /* nextstep */
540 #else /* HAS_TEST_AND_SET */
542 #ifdef HAVE_SPINLOCKS
543 #error This platform does not support native spinlocks. To continue the compile, rerun configure using --disable-spinlocks. However, performance will be poor. Please report this to pgsql-bugs@postgresql.org.
547 * Fake spinlock implementation using semaphores --- slow and prone
548 * to fall foul of kernel limits on number of semaphores, so don't use this
549 * unless you must! The subroutines appear in spin.c.
551 typedef PGSemaphoreData slock_t;
553 extern bool s_lock_free_sema(volatile slock_t *lock);
554 extern void s_unlock_sema(volatile slock_t *lock);
555 extern void s_init_lock_sema(volatile slock_t *lock);
556 extern int tas_sema(volatile slock_t *lock);
558 #define S_LOCK_FREE(lock) s_lock_free_sema(lock)
559 #define S_UNLOCK(lock) s_unlock_sema(lock)
560 #define S_INIT_LOCK(lock) s_init_lock_sema(lock)
561 #define TAS(lock) tas_sema(lock)
563 #endif /* HAS_TEST_AND_SET */
568 * Default Definitions - override these above as needed.
572 #define S_LOCK(lock) \
575 s_lock((lock), __FILE__, __LINE__); \
579 #if !defined(S_LOCK_FREE)
580 #define S_LOCK_FREE(lock) (*(lock) == 0)
581 #endif /* S_LOCK_FREE */
583 #if !defined(S_UNLOCK)
584 #define S_UNLOCK(lock) (*((volatile slock_t *) (lock)) = 0)
585 #endif /* S_UNLOCK */
587 #if !defined(S_INIT_LOCK)
588 #define S_INIT_LOCK(lock) S_UNLOCK(lock)
589 #endif /* S_INIT_LOCK */
592 extern int tas(volatile slock_t *lock); /* in port/.../tas.s, or
595 #define TAS(lock) tas(lock)
600 * Platform-independent out-of-line support routines
602 extern void s_lock(volatile slock_t *lock, const char *file, int line);
604 #endif /* S_LOCK_H */