s_lock.h 18.1 KB
Newer Older
1 2
/*-------------------------------------------------------------------------
 *
3
 * s_lock.h
4
 *	   Hardware-dependent implementation of spinlocks.
5
 *
6 7
 *	NOTE: none of the macros in this file are intended to be called directly.
 *	Call them through the hardware-independent macros in spin.h.
8
 *
9 10
 *	The following hardware-dependent macros must be provided for each
 *	supported platform:
11 12 13 14 15 16 17 18
 *
 *	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.
19
 *
20 21
 *	void S_UNLOCK(slock_t *lock)
 *		Unlock a previously acquired lock.
22
 *
23 24 25
 *	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.
26
 *
27 28 29
 *	void SPIN_DELAY(void)
 *		Delay operation to occur inside spinlock wait loop.
 *
30 31 32 33 34 35 36
 *	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.
 *
 *  Usually, S_LOCK() is implemented in terms of an even lower-level macro
 *	TAS():
 *
37 38
 *	int TAS(slock_t *lock)
 *		Atomic test-and-set instruction.  Attempt to acquire the lock,
Bruce Momjian's avatar
Bruce Momjian committed
39
 *		but do *not* wait.	Returns 0 if successful, nonzero if unable
40
 *		to acquire the lock.
41
 *
42
 *	TAS() is NOT part of the API, and should never be called directly.
43
 *
44 45 46 47
 *	CAUTION: 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() should always be invoked
 *	in a retry loop, even if you are certain the lock is free.
48
 *
49 50 51 52 53
 *	ANOTHER CAUTION: be sure that TAS() and S_UNLOCK() represent sequence
 *	points, ie, loads and stores of other values must not be moved across
 *	a lock or unlock.  In most cases it suffices to make the operation be
 *	done through a "volatile" pointer.
 *
54 55 56
 *	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.
57
 *
58
 *	If no system-specific TAS() is available (ie, HAVE_SPINLOCKS is not
59 60
 *	defined), then we fall back on an emulation that uses SysV semaphores
 *	(see spin.c).  This emulation will be MUCH MUCH slower than a proper TAS()
61 62 63
 *	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.
64
 *
65
 *
66
 * Portions Copyright (c) 1996-2005, PostgreSQL Global Development Group
67 68
 * Portions Copyright (c) 1994, Regents of the University of California
 *
69
 *	  $PostgreSQL: pgsql/src/include/storage/s_lock.h,v 1.134 2005/03/10 21:41:01 momjian Exp $
70 71
 *
 *-------------------------------------------------------------------------
72
 */
73
#ifndef S_LOCK_H
74 75
#define S_LOCK_H

76
#include "storage/pg_sema.h"
77

78
#ifdef HAVE_SPINLOCKS	/* skip spinlocks if requested */
Vadim B. Mikheev's avatar
Vadim B. Mikheev committed
79

80

81
#if defined(__GNUC__) || defined(__ICC)
82 83
/*************************************************************************
 * All the gcc inlines
84 85
 * Gcc consistently defines the CPU as __cpu__.
 * Other compilers use __cpu or __cpu__ so we test for both in those cases.
86 87
 */

88 89 90
/*----------
 * Standard gcc asm format (assuming "volatile slock_t *lock"):

91
	__asm__ __volatile__(
92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107
		"	instruction	\n"
		"	instruction	\n"
		"	instruction	\n"
:		"=r"(_res), "+m"(*lock)		// return register, in/out lock value
:		"r"(lock)					// lock pointer, in input register
:		"memory", "cc");			// show clobbered registers here

 * The output-operands list (after first colon) should always include
 * "+m"(*lock), whether or not the asm code actually refers to this
 * operand directly.  This ensures that gcc believes the value in the
 * lock variable is used and set by the asm code.  Also, the clobbers
 * list (after third colon) should always include "memory"; this prevents
 * gcc from thinking it can cache the values of shared-memory fields
 * across the asm code.  Add "cc" if your asm code changes the condition
 * code register, and also list any temp registers the code uses.
 *----------
Bruce Momjian's avatar
Bruce Momjian committed
108 109
 */

