]> granicus.if.org Git - postgresql/blob - src/include/storage/s_lock.h
s_lock aix patch.
[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.55 1998/10/28 15:58:34 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
105 #endif   /* __alpha */
106
107
108
109 #if defined(i386)
110 #define TAS(lock) tas(lock)
111
112 static __inline__ int
113 tas(volatile slock_t *lock)
114 {
115         register slock_t _res = 1;
116
117 __asm__("lock; xchgb %0,%1": "=q"(_res), "=m"(*lock):"0"(_res));
118         return (int) _res;
119 }
120
121 #endif   /* i386 */
122
123
124
125 #if defined(sparc)
126 #define TAS(lock) tas(lock)
127
128 static __inline__ int
129 tas(volatile slock_t *lock)
130 {
131         register slock_t _res = 1;
132
133         __asm__("ldstub [%2], %0" \
134 :                       "=r"(_res), "=m"(*lock) \
135 :                       "r"(lock));
136         return (int) _res;
137 }
138
139 #endif   /* sparc */
140
141
142
143 #if defined(NEED_VAX_TAS_ASM)
144 /*
145  * VAXen -- even multiprocessor ones
146  * (thanks to Tom Ivar Helbekkmo)
147  */
148 #define TAS(lock) tas(lock)
149
150 typedef unsigned char slock_t;
151
152 static __inline__ int
153 tas(volatile slock_t *lock)
154 {
155         register        _res;
156
157         __asm__("       movl $1, r0 \
158                         bbssi $0, (%1), 1 f \
159                         clrl r0 \
160 1:                      movl r0, %0 "
161 :                       "=r"(_res)                      /* return value, in register */
162 :                       "r"(lock)                       /* argument, 'lock pointer', in register */
163 :                       "r0");                          /* inline code uses this register */
164         return (int) _res;
165 }
166
167 #endif   /* NEED_VAX_TAS_ASM */
168
169
170
171 #if defined(NEED_NS32K_TAS_ASM)
172 #define TAS(lock) tas(lock)
173
174 static __inline__ int
175 tas(volatile slock_t *lock)
176 {
177   register _res;
178   __asm__("sbitb 0, %0 \n\
179         sfsd %1"
180         : "=m"(*lock), "=r"(_res));
181   return (int) _res; 
182 }
183
184 #endif  /* NEED_NS32K_TAS_ASM */
185
186
187
188 #else                                                   /* __GNUC__ */
189 /***************************************************************************
190  * All non gcc
191  */
192
193 #if defined(__alpha)
194 /*
195  * OSF/1 (Alpha AXP)
196  *
197  * Note that slock_t on the Alpha AXP is msemaphore instead of char
198  * (see storage/ipc.h).
199  */
200 #define TAS(lock)       (msem_lock((lock), MSEM_IF_NOWAIT) < 0)
201 #define S_UNLOCK(lock)  msem_unlock((lock), 0)
202 #define S_INIT_LOCK(lock)       msem_init((lock), MSEM_UNLOCKED)
203 #define S_LOCK_FREE(lock)       (!(lock)->msem_state)
204 #endif   /* __alpha */
205
206
207
208 #if defined(NEED_I386_TAS_ASM)
209 /* non gcc i386 based things */
210
211 #if defined(USE_UNIVEL_CC)
212 #define TAS(lock)       tas(lock)
213
214 asm int
215 tas(slock_t *s_lock)
216 {
217 /* UNIVEL wants %mem in column 1, so we don't pg_indent this file */
218 %mem s_lock
219         pushl %ebx
220         movl s_lock, %ebx
221         movl $255, %eax
222         lock
223         xchgb %al, (%ebx)
224         popl %ebx
225 }
226
227 #endif   /* USE_UNIVEL_CC */
228
229 #endif   /* NEED_I386_TAS_ASM */
230
231 #endif   /* defined(__GNUC__) */
232
233
234
235 /*************************************************************************
236  * These are the platforms that have common code for gcc and non-gcc
237  */
238
239 #if defined(__hpux)
240 /*
241  * HP-UX (PA-RISC)
242  *
243  * Note that slock_t on PA-RISC is a structure instead of char
244  * (see include/port/hpux.h).
245  *
246  * a "set" slock_t has a single word cleared.  a "clear" slock_t has
247  * all words set to non-zero. tas() in tas.s
248  */
249
250 #define S_UNLOCK(lock) \
251 { \
252         volatile slock_t *lock_ = (volatile slock_t *) (lock); \
253         lock_->sema[0] = lock_->sema[1] = lock_->sema[2] = lock_->sema[3] = -1; \
254 }
255
256 #define S_LOCK_FREE(lock)       ( *(int *) (((long) (lock) + 15) & ~15) != 0)
257
258 #endif   /* __hpux */
259
260
261 #if defined(__sgi)
262 /*
263  * SGI IRIX 5
264  * slock_t is defined as a unsigned long. We use the standard SGI
265  * mutex API. 
266  *
267  * The following comment is left for historical reasons, but is probably
268  * not a good idea since the mutex ABI is supported.
269  *
270  * This stuff may be supplemented in the future with Masato Kataoka's MIPS-II
271  * assembly from his NECEWS SVR4 port, but we probably ought to retain this
272  * for the R3000 chips out there.
273  */
274 #include <mutex.h>
275 #define TAS(lock)       (test_and_set(lock,1))
276 #define S_UNLOCK(lock)  (test_then_and(lock,0))
277 #define S_INIT_LOCK(lock)       (test_then_and(lock,0))
278 #define S_LOCK_FREE(lock)       (test_then_add(lock,0) == 0)
279 #endif   /* __sgi */
280
281 #if defined(sinix)
282 /*
283  * SINIX / Reliant UNIX 
284  * slock_t is defined as a struct abilock_t, which has a single unsigned long
285  * member. (Basically same as SGI)
286  *
287  */
288 #define TAS(lock)       (!acquire_lock(lock))
289 #define S_UNLOCK(lock)  release_lock(lock)
290 #define S_INIT_LOCK(lock)       init_lock(lock)
291 #define S_LOCK_FREE(lock)       (stat_lock(lock) == UNLOCKED)
292 #endif   /* sinix */
293  
294
295 #if defined(_AIX)
296 /*
297  * AIX (POWER)
298  *
299  * Note that slock_t on POWER/POWER2/PowerPC is int instead of char
300  * (see storage/ipc.h).
301  */
302 #define TAS(lock)       cs((int *) (lock), 0, 1)
303 #endif   /* _AIX */
304
305
306 #if defined (nextstep)
307 /*
308  * NEXTSTEP (mach)
309  * slock_t is defined as a struct mutex.
310  */
311
312 #define S_LOCK(lock)    mutex_lock(lock)
313 #define S_UNLOCK(lock)  mutex_unlock(lock)
314 #define S_INIT_LOCK(lock)       mutex_init(lock)
315 /* For Mach, we have to delve inside the entrails of `struct mutex'.  Ick! */
316 #define S_LOCK_FREE(alock)      ((alock)->lock == 0)
317 #endif   /* nextstep */
318
319
320
321
322 /****************************************************************************
323  * Default Definitions - override these above as needed.
324  */
325
326 #if !defined(S_LOCK)
327 extern void s_lock(volatile slock_t *lock, const char *file, const int line);
328
329 #define S_LOCK(lock) \
330         do { \
331                 if (TAS((volatile slock_t *) lock)) \
332                         s_lock((volatile slock_t *) lock, __FILE__, __LINE__); \
333         } while (0)
334 #endif   /* S_LOCK */
335
336 #if !defined(S_LOCK_FREE)
337 #define S_LOCK_FREE(lock)       (*(lock) == 0)
338 #endif   /* S_LOCK_FREE */
339
340 #if !defined(S_UNLOCK)
341 #define S_UNLOCK(lock)          (*(lock) = 0)
342 #endif   /* S_UNLOCK */
343
344 #if !defined(S_INIT_LOCK)
345 #define S_INIT_LOCK(lock)       S_UNLOCK(lock)
346 #endif   /* S_INIT_LOCK */
347
348 #if !defined(TAS)
349 int                     tas(volatile slock_t *lock);            /* port/.../tas.s, or
350                                                                                                  * s_lock.c */
351
352 #define TAS(lock)               tas((volatile slock_t *) lock)
353 #endif   /* TAS */
354
355 #endif   /* HAS_TEST_AND_SET */
356 #endif   /* S_LOCK_H */