1 /*-------------------------------------------------------------------------
4 * Hardware-dependent implementation of spinlocks.
7 * Portions Copyright (c) 1996-2003, PostgreSQL Global Development Group
8 * Portions Copyright (c) 1994, Regents of the University of California
12 * $Header: /cvsroot/pgsql/src/backend/storage/lmgr/s_lock.c,v 1.13 2003/08/04 02:40:03 momjian Exp $
14 *-------------------------------------------------------------------------
21 #include "storage/s_lock.h"
25 * s_lock_stuck() - complain about a stuck spinlock
28 s_lock_stuck(volatile slock_t *lock, const char *file, int line)
30 #if defined(S_LOCK_TEST)
32 "\nFATAL: stuck spinlock (%p) detected at %s:%d.\n",
36 elog(PANIC, "stuck spinlock (%p) detected at %s:%d",
43 * s_lock(lock) - platform-independent portion of waiting for a spinlock.
46 s_lock(volatile slock_t *lock, const char *file, int line)
53 * We loop tightly for awhile, then delay using select() and try
54 * again. Preferably, "awhile" should be a small multiple of the
55 * maximum time we expect a spinlock to be held. 100 iterations seems
58 * We use a 10 millisec select delay because that is the lower limit on
59 * many platforms. The timeout is figured on this delay only, and so
60 * the nominal 1 minute is a lower bound.
62 #define SPINS_PER_DELAY 100
64 #define TIMEOUT_MSEC (60 * 1000)
68 if (++spins > SPINS_PER_DELAY)
70 if (++delays > (TIMEOUT_MSEC / DELAY_MSEC))
71 s_lock_stuck(lock, file, line);
74 delay.tv_usec = DELAY_MSEC * 1000;
75 (void) select(0, NULL, NULL, NULL, &delay);
83 * Various TAS implementations that cannot live in s_lock.h as no inline
84 * definition exists (yet).
85 * In the future, get rid of tas.[cso] and fold it into this file.
90 /*************************************************************************
91 * All the gcc flavors that are not inlined
97 tas_dummy() /* really means: extern int tas(slock_t
100 __asm__ __volatile__(
104 movel sp@(0x4),a0 \n\
114 #endif /* __m68k__ */
116 #if defined(__mips__) && !defined(__sgi)
120 __asm__ __volatile__(
124 .frame $sp, 0, $31 \n\
134 .livereg 0x2000FF0E,0x00000FFF \n\
141 #endif /* __mips__ && !__sgi */
143 #else /* not __GNUC__ */
144 /***************************************************************************
152 tas_dummy() /* really means: extern int tas(slock_t
161 asm("|#PROLOGUE# 1");
162 asm(" movel sp@(0x4),a0");
165 asm(" moveq #-128,d0");
176 #if defined(NEED_SPARC_TAS_ASM)
178 * sparc machines not using gcc
181 tas_dummy() /* really means: extern int tas(slock_t
184 asm(".seg \"data\"");
185 asm(".seg \"text\"");
189 * Sparc atomic test and set (sparc calls it "atomic load-store")
191 asm("ldstub [%r8], %r8");
195 #endif /* NEED_SPARC_TAS_ASM */
200 #if defined(NEED_I386_TAS_ASM)
201 /* non gcc i386 based things */
202 #endif /* NEED_I386_TAS_ASM */
203 #endif /* not __GNUC__ */
208 /*****************************************************************************/
209 #if defined(S_LOCK_TEST)
212 * test program for verifying a port.
215 volatile slock_t test_lock;
220 S_INIT_LOCK(&test_lock);
222 if (!S_LOCK_FREE(&test_lock))
224 printf("S_LOCK_TEST: failed, lock not initialized.\n");
230 if (S_LOCK_FREE(&test_lock))
232 printf("S_LOCK_TEST: failed, lock not locked\n");
236 printf("S_LOCK_TEST: this will hang for a few minutes and then abort\n");
237 printf(" with a 'stuck spinlock' message if S_LOCK()\n");
238 printf(" and TAS() are working.\n");
239 s_lock(&test_lock, __FILE__, __LINE__);
241 printf("S_LOCK_TEST: failed, lock not locked~\n");
245 #endif /* S_LOCK_TEST */