1 /*-------------------------------------------------------------------------
4 * This file contains the in-line portion of the implementation
7 * Portions Copyright (c) 1996-2001, PostgreSQL Global Development Group
8 * Portions Copyright (c) 1994, Regents of the University of California
12 * $Header: /cvsroot/pgsql/src/include/storage/s_lock.h,v 1.94 2001/09/24 20:10:44 petere Exp $
14 *-------------------------------------------------------------------------
19 * The public macros that must be provided are:
21 * void S_INIT_LOCK(slock_t *lock)
22 * Initialize a spinlock (to the unlocked state).
24 * void S_LOCK(slock_t *lock)
25 * Acquire a spinlock, waiting if necessary.
26 * Time out and abort() if unable to acquire the lock in a
27 * "reasonable" amount of time --- typically ~ 1 minute.
29 * void S_UNLOCK(slock_t *lock)
30 * Unlock a previously acquired lock.
32 * bool S_LOCK_FREE(slock_t *lock)
33 * Tests if the lock is free. Returns TRUE if free, FALSE if locked.
34 * This does *not* change the state of the lock.
36 * int TAS(slock_t *lock)
37 * Atomic test-and-set instruction. Attempt to acquire the lock,
38 * but do *not* wait. Returns 0 if successful, nonzero if unable
39 * to acquire the lock.
41 * TAS() is a lower-level part of the API, but is used directly in a
42 * few places that want to do other things while waiting for a lock.
43 * The S_LOCK() macro is equivalent to
46 * S_LOCK(slock_t *lock)
51 * S_LOCK_SLEEP(lock, spins++, timeout);
54 * where S_LOCK_SLEEP() checks for timeout and sleeps for a short
55 * interval. (The timeout is expressed in microseconds, or can be 0 for
56 * "infinity".) Callers that want to perform useful work while waiting
57 * can write out this entire loop and insert the "useful work" inside
60 * CAUTION to TAS() callers: on some platforms TAS() may sometimes
61 * report failure to acquire a lock even when the lock is not locked.
62 * For example, on Alpha TAS() will "fail" if interrupted. Therefore
63 * TAS() must *always* be invoked in a retry loop as depicted, even when
64 * you are certain the lock is free.
66 * On most supported platforms, TAS() uses a tas() function written
67 * in assembly language to execute a hardware atomic-test-and-set
68 * instruction. Equivalent OS-supplied mutex routines could be used too.
70 * If no system-specific TAS() is available (ie, HAS_TEST_AND_SET is not
71 * defined), then we fall back on an emulation that uses SysV semaphores.
72 * This emulation will be MUCH MUCH MUCH slower than a proper TAS()
73 * implementation, because of the cost of a kernel call per lock or unlock.
74 * An old report is that Postgres spends around 40% of its time in semop(2)
75 * when using the SysV semaphore code.
77 * Note to implementors: there are default implementations for all these
78 * macros at the bottom of the file. Check if your platform can use
79 * these or needs to override them.
85 #include "storage/ipc.h"
88 #if defined(HAS_TEST_AND_SET)
92 /*************************************************************************
97 * Standard gcc asm format:
103 : "=r"(_res) return value, in register
104 : "r"(lock) argument, 'lock pointer', in register
105 : "r0"); inline code uses this register
109 #if defined(__i386__)
110 #define TAS(lock) tas(lock)
112 static __inline__ int
113 tas(volatile slock_t *lock)
115 register slock_t _res = 1;
117 __asm__ __volatile__(
120 : "=q"(_res), "=m"(*lock)
125 #endif /* __i386__ */
129 #define TAS(lock) tas(lock)
131 static __inline__ int
132 tas(volatile slock_t *lock)
136 __asm__ __volatile__(
138 : "=r"(ret), "=m"(*lock)
145 #endif /* __ia64__ */
148 #if defined(__arm__) || defined(__arm__)
149 #define TAS(lock) tas(lock)
151 static __inline__ int
152 tas(volatile slock_t *lock)
154 register slock_t _res = 1;
156 __asm__ __volatile__(
157 " swpb %0, %0, [%3] \n"
158 : "=r"(_res), "=m"(*lock)
159 : "0"(_res), "r"(lock));
165 #if defined(__s390__) || defined(__s390x__)
169 #define TAS(lock) tas(lock)
172 tas(volatile slock_t *lock)
176 __asm__ __volatile__(
182 : "=m"(lock), "=d"(_res)
189 #endif /* __s390__ */
192 #if defined(__sparc__)
193 #define TAS(lock) tas(lock)
195 static __inline__ int
196 tas(volatile slock_t *lock)
198 register slock_t _res = 1;
200 __asm__ __volatile__(
201 " ldstub [%2], %0 \n"
202 : "=r"(_res), "=m"(*lock)
207 #endif /* __sparc__ */
210 #if defined(__mc68000__) && defined(__linux__)
211 #define TAS(lock) tas(lock)
213 static __inline__ int
214 tas(volatile slock_t *lock)
218 __asm__ __volatile__(
222 : "=d"(rv), "=m"(*lock)
229 #endif /* defined(__mc68000__) && defined(__linux__) */
232 #if defined(NEED_VAX_TAS_ASM)
234 * VAXen -- even multiprocessor ones
235 * (thanks to Tom Ivar Helbekkmo)
237 #define TAS(lock) tas(lock)
239 static __inline__ int
240 tas(volatile slock_t *lock)
244 __asm__ __volatile__(
246 " bbssi $0, (%1), 1f \n"
255 #endif /* NEED_VAX_TAS_ASM */
258 #if defined(NEED_NS32K_TAS_ASM)
259 #define TAS(lock) tas(lock)
261 static __inline__ int
262 tas(volatile slock_t *lock)
266 __asm__ __volatile__(
269 : "=m"(*lock), "=r"(_res));
273 #endif /* NEED_NS32K_TAS_ASM */
277 #else /* !__GNUC__ */
279 /***************************************************************************
280 * All non-gcc inlines
283 #if defined(NEED_I386_TAS_ASM) && defined(USE_UNIVEL_CC)
284 #define TAS(lock) tas(lock)
287 tas(volatile slock_t *s_lock)
289 /* UNIVEL wants %mem in column 1, so we don't pg_indent this file */
299 #endif /* defined(NEED_I386_TAS_ASM) && defined(USE_UNIVEL_CC) */
301 #endif /* defined(__GNUC__) */
305 /*************************************************************************
306 * These are the platforms that do not use inline assembler (and hence
307 * have common code for gcc and non-gcc compilers, if both are available).
314 * Correct multi-processor locking methods are explained in section 5.5.3
315 * of the Alpha AXP Architecture Handbook, which at this writing can be
316 * found at ftp://ftp.netbsd.org/pub/NetBSD/misc/dec-docs/index.html.
317 * For gcc we implement the handbook's code directly with inline assembler.
319 #if defined(__GNUC__)
321 #define TAS(lock) tas(lock)
322 #define S_UNLOCK(lock) \
325 __asm__ __volatile__ (" mb \n"); \
329 static __inline__ int
330 tas(volatile slock_t *lock)
332 register slock_t _res;
334 __asm__ __volatile__(
346 : "=m"(*lock), "=r"(_res)
353 #else /* !defined(__GNUC__) */
356 * The Tru64 compiler doesn't support gcc-style inline asm, but it does
357 * have some builtin functions that accomplish much the same results.
358 * For simplicity, slock_t is defined as long (ie, quadword) on Alpha
359 * regardless of the compiler in use. LOCK_LONG and UNLOCK_LONG only
360 * operate on an int (ie, longword), but that's OK as long as we define
361 * S_INIT_LOCK to zero out the whole quadword.
364 #include <alpha/builtins.h>
366 #define S_INIT_LOCK(lock) (*(lock) = 0)
367 #define TAS(lock) (__LOCK_LONG_RETRY((lock), 1) == 0)
368 #define S_UNLOCK(lock) __UNLOCK_LONG(lock)
370 #endif /* defined(__GNUC__) */
379 * Note that slock_t on PA-RISC is a structure instead of char
380 * (see include/port/hpux.h).
382 * a "set" slock_t has a single word cleared. a "clear" slock_t has
383 * all words set to non-zero. tas() is in tas.s
386 #define S_UNLOCK(lock) \
388 volatile slock_t *lock_ = (volatile slock_t *) (lock); \
389 lock_->sema[0] = -1; \
390 lock_->sema[1] = -1; \
391 lock_->sema[2] = -1; \
392 lock_->sema[3] = -1; \
395 #define S_LOCK_FREE(lock) ( *(int *) (((long) (lock) + 15) & ~15) != 0)
399 #if defined(__QNX__) && defined(__WATCOMC__)
401 * QNX 4 using WATCOM C
403 #define TAS(lock) wc_tas(lock)
404 extern slock_t wc_tas(volatile slock_t *lock);
405 #pragma aux wc_tas =\
407 " lock xchg al,[esi]" \
411 #endif /* __QNX__ and __WATCOMC__*/
417 * slock_t is defined as a unsigned long. We use the standard SGI
420 * The following comment is left for historical reasons, but is probably
421 * not a good idea since the mutex ABI is supported.
423 * This stuff may be supplemented in the future with Masato Kataoka's MIPS-II
424 * assembly from his NECEWS SVR4 port, but we probably ought to retain this
425 * for the R3000 chips out there.
428 #define TAS(lock) (test_and_set(lock,1))
429 #define S_UNLOCK(lock) (test_then_and(lock,0))
430 #define S_INIT_LOCK(lock) (test_then_and(lock,0))
431 #define S_LOCK_FREE(lock) (test_then_add(lock,0) == 0)
436 * SINIX / Reliant UNIX
437 * slock_t is defined as a struct abilock_t, which has a single unsigned long
438 * member. (Basically same as SGI)
441 #define TAS(lock) (!acquire_lock(lock))
442 #define S_UNLOCK(lock) release_lock(lock)
443 #define S_INIT_LOCK(lock) init_lock(lock)
444 #define S_LOCK_FREE(lock) (stat_lock(lock) == UNLOCKED)
452 * Note that slock_t on POWER/POWER2/PowerPC is int instead of char
453 * (see storage/ipc.h).
455 #define TAS(lock) cs((int *) (lock), 0, 1)
459 #if defined (nextstep)
462 * slock_t is defined as a struct mutex.
465 #define S_LOCK(lock) mutex_lock(lock)
466 #define S_UNLOCK(lock) mutex_unlock(lock)
467 #define S_INIT_LOCK(lock) mutex_init(lock)
468 /* For Mach, we have to delve inside the entrails of `struct mutex'. Ick! */
469 #define S_LOCK_FREE(alock) ((alock)->lock == 0)
470 #endif /* nextstep */
474 #else /* !HAS_TEST_AND_SET */
477 * Fake spinlock implementation using SysV semaphores --- slow and prone
478 * to fall foul of kernel limits on number of semaphores, so don't use this
484 /* reference to semaphore used to implement this spinlock */
485 IpcSemaphoreId semId;
489 extern bool s_lock_free_sema(volatile slock_t *lock);
490 extern void s_unlock_sema(volatile slock_t *lock);
491 extern void s_init_lock_sema(volatile slock_t *lock);
492 extern int tas_sema(volatile slock_t *lock);
494 #define S_LOCK_FREE(lock) s_lock_free_sema(lock)
495 #define S_UNLOCK(lock) s_unlock_sema(lock)
496 #define S_INIT_LOCK(lock) s_init_lock_sema(lock)
497 #define TAS(lock) tas_sema(lock)
499 #endif /* HAS_TEST_AND_SET */
503 /****************************************************************************
504 * Default Definitions - override these above as needed.
508 #define S_LOCK(lock) \
511 s_lock((lock), __FILE__, __LINE__); \
515 #if !defined(S_LOCK_SLEEP)
516 #define S_LOCK_SLEEP(lock,spins,timeout) \
517 s_lock_sleep((spins), (timeout), 0, (lock), __FILE__, __LINE__)
518 #endif /* S_LOCK_SLEEP */
520 #if !defined(S_LOCK_SLEEP_INTERVAL)
521 #define S_LOCK_SLEEP_INTERVAL(lock,spins,timeout,microsec) \
522 s_lock_sleep((spins), (timeout), (microsec), (lock), __FILE__, __LINE__)
523 #endif /* S_LOCK_SLEEP_INTERVAL */
525 #if !defined(S_LOCK_FREE)
526 #define S_LOCK_FREE(lock) (*(lock) == 0)
527 #endif /* S_LOCK_FREE */
529 #if !defined(S_UNLOCK)
530 #define S_UNLOCK(lock) (*(lock) = 0)
531 #endif /* S_UNLOCK */
533 #if !defined(S_INIT_LOCK)
534 #define S_INIT_LOCK(lock) S_UNLOCK(lock)
535 #endif /* S_INIT_LOCK */
538 extern int tas(volatile slock_t *lock); /* in port/.../tas.s, or
541 #define TAS(lock) tas(lock)
545 /****************************************************************************
546 * Platform-independent out-of-line support routines
548 extern void s_lock(volatile slock_t *lock,
549 const char *file, const int line);
550 extern void s_lock_sleep(unsigned spins, int timeout, int microsec,
551 volatile slock_t *lock,
552 const char *file, const int line);
554 #endif /* S_LOCK_H */