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
|
|
|
*
|
2003-08-04 04:40:20 +02:00
|
|
|
* Portions Copyright (c) 1996-2003, 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
|
2003-12-23 04:31:30 +01:00
|
|
|
* $PostgreSQL: pgsql/src/backend/storage/lmgr/spin.c,v 1.13 2003/12/23 03:31:30 momjian Exp $
|
1996-07-09 08:22:35 +02:00
|
|
|
*
|
|
|
|
*-------------------------------------------------------------------------
|
|
|
|
*/
|
|
|
|
#include "postgres.h"
|
|
|
|
|
2001-09-29 06:02:27 +02:00
|
|
|
#include "storage/lwlock.h"
|
2002-05-05 02:03:29 +02:00
|
|
|
#include "storage/pg_sema.h"
|
2001-09-29 06:02:27 +02:00
|
|
|
#include "storage/spin.h"
|
2003-12-23 04:31:30 +01:00
|
|
|
#include "storage/s_lock.h"
|
1996-07-09 08:22:35 +02:00
|
|
|
|
2000-05-31 02:28:42 +02:00
|
|
|
|
2000-11-29 00:27:57 +01:00
|
|
|
#ifdef HAS_TEST_AND_SET
|
|
|
|
|
|
|
|
/*
|
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
|
|
|
}
|
|
|
|
|
2001-03-22 05:01:46 +01:00
|
|
|
#else /* !HAS_TEST_AND_SET */
|
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
|
|
|
{
|
|
|
|
/*
|
2002-09-04 22:31:48 +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
|
|
|
*
|
2002-09-04 22:31:48 +02:00
|
|
|
* For now, though, we just need a few spinlocks (10 should be plenty)
|
|
|
|
* plus one for each LWLock.
|
1997-09-18 16:21:02 +02:00
|
|
|
*/
|
2002-05-05 02:03:29 +02:00
|
|
|
return NumLWLocks() + 10;
|
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
|
|
|
|
2001-11-05 18:46:40 +01:00
|
|
|
#endif /* !HAS_TEST_AND_SET */
|