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.35 1998/06/15 18:40:04 momjian Exp $
12 *-------------------------------------------------------------------------
16 * The public functions that must be provided are:
18 * void S_INIT_LOCK(slock_t *lock)
20 * void S_LOCK(slock_t *lock)
22 * void S_UNLOCK(slock_t *lock)
24 * void S_LOCK_FREE(slock_t *lock)
25 * Tests if the lock is free. Returns non-zero if free, 0 if locked.
27 * The S_LOCK() macro implements a primitive but still useful random
28 * backoff to avoid hordes of busywaiting lockers chewing CPU.
32 * S_LOCK(slock_t *lock)
36 * // back off the cpu for a semi-random short time
40 * This implementation takes advantage of a tas function written
41 * (in assembly language) on machines that have a native test-and-set
42 * instruction. Alternative mutex implementations may also be used.
43 * This function is hidden under the TAS macro to allow substitutions.
45 * #define TAS(lock) tas(lock)
46 * int tas(slock_t *lock) // True if lock already set
48 * If none of this can be done, POSTGRES will default to using
49 * System V semaphores (and take a large performance hit -- around 40%
50 * of its time on a DS5000/240 is spent in semop(3)...).
53 * AIX has a test-and-set but the recommended interface is the cs(3)
54 * system call. This provides an 8-instruction (plus system call
55 * overhead) uninterruptible compare-and-set operation. True
56 * spinlocks might be faster but using cs(3) still speeds up the
57 * regression test suite by about 25%. I don't have an assembler
58 * manual for POWER in any case.
60 * There are default implementations for all these macros at the bottom
61 * of this file. Check if your platform can use these or needs to
65 #if !defined(S_LOCK_H)
68 #include "storage/ipc.h"
70 #if defined(HAS_TEST_AND_SET)
73 /***************************************************************************
79 #define S_UNLOCK(lock) { __asm__("mb"); *(lock) = 0; }
87 /***************************************************************************
91 #if defined (nextstep)
94 * slock_t is defined as a struct mutex.
97 #define S_LOCK(lock) mutex_lock(lock)
99 #define S_UNLOCK(lock) mutex_unlock(lock)
101 #define S_INIT_LOCK(lock) mutex_init(lock)
103 /* For Mach, we have to delve inside the entrails of `struct mutex'. Ick! */
104 #define S_LOCK_FREE(alock) ((alock)->lock == 0)
106 #endif /* nextstep */
113 * slock_t is defined as a struct abilock_t, which has a single unsigned long
116 * This stuff may be supplemented in the future with Masato Kataoka's MIPS-II
117 * assembly from his NECEWS SVR4 port, but we probably ought to retain this
118 * for the R3000 chips out there.
120 #define TAS(lock) (!acquire_lock(lock))
122 #define S_UNLOCK(lock) release_lock(lock)
124 #define S_INIT_LOCK(lock) init_lock(lock)
126 #define S_LOCK_FREE(lock) (stat_lock(lock) == UNLOCKED)
136 * Note that slock_t on the Alpha AXP is msemaphore instead of char
137 * (see storage/ipc.h).
139 #define TAS(lock) (msem_lock((lock), MSEM_IF_NOWAIT) < 0)
141 #define S_UNLOCK(lock) msem_unlock((lock), 0)
143 #define S_INIT_LOCK(lock) msem_init((lock), MSEM_UNLOCKED)
145 #define S_LOCK_FREE(lock) (!(lock)->msem_state)
155 * Note that slock_t on POWER/POWER2/PowerPC is int instead of char
156 * (see storage/ipc.h).
158 #define TAS(lock) cs((int *) (lock), 0, 1)
168 * Note that slock_t on PA-RISC is a structure instead of char
169 * (see storage/ipc.h).
171 * a "set" slock_t has a single word cleared. a "clear" slock_t has
172 * all words set to non-zero. tas() in tas.s
174 static slock_t clear_lock =
177 #define S_UNLOCK(lock) (*(lock) = clear_lock) /* struct assignment */
179 #define S_LOCK_FREE(lock) ( *(int *) (((long) (lock) + 15) & ~15) != 0)
185 #endif /* else defined(linux) */
190 /****************************************************************************
191 * Default Definitions - override these above as needed.
196 #include <sys/time.h>
198 #define S_NSPINCYCLE 16
199 #define S_MAX_BUSY 1000 * S_NSPINCYCLE
201 extern int s_spincycle[];
202 extern void s_lock_stuck(slock_t *lock, char *file, int line);
204 #if defined(S_LOCK_DEBUG)
206 extern void s_lock(slock_t *lock);
208 #define S_LOCK(lock) s_lock(lock, __FILE__, __LINE__)
210 #else /* S_LOCK_DEBUG */
212 #define S_LOCK(lock) \
217 struct timeval delay; \
219 delay.tv_usec = s_spincycle[spins++ % S_NSPINCYCLE]; \
220 (void) select(0, NULL, NULL, NULL, &delay); \
221 if (spins > S_MAX_BUSY) \
223 /* It's been well over a minute... */ \
224 s_lock_stuck(lock, __FILE__, __LINE__); \
229 #endif /* S_LOCK_DEBUG */
234 #if !defined(S_LOCK_FREE)
235 #define S_LOCK_FREE(lock) ((*lock) == 0)
236 #endif /* S_LOCK_FREE */
238 #if !defined(S_UNLOCK)
239 #define S_UNLOCK(lock) (*(lock) = 0)
240 #endif /* S_UNLOCK */
242 #if !defined(S_INIT_LOCK)
243 #define S_INIT_LOCK(lock) S_UNLOCK(lock)
244 #endif /* S_INIT_LOCK */
247 int tas(slock_t *lock); /* port/.../tas.s, or s_lock.c */
249 #define TAS(lock) tas(lock)
253 #endif /* HAS_TEST_AND_SET */
255 #endif /* S_LOCK_H */