]> granicus.if.org Git - postgresql/blob - src/include/storage/s_lock.h
slock_t must be int not char for MIPS. 7.4 got this right, but the
[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  *      void SPIN_DELAY(void)
28  *              Delay operation to occur inside spinlock wait loop.
29  *
30  *      Note to implementors: there are default implementations for all these
31  *      macros at the bottom of the file.  Check if your platform can use
32  *      these or needs to override them.
33  *
34  *  Usually, S_LOCK() is implemented in terms of an even lower-level macro
35  *      TAS():
36  *
37  *      int TAS(slock_t *lock)
38  *              Atomic test-and-set instruction.  Attempt to acquire the lock,
39  *              but do *not* wait.      Returns 0 if successful, nonzero if unable
40  *              to acquire the lock.
41  *
42  *      TAS() is NOT part of the API, and should never be called directly.
43  *
44  *      CAUTION: on some platforms TAS() may sometimes report failure to acquire
45  *      a lock even when the lock is not locked.  For example, on Alpha TAS()
46  *      will "fail" if interrupted.  Therefore TAS() should always be invoked
47  *      in a retry loop, even if you are certain the lock is free.
48  *
49  *      ANOTHER CAUTION: be sure that TAS() and S_UNLOCK() represent sequence
50  *      points, ie, loads and stores of other values must not be moved across
51  *      a lock or unlock.  In most cases it suffices to make the operation be
52  *      done through a "volatile" pointer.
53  *
54  *      On most supported platforms, TAS() uses a tas() function written
55  *      in assembly language to execute a hardware atomic-test-and-set
56  *      instruction.  Equivalent OS-supplied mutex routines could be used too.
57  *
58  *      If no system-specific TAS() is available (ie, HAVE_SPINLOCKS is not
59  *      defined), then we fall back on an emulation that uses SysV semaphores
60  *      (see spin.c).  This emulation will be MUCH MUCH slower than a proper TAS()
61  *      implementation, because of the cost of a kernel call per lock or unlock.
62  *      An old report is that Postgres spends around 40% of its time in semop(2)
63  *      when using the SysV semaphore code.
64  *
65  *
66  * Portions Copyright (c) 1996-2004, PostgreSQL Global Development Group
67  * Portions Copyright (c) 1994, Regents of the University of California
68  *
69  *        $PostgreSQL: pgsql/src/include/storage/s_lock.h,v 1.128 2004/08/30 22:49:07 tgl Exp $
70  *
71  *-------------------------------------------------------------------------
72  */
73 #ifndef S_LOCK_H
74 #define S_LOCK_H
75
76 #include "storage/pg_sema.h"
77
78 #ifdef HAVE_SPINLOCKS   /* skip spinlocks if requested */
79
80
81 #if defined(__GNUC__) || defined(__ICC)
82 /*************************************************************************
83  * All the gcc inlines
84  * Gcc consistently defines the CPU as __cpu__.
85  * Other compilers use __cpu or __cpu__ so we test for both in those cases.
86  */
87
88 /*----------
89  * Standard gcc asm format (assuming "volatile slock_t *lock"):
90
91         __asm__ __volatile__(
92                 "       instruction     \n"
93                 "       instruction     \n"
94                 "       instruction     \n"
95 :               "=r"(_res), "+m"(*lock)         // return register, in/out lock value
96 :               "r"(lock)                                       // lock pointer, in input register
97 :               "memory", "cc");                        // show clobbered registers here
98
99  * The output-operands list (after first colon) should always include
100  * "+m"(*lock), whether or not the asm code actually refers to this
101  * operand directly.  This ensures that gcc believes the value in the
102  * lock variable is used and set by the asm code.  Also, the clobbers
103  * list (after third colon) should always include "memory"; this prevents
104  * gcc from thinking it can cache the values of shared-memory fields
105  * across the asm code.  Add "cc" if your asm code changes the condition
106  * code register, and also list any temp registers the code uses.
107  *----------
108  */
109
110
111 #if defined(__i386__) || defined(__x86_64__) /* AMD Opteron */
112 #define HAS_TEST_AND_SET
113
114 typedef unsigned char slock_t;
115
116 #define TAS(lock) tas(lock)
117
118 static __inline__ int
119 tas(volatile slock_t *lock)
120 {
121         register slock_t _res = 1;
122
123         /* Use a non-locking test before asserting the bus lock */
124         __asm__ __volatile__(
125                 "       cmpb    $0,%1   \n"
126                 "       jne             1f              \n"
127                 "       lock                    \n"
128                 "       xchgb   %0,%1   \n"
129                 "1: \n"
130 :               "+q"(_res), "+m"(*lock)
131 :
132 :               "memory", "cc");
133         return (int) _res;
134 }
135
136 #define SPIN_DELAY() spin_delay()
137
138 static __inline__ void
139 spin_delay(void)
140 {
141         __asm__ __volatile__(
142                 " rep; nop                      \n");
143 }
144
145 #endif   /* __i386__ || __x86_64__ */
146
147
148 #if defined(__ia64__) || defined(__ia64)  /* __ia64 used by ICC compiler? */
149 /* Intel Itanium */
150 #define HAS_TEST_AND_SET
151
152 typedef unsigned int slock_t;
153
154 #define TAS(lock) tas(lock)
155
156 static __inline__ int
157 tas(volatile slock_t *lock)
158 {
159         long int        ret;
160
161         __asm__ __volatile__(
162                 "       xchg4   %0=%1,%2        \n"
163 :               "=r"(ret), "+m"(*lock)
164 :               "r"(1)
165 :               "memory");
166         return (int) ret;
167 }
168
169 #endif   /* __ia64__ || __ia64 */
170
171
172 #if defined(__arm__) || defined(__arm)
173 #define HAS_TEST_AND_SET
174
175 typedef unsigned char slock_t;
176
177 #define TAS(lock) tas(lock)
178
179 static __inline__ int
180 tas(volatile slock_t *lock)
181 {
182         register slock_t _res = 1;
183
184         __asm__ __volatile__(
185                 "       swpb    %0, %0, [%2]    \n"
186 :               "+r"(_res), "+m"(*lock)
187 :               "r"(lock)
188 :               "memory");
189         return (int) _res;
190 }
191
192 #endif   /* __arm__ */
193
194
195 #if defined(__s390__) || defined(__s390x__)
196 /* S/390 and S/390x Linux (32- and 64-bit zSeries) */
197 #define HAS_TEST_AND_SET
198
199 typedef unsigned int slock_t;
200
201 #define TAS(lock)          tas(lock)
202
203 static __inline__ int
204 tas(volatile slock_t *lock)
205 {
206         int                     _res = 0;
207
208         __asm__ __volatile__(
209                 "       cs      %0,%3,0(%2)             \n"
210 :               "+d"(_res), "+m"(*lock)
211 :               "a"(lock), "d"(1)
212 :               "memory", "cc");
213         return _res;
214 }
215
216 #endif   /* __s390__ || __s390x__ */
217
218
219 #if defined(__sparc__)
220 #define HAS_TEST_AND_SET
221
222 typedef unsigned char slock_t;
223
224 #define TAS(lock) tas(lock)
225
226 static __inline__ int
227 tas(volatile slock_t *lock)
228 {
229         register slock_t _res;
230
231         __asm__ __volatile__(
232                 "       ldstub  [%2], %0        \n"
233 :               "=r"(_res), "+m"(*lock)
234 :               "r"(lock)
235 :               "memory");
236         return (int) _res;
237 }
238
239 #endif   /* __sparc__ */
240
241
242 #if defined(__ppc__) || defined(__powerpc__) || defined(__powerpc64__)
243 #define HAS_TEST_AND_SET
244
245 #if defined(__powerpc64__)
246 typedef unsigned long slock_t;
247 #else
248 typedef unsigned int slock_t;
249 #endif
250
251 #define TAS(lock) tas(lock)
252 /*
253  * NOTE: per the Enhanced PowerPC Architecture manual, v1.0 dated 7-May-2002,
254  * an isync is a sufficient synchronization barrier after a lwarx/stwcx loop.
255  */
256 static __inline__ int
257 tas(volatile slock_t *lock)
258 {
259         slock_t _t;
260         int _res;
261
262         __asm__ __volatile__(
263 "       lwarx   %0,0,%3         \n"
264 "       cmpwi   %0,0            \n"
265 "       bne     1f                      \n"
266 "       addi    %0,%0,1         \n"
267 "       stwcx.  %0,0,%3         \n"
268 "       beq     2f              \n"
269 "1:     li      %1,1            \n"
270 "       b               3f                      \n"
271 "2:                                             \n"
272 "       isync                           \n"
273 "       li      %1,0            \n"
274 "3:                                             \n"
275
276 :       "=&r"(_t), "=r"(_res), "+m"(*lock)
277 :       "r"(lock)
278 :       "memory", "cc");
279         return _res;
280 }
281
282 /* PowerPC S_UNLOCK is almost standard but requires a "sync" instruction */
283 #define S_UNLOCK(lock)  \
284 do \
285 {\
286         __asm__ __volatile__ (" sync \n"); \
287         *((volatile slock_t *) (lock)) = 0; \
288 } while (0)
289
290 #endif /* powerpc */
291
292
293 #if defined(__mc68000__) && defined(__linux__)
294 #define HAS_TEST_AND_SET
295
296 typedef unsigned char slock_t;
297
298 #define TAS(lock) tas(lock)
299
300 static __inline__ int
301 tas(volatile slock_t *lock)
302 {
303         register int rv;
304
305         __asm__ __volatile__(
306                 "       clrl    %0              \n"
307                 "       tas             %1              \n"
308                 "       sne             %0              \n"
309 :               "=d"(rv), "+m"(*lock)
310 :
311 :               "memory", "cc");
312         return rv;
313 }
314
315 #endif   /* defined(__mc68000__) && defined(__linux__) */
316
317
318 #if defined(__vax__)
319 /*
320  * VAXen -- even multiprocessor ones
321  * (thanks to Tom Ivar Helbekkmo)
322  */
323 #define HAS_TEST_AND_SET
324
325 typedef unsigned char slock_t;
326
327 #define TAS(lock) tas(lock)
328
329 static __inline__ int
330 tas(volatile slock_t *lock)
331 {
332         register int    _res;
333
334         __asm__ __volatile__(
335                 "       movl    $1, %0                  \n"
336                 "       bbssi   $0, (%2), 1f    \n"
337                 "       clrl    %0                              \n"
338                 "1: \n"
339 :               "=&r"(_res), "+m"(*lock)
340 :               "r"(lock)
341 :               "memory");
342         return _res;
343 }
344
345 #endif   /* __vax__ */
346
347
348 #if defined(__ns32k__)
349 #define HAS_TEST_AND_SET
350
351 typedef unsigned char slock_t;
352
353 #define TAS(lock) tas(lock)
354
355 static __inline__ int
356 tas(volatile slock_t *lock)
357 {
358         register int    _res;
359
360         __asm__ __volatile__(
361                 "       sbitb   0, %1   \n"
362                 "       sfsd    %0              \n"
363 :               "=r"(_res), "+m"(*lock)
364 :
365 :               "memory");
366         return _res;
367 }
368
369 #endif   /* __ns32k__ */
370
371
372 #if defined(__alpha) || defined(__alpha__)
373 /*
374  * Correct multi-processor locking methods are explained in section 5.5.3
375  * of the Alpha AXP Architecture Handbook, which at this writing can be
376  * found at ftp://ftp.netbsd.org/pub/NetBSD/misc/dec-docs/index.html.
377  * For gcc we implement the handbook's code directly with inline assembler.
378  */
379 #define HAS_TEST_AND_SET
380
381 typedef unsigned long slock_t;
382
383 #define TAS(lock)  tas(lock)
384
385 static __inline__ int
386 tas(volatile slock_t *lock)
387 {
388         register slock_t _res;
389
390         __asm__ __volatile__(
391                 "       ldq             $0, %1  \n"
392                 "       bne             $0, 2f  \n"
393                 "       ldq_l   %0, %1  \n"
394                 "       bne             %0, 2f  \n"
395                 "       mov             1,  $0  \n"
396                 "       stq_c   $0, %1  \n"
397                 "       beq             $0, 2f  \n"
398                 "       mb                              \n"
399                 "       br              3f              \n"
400                 "2:     mov             1, %0   \n"
401                 "3:                                     \n"
402 :               "=&r"(_res), "+m"(*lock)
403 :
404 :               "memory", "0");
405         return (int) _res;
406 }
407
408 #define S_UNLOCK(lock)  \
409 do \
410 {\
411         __asm__ __volatile__ (" mb \n"); \
412         *((volatile slock_t *) (lock)) = 0; \
413 } while (0)
414
415 #endif /* __alpha || __alpha__ */
416
417
418 /* These live in s_lock.c, but only for gcc */
419
420
421 #if defined(__m68k__)
422 #define HAS_TEST_AND_SET
423
424 typedef unsigned char slock_t;
425 #endif
426
427
428 #if defined(__mips__) && !defined(__sgi)
429 #define HAS_TEST_AND_SET
430
431 typedef unsigned int slock_t;
432 #endif
433
434
435 #endif  /* __GNUC__ */
436
437
438
439 /***************************************************************************
440  * Platforms that use non-gcc inline assembly:
441  */
442
443 #if !defined(HAS_TEST_AND_SET)  /* We didn't trigger above, let's try here */
444
445
446 #if defined(USE_UNIVEL_CC)
447 #define HAS_TEST_AND_SET
448
449 typedef unsigned char slock_t;
450
451 #define TAS(lock)       tas(lock)
452
453 asm int
454 tas(volatile slock_t *s_lock)
455 {
456 /* UNIVEL wants %mem in column 1, so we don't pg_indent this file */
457 %mem s_lock
458         pushl %ebx
459         movl s_lock, %ebx
460         movl $255, %eax
461         lock
462         xchgb %al, (%ebx)
463         popl %ebx
464 }
465
466 #endif   /* defined(USE_UNIVEL_CC) */
467
468
469 #if defined(__alpha) || defined(__alpha__)
470 /*
471  * The Tru64 compiler doesn't support gcc-style inline asm, but it does
472  * have some builtin functions that accomplish much the same results.
473  * For simplicity, slock_t is defined as long (ie, quadword) on Alpha
474  * regardless of the compiler in use.  LOCK_LONG and UNLOCK_LONG only
475  * operate on an int (ie, longword), but that's OK as long as we define
476  * S_INIT_LOCK to zero out the whole quadword.
477  */
478 #define HAS_TEST_AND_SET
479
480 typedef unsigned long slock_t;
481
482 #include <alpha/builtins.h>
483 #define S_INIT_LOCK(lock)  (*(lock) = 0)
484 #define TAS(lock)                  (__LOCK_LONG_RETRY((lock), 1) == 0)
485 #define S_UNLOCK(lock)     __UNLOCK_LONG(lock)
486
487 #endif   /* __alpha || __alpha__ */
488
489
490 #if defined(__hppa) || defined(__hppa__)
491 /*
492  * HP's PA-RISC
493  *
494  * See src/backend/port/hpux/tas.c.template for details about LDCWX.  Because
495  * LDCWX requires a 16-byte-aligned address, we declare slock_t as a 16-byte
496  * struct.  The active word in the struct is whichever has the aligned address;
497  * the other three words just sit at -1.
498  *
499  * When using gcc, we can inline the required assembly code.
500  */
501 #define HAS_TEST_AND_SET
502
503 typedef struct
504 {
505         int                     sema[4];
506 } slock_t;
507
508 #define TAS_ACTIVE_WORD(lock)   ((volatile int *) (((long) (lock) + 15) & ~15))
509
510 #if defined(__GNUC__)
511
512 static __inline__ int
513 tas(volatile slock_t *lock)
514 {
515         volatile int *lockword = TAS_ACTIVE_WORD(lock);
516         register int lockval;
517
518         __asm__ __volatile__(
519                 "       ldcwx   0(0,%2),%0      \n"
520 :               "=r"(lockval), "+m"(*lockword)
521 :               "r"(lockword)
522 :               "memory");
523         return (lockval == 0);
524 }
525
526 #endif /* __GNUC__ */
527
528 #define S_UNLOCK(lock)  (*TAS_ACTIVE_WORD(lock) = -1)
529
530 #define S_INIT_LOCK(lock) \
531         do { \
532                 volatile slock_t *lock_ = (lock); \
533                 lock_->sema[0] = -1; \
534                 lock_->sema[1] = -1; \
535                 lock_->sema[2] = -1; \
536                 lock_->sema[3] = -1; \
537         } while (0)
538
539 #define S_LOCK_FREE(lock)       (*TAS_ACTIVE_WORD(lock) != 0)
540
541 #endif   /* __hppa || __hppa__ */
542
543
544 #if defined(__QNX__) && defined(__WATCOMC__)
545 /*
546  * QNX 4 using WATCOM C
547  */
548 #define HAS_TEST_AND_SET
549
550 typedef unsigned char slock_t;
551
552 #define TAS(lock) wc_tas(lock)
553 extern slock_t wc_tas(volatile slock_t *lock);
554 #pragma aux wc_tas =\
555                 "       mov   al,1    " \
556                 " lock  xchg    al,[esi]" \
557                 parm [esi]        \
558                 value [al];
559
560 #endif   /* __QNX__ and __WATCOMC__*/
561
562
563 #if defined(__sgi)
564 /*
565  * SGI IRIX 5
566  * slock_t is defined as a unsigned long. We use the standard SGI
567  * mutex API.
568  *
569  * The following comment is left for historical reasons, but is probably
570  * not a good idea since the mutex ABI is supported.
571  *
572  * This stuff may be supplemented in the future with Masato Kataoka's MIPS-II
573  * assembly from his NECEWS SVR4 port, but we probably ought to retain this
574  * for the R3000 chips out there.
575  */
576 #define HAS_TEST_AND_SET
577
578 typedef unsigned long slock_t;
579
580 #include "mutex.h"
581 #define TAS(lock)       (test_and_set(lock,1))
582 #define S_UNLOCK(lock)  (test_then_and(lock,0))
583 #define S_INIT_LOCK(lock)       (test_then_and(lock,0))
584 #define S_LOCK_FREE(lock)       (test_then_add(lock,0) == 0)
585 #endif   /* __sgi */
586
587
588 #if defined(sinix)
589 /*
590  * SINIX / Reliant UNIX
591  * slock_t is defined as a struct abilock_t, which has a single unsigned long
592  * member. (Basically same as SGI)
593  */
594 #define HAS_TEST_AND_SET
595
596 #include "abi_mutex.h"
597 typedef abilock_t slock_t;
598
599 #define TAS(lock)       (!acquire_lock(lock))
600 #define S_UNLOCK(lock)  release_lock(lock)
601 #define S_INIT_LOCK(lock)       init_lock(lock)
602 #define S_LOCK_FREE(lock)       (stat_lock(lock) == UNLOCKED)
603 #endif   /* sinix */
604
605
606 #if defined(_AIX)
607 /*
608  * AIX (POWER)
609  */
610 #define HAS_TEST_AND_SET
611
612 typedef unsigned int slock_t;
613
614 #define TAS(lock)                       _check_lock(lock, 0, 1)
615 #define S_UNLOCK(lock)          _clear_lock(lock, 0)
616 #endif   /* _AIX */
617
618
619 #if defined (nextstep)
620 #define HAS_TEST_AND_SET
621
622 typedef struct mutex slock_t;
623
624 #define S_LOCK(lock)    mutex_lock(lock)
625 #define S_UNLOCK(lock)  mutex_unlock(lock)
626 #define S_INIT_LOCK(lock)       mutex_init(lock)
627 /* For Mach, we have to delve inside the entrails of `struct mutex'.  Ick! */
628 #define S_LOCK_FREE(alock)      ((alock)->lock == 0)
629 #endif   /* nextstep */
630
631
632 /* These are in s_lock.c */
633
634
635 #if defined(sun3)
636 #define HAS_TEST_AND_SET
637
638 typedef unsigned char slock_t;
639 #endif
640
641
642 #if defined(__sparc__) || defined(__sparc)
643 #define HAS_TEST_AND_SET
644
645 typedef unsigned char slock_t;
646 #endif
647
648
649 #endif  /* !defined(HAS_TEST_AND_SET) */
650
651
652 /* Blow up if we didn't have any way to do spinlocks */
653 #ifndef HAS_TEST_AND_SET
654 #error PostgreSQL does not have native spinlock support on this platform.  To continue the compilation, rerun configure using --disable-spinlocks.  However, performance will be poor.  Please report this to pgsql-bugs@postgresql.org.
655 #endif
656
657
658 #else   /* !HAVE_SPINLOCKS */
659
660
661 /*
662  * Fake spinlock implementation using semaphores --- slow and prone
663  * to fall foul of kernel limits on number of semaphores, so don't use this
664  * unless you must!  The subroutines appear in spin.c.
665  */
666 typedef PGSemaphoreData slock_t;
667
668 extern bool s_lock_free_sema(volatile slock_t *lock);
669 extern void s_unlock_sema(volatile slock_t *lock);
670 extern void s_init_lock_sema(volatile slock_t *lock);
671 extern int      tas_sema(volatile slock_t *lock);
672
673 #define S_LOCK_FREE(lock)       s_lock_free_sema(lock)
674 #define S_UNLOCK(lock)   s_unlock_sema(lock)
675 #define S_INIT_LOCK(lock)       s_init_lock_sema(lock)
676 #define TAS(lock)       tas_sema(lock)
677
678
679 #endif  /* HAVE_SPINLOCKS */
680
681
682 /*
683  * Default Definitions - override these above as needed.
684  */
685
686 #if !defined(S_LOCK)
687 #define S_LOCK(lock) \
688         do { \
689                 if (TAS(lock)) \
690                         s_lock((lock), __FILE__, __LINE__); \
691         } while (0)
692 #endif   /* S_LOCK */
693
694 #if !defined(S_LOCK_FREE)
695 #define S_LOCK_FREE(lock)       (*(lock) == 0)
696 #endif   /* S_LOCK_FREE */
697
698 #if !defined(S_UNLOCK)
699 #define S_UNLOCK(lock)          (*((volatile slock_t *) (lock)) = 0)
700 #endif   /* S_UNLOCK */
701
702 #if !defined(S_INIT_LOCK)
703 #define S_INIT_LOCK(lock)       S_UNLOCK(lock)
704 #endif   /* S_INIT_LOCK */
705
706 #if !defined(SPIN_DELAY)
707 #define SPIN_DELAY()    ((void) 0)
708 #endif   /* SPIN_DELAY */
709
710 #if !defined(TAS)
711 extern int      tas(volatile slock_t *lock);            /* in port/.../tas.s, or
712                                                                                                  * s_lock.c */
713
714 #define TAS(lock)               tas(lock)
715 #endif   /* TAS */
716
717
718 /*
719  * Platform-independent out-of-line support routines
720  */
721 extern void s_lock(volatile slock_t *lock, const char *file, int line);
722
723 #endif   /* S_LOCK_H */