s_lock.h 12.6 KB
Newer Older
1 2
/*-------------------------------------------------------------------------
 *
3
 * s_lock.h
4 5
 *	   This file contains the in-line portion of the implementation
 *	   of spinlocks.
6
 *
Bruce Momjian's avatar
Add:  
Bruce Momjian committed
7 8
 * Portions Copyright (c) 1996-2000, PostgreSQL, Inc
 * Portions Copyright (c) 1994, Regents of the University of California
9 10 11
 *
 *
 * IDENTIFICATION
Bruce Momjian's avatar
Bruce Momjian committed
12
 *	  $Header: /cvsroot/pgsql/src/include/storage/s_lock.h,v 1.82 2001/01/19 07:03:53 momjian Exp $
13 14 15
 *
 *-------------------------------------------------------------------------
 */
16

17 18 19 20 21 22 23 24 25 26 27
/*----------
 * DESCRIPTION
 *	The public macros that must be provided are:
 *
 *	void S_INIT_LOCK(slock_t *lock)
 *		Initialize a spinlock (to the unlocked state).
 *
 *	void S_LOCK(slock_t *lock)
 *		Acquire a spinlock, waiting if necessary.
 *		Time out and abort() if unable to acquire the lock in a
 *		"reasonable" amount of time --- typically ~ 1 minute.
28
 *
29 30
 *	void S_UNLOCK(slock_t *lock)
 *		Unlock a previously acquired lock.
31
 *
32 33 34
 *	bool S_LOCK_FREE(slock_t *lock)
 *		Tests if the lock is free. Returns TRUE if free, FALSE if locked.
 *		This does *not* change the state of the lock.
35
 *
36 37
 *	int TAS(slock_t *lock)
 *		Atomic test-and-set instruction.  Attempt to acquire the lock,
Bruce Momjian's avatar
Bruce Momjian committed
38
 *		but do *not* wait.	Returns 0 if successful, nonzero if unable
39
 *		to acquire the lock.
40
 *
41 42 43
 *	TAS() is a lower-level part of the API, but is used directly in a
 *	few places that want to do other things while waiting for a lock.
 *	The S_LOCK() macro is equivalent to
44
 *
45 46 47 48
 *	void
 *	S_LOCK(slock_t *lock)
 *	{
 *		unsigned	spins = 0;
49
 *
50 51 52
 *		while (TAS(lock))
 *			S_LOCK_SLEEP(lock, spins++);
 *	}
53
 *
54 55 56 57
 *	where S_LOCK_SLEEP() checks for timeout and sleeps for a short
 *	interval.  Callers that want to perform useful work while waiting
 *	can write out this entire loop and insert the "useful work" inside
 *	the loop.
58
 *
59 60 61 62 63
 *	CAUTION to TAS() callers: on some platforms TAS() may sometimes
 *	report failure to acquire a lock even when the lock is not locked.
 *	For example, on Alpha TAS() will "fail" if interrupted.  Therefore
 *	TAS() must *always* be invoked in a retry loop as depicted, even when
 *	you are certain the lock is free.
64
 *
65 66 67
 *	On most supported platforms, TAS() uses a tas() function written
 *	in assembly language to execute a hardware atomic-test-and-set
 *	instruction.  Equivalent OS-supplied mutex routines could be used too.
68
 *
69 70 71 72 73 74
 *	If no system-specific TAS() is available (ie, HAS_TEST_AND_SET is not
 *	defined), then we fall back on an emulation that uses SysV semaphores.
 *	This emulation will be MUCH MUCH MUCH slower than a proper TAS()
 *	implementation, because of the cost of a kernel call per lock or unlock.
 *	An old report is that Postgres spends around 40% of its time in semop(2)
 *	when using the SysV semaphore code.
75
 *
76 77 78 79
 *	Note to implementors: there are default implementations for all these
 *	macros at the bottom of the file.  Check if your platform can use
 *	these or needs to override them.
 *----------
80
 */
81
#ifndef S_LOCK_H
82 83 84 85
#define S_LOCK_H

#include "storage/ipc.h"

86 87
/* Platform-independent out-of-line support routines */
extern void s_lock(volatile slock_t *lock,
Bruce Momjian's avatar
Bruce Momjian committed
88
	   const char *file, const int line);
89
extern void s_lock_sleep(unsigned spins, int microsec,
Bruce Momjian's avatar
Bruce Momjian committed
90 91
			 volatile slock_t *lock,
			 const char *file, const int line);
