]> granicus.if.org Git - postgresql/blob - src/include/storage/s_lock.h
Fix problem brought in with 32K machine.
[postgresql] / src / include / storage / s_lock.h
1 /*-------------------------------------------------------------------------
2  *
3  * s_lock.h--
4  *         This file contains the implementation (if any) for spinlocks.
5  *
6  * Copyright (c) 1994, Regents of the University of California
7  *
8  *
9  * IDENTIFICATION
10  *        $Header: /cvsroot/pgsql/src/include/storage/s_lock.h,v 1.43 1998/07/20 17:45:49 momjian Exp $
11  *
12  *-------------------------------------------------------------------------
13  */
14
15 /*
16  *       DESCRIPTION
17  *              The public macros that must be provided are:
18  *
19  *              void S_INIT_LOCK(slock_t *lock)
20  *
21  *              void S_LOCK(slock_t *lock)
22  *
23  *              void S_UNLOCK(slock_t *lock)
24  *
25  *              void S_LOCK_FREE(slock_t *lock)
26  *                      Tests if the lock is free. Returns non-zero if free, 0 if locked.
27  *
28  *              The S_LOCK() macro      implements a primitive but still useful random
29  *              backoff to avoid hordes of busywaiting lockers chewing CPU.
30  *
31  *              Effectively:
32  *              void
33  *              S_LOCK(slock_t *lock)
34  *              {
35  *                      while (TAS(lock))
36  *                      {
37  *                      // back off the cpu for a semi-random short time
38  *                      }
39  *              }
40  *
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.
45  *
46  *              #define TAS(lock) tas(lock)
47  *              int tas(slock_t *lock)          // True if lock already set
48  *
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
51  *              override them.
52  *
53  *      NOTES
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)...).
57  *
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.
64  *
65  */
66 #if !defined(S_LOCK_H)
67 #define S_LOCK_H
68
69 #include "storage/ipc.h"
70
71 #if defined(HAS_TEST_AND_SET)
72
73
74 #if defined(__GNUC__)
75 /*************************************************************************
76  * All the gcc inlines
77  */
78
79 #if defined(__alpha)
80 #define TAS(lock) tas(lock)
81 #define S_UNLOCK(lock) { __asm__("mb"); *(lock) = 0; }
82
83 static __inline__ int
84 tas(volatile slock_t *lock)
85 {
86         register slock_t        _res;
87
88     __asm__("    ldq   $0, %0              \n\
89                  bne   $0, already_set     \n\
90                  ldq_l $0, %0              \n\
91                  bne   $0, already_set     \n\
92                  or    $31, 1, $0          \n\
93                  stq_c $0, %0              \n\
94                  beq   $0, stqc_fail       \n\
95         success: bis   $31, $31, %1        \n\
96                  mb                                \n\
97                  jmp   $31, end            \n\
98       stqc_fail: or    $31, 1, $0              \n\
99     already_set: bis   $0, $0, %1              \n\
100             end: nop      " : "=m"(*lock), "=r"(_res) : : "0");
101
102         return (int) _res;
103 }
104 #endif /* __alpha */
105
106
107
108 #if defined(i386)
109 #define TAS(lock) tas(lock)
110
111 static __inline__ int
112 tas(volatile slock_t *lock)
113 {
114         register slock_t        _res = 1;
115
116     __asm__("lock; xchgb %0,%1" : "=q"(_res), "=m"(*lock) : "0"(_res) );
117         return (int) _res;
118 }
119 #endif /* i386 */
120
121
122
123 #if defined(sparc)
124 #define TAS(lock) tas(lock)
125
126 static __inline__ int
127 tas(volatile slock_t *lock)
128 {
129         register slock_t        _res = 1;
130
131     __asm__("ldstub [%2], %0" \
132             : "=r" (_res), "=m" (*lock) \
133             : "r" (lock));
134         return (int) _res;
135 }
136 #endif /* sparc */
137
138
139
140 #if defined(NEED_VAX_TAS_ASM)
141 /*
142  * VAXen -- even multiprocessor ones
143  * (thanks to Tom Ivar Helbekkmo)
144  */
145 #define TAS(lock) tas(lock)
146
147 typedef unsigned char slock_t;
148
149 static __inline__ int
150 tas(volatile slock_t *lock)
151 {
152         register        _res;
153
154         __asm__("       movl $1, r0
155                 bbssi $0, (%1), 1f
156                 clrl r0
157   1:    movl r0, %0 "
158   :             "=r"(_res)                              /* return value, in register */
159   :             "r"(lock)                               /* argument, 'lock pointer', in register */
160   :             "r0");                                  /* inline code uses this register */
161         return (int) _res;
162 }
163 #endif /* NEED_VAX_TAS_ASM */
164
165
166
167
168 #else /* __GNUC__ */
169 /***************************************************************************
170  * All non gcc
171  */
172
173 #if defined(__alpha)
174 /*
175  * OSF/1 (Alpha AXP)
176  *
177  * Note that slock_t on the Alpha AXP is msemaphore instead of char
178  * (see storage/ipc.h).
179  */
180 #define TAS(lock)       (msem_lock((lock), MSEM_IF_NOWAIT) < 0)
181 #define S_UNLOCK(lock)  msem_unlock((lock), 0)
182 #define S_INIT_LOCK(lock)       msem_init((lock), MSEM_UNLOCKED)
183 #define S_LOCK_FREE(lock)       (!(lock)->msem_state)
184 #endif /* __alpha */
185
186
187
188 #if defined(NEED_I386_TAS_ASM)
189 /* non gcc i386 based things */
190
191 #if defined(USE_UNIVEL_CC) || defined(UNIXWARE)
192 #define TAS(lock)       tas(lock)
193
194 asm int 
195 tas(slock_t *s_lock)
196 {
197 % mem s_lock
198         pushl   %ebx
199         movl    s_lock,%ebx
200         movl    $255,%eax
201         lock
202         xchgb   %al,(%ebx)
203         popl    %ebx
204 }
205
206 #endif /* USE_UNIVEL_CC || UNIXWARE */
207
208 #endif /* NEED_I386_TAS_ASM */
209
210
211
212 #if defined(NEED_NS32K_TAS_ASM)
213
214 #define S_LOCK(lock)                            \
215 {                                               \
216     slock_t res = 1;                            \
217     while (res) {                               \
218       __asm__("movqd 0, r0");                   \
219       __asm__("sbitd r0, %0" : "=m"(*lock));    \
220       __asm__("sprb us, %0" : "=r" (res));      \
221       res = ((res >> 5) & 1);                   \
222     }                                           \
223 }
224
225 #endif /* NEED_NS32K_TAS_ASM */
226
227
228 #endif /* defined(__GNUC__) */
229
230
231
232 /*************************************************************************
233  * These are the platforms that have common code for gcc and non-gcc
234  */
235
236 #if defined(__hpux)
237 /*
238  * HP-UX (PA-RISC)
239  *
240  * Note that slock_t on PA-RISC is a structure instead of char
241  * (see storage/ipc.h).
242  *
243  * a "set" slock_t has a single word cleared.  a "clear" slock_t has
244  * all words set to non-zero. tas() in tas.s
245  */
246 static const slock_t clear_lock =
247 {{-1, -1, -1, -1}};
248
249 #define S_UNLOCK(lock)  (*(lock) = clear_lock)  /* struct assignment */
250 #define S_LOCK_FREE(lock)       ( *(int *) (((long) (lock) + 15) & ~15) != 0)
251 #endif /* __hpux */
252
253
254 #if defined(__sgi)
255 /*
256  * SGI IRIX 5
257  * slock_t is defined as a struct abilock_t, which has a single unsigned long
258  * member.
259  *
260  * This stuff may be supplemented in the future with Masato Kataoka's MIPS-II
261  * assembly from his NECEWS SVR4 port, but we probably ought to retain this
262  * for the R3000 chips out there.
263  */
264 #define TAS(lock)       (!acquire_lock(lock))
265 #define S_UNLOCK(lock)  release_lock(lock)
266 #define S_INIT_LOCK(lock)       init_lock(lock)
267 #define S_LOCK_FREE(lock)       (stat_lock(lock) == UNLOCKED)
268 #endif /* __sgi */
269
270
271 #if defined(_AIX)
272 /*
273  * AIX (POWER)
274  *
275  * Note that slock_t on POWER/POWER2/PowerPC is int instead of char
276  * (see storage/ipc.h).
277  */
278 #define TAS(lock)       cs((int *) (lock), 0, 1)
279 #endif /* _AIX */
280
281
282 #if defined (nextstep)
283 /*
284  * NEXTSTEP (mach)
285  * slock_t is defined as a struct mutex.
286  */
287
288 #define S_LOCK(lock)    mutex_lock(lock)
289 #define S_UNLOCK(lock)  mutex_unlock(lock)
290 #define S_INIT_LOCK(lock)       mutex_init(lock)
291 /* For Mach, we have to delve inside the entrails of `struct mutex'.  Ick! */
292 #define S_LOCK_FREE(alock)      ((alock)->lock == 0)
293 #endif /* nextstep */
294
295
296
297
298 /****************************************************************************
299  * Default Definitions - override these above as needed.
300  */
301
302 #if !defined(S_LOCK)
303 extern void s_lock(volatile slock_t *lock, const char *file, const int line);
304 #define S_LOCK(lock) \
305     if (TAS((volatile slock_t *) lock)) {\
306         s_lock((volatile slock_t *) lock, __FILE__, __LINE__); \
307     } else
308 #endif /* S_LOCK */
309
310 #if !defined(S_LOCK_FREE)
311 #define S_LOCK_FREE(lock)       (*(lock) == 0)
312 #endif /* S_LOCK_FREE */
313
314 #if !defined(S_UNLOCK)
315 #define S_UNLOCK(lock)          (*(lock) = 0)
316 #endif /* S_UNLOCK */
317
318 #if !defined(S_INIT_LOCK)
319 #define S_INIT_LOCK(lock)       S_UNLOCK(lock)
320 #endif /* S_INIT_LOCK */
321
322 #if !defined(TAS)
323 int     tas(volatile slock_t *lock); /* port/.../tas.s, or s_lock.c */
324 #define TAS(lock)               tas((volatile slock_t *) lock)
325 #endif /* TAS */
326
327 #endif /* HAS_TEST_AND_SET */
328 #endif /* S_LOCK_H */
329