1 /*-------------------------------------------------------------------------
4 * This file contains the implementation (if any) for spinlocks.
6 * Copyright (c) 1994, Regents of the University of California
10 * $Header: /cvsroot/pgsql/src/include/storage/s_lock.h,v 1.59 1999/04/13 17:42:26 momjian Exp $
12 *-------------------------------------------------------------------------
17 * The public macros that must be provided are:
19 * void S_INIT_LOCK(slock_t *lock)
21 * void S_LOCK(slock_t *lock)
23 * void S_UNLOCK(slock_t *lock)
25 * void S_LOCK_FREE(slock_t *lock)
26 * Tests if the lock is free. Returns non-zero if free, 0 if locked.
28 * The S_LOCK() macro implements a primitive but still useful random
29 * backoff to avoid hordes of busywaiting lockers chewing CPU.
33 * S_LOCK(slock_t *lock)
37 * // back off the cpu for a semi-random short time
41 * This implementation takes advantage of a tas function written
42 * (in assembly language) on machines that have a native test-and-set
43 * instruction. Alternative mutex implementations may also be used.
44 * This function is hidden under the TAS macro to allow substitutions.
46 * #define TAS(lock) tas(lock)
47 * int tas(slock_t *lock) // True if lock already set
49 * There are default implementations for all these macros at the bottom
50 * of this file. Check if your platform can use these or needs to
54 * If none of this can be done, POSTGRES will default to using
55 * System V semaphores (and take a large performance hit -- around 40%
56 * of its time on a DS5000/240 is spent in semop(3)...).
58 * AIX has a test-and-set but the recommended interface is the cs(3)
59 * system call. This provides an 8-instruction (plus system call
60 * overhead) uninterruptible compare-and-set operation. True
61 * spinlocks might be faster but using cs(3) still speeds up the
62 * regression test suite by about 25%. I don't have an assembler
63 * manual for POWER in any case.
66 #if !defined(S_LOCK_H)
69 #include "storage/ipc.h"
71 extern void s_lock_sleep(unsigned spin);
73 #if defined(HAS_TEST_AND_SET)
77 /*************************************************************************
82 #define TAS(lock) tas(lock)
83 #define S_UNLOCK(lock) { __asm__("mb"); *(lock) = 0; }
86 tas(volatile slock_t *lock)
88 register slock_t _res;
90 __asm__(" ldq $0, %0 \n\
101 3: bis $0, $0, %1 \n\
102 4: nop ": "=m"(*lock), "=r"(_res): :"0");
112 #define TAS(lock) tas(lock)
114 static __inline__ int
115 tas(volatile slock_t *lock)
117 register slock_t _res = 1;
119 __asm__("lock; xchgb %0,%1": "=q"(_res), "=m"(*lock):"0"(_res));
127 #if defined(__arm32__)
128 #define TAS(lock) tas(lock)
130 static __inline__ int
131 tas(volatile slock_t *lock)
133 register slock_t _res = 1;
135 __asm__("swpb %0, %0, [%3]": "=r"(_res), "=m"(*lock):"0"(_res), "r" (lock));
139 #endif /* __arm32__ */
144 #define TAS(lock) tas(lock)
146 static __inline__ int
147 tas(volatile slock_t *lock)
149 register slock_t _res = 1;
151 __asm__("ldstub [%2], %0" \
152 : "=r"(_res), "=m"(*lock) \
161 #if defined(NEED_VAX_TAS_ASM)
163 * VAXen -- even multiprocessor ones
164 * (thanks to Tom Ivar Helbekkmo)
166 #define TAS(lock) tas(lock)
168 typedef unsigned char slock_t;
170 static __inline__ int
171 tas(volatile slock_t *lock)
175 __asm__(" movl $1, r0 \
176 bbssi $0, (%1), 1 f \
179 : "=r"(_res) /* return value, in register */
180 : "r"(lock) /* argument, 'lock pointer', in register */
181 : "r0"); /* inline code uses this register */
185 #endif /* NEED_VAX_TAS_ASM */
189 #if defined(NEED_NS32K_TAS_ASM)
190 #define TAS(lock) tas(lock)
192 static __inline__ int
193 tas(volatile slock_t *lock)
196 __asm__("sbitb 0, %0 \n\
198 : "=m"(*lock), "=r"(_res));
202 #endif /* NEED_NS32K_TAS_ASM */
207 /***************************************************************************
215 * Note that slock_t on the Alpha AXP is msemaphore instead of char
216 * (see storage/ipc.h).
218 #define TAS(lock) (msem_lock((lock), MSEM_IF_NOWAIT) < 0)
219 #define S_UNLOCK(lock) msem_unlock((lock), 0)
220 #define S_INIT_LOCK(lock) msem_init((lock), MSEM_UNLOCKED)
221 #define S_LOCK_FREE(lock) (!(lock)->msem_state)
226 #if defined(NEED_I386_TAS_ASM)
227 /* non gcc i386 based things */
229 #if defined(USE_UNIVEL_CC)
230 #define TAS(lock) tas(lock)
235 /* UNIVEL wants %mem in column 1, so we don't pg_indent this file */
245 #endif /* USE_UNIVEL_CC */
247 #endif /* NEED_I386_TAS_ASM */
249 #endif /* defined(__GNUC__) */
253 /*************************************************************************
254 * These are the platforms that have common code for gcc and non-gcc
261 * Note that slock_t on PA-RISC is a structure instead of char
262 * (see include/port/hpux.h).
264 * a "set" slock_t has a single word cleared. a "clear" slock_t has
265 * all words set to non-zero. tas() in tas.s
268 #define S_UNLOCK(lock) \
270 volatile slock_t *lock_ = (volatile slock_t *) (lock); \
271 lock_->sema[0] = lock_->sema[1] = lock_->sema[2] = lock_->sema[3] = -1; \
274 #define S_LOCK_FREE(lock) ( *(int *) (((long) (lock) + 15) & ~15) != 0)
282 * slock_t is defined as a unsigned long. We use the standard SGI
285 * The following comment is left for historical reasons, but is probably
286 * not a good idea since the mutex ABI is supported.
288 * This stuff may be supplemented in the future with Masato Kataoka's MIPS-II
289 * assembly from his NECEWS SVR4 port, but we probably ought to retain this
290 * for the R3000 chips out there.
293 #define TAS(lock) (test_and_set(lock,1))
294 #define S_UNLOCK(lock) (test_then_and(lock,0))
295 #define S_INIT_LOCK(lock) (test_then_and(lock,0))
296 #define S_LOCK_FREE(lock) (test_then_add(lock,0) == 0)
301 * SINIX / Reliant UNIX
302 * slock_t is defined as a struct abilock_t, which has a single unsigned long
303 * member. (Basically same as SGI)
306 #define TAS(lock) (!acquire_lock(lock))
307 #define S_UNLOCK(lock) release_lock(lock)
308 #define S_INIT_LOCK(lock) init_lock(lock)
309 #define S_LOCK_FREE(lock) (stat_lock(lock) == UNLOCKED)
317 * Note that slock_t on POWER/POWER2/PowerPC is int instead of char
318 * (see storage/ipc.h).
320 #define TAS(lock) cs((int *) (lock), 0, 1)
324 #if defined (nextstep)
327 * slock_t is defined as a struct mutex.
330 #define S_LOCK(lock) mutex_lock(lock)
331 #define S_UNLOCK(lock) mutex_unlock(lock)
332 #define S_INIT_LOCK(lock) mutex_init(lock)
333 /* For Mach, we have to delve inside the entrails of `struct mutex'. Ick! */
334 #define S_LOCK_FREE(alock) ((alock)->lock == 0)
335 #endif /* nextstep */
340 /****************************************************************************
341 * Default Definitions - override these above as needed.
345 extern void s_lock(volatile slock_t *lock, const char *file, const int line);
347 #define S_LOCK(lock) \
349 if (TAS((volatile slock_t *) lock)) \
350 s_lock((volatile slock_t *) lock, __FILE__, __LINE__); \
354 #if !defined(S_LOCK_FREE)
355 #define S_LOCK_FREE(lock) (*(lock) == 0)
356 #endif /* S_LOCK_FREE */
358 #if !defined(S_UNLOCK)
359 #define S_UNLOCK(lock) (*(lock) = 0)
360 #endif /* S_UNLOCK */
362 #if !defined(S_INIT_LOCK)
363 #define S_INIT_LOCK(lock) S_UNLOCK(lock)
364 #endif /* S_INIT_LOCK */
367 int tas(volatile slock_t *lock); /* port/.../tas.s, or
370 #define TAS(lock) tas((volatile slock_t *) lock)
373 #endif /* HAS_TEST_AND_SET */
374 #endif /* S_LOCK_H */