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.76 2000/12/29 21:31:20 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)
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 \
229 bbssi $0, (%1), 1 f \
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).
298 * Note that slock_t on the Alpha AXP is msemaphore instead of char
299 * (see storage/ipc.h).
301 #include <alpha/builtins.h>
303 #define TAS(lock) (msem_lock((lock), MSEM_IF_NOWAIT) < 0)
304 #define S_UNLOCK(lock) msem_unlock((lock), 0)
305 #define S_INIT_LOCK(lock) msem_init((lock), MSEM_UNLOCKED)
306 #define S_LOCK_FREE(lock) (!(lock)->msem_state)
308 #define TAS(lock) (__INTERLOCKED_TESTBITSS_QUAD((lock),0))
311 #else /* i.e. not __osf__ */
313 #define TAS(lock) tas(lock)
314 #define S_UNLOCK(lock) do { __asm__("mb"); *(lock) = 0; } while (0)
316 static __inline__ int
317 tas(volatile slock_t *lock)
319 register slock_t _res;
321 __asm__(" ldq $0, %0 \n\
332 3: bis $0, $0, %1 \n\
333 4: nop ": "=m"(*lock), "=r"(_res): :"0");
346 * Note that slock_t on PA-RISC is a structure instead of char
347 * (see include/port/hpux.h).
349 * a "set" slock_t has a single word cleared. a "clear" slock_t has
350 * all words set to non-zero. tas() is in tas.s
353 #define S_UNLOCK(lock) \
355 volatile slock_t *lock_ = (volatile slock_t *) (lock); \
356 lock_->sema[0] = lock_->sema[1] = lock_->sema[2] = lock_->sema[3] = -1; \
359 #define S_LOCK_FREE(lock) ( *(int *) (((long) (lock) + 15) & ~15) != 0)
368 * Note that slock_t under QNX is sem_t instead of char
370 #define TAS(lock) (sem_trywait((lock)) < 0)
371 #define S_UNLOCK(lock) sem_post((lock))
372 #define S_INIT_LOCK(lock) sem_init((lock), 1, 1)
373 #define S_LOCK_FREE(lock) ((lock)->value)
380 * slock_t is defined as a unsigned long. We use the standard SGI
383 * The following comment is left for historical reasons, but is probably
384 * not a good idea since the mutex ABI is supported.
386 * This stuff may be supplemented in the future with Masato Kataoka's MIPS-II
387 * assembly from his NECEWS SVR4 port, but we probably ought to retain this
388 * for the R3000 chips out there.
391 #define TAS(lock) (test_and_set(lock,1))
392 #define S_UNLOCK(lock) (test_then_and(lock,0))
393 #define S_INIT_LOCK(lock) (test_then_and(lock,0))
394 #define S_LOCK_FREE(lock) (test_then_add(lock,0) == 0)
399 * SINIX / Reliant UNIX
400 * slock_t is defined as a struct abilock_t, which has a single unsigned long
401 * member. (Basically same as SGI)
404 #define TAS(lock) (!acquire_lock(lock))
405 #define S_UNLOCK(lock) release_lock(lock)
406 #define S_INIT_LOCK(lock) init_lock(lock)
407 #define S_LOCK_FREE(lock) (stat_lock(lock) == UNLOCKED)
415 * Note that slock_t on POWER/POWER2/PowerPC is int instead of char
416 * (see storage/ipc.h).
418 #define TAS(lock) cs((int *) (lock), 0, 1)
422 #if defined (nextstep)
425 * slock_t is defined as a struct mutex.
428 #define S_LOCK(lock) mutex_lock(lock)
429 #define S_UNLOCK(lock) mutex_unlock(lock)
430 #define S_INIT_LOCK(lock) mutex_init(lock)
431 /* For Mach, we have to delve inside the entrails of `struct mutex'. Ick! */
432 #define S_LOCK_FREE(alock) ((alock)->lock == 0)
433 #endif /* nextstep */
437 #else /* !HAS_TEST_AND_SET */
440 * Fake spinlock implementation using SysV semaphores --- slow and prone
441 * to fall foul of kernel limits on number of semaphores, so don't use this
447 /* reference to semaphore used to implement this spinlock */
448 IpcSemaphoreId semId;
452 extern bool s_lock_free_sema(volatile slock_t *lock);
453 extern void s_unlock_sema(volatile slock_t *lock);
454 extern void s_init_lock_sema(volatile slock_t *lock);
455 extern int tas_sema(volatile slock_t *lock);
457 #define S_LOCK_FREE(lock) s_lock_free_sema(lock)
458 #define S_UNLOCK(lock) s_unlock_sema(lock)
459 #define S_INIT_LOCK(lock) s_init_lock_sema(lock)
460 #define TAS(lock) tas_sema(lock)
462 #endif /* HAS_TEST_AND_SET */
466 /****************************************************************************
467 * Default Definitions - override these above as needed.
471 #define S_LOCK(lock) \
474 s_lock((lock), __FILE__, __LINE__); \
478 #if !defined(S_LOCK_SLEEP)
479 #define S_LOCK_SLEEP(lock,spins) \
480 s_lock_sleep((spins), 0, (lock), __FILE__, __LINE__)
481 #endif /* S_LOCK_SLEEP */
483 #if !defined(S_LOCK_SLEEP_INTERVAL)
484 #define S_LOCK_SLEEP_INTERVAL(lock,spins,microsec) \
485 s_lock_sleep((spins), (microsec), (lock), __FILE__, __LINE__)
486 #endif /* S_LOCK_SLEEP_INTERVAL */
488 #if !defined(S_LOCK_FREE)
489 #define S_LOCK_FREE(lock) (*(lock) == 0)
490 #endif /* S_LOCK_FREE */
492 #if !defined(S_UNLOCK)
493 #define S_UNLOCK(lock) (*(lock) = 0)
494 #endif /* S_UNLOCK */
496 #if !defined(S_INIT_LOCK)
497 #define S_INIT_LOCK(lock) S_UNLOCK(lock)
498 #endif /* S_INIT_LOCK */
501 extern int tas(volatile slock_t *lock); /* in port/.../tas.s, or
504 #define TAS(lock) tas(lock)
508 #endif /* S_LOCK_H */