92

Vadim B. Mikheev's avatar
Vadim B. Mikheev committed
93

94 95
#if defined(HAS_TEST_AND_SET)

96 97 98 99

#if defined(__GNUC__)
/*************************************************************************
 * All the gcc inlines
100 101
 */

Bruce Momjian's avatar
Bruce Momjian committed
102
/*
103
 * Standard _asm format:
Bruce Momjian's avatar
Bruce Momjian committed
104
 *
105
 *	__asm__ __volatile__(
Bruce Momjian's avatar
Bruce Momjian committed
106 107 108 109 110 111 112 113
 *			"command;"
 *			"command;"
 *			"command;"
 *		:	"=r"(_res)			return value, in register
 *		:	"r"(lock)			argument, 'lock pointer', in register
 *		:	"r0");				inline code uses this register
 */

114

115
#if defined(__i386__)
116 117 118 119 120
#define TAS(lock) tas(lock)

static __inline__ int
tas(volatile slock_t *lock)
{
121
	register slock_t _res = 1;
122

123 124 125 126 127
	__asm__ __volatile__(
						"lock;"
						"xchgb %0,%1;"
			:			"=q"(_res), "=m"(*lock)
			:			"0"(_res));
128 129
	return (int) _res;
}
130

131
#endif	 /* __i386__ */
132 133


134 135 136 137
#ifdef __ia64__
#define TAS(lock) tas(lock)

static __inline__ int
Bruce Momjian's avatar
Bruce Momjian committed
138
tas(volatile slock_t *lock)
139
{
Bruce Momjian's avatar
Bruce Momjian committed
140
	long int	ret;
141

142 143 144 145 146
	__asm__ __volatile__(
						"xchg4 %0=%1,%2;"
			 :			"=r"(ret), "=m"(*lock)
			 :			"r"(1), "1"(*lock)
			 :			"memory");
147

Bruce Momjian's avatar
Bruce Momjian committed
148
	return (int) ret;
149
}
Bruce Momjian's avatar
Bruce Momjian committed
150 151

#endif	 /* __ia64__ */
152

153

154
#if defined(__arm__) || defined(__arm__)
155 156 157 158 159
#define TAS(lock) tas(lock)

static __inline__ int
tas(volatile slock_t *lock)
{
Bruce Momjian's avatar
Bruce Momjian committed
160
	register slock_t _res = 1;
161

162 163 164 165
	__asm__ __volatile__(
						"swpb %0, %0, [%3];"
			:			"=r"(_res), "=m"(*lock)
			:			"0"(_res), "r"(lock));
Bruce Momjian's avatar
Bruce Momjian committed
166
	return (int) _res;
167 168
}

Bruce Momjian's avatar
Bruce Momjian committed
169
#endif	 /* __arm__ */
170

171 172 173 174
#if defined(__s390__)
/*
 * S/390 Linux
 */
Bruce Momjian's avatar
Bruce Momjian committed
175
#define TAS(lock)	   tas(lock)
176 177 178 179

static inline int
tas(volatile slock_t *lock)
{
Bruce Momjian's avatar
Bruce Momjian committed
180 181
	int			_res;

182 183 184 185 186 187 188 189 190
	__asm__	__volatile__(
						"la 1,1;"
						"l 2,%2;"
						"slr 0,0;"
						"cs 0,1,0(2);"
						"lr %1,0;"
		   :			"=m"(lock), "=d"(_res)
		   :			"m"(lock)
		   :			"0", "1", "2");
Bruce Momjian's avatar
Bruce Momjian committed
191 192

	return (_res);
193
}
Bruce Momjian's avatar
Bruce Momjian committed
194 195

#endif	 /* __s390__ */
196 197


198
#if defined(__sparc__)
199 200 201 202 203
#define TAS(lock) tas(lock)

static __inline__ int
tas(volatile slock_t *lock)
{
204
	register slock_t _res = 1;
205

206 207 208 209
	__asm__ __volatile__(
						"ldstub [%2], %0;"
			:			"=r"(_res), "=m"(*lock)
			:			"r"(lock));
210 211
	return (int) _res;
}
212

213
#endif	 /* __sparc__ */
214 215


216
#if defined(__mc68000__) && defined(__linux__)
217 218 219 220 221 222
#define TAS(lock) tas(lock)

