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.27 2004/03/23 21:39:46 momjian Exp $
14 *-------------------------------------------------------------------------
20 #include "storage/s_lock.h"
21 #include "miscadmin.h"
24 * s_lock_stuck() - complain about a stuck spinlock
27 s_lock_stuck(volatile slock_t *lock, const char *file, int line)
29 #if defined(S_LOCK_TEST)
31 "\nStuck spinlock (%p) detected at %s:%d.\n",
35 elog(PANIC, "stuck spinlock (%p) detected at %s:%d",
42 * s_lock(lock) - platform-independent portion of waiting for a spinlock.
45 s_lock(volatile slock_t *lock, const char *file, int line)
48 * We loop tightly for awhile, then delay using pg_usleep() and try
49 * again. Preferably, "awhile" should be a small multiple of the
50 * maximum time we expect a spinlock to be held. 100 iterations seems
51 * about right. In most multi-CPU scenarios, the spinlock is probably
52 * held by a process on another CPU and will be released before we
53 * finish 100 iterations. However, on a uniprocessor, the tight loop
54 * is just a waste of cycles, so don't iterate thousands of times.
56 * Once we do decide to block, we use randomly increasing pg_usleep()
57 * delays. The first delay is 10 msec, then the delay randomly
58 * increases to about one second, after which we reset to 10 msec and
59 * start again. The idea here is that in the presence of heavy
60 * contention we need to increase the delay, else the spinlock holder
61 * may never get to run and release the lock. (Consider situation
62 * where spinlock holder has been nice'd down in priority by the
63 * scheduler --- it will not get scheduled until all would-be
64 * acquirers are sleeping, so if we always use a 10-msec sleep, there
65 * is a real possibility of starvation.) But we can't just clamp the
66 * delay to an upper bound, else it would take a long time to make a
67 * reasonable number of tries.
69 * We time out and declare error after NUM_DELAYS delays (thus, exactly
70 * that many tries). With the given settings, this will usually take
71 * 3 or so minutes. It seems better to fix the total number of tries
72 * (and thus the probability of unintended failure) than to fix the
75 * The pg_usleep() delays are measured in centiseconds (0.01 sec) because 10
76 * msec is a common resolution limit at the OS level.
78 #define SPINS_PER_DELAY 100
79 #define NUM_DELAYS 1000
80 #define MIN_DELAY_CSEC 1
81 #define MAX_DELAY_CSEC 100
85 int cur_delay = MIN_DELAY_CSEC;
89 /* CPU-specific delay each time through the loop */
92 /* Block the process every SPINS_PER_DELAY tries */
93 if (++spins > SPINS_PER_DELAY)
95 if (++delays > NUM_DELAYS)
96 s_lock_stuck(lock, file, line);
98 pg_usleep(cur_delay * 10000L);
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.
122 * If you change something here, you will likely need to modify s_lock.h too,
123 * because the definitions for these are split between this file and s_lock.h.
127 #ifdef HAVE_SPINLOCKS /* skip spinlocks if requested */
130 #if defined(__GNUC__)
133 * All the gcc flavors that are not inlined
137 #if defined(__m68k__)
139 tas_dummy() /* really means: extern int tas(slock_t
142 __asm__ __volatile__(
146 movel sp@(0x4),a0 \n\
156 #endif /* __m68k__ */
159 #if defined(__mips__) && !defined(__sgi)
163 __asm__ __volatile__(
167 .frame $sp, 0, $31 \n\
177 .livereg 0x2000FF0E,0x00000FFF \n\
184 #endif /* __mips__ && !__sgi */
187 #else /* not __GNUC__ */
196 tas_dummy() /* really means: extern int tas(slock_t
205 asm("|#PROLOGUE# 1");
206 asm(" movel sp@(0x4),a0");
209 asm(" moveq #-128,d0");
219 #if defined(__sparc__) || defined(__sparc)
221 * sparc machines not using gcc
224 tas_dummy() /* really means: extern int tas(slock_t
227 asm(".seg \"data\"");
228 asm(".seg \"text\"");
232 * Sparc atomic test and set (sparc calls it "atomic load-store")
234 asm("ldstub [%r8], %r8");
238 #endif /* __sparc || __sparc__ */
241 #endif /* not __GNUC__ */
243 #endif /* HAVE_SPINLOCKS */
247 /*****************************************************************************/
248 #if defined(S_LOCK_TEST)
251 * test program for verifying a port's spinlock support.
254 volatile slock_t test_lock;
259 srandom((unsigned int) time(NULL));
261 S_INIT_LOCK(&test_lock);
263 if (!S_LOCK_FREE(&test_lock))
265 printf("S_LOCK_TEST: failed, lock not initialized\n");
271 if (S_LOCK_FREE(&test_lock))
273 printf("S_LOCK_TEST: failed, lock not locked\n");
277 S_UNLOCK(&test_lock);
279 if (!S_LOCK_FREE(&test_lock))
281 printf("S_LOCK_TEST: failed, lock not unlocked\n");
287 if (S_LOCK_FREE(&test_lock))
289 printf("S_LOCK_TEST: failed, lock not re-locked\n");
293 printf("S_LOCK_TEST: this will print %d stars and then\n", NUM_DELAYS);
294 printf(" exit with a 'stuck spinlock' message\n");
295 printf(" if S_LOCK() and TAS() are working.\n");
298 s_lock(&test_lock, __FILE__, __LINE__);
300 printf("S_LOCK_TEST: failed, lock not locked\n");
304 #endif /* S_LOCK_TEST */