110

Bruce Momjian's avatar
Bruce Momjian committed
111
#if defined(__i386__) || defined(__x86_64__) /* AMD Opteron */
112 113
#define HAS_TEST_AND_SET

114 115
typedef unsigned char slock_t;

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
	/* Use a non-locking test before asserting the bus lock */
124
	__asm__ __volatile__(
125 126
		"	cmpb	$0,%1	\n"
		"	jne		1f		\n"
127 128
		"	lock			\n"
		"	xchgb	%0,%1	\n"
129
		"1: \n"
130 131 132
:		"+q"(_res), "+m"(*lock)
:
:		"memory", "cc");
133 134
	return (int) _res;
}
135

136 137 138 139 140
#define SPIN_DELAY() spin_delay()

static __inline__ void
spin_delay(void)
{
141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163
	/*
	 * This sequence is equivalent to the PAUSE instruction ("rep" is
	 * ignored by old IA32 processors if the following instruction is
	 * not a string operation); the IA-32 Architecture Software
	 * Developer's Manual, Vol. 3, Section 7.7.2 describes why using
	 * PAUSE in the inner loop of a spin lock is necessary for good
	 * performance:
	 *
	 *     The PAUSE instruction improves the performance of IA-32
	 *     processors supporting Hyper-Threading Technology when
	 *     executing spin-wait loops and other routines where one
	 *     thread is accessing a shared lock or semaphore in a tight
	 *     polling loop. When executing a spin-wait loop, the
	 *     processor can suffer a severe performance penalty when
	 *     exiting the loop because it detects a possible memory order
	 *     violation and flushes the core processor's pipeline. The
	 *     PAUSE instruction provides a hint to the processor that the
	 *     code sequence is a spin-wait loop. The processor uses this
	 *     hint to avoid the memory order violation and prevent the
	 *     pipeline flush. In addition, the PAUSE instruction
	 *     de-pipelines the spin-wait loop to prevent it from
	 *     consuming execution resources excessively.
	 */
164
	__asm__ __volatile__(
165
		" rep; nop			\n");
166 167
}

168
#endif	 /* __i386__ || __x86_64__ */
169 170


171
#if defined(__ia64__) || defined(__ia64)
Bruce Momjian's avatar
Bruce Momjian committed
172
/* Intel Itanium */
173 174
#define HAS_TEST_AND_SET

175 176
typedef unsigned int slock_t;

177 178
#define TAS(lock) tas(lock)

179 180
#ifndef __INTEL_COMPILER

181
static __inline__ int
Bruce Momjian's avatar
Bruce Momjian committed
182
tas(volatile slock_t *lock)
183
{
Bruce Momjian's avatar
Bruce Momjian committed
184
	long int	ret;
185

186
	__asm__ __volatile__(
187
		"	xchg4 	%0=%1,%2	\n"
188 189
:		"=r"(ret), "+m"(*lock)
:		"r"(1)
190
:		"memory");
Bruce Momjian's avatar
Bruce Momjian committed
191
	return (int) ret;
192
}
Bruce Momjian's avatar
Bruce Momjian committed
193

194 195 196 197 198 199 200 201 202 203 204 205 206
#else

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

	ret = _InterlockedExchange(lock,1);	/* this is a xchg asm macro */

	return ret;
}

#endif
207
#endif	 /* __ia64__ || __ia64 */
208

209

210
#if defined(__arm__) || defined(__arm)
211 212
#define HAS_TEST_AND_SET

213 214
typedef unsigned char slock_t;

215 216 217 218 219
#define TAS(lock) tas(lock)

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

222
	__asm__ __volatile__(
223 224 225 226
		"	swpb 	%0, %0, [%2]	\n"
:		"+r"(_res), "+m"(*lock)
:		"r"(lock)
:		"memory");
Bruce Momjian's avatar
Bruce Momjian committed
227
	return (int) _res;