static __inline__ int
tas(volatile slock_t *lock)
{
	register int rv;
Bruce Momjian's avatar
Bruce Momjian committed
223

224 225 226 227 228 229
	__asm__	__volatile__(
						"tas %1;"
						"sne %0;"
			 :			"=d"(rv), "=m"(*lock)
			 :			"1"(*lock)
			 :			"cc");
Bruce Momjian's avatar
Bruce Momjian committed
230

231 232 233
	return rv;
}

Bruce Momjian's avatar
Bruce Momjian committed
234
#endif	 /* defined(__mc68000__) && defined(__linux__) */
235

236 237 238 239 240 241 242 243 244 245 246 247 248 249 250

#if defined(NEED_VAX_TAS_ASM)
/*
 * VAXen -- even multiprocessor ones
 * (thanks to Tom Ivar Helbekkmo)
 */
#define TAS(lock) tas(lock)

typedef unsigned char slock_t;

static __inline__ int
tas(volatile slock_t *lock)
{
	register	_res;

251 252 253 254 255 256 257 258
	__asm__ __volatile__(
						"movl $1, r0;"
						"bbssi $0, (%1), 1f;"
						"clrl r0;"
						"1: movl r0, %0;"
			:			"=r"(_res)
			:			"r"(lock)
			:			"r0");
259 260
	return (int) _res;
}
261 262

#endif	 /* NEED_VAX_TAS_ASM */
263 264


265 266 267 268 269 270
#if defined(NEED_NS32K_TAS_ASM)
#define TAS(lock) tas(lock)

static __inline__ int
tas(volatile slock_t *lock)
{
Bruce Momjian's avatar
Bruce Momjian committed
271 272
	register	_res;

273 274 275 276
	__asm__ __volatile__(
						"sbitb 0, %0;"
						"sfsd %1;"
			:			"=m"(*lock), "=r"(_res));
Bruce Momjian's avatar
Bruce Momjian committed
277
	return (int) _res;
278 279
}

Bruce Momjian's avatar
Bruce Momjian committed
280
#endif	 /* NEED_NS32K_TAS_ASM */
281 282


283

284
#else							/* !__GNUC__ */
285

286 287
/***************************************************************************
 * All non-gcc inlines
288 289
 */

290
#if defined(NEED_I386_TAS_ASM) && defined(USE_UNIVEL_CC)
291
#define TAS(lock)	tas(lock)
292

293
asm int
294
tas(volatile slock_t *s_lock)
295
{
296
/* UNIVEL wants %mem in column 1, so we don't pg_indent this file */
Bruce Momjian's avatar
Bruce Momjian committed
297
%mem s_lock
298
	pushl %ebx
299 300
	movl s_lock, %ebx
	movl $255, %eax
301
	lock
302 303
	xchgb %al, (%ebx)
	popl %ebx
304
}
305

Bruce Momjian's avatar
Bruce Momjian committed
306
#endif	 /* defined(NEED_I386_TAS_ASM) && defined(USE_UNIVEL_CC) */
307

308
#endif	 /* defined(__GNUC__) */
309 310 311 312



/*************************************************************************
313 314
 * These are the platforms that do not use inline assembler (and hence
 * have common code for gcc and non-gcc compilers, if both are available).
315
 */
316

317 318 319 320

#if defined(__alpha)

/*
321 322 323 324
 * Correct multi-processor locking methods are explained in section 5.5.3
 * of the Alpha AXP Architecture Handbook, which at this writing can be
 * found at ftp://ftp.netbsd.org/pub/NetBSD/misc/dec-docs/index.html.
 * For gcc we implement the handbook's code directly with inline assembler.
325
 */
326
#if defined(__GNUC__)
327

328
#define TAS(lock)  tas(lock)
329 330 331 332 333 334
#define S_UNLOCK(lock)	\
do \
{\
	__asm__ __volatile__ ("mb"); \
	*(lock) = 0; \
} while (0)
335 336 337 338

static __inline__ int
tas(volatile slock_t *lock)
{
339 340
	register slock_t _res;

341 342 343 344 345 346 347 348 349 350 351 352 353 354 355
	__asm__	__volatile__(
						"ldq   $0, %0;"
						"bne   $0, 2f;"
						"ldq_l %1, %0;"
						"bne   %1, 2f;"
						"mov   1, $0;"
						"stq_c $0, %0;"
						"beq   $0, 2f;"
						"mb;"
						"br 3f;"
						"2: mov   1, %1;"
						"3:"
			 :			"=m"(*lock), "=r"(_res)
			 :
			 :			"0");
356 357 358

	return (int) _res;
}
359

