1996-07-09 08:22:35 +02:00
|
|
|
/*-------------------------------------------------------------------------
|
|
|
|
*
|
1999-02-14 00:22:53 +01:00
|
|
|
* spin.c
|
2001-09-29 06:02:27 +02:00
|
|
|
* Hardware-independent implementation of spinlocks.
|
|
|
|
*
|
|
|
|
*
|
|
|
|
* For machines that have test-and-set (TAS) instructions, s_lock.h/.c
|
2001-10-25 07:50:21 +02:00
|
|
|
* define the spinlock implementation. This file contains only a stub
|
2002-05-05 02:03:29 +02:00
|
|
|
* implementation for spinlocks using PGSemaphores. Unless semaphores
|
|
|
|
* are implemented in a way that doesn't involve a kernel call, this
|
2001-09-29 06:02:27 +02:00
|
|
|
* is too slow to be very useful :-(
|
1996-07-09 08:22:35 +02:00
|
|
|
*
|
2000-11-29 00:27:57 +01:00
|
|
|
*
|
2014-01-07 22:05:30 +01:00
|
|
|
* Portions Copyright (c) 1996-2014, PostgreSQL Global Development Group
|
2000-01-26 06:58:53 +01:00
|
|
|
* Portions Copyright (c) 1994, Regents of the University of California
|
1996-07-09 08:22:35 +02:00
|
|
|
*
|
|
|
|
*
|
|
|
|
* IDENTIFICATION
|
2010-09-20 22:08:53 +02:00
|
|
|
* src/backend/storage/lmgr/spin.c
|
1996-07-09 08:22:35 +02:00
|
|
|
*
|
|
|
|
*-------------------------------------------------------------------------
|
|
|
|
*/
|
|
|
|
#include "postgres.h"
|
|
|
|
|
2013-07-08 10:23:56 +02:00
|
|
|
#include "access/xlog.h"
|
2006-07-22 23:04:40 +02:00
|
|
|
#include "miscadmin.h"
|
2013-04-04 15:31:44 +02:00
|
|
|
#include "replication/walsender.h"
|
2006-07-22 23:04:40 +02:00
|
|
|
#include "storage/lwlock.h"
|
2001-09-29 06:02:27 +02:00
|
|
|
#include "storage/spin.h"
|
1996-07-09 08:22:35 +02:00
|
|
|
|
2000-05-31 02:28:42 +02:00
|
|
|
|
2003-12-23 19:13:17 +01:00
|
|
|
#ifdef HAVE_SPINLOCKS
|
2000-11-29 00:27:57 +01:00
|
|
|
|
|
|
|
/*
|
2002-05-05 02:03:29 +02:00
|
|
|
* Report number of semaphores needed to support spinlocks.
|
2000-11-29 00:27:57 +01:00
|
|
|
*/
|
2002-05-05 02:03:29 +02:00
|
|
|
int
|
|
|
|
SpinlockSemas(void)
|
1996-07-09 08:22:35 +02:00
|
|
|
{
|
2002-05-05 02:03:29 +02:00
|
|
|
return 0;
|
2000-11-29 00:27:57 +01:00
|
|
|
}
|
2003-12-23 19:13:17 +01:00
|
|
|
#else /* !HAVE_SPINLOCKS */
|
2000-11-29 00:27:57 +01:00
|
|
|
|
|
|
|
/*
|
2002-05-05 02:03:29 +02:00
|
|
|
* No TAS, so spinlocks are implemented as PGSemaphores.
|
2000-11-29 00:27:57 +01:00
|
|
|
*/
|
|
|
|
|
|
|
|
|
|
|
|
/*
|
2002-05-05 02:03:29 +02:00
|
|
|
* Report number of semaphores needed to support spinlocks.
|
2000-11-29 00:27:57 +01:00
|
|
|
*/
|
2002-05-05 02:03:29 +02:00
|
|
|
int
|
|
|
|
SpinlockSemas(void)
|
2000-11-29 00:27:57 +01:00
|
|
|
{
|
2013-05-29 22:58:43 +02:00
|
|
|
int nsemas;
|
2013-04-04 15:31:44 +02:00
|
|
|
|
2000-11-29 00:27:57 +01:00
|
|
|
/*
|
2005-10-15 04:49:52 +02:00
|
|
|
* It would be cleaner to distribute this logic into the affected modules,
|
|
|
|
* similar to the way shmem space estimation is handled.
|
2002-05-05 02:03:29 +02:00
|
|
|
*
|
2013-04-04 15:31:44 +02:00
|
|
|
* For now, though, there are few enough users of spinlocks that we just
|
|
|
|
* keep the knowledge here.
|
1997-09-18 16:21:02 +02:00
|
|
|
*/
|
2013-04-04 15:31:44 +02:00
|
|
|
nsemas = NumLWLocks(); /* one for each lwlock */
|
|
|
|
nsemas += NBuffers; /* one for each buffer header */
|
|
|
|
nsemas += max_wal_senders; /* one for each wal sender process */
|
2013-07-08 10:23:56 +02:00
|
|
|
nsemas += num_xloginsert_slots; /* one for each WAL insertion slot */
|
2013-04-04 15:31:44 +02:00
|
|
|
nsemas += 30; /* plus a bunch for other small-scale use */
|
|
|
|
|
|
|
|
return nsemas;
|
2000-11-29 00:27:57 +01:00
|
|
|
}
|
1997-08-19 23:40:56 +02:00
|
|
|
|
1996-07-09 08:22:35 +02:00
|
|
|
/*
|
2000-11-29 00:27:57 +01:00
|
|
|
* s_lock.h hardware-spinlock emulation
|
1996-07-09 08:22:35 +02:00
|
|
|
*/
|
2000-11-29 00:27:57 +01:00
|
|
|
|
1999-10-06 23:58:18 +02:00
|
|
|
void
|
2000-11-29 00:27:57 +01:00
|
|
|
s_init_lock_sema(volatile slock_t *lock)
|
1996-07-09 08:22:35 +02:00
|
|
|
{
|
2002-05-05 02:03:29 +02:00
|
|
|
PGSemaphoreCreate((PGSemaphore) lock);
|
1996-07-09 08:22:35 +02:00
|
|
|
}
|
|
|
|
|
1999-10-06 23:58:18 +02:00
|
|
|
void
|
2000-11-29 00:27:57 +01:00
|
|
|
s_unlock_sema(volatile slock_t *lock)
|
1996-07-09 08:22:35 +02:00
|
|
|
{
|
2002-05-05 02:03:29 +02:00
|
|
|
PGSemaphoreUnlock((PGSemaphore) lock);
|
2000-11-29 00:27:57 +01:00
|
|
|
}
|
1997-09-07 07:04:48 +02:00
|
|
|
|
2000-11-29 00:27:57 +01:00
|
|
|
bool
|
|
|
|
s_lock_free_sema(volatile slock_t *lock)
|
|
|
|
{
|
2002-05-05 02:03:29 +02:00
|
|
|
/* We don't currently use S_LOCK_FREE anyway */
|
|
|
|
elog(ERROR, "spin.c does not support S_LOCK_FREE()");
|
|
|
|
return false;
|
2000-11-29 00:27:57 +01:00
|
|
|
}
|
1997-09-07 07:04:48 +02:00
|
|
|
|
2000-11-29 00:27:57 +01:00
|
|
|
int
|
|
|
|
tas_sema(volatile slock_t *lock)
|
|
|
|
{
|
|
|
|
/* Note that TAS macros return 0 if *success* */
|
2002-05-05 02:03:29 +02:00
|
|
|
return !PGSemaphoreTryLock((PGSemaphore) lock);
|
1996-07-09 08:22:35 +02:00
|
|
|
}
|
2001-10-28 07:26:15 +01:00
|
|
|
|
2003-12-23 19:13:17 +01:00
|
|
|
#endif /* !HAVE_SPINLOCKS */
|