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-2002, PostgreSQL Global Development Group
64 * Portions Copyright (c) 1994, Regents of the University of California
66 * $Id: s_lock.h,v 1.102 2002/11/10 00:33:43 momjian Exp $
68 *-------------------------------------------------------------------------
73 #include "storage/pg_sema.h"
76 #if defined(HAS_TEST_AND_SET)
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
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__ */
117 #define TAS(lock) tas(lock)
119 static __inline__ int
120 tas(volatile slock_t *lock)
124 __asm__ __volatile__(
126 : "=r"(ret), "=m"(*lock)
133 #endif /* __ia64__ */
136 #if defined(__arm__) || defined(__arm__)
137 #define TAS(lock) tas(lock)
139 static __inline__ int
140 tas(volatile slock_t *lock)
142 register slock_t _res = 1;
144 __asm__ __volatile__(
145 " swpb %0, %0, [%3] \n"
146 : "=r"(_res), "=m"(*lock)
147 : "0"(_res), "r"(lock));
153 #if defined(__s390__) || defined(__s390x__)
157 #define TAS(lock) tas(lock)
159 static __inline__ int
160 tas(volatile slock_t *lock)
164 __asm__ __volatile__(
170 : "=m"(lock), "=d"(_res)
177 #endif /* __s390__ */
180 #if defined(__sparc__)
181 #define TAS(lock) tas(lock)
183 static __inline__ int
184 tas(volatile slock_t *lock)
186 register slock_t _res = 1;
188 __asm__ __volatile__(
189 " ldstub [%2], %0 \n"
190 : "=r"(_res), "=m"(*lock)
195 #endif /* __sparc__ */
197 #if defined(__powerpc__) || defined(__powerpc64__)
198 static __inline__ int
199 tas(volatile slock_t *lock)
204 __asm__ __volatile__(
218 : "=&r" (_t), "=m" (lock), "=r" (_res)
227 #if defined(__mc68000__) && defined(__linux__)
228 #define TAS(lock) tas(lock)
230 static __inline__ int
231 tas(volatile slock_t *lock)
235 __asm__ __volatile__(
239 : "=d"(rv), "=m"(*lock)
246 #endif /* defined(__mc68000__) && defined(__linux__) */
249 #if defined(__ppc__) || defined(__powerpc__)
251 * We currently use out-of-line assembler for TAS on PowerPC; see s_lock.c.
252 * S_UNLOCK is almost standard but requires a "sync" instruction.
254 #define S_UNLOCK(lock) \
257 __asm__ __volatile__ (" sync \n"); \
258 *((volatile slock_t *) (lock)) = 0; \
261 #endif /* defined(__ppc__) || defined(__powerpc__) */
264 #if defined(NEED_VAX_TAS_ASM)
266 * VAXen -- even multiprocessor ones
267 * (thanks to Tom Ivar Helbekkmo)
269 #define TAS(lock) tas(lock)
271 static __inline__ int
272 tas(volatile slock_t *lock)
276 __asm__ __volatile__(
278 " bbssi $0, (%1), 1f \n"
287 #endif /* NEED_VAX_TAS_ASM */
290 #if defined(NEED_NS32K_TAS_ASM)
291 #define TAS(lock) tas(lock)
293 static __inline__ int
294 tas(volatile slock_t *lock)
298 __asm__ __volatile__(
301 : "=m"(*lock), "=r"(_res));
305 #endif /* NEED_NS32K_TAS_ASM */
309 #else /* !__GNUC__ */
311 /***************************************************************************
312 * All non-gcc inlines
315 #if defined(NEED_I386_TAS_ASM) && defined(USE_UNIVEL_CC)
316 #define TAS(lock) tas(lock)
319 tas(volatile slock_t *s_lock)
321 /* UNIVEL wants %mem in column 1, so we don't pg_indent this file */
331 #endif /* defined(NEED_I386_TAS_ASM) && defined(USE_UNIVEL_CC) */
333 #endif /* defined(__GNUC__) */
337 /*************************************************************************
338 * These are the platforms that do not use inline assembler (and hence
339 * have common code for gcc and non-gcc compilers, if both are available).
346 * Correct multi-processor locking methods are explained in section 5.5.3
347 * of the Alpha AXP Architecture Handbook, which at this writing can be
348 * found at ftp://ftp.netbsd.org/pub/NetBSD/misc/dec-docs/index.html.
349 * For gcc we implement the handbook's code directly with inline assembler.
351 #if defined(__GNUC__)
353 #define TAS(lock) tas(lock)
354 #define S_UNLOCK(lock) \
357 __asm__ __volatile__ (" mb \n"); \
358 *((volatile slock_t *) (lock)) = 0; \
361 static __inline__ int
362 tas(volatile slock_t *lock)
364 register slock_t _res;
366 __asm__ __volatile__(
378 : "=m"(*lock), "=r"(_res)
385 #else /* !defined(__GNUC__) */
388 * The Tru64 compiler doesn't support gcc-style inline asm, but it does
389 * have some builtin functions that accomplish much the same results.
390 * For simplicity, slock_t is defined as long (ie, quadword) on Alpha
391 * regardless of the compiler in use. LOCK_LONG and UNLOCK_LONG only
392 * operate on an int (ie, longword), but that's OK as long as we define
393 * S_INIT_LOCK to zero out the whole quadword.
396 #include <alpha/builtins.h>
398 #define S_INIT_LOCK(lock) (*(lock) = 0)
399 #define TAS(lock) (__LOCK_LONG_RETRY((lock), 1) == 0)
400 #define S_UNLOCK(lock) __UNLOCK_LONG(lock)
402 #endif /* defined(__GNUC__) */
411 * Note that slock_t on PA-RISC is a structure instead of char
412 * (see include/port/hpux.h).
414 * a "set" slock_t has a single word cleared. a "clear" slock_t has
415 * all words set to non-zero. tas() is in tas.s
418 #define S_UNLOCK(lock) \
420 volatile slock_t *lock_ = (volatile slock_t *) (lock); \
421 lock_->sema[0] = -1; \
422 lock_->sema[1] = -1; \
423 lock_->sema[2] = -1; \
424 lock_->sema[3] = -1; \
427 #define S_LOCK_FREE(lock) ( *(int *) (((long) (lock) + 15) & ~15) != 0)
431 #if defined(__QNX__) && defined(__WATCOMC__)
433 * QNX 4 using WATCOM C
435 #define TAS(lock) wc_tas(lock)
436 extern slock_t wc_tas(volatile slock_t *lock);
437 #pragma aux wc_tas =\
439 " lock xchg al,[esi]" \
443 #endif /* __QNX__ and __WATCOMC__*/
449 * slock_t is defined as a unsigned long. We use the standard SGI
452 * The following comment is left for historical reasons, but is probably
453 * not a good idea since the mutex ABI is supported.
455 * This stuff may be supplemented in the future with Masato Kataoka's MIPS-II
456 * assembly from his NECEWS SVR4 port, but we probably ought to retain this
457 * for the R3000 chips out there.
460 #define TAS(lock) (test_and_set(lock,1))
461 #define S_UNLOCK(lock) (test_then_and(lock,0))
462 #define S_INIT_LOCK(lock) (test_then_and(lock,0))
463 #define S_LOCK_FREE(lock) (test_then_add(lock,0) == 0)
468 * SINIX / Reliant UNIX
469 * slock_t is defined as a struct abilock_t, which has a single unsigned long
470 * member. (Basically same as SGI)
473 #define TAS(lock) (!acquire_lock(lock))
474 #define S_UNLOCK(lock) release_lock(lock)
475 #define S_INIT_LOCK(lock) init_lock(lock)
476 #define S_LOCK_FREE(lock) (stat_lock(lock) == UNLOCKED)
484 * Note that slock_t on POWER/POWER2/PowerPC is int instead of char
486 #define TAS(lock) _check_lock(lock, 0, 1)
487 #define S_UNLOCK(lock) _clear_lock(lock, 0)
491 #if defined (nextstep)
494 * slock_t is defined as a struct mutex.
497 #define S_LOCK(lock) mutex_lock(lock)
498 #define S_UNLOCK(lock) mutex_unlock(lock)
499 #define S_INIT_LOCK(lock) mutex_init(lock)
500 /* For Mach, we have to delve inside the entrails of `struct mutex'. Ick! */
501 #define S_LOCK_FREE(alock) ((alock)->lock == 0)
502 #endif /* nextstep */
506 #else /* !HAS_TEST_AND_SET */
509 * Fake spinlock implementation using semaphores --- slow and prone
510 * to fall foul of kernel limits on number of semaphores, so don't use this
511 * unless you must! The subroutines appear in spin.c.
513 typedef PGSemaphoreData slock_t;
515 extern bool s_lock_free_sema(volatile slock_t *lock);
516 extern void s_unlock_sema(volatile slock_t *lock);
517 extern void s_init_lock_sema(volatile slock_t *lock);
518 extern int tas_sema(volatile slock_t *lock);
520 #define S_LOCK_FREE(lock) s_lock_free_sema(lock)
521 #define S_UNLOCK(lock) s_unlock_sema(lock)
522 #define S_INIT_LOCK(lock) s_init_lock_sema(lock)
523 #define TAS(lock) tas_sema(lock)
525 #endif /* HAS_TEST_AND_SET */
530 * Default Definitions - override these above as needed.
534 #define S_LOCK(lock) \
537 s_lock((lock), __FILE__, __LINE__); \
541 #if !defined(S_LOCK_FREE)
542 #define S_LOCK_FREE(lock) (*(lock) == 0)
543 #endif /* S_LOCK_FREE */
545 #if !defined(S_UNLOCK)
546 #define S_UNLOCK(lock) (*((volatile slock_t *) (lock)) = 0)
547 #endif /* S_UNLOCK */
549 #if !defined(S_INIT_LOCK)
550 #define S_INIT_LOCK(lock) S_UNLOCK(lock)
551 #endif /* S_INIT_LOCK */
554 extern int tas(volatile slock_t *lock); /* in port/.../tas.s, or
557 #define TAS(lock) tas(lock)
562 * Platform-independent out-of-line support routines
564 extern void s_lock(volatile slock_t *lock, const char *file, int line);
566 #endif /* S_LOCK_H */