diff options
Diffstat (limited to 'src/backend/storage/ipc/spin.c')
-rw-r--r-- | src/backend/storage/ipc/spin.c | 246 |
1 files changed, 129 insertions, 117 deletions
diff --git a/src/backend/storage/ipc/spin.c b/src/backend/storage/ipc/spin.c index b50d5d9500f..e93d5894a58 100644 --- a/src/backend/storage/ipc/spin.c +++ b/src/backend/storage/ipc/spin.c @@ -1,13 +1,13 @@ /*------------------------------------------------------------------------- * * spin.c-- - * routines for managing spin locks + * routines for managing spin locks * * Copyright (c) 1994, Regents of the University of California * * * IDENTIFICATION - * $Header: /cvsroot/pgsql/src/backend/storage/ipc/Attic/spin.c,v 1.6 1997/08/21 13:43:46 momjian Exp $ + * $Header: /cvsroot/pgsql/src/backend/storage/ipc/Attic/spin.c,v 1.7 1997/09/07 04:48:45 momjian Exp $ * *------------------------------------------------------------------------- */ @@ -21,8 +21,8 @@ * term semaphores separately anyway. * * NOTE: These routines are not supposed to be widely used in Postgres. - * They are preserved solely for the purpose of porting Mark Sullivan's - * buffer manager to Postgres. + * They are preserved solely for the purpose of porting Mark Sullivan's + * buffer manager to Postgres. */ #include <errno.h> #include "postgres.h" @@ -43,61 +43,62 @@ IpcSemaphoreId SpinLockId; bool CreateSpinlocks(IPCKey key) -{ - /* the spin lock shared memory must have been created by now */ - return(TRUE); +{ + /* the spin lock shared memory must have been created by now */ + return (TRUE); } bool InitSpinLocks(int init, IPCKey key) { - extern SPINLOCK ShmemLock; - extern SPINLOCK BindingLock; - extern SPINLOCK BufMgrLock; - extern SPINLOCK LockMgrLock; - extern SPINLOCK ProcStructLock; - extern SPINLOCK SInvalLock; - extern SPINLOCK OidGenLockId; - + extern SPINLOCK ShmemLock; + extern SPINLOCK BindingLock; + extern SPINLOCK BufMgrLock; + extern SPINLOCK LockMgrLock; + extern SPINLOCK ProcStructLock; + extern SPINLOCK SInvalLock; + extern SPINLOCK OidGenLockId; + #ifdef MAIN_MEMORY - extern SPINLOCK MMCacheLock; -#endif /* SONY_JUKEBOX */ - - /* These six spinlocks have fixed location is shmem */ - ShmemLock = (SPINLOCK) SHMEMLOCKID; - BindingLock = (SPINLOCK) BINDINGLOCKID; - BufMgrLock = (SPINLOCK) BUFMGRLOCKID; - LockMgrLock = (SPINLOCK) LOCKMGRLOCKID; - ProcStructLock = (SPINLOCK) PROCSTRUCTLOCKID; - SInvalLock = (SPINLOCK) SINVALLOCKID; - OidGenLockId = (SPINLOCK) OIDGENLOCKID; - + extern SPINLOCK MMCacheLock; + +#endif /* SONY_JUKEBOX */ + + /* These six spinlocks have fixed location is shmem */ + ShmemLock = (SPINLOCK) SHMEMLOCKID; + BindingLock = (SPINLOCK) BINDINGLOCKID; + BufMgrLock = (SPINLOCK) BUFMGRLOCKID; + LockMgrLock = (SPINLOCK) LOCKMGRLOCKID; + ProcStructLock = (SPINLOCK) PROCSTRUCTLOCKID; + SInvalLock = (SPINLOCK) SINVALLOCKID; + OidGenLockId = (SPINLOCK) OIDGENLOCKID; + #ifdef MAIN_MEMORY - MMCacheLock = (SPINLOCK) MMCACHELOCKID; -#endif /* MAIN_MEMORY */ - - return(TRUE); + MMCacheLock = (SPINLOCK) MMCACHELOCKID; +#endif /* MAIN_MEMORY */ + + return (TRUE); } void SpinAcquire(SPINLOCK lock) { - ExclusiveLock(lock); - PROC_INCR_SLOCK(lock); + ExclusiveLock(lock); + PROC_INCR_SLOCK(lock); } void SpinRelease(SPINLOCK lock) { - PROC_DECR_SLOCK(lock); - ExclusiveUnlock(lock); + PROC_DECR_SLOCK(lock); + ExclusiveUnlock(lock); } -#else /* HAS_TEST_AND_SET */ +#else /* HAS_TEST_AND_SET */ /* Spinlocks are implemented using SysV semaphores */ -static bool AttachSpinLocks(IPCKey key); -static bool SpinIsLocked(SPINLOCK lock); +static bool AttachSpinLocks(IPCKey key); +static bool SpinIsLocked(SPINLOCK lock); /* * SpinAcquire -- try to grab a spinlock @@ -107,86 +108,91 @@ static bool SpinIsLocked(SPINLOCK lock); void SpinAcquire(SPINLOCK lock) { - IpcSemaphoreLock(SpinLockId, lock, IpcExclusiveLock); - PROC_INCR_SLOCK(lock); + IpcSemaphoreLock(SpinLockId, lock, IpcExclusiveLock); + PROC_INCR_SLOCK(lock); } /* * SpinRelease -- release a spin lock - * + * * FAILS if the semaphore is corrupted */ void SpinRelease(SPINLOCK lock) { - Assert(SpinIsLocked(lock)) + Assert(SpinIsLocked(lock)) PROC_DECR_SLOCK(lock); - IpcSemaphoreUnlock(SpinLockId, lock, IpcExclusiveLock); + IpcSemaphoreUnlock(SpinLockId, lock, IpcExclusiveLock); } -static bool +static bool SpinIsLocked(SPINLOCK lock) { - int semval; - - semval = IpcSemaphoreGetValue(SpinLockId, lock); - return(semval < IpcSemaphoreDefaultStartValue); + int semval; + + semval = IpcSemaphoreGetValue(SpinLockId, lock); + return (semval < IpcSemaphoreDefaultStartValue); } /* * CreateSpinlocks -- Create a sysV semaphore array for - * the spinlocks + * the spinlocks * */ bool CreateSpinlocks(IPCKey key) { - - int status; - IpcSemaphoreId semid; - semid = IpcSemaphoreCreate(key, MAX_SPINS, IPCProtection, - IpcSemaphoreDefaultStartValue, 1, &status); - if (status == IpcSemIdExist) { - IpcSemaphoreKill(key); - elog(NOTICE,"Destroying old spinlock semaphore"); - semid = IpcSemaphoreCreate(key, MAX_SPINS, IPCProtection, - IpcSemaphoreDefaultStartValue, 1, &status); - } - - if (semid >= 0) { - SpinLockId = semid; - return(TRUE); - } - /* cannot create spinlocks */ - elog(FATAL,"CreateSpinlocks: cannot create spin locks"); - return(FALSE); + + int status; + IpcSemaphoreId semid; + + semid = IpcSemaphoreCreate(key, MAX_SPINS, IPCProtection, + IpcSemaphoreDefaultStartValue, 1, &status); + if (status == IpcSemIdExist) + { + IpcSemaphoreKill(key); + elog(NOTICE, "Destroying old spinlock semaphore"); + semid = IpcSemaphoreCreate(key, MAX_SPINS, IPCProtection, + IpcSemaphoreDefaultStartValue, 1, &status); + } + + if (semid >= 0) + { + SpinLockId = semid; + return (TRUE); + } + /* cannot create spinlocks */ + elog(FATAL, "CreateSpinlocks: cannot create spin locks"); + return (FALSE); } /* * Attach to existing spinlock set */ -static bool +static bool AttachSpinLocks(IPCKey key) { - IpcSemaphoreId id; - - id = semget (key, MAX_SPINS, 0); - if (id < 0) { - if (errno == EEXIST) { - /* key is the name of someone else's semaphore */ - elog (FATAL,"AttachSpinlocks: SPIN_KEY belongs to someone else"); + IpcSemaphoreId id; + + id = semget(key, MAX_SPINS, 0); + if (id < 0) + { + if (errno == EEXIST) + { + /* key is the name of someone else's semaphore */ + elog(FATAL, "AttachSpinlocks: SPIN_KEY belongs to someone else"); + } + /* cannot create spinlocks */ + elog(FATAL, "AttachSpinlocks: cannot create spin locks"); + return (FALSE); } - /* cannot create spinlocks */ - elog(FATAL,"AttachSpinlocks: cannot create spin locks"); - return(FALSE); - } - SpinLockId = id; - return(TRUE); + SpinLockId = id; + return (TRUE); } /* * InitSpinLocks -- Spinlock bootstrapping - * + * * We need several spinlocks for bootstrapping: * BindingLock (for the shmem binding table) and * ShmemLock (for the shmem allocator), BufMgrLock (for buffer @@ -199,41 +205,47 @@ AttachSpinLocks(IPCKey key) bool InitSpinLocks(int init, IPCKey key) { - extern SPINLOCK ShmemLock; - extern SPINLOCK BindingLock; - extern SPINLOCK BufMgrLock; - extern SPINLOCK LockMgrLock; - extern SPINLOCK ProcStructLock; - extern SPINLOCK SInvalLock; - extern SPINLOCK OidGenLockId; - + extern SPINLOCK ShmemLock; + extern SPINLOCK BindingLock; + extern SPINLOCK BufMgrLock; + extern SPINLOCK LockMgrLock; + extern SPINLOCK ProcStructLock; + extern SPINLOCK SInvalLock; + extern SPINLOCK OidGenLockId; + #ifdef MAIN_MEMORY - extern SPINLOCK MMCacheLock; -#endif /* MAIN_MEMORY */ - - if (!init || key != IPC_PRIVATE) { - /* if bootstrap and key is IPC_PRIVATE, it means that we are running - * backend by itself. no need to attach spinlocks - */ - if (! AttachSpinLocks(key)) { - elog(FATAL,"InitSpinLocks: couldnt attach spin locks"); - return(FALSE); + extern SPINLOCK MMCacheLock; + +#endif /* MAIN_MEMORY */ + + if (!init || key != IPC_PRIVATE) + { + + /* + * if bootstrap and key is IPC_PRIVATE, it means that we are + * running backend by itself. no need to attach spinlocks + */ + if (!AttachSpinLocks(key)) + { + elog(FATAL, "InitSpinLocks: couldnt attach spin locks"); + return (FALSE); + } } - } - - /* These five (or six) spinlocks have fixed location is shmem */ - ShmemLock = (SPINLOCK) SHMEMLOCKID; - BindingLock = (SPINLOCK) BINDINGLOCKID; - BufMgrLock = (SPINLOCK) BUFMGRLOCKID; - LockMgrLock = (SPINLOCK) LOCKMGRLOCKID; - ProcStructLock = (SPINLOCK) PROCSTRUCTLOCKID; - SInvalLock = (SPINLOCK) SINVALLOCKID; - OidGenLockId = (SPINLOCK) OIDGENLOCKID; - + + /* These five (or six) spinlocks have fixed location is shmem */ + ShmemLock = (SPINLOCK) SHMEMLOCKID; + BindingLock = (SPINLOCK) BINDINGLOCKID; + BufMgrLock = (SPINLOCK) BUFMGRLOCKID; + LockMgrLock = (SPINLOCK) LOCKMGRLOCKID; + ProcStructLock = (SPINLOCK) PROCSTRUCTLOCKID; + SInvalLock = (SPINLOCK) SINVALLOCKID; + OidGenLockId = (SPINLOCK) OIDGENLOCKID; + #ifdef MAIN_MEMORY - MMCacheLock = (SPINLOCK) MMCACHELOCKID; -#endif /* MAIN_MEMORY */ - - return(TRUE); + MMCacheLock = (SPINLOCK) MMCACHELOCKID; +#endif /* MAIN_MEMORY */ + + return (TRUE); } -#endif /* HAS_TEST_AND_SET */ + +#endif /* HAS_TEST_AND_SET */ |