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.90 2001/02/18 04:39:42 tgl 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"
87 /* Platform-independent out-of-line support routines */
88 extern void s_lock(volatile slock_t *lock,
89 const char *file, const int line);
90 extern void s_lock_sleep(unsigned spins, int timeout, int microsec,
91 volatile slock_t *lock,
92 const char *file, const int line);
95 #if defined(HAS_TEST_AND_SET)
99 /*************************************************************************
100 * All the gcc inlines
104 * Standard gcc asm format:
106 __asm__ __volatile__(
110 : "=r"(_res) return value, in register
111 : "r"(lock) argument, 'lock pointer', in register
112 : "r0"); inline code uses this register
116 #if defined(__i386__) && !defined(__QNX__)
117 #define TAS(lock) tas(lock)
119 static __inline__ int
120 tas(volatile slock_t *lock)
122 register slock_t _res = 1;
124 __asm__ __volatile__(
127 : "=q"(_res), "=m"(*lock)
132 #endif /* __i386__ */
136 #define TAS(lock) tas(lock)
138 static __inline__ int
139 tas(volatile slock_t *lock)
143 __asm__ __volatile__(
145 : "=r"(ret), "=m"(*lock)
152 #endif /* __ia64__ */
155 #if defined(__arm__) || defined(__arm__)
156 #define TAS(lock) tas(lock)
158 static __inline__ int
159 tas(volatile slock_t *lock)
161 register slock_t _res = 1;
163 __asm__ __volatile__(
164 " swpb %0, %0, [%3] \n"
165 : "=r"(_res), "=m"(*lock)
166 : "0"(_res), "r"(lock));
172 #if defined(__s390__)
176 #define TAS(lock) tas(lock)
179 tas(volatile slock_t *lock)
183 __asm__ __volatile__(
189 : "=m"(lock), "=d"(_res)
196 #endif /* __s390__ */
199 #if defined(__sparc__)
200 #define TAS(lock) tas(lock)
202 static __inline__ int
203 tas(volatile slock_t *lock)
205 register slock_t _res = 1;
207 __asm__ __volatile__(
208 " ldstub [%2], %0 \n"
209 : "=r"(_res), "=m"(*lock)
214 #endif /* __sparc__ */
217 #if defined(__mc68000__) && defined(__linux__)
218 #define TAS(lock) tas(lock)
220 static __inline__ int
221 tas(volatile slock_t *lock)
225 __asm__ __volatile__(
229 : "=d"(rv), "=m"(*lock)
236 #endif /* defined(__mc68000__) && defined(__linux__) */
239 #if defined(NEED_VAX_TAS_ASM)
241 * VAXen -- even multiprocessor ones
242 * (thanks to Tom Ivar Helbekkmo)
244 #define TAS(lock) tas(lock)
246 static __inline__ int
247 tas(volatile slock_t *lock)
251 __asm__ __volatile__(
253 " bbssi $0, (%1), 1f \n"
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 \n"); \
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] = -1; \
397 lock_->sema[1] = -1; \
398 lock_->sema[2] = -1; \
399 lock_->sema[3] = -1; \
402 #define S_LOCK_FREE(lock) ( *(int *) (((long) (lock) + 15) & ~15) != 0)
411 * Note that slock_t under QNX is sem_t instead of char
413 #define TAS(lock) (sem_trywait((lock)) < 0)
414 #define S_UNLOCK(lock) sem_post((lock))
415 #define S_INIT_LOCK(lock) sem_init((lock), 1, 1)
416 #define S_LOCK_FREE(lock) ((lock)->value)
423 * slock_t is defined as a unsigned long. We use the standard SGI
426 * The following comment is left for historical reasons, but is probably
427 * not a good idea since the mutex ABI is supported.
429 * This stuff may be supplemented in the future with Masato Kataoka's MIPS-II
430 * assembly from his NECEWS SVR4 port, but we probably ought to retain this
431 * for the R3000 chips out there.
434 #define TAS(lock) (test_and_set(lock,1))
435 #define S_UNLOCK(lock) (test_then_and(lock,0))
436 #define S_INIT_LOCK(lock) (test_then_and(lock,0))
437 #define S_LOCK_FREE(lock) (test_then_add(lock,0) == 0)
442 * SINIX / Reliant UNIX
443 * slock_t is defined as a struct abilock_t, which has a single unsigned long
444 * member. (Basically same as SGI)
447 #define TAS(lock) (!acquire_lock(lock))
448 #define S_UNLOCK(lock) release_lock(lock)
449 #define S_INIT_LOCK(lock) init_lock(lock)
450 #define S_LOCK_FREE(lock) (stat_lock(lock) == UNLOCKED)
458 * Note that slock_t on POWER/POWER2/PowerPC is int instead of char
459 * (see storage/ipc.h).
461 #define TAS(lock) cs((int *) (lock), 0, 1)
465 #if defined (nextstep)
468 * slock_t is defined as a struct mutex.
471 #define S_LOCK(lock) mutex_lock(lock)
472 #define S_UNLOCK(lock) mutex_unlock(lock)
473 #define S_INIT_LOCK(lock) mutex_init(lock)
474 /* For Mach, we have to delve inside the entrails of `struct mutex'. Ick! */
475 #define S_LOCK_FREE(alock) ((alock)->lock == 0)
476 #endif /* nextstep */
480 #else /* !HAS_TEST_AND_SET */
483 * Fake spinlock implementation using SysV semaphores --- slow and prone
484 * to fall foul of kernel limits on number of semaphores, so don't use this
490 /* reference to semaphore used to implement this spinlock */
491 IpcSemaphoreId semId;
495 extern bool s_lock_free_sema(volatile slock_t *lock);
496 extern void s_unlock_sema(volatile slock_t *lock);
497 extern void s_init_lock_sema(volatile slock_t *lock);
498 extern int tas_sema(volatile slock_t *lock);
500 #define S_LOCK_FREE(lock) s_lock_free_sema(lock)
501 #define S_UNLOCK(lock) s_unlock_sema(lock)
502 #define S_INIT_LOCK(lock) s_init_lock_sema(lock)
503 #define TAS(lock) tas_sema(lock)
505 #endif /* HAS_TEST_AND_SET */
509 /****************************************************************************
510 * Default Definitions - override these above as needed.
514 #define S_LOCK(lock) \
517 s_lock((lock), __FILE__, __LINE__); \
521 #if !defined(S_LOCK_SLEEP)
522 #define S_LOCK_SLEEP(lock,spins,timeout) \
523 s_lock_sleep((spins), (timeout), 0, (lock), __FILE__, __LINE__)
524 #endif /* S_LOCK_SLEEP */
526 #if !defined(S_LOCK_SLEEP_INTERVAL)
527 #define S_LOCK_SLEEP_INTERVAL(lock,spins,timeout,microsec) \
528 s_lock_sleep((spins), (timeout), (microsec), (lock), __FILE__, __LINE__)
529 #endif /* S_LOCK_SLEEP_INTERVAL */
531 #if !defined(S_LOCK_FREE)
532 #define S_LOCK_FREE(lock) (*(lock) == 0)
533 #endif /* S_LOCK_FREE */
535 #if !defined(S_UNLOCK)
536 #define S_UNLOCK(lock) (*(lock) = 0)
537 #endif /* S_UNLOCK */
539 #if !defined(S_INIT_LOCK)
540 #define S_INIT_LOCK(lock) S_UNLOCK(lock)
541 #endif /* S_INIT_LOCK */
544 extern int tas(volatile slock_t *lock); /* in port/.../tas.s, or
547 #define TAS(lock) tas(lock)
551 #endif /* S_LOCK_H */