Bruce Momjian's avatar
Bruce Momjian committed
360
#else							/* !defined(__GNUC__) */
361 362 363 364 365 366 367 368 369 370 371 372 373

/*
 * The Tru64 compiler doesn't support gcc-style inline asm, but it does
 * have some builtin functions that accomplish much the same results.
 * For simplicity, slock_t is defined as long (ie, quadword) on Alpha
 * regardless of the compiler in use.  LOCK_LONG and UNLOCK_LONG only
 * operate on an int (ie, longword), but that's OK as long as we define
 * S_INIT_LOCK to zero out the whole quadword.
 */

#include <alpha/builtins.h>

#define S_INIT_LOCK(lock)  (*(lock) = 0)
Bruce Momjian's avatar
Bruce Momjian committed
374 375
#define TAS(lock)		   (__LOCK_LONG_RETRY((lock), 1) == 0)
#define S_UNLOCK(lock)	   __UNLOCK_LONG(lock)
376

Bruce Momjian's avatar
Bruce Momjian committed
377
#endif	 /* defined(__GNUC__) */
378

Bruce Momjian's avatar
Bruce Momjian committed
379
#endif	 /* __alpha */
380 381


382 383 384 385 386
#if defined(__hpux)
/*
 * HP-UX (PA-RISC)
 *
 * Note that slock_t on PA-RISC is a structure instead of char
387
 * (see include/port/hpux.h).
388 389
 *
 * a "set" slock_t has a single word cleared.  a "clear" slock_t has
390
 * all words set to non-zero. tas() is in tas.s
391
 */
392

393
#define S_UNLOCK(lock) \
Bruce Momjian's avatar
Bruce Momjian committed
394 395 396 397 398
	do { \
		volatile slock_t *lock_ = (volatile slock_t *) (lock); \
		lock_->sema[0] = lock_->sema[1] = \
		lock_->sema[2] = lock_->sema[3] = -1; \
	} while (0)
399

400
#define S_LOCK_FREE(lock)	( *(int *) (((long) (lock) + 15) & ~15) != 0)
401

402
#endif	 /* __hpux */
403

404

405 406 407 408 409 410
#if defined(__QNX__)
/*
 * QNX 4
 *
 * Note that slock_t under QNX is sem_t instead of char
 */
Bruce Momjian's avatar
Bruce Momjian committed
411 412 413 414 415
#define TAS(lock)		(sem_trywait((lock)) < 0)
#define S_UNLOCK(lock)	sem_post((lock))
#define S_INIT_LOCK(lock)		sem_init((lock), 1, 1)
#define S_LOCK_FREE(lock)		((lock)->value)
#endif	 /* __QNX__ */
416 417


418 419 420
#if defined(__sgi)
/*
 * SGI IRIX 5
421
 * slock_t is defined as a unsigned long. We use the standard SGI
Bruce Momjian's avatar
Bruce Momjian committed
422
 * mutex API.
423 424 425
 *
 * The following comment is left for historical reasons, but is probably
 * not a good idea since the mutex ABI is supported.
426 427 428 429 430
 *
 * This stuff may be supplemented in the future with Masato Kataoka's MIPS-II
 * assembly from his NECEWS SVR4 port, but we probably ought to retain this
 * for the R3000 chips out there.
 */
431
#include "mutex.h"
432 433 434 435
#define TAS(lock)	(test_and_set(lock,1))
#define S_UNLOCK(lock)	(test_then_and(lock,0))
#define S_INIT_LOCK(lock)	(test_then_and(lock,0))
#define S_LOCK_FREE(lock)	(test_then_add(lock,0) == 0)
436
#endif	 /* __sgi */
437

438 439
#if defined(sinix)
/*
Bruce Momjian's avatar
Bruce Momjian committed
440
 * SINIX / Reliant UNIX
441 442 443 444 445 446 447 448 449
 * slock_t is defined as a struct abilock_t, which has a single unsigned long
 * member. (Basically same as SGI)
 *
 */
