]> granicus.if.org Git - postgresql/blob - src/include/storage/s_lock.h
Tom Lane wrote:
[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  * Portions Copyright (c) 1996-2000, PostgreSQL, Inc
7  * Portions Copyright (c) 1994, Regents of the University of California
8  *
9  *
10  * IDENTIFICATION
11  *        $Header: /cvsroot/pgsql/src/include/storage/s_lock.h,v 1.72 2000/10/08 04:38:21 momjian Exp $
12  *
13  *-------------------------------------------------------------------------
14  */
15
16 /*
17  *       DESCRIPTION
18  *              The public macros that must be provided are:
19  *
20  *              void S_INIT_LOCK(slock_t *lock)
21  *
22  *              void S_LOCK(slock_t *lock)
23  *
24  *              void S_UNLOCK(slock_t *lock)
25  *
26  *              void S_LOCK_FREE(slock_t *lock)
27  *                      Tests if the lock is free. Returns non-zero if free, 0 if locked.
28  *
29  *              The S_LOCK() macro      implements a primitive but still useful random
30  *              backoff to avoid hordes of busywaiting lockers chewing CPU.
31  *
32  *              Effectively:
33  *              void
34  *              S_LOCK(slock_t *lock)
35  *              {
36  *                      while (TAS(lock))
37  *                      {
38  *                      // back off the cpu for a semi-random short time
39  *                      }
40  *              }
41  *
42  *              This implementation takes advantage of a tas function written
43  *              (in assembly language) on machines that have a native test-and-set
44  *              instruction. Alternative mutex implementations may also be used.
45  *              This function is hidden under the TAS macro to allow substitutions.
46  *
47  *              #define TAS(lock) tas(lock)
48  *              int tas(slock_t *lock)          // True if lock already set
49  *
50  *              There are default implementations for all these macros at the bottom
51  *              of this file. Check if your platform can use these or needs to
52  *              override them.
53  *
54  *      NOTES
55  *              If none of this can be done, POSTGRES will default to using
56  *              System V semaphores (and take a large performance hit -- around 40%
57  *              of its time on a DS5000/240 is spent in semop(3)...).
58  *
59  *              AIX has a test-and-set but the recommended interface is the cs(3)
60  *              system call.  This provides an 8-instruction (plus system call
61  *              overhead) uninterruptible compare-and-set operation.  True
62  *              spinlocks might be faster but using cs(3) still speeds up the
63  *              regression test suite by about 25%.  I don't have an assembler
64  *              manual for POWER in any case.
65  *
66  */
67 #if !defined(S_LOCK_H)
68 #define S_LOCK_H
69
70 #include "storage/ipc.h"
71
72 extern void s_lock_sleep(unsigned spin);
73
74 #if defined(HAS_TEST_AND_SET)
75
76
77 #if defined(__GNUC__)
78 /*************************************************************************
79  * All the gcc inlines
80  */
81
82
83 #if defined(__i386__)
84 #define TAS(lock) tas(lock)
85
86 static __inline__ int
87 tas(volatile slock_t *lock)
88 {
89         register slock_t _res = 1;
90
91 __asm__("lock; xchgb %0,%1": "=q"(_res), "=m"(*lock):"0"(_res));
92         return (int) _res;
93 }
94
95 #endif   /* __i386__ */
96
97
98 #ifdef __ia64__
99 #define TAS(lock) tas(lock)
100
101 static __inline__ int
102 tas (volatile slock_t *lock)
103 {
104   long int ret;
105
106   __asm__ __volatile__(
107        "xchg4 %0=%1,%2"
108        : "=r"(ret), "=m"(*lock)
109        : "r"(1), "1"(*lock)
110        : "memory");
111
112   return (int) ret;
113 }
114 #endif /* __ia64__ */
115
116
117 #if defined(__arm__) || defined(__arm__)
118 #define TAS(lock) tas(lock)
119
120 static __inline__ int
121 tas(volatile slock_t *lock)
122 {
123         register slock_t _res = 1;
124
125 __asm__("swpb %0, %0, [%3]": "=r"(_res), "=m"(*lock):"0"(_res), "r" (lock));
126         return (int) _res;
127 }
128
129 #endif   /* __arm__ */
130
131
132
133 #if defined(__sparc__)
134 #define TAS(lock) tas(lock)
135
136 static __inline__ int
137 tas(volatile slock_t *lock)
138 {
139         register slock_t _res = 1;
140
141         __asm__("ldstub [%2], %0" \
142 :                       "=r"(_res), "=m"(*lock) \
143 :                       "r"(lock));
144         return (int) _res;
145 }
146
147 #endif   /* __sparc__ */
148
149
150 #if defined(__mc68000__) && defined(__linux__)
151 #define TAS(lock) tas(lock)
152
153 static __inline__ int
154 tas(volatile slock_t *lock)
155 {
156         register int rv;
157         
158         __asm__ __volatile__ (
159                 "tas %1; sne %0"
160                 : "=d" (rv), "=m"(*lock)
161                 : "1" (*lock)
162                 : "cc" );
163         return rv;
164 }
165
166 #endif /* defined(__mc68000__) && defined(__linux__) */
167
168
169 #if defined(NEED_VAX_TAS_ASM)
170 /*
171  * VAXen -- even multiprocessor ones
172  * (thanks to Tom Ivar Helbekkmo)
173  */
174 #define TAS(lock) tas(lock)
175
176 typedef unsigned char slock_t;
177
178 static __inline__ int
179 tas(volatile slock_t *lock)
180 {
181         register        _res;
182
183         __asm__("       movl $1, r0 \
184                         bbssi $0, (%1), 1 f \
185                         clrl r0 \
186 1:                      movl r0, %0 "
187 :                       "=r"(_res)                      /* return value, in register */
188 :                       "r"(lock)                       /* argument, 'lock pointer', in register */
189 :                       "r0");                          /* inline code uses this register */
190         return (int) _res;
191 }
192
193 #endif   /* NEED_VAX_TAS_ASM */
194
195
196
197 #if defined(NEED_NS32K_TAS_ASM)
198 #define TAS(lock) tas(lock)
199
200 static __inline__ int
201 tas(volatile slock_t *lock)
202 {
203   register _res;
204   __asm__("sbitb 0, %0 \n\
205         sfsd %1"
206         : "=m"(*lock), "=r"(_res));
207   return (int) _res; 
208 }
209
210 #endif  /* NEED_NS32K_TAS_ASM */
211
212
213
214 #else                                                   /* __GNUC__ */
215 /***************************************************************************
216  * All non gcc
217  */
218
219 #if defined(__QNX__)
220 /*
221  * QNX 4
222  *
223  * Note that slock_t under QNX is sem_t instead of char
224  */
225 #define TAS(lock)       (sem_trywait((lock)) < 0)
226 #define S_UNLOCK(lock)  sem_post((lock))
227 #define S_INIT_LOCK(lock)       sem_init((lock), 1, 1)
228 #define S_LOCK_FREE(lock)       (lock)->value
229 #endif   /* __QNX__ */
230
231
232 #if defined(NEED_I386_TAS_ASM)
233 /* non gcc i386 based things */
234
235 #if defined(USE_UNIVEL_CC)
236 #define TAS(lock)       tas(lock)
237
238 asm int
239 tas(slock_t *s_lock)
240 {
241 /* UNIVEL wants %mem in column 1, so we don't pg_indent this file */
242 %mem s_lock
243         pushl %ebx
244         movl s_lock, %ebx
245         movl $255, %eax
246         lock
247         xchgb %al, (%ebx)
248         popl %ebx
249 }
250
251 #endif   /* USE_UNIVEL_CC */
252
253 #endif   /* NEED_I386_TAS_ASM */
254
255 #endif   /* defined(__GNUC__) */
256
257
258
259 /*************************************************************************
260  * These are the platforms that have common code for gcc and non-gcc
261  */
262
263
264 #if defined(__alpha)
265
266 #if defined(__osf__)
267 /*
268  * OSF/1 (Alpha AXP)
269  *
270  * Note that slock_t on the Alpha AXP is msemaphore instead of char
271  * (see storage/ipc.h).
272  */
273 #include <alpha/builtins.h>
274 #if 0
275 #define TAS(lock)         (msem_lock((lock), MSEM_IF_NOWAIT) < 0)
276 #define S_UNLOCK(lock) msem_unlock((lock), 0)
277 #define S_INIT_LOCK(lock)               msem_init((lock), MSEM_UNLOCKED)
278 #define S_LOCK_FREE(lock)         (!(lock)->msem_state)
279 #else
280 #define TAS(lock)         (__INTERLOCKED_TESTBITSS_QUAD((lock),0))
281 #endif
282
283 #else /* i.e. not __osf__ */
284
285 #define TAS(lock) tas(lock)
286 #define S_UNLOCK(lock) do { __asm__("mb"); *(lock) = 0; } while (0)
287
288 static __inline__ int
289 tas(volatile slock_t *lock)
290 {
291  register slock_t _res;
292
293 __asm__("        ldq   $0, %0                      \n\
294                                  bne   $0, 3f              \n\
295                                  ldq_l $0, %0                    \n\
296                                  bne   $0, 3f              \n\
297                                  or    $31, 1, $0                  \n\
298                                  stq_c $0, %0                              \n\
299                                  beq   $0, 2f                      \n\
300                                  bis   $31, $31, %1        \n\
301                                  mb                                                        \n\
302                                  jmp   $31, 4f                     \n\
303                           2: or    $31, 1, $0                      \n\
304                           3: bis   $0, $0, %1              \n\
305                           4: nop          ": "=m"(*lock), "=r"(_res): :"0");
306
307         return (int) _res;
308 }
309 #endif /* __osf__ */
310
311 #endif /* __alpha */
312
313
314 #if defined(__hpux)
315 /*
316  * HP-UX (PA-RISC)
317  *
318  * Note that slock_t on PA-RISC is a structure instead of char
319  * (see include/port/hpux.h).
320  *
321  * a "set" slock_t has a single word cleared.  a "clear" slock_t has
322  * all words set to non-zero. tas() in tas.s
323  */
324
325 #define S_UNLOCK(lock) \
326 do { \
327         volatile slock_t *lock_ = (volatile slock_t *) (lock); \
328         lock_->sema[0] = lock_->sema[1] = lock_->sema[2] = lock_->sema[3] = -1; \
329 } while (0)
330
331 #define S_LOCK_FREE(lock)       ( *(int *) (((long) (lock) + 15) & ~15) != 0)
332
333 #endif   /* __hpux */
334
335
336 #if defined(__sgi)
337 /*
338  * SGI IRIX 5
339  * slock_t is defined as a unsigned long. We use the standard SGI
340  * mutex API. 
341  *
342  * The following comment is left for historical reasons, but is probably
343  * not a good idea since the mutex ABI is supported.
344  *
345  * This stuff may be supplemented in the future with Masato Kataoka's MIPS-II
346  * assembly from his NECEWS SVR4 port, but we probably ought to retain this
347  * for the R3000 chips out there.
348  */
349 #include "mutex.h"
350 #define TAS(lock)       (test_and_set(lock,1))
351 #define S_UNLOCK(lock)  (test_then_and(lock,0))
352 #define S_INIT_LOCK(lock)       (test_then_and(lock,0))
353 #define S_LOCK_FREE(lock)       (test_then_add(lock,0) == 0)
354 #endif   /* __sgi */
355
356 #if defined(sinix)
357 /*
358  * SINIX / Reliant UNIX 
359  * slock_t is defined as a struct abilock_t, which has a single unsigned long
360  * member. (Basically same as SGI)
361  *
362  */
363 #define TAS(lock)       (!acquire_lock(lock))
364 #define S_UNLOCK(lock)  release_lock(lock)
365 #define S_INIT_LOCK(lock)       init_lock(lock)
366 #define S_LOCK_FREE(lock)       (stat_lock(lock) == UNLOCKED)
367 #endif   /* sinix */
368  
369
370 #if defined(_AIX)
371 /*
372  * AIX (POWER)
373  *
374  * Note that slock_t on POWER/POWER2/PowerPC is int instead of char
375  * (see storage/ipc.h).
376  */
377 #define TAS(lock)       cs((int *) (lock), 0, 1)
378 #endif   /* _AIX */
379
380
381 #if defined (nextstep)
382 /*
383  * NEXTSTEP (mach)
384  * slock_t is defined as a struct mutex.
385  */
386
387 #define S_LOCK(lock)    mutex_lock(lock)
388 #define S_UNLOCK(lock)  mutex_unlock(lock)
389 #define S_INIT_LOCK(lock)       mutex_init(lock)
390 /* For Mach, we have to delve inside the entrails of `struct mutex'.  Ick! */
391 #define S_LOCK_FREE(alock)      ((alock)->lock == 0)
392 #endif   /* nextstep */
393
394
395
396
397 /****************************************************************************
398  * Default Definitions - override these above as needed.
399  */
400
401 #if !defined(S_LOCK)
402 extern void s_lock(volatile slock_t *lock, const char *file, const int line);
403
404 #define S_LOCK(lock) \
405         do { \
406                 if (TAS((volatile slock_t *) lock)) \
407                         s_lock((volatile slock_t *) lock, __FILE__, __LINE__); \
408         } while (0)
409 #endif   /* S_LOCK */
410
411 #if !defined(S_LOCK_FREE)
412 #define S_LOCK_FREE(lock)       (*(lock) == 0)
413 #endif   /* S_LOCK_FREE */
414
415 #if !defined(S_UNLOCK)
416 #define S_UNLOCK(lock)          (*(lock) = 0)
417 #endif   /* S_UNLOCK */
418
419 #if !defined(S_INIT_LOCK)
420 #define S_INIT_LOCK(lock)       S_UNLOCK(lock)
421 #endif   /* S_INIT_LOCK */
422
423 #if !defined(TAS)
424 int                     tas(volatile slock_t *lock);            /* port/.../tas.s, or
425                                                                                                  * s_lock.c */
426
427 #define TAS(lock)               tas((volatile slock_t *) lock)
428 #endif   /* TAS */
429
430 #endif   /* HAS_TEST_AND_SET */
431 #endif   /* S_LOCK_H */
432