1 /*-------------------------------------------------------------------------
4 * This file contains the in-line portion of the implementation
7 * Portions Copyright (c) 1996-2000, PostgreSQL, Inc
8 * Portions Copyright (c) 1994, Regents of the University of California
12 * $Header: /cvsroot/pgsql/src/include/storage/s_lock.h,v 1.81 2001/01/19 06:59:59 momjian 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++);
54 * where S_LOCK_SLEEP() checks for timeout and sleeps for a short
55 * interval. Callers that want to perform useful work while waiting
56 * can write out this entire loop and insert the "useful work" inside
59 * CAUTION to TAS() callers: on some platforms TAS() may sometimes
60 * report failure to acquire a lock even when the lock is not locked.
61 * For example, on Alpha TAS() will "fail" if interrupted. Therefore
62 * TAS() must *always* be invoked in a retry loop as depicted, even when
63 * you are certain the lock is free.
65 * On most supported platforms, TAS() uses a tas() function written
66 * in assembly language to execute a hardware atomic-test-and-set
67 * instruction. Equivalent OS-supplied mutex routines could be used too.
69 * If no system-specific TAS() is available (ie, HAS_TEST_AND_SET is not
70 * defined), then we fall back on an emulation that uses SysV semaphores.
71 * This emulation will be MUCH MUCH MUCH slower than a proper TAS()
72 * implementation, because of the cost of a kernel call per lock or unlock.
73 * An old report is that Postgres spends around 40% of its time in semop(2)
74 * when using the SysV semaphore code.
76 * Note to implementors: there are default implementations for all these
77 * macros at the bottom of the file. Check if your platform can use
78 * these or needs to override them.
84 #include "storage/ipc.h"
86 /* Platform-independent out-of-line support routines */
87 extern void s_lock(volatile slock_t *lock,
88 const char *file, const int line);
89 extern void s_lock_sleep(unsigned spins, int microsec,
90 volatile slock_t *lock,
91 const char *file, const int line);
94 #if defined(HAS_TEST_AND_SET)
98 /*************************************************************************
103 * Standard _asm format:
105 * __asm__ __volatile__(
109 * : "=r"(_res) return value, in register
110 * : "r"(lock) argument, 'lock pointer', in register
111 * : "r0"); inline code uses this register
115 #if defined(__i386__)
116 #define TAS(lock) tas(lock)
118 static __inline__ int
119 tas(volatile slock_t *lock)
121 register slock_t _res = 1;
123 __asm__ __volatile__(
126 : "=q"(_res), "=m"(*lock)
131 #endif /* __i386__ */
135 #define TAS(lock) tas(lock)
137 static __inline__ int
138 tas(volatile slock_t *lock)
142 __asm__ __volatile__(
144 : "=r"(ret), "=m"(*lock)
151 #endif /* __ia64__ */
154 #if defined(__arm__) || defined(__arm__)
155 #define TAS(lock) tas(lock)
157 static __inline__ int
158 tas(volatile slock_t *lock)
160 register slock_t _res = 1;
162 __asm__ __volatile__(
164 : "=r"(_res), "=m"(*lock)
165 : "0"(_res), "r"(lock));
171 #if defined(__s390__)
175 #define TAS(lock) tas(lock)
178 tas(volatile slock_t *lock)
182 __asm__ __volatile__(
188 : "=m"(lock), "=d"(_res)
195 #endif /* __s390__ */
198 #if defined(__sparc__)
199 #define TAS(lock) tas(lock)
201 static __inline__ int
202 tas(volatile slock_t *lock)
204 register slock_t _res = 1;
206 __asm__ __volatile__(
208 : "=r"(_res), "=m"(*lock)
213 #endif /* __sparc__ */
216 #if defined(__mc68000__) && defined(__linux__)
217 #define TAS(lock) tas(lock)
219 static __inline__ int
220 tas(volatile slock_t *lock)
224 __asm__ __volatile__(
227 : "=d"(rv), "=m"(*lock)
234 #endif /* defined(__mc68000__) && defined(__linux__) */
237 #if defined(NEED_VAX_TAS_ASM)
239 * VAXen -- even multiprocessor ones
240 * (thanks to Tom Ivar Helbekkmo)
242 #define TAS(lock) tas(lock)
244 typedef unsigned char slock_t;
246 static __inline__ int
247 tas(volatile slock_t *lock)
251 __asm__ __volatile__(
253 "bbssi $0, (%1), 1f;"
262 #endif /* NEED_VAX_TAS_ASM */
265 #if defined(NEED_NS32K_TAS_ASM)
266 #define TAS(lock) tas(lock)
268 static __inline__ int
269 tas(volatile slock_t *lock)
273 __asm__ __volatile__(
276 : "=m"(*lock), "=r"(_res));
280 #endif /* NEED_NS32K_TAS_ASM */
284 #else /* !__GNUC__ */
286 /***************************************************************************
287 * All non-gcc inlines
290 #if defined(NEED_I386_TAS_ASM) && defined(USE_UNIVEL_CC)
291 #define TAS(lock) tas(lock)
294 tas(volatile slock_t *s_lock)
296 /* UNIVEL wants %mem in column 1, so we don't pg_indent this file */
306 #endif /* defined(NEED_I386_TAS_ASM) && defined(USE_UNIVEL_CC) */
308 #endif /* defined(__GNUC__) */
312 /*************************************************************************
313 * These are the platforms that do not use inline assembler (and hence
314 * have common code for gcc and non-gcc compilers, if both are available).
321 * Correct multi-processor locking methods are explained in section 5.5.3
322 * of the Alpha AXP Architecture Handbook, which at this writing can be
323 * found at ftp://ftp.netbsd.org/pub/NetBSD/misc/dec-docs/index.html.
324 * For gcc we implement the handbook's code directly with inline assembler.
326 #if defined(__GNUC__)
328 #define TAS(lock) tas(lock)
329 #define S_UNLOCK(lock) \
332 __asm__ __volatile__ ("mb"); \
336 static __inline__ int
337 tas(volatile slock_t *lock)
339 register slock_t _res;
341 __asm__ __volatile__(
353 : "=m"(*lock), "=r"(_res)
360 #else /* !defined(__GNUC__) */
363 * The Tru64 compiler doesn't support gcc-style inline asm, but it does
364 * have some builtin functions that accomplish much the same results.
365 * For simplicity, slock_t is defined as long (ie, quadword) on Alpha
366 * regardless of the compiler in use. LOCK_LONG and UNLOCK_LONG only
367 * operate on an int (ie, longword), but that's OK as long as we define
368 * S_INIT_LOCK to zero out the whole quadword.
371 #include <alpha/builtins.h>
373 #define S_INIT_LOCK(lock) (*(lock) = 0)
374 #define TAS(lock) (__LOCK_LONG_RETRY((lock), 1) == 0)
375 #define S_UNLOCK(lock) __UNLOCK_LONG(lock)
377 #endif /* defined(__GNUC__) */
386 * Note that slock_t on PA-RISC is a structure instead of char
387 * (see include/port/hpux.h).
389 * a "set" slock_t has a single word cleared. a "clear" slock_t has
390 * all words set to non-zero. tas() is in tas.s
393 #define S_UNLOCK(lock) \
395 volatile slock_t *lock_ = (volatile slock_t *) (lock); \
396 lock_->sema[0] = lock_->sema[1] = lock_->sema[2] = lock_->sema[3] = -1; \
399 #define S_LOCK_FREE(lock) ( *(int *) (((long) (lock) + 15) & ~15) != 0)
408 * Note that slock_t under QNX is sem_t instead of char
410 #define TAS(lock) (sem_trywait((lock)) < 0)
411 #define S_UNLOCK(lock) sem_post((lock))
412 #define S_INIT_LOCK(lock) sem_init((lock), 1, 1)
413 #define S_LOCK_FREE(lock) ((lock)->value)
420 * slock_t is defined as a unsigned long. We use the standard SGI
423 * The following comment is left for historical reasons, but is probably
424 * not a good idea since the mutex ABI is supported.
426 * This stuff may be supplemented in the future with Masato Kataoka's MIPS-II
427 * assembly from his NECEWS SVR4 port, but we probably ought to retain this
428 * for the R3000 chips out there.
431 #define TAS(lock) (test_and_set(lock,1))
432 #define S_UNLOCK(lock) (test_then_and(lock,0))
433 #define S_INIT_LOCK(lock) (test_then_and(lock,0))
434 #define S_LOCK_FREE(lock) (test_then_add(lock,0) == 0)
439 * SINIX / Reliant UNIX
440 * slock_t is defined as a struct abilock_t, which has a single unsigned long
441 * member. (Basically same as SGI)
444 #define TAS(lock) (!acquire_lock(lock))
445 #define S_UNLOCK(lock) release_lock(lock)
446 #define S_INIT_LOCK(lock) init_lock(lock)
447 #define S_LOCK_FREE(lock) (stat_lock(lock) == UNLOCKED)
455 * Note that slock_t on POWER/POWER2/PowerPC is int instead of char
456 * (see storage/ipc.h).
458 #define TAS(lock) cs((int *) (lock), 0, 1)
462 #if defined (nextstep)
465 * slock_t is defined as a struct mutex.
468 #define S_LOCK(lock) mutex_lock(lock)
469 #define S_UNLOCK(lock) mutex_unlock(lock)
470 #define S_INIT_LOCK(lock) mutex_init(lock)
471 /* For Mach, we have to delve inside the entrails of `struct mutex'. Ick! */
472 #define S_LOCK_FREE(alock) ((alock)->lock == 0)
473 #endif /* nextstep */
477 #else /* !HAS_TEST_AND_SET */
480 * Fake spinlock implementation using SysV semaphores --- slow and prone
481 * to fall foul of kernel limits on number of semaphores, so don't use this
487 /* reference to semaphore used to implement this spinlock */
488 IpcSemaphoreId semId;
492 extern bool s_lock_free_sema(volatile slock_t *lock);
493 extern void s_unlock_sema(volatile slock_t *lock);
494 extern void s_init_lock_sema(volatile slock_t *lock);
495 extern int tas_sema(volatile slock_t *lock);
497 #define S_LOCK_FREE(lock) s_lock_free_sema(lock)
498 #define S_UNLOCK(lock) s_unlock_sema(lock)
499 #define S_INIT_LOCK(lock) s_init_lock_sema(lock)
500 #define TAS(lock) tas_sema(lock)
502 #endif /* HAS_TEST_AND_SET */
506 /****************************************************************************
507 * Default Definitions - override these above as needed.
511 #define S_LOCK(lock) \
514 s_lock((lock), __FILE__, __LINE__); \
518 #if !defined(S_LOCK_SLEEP)
519 #define S_LOCK_SLEEP(lock,spins) \
520 s_lock_sleep((spins), 0, (lock), __FILE__, __LINE__)
521 #endif /* S_LOCK_SLEEP */
523 #if !defined(S_LOCK_SLEEP_INTERVAL)
524 #define S_LOCK_SLEEP_INTERVAL(lock,spins,microsec) \
525 s_lock_sleep((spins), (microsec), (lock), __FILE__, __LINE__)
526 #endif /* S_LOCK_SLEEP_INTERVAL */
528 #if !defined(S_LOCK_FREE)
529 #define S_LOCK_FREE(lock) (*(lock) == 0)
530 #endif /* S_LOCK_FREE */
532 #if !defined(S_UNLOCK)
533 #define S_UNLOCK(lock) (*(lock) = 0)
534 #endif /* S_UNLOCK */
536 #if !defined(S_INIT_LOCK)
537 #define S_INIT_LOCK(lock) S_UNLOCK(lock)
538 #endif /* S_INIT_LOCK */
541 extern int tas(volatile slock_t *lock); /* in port/.../tas.s, or
544 #define TAS(lock) tas(lock)
548 #endif /* S_LOCK_H */