228 229
}

Bruce Momjian's avatar
Bruce Momjian committed
230
#endif	 /* __arm__ */
231

232

233 234
#if defined(__s390__) || defined(__s390x__)
/* S/390 and S/390x Linux (32- and 64-bit zSeries) */
235 236 237 238
#define HAS_TEST_AND_SET

typedef unsigned int slock_t;

239 240 241 242 243
#define TAS(lock)	   tas(lock)

static __inline__ int
tas(volatile slock_t *lock)
{
244
	int			_res = 0;
245 246

	__asm__	__volatile__(
247 248 249 250 251
		"	cs 	%0,%3,0(%2)		\n"
:		"+d"(_res), "+m"(*lock)
:		"a"(lock), "d"(1)
:		"memory", "cc");
	return _res;
252 253
}

254
#endif	 /* __s390__ || __s390x__ */
255

256

257
#if defined(__sparc__)
258
#define HAS_TEST_AND_SET
259 260 261

typedef unsigned char slock_t;

262 263 264 265 266
#define TAS(lock) tas(lock)

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

269
	__asm__ __volatile__(
270
		"	ldstub	[%2], %0	\n"
271 272 273
:		"=r"(_res), "+m"(*lock)
:		"r"(lock)
:		"memory");
274 275
	return (int) _res;
}
276

277
#endif	 /* __sparc__ */
278

279

280
#if defined(__ppc__) || defined(__powerpc__) || defined(__powerpc64__)
281 282 283
#define HAS_TEST_AND_SET

#if defined(__powerpc64__)
284
typedef unsigned long slock_t;
285 286
#else
typedef unsigned int slock_t;
287 288
#endif

289 290 291 292 293
#define TAS(lock) tas(lock)
/*
 * NOTE: per the Enhanced PowerPC Architecture manual, v1.0 dated 7-May-2002,
 * an isync is a sufficient synchronization barrier after a lwarx/stwcx loop.
 */
294 295 296 297 298 299 300
static __inline__ int
tas(volatile slock_t *lock)
{
	slock_t _t;
	int _res;

	__asm__ __volatile__(
301
"	lwarx   %0,0,%3		\n"
302
"	cmpwi   %0,0		\n"
303
"	bne     1f			\n"
304
"	addi    %0,%0,1		\n"
305
"	stwcx.  %0,0,%3		\n"
306
"	beq     2f         	\n"
307
"1:	li      %1,1		\n"
308 309 310
"	b		3f			\n"
"2:						\n"
"	isync				\n"
311
"	li      %1,0		\n"
312
"3:						\n"
313

314 315 316
:	"=&r"(_t), "=r"(_res), "+m"(*lock)
:	"r"(lock)
:	"memory", "cc");
317 318
	return _res;
}
319

320
/* PowerPC S_UNLOCK is almost standard but requires a "sync" instruction */
321 322 323 324 325 326 327
#define S_UNLOCK(lock)	\
do \
{\
	__asm__ __volatile__ ("	sync \n"); \
	*((volatile slock_t *) (lock)) = 0; \
} while (0)

328
#endif /* powerpc */
329

330

331
#if defined(__mc68000__) && defined(__linux__)
332
#define HAS_TEST_AND_SET
333 334 335

typedef unsigned char slock_t;

336 337 338 339 340 341
#define TAS(lock) tas(lock)

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

343
	__asm__	__volatile__(
344 345 346
		"	clrl	%0		\n"
		"	tas		%1		\n"
		"	sne		%0		\n"
347 348 349
:		"=d"(rv), "+m"(*lock)
:
:		"memory", "cc");
350 351 352
	return rv;
}

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

355

356
#if defined(__vax__)
357 358 359 360
/*
 * VAXen -- even multiprocessor ones
 * (thanks to Tom Ivar Helbekkmo)
 */
361
#define HAS_TEST_AND_SET
362 363 364

typedef unsigned char slock_t;

365 366 367 368 369
#define TAS(lock) tas(lock)

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