#define TAS(lock)	(!acquire_lock(lock))
#define S_UNLOCK(lock)	release_lock(lock)
#define S_INIT_LOCK(lock)	init_lock(lock)
#define S_LOCK_FREE(lock)	(stat_lock(lock) == UNLOCKED)
#endif	 /* sinix */
Bruce Momjian's avatar
Bruce Momjian committed
450

451 452 453 454 455 456 457 458 459

#if defined(_AIX)
/*
 * AIX (POWER)
 *
 * Note that slock_t on POWER/POWER2/PowerPC is int instead of char
 * (see storage/ipc.h).
 */
#define TAS(lock)	cs((int *) (lock), 0, 1)
460
#endif	 /* _AIX */
461 462 463 464 465 466 467 468 469 470 471 472 473


#if defined (nextstep)
/*
 * NEXTSTEP (mach)
 * slock_t is defined as a struct mutex.
 */

#define S_LOCK(lock)	mutex_lock(lock)
#define S_UNLOCK(lock)	mutex_unlock(lock)
#define S_INIT_LOCK(lock)	mutex_init(lock)
/* For Mach, we have to delve inside the entrails of `struct mutex'.  Ick! */
#define S_LOCK_FREE(alock)	((alock)->lock == 0)
474
#endif	 /* nextstep */
475 476 477



Bruce Momjian's avatar
Bruce Momjian committed
478
#else							/* !HAS_TEST_AND_SET */
479 480 481 482 483 484 485 486 487 488

/*
 * Fake spinlock implementation using SysV semaphores --- slow and prone
 * to fall foul of kernel limits on number of semaphores, so don't use this
 * unless you must!
 */

typedef struct
{
	/* reference to semaphore used to implement this spinlock */
Bruce Momjian's avatar
Bruce Momjian committed
489 490
	IpcSemaphoreId semId;
	int			sem;
491 492 493 494 495
} slock_t;

extern bool s_lock_free_sema(volatile slock_t *lock);
extern void s_unlock_sema(volatile slock_t *lock);
extern void s_init_lock_sema(volatile slock_t *lock);
Bruce Momjian's avatar
Bruce Momjian committed
496
extern int	tas_sema(volatile slock_t *lock);
497

Bruce Momjian's avatar
Bruce Momjian committed
498 499 500 501
#define S_LOCK_FREE(lock)	s_lock_free_sema(lock)
#define S_UNLOCK(lock)	 s_unlock_sema(lock)
#define S_INIT_LOCK(lock)	s_init_lock_sema(lock)
#define TAS(lock)	tas_sema(lock)
502 503 504 505

#endif	 /* HAS_TEST_AND_SET */


506

507 508 509
/****************************************************************************
 * Default Definitions - override these above as needed.
 */
510

511
#if !defined(S_LOCK)
512
#define S_LOCK(lock) \
513
	do { \
514 515
		if (TAS(lock)) \
			s_lock((lock), __FILE__, __LINE__); \
516
	} while (0)
517
#endif	 /* S_LOCK */
518

519 520 521 522 523 524 525 526 527 528
#if !defined(S_LOCK_SLEEP)
#define S_LOCK_SLEEP(lock,spins) \
	s_lock_sleep((spins), 0, (lock), __FILE__, __LINE__)
#endif	 /* S_LOCK_SLEEP */

#if !defined(S_LOCK_SLEEP_INTERVAL)
#define S_LOCK_SLEEP_INTERVAL(lock,spins,microsec) \
	s_lock_sleep((spins), (microsec), (lock), __FILE__, __LINE__)
#endif	 /* S_LOCK_SLEEP_INTERVAL */

529
#if !defined(S_LOCK_FREE)
530
#define S_LOCK_FREE(lock)	(*(lock) == 0)
531
#endif	 /* S_LOCK_FREE */
532

533 534
#if !defined(S_UNLOCK)
#define S_UNLOCK(lock)		(*(lock) = 0)
535
#endif	 /* S_UNLOCK */
536

537
#if !defined(S_INIT_LOCK)
538
#define S_INIT_LOCK(lock)	S_UNLOCK(lock)
539
#endif	 /* S_INIT_LOCK */
540

541
#if !defined(TAS)
542
extern int	tas(volatile slock_t *lock);		/* in port/.../tas.s, or
543
												 * s_lock.c */
Bruce Momjian's avatar
Bruce Momjian committed
544

545
#define TAS(lock)		tas(lock)
546
#endif	 /* TAS */
547

548 549

#endif	 /* S_LOCK_H */