Fix obsolete comment regarding FSM truncation.
[PostgreSQL.git] / src / include / storage / s_lock.h
bloba253d1301fa6e8859e58785afcb297a77523edc3
1 /*-------------------------------------------------------------------------
3 * s_lock.h
4 * Hardware-dependent implementation of spinlocks.
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.
9 * The following hardware-dependent macros must be provided for each
10 * supported platform:
12 * void S_INIT_LOCK(slock_t *lock)
13 * Initialize a spinlock (to the unlocked state).
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.
20 * void S_UNLOCK(slock_t *lock)
21 * Unlock a previously acquired lock.
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.
27 * void SPIN_DELAY(void)
28 * Delay operation to occur inside spinlock wait loop.
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.
34 * Usually, S_LOCK() is implemented in terms of an even lower-level macro
35 * TAS():
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.
42 * TAS() is NOT part of the API, and should never be called directly.
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.
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.
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.
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.
66 * Portions Copyright (c) 1996-2008, PostgreSQL Global Development Group
67 * Portions Copyright (c) 1994, Regents of the University of California
69 * $PostgreSQL$
71 *-------------------------------------------------------------------------
73 #ifndef S_LOCK_H
74 #define S_LOCK_H
76 #include "storage/pg_sema.h"
78 #ifdef HAVE_SPINLOCKS /* skip spinlocks if requested */
81 #if defined(__GNUC__) || defined(__INTEL_COMPILER)
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.
88 /*----------
89 * Standard gcc asm format (assuming "volatile slock_t *lock"):
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
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 *----------
111 #ifdef __i386__ /* 32-bit i386 */
112 #define HAS_TEST_AND_SET
114 typedef unsigned char slock_t;
116 #define TAS(lock) tas(lock)
118 static __inline__ int
119 tas(volatile slock_t *lock)
121 register slock_t _res = 1;
124 * Use a non-locking test before asserting the bus lock. Note that the
125 * extra test appears to be a small loss on some x86 platforms and a small
126 * win on others; it's by no means clear that we should keep it.
128 __asm__ __volatile__(
129 " cmpb $0,%1 \n"
130 " jne 1f \n"
131 " lock \n"
132 " xchgb %0,%1 \n"
133 "1: \n"
134 : "+q"(_res), "+m"(*lock)
136 : "memory", "cc");
137 return (int) _res;
140 #define SPIN_DELAY() spin_delay()
142 static __inline__ void
143 spin_delay(void)
146 * This sequence is equivalent to the PAUSE instruction ("rep" is
147 * ignored by old IA32 processors if the following instruction is
148 * not a string operation); the IA-32 Architecture Software
149 * Developer's Manual, Vol. 3, Section 7.7.2 describes why using
150 * PAUSE in the inner loop of a spin lock is necessary for good
151 * performance:
153 * The PAUSE instruction improves the performance of IA-32
154 * processors supporting Hyper-Threading Technology when
155 * executing spin-wait loops and other routines where one
156 * thread is accessing a shared lock or semaphore in a tight
157 * polling loop. When executing a spin-wait loop, the
158 * processor can suffer a severe performance penalty when
159 * exiting the loop because it detects a possible memory order
160 * violation and flushes the core processor's pipeline. The
161 * PAUSE instruction provides a hint to the processor that the
162 * code sequence is a spin-wait loop. The processor uses this
163 * hint to avoid the memory order violation and prevent the
164 * pipeline flush. In addition, the PAUSE instruction
165 * de-pipelines the spin-wait loop to prevent it from
166 * consuming execution resources excessively.
168 __asm__ __volatile__(
169 " rep; nop \n");
172 #endif /* __i386__ */
175 #ifdef __x86_64__ /* AMD Opteron, Intel EM64T */
176 #define HAS_TEST_AND_SET
178 typedef unsigned char slock_t;
180 #define TAS(lock) tas(lock)
182 static __inline__ int
183 tas(volatile slock_t *lock)
185 register slock_t _res = 1;
188 * On Opteron, using a non-locking test before the locking instruction
189 * is a huge loss. On EM64T, it appears to be a wash or small loss,
190 * so we needn't bother to try to distinguish the sub-architectures.
192 __asm__ __volatile__(
193 " lock \n"
194 " xchgb %0,%1 \n"
195 : "+q"(_res), "+m"(*lock)
197 : "memory", "cc");
198 return (int) _res;
201 #define SPIN_DELAY() spin_delay()
203 static __inline__ void
204 spin_delay(void)
207 * Adding a PAUSE in the spin delay loop is demonstrably a no-op on
208 * Opteron, but it may be of some use on EM64T, so we keep it.
210 __asm__ __volatile__(
211 " rep; nop \n");
214 #endif /* __x86_64__ */
217 #if defined(__ia64__) || defined(__ia64) /* Intel Itanium */
218 #define HAS_TEST_AND_SET
220 typedef unsigned int slock_t;
222 #define TAS(lock) tas(lock)
224 #ifndef __INTEL_COMPILER
226 static __inline__ int
227 tas(volatile slock_t *lock)
229 long int ret;
231 __asm__ __volatile__(
232 " xchg4 %0=%1,%2 \n"
233 : "=r"(ret), "+m"(*lock)
234 : "r"(1)
235 : "memory");
236 return (int) ret;
239 #else /* __INTEL_COMPILER */
241 static __inline__ int
242 tas(volatile slock_t *lock)
244 int ret;
246 ret = _InterlockedExchange(lock,1); /* this is a xchg asm macro */
248 return ret;
251 #endif /* __INTEL_COMPILER */
252 #endif /* __ia64__ || __ia64 */
255 #if defined(__arm__) || defined(__arm)
256 #define HAS_TEST_AND_SET
258 typedef unsigned char slock_t;
260 #define TAS(lock) tas(lock)
262 static __inline__ int
263 tas(volatile slock_t *lock)
265 register slock_t _res = 1;
267 __asm__ __volatile__(
268 " swpb %0, %0, [%2] \n"
269 : "+r"(_res), "+m"(*lock)
270 : "r"(lock)
271 : "memory");
272 return (int) _res;
275 #endif /* __arm__ */
278 /* S/390 and S/390x Linux (32- and 64-bit zSeries) */
279 #if defined(__s390__) || defined(__s390x__)
280 #define HAS_TEST_AND_SET
282 typedef unsigned int slock_t;
284 #define TAS(lock) tas(lock)
286 static __inline__ int
287 tas(volatile slock_t *lock)
289 int _res = 0;
291 __asm__ __volatile__(
292 " cs %0,%3,0(%2) \n"
293 : "+d"(_res), "+m"(*lock)
294 : "a"(lock), "d"(1)
295 : "memory", "cc");
296 return _res;
299 #endif /* __s390__ || __s390x__ */
302 #if defined(__sparc__) /* Sparc */
303 #define HAS_TEST_AND_SET
305 typedef unsigned char slock_t;
307 #define TAS(lock) tas(lock)
309 static __inline__ int
310 tas(volatile slock_t *lock)
312 register slock_t _res;
315 * See comment in /pg/backend/port/tas/solaris_sparc.s for why this
316 * uses "ldstub", and that file uses "cas". gcc currently generates
317 * sparcv7-targeted binaries, so "cas" use isn't possible.
319 __asm__ __volatile__(
320 " ldstub [%2], %0 \n"
321 : "=r"(_res), "+m"(*lock)
322 : "r"(lock)
323 : "memory");
324 return (int) _res;
327 #endif /* __sparc__ */
330 /* PowerPC */
331 #if defined(__ppc__) || defined(__powerpc__) || defined(__ppc64__) || defined(__powerpc64__)
332 #define HAS_TEST_AND_SET
334 #if defined(__ppc64__) || defined(__powerpc64__)
335 typedef unsigned long slock_t;
336 #else
337 typedef unsigned int slock_t;
338 #endif
340 #define TAS(lock) tas(lock)
342 * NOTE: per the Enhanced PowerPC Architecture manual, v1.0 dated 7-May-2002,
343 * an isync is a sufficient synchronization barrier after a lwarx/stwcx loop.
345 static __inline__ int
346 tas(volatile slock_t *lock)
348 slock_t _t;
349 int _res;
351 __asm__ __volatile__(
352 " lwarx %0,0,%3 \n"
353 " cmpwi %0,0 \n"
354 " bne 1f \n"
355 " addi %0,%0,1 \n"
356 " stwcx. %0,0,%3 \n"
357 " beq 2f \n"
358 "1: li %1,1 \n"
359 " b 3f \n"
360 "2: \n"
361 " isync \n"
362 " li %1,0 \n"
363 "3: \n"
365 : "=&r"(_t), "=r"(_res), "+m"(*lock)
366 : "r"(lock)
367 : "memory", "cc");
368 return _res;
371 /* PowerPC S_UNLOCK is almost standard but requires a "sync" instruction */
372 #define S_UNLOCK(lock) \
373 do \
375 __asm__ __volatile__ (" sync \n"); \
376 *((volatile slock_t *) (lock)) = 0; \
377 } while (0)
379 #endif /* powerpc */
382 /* Linux Motorola 68k */
383 #if (defined(__mc68000__) || defined(__m68k__)) && defined(__linux__)
384 #define HAS_TEST_AND_SET
386 typedef unsigned char slock_t;
388 #define TAS(lock) tas(lock)
390 static __inline__ int
391 tas(volatile slock_t *lock)
393 register int rv;
395 __asm__ __volatile__(
396 " clrl %0 \n"
397 " tas %1 \n"
398 " sne %0 \n"
399 : "=d"(rv), "+m"(*lock)
401 : "memory", "cc");
402 return rv;
405 #endif /* (__mc68000__ || __m68k__) && __linux__ */
409 * VAXen -- even multiprocessor ones
410 * (thanks to Tom Ivar Helbekkmo)
412 #if defined(__vax__)
413 #define HAS_TEST_AND_SET
415 typedef unsigned char slock_t;
417 #define TAS(lock) tas(lock)
419 static __inline__ int
420 tas(volatile slock_t *lock)
422 register int _res;
424 __asm__ __volatile__(
425 " movl $1, %0 \n"
426 " bbssi $0, (%2), 1f \n"
427 " clrl %0 \n"
428 "1: \n"
429 : "=&r"(_res), "+m"(*lock)
430 : "r"(lock)
431 : "memory");
432 return _res;
435 #endif /* __vax__ */
438 #if defined(__ns32k__) /* National Semiconductor 32K */
439 #define HAS_TEST_AND_SET
441 typedef unsigned char slock_t;
443 #define TAS(lock) tas(lock)
445 static __inline__ int
446 tas(volatile slock_t *lock)
448 register int _res;
450 __asm__ __volatile__(
451 " sbitb 0, %1 \n"
452 " sfsd %0 \n"
453 : "=r"(_res), "+m"(*lock)
455 : "memory");
456 return _res;
459 #endif /* __ns32k__ */
462 #if defined(__alpha) || defined(__alpha__) /* Alpha */
464 * Correct multi-processor locking methods are explained in section 5.5.3
465 * of the Alpha AXP Architecture Handbook, which at this writing can be
466 * found at ftp://ftp.netbsd.org/pub/NetBSD/misc/dec-docs/index.html.
467 * For gcc we implement the handbook's code directly with inline assembler.
469 #define HAS_TEST_AND_SET
471 typedef unsigned long slock_t;
473 #define TAS(lock) tas(lock)
475 static __inline__ int
476 tas(volatile slock_t *lock)
478 register slock_t _res;
480 __asm__ __volatile__(
481 " ldq $0, %1 \n"
482 " bne $0, 2f \n"
483 " ldq_l %0, %1 \n"
484 " bne %0, 2f \n"
485 " mov 1, $0 \n"
486 " stq_c $0, %1 \n"
487 " beq $0, 2f \n"
488 " mb \n"
489 " br 3f \n"
490 "2: mov 1, %0 \n"
491 "3: \n"
492 : "=&r"(_res), "+m"(*lock)
494 : "memory", "0");
495 return (int) _res;
498 #define S_UNLOCK(lock) \
499 do \
501 __asm__ __volatile__ (" mb \n"); \
502 *((volatile slock_t *) (lock)) = 0; \
503 } while (0)
505 #endif /* __alpha || __alpha__ */
508 #if defined(__mips__) && !defined(__sgi) /* non-SGI MIPS */
509 /* Note: on SGI we use the OS' mutex ABI, see below */
510 /* Note: R10000 processors require a separate SYNC */
511 #define HAS_TEST_AND_SET
513 typedef unsigned int slock_t;
515 #define TAS(lock) tas(lock)
517 static __inline__ int
518 tas(volatile slock_t *lock)
520 register volatile slock_t *_l = lock;
521 register int _res;
522 register int _tmp;
524 __asm__ __volatile__(
525 " .set push \n"
526 " .set mips2 \n"
527 " .set noreorder \n"
528 " .set nomacro \n"
529 " ll %0, %2 \n"
530 " or %1, %0, 1 \n"
531 " sc %1, %2 \n"
532 " xori %1, 1 \n"
533 " or %0, %0, %1 \n"
534 " sync \n"
535 " .set pop "
536 : "=&r" (_res), "=&r" (_tmp), "+R" (*_l)
538 : "memory");
539 return _res;
542 /* MIPS S_UNLOCK is almost standard but requires a "sync" instruction */
543 #define S_UNLOCK(lock) \
544 do \
546 __asm__ __volatile__( \
547 " .set push \n" \
548 " .set mips2 \n" \
549 " .set noreorder \n" \
550 " .set nomacro \n" \
551 " sync \n" \
552 " .set pop "); \
553 *((volatile slock_t *) (lock)) = 0; \
554 } while (0)
556 #endif /* __mips__ && !__sgi */
559 #if defined(__m32r__) && defined(HAVE_SYS_TAS_H) /* Renesas' M32R */
560 #define HAS_TEST_AND_SET
562 #include <sys/tas.h>
564 typedef int slock_t;
566 #define TAS(lock) tas(lock)
568 #endif /* __m32r__ */
571 /* These live in s_lock.c, but only for gcc */
574 #if defined(__m68k__) && !defined(__linux__) /* non-Linux Motorola 68k */
575 #define HAS_TEST_AND_SET
577 typedef unsigned char slock_t;
578 #endif
581 #endif /* defined(__GNUC__) || defined(__INTEL_COMPILER) */
586 * ---------------------------------------------------------------------
587 * Platforms that use non-gcc inline assembly:
588 * ---------------------------------------------------------------------
591 #if !defined(HAS_TEST_AND_SET) /* We didn't trigger above, let's try here */
594 #if defined(USE_UNIVEL_CC) /* Unixware compiler */
595 #define HAS_TEST_AND_SET
597 typedef unsigned char slock_t;
599 #define TAS(lock) tas(lock)
601 asm int
602 tas(volatile slock_t *s_lock)
604 /* UNIVEL wants %mem in column 1, so we don't pg_indent this file */
605 %mem s_lock
606 pushl %ebx
607 movl s_lock, %ebx
608 movl $255, %eax
609 lock
610 xchgb %al, (%ebx)
611 popl %ebx
614 #endif /* defined(USE_UNIVEL_CC) */
617 #if defined(__alpha) || defined(__alpha__) /* Tru64 Unix Alpha compiler */
619 * The Tru64 compiler doesn't support gcc-style inline asm, but it does
620 * have some builtin functions that accomplish much the same results.
621 * For simplicity, slock_t is defined as long (ie, quadword) on Alpha
622 * regardless of the compiler in use. LOCK_LONG and UNLOCK_LONG only
623 * operate on an int (ie, longword), but that's OK as long as we define
624 * S_INIT_LOCK to zero out the whole quadword.
626 #define HAS_TEST_AND_SET
628 typedef unsigned long slock_t;
630 #include <alpha/builtins.h>
631 #define S_INIT_LOCK(lock) (*(lock) = 0)
632 #define TAS(lock) (__LOCK_LONG_RETRY((lock), 1) == 0)
633 #define S_UNLOCK(lock) __UNLOCK_LONG(lock)
635 #endif /* __alpha || __alpha__ */
638 #if defined(__hppa) || defined(__hppa__) /* HP PA-RISC, GCC and HP compilers */
640 * HP's PA-RISC
642 * See src/backend/port/hpux/tas.c.template for details about LDCWX. Because
643 * LDCWX requires a 16-byte-aligned address, we declare slock_t as a 16-byte
644 * struct. The active word in the struct is whichever has the aligned address;
645 * the other three words just sit at -1.
647 * When using gcc, we can inline the required assembly code.
649 #define HAS_TEST_AND_SET
651 typedef struct
653 int sema[4];
654 } slock_t;
656 #define TAS_ACTIVE_WORD(lock) ((volatile int *) (((long) (lock) + 15) & ~15))
658 #if defined(__GNUC__)
660 static __inline__ int
661 tas(volatile slock_t *lock)
663 volatile int *lockword = TAS_ACTIVE_WORD(lock);
664 register int lockval;
666 __asm__ __volatile__(
667 " ldcwx 0(0,%2),%0 \n"
668 : "=r"(lockval), "+m"(*lockword)
669 : "r"(lockword)
670 : "memory");
671 return (lockval == 0);
674 #endif /* __GNUC__ */
676 #define S_UNLOCK(lock) (*TAS_ACTIVE_WORD(lock) = -1)
678 #define S_INIT_LOCK(lock) \
679 do { \
680 volatile slock_t *lock_ = (lock); \
681 lock_->sema[0] = -1; \
682 lock_->sema[1] = -1; \
683 lock_->sema[2] = -1; \
684 lock_->sema[3] = -1; \
685 } while (0)
687 #define S_LOCK_FREE(lock) (*TAS_ACTIVE_WORD(lock) != 0)
689 #endif /* __hppa || __hppa__ */
692 #if defined(__hpux) && defined(__ia64) && !defined(__GNUC__)
694 #define HAS_TEST_AND_SET
696 typedef unsigned int slock_t;
698 #include <ia64/sys/inline.h>
699 #define TAS(lock) _Asm_xchg(_SZ_W, lock, 1, _LDHINT_NONE)
701 #endif /* HPUX on IA64, non gcc */
704 #if defined(__sgi) /* SGI compiler */
706 * SGI IRIX 5
707 * slock_t is defined as a unsigned long. We use the standard SGI
708 * mutex API.
710 * The following comment is left for historical reasons, but is probably
711 * not a good idea since the mutex ABI is supported.
713 * This stuff may be supplemented in the future with Masato Kataoka's MIPS-II
714 * assembly from his NECEWS SVR4 port, but we probably ought to retain this
715 * for the R3000 chips out there.
717 #define HAS_TEST_AND_SET
719 typedef unsigned long slock_t;
721 #include "mutex.h"
722 #define TAS(lock) (test_and_set(lock,1))
723 #define S_UNLOCK(lock) (test_then_and(lock,0))
724 #define S_INIT_LOCK(lock) (test_then_and(lock,0))
725 #define S_LOCK_FREE(lock) (test_then_add(lock,0) == 0)
726 #endif /* __sgi */
729 #if defined(sinix) /* Sinix */
731 * SINIX / Reliant UNIX
732 * slock_t is defined as a struct abilock_t, which has a single unsigned long
733 * member. (Basically same as SGI)
735 #define HAS_TEST_AND_SET
737 #include "abi_mutex.h"
738 typedef abilock_t slock_t;
740 #define TAS(lock) (!acquire_lock(lock))
741 #define S_UNLOCK(lock) release_lock(lock)
742 #define S_INIT_LOCK(lock) init_lock(lock)
743 #define S_LOCK_FREE(lock) (stat_lock(lock) == UNLOCKED)
744 #endif /* sinix */
747 #if defined(_AIX) /* AIX */
749 * AIX (POWER)
751 #define HAS_TEST_AND_SET
753 #include <sys/atomic_op.h>
755 typedef int slock_t;
757 #define TAS(lock) _check_lock((slock_t *) (lock), 0, 1)
758 #define S_UNLOCK(lock) _clear_lock((slock_t *) (lock), 0)
759 #endif /* _AIX */
762 #if defined (nextstep) /* Nextstep */
763 #define HAS_TEST_AND_SET
765 typedef struct mutex slock_t;
767 #define S_LOCK(lock) mutex_lock(lock)
768 #define S_UNLOCK(lock) mutex_unlock(lock)
769 #define S_INIT_LOCK(lock) mutex_init(lock)
770 /* For Mach, we have to delve inside the entrails of `struct mutex'. Ick! */
771 #define S_LOCK_FREE(alock) ((alock)->lock == 0)
772 #endif /* nextstep */
775 /* These are in s_lock.c */
778 #if defined(sun3) /* Sun3 */
779 #define HAS_TEST_AND_SET
781 typedef unsigned char slock_t;
782 #endif
785 #if defined(__SUNPRO_C) && (defined(__i386) || defined(__x86_64__) || defined(__sparc__) || defined(__sparc))
786 #define HAS_TEST_AND_SET
788 #if defined(__i386) || defined(__x86_64__) || defined(__sparcv9) || defined(__sparcv8plus)
789 typedef unsigned int slock_t;
790 #else
791 typedef unsigned char slock_t;
792 #endif
794 extern slock_t pg_atomic_cas(volatile slock_t *lock, slock_t with,
795 slock_t cmp);
797 #define TAS(a) (pg_atomic_cas((a), 1, 0) != 0)
798 #endif
801 #ifdef WIN32_ONLY_COMPILER
802 typedef LONG slock_t;
804 #define HAS_TEST_AND_SET
805 #define TAS(lock) (InterlockedCompareExchange(lock, 1, 0))
807 #define SPIN_DELAY() spin_delay()
809 static __forceinline void
810 spin_delay(void)
812 /* See comment for gcc code. Same code, MASM syntax */
813 __asm rep nop;
816 #endif
819 #endif /* !defined(HAS_TEST_AND_SET) */
822 /* Blow up if we didn't have any way to do spinlocks */
823 #ifndef HAS_TEST_AND_SET
824 #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.
825 #endif
828 #else /* !HAVE_SPINLOCKS */
832 * Fake spinlock implementation using semaphores --- slow and prone
833 * to fall foul of kernel limits on number of semaphores, so don't use this
834 * unless you must! The subroutines appear in spin.c.
836 typedef PGSemaphoreData slock_t;
838 extern bool s_lock_free_sema(volatile slock_t *lock);
839 extern void s_unlock_sema(volatile slock_t *lock);
840 extern void s_init_lock_sema(volatile slock_t *lock);
841 extern int tas_sema(volatile slock_t *lock);
843 #define S_LOCK_FREE(lock) s_lock_free_sema(lock)
844 #define S_UNLOCK(lock) s_unlock_sema(lock)
845 #define S_INIT_LOCK(lock) s_init_lock_sema(lock)
846 #define TAS(lock) tas_sema(lock)
849 #endif /* HAVE_SPINLOCKS */
853 * Default Definitions - override these above as needed.
856 #if !defined(S_LOCK)
857 #define S_LOCK(lock) \
858 do { \
859 if (TAS(lock)) \
860 s_lock((lock), __FILE__, __LINE__); \
861 } while (0)
862 #endif /* S_LOCK */
864 #if !defined(S_LOCK_FREE)
865 #define S_LOCK_FREE(lock) (*(lock) == 0)
866 #endif /* S_LOCK_FREE */
868 #if !defined(S_UNLOCK)
869 #define S_UNLOCK(lock) (*((volatile slock_t *) (lock)) = 0)
870 #endif /* S_UNLOCK */
872 #if !defined(S_INIT_LOCK)
873 #define S_INIT_LOCK(lock) S_UNLOCK(lock)
874 #endif /* S_INIT_LOCK */
876 #if !defined(SPIN_DELAY)
877 #define SPIN_DELAY() ((void) 0)
878 #endif /* SPIN_DELAY */
880 #if !defined(TAS)
881 extern int tas(volatile slock_t *lock); /* in port/.../tas.s, or
882 * s_lock.c */
884 #define TAS(lock) tas(lock)
885 #endif /* TAS */
889 * Platform-independent out-of-line support routines
891 extern void s_lock(volatile slock_t *lock, const char *file, int line);
893 /* Support for dynamic adjustment of spins_per_delay */
894 #define DEFAULT_SPINS_PER_DELAY 100
896 extern void set_spins_per_delay(int shared_spins_per_delay);
897 extern int update_spins_per_delay(int shared_spins_per_delay);
899 #endif /* S_LOCK_H */