372
	__asm__ __volatile__(
373 374 375 376 377
		"	movl 	$1, %0			\n"
		"	bbssi	$0, (%2), 1f	\n"
		"	clrl	%0				\n"
		"1: \n"
:		"=&r"(_res), "+m"(*lock)
378
:		"r"(lock)
379
:		"memory");
380
	return _res;
381
}
382

383
#endif	 /* __vax__ */
384 385


386 387
#if defined(__ns32k__)
#define HAS_TEST_AND_SET
388 389 390

typedef unsigned char slock_t;

391 392 393 394 395
#define TAS(lock) tas(lock)

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

398
	__asm__ __volatile__(
399 400 401 402 403
		"	sbitb	0, %1	\n"
		"	sfsd	%0		\n"
:		"=r"(_res), "+m"(*lock)
:
:		"memory");
404
	return _res;
405 406
}

407
#endif	 /* __ns32k__ */
408

409

410
#if defined(__alpha) || defined(__alpha__)
411
/*
412 413 414 415
 * 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.
416
 */
417
#define HAS_TEST_AND_SET
418

419 420
typedef unsigned long slock_t;

421
#define TAS(lock)  tas(lock)
422 423 424 425

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

428
	__asm__	__volatile__(
429
		"	ldq		$0, %1	\n"
430
		"	bne		$0, 2f	\n"
431 432
		"	ldq_l	%0, %1	\n"
		"	bne		%0, 2f	\n"
433
		"	mov		1,  $0	\n"
434
		"	stq_c	$0, %1	\n"
435 436 437
		"	beq		$0, 2f	\n"
		"	mb				\n"
		"	br		3f		\n"
438
		"2:	mov		1, %0	\n"
439
		"3:					\n"
440
:		"=&r"(_res), "+m"(*lock)
Bruce Momjian's avatar
Bruce Momjian committed
441
:
442
:		"memory", "0");
443 444
	return (int) _res;
}
445

446 447 448 449 450 451 452
#define S_UNLOCK(lock)	\
do \
{\
	__asm__ __volatile__ ("	mb \n"); \
	*((volatile slock_t *) (lock)) = 0; \
} while (0)

453 454 455 456 457
#endif /* __alpha || __alpha__ */


/* These live in s_lock.c, but only for gcc */

458

459 460 461
#if defined(__m68k__)
#define HAS_TEST_AND_SET

462
typedef unsigned char slock_t;
463 464 465
#endif


466
#if defined(__mips__) && !defined(__sgi)
467 468
#define HAS_TEST_AND_SET

469
typedef unsigned int slock_t;
470 471
#endif

472

473 474 475 476 477
#endif	/* __GNUC__ */



/***************************************************************************
478
 * Platforms that use non-gcc inline assembly:
479 480
 */

481
#if !defined(HAS_TEST_AND_SET)	/* We didn't trigger above, let's try here */
482

483

484 485
#if defined(USE_UNIVEL_CC)
#define HAS_TEST_AND_SET
486 487 488

typedef unsigned char slock_t;

489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505
#define TAS(lock)	tas(lock)

asm int
tas(volatile slock_t *s_lock)
{
/* UNIVEL wants %mem in column 1, so we don't pg_indent this file */
%mem s_lock
	pushl %ebx
	movl s_lock, %ebx
	movl $255, %eax
	lock
	xchgb %al, (%ebx)
	popl %ebx
}

#endif	 /* defined(USE_UNIVEL_CC) */

506

507
#if defined(__alpha) || defined(__alpha__)
508 509 510 511 512 513 514 515
/*
 * 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.
 */
516 517 518
#define HAS_TEST_AND_SET

typedef unsigned long slock_t;
519 520 521

#include <alpha/builtins.h>
#define S_INIT_LOCK(lock)  (*(lock) = 0)
Bruce Momjian's avatar
Bruce Momjian committed
522 523
#define TAS(lock)		   (__LOCK_LONG_RETRY((lock), 1) == 0)
#define S_UNLOCK(lock)	   __UNLOCK_LONG(lock)
524

525
#endif	 /* __alpha || __alpha__ */
526 527


528
#if defined(__hppa) || defined(__hppa__)
529
/*
530
 * HP's PA-RISC
531
 *
532 533 534 535 536 537
 * See src/backend/port/hpux/tas.c.template for details about LDCWX.  Because
 * LDCWX requires a 16-byte-aligned address, we declare slock_t as a 16-byte
 * struct.  The active word in the struct is whichever has the aligned address;
 * the other three words just sit at -1.
 *
 * When using gcc, we can inline the required assembly code.
538
 */
539 540 541 542 543 544
#define HAS_TEST_AND_SET

typedef struct
{
	int			sema[4];
} slock_t;
545

546 547 548 549 550 551 552 553 554 555 556 557
#define TAS_ACTIVE_WORD(lock)	((volatile int *) (((long) (lock) + 15) & ~15))

#if defined(__GNUC__)

static __inline__ int
tas(volatile slock_t *lock)
{
	volatile int *lockword = TAS_ACTIVE_WORD(lock);
	register int lockval;

	__asm__ __volatile__(
		"	ldcwx	0(0,%2),%0	\n"
558 559 560
:		"=r"(lockval), "+m"(*lockword)
:		"r"(lockword)
:		"memory");
561 562 563 564 565 566 567 568
	return (lockval == 0);
}

#endif /* __GNUC__ */

#define S_UNLOCK(lock)	(*TAS_ACTIVE_WORD(lock) = -1)

#define S_INIT_LOCK(lock) \
Bruce Momjian's avatar
Bruce Momjian committed
569
	do { \
570
		volatile slock_t *lock_ = (lock); \
571 572 573 574
		lock_->sema[0] = -1; \
		lock_->sema[1] = -1; \
		lock_->sema[2] = -1; \
		lock_->sema[3] = -1; \
Bruce Momjian's avatar
Bruce Momjian committed
575
	} while (0)
576

577
#define S_LOCK_FREE(lock)	(*TAS_ACTIVE_WORD(lock) != 0)
578

579
#endif	 /* __hppa || __hppa__ */
580

581

582 583 584 585 586 587 588 589 590 591 592 593
#if defined(__hpux) && defined(__ia64) && !defined(__GNUC__)

#define HAS_TEST_AND_SET

typedef unsigned int slock_t;

#include <ia64/sys/inline.h>
#define TAS(lock) _Asm_xchg(_SZ_W, lock, 1, _LDHINT_NONE)

#endif	/* HPUX on IA64, non gcc */


594
#if defined(__QNX__) && defined(__WATCOMC__)
595
/*
596
 * QNX 4 using WATCOM C
597
 */
598 599 600 601
#define HAS_TEST_AND_SET

typedef unsigned char slock_t;

602 603 604 605 606 607 608 609 610
#define TAS(lock) wc_tas(lock)
extern slock_t wc_tas(volatile slock_t *lock);
#pragma aux wc_tas =\
		"	mov   al,1    " \
		" lock	xchg	al,[esi]" \
		parm [esi]        \
		value [al];

#endif	 /* __QNX__ and __WATCOMC__*/
611 612


613 614 615
#if defined(__sgi)
/*
 * SGI IRIX 5
616
 * slock_t is defined as a unsigned long. We use the standard SGI
Bruce Momjian's avatar
Bruce Momjian committed
617
 * mutex API.
618 619 620
 *
 * The following comment is left for historical reasons, but is probably
 * not a good idea since the mutex ABI is supported.
621 622 623 624 625
 *
 * 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.
 */
626
#define HAS_TEST_AND_SET
627 628 629

typedef unsigned long slock_t;

630
#include "mutex.h"
631 632 633 634
#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)
635
#endif	 /* __sgi */
636

637

638 639
#if defined(sinix)
/*
Bruce Momjian's avatar
Bruce Momjian committed
640
 * SINIX / Reliant UNIX
641 642 643
 * slock_t is defined as a struct abilock_t, which has a single unsigned long
 * member. (Basically same as SGI)
 */
644
#define HAS_TEST_AND_SET
645 646 647 648

#include "abi_mutex.h"
typedef abilock_t slock_t;

649 650 651 652 653
#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
654

655 656 657 658 659

#if defined(_AIX)
/*
 * AIX (POWER)
 */
660
#define HAS_TEST_AND_SET
661 662 663

typedef unsigned int slock_t;

664 665
#define TAS(lock)			_check_lock(lock, 0, 1)
#define S_UNLOCK(lock)		_clear_lock(lock, 0)
666
#endif	 /* _AIX */
667 668 669


#if defined (nextstep)
670
#define HAS_TEST_AND_SET
671

672 673
typedef struct mutex slock_t;

674 675 676 677 678
#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)
679
#endif	 /* nextstep */
680

681

682
/* These are in s_lock.c */
683

684

685 686
#if defined(sun3)
#define HAS_TEST_AND_SET
687 688

typedef unsigned char slock_t;
689 690
#endif

691

692 693
#if defined(__sparc__) || defined(__sparc)
#define HAS_TEST_AND_SET
694

695 696 697 698
typedef unsigned char slock_t;
#endif


699
/* out-of-line assembler from src/backend/port/tas/foo.s */
700 701 702 703 704 705 706 707 708 709 710

#if defined(__sun) && defined(__i386)
/*
 * Solaris/386 (we only get here for non-gcc case)
 */
#define HAS_TEST_AND_SET

typedef unsigned char slock_t;
#endif


711
#endif	/* !defined(HAS_TEST_AND_SET) */
712

713

714
/* Blow up if we didn't have any way to do spinlocks */
715
#ifndef HAS_TEST_AND_SET
716
#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.
717 718 719 720
#endif


#else	/* !HAVE_SPINLOCKS */
721

722 723

/*
724
 * Fake spinlock implementation using semaphores --- slow and prone
725
 * to fall foul of kernel limits on number of semaphores, so don't use this
726
 * unless you must!  The subroutines appear in spin.c.
727
 */
728
typedef PGSemaphoreData slock_t;
729 730 731 732

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
733
extern int	tas_sema(volatile slock_t *lock);
734

Bruce Momjian's avatar
Bruce Momjian committed
735 736 737 738
#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)
739 740


741
#endif	/* HAVE_SPINLOCKS */
742

743

744
/*
745 746
 * Default Definitions - override these above as needed.
 */
747

748
#if !defined(S_LOCK)
749
#define S_LOCK(lock) \
750
	do { \
751 752
		if (TAS(lock)) \
			s_lock((lock), __FILE__, __LINE__); \
753
	} while (0)
754
#endif	 /* S_LOCK */
755

756
#if !defined(S_LOCK_FREE)
757
#define S_LOCK_FREE(lock)	(*(lock) == 0)
758
#endif	 /* S_LOCK_FREE */
759

760
#if !defined(S_UNLOCK)
761
#define S_UNLOCK(lock)		(*((volatile slock_t *) (lock)) = 0)
762
#endif	 /* S_UNLOCK */
763

764
#if !defined(S_INIT_LOCK)
765
#define S_INIT_LOCK(lock)	S_UNLOCK(lock)
766
#endif	 /* S_INIT_LOCK */
767

768 769 770 771
#if !defined(SPIN_DELAY)
#define SPIN_DELAY()	((void) 0)
#endif	 /* SPIN_DELAY */

772
#if !defined(TAS)
773
extern int	tas(volatile slock_t *lock);		/* in port/.../tas.s, or
774
												 * s_lock.c */
Bruce Momjian's avatar
Bruce Momjian committed
775

776
#define TAS(lock)		tas(lock)
777
#endif	 /* TAS */
778

779

780
/*
781 782
 * Platform-independent out-of-line support routines
 */
783
extern void s_lock(volatile slock_t *lock, const char *file, int line);
784

785
#endif	 /* S_LOCK_H */