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.78 2001/01/18 23:40:26 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)
52 * S_LOCK_SLEEP(lock, spins++);
56 * where S_LOCK_SLEEP() checks for timeout and sleeps for a short
57 * interval. Callers that want to perform useful work while waiting
58 * can write out this entire loop and insert the "useful work" inside
61 * CAUTION to TAS() callers: on some platforms TAS() may sometimes
62 * report failure to acquire a lock even when the lock is not locked.
63 * For example, on Alpha TAS() will "fail" if interrupted. Therefore
64 * TAS() must *always* be invoked in a retry loop as depicted, even when
65 * you are certain the lock is free.
67 * On most supported platforms, TAS() uses a tas() function written
68 * in assembly language to execute a hardware atomic-test-and-set
69 * instruction. Equivalent OS-supplied mutex routines could be used too.
71 * If no system-specific TAS() is available (ie, HAS_TEST_AND_SET is not
72 * defined), then we fall back on an emulation that uses SysV semaphores.
73 * This emulation will be MUCH MUCH MUCH slower than a proper TAS()
74 * implementation, because of the cost of a kernel call per lock or unlock.
75 * An old report is that Postgres spends around 40% of its time in semop(2)
76 * when using the SysV semaphore code.
78 * Note to implementors: there are default implementations for all these
79 * macros at the bottom of the file. Check if your platform can use
80 * these or needs to override them.
86 #include "storage/ipc.h"
88 /* Platform-independent out-of-line support routines */
89 extern void s_lock(volatile slock_t *lock,
90 const char *file, const int line);
91 extern void s_lock_sleep(unsigned spins, int microsec,
92 volatile slock_t *lock,
93 const char *file, const int line);
96 #if defined(HAS_TEST_AND_SET)
100 /*************************************************************************
101 * All the gcc inlines
105 #if defined(__i386__)
106 #define TAS(lock) tas(lock)
108 static __inline__ int
109 tas(volatile slock_t *lock)
111 register slock_t _res = 1;
113 __asm__("lock; xchgb %0,%1": "=q"(_res), "=m"(*lock):"0"(_res));
117 #endif /* __i386__ */
121 #define TAS(lock) tas(lock)
123 static __inline__ int
124 tas (volatile slock_t *lock)
128 __asm__ __volatile__(
130 : "=r"(ret), "=m"(*lock)
136 #endif /* __ia64__ */
139 #if defined(__arm__) || defined(__arm__)
140 #define TAS(lock) tas(lock)
142 static __inline__ int
143 tas(volatile slock_t *lock)
145 register slock_t _res = 1;
147 __asm__("swpb %0, %0, [%3]": "=r"(_res), "=m"(*lock):"0"(_res), "r" (lock));
153 #if defined(__s390__)
157 #define TAS(lock) tas(lock)
160 tas(volatile slock_t *lock)
164 __asm__ __volatile(" la 1,1\n"
169 : "=m" (lock), "=d" (_res)
175 #endif /* __s390__ */
178 #if defined(__sparc__)
179 #define TAS(lock) tas(lock)
181 static __inline__ int
182 tas(volatile slock_t *lock)
184 register slock_t _res = 1;
186 __asm__("ldstub [%2], %0" \
187 : "=r"(_res), "=m"(*lock) \
192 #endif /* __sparc__ */
195 #if defined(__mc68000__) && defined(__linux__)
196 #define TAS(lock) tas(lock)
198 static __inline__ int
199 tas(volatile slock_t *lock)
203 __asm__ __volatile__ (
205 : "=d" (rv), "=m"(*lock)
211 #endif /* defined(__mc68000__) && defined(__linux__) */
214 #if defined(NEED_VAX_TAS_ASM)
216 * VAXen -- even multiprocessor ones
217 * (thanks to Tom Ivar Helbekkmo)
219 #define TAS(lock) tas(lock)
221 typedef unsigned char slock_t;
223 static __inline__ int
224 tas(volatile slock_t *lock)
228 __asm__(" movl $1, r0 \
232 : "=r"(_res) /* return value, in register */
233 : "r"(lock) /* argument, 'lock pointer', in register */
234 : "r0"); /* inline code uses this register */
238 #endif /* NEED_VAX_TAS_ASM */
241 #if defined(NEED_NS32K_TAS_ASM)
242 #define TAS(lock) tas(lock)
244 static __inline__ int
245 tas(volatile slock_t *lock)
248 __asm__("sbitb 0, %0 \n\
250 : "=m"(*lock), "=r"(_res));
254 #endif /* NEED_NS32K_TAS_ASM */
258 #else /* !__GNUC__ */
260 /***************************************************************************
261 * All non-gcc inlines
264 #if defined(NEED_I386_TAS_ASM) && defined(USE_UNIVEL_CC)
265 #define TAS(lock) tas(lock)
268 tas(volatile slock_t *s_lock)
270 /* UNIVEL wants %mem in column 1, so we don't pg_indent this file */
280 #endif /* defined(NEED_I386_TAS_ASM) && defined(USE_UNIVEL_CC) */
282 #endif /* defined(__GNUC__) */
286 /*************************************************************************
287 * These are the platforms that do not use inline assembler (and hence
288 * have common code for gcc and non-gcc compilers, if both are available).
295 * Correct multi-processor locking methods are explained in section 5.5.3
296 * of the Alpha AXP Architecture Handbook, which at this writing can be
297 * found at ftp://ftp.netbsd.org/pub/NetBSD/misc/dec-docs/index.html.
298 * For gcc we implement the handbook's code directly with inline assembler.
300 #if defined(__GNUC__)
302 #define TAS(lock) tas(lock)
303 #define S_UNLOCK(lock) do { __asm__ volatile ("mb"); *(lock) = 0; } while (0)
305 static __inline__ int
306 tas(volatile slock_t *lock)
308 register slock_t _res;
321 3: \n" : "=m"(*lock), "=r"(_res) : : "0");
326 #else /* !defined(__GNUC__) */
329 * The Tru64 compiler doesn't support gcc-style inline asm, but it does
330 * have some builtin functions that accomplish much the same results.
331 * For simplicity, slock_t is defined as long (ie, quadword) on Alpha
332 * regardless of the compiler in use. LOCK_LONG and UNLOCK_LONG only
333 * operate on an int (ie, longword), but that's OK as long as we define
334 * S_INIT_LOCK to zero out the whole quadword.
337 #include <alpha/builtins.h>
339 #define S_INIT_LOCK(lock) (*(lock) = 0)
340 #define TAS(lock) (__LOCK_LONG_RETRY((lock), 1) == 0)
341 #define S_UNLOCK(lock) __UNLOCK_LONG(lock)
343 #endif /* defined(__GNUC__) */
352 * Note that slock_t on PA-RISC is a structure instead of char
353 * (see include/port/hpux.h).
355 * a "set" slock_t has a single word cleared. a "clear" slock_t has
356 * all words set to non-zero. tas() is in tas.s
359 #define S_UNLOCK(lock) \
361 volatile slock_t *lock_ = (volatile slock_t *) (lock); \
362 lock_->sema[0] = lock_->sema[1] = lock_->sema[2] = lock_->sema[3] = -1; \
365 #define S_LOCK_FREE(lock) ( *(int *) (((long) (lock) + 15) & ~15) != 0)
374 * Note that slock_t under QNX is sem_t instead of char
376 #define TAS(lock) (sem_trywait((lock)) < 0)
377 #define S_UNLOCK(lock) sem_post((lock))
378 #define S_INIT_LOCK(lock) sem_init((lock), 1, 1)
379 #define S_LOCK_FREE(lock) ((lock)->value)
386 * slock_t is defined as a unsigned long. We use the standard SGI
389 * The following comment is left for historical reasons, but is probably
390 * not a good idea since the mutex ABI is supported.
392 * This stuff may be supplemented in the future with Masato Kataoka's MIPS-II
393 * assembly from his NECEWS SVR4 port, but we probably ought to retain this
394 * for the R3000 chips out there.
397 #define TAS(lock) (test_and_set(lock,1))
398 #define S_UNLOCK(lock) (test_then_and(lock,0))
399 #define S_INIT_LOCK(lock) (test_then_and(lock,0))
400 #define S_LOCK_FREE(lock) (test_then_add(lock,0) == 0)
405 * SINIX / Reliant UNIX
406 * slock_t is defined as a struct abilock_t, which has a single unsigned long
407 * member. (Basically same as SGI)
410 #define TAS(lock) (!acquire_lock(lock))
411 #define S_UNLOCK(lock) release_lock(lock)
412 #define S_INIT_LOCK(lock) init_lock(lock)
413 #define S_LOCK_FREE(lock) (stat_lock(lock) == UNLOCKED)
421 * Note that slock_t on POWER/POWER2/PowerPC is int instead of char
422 * (see storage/ipc.h).
424 #define TAS(lock) cs((int *) (lock), 0, 1)
428 #if defined (nextstep)
431 * slock_t is defined as a struct mutex.
434 #define S_LOCK(lock) mutex_lock(lock)
435 #define S_UNLOCK(lock) mutex_unlock(lock)
436 #define S_INIT_LOCK(lock) mutex_init(lock)
437 /* For Mach, we have to delve inside the entrails of `struct mutex'. Ick! */
438 #define S_LOCK_FREE(alock) ((alock)->lock == 0)
439 #endif /* nextstep */
443 #else /* !HAS_TEST_AND_SET */
446 * Fake spinlock implementation using SysV semaphores --- slow and prone
447 * to fall foul of kernel limits on number of semaphores, so don't use this
453 /* reference to semaphore used to implement this spinlock */
454 IpcSemaphoreId semId;
458 extern bool s_lock_free_sema(volatile slock_t *lock);
459 extern void s_unlock_sema(volatile slock_t *lock);
460 extern void s_init_lock_sema(volatile slock_t *lock);
461 extern int tas_sema(volatile slock_t *lock);
463 #define S_LOCK_FREE(lock) s_lock_free_sema(lock)
464 #define S_UNLOCK(lock) s_unlock_sema(lock)
465 #define S_INIT_LOCK(lock) s_init_lock_sema(lock)
466 #define TAS(lock) tas_sema(lock)
468 #endif /* HAS_TEST_AND_SET */
472 /****************************************************************************
473 * Default Definitions - override these above as needed.
477 #define S_LOCK(lock) \
480 s_lock((lock), __FILE__, __LINE__); \
484 #if !defined(S_LOCK_SLEEP)
485 #define S_LOCK_SLEEP(lock,spins) \
486 s_lock_sleep((spins), 0, (lock), __FILE__, __LINE__)
487 #endif /* S_LOCK_SLEEP */
489 #if !defined(S_LOCK_SLEEP_INTERVAL)
490 #define S_LOCK_SLEEP_INTERVAL(lock,spins,microsec) \
491 s_lock_sleep((spins), (microsec), (lock), __FILE__, __LINE__)
492 #endif /* S_LOCK_SLEEP_INTERVAL */
494 #if !defined(S_LOCK_FREE)
495 #define S_LOCK_FREE(lock) (*(lock) == 0)
496 #endif /* S_LOCK_FREE */
498 #if !defined(S_UNLOCK)
499 #define S_UNLOCK(lock) (*(lock) = 0)
500 #endif /* S_UNLOCK */
502 #if !defined(S_INIT_LOCK)
503 #define S_INIT_LOCK(lock) S_UNLOCK(lock)
504 #endif /* S_INIT_LOCK */
507 extern int tas(volatile slock_t *lock); /* in port/.../tas.s, or
510 #define TAS(lock) tas(lock)
514 #endif /* S_LOCK_H */