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 * $PostgreSQL: pgsql/src/backend/storage/lmgr/s_lock.c,v 1.18 2003/12/22 23:39:53 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 "\nStuck 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)
49 * We loop tightly for awhile, then delay using select() and try
50 * again. Preferably, "awhile" should be a small multiple of the
51 * maximum time we expect a spinlock to be held. 100 iterations seems
52 * about right. In most multi-CPU scenarios, the spinlock is probably
53 * held by a process on another CPU and will be released before we
54 * finish 100 iterations. However, on a uniprocessor, the tight loop
55 * is just a waste of cycles, so don't iterate thousands of times.
57 * Once we do decide to block, we use randomly increasing select()
58 * delays. The first delay is 10 msec, then the delay randomly
59 * increases to about one second, after which we reset to 10 msec and
60 * start again. The idea here is that in the presence of heavy
61 * contention we need to increase the delay, else the spinlock holder
62 * may never get to run and release the lock. (Consider situation
63 * where spinlock holder has been nice'd down in priority by the
64 * scheduler --- it will not get scheduled until all would-be
65 * acquirers are sleeping, so if we always use a 10-msec sleep, there
66 * is a real possibility of starvation.) But we can't just clamp the
67 * delay to an upper bound, else it would take a long time to make a
68 * reasonable number of tries.
70 * We time out and declare error after NUM_DELAYS delays (thus, exactly
71 * that many tries). With the given settings, this will usually take
72 * 3 or so minutes. It seems better to fix the total number of tries
73 * (and thus the probability of unintended failure) than to fix the
76 * The select() delays are measured in centiseconds (0.01 sec) because 10
77 * msec is a common resolution limit at the OS level.
79 #define SPINS_PER_DELAY 100
80 #define NUM_DELAYS 1000
81 #define MIN_DELAY_CSEC 1
82 #define MAX_DELAY_CSEC 100
86 int cur_delay = MIN_DELAY_CSEC;
91 if (++spins > SPINS_PER_DELAY)
93 if (++delays > NUM_DELAYS)
94 s_lock_stuck(lock, file, line);
96 delay.tv_sec = cur_delay / 100;
97 delay.tv_usec = (cur_delay % 100) * 10000;
98 (void) select(0, NULL, NULL, NULL, &delay);
100 #if defined(S_LOCK_TEST)
101 fprintf(stdout, "*");
105 /* increase delay by a random fraction between 1X and 2X */
106 cur_delay += (int) (cur_delay *
107 (((double) random()) / ((double) MAX_RANDOM_VALUE)) + 0.5);
108 /* wrap back to minimum delay when max is exceeded */
109 if (cur_delay > MAX_DELAY_CSEC)
110 cur_delay = MIN_DELAY_CSEC;
118 * Various TAS implementations that cannot live in s_lock.h as no inline
119 * definition exists (yet).
120 * In the future, get rid of tas.[cso] and fold it into this file.
124 #if defined(__GNUC__)
125 /*************************************************************************
126 * All the gcc flavors that are not inlined
130 #if defined(__m68k__)
132 tas_dummy() /* really means: extern int tas(slock_t
135 __asm__ __volatile__(
139 movel sp@(0x4),a0 \n\
149 #endif /* __m68k__ */
151 #if defined(__mips__) && !defined(__sgi)
155 __asm__ __volatile__(
159 .frame $sp, 0, $31 \n\
169 .livereg 0x2000FF0E,0x00000FFF \n\
176 #endif /* __mips__ && !__sgi */
178 #else /* not __GNUC__ */
179 /***************************************************************************
187 tas_dummy() /* really means: extern int tas(slock_t
196 asm("|#PROLOGUE# 1");
197 asm(" movel sp@(0x4),a0");
200 asm(" moveq #-128,d0");
211 #if defined(__sparc__) || defined(__sparc)
213 * sparc machines not using gcc
216 tas_dummy() /* really means: extern int tas(slock_t
219 asm(".seg \"data\"");
220 asm(".seg \"text\"");
224 * Sparc atomic test and set (sparc calls it "atomic load-store")
226 asm("ldstub [%r8], %r8");
230 #endif /* __sparc || __sparc__ */
235 #if defined(NEED_I386_TAS_ASM)
236 /* non gcc i386 based things */
237 #endif /* NEED_I386_TAS_ASM */
238 #endif /* not __GNUC__ */
243 /*****************************************************************************/
244 #if defined(S_LOCK_TEST)
247 * test program for verifying a port's spinlock support.
250 volatile slock_t test_lock;
255 srandom((unsigned int) time(NULL));
257 S_INIT_LOCK(&test_lock);
259 if (!S_LOCK_FREE(&test_lock))
261 printf("S_LOCK_TEST: failed, lock not initialized\n");
267 if (S_LOCK_FREE(&test_lock))
269 printf("S_LOCK_TEST: failed, lock not locked\n");
273 S_UNLOCK(&test_lock);
275 if (!S_LOCK_FREE(&test_lock))
277 printf("S_LOCK_TEST: failed, lock not unlocked\n");
283 if (S_LOCK_FREE(&test_lock))
285 printf("S_LOCK_TEST: failed, lock not re-locked\n");
289 printf("S_LOCK_TEST: this will print %d stars and then\n", NUM_DELAYS);
290 printf(" exit with a 'stuck spinlock' message\n");
291 printf(" if S_LOCK() and TAS() are working.\n");
294 s_lock(&test_lock, __FILE__, __LINE__);
296 printf("S_LOCK_TEST: failed, lock not locked\n");
300 #endif /* S_LOCK_TEST */