]> granicus.if.org Git - postgresql/blob - src/include/storage/s_lock.h
Fix #error message to mention renamed option --disable-spinlocks.
[postgresql] / src / include / storage / s_lock.h
1 /*-------------------------------------------------------------------------
2  *
3  * s_lock.h
4  *         Hardware-dependent implementation of spinlocks.
5  *
6  *      NOTE: none of the macros in this file are intended to be called directly.
7  *      Call them through the hardware-independent macros in spin.h.
8  *
9  *      The following hardware-dependent macros must be provided for each
10  *      supported platform:
11  *
12  *      void S_INIT_LOCK(slock_t *lock)
13  *              Initialize a spinlock (to the unlocked state).
14  *
15  *      void S_LOCK(slock_t *lock)
16  *              Acquire a spinlock, waiting if necessary.
17  *              Time out and abort() if unable to acquire the lock in a
18  *              "reasonable" amount of time --- typically ~ 1 minute.
19  *
20  *      void S_UNLOCK(slock_t *lock)
21  *              Unlock a previously acquired lock.
22  *
23  *      bool S_LOCK_FREE(slock_t *lock)
24  *              Tests if the lock is free. Returns TRUE if free, FALSE if locked.
25  *              This does *not* change the state of the lock.
26  *
27  *      Note to implementors: there are default implementations for all these
28  *      macros at the bottom of the file.  Check if your platform can use
29  *      these or needs to override them.
30  *
31  *  Usually, S_LOCK() is implemented in terms of an even lower-level macro
32  *      TAS():
33  *
34  *      int TAS(slock_t *lock)
35  *              Atomic test-and-set instruction.  Attempt to acquire the lock,
36  *              but do *not* wait.      Returns 0 if successful, nonzero if unable
37  *              to acquire the lock.
38  *
39  *      TAS() is NOT part of the API, and should never be called directly.
40  *
41  *      CAUTION: on some platforms TAS() may sometimes report failure to acquire
42  *      a lock even when the lock is not locked.  For example, on Alpha TAS()
43  *      will "fail" if interrupted.  Therefore TAS() should always be invoked
44  *      in a retry loop, even if you are certain the lock is free.
45  *
46  *      ANOTHER CAUTION: be sure that TAS() and S_UNLOCK() represent sequence
47  *      points, ie, loads and stores of other values must not be moved across
48  *      a lock or unlock.  In most cases it suffices to make the operation be
49  *      done through a "volatile" pointer.
50  *
51  *      On most supported platforms, TAS() uses a tas() function written
52  *      in assembly language to execute a hardware atomic-test-and-set
53  *      instruction.  Equivalent OS-supplied mutex routines could be used too.
54  *
55  *      If no system-specific TAS() is available (ie, HAS_TEST_AND_SET is not
56  *      defined), then we fall back on an emulation that uses SysV semaphores
57  *      (see spin.c).  This emulation will be MUCH MUCH slower than a proper TAS()
58  *      implementation, because of the cost of a kernel call per lock or unlock.
59  *      An old report is that Postgres spends around 40% of its time in semop(2)
60  *      when using the SysV semaphore code.
61  *
62  *
63  * Portions Copyright (c) 1996-2003, PostgreSQL Global Development Group
64  * Portions Copyright (c) 1994, Regents of the University of California
65  *
66  *        $Id: s_lock.h,v 1.114 2003/09/29 04:20:22 momjian Exp $
67  *
68  *-------------------------------------------------------------------------
69  */
70 #ifndef S_LOCK_H
71 #define S_LOCK_H
72
73 #include "storage/pg_sema.h"
74
75
76 #if defined(HAS_TEST_AND_SET)
77
78
79 #if defined(__GNUC__) || defined(__ICC)
80 /*************************************************************************
81  * All the gcc inlines
82  */
83
84 /*
85  * Standard gcc asm format:
86  *
87         __asm__ __volatile__(
88                 "       command \n"
89                 "       command \n"
90                 "       command \n"
91 :               "=r"(_res)                      return value, in register
92 :               "r"(lock)                       argument, 'lock pointer', in register
93 :               "r0");                          inline code uses this register
94  */
95
96
97 #if defined(__i386__) || defined(__x86_64__) /* AMD Opteron */
98 #define TAS(lock) tas(lock)
99
100 static __inline__ int
101 tas(volatile slock_t *lock)
102 {
103         register slock_t _res = 1;
104
105         __asm__ __volatile__(
106                 "       lock                    \n"
107                 "       xchgb   %0,%1   \n"
108 :               "=q"(_res), "=m"(*lock)
109 :               "0"(_res));
110         return (int) _res;
111 }
112
113 #endif   /* __i386__ || __x86_64__ */
114
115
116 /* Intel Itanium */
117 #if defined(__ia64__) || defined(__ia64)
118 #define TAS(lock) tas(lock)
119
120 static __inline__ int
121 tas(volatile slock_t *lock)
122 {
123         long int        ret;
124
125         __asm__ __volatile__(
126                 "       xchg4   %0=%1,%2        \n"
127 :               "=r"(ret), "=m"(*lock)
128 :               "r"(1), "1"(*lock)
129 :               "memory");
130
131         return (int) ret;
132 }
133
134 #endif   /* __ia64__ || __ia64 */
135
136
137 #if defined(__arm__) || defined(__arm__)
138 #define TAS(lock) tas(lock)
139
140 static __inline__ int
141 tas(volatile slock_t *lock)
142 {
143         register slock_t _res = 1;
144
145         __asm__ __volatile__(
146                 "       swpb    %0, %0, [%3]    \n"
147 :               "=r"(_res), "=m"(*lock)
148 :               "0"(_res), "r"(lock));
149         return (int) _res;
150 }
151
152 #endif   /* __arm__ */
153
154
155 #if defined(__s390__) && !defined(__s390x__)
156 /*
157  * S/390 Linux
158  */
159 #define TAS(lock)          tas(lock)
160
161 static __inline__ int
162 tas(volatile slock_t *lock)
163 {
164         int                     _res;
165
166         __asm__ __volatile__(
167                 "       la      1,1                     \n"
168                 "       l       2,%2            \n"
169                 "       slr 0,0                 \n"
170                 "       cs      0,1,0(2)        \n"
171                 "       lr      %1,0            \n"
172 :               "=m"(lock), "=d"(_res)
173 :               "m"(lock)
174 :               "0", "1", "2");
175
176         return (_res);
177 }
178
179 #endif   /* __s390__ */
180
181 #if defined(__s390x__)
182 /*
183  * S/390x Linux (64-bit zSeries)
184  */
185 #define TAS(lock)          tas(lock)
186
187 static __inline__ int
188 tas(volatile slock_t *lock)
189 {
190         int                     _res;
191
192         __asm__ __volatile__(
193                 "       la      1,1                     \n"
194                 "       lg      2,%2            \n"
195                 "       slr 0,0                 \n"
196                 "       cs      0,1,0(2)        \n"
197                 "       lr      %1,0            \n"
198 :               "=m"(lock), "=d"(_res)
199 :               "m"(lock)
200 :               "0", "1", "2");
201
202         return (_res);
203 }
204
205 #endif   /* __s390x__ */
206
207
208 #if defined(__sparc__)
209 #define TAS(lock) tas(lock)
210
211 static __inline__ int
212 tas(volatile slock_t *lock)
213 {
214         register slock_t _res = 1;
215
216         __asm__ __volatile__(
217                 "       ldstub  [%2], %0        \n"
218 :               "=r"(_res), "=m"(*lock)
219 :               "r"(lock));
220         return (int) _res;
221 }
222
223 #endif   /* __sparc__ */
224
225 #if defined(__ppc__) || defined(__powerpc__) || defined(__powerpc64__)
226 #define TAS(lock) tas(lock)
227 /*
228  * NOTE: per the Enhanced PowerPC Architecture manual, v1.0 dated 7-May-2002,
229  * an isync is a sufficient synchronization barrier after a lwarx/stwcx loop.
230  */
231 static __inline__ int
232 tas(volatile slock_t *lock)
233 {
234         slock_t _t;
235         int _res;
236
237         __asm__ __volatile__(
238 "       lwarx   %0,0,%2         \n"
239 "       cmpwi   %0,0            \n"
240 "       bne     1f                      \n"
241 "       addi    %0,%0,1         \n"
242 "       stwcx.  %0,0,%2         \n"
243 "       beq     2f              \n"
244 "1:     li      %1,1            \n"
245 "       b               3f                      \n"
246 "2:                                             \n"
247 "       isync                           \n"
248 "       li      %1,0            \n"
249 "3:                                             \n"
250
251 :       "=&r" (_t), "=r" (_res)
252 :       "r" (lock)
253 :       "cc", "memory"
254         );
255         return _res;
256 }
257
258 #endif /* powerpc */
259
260
261 #if defined(__mc68000__) && defined(__linux__)
262 #define TAS(lock) tas(lock)
263
264 static __inline__ int
265 tas(volatile slock_t *lock)
266 {
267         register int rv;
268
269         __asm__ __volatile__(
270                 "       clrl    %0              \n"
271                 "       tas             %1              \n"
272                 "       sne             %0              \n"
273 :               "=d"(rv), "=m"(*lock)
274 :               "1"(*lock)
275 :               "cc");
276
277         return rv;
278 }
279
280 #endif   /* defined(__mc68000__) && defined(__linux__) */
281
282
283 #if defined(__ppc__) || defined(__powerpc__) || defined(__powerpc64__)
284 /*
285  * PowerPC S_UNLOCK is almost standard but requires a "sync" instruction.
286  */
287 #define S_UNLOCK(lock)  \
288 do \
289 {\
290         __asm__ __volatile__ (" sync \n"); \
291         *((volatile slock_t *) (lock)) = 0; \
292 } while (0)
293
294 #endif /* powerpc */
295
296
297 #if defined(NEED_VAX_TAS_ASM)
298 /*
299  * VAXen -- even multiprocessor ones
300  * (thanks to Tom Ivar Helbekkmo)
301  */
302 #define TAS(lock) tas(lock)
303
304 static __inline__ int
305 tas(volatile slock_t *lock)
306 {
307         register int    _res;
308
309         __asm__ __volatile__(
310                 "       movl    $1, r0                  \n"
311                 "       bbssi   $0, (%1), 1f    \n"
312                 "       clrl    r0                              \n"
313                 "1:     movl    r0, %0                  \n"
314 :               "=r"(_res)
315 :               "r"(lock)
316 :               "r0");
317         return _res;
318 }
319
320 #endif   /* NEED_VAX_TAS_ASM */
321
322
323 #if defined(NEED_NS32K_TAS_ASM)
324 #define TAS(lock) tas(lock)
325
326 static __inline__ int
327 tas(volatile slock_t *lock)
328 {
329         register int    _res;
330
331         __asm__ __volatile__(
332                 "       sbitb   0, %0   \n"
333                 "       sfsd    %1              \n"
334 :               "=m"(*lock), "=r"(_res));
335         return _res;
336 }
337
338 #endif   /* NEED_NS32K_TAS_ASM */
339
340
341
342 #else                                                   /* !__GNUC__ */
343
344 /***************************************************************************
345  * All non-gcc inlines
346  */
347
348 #if defined(NEED_I386_TAS_ASM) && defined(USE_UNIVEL_CC)
349 #define TAS(lock)       tas(lock)
350
351 asm int
352 tas(volatile slock_t *s_lock)
353 {
354 /* UNIVEL wants %mem in column 1, so we don't pg_indent this file */
355 %mem s_lock
356         pushl %ebx
357         movl s_lock, %ebx
358         movl $255, %eax
359         lock
360         xchgb %al, (%ebx)
361         popl %ebx
362 }
363
364 #endif   /* defined(NEED_I386_TAS_ASM) && defined(USE_UNIVEL_CC) */
365
366 #endif   /* defined(__GNUC__) */
367
368
369
370 /*************************************************************************
371  * These are the platforms that have only one compiler, or do not use inline
372  * assembler (and hence have common code for gcc and non-gcc compilers,
373  * if both are available).
374  */
375
376
377 #if defined(__alpha)
378
379 /*
380  * Correct multi-processor locking methods are explained in section 5.5.3
381  * of the Alpha AXP Architecture Handbook, which at this writing can be
382  * found at ftp://ftp.netbsd.org/pub/NetBSD/misc/dec-docs/index.html.
383  * For gcc we implement the handbook's code directly with inline assembler.
384  */
385 #if defined(__GNUC__)
386
387 #define TAS(lock)  tas(lock)
388 #define S_UNLOCK(lock)  \
389 do \
390 {\
391         __asm__ __volatile__ (" mb \n"); \
392         *((volatile slock_t *) (lock)) = 0; \
393 } while (0)
394
395 static __inline__ int
396 tas(volatile slock_t *lock)
397 {
398         register slock_t _res;
399
400         __asm__ __volatile__(
401                 "       ldq             $0, %0  \n"
402                 "       bne             $0, 2f  \n"
403                 "       ldq_l   %1, %0  \n"
404                 "       bne             %1, 2f  \n"
405                 "       mov             1,  $0  \n"
406                 "       stq_c   $0, %0  \n"
407                 "       beq             $0, 2f  \n"
408                 "       mb                              \n"
409                 "       br              3f              \n"
410                 "2:     mov             1, %1   \n"
411                 "3:                                     \n"
412 :               "=m"(*lock), "=r"(_res)
413 :
414 :               "0");
415
416         return (int) _res;
417 }
418
419 #else                                                   /* !defined(__GNUC__) */
420
421 /*
422  * The Tru64 compiler doesn't support gcc-style inline asm, but it does
423  * have some builtin functions that accomplish much the same results.
424  * For simplicity, slock_t is defined as long (ie, quadword) on Alpha
425  * regardless of the compiler in use.  LOCK_LONG and UNLOCK_LONG only
426  * operate on an int (ie, longword), but that's OK as long as we define
427  * S_INIT_LOCK to zero out the whole quadword.
428  */
429
430 #include <alpha/builtins.h>
431
432 #define S_INIT_LOCK(lock)  (*(lock) = 0)
433 #define TAS(lock)                  (__LOCK_LONG_RETRY((lock), 1) == 0)
434 #define S_UNLOCK(lock)     __UNLOCK_LONG(lock)
435
436 #endif   /* defined(__GNUC__) */
437
438 #endif   /* __alpha */
439
440
441 #if defined(__hppa)
442 /*
443  * HP's PA-RISC
444  *
445  * Note that slock_t on PA-RISC is a structure instead of char
446  * (see include/port/hpux.h).
447  *
448  * a "set" slock_t has a single word cleared.  a "clear" slock_t has
449  * all words set to non-zero. tas() is in tas.s
450  */
451
452 #define S_UNLOCK(lock) \
453         do { \
454                 volatile slock_t *lock_ = (volatile slock_t *) (lock); \
455                 lock_->sema[0] = -1; \
456                 lock_->sema[1] = -1; \
457                 lock_->sema[2] = -1; \
458                 lock_->sema[3] = -1; \
459         } while (0)
460
461 #define S_LOCK_FREE(lock)       ( *(int *) (((long) (lock) + 15) & ~15) != 0)
462
463 #endif   /* __hppa */
464
465 #if defined(__QNX__) && defined(__WATCOMC__)
466 /*
467  * QNX 4 using WATCOM C
468  */
469 #define TAS(lock) wc_tas(lock)
470 extern slock_t wc_tas(volatile slock_t *lock);
471 #pragma aux wc_tas =\
472                 "       mov   al,1    " \
473                 " lock  xchg    al,[esi]" \
474                 parm [esi]        \
475                 value [al];
476
477 #endif   /* __QNX__ and __WATCOMC__*/
478
479
480 #if defined(__sgi)
481 /*
482  * SGI IRIX 5
483  * slock_t is defined as a unsigned long. We use the standard SGI
484  * mutex API.
485  *
486  * The following comment is left for historical reasons, but is probably
487  * not a good idea since the mutex ABI is supported.
488  *
489  * This stuff may be supplemented in the future with Masato Kataoka's MIPS-II
490  * assembly from his NECEWS SVR4 port, but we probably ought to retain this
491  * for the R3000 chips out there.
492  */
493 #include "mutex.h"
494 #define TAS(lock)       (test_and_set(lock,1))
495 #define S_UNLOCK(lock)  (test_then_and(lock,0))
496 #define S_INIT_LOCK(lock)       (test_then_and(lock,0))
497 #define S_LOCK_FREE(lock)       (test_then_add(lock,0) == 0)
498 #endif   /* __sgi */
499
500 #if defined(sinix)
501 /*
502  * SINIX / Reliant UNIX
503  * slock_t is defined as a struct abilock_t, which has a single unsigned long
504  * member. (Basically same as SGI)
505  *
506  */
507 #define TAS(lock)       (!acquire_lock(lock))
508 #define S_UNLOCK(lock)  release_lock(lock)
509 #define S_INIT_LOCK(lock)       init_lock(lock)
510 #define S_LOCK_FREE(lock)       (stat_lock(lock) == UNLOCKED)
511 #endif   /* sinix */
512
513
514 #if defined(_AIX)
515 /*
516  * AIX (POWER)
517  *
518  * Note that slock_t on POWER/POWER2/PowerPC is int instead of char
519  */
520 #define TAS(lock)                       _check_lock(lock, 0, 1)
521 #define S_UNLOCK(lock)          _clear_lock(lock, 0)
522 #endif   /* _AIX */
523
524
525 #if defined (nextstep)
526 /*
527  * NEXTSTEP (mach)
528  * slock_t is defined as a struct mutex.
529  */
530
531 #define S_LOCK(lock)    mutex_lock(lock)
532 #define S_UNLOCK(lock)  mutex_unlock(lock)
533 #define S_INIT_LOCK(lock)       mutex_init(lock)
534 /* For Mach, we have to delve inside the entrails of `struct mutex'.  Ick! */
535 #define S_LOCK_FREE(alock)      ((alock)->lock == 0)
536 #endif   /* nextstep */
537
538
539
540 #else    /* HAS_TEST_AND_SET */
541
542 #ifdef HAVE_SPINLOCKS
543 #error This platform does not support native spinlocks.  To continue the compile, rerun configure using --disable-spinlocks.  However, performance will be poor.  Please report this to pgsql-bugs@postgresql.org.
544 #endif
545
546 /*
547  * Fake spinlock implementation using semaphores --- slow and prone
548  * to fall foul of kernel limits on number of semaphores, so don't use this
549  * unless you must!  The subroutines appear in spin.c.
550  */
551 typedef PGSemaphoreData slock_t;
552
553 extern bool s_lock_free_sema(volatile slock_t *lock);
554 extern void s_unlock_sema(volatile slock_t *lock);
555 extern void s_init_lock_sema(volatile slock_t *lock);
556 extern int      tas_sema(volatile slock_t *lock);
557
558 #define S_LOCK_FREE(lock)       s_lock_free_sema(lock)
559 #define S_UNLOCK(lock)   s_unlock_sema(lock)
560 #define S_INIT_LOCK(lock)       s_init_lock_sema(lock)
561 #define TAS(lock)       tas_sema(lock)
562
563 #endif   /* HAS_TEST_AND_SET */
564
565
566
567 /*
568  * Default Definitions - override these above as needed.
569  */
570
571 #if !defined(S_LOCK)
572 #define S_LOCK(lock) \
573         do { \
574                 if (TAS(lock)) \
575                         s_lock((lock), __FILE__, __LINE__); \
576         } while (0)
577 #endif   /* S_LOCK */
578
579 #if !defined(S_LOCK_FREE)
580 #define S_LOCK_FREE(lock)       (*(lock) == 0)
581 #endif   /* S_LOCK_FREE */
582
583 #if !defined(S_UNLOCK)
584 #define S_UNLOCK(lock)          (*((volatile slock_t *) (lock)) = 0)
585 #endif   /* S_UNLOCK */
586
587 #if !defined(S_INIT_LOCK)
588 #define S_INIT_LOCK(lock)       S_UNLOCK(lock)
589 #endif   /* S_INIT_LOCK */
590
591 #if !defined(TAS)
592 extern int      tas(volatile slock_t *lock);            /* in port/.../tas.s, or
593                                                                                                  * s_lock.c */
594
595 #define TAS(lock)               tas(lock)
596 #endif   /* TAS */
597
598
599 /*
600  * Platform-independent out-of-line support routines
601  */
602 extern void s_lock(volatile slock_t *lock, const char *file, int line);
603
604 #endif   /* S_LOCK_H */