PostgreSQL Source Code  git master
 All Data Structures Namespaces Files Functions Variables Typedefs Enumerations Enumerator Macros
lwlock.h File Reference
#include "storage/proclist_types.h"
#include "storage/s_lock.h"
#include "port/atomics.h"
#include "storage/lwlocknames.h"
Include dependency graph for lwlock.h:
This graph shows which files directly or indirectly include this file:

Go to the source code of this file.

Data Structures

struct  LWLock
 
union  LWLockPadded
 
union  LWLockMinimallyPadded
 
struct  NamedLWLockTranche
 

Macros

#define LWLOCK_PADDED_SIZE   PG_CACHE_LINE_SIZE
 
#define LWLOCK_MINIMAL_SIZE   (sizeof(LWLock) <= 32 ? 32 : 64)
 
#define NUM_BUFFER_PARTITIONS   128
 
#define LOG2_NUM_LOCK_PARTITIONS   4
 
#define NUM_LOCK_PARTITIONS   (1 << LOG2_NUM_LOCK_PARTITIONS)
 
#define LOG2_NUM_PREDICATELOCK_PARTITIONS   4
 
#define NUM_PREDICATELOCK_PARTITIONS   (1 << LOG2_NUM_PREDICATELOCK_PARTITIONS)
 
#define BUFFER_MAPPING_LWLOCK_OFFSET   NUM_INDIVIDUAL_LWLOCKS
 
#define LOCK_MANAGER_LWLOCK_OFFSET   (BUFFER_MAPPING_LWLOCK_OFFSET + NUM_BUFFER_PARTITIONS)
 
#define PREDICATELOCK_MANAGER_LWLOCK_OFFSET   (LOCK_MANAGER_LWLOCK_OFFSET + NUM_LOCK_PARTITIONS)
 
#define NUM_FIXED_LWLOCKS   (PREDICATELOCK_MANAGER_LWLOCK_OFFSET + NUM_PREDICATELOCK_PARTITIONS)
 

Typedefs

typedef struct LWLock LWLock
 
typedef union LWLockPadded LWLockPadded
 
typedef union LWLockMinimallyPadded LWLockMinimallyPadded
 
typedef struct NamedLWLockTranche NamedLWLockTranche
 
typedef enum LWLockMode LWLockMode
 
typedef enum BuiltinTrancheIds BuiltinTrancheIds
 
typedef LWLockLWLockId
 

Enumerations

enum  LWLockMode { LW_EXCLUSIVE, LW_SHARED, LW_WAIT_UNTIL_FREE }
 
enum  BuiltinTrancheIds {
  LWTRANCHE_CLOG_BUFFERS = NUM_INDIVIDUAL_LWLOCKS, LWTRANCHE_COMMITTS_BUFFERS, LWTRANCHE_SUBTRANS_BUFFERS, LWTRANCHE_MXACTOFFSET_BUFFERS,
  LWTRANCHE_MXACTMEMBER_BUFFERS, LWTRANCHE_ASYNC_BUFFERS, LWTRANCHE_OLDSERXID_BUFFERS, LWTRANCHE_WAL_INSERT,
  LWTRANCHE_BUFFER_CONTENT, LWTRANCHE_BUFFER_IO_IN_PROGRESS, LWTRANCHE_REPLICATION_ORIGIN, LWTRANCHE_REPLICATION_SLOT_IO_IN_PROGRESS,
  LWTRANCHE_PROC, LWTRANCHE_BUFFER_MAPPING, LWTRANCHE_LOCK_MANAGER, LWTRANCHE_PREDICATE_LOCK_MANAGER,
  LWTRANCHE_PARALLEL_QUERY_DSA, LWTRANCHE_FIRST_USER_DEFINED
}
 

Functions

bool LWLockAcquire (LWLock *lock, LWLockMode mode)
 
bool LWLockConditionalAcquire (LWLock *lock, LWLockMode mode)
 
bool LWLockAcquireOrWait (LWLock *lock, LWLockMode mode)
 
void LWLockRelease (LWLock *lock)
 
void LWLockReleaseClearVar (LWLock *lock, uint64 *valptr, uint64 val)
 
void LWLockReleaseAll (void)
 
bool LWLockHeldByMe (LWLock *lock)
 
bool LWLockHeldByMeInMode (LWLock *lock, LWLockMode mode)
 
bool LWLockWaitForVar (LWLock *lock, uint64 *valptr, uint64 oldval, uint64 *newval)
 
void LWLockUpdateVar (LWLock *lock, uint64 *valptr, uint64 value)
 
Size LWLockShmemSize (void)
 
void CreateLWLocks (void)
 
void InitLWLockAccess (void)
 
const char * GetLWLockIdentifier (uint32 classId, uint16 eventId)
 
void RequestNamedLWLockTranche (const char *tranche_name, int num_lwlocks)
 
LWLockPaddedGetNamedLWLockTranche (const char *tranche_name)
 
int LWLockNewTrancheId (void)
 
void LWLockRegisterTranche (int tranche_id, char *tranche_name)
 
void LWLockInitialize (LWLock *lock, int tranche_id)
 

Variables

PGDLLIMPORT LWLockPaddedMainLWLockArray
 
char * MainLWLockNames []
 
PGDLLIMPORT NamedLWLockTrancheNamedLWLockTrancheArray
 
PGDLLIMPORT int NamedLWLockTrancheRequests
 

Macro Definition Documentation

#define BUFFER_MAPPING_LWLOCK_OFFSET   NUM_INDIVIDUAL_LWLOCKS

Definition at line 124 of file lwlock.h.

#define LOCK_MANAGER_LWLOCK_OFFSET   (BUFFER_MAPPING_LWLOCK_OFFSET + NUM_BUFFER_PARTITIONS)

Definition at line 125 of file lwlock.h.

#define LOG2_NUM_LOCK_PARTITIONS   4

Definition at line 116 of file lwlock.h.

Referenced by proclock_hash(), and ProcLockHashCode().

#define LOG2_NUM_PREDICATELOCK_PARTITIONS   4

Definition at line 120 of file lwlock.h.

#define LWLOCK_MINIMAL_SIZE   (sizeof(LWLock) <= 32 ? 32 : 64)

Definition at line 74 of file lwlock.h.

Referenced by CreateLWLocks().

#define LWLOCK_PADDED_SIZE   PG_CACHE_LINE_SIZE

Definition at line 73 of file lwlock.h.

Referenced by CreateLWLocks(), and LWLockShmemSize().

#define NUM_BUFFER_PARTITIONS   128

Definition at line 113 of file lwlock.h.

Referenced by InitBufTable(), InitializeLWLocks(), StrategyInitialize(), and StrategyShmemSize().

Definition at line 129 of file lwlock.h.

Referenced by GetNamedLWLockTranche(), InitializeLWLocks(), and LWLockShmemSize().

#define PREDICATELOCK_MANAGER_LWLOCK_OFFSET   (LOCK_MANAGER_LWLOCK_OFFSET + NUM_LOCK_PARTITIONS)

Definition at line 127 of file lwlock.h.

Typedef Documentation

Definition at line 223 of file lwlock.h.

Enumeration Type Documentation

Enumerator
LWTRANCHE_CLOG_BUFFERS 
LWTRANCHE_COMMITTS_BUFFERS 
LWTRANCHE_SUBTRANS_BUFFERS 
LWTRANCHE_MXACTOFFSET_BUFFERS 
LWTRANCHE_MXACTMEMBER_BUFFERS 
LWTRANCHE_ASYNC_BUFFERS 
LWTRANCHE_OLDSERXID_BUFFERS 
LWTRANCHE_WAL_INSERT 
LWTRANCHE_BUFFER_CONTENT 
LWTRANCHE_BUFFER_IO_IN_PROGRESS 
LWTRANCHE_REPLICATION_ORIGIN 
LWTRANCHE_REPLICATION_SLOT_IO_IN_PROGRESS 
LWTRANCHE_PROC 
LWTRANCHE_BUFFER_MAPPING 
LWTRANCHE_LOCK_MANAGER 
LWTRANCHE_PREDICATE_LOCK_MANAGER 
LWTRANCHE_PARALLEL_QUERY_DSA 
LWTRANCHE_FIRST_USER_DEFINED 

Definition at line 196 of file lwlock.h.

197 {
198  LWTRANCHE_CLOG_BUFFERS = NUM_INDIVIDUAL_LWLOCKS,
BuiltinTrancheIds
Definition: lwlock.h:196
enum LWLockMode
Enumerator
LW_EXCLUSIVE 
LW_SHARED 
LW_WAIT_UNTIL_FREE 

Definition at line 132 of file lwlock.h.

133 {
134  LW_EXCLUSIVE,
135  LW_SHARED,
136  LW_WAIT_UNTIL_FREE /* A special mode used in PGPROC->lwlockMode,
137  * when waiting for lock to become free. Not
138  * to be used as LWLockAcquire argument */
139 } LWLockMode;
LWLockMode
Definition: lwlock.h:132

Function Documentation

void CreateLWLocks ( void  )

Definition at line 381 of file lwlock.c.

References InitializeLWLocks(), IsUnderPostmaster, LW_VAL_EXCLUSIVE, LWLOCK_MINIMAL_SIZE, LWLOCK_PADDED_SIZE, LWLockShmemSize(), LWTRANCHE_FIRST_USER_DEFINED, MAX_BACKENDS, RegisterLWLockTranches(), ShmemAlloc(), and StaticAssertExpr.

Referenced by CreateSharedMemoryAndSemaphores().

382 {
384  "MAX_BACKENDS too big for lwlock.c");
385 
387  sizeof(LWLock) <= LWLOCK_PADDED_SIZE,
388  "Miscalculated LWLock padding");
389 
390  if (!IsUnderPostmaster)
391  {
392  Size spaceLocks = LWLockShmemSize();
393  int *LWLockCounter;
394  char *ptr;
395 
396  /* Allocate space */
397  ptr = (char *) ShmemAlloc(spaceLocks);
398 
399  /* Leave room for dynamic allocation of tranches */
400  ptr += sizeof(int);
401 
402  /* Ensure desired alignment of LWLock array */
403  ptr += LWLOCK_PADDED_SIZE - ((uintptr_t) ptr) % LWLOCK_PADDED_SIZE;
404 
405  MainLWLockArray = (LWLockPadded *) ptr;
406 
407  /*
408  * Initialize the dynamic-allocation counter for tranches, which is
409  * stored just before the first LWLock.
410  */
411  LWLockCounter = (int *) ((char *) MainLWLockArray - sizeof(int));
412  *LWLockCounter = LWTRANCHE_FIRST_USER_DEFINED;
413 
414  /* Initialize all LWLocks */
416  }
417 
418  /* Register all LWLock tranches */
420 }
Definition: lwlock.h:32
void * ShmemAlloc(Size size)
Definition: shmem.c:157
Size LWLockShmemSize(void)
Definition: lwlock.c:349
static void RegisterLWLockTranches(void)
Definition: lwlock.c:491
#define LWLOCK_MINIMAL_SIZE
Definition: lwlock.h:74
#define MAX_BACKENDS
Definition: postmaster.h:75
bool IsUnderPostmaster
Definition: globals.c:100
unsigned int uint32
Definition: c.h:265
static void InitializeLWLocks(void)
Definition: lwlock.c:426
#define StaticAssertExpr(condition, errmessage)
Definition: c.h:755
size_t Size
Definition: c.h:353
#define LW_VAL_EXCLUSIVE
Definition: lwlock.c:103
#define LWLOCK_PADDED_SIZE
Definition: lwlock.h:73
LWLockPadded * MainLWLockArray
Definition: lwlock.c:125
const char* GetLWLockIdentifier ( uint32  classId,
uint16  eventId 
)

Definition at line 702 of file lwlock.c.

References Assert, LWLockTrancheArray, LWLockTranchesAllocated, NULL, and PG_WAIT_LWLOCK.

Referenced by pgstat_get_wait_event().

703 {
704  Assert(classId == PG_WAIT_LWLOCK);
705 
706  /*
707  * It is quite possible that user has registered tranche in one of the
708  * backends (e.g. by allocating lwlocks in dynamic shared memory) but not
709  * all of them, so we can't assume the tranche is registered here.
710  */
711  if (eventId >= LWLockTranchesAllocated ||
712  LWLockTrancheArray[eventId] == NULL)
713  return "extension";
714 
715  return LWLockTrancheArray[eventId];
716 }
static int LWLockTranchesAllocated
Definition: lwlock.c:115
static char ** LWLockTrancheArray
Definition: lwlock.c:114
#define NULL
Definition: c.h:226
#define Assert(condition)
Definition: c.h:671
#define PG_WAIT_LWLOCK
Definition: pgstat.h:718
LWLockPadded* GetNamedLWLockTranche ( const char *  tranche_name)

Definition at line 540 of file lwlock.c.

References elog, ERROR, i, NamedLWLockTrancheRequests, NULL, NUM_FIXED_LWLOCKS, and NamedLWLockTrancheRequest::num_lwlocks.

Referenced by pgss_shmem_startup().

541 {
542  int lock_pos;
543  int i;
544 
545  /*
546  * Obtain the position of base address of LWLock belonging to requested
547  * tranche_name in MainLWLockArray. LWLocks for named tranches are placed
548  * in MainLWLockArray after fixed locks.
549  */
550  lock_pos = NUM_FIXED_LWLOCKS;
551  for (i = 0; i < NamedLWLockTrancheRequests; i++)
552  {
553  if (strcmp(NamedLWLockTrancheRequestArray[i].tranche_name,
554  tranche_name) == 0)
555  return &MainLWLockArray[lock_pos];
556 
558  }
559 
560  if (i >= NamedLWLockTrancheRequests)
561  elog(ERROR, "requested tranche is not registered");
562 
563  /* just to keep compiler quiet */
564  return NULL;
565 }
#define NUM_FIXED_LWLOCKS
Definition: lwlock.h:129
#define ERROR
Definition: elog.h:43
int NamedLWLockTrancheRequests
Definition: lwlock.c:154
#define NULL
Definition: c.h:226
NamedLWLockTrancheRequest * NamedLWLockTrancheRequestArray
Definition: lwlock.c:152
int i
#define elog
Definition: elog.h:219
LWLockPadded * MainLWLockArray
Definition: lwlock.c:125
void InitLWLockAccess ( void  )

Definition at line 524 of file lwlock.c.

Referenced by InitProcess().

525 {
526 #ifdef LWLOCK_STATS
527  init_lwlock_stats();
528 #endif
529 }
bool LWLockAcquire ( LWLock lock,
LWLockMode  mode 
)

Definition at line 1110 of file lwlock.c.

References Assert, AssertArg, elog, ERROR, HOLD_INTERRUPTS, IsUnderPostmaster, LWLockHandle::lock, LOG_LWDEBUG, LW_EXCLUSIVE, LW_FLAG_RELEASE_OK, LW_SHARED, LWLockAttemptLock(), LWLockDequeueSelf(), LWLockQueueSelf(), LWLockReportWaitEnd(), LWLockReportWaitStart(), PGPROC::lwWaiting, MAX_BACKENDS, MAX_SIMUL_LWLOCKS, LWLockHandle::mode, MyProc, NULL, num_held_lwlocks, pg_atomic_fetch_or_u32(), pg_atomic_fetch_sub_u32(), PG_USED_FOR_ASSERTS_ONLY, PGSemaphoreLock(), PGSemaphoreUnlock(), PRINT_LWDEBUG, PGPROC::sem, LWLock::state, and T_NAME.

Referenced by _bt_end_vacuum(), _bt_start_vacuum(), _bt_vacuum_cycleid(), AbortBufferIO(), AbsorbFsyncRequests(), ActivateCommitTs(), AdvanceOldestCommitTsXid(), AdvanceXLInsertBuffer(), alloc_object(), AlterSystemSetConfigFile(), analyze_rel(), ApplyLauncherMain(), asyncQueueAddEntries(), asyncQueueAdvanceTail(), asyncQueueReadAllNotifications(), asyncQueueUnregister(), AsyncShmemInit(), AtAbort_Twophase(), AtPrepare_PredicateLocks(), attach_internal(), AutoVacLauncherMain(), AutoVacWorkerMain(), BackendIdGetProc(), BackendIdGetTransactionIds(), BackendPidGetProc(), BackendXidGetPid(), BecomeLockGroupLeader(), BecomeLockGroupMember(), BootStrapCLOG(), BootStrapMultiXact(), BootStrapSUBTRANS(), BufferAlloc(), CancelDBBackends(), CancelVirtualTransaction(), check_for_freed_segments(), CheckDeadLock(), CheckForSerializableConflictOut(), CheckPointPredicate(), CheckPointRelationMap(), CheckPointReplicationOrigin(), CheckPointReplicationSlots(), CheckPointTwoPhase(), CheckRecoveryConsistency(), CheckTableForSerializableConflictIn(), CheckTargetForConflictsIn(), CleanupInvalidationState(), ClearOldPredicateLocks(), clog_redo(), commit_ts_redo(), CountDBBackends(), CountDBConnections(), CountOtherDBBackends(), CountUserBackends(), CreateCheckPoint(), CreateEndOfRecoveryRecord(), CreateInitDecodingContext(), CreatePredicateLock(), CreateRestartPoint(), DeactivateCommitTs(), DeleteChildTargetLocks(), DeleteLockTarget(), destroy_superblock(), do_autovacuum(), do_pg_start_backup(), do_pg_stop_backup(), do_start_worker(), DropAllPredicateLocksFromTable(), DropSubscription(), DropTableSpace(), dsa_allocate_extended(), dsa_dump(), dsa_free(), dsa_pin(), dsa_release_in_place(), dsa_set_size_limit(), dsa_trim(), dsa_unpin(), dsm_attach(), dsm_create(), dsm_detach(), dsm_pin_segment(), dsm_unpin_segment(), ensure_active_superblock(), entry_reset(), Exec_ListenPreCommit(), ExpireAllKnownAssignedTransactionIds(), ExpireOldKnownAssignedTransactionIds(), ExpireTreeKnownAssignedTransactionIds(), ExtendCLOG(), ExtendCommitTs(), ExtendMultiXactMember(), ExtendMultiXactOffset(), ExtendSUBTRANS(), FastPathGetRelationLockEntry(), FastPathTransferRelationLocks(), FlushDatabaseBuffers(), FlushRelationBuffers(), ForceTransactionIdLimitUpdate(), ForwardFsyncRequest(), FreeWorkerInfo(), GetBackgroundWorkerPid(), GetBlockerStatusData(), GetConflictingVirtualXIDs(), GetCurrentVirtualXIDs(), GetLastImportantRecPtr(), GetLastSegSwitchData(), GetLatestCommitTsData(), GetLockConflicts(), GetLockStatusData(), GetMultiXactIdMembers(), GetNewMultiXactId(), GetNewObjectId(), GetNewTransactionId(), GetOldestActiveTransactionId(), GetOldestMultiXactId(), GetOldestRestartPoint(), GetOldestSafeDecodingTransactionId(), GetOldestXmin(), GetPredicateLockStatusData(), GetPreparedTransactionList(), GetRunningTransactionData(), GetRunningTransactionLocks(), GetSafeSnapshot(), GetSerializableTransactionSnapshotInt(), GetSnapshotData(), GetVirtualXIDsDelayingChkpt(), HaveVirtualXIDsDelayingChkpt(), InstallXLogFileSegment(), InvalidateBuffer(), KnownAssignedXidsAdd(), KnownAssignedXidsReset(), lock_twophase_recover(), LockAcquireExtended(), LockBuffer(), LockErrorCleanup(), LockGXact(), LockHasWaiters(), LockRefindAndRelease(), LockRelease(), LockReleaseAll(), LockWaiterCount(), logicalrep_worker_attach(), logicalrep_worker_detach(), logicalrep_worker_launch(), logicalrep_worker_stop(), MaintainOldSnapshotTimeMapping(), MarkAsPrepared(), MarkAsPreparing(), MaybeExtendOffsetSlru(), multixact_redo(), MultiXactAdvanceNextMXact(), MultiXactGetCheckptMulti(), MultiXactIdSetOldestMember(), MultiXactIdSetOldestVisible(), MultiXactSetNextMXact(), OldSerXidAdd(), OldSerXidGetMinConflictCommitSeqNo(), OldSerXidSetActiveSerXmin(), PageIsPredicateLocked(), perform_relmap_update(), pg_backend_random(), pg_notification_queue_usage(), pg_show_replication_origin_status(), pg_stat_get_subscription(), pg_stat_get_wal_senders(), pg_stat_statements_internal(), pgss_shmem_startup(), pgss_store(), PostPrepare_Locks(), PostPrepare_MultiXact(), PostPrepare_Twophase(), PreCommit_CheckForSerializationFailure(), PreCommit_Notify(), predicatelock_twophase_recover(), PredicateLockPageSplit(), PredicateLockTwoPhaseFinish(), PrefetchBuffer(), PrescanPreparedTransactions(), ProcArrayAdd(), ProcArrayApplyRecoveryInfo(), ProcArrayApplyXidAssignment(), ProcArrayGetReplicationSlotXmin(), ProcArrayGroupClearXid(), ProcArrayInstallImportedXmin(), ProcArrayInstallRestoredXmin(), ProcArrayRemove(), ProcArraySetReplicationSlotXmin(), ProcKill(), ProcSleep(), ReadBuffer_common(), ReadMultiXactCounts(), ReadNewTransactionId(), ReadNextMultiXactId(), ReadRecord(), RecordKnownAssignedTransactionIds(), RecordNewMultiXact(), RegisterDynamicBackgroundWorker(), RegisterPredicateLockingXid(), RelationCacheInitFilePreInvalidate(), ReleaseOneSerializableXact(), ReleasePredicateLocks(), RemoveGXact(), RemoveScratchTarget(), ReplicationOriginExitCleanup(), ReplicationSlotAcquire(), ReplicationSlotCreate(), ReplicationSlotDropPtr(), ReplicationSlotRelease(), ReplicationSlotsComputeLogicalRestartLSN(), ReplicationSlotsComputeRequiredLSN(), ReplicationSlotsComputeRequiredXmin(), ReplicationSlotsCountDBSlots(), replorigin_advance(), replorigin_drop(), replorigin_get_progress(), replorigin_session_advance(), replorigin_session_get_progress(), replorigin_session_reset(), replorigin_session_setup(), RestoreScratchTarget(), SaveSlotToPath(), SetCommitTsLimit(), SetMultiXactIdLimit(), SetOffsetVacuumLimit(), SetTransactionIdLimit(), SetXidCommitTsInPage(), SharedInvalBackendInit(), ShmemInitStruct(), SICleanupQueue(), SIGetDataEntries(), SignalBackends(), SIInsertDataEntries(), SimpleLruFlush(), SimpleLruReadPage(), SimpleLruReadPage_ReadOnly(), SimpleLruTruncate(), SimpleLruWaitIO(), SlruDeleteSegment(), SlruInternalWritePage(), ss_get_location(), StartBufferIO(), StartupCLOG(), StartupDecodingContext(), StartupSUBTRANS(), StartupXLOG(), SubTransSetParent(), SummarizeOldestCommittedSxact(), SyncOneBuffer(), SyncRepCancelWait(), SyncRepCleanupAtProcExit(), SyncRepInitConfig(), SyncRepReleaseWaiters(), SyncRepUpdateSyncStandbysDefined(), SyncRepWaitForLSN(), TablespaceCreateDbspace(), TerminateBackgroundWorker(), TransactionIdGetCommitTsData(), TransactionIdIsActive(), TransactionIdIsInProgress(), TransactionIdLimitedForOldSnapshots(), TransactionIdSetPageStatus(), TransactionTreeSetCommitTsData(), TransferPredicateLocksToNewTarget(), TrimCLOG(), TrimMultiXact(), TruncateMultiXact(), TwoPhaseGetGXact(), UpdateMinRecoveryPoint(), vacuum_rel(), VirtualXactLock(), VirtualXactLockTableCleanup(), VirtualXactLockTableInsert(), WaitIO(), WALInsertLockAcquire(), WALInsertLockAcquireExclusive(), write_relcache_init_file(), xact_redo_abort(), xact_redo_commit(), XidCacheRemoveRunningXids(), xlog_redo(), and XLogBackgroundFlush().

1111 {
1112  PGPROC *proc = MyProc;
1113  bool result = true;
1114  int extraWaits = 0;
1115 #ifdef LWLOCK_STATS
1116  lwlock_stats *lwstats;
1117 
1118  lwstats = get_lwlock_stats_entry(lock);
1119 #endif
1120 
1121  AssertArg(mode == LW_SHARED || mode == LW_EXCLUSIVE);
1122 
1123  PRINT_LWDEBUG("LWLockAcquire", lock, mode);
1124 
1125 #ifdef LWLOCK_STATS
1126  /* Count lock acquisition attempts */
1127  if (mode == LW_EXCLUSIVE)
1128  lwstats->ex_acquire_count++;
1129  else
1130  lwstats->sh_acquire_count++;
1131 #endif /* LWLOCK_STATS */
1132 
1133  /*
1134  * We can't wait if we haven't got a PGPROC. This should only occur
1135  * during bootstrap or shared memory initialization. Put an Assert here
1136  * to catch unsafe coding practices.
1137  */
1138  Assert(!(proc == NULL && IsUnderPostmaster));
1139 
1140  /* Ensure we will have room to remember the lock */
1142  elog(ERROR, "too many LWLocks taken");
1143 
1144  /*
1145  * Lock out cancel/die interrupts until we exit the code section protected
1146  * by the LWLock. This ensures that interrupts will not interfere with
1147  * manipulations of data structures in shared memory.
1148  */
1149  HOLD_INTERRUPTS();
1150 
1151  /*
1152  * Loop here to try to acquire lock after each time we are signaled by
1153  * LWLockRelease.
1154  *
1155  * NOTE: it might seem better to have LWLockRelease actually grant us the
1156  * lock, rather than retrying and possibly having to go back to sleep. But
1157  * in practice that is no good because it means a process swap for every
1158  * lock acquisition when two or more processes are contending for the same
1159  * lock. Since LWLocks are normally used to protect not-very-long
1160  * sections of computation, a process needs to be able to acquire and
1161  * release the same lock many times during a single CPU time slice, even
1162  * in the presence of contention. The efficiency of being able to do that
1163  * outweighs the inefficiency of sometimes wasting a process dispatch
1164  * cycle because the lock is not free when a released waiter finally gets
1165  * to run. See pgsql-hackers archives for 29-Dec-01.
1166  */
1167  for (;;)
1168  {
1169  bool mustwait;
1170 
1171  /*
1172  * Try to grab the lock the first time, we're not in the waitqueue
1173  * yet/anymore.
1174  */
1175  mustwait = LWLockAttemptLock(lock, mode);
1176 
1177  if (!mustwait)
1178  {
1179  LOG_LWDEBUG("LWLockAcquire", lock, "immediately acquired lock");
1180  break; /* got the lock */
1181  }
1182 
1183  /*
1184  * Ok, at this point we couldn't grab the lock on the first try. We
1185  * cannot simply queue ourselves to the end of the list and wait to be
1186  * woken up because by now the lock could long have been released.
1187  * Instead add us to the queue and try to grab the lock again. If we
1188  * succeed we need to revert the queuing and be happy, otherwise we
1189  * recheck the lock. If we still couldn't grab it, we know that the
1190  * other locker will see our queue entries when releasing since they
1191  * existed before we checked for the lock.
1192  */
1193 
1194  /* add to the queue */
1195  LWLockQueueSelf(lock, mode);
1196 
1197  /* we're now guaranteed to be woken up if necessary */
1198  mustwait = LWLockAttemptLock(lock, mode);
1199 
1200  /* ok, grabbed the lock the second time round, need to undo queueing */
1201  if (!mustwait)
1202  {
1203  LOG_LWDEBUG("LWLockAcquire", lock, "acquired, undoing queue");
1204 
1205  LWLockDequeueSelf(lock);
1206  break;
1207  }
1208 
1209  /*
1210  * Wait until awakened.
1211  *
1212  * Since we share the process wait semaphore with the regular lock
1213  * manager and ProcWaitForSignal, and we may need to acquire an LWLock
1214  * while one of those is pending, it is possible that we get awakened
1215  * for a reason other than being signaled by LWLockRelease. If so,
1216  * loop back and wait again. Once we've gotten the LWLock,
1217  * re-increment the sema by the number of additional signals received,
1218  * so that the lock manager or signal manager will see the received
1219  * signal when it next waits.
1220  */
1221  LOG_LWDEBUG("LWLockAcquire", lock, "waiting");
1222 
1223 #ifdef LWLOCK_STATS
1224  lwstats->block_count++;
1225 #endif
1226 
1227  LWLockReportWaitStart(lock);
1228  TRACE_POSTGRESQL_LWLOCK_WAIT_START(T_NAME(lock), mode);
1229 
1230  for (;;)
1231  {
1232  PGSemaphoreLock(proc->sem);
1233  if (!proc->lwWaiting)
1234  break;
1235  extraWaits++;
1236  }
1237 
1238  /* Retrying, allow LWLockRelease to release waiters again. */
1240 
1241 #ifdef LOCK_DEBUG
1242  {
1243  /* not waiting anymore */
1244  uint32 nwaiters PG_USED_FOR_ASSERTS_ONLY = pg_atomic_fetch_sub_u32(&lock->nwaiters, 1);
1245 
1246  Assert(nwaiters < MAX_BACKENDS);
1247  }
1248 #endif
1249 
1250  TRACE_POSTGRESQL_LWLOCK_WAIT_DONE(T_NAME(lock), mode);
1252 
1253  LOG_LWDEBUG("LWLockAcquire", lock, "awakened");
1254 
1255  /* Now loop back and try to acquire lock again. */
1256  result = false;
1257  }
1258 
1259  TRACE_POSTGRESQL_LWLOCK_ACQUIRE(T_NAME(lock), mode);
1260 
1261  /* Add lock to list of locks held by this backend */
1264 
1265  /*
1266  * Fix the process wait semaphore's count for any absorbed wakeups.
1267  */
1268  while (extraWaits-- > 0)
1269  PGSemaphoreUnlock(proc->sem);
1270 
1271  return result;
1272 }
#define T_NAME(lock)
Definition: lwlock.c:117
static uint32 pg_atomic_fetch_sub_u32(volatile pg_atomic_uint32 *ptr, int32 sub_)
Definition: atomics.h:353
static bool LWLockAttemptLock(LWLock *lock, LWLockMode mode)
Definition: lwlock.c:728
void PGSemaphoreUnlock(PGSemaphore sema)
Definition: posix_sema.c:323
PGPROC * MyProc
Definition: proc.c:67
bool lwWaiting
Definition: proc.h:116
#define MAX_SIMUL_LWLOCKS
Definition: lwlock.c:133
pg_atomic_uint32 state
Definition: lwlock.h:35
static LWLockHandle held_lwlocks[MAX_SIMUL_LWLOCKS]
Definition: lwlock.c:143
#define ERROR
Definition: elog.h:43
#define MAX_BACKENDS
Definition: postmaster.h:75
LWLockMode mode
Definition: lwlock.c:139
bool IsUnderPostmaster
Definition: globals.c:100
static void LWLockReportWaitEnd(void)
Definition: lwlock.c:693
unsigned int uint32
Definition: c.h:265
static void LWLockDequeueSelf(LWLock *lock)
Definition: lwlock.c:1015
#define AssertArg(condition)
Definition: c.h:673
static void LWLockReportWaitStart(LWLock *lock)
Definition: lwlock.c:684
static void LWLockQueueSelf(LWLock *lock, LWLockMode mode)
Definition: lwlock.c:971
static int num_held_lwlocks
Definition: lwlock.c:142
#define NULL
Definition: c.h:226
#define Assert(condition)
Definition: c.h:671
LWLock * lock
Definition: lwlock.c:138
#define PRINT_LWDEBUG(a, b, c)
Definition: lwlock.c:227
#define LW_FLAG_RELEASE_OK
Definition: lwlock.c:100
void PGSemaphoreLock(PGSemaphore sema)
Definition: posix_sema.c:303
#define HOLD_INTERRUPTS()
Definition: miscadmin.h:114
static uint32 pg_atomic_fetch_or_u32(volatile pg_atomic_uint32 *ptr, uint32 or_)
Definition: atomics.h:382
#define elog
Definition: elog.h:219
PGSemaphore sem
Definition: proc.h:90
#define PG_USED_FOR_ASSERTS_ONLY
Definition: c.h:986
Definition: proc.h:84
#define LOG_LWDEBUG(a, b, c)
Definition: lwlock.c:228
bool LWLockAcquireOrWait ( LWLock lock,
LWLockMode  mode 
)

Definition at line 1337 of file lwlock.c.

References Assert, elog, ERROR, HOLD_INTERRUPTS, LWLockHandle::lock, LOG_LWDEBUG, LW_EXCLUSIVE, LW_SHARED, LW_WAIT_UNTIL_FREE, LWLockAttemptLock(), LWLockDequeueSelf(), LWLockQueueSelf(), LWLockReportWaitEnd(), LWLockReportWaitStart(), PGPROC::lwWaiting, MAX_BACKENDS, MAX_SIMUL_LWLOCKS, LWLockHandle::mode, MyProc, num_held_lwlocks, pg_atomic_fetch_sub_u32(), PG_USED_FOR_ASSERTS_ONLY, PGSemaphoreLock(), PGSemaphoreUnlock(), PRINT_LWDEBUG, RESUME_INTERRUPTS, PGPROC::sem, and T_NAME.

Referenced by XLogFlush().

1338 {
1339  PGPROC *proc = MyProc;
1340  bool mustwait;
1341  int extraWaits = 0;
1342 #ifdef LWLOCK_STATS
1343  lwlock_stats *lwstats;
1344 
1345  lwstats = get_lwlock_stats_entry(lock);
1346 #endif
1347 
1348  Assert(mode == LW_SHARED || mode == LW_EXCLUSIVE);
1349 
1350  PRINT_LWDEBUG("LWLockAcquireOrWait", lock, mode);
1351 
1352  /* Ensure we will have room to remember the lock */
1354  elog(ERROR, "too many LWLocks taken");
1355 
1356  /*
1357  * Lock out cancel/die interrupts until we exit the code section protected
1358  * by the LWLock. This ensures that interrupts will not interfere with
1359  * manipulations of data structures in shared memory.
1360  */
1361  HOLD_INTERRUPTS();
1362 
1363  /*
1364  * NB: We're using nearly the same twice-in-a-row lock acquisition
1365  * protocol as LWLockAcquire(). Check its comments for details.
1366  */
1367  mustwait = LWLockAttemptLock(lock, mode);
1368 
1369  if (mustwait)
1370  {
1372 
1373  mustwait = LWLockAttemptLock(lock, mode);
1374 
1375  if (mustwait)
1376  {
1377  /*
1378  * Wait until awakened. Like in LWLockAcquire, be prepared for
1379  * bogus wakeups, because we share the semaphore with
1380  * ProcWaitForSignal.
1381  */
1382  LOG_LWDEBUG("LWLockAcquireOrWait", lock, "waiting");
1383 
1384 #ifdef LWLOCK_STATS
1385  lwstats->block_count++;
1386 #endif
1387 
1388  LWLockReportWaitStart(lock);
1389  TRACE_POSTGRESQL_LWLOCK_WAIT_START(T_NAME(lock), mode);
1390 
1391  for (;;)
1392  {
1393  PGSemaphoreLock(proc->sem);
1394  if (!proc->lwWaiting)
1395  break;
1396  extraWaits++;
1397  }
1398 
1399 #ifdef LOCK_DEBUG
1400  {
1401  /* not waiting anymore */
1402  uint32 nwaiters PG_USED_FOR_ASSERTS_ONLY = pg_atomic_fetch_sub_u32(&lock->nwaiters, 1);
1403 
1404  Assert(nwaiters < MAX_BACKENDS);
1405  }
1406 #endif
1407  TRACE_POSTGRESQL_LWLOCK_WAIT_DONE(T_NAME(lock), mode);
1409 
1410  LOG_LWDEBUG("LWLockAcquireOrWait", lock, "awakened");
1411  }
1412  else
1413  {
1414  LOG_LWDEBUG("LWLockAcquireOrWait", lock, "acquired, undoing queue");
1415 
1416  /*
1417  * Got lock in the second attempt, undo queueing. We need to treat
1418  * this as having successfully acquired the lock, otherwise we'd
1419  * not necessarily wake up people we've prevented from acquiring
1420  * the lock.
1421  */
1422  LWLockDequeueSelf(lock);
1423  }
1424  }
1425 
1426  /*
1427  * Fix the process wait semaphore's count for any absorbed wakeups.
1428  */
1429  while (extraWaits-- > 0)
1430  PGSemaphoreUnlock(proc->sem);
1431 
1432  if (mustwait)
1433  {
1434  /* Failed to get lock, so release interrupt holdoff */
1436  LOG_LWDEBUG("LWLockAcquireOrWait", lock, "failed");
1437  TRACE_POSTGRESQL_LWLOCK_ACQUIRE_OR_WAIT_FAIL(T_NAME(lock), mode);
1438  }
1439  else
1440  {
1441  LOG_LWDEBUG("LWLockAcquireOrWait", lock, "succeeded");
1442  /* Add lock to list of locks held by this backend */
1445  TRACE_POSTGRESQL_LWLOCK_ACQUIRE_OR_WAIT(T_NAME(lock), mode);
1446  }
1447 
1448  return !mustwait;
1449 }
#define T_NAME(lock)
Definition: lwlock.c:117
static uint32 pg_atomic_fetch_sub_u32(volatile pg_atomic_uint32 *ptr, int32 sub_)
Definition: atomics.h:353
static bool LWLockAttemptLock(LWLock *lock, LWLockMode mode)
Definition: lwlock.c:728
void PGSemaphoreUnlock(PGSemaphore sema)
Definition: posix_sema.c:323
PGPROC * MyProc
Definition: proc.c:67
bool lwWaiting
Definition: proc.h:116
#define MAX_SIMUL_LWLOCKS
Definition: lwlock.c:133
#define RESUME_INTERRUPTS()
Definition: miscadmin.h:116
static LWLockHandle held_lwlocks[MAX_SIMUL_LWLOCKS]
Definition: lwlock.c:143
#define ERROR
Definition: elog.h:43
#define MAX_BACKENDS
Definition: postmaster.h:75
LWLockMode mode
Definition: lwlock.c:139
static void LWLockReportWaitEnd(void)
Definition: lwlock.c:693
unsigned int uint32
Definition: c.h:265
static void LWLockDequeueSelf(LWLock *lock)
Definition: lwlock.c:1015
static void LWLockReportWaitStart(LWLock *lock)
Definition: lwlock.c:684
static void LWLockQueueSelf(LWLock *lock, LWLockMode mode)
Definition: lwlock.c:971
static int num_held_lwlocks
Definition: lwlock.c:142
#define Assert(condition)
Definition: c.h:671
LWLock * lock
Definition: lwlock.c:138
#define PRINT_LWDEBUG(a, b, c)
Definition: lwlock.c:227
void PGSemaphoreLock(PGSemaphore sema)
Definition: posix_sema.c:303
#define HOLD_INTERRUPTS()
Definition: miscadmin.h:114
#define elog
Definition: elog.h:219
PGSemaphore sem
Definition: proc.h:90
#define PG_USED_FOR_ASSERTS_ONLY
Definition: c.h:986
Definition: proc.h:84
#define LOG_LWDEBUG(a, b, c)
Definition: lwlock.c:228
bool LWLockConditionalAcquire ( LWLock lock,
LWLockMode  mode 
)

Definition at line 1282 of file lwlock.c.

References AssertArg, elog, ERROR, HOLD_INTERRUPTS, LWLockHandle::lock, LOG_LWDEBUG, LW_EXCLUSIVE, LW_SHARED, LWLockAttemptLock(), MAX_SIMUL_LWLOCKS, LWLockHandle::mode, num_held_lwlocks, PRINT_LWDEBUG, RESUME_INTERRUPTS, and T_NAME.

Referenced by BufferAlloc(), ConditionalLockBuffer(), ProcArrayEndTransaction(), SimpleLruWaitIO(), ss_report_location(), and XLogNeedsFlush().

1283 {
1284  bool mustwait;
1285 
1286  AssertArg(mode == LW_SHARED || mode == LW_EXCLUSIVE);
1287 
1288  PRINT_LWDEBUG("LWLockConditionalAcquire", lock, mode);
1289 
1290  /* Ensure we will have room to remember the lock */
1292  elog(ERROR, "too many LWLocks taken");
1293 
1294  /*
1295  * Lock out cancel/die interrupts until we exit the code section protected
1296  * by the LWLock. This ensures that interrupts will not interfere with
1297  * manipulations of data structures in shared memory.
1298  */
1299  HOLD_INTERRUPTS();
1300 
1301  /* Check for the lock */
1302  mustwait = LWLockAttemptLock(lock, mode);
1303 
1304  if (mustwait)
1305  {
1306  /* Failed to get lock, so release interrupt holdoff */
1308 
1309  LOG_LWDEBUG("LWLockConditionalAcquire", lock, "failed");
1310  TRACE_POSTGRESQL_LWLOCK_CONDACQUIRE_FAIL(T_NAME(lock), mode);
1311  }
1312  else
1313  {
1314  /* Add lock to list of locks held by this backend */
1317  TRACE_POSTGRESQL_LWLOCK_CONDACQUIRE(T_NAME(lock), mode);
1318  }
1319  return !mustwait;
1320 }
#define T_NAME(lock)
Definition: lwlock.c:117
static bool LWLockAttemptLock(LWLock *lock, LWLockMode mode)
Definition: lwlock.c:728
#define MAX_SIMUL_LWLOCKS
Definition: lwlock.c:133
#define RESUME_INTERRUPTS()
Definition: miscadmin.h:116
static LWLockHandle held_lwlocks[MAX_SIMUL_LWLOCKS]
Definition: lwlock.c:143
#define ERROR
Definition: elog.h:43
LWLockMode mode
Definition: lwlock.c:139
#define AssertArg(condition)
Definition: c.h:673
static int num_held_lwlocks
Definition: lwlock.c:142
LWLock * lock
Definition: lwlock.c:138
#define PRINT_LWDEBUG(a, b, c)
Definition: lwlock.c:227
#define HOLD_INTERRUPTS()
Definition: miscadmin.h:114
#define elog
Definition: elog.h:219
#define LOG_LWDEBUG(a, b, c)
Definition: lwlock.c:228
bool LWLockHeldByMeInMode ( LWLock lock,
LWLockMode  mode 
)

Definition at line 1848 of file lwlock.c.

References i, and num_held_lwlocks.

Referenced by IsBufferCleanupOK(), and MarkBufferDirty().

1849 {
1850  int i;
1851 
1852  for (i = 0; i < num_held_lwlocks; i++)
1853  {
1854  if (held_lwlocks[i].lock == l && held_lwlocks[i].mode == mode)
1855  return true;
1856  }
1857  return false;
1858 }
static LWLockHandle held_lwlocks[MAX_SIMUL_LWLOCKS]
Definition: lwlock.c:143
static int num_held_lwlocks
Definition: lwlock.c:142
int i
void LWLockInitialize ( LWLock lock,
int  tranche_id 
)

Definition at line 666 of file lwlock.c.

References LW_FLAG_RELEASE_OK, pg_atomic_init_u32(), proclist_init(), LWLock::state, LWLock::tranche, and LWLock::waiters.

Referenced by create_internal(), InitBufferPool(), InitializeLWLocks(), InitProcGlobal(), ReplicationOriginShmemInit(), ReplicationSlotsShmemInit(), SimpleLruInit(), and XLOGShmemInit().

667 {
669 #ifdef LOCK_DEBUG
670  pg_atomic_init_u32(&lock->nwaiters, 0);
671 #endif
672  lock->tranche = tranche_id;
673  proclist_init(&lock->waiters);
674 }
proclist_head waiters
Definition: lwlock.h:36
pg_atomic_uint32 state
Definition: lwlock.h:35
uint16 tranche
Definition: lwlock.h:34
#define LW_FLAG_RELEASE_OK
Definition: lwlock.c:100
static void proclist_init(proclist_head *list)
Definition: proclist.h:29
static void pg_atomic_init_u32(volatile pg_atomic_uint32 *ptr, uint32 val)
Definition: atomics.h:233
int LWLockNewTrancheId ( void  )

Definition at line 571 of file lwlock.c.

References ShmemLock, SpinLockAcquire, and SpinLockRelease.

Referenced by InitializeLWLocks().

572 {
573  int result;
574  int *LWLockCounter;
575 
576  LWLockCounter = (int *) ((char *) MainLWLockArray - sizeof(int));
578  result = (*LWLockCounter)++;
580 
581  return result;
582 }
#define SpinLockAcquire(lock)
Definition: spin.h:62
#define SpinLockRelease(lock)
Definition: spin.h:64
slock_t * ShmemLock
Definition: shmem.c:84
LWLockPadded * MainLWLockArray
Definition: lwlock.c:125
void LWLockRegisterTranche ( int  tranche_id,
char *  tranche_name 
)

Definition at line 591 of file lwlock.c.

References Assert, i, LWLockTrancheArray, LWLockTranchesAllocated, NULL, and repalloc().

Referenced by CreateSharedProcArray(), InitBufferPool(), RegisterLWLockTranches(), ReplicationOriginShmemInit(), ReplicationSlotsShmemInit(), SimpleLruInit(), and XLOGShmemInit().

592 {
594 
595  if (tranche_id >= LWLockTranchesAllocated)
596  {
598  int j = LWLockTranchesAllocated;
599 
600  while (i <= tranche_id)
601  i *= 2;
602 
603  LWLockTrancheArray = (char **)
604  repalloc(LWLockTrancheArray, i * sizeof(char *));
606  while (j < LWLockTranchesAllocated)
607  LWLockTrancheArray[j++] = NULL;
608  }
609 
610  LWLockTrancheArray[tranche_id] = tranche_name;
611 }
static int LWLockTranchesAllocated
Definition: lwlock.c:115
static char ** LWLockTrancheArray
Definition: lwlock.c:114
#define NULL
Definition: c.h:226
#define Assert(condition)
Definition: c.h:671
void * repalloc(void *pointer, Size size)
Definition: mcxt.c:1021
int i
void LWLockRelease ( LWLock lock)

Definition at line 1714 of file lwlock.c.

References Assert, elog, ERROR, i, LOG_LWDEBUG, LW_EXCLUSIVE, LW_FLAG_HAS_WAITERS, LW_FLAG_RELEASE_OK, LW_LOCK_MASK, LW_VAL_EXCLUSIVE, LW_VAL_SHARED, LWLockWakeup(), LWLockHandle::mode, num_held_lwlocks, pg_atomic_sub_fetch_u32(), PRINT_LWDEBUG, RESUME_INTERRUPTS, and T_NAME.

Referenced by _bt_end_vacuum(), _bt_start_vacuum(), _bt_vacuum_cycleid(), AbsorbFsyncRequests(), ActivateCommitTs(), AdvanceOldestCommitTsXid(), AdvanceXLInsertBuffer(), alloc_object(), AlterSystemSetConfigFile(), analyze_rel(), ApplyLauncherMain(), asyncQueueAddEntries(), asyncQueueAdvanceTail(), asyncQueueReadAllNotifications(), asyncQueueUnregister(), AsyncShmemInit(), AtAbort_Twophase(), AtPrepare_PredicateLocks(), attach_internal(), AutoVacLauncherMain(), AutoVacWorkerMain(), BackendIdGetProc(), BackendIdGetTransactionIds(), BackendPidGetProc(), BackendXidGetPid(), BecomeLockGroupLeader(), BecomeLockGroupMember(), BootStrapCLOG(), BootStrapMultiXact(), BootStrapSUBTRANS(), BufferAlloc(), CancelDBBackends(), CancelVirtualTransaction(), check_for_freed_segments(), CheckDeadLock(), CheckForSerializableConflictOut(), CheckPointPredicate(), CheckPointRelationMap(), CheckPointReplicationOrigin(), CheckPointReplicationSlots(), CheckPointTwoPhase(), CheckRecoveryConsistency(), CheckTableForSerializableConflictIn(), CheckTargetForConflictsIn(), CleanupInvalidationState(), ClearOldPredicateLocks(), clog_redo(), commit_ts_redo(), CountDBBackends(), CountDBConnections(), CountOtherDBBackends(), CountUserBackends(), CreateCheckPoint(), CreateEndOfRecoveryRecord(), CreateInitDecodingContext(), CreatePredicateLock(), CreateRestartPoint(), DeactivateCommitTs(), DeleteChildTargetLocks(), DeleteLockTarget(), destroy_superblock(), do_autovacuum(), do_pg_start_backup(), do_pg_stop_backup(), do_start_worker(), DropAllPredicateLocksFromTable(), DropSubscription(), DropTableSpace(), dsa_allocate_extended(), dsa_dump(), dsa_free(), dsa_pin(), dsa_release_in_place(), dsa_set_size_limit(), dsa_trim(), dsa_unpin(), dsm_attach(), dsm_create(), dsm_detach(), dsm_pin_segment(), dsm_unpin_segment(), ensure_active_superblock(), entry_reset(), Exec_ListenPreCommit(), ExpireAllKnownAssignedTransactionIds(), ExpireOldKnownAssignedTransactionIds(), ExpireTreeKnownAssignedTransactionIds(), ExtendCLOG(), ExtendCommitTs(), ExtendMultiXactMember(), ExtendMultiXactOffset(), ExtendSUBTRANS(), FastPathGetRelationLockEntry(), FastPathTransferRelationLocks(), find_multixact_start(), FlushDatabaseBuffers(), FlushRelationBuffers(), ForceTransactionIdLimitUpdate(), ForwardFsyncRequest(), FreeWorkerInfo(), GetBackgroundWorkerPid(), GetBlockerStatusData(), GetConflictingVirtualXIDs(), GetCurrentVirtualXIDs(), GetLastImportantRecPtr(), GetLastSegSwitchData(), GetLatestCommitTsData(), GetLockConflicts(), GetLockStatusData(), GetMultiXactIdMembers(), GetNewMultiXactId(), GetNewObjectId(), GetNewTransactionId(), GetOldestActiveTransactionId(), GetOldestMultiXactId(), GetOldestRestartPoint(), GetOldestSafeDecodingTransactionId(), GetOldestXmin(), GetPredicateLockStatusData(), GetPreparedTransactionList(), GetRunningTransactionLocks(), GetSafeSnapshot(), GetSerializableTransactionSnapshotInt(), GetSnapshotData(), GetVirtualXIDsDelayingChkpt(), HaveVirtualXIDsDelayingChkpt(), InstallXLogFileSegment(), InvalidateBuffer(), KnownAssignedXidsAdd(), KnownAssignedXidsReset(), lock_twophase_recover(), LockAcquireExtended(), LockBuffer(), LockErrorCleanup(), LockGXact(), LockHasWaiters(), LockRefindAndRelease(), LockRelease(), LockReleaseAll(), LockWaiterCount(), logicalrep_worker_attach(), logicalrep_worker_detach(), logicalrep_worker_launch(), logicalrep_worker_stop(), LogStandbySnapshot(), LWLockReleaseAll(), LWLockReleaseClearVar(), MaintainOldSnapshotTimeMapping(), MarkAsPrepared(), MarkAsPreparing(), MaybeExtendOffsetSlru(), multixact_redo(), MultiXactAdvanceNextMXact(), MultiXactGetCheckptMulti(), MultiXactIdSetOldestMember(), MultiXactIdSetOldestVisible(), MultiXactSetNextMXact(), OldSerXidAdd(), OldSerXidGetMinConflictCommitSeqNo(), OldSerXidSetActiveSerXmin(), OnConflict_CheckForSerializationFailure(), PageIsPredicateLocked(), perform_relmap_update(), pg_backend_random(), pg_notification_queue_usage(), pg_show_replication_origin_status(), pg_stat_get_subscription(), pg_stat_get_wal_senders(), pg_stat_statements_internal(), pgss_shmem_startup(), pgss_store(), PostPrepare_Locks(), PostPrepare_MultiXact(), PostPrepare_Twophase(), PreCommit_CheckForSerializationFailure(), PreCommit_Notify(), predicatelock_twophase_recover(), PredicateLockPageSplit(), PredicateLockTwoPhaseFinish(), PrefetchBuffer(), PrescanPreparedTransactions(), ProcArrayAdd(), ProcArrayApplyRecoveryInfo(), ProcArrayApplyXidAssignment(), ProcArrayEndTransaction(), ProcArrayGetReplicationSlotXmin(), ProcArrayGroupClearXid(), ProcArrayInstallImportedXmin(), ProcArrayInstallRestoredXmin(), ProcArrayRemove(), ProcArraySetReplicationSlotXmin(), ProcKill(), ProcSleep(), ReadMultiXactCounts(), ReadNewTransactionId(), ReadNextMultiXactId(), ReadRecord(), RecordKnownAssignedTransactionIds(), RecordNewMultiXact(), RegisterDynamicBackgroundWorker(), RegisterPredicateLockingXid(), RelationCacheInitFilePostInvalidate(), ReleaseOneSerializableXact(), ReleasePredicateLocks(), RemoveGXact(), RemoveScratchTarget(), ReplicationOriginExitCleanup(), ReplicationSlotAcquire(), ReplicationSlotCreate(), ReplicationSlotDropPtr(), ReplicationSlotRelease(), ReplicationSlotsComputeLogicalRestartLSN(), ReplicationSlotsComputeRequiredLSN(), ReplicationSlotsComputeRequiredXmin(), ReplicationSlotsCountDBSlots(), replorigin_advance(), replorigin_drop(), replorigin_get_progress(), replorigin_session_advance(), replorigin_session_get_progress(), replorigin_session_reset(), replorigin_session_setup(), RestoreScratchTarget(), SaveSlotToPath(), SetCommitTsLimit(), SetMultiXactIdLimit(), SetOffsetVacuumLimit(), SetTransactionIdLimit(), SetXidCommitTsInPage(), SharedInvalBackendInit(), ShmemInitStruct(), SICleanupQueue(), SIGetDataEntries(), SignalBackends(), SIInsertDataEntries(), SimpleLruFlush(), SimpleLruReadPage(), SimpleLruReadPage_ReadOnly(), SimpleLruTruncate(), SimpleLruWaitIO(), SlruDeleteSegment(), SlruInternalWritePage(), ss_get_location(), ss_report_location(), StartBufferIO(), StartupCLOG(), StartupDecodingContext(), StartupSUBTRANS(), StartupXLOG(), SubTransGetParent(), SubTransSetParent(), SummarizeOldestCommittedSxact(), SyncOneBuffer(), SyncRepCancelWait(), SyncRepCleanupAtProcExit(), SyncRepInitConfig(), SyncRepReleaseWaiters(), SyncRepUpdateSyncStandbysDefined(), SyncRepWaitForLSN(), TablespaceCreateDbspace(), TerminateBackgroundWorker(), TerminateBufferIO(), TransactionIdGetCommitTsData(), TransactionIdGetStatus(), TransactionIdIsActive(), TransactionIdIsInProgress(), TransactionIdLimitedForOldSnapshots(), TransactionIdSetPageStatus(), TransactionTreeSetCommitTsData(), TransferPredicateLocksToNewTarget(), TrimCLOG(), TrimMultiXact(), TruncateMultiXact(), TwoPhaseGetGXact(), UpdateMinRecoveryPoint(), vacuum_rel(), VirtualXactLock(), VirtualXactLockTableCleanup(), VirtualXactLockTableInsert(), WaitIO(), WaitOnLock(), write_relcache_init_file(), xact_redo_abort(), xact_redo_commit(), XidCacheRemoveRunningXids(), xlog_redo(), XLogBackgroundFlush(), XLogFlush(), and XLogNeedsFlush().

1715 {
1716  LWLockMode mode;
1717  uint32 oldstate;
1718  bool check_waiters;
1719  int i;
1720 
1721  /*
1722  * Remove lock from list of locks held. Usually, but not always, it will
1723  * be the latest-acquired lock; so search array backwards.
1724  */
1725  for (i = num_held_lwlocks; --i >= 0;)
1726  if (lock == held_lwlocks[i].lock)
1727  break;
1728 
1729  if (i < 0)
1730  elog(ERROR, "lock %s is not held", T_NAME(lock));
1731 
1732  mode = held_lwlocks[i].mode;
1733 
1734  num_held_lwlocks--;
1735  for (; i < num_held_lwlocks; i++)
1736  held_lwlocks[i] = held_lwlocks[i + 1];
1737 
1738  PRINT_LWDEBUG("LWLockRelease", lock, mode);
1739 
1740  /*
1741  * Release my hold on lock, after that it can immediately be acquired by
1742  * others, even if we still have to wakeup other waiters.
1743  */
1744  if (mode == LW_EXCLUSIVE)
1745  oldstate = pg_atomic_sub_fetch_u32(&lock->state, LW_VAL_EXCLUSIVE);
1746  else
1747  oldstate = pg_atomic_sub_fetch_u32(&lock->state, LW_VAL_SHARED);
1748 
1749  /* nobody else can have that kind of lock */
1750  Assert(!(oldstate & LW_VAL_EXCLUSIVE));
1751 
1752 
1753  /*
1754  * We're still waiting for backends to get scheduled, don't wake them up
1755  * again.
1756  */
1757  if ((oldstate & (LW_FLAG_HAS_WAITERS | LW_FLAG_RELEASE_OK)) ==
1759  (oldstate & LW_LOCK_MASK) == 0)
1760  check_waiters = true;
1761  else
1762  check_waiters = false;
1763 
1764  /*
1765  * As waking up waiters requires the spinlock to be acquired, only do so
1766  * if necessary.
1767  */
1768  if (check_waiters)
1769  {
1770  /* XXX: remove before commit? */
1771  LOG_LWDEBUG("LWLockRelease", lock, "releasing waiters");
1772  LWLockWakeup(lock);
1773  }
1774 
1775  TRACE_POSTGRESQL_LWLOCK_RELEASE(T_NAME(lock));
1776 
1777  /*
1778  * Now okay to allow cancel/die interrupts.
1779  */
1781 }
#define T_NAME(lock)
Definition: lwlock.c:117
LWLockMode
Definition: lwlock.h:132
#define LW_FLAG_HAS_WAITERS
Definition: lwlock.c:99
static uint32 pg_atomic_sub_fetch_u32(volatile pg_atomic_uint32 *ptr, int32 sub_)
Definition: atomics.h:411
#define LW_LOCK_MASK
Definition: lwlock.c:106
#define LW_VAL_SHARED
Definition: lwlock.c:104
#define RESUME_INTERRUPTS()
Definition: miscadmin.h:116
static LWLockHandle held_lwlocks[MAX_SIMUL_LWLOCKS]
Definition: lwlock.c:143
#define ERROR
Definition: elog.h:43
LWLockMode mode
Definition: lwlock.c:139
static void LWLockWakeup(LWLock *lock)
Definition: lwlock.c:864
unsigned int uint32
Definition: c.h:265
static int num_held_lwlocks
Definition: lwlock.c:142
#define Assert(condition)
Definition: c.h:671
#define PRINT_LWDEBUG(a, b, c)
Definition: lwlock.c:227
#define LW_FLAG_RELEASE_OK
Definition: lwlock.c:100
#define LW_VAL_EXCLUSIVE
Definition: lwlock.c:103
int i
#define elog
Definition: elog.h:219
#define LOG_LWDEBUG(a, b, c)
Definition: lwlock.c:228
void LWLockReleaseAll ( void  )

Definition at line 1813 of file lwlock.c.

References HOLD_INTERRUPTS, LWLockRelease(), and num_held_lwlocks.

Referenced by AbortSubTransaction(), AbortTransaction(), AuxiliaryProcKill(), BackgroundWriterMain(), CheckpointerMain(), ProcKill(), ShutdownAuxiliaryProcess(), WalSndErrorCleanup(), and WalWriterMain().

1814 {
1815  while (num_held_lwlocks > 0)
1816  {
1817  HOLD_INTERRUPTS(); /* match the upcoming RESUME_INTERRUPTS */
1818 
1820  }
1821 }
void LWLockRelease(LWLock *lock)
Definition: lwlock.c:1714
static LWLockHandle held_lwlocks[MAX_SIMUL_LWLOCKS]
Definition: lwlock.c:143
static int num_held_lwlocks
Definition: lwlock.c:142
#define HOLD_INTERRUPTS()
Definition: miscadmin.h:114
void LWLockReleaseClearVar ( LWLock lock,
uint64 *  valptr,
uint64  val 
)

Definition at line 1787 of file lwlock.c.

References LWLockRelease(), LWLockWaitListLock(), LWLockWaitListUnlock(), and val.

Referenced by WALInsertLockRelease().

1788 {
1789  LWLockWaitListLock(lock);
1790 
1791  /*
1792  * Set the variable's value before releasing the lock, that prevents race
1793  * a race condition wherein a new locker acquires the lock, but hasn't yet
1794  * set the variables value.
1795  */
1796  *valptr = val;
1797  LWLockWaitListUnlock(lock);
1798 
1799  LWLockRelease(lock);
1800 }
static void LWLockWaitListUnlock(LWLock *lock)
Definition: lwlock.c:851
void LWLockRelease(LWLock *lock)
Definition: lwlock.c:1714
static void LWLockWaitListLock(LWLock *lock)
Definition: lwlock.c:799
long val
Definition: informix.c:689
Size LWLockShmemSize ( void  )

Definition at line 349 of file lwlock.c.

References add_size(), i, lock_named_request_allowed, LWLOCK_PADDED_SIZE, mul_size(), NamedLWLockTrancheRequests, NUM_FIXED_LWLOCKS, and NumLWLocksByNamedTranches().

Referenced by CreateLWLocks(), and CreateSharedMemoryAndSemaphores().

350 {
351  Size size;
352  int i;
353  int numLocks = NUM_FIXED_LWLOCKS;
354 
355  numLocks += NumLWLocksByNamedTranches();
356 
357  /* Space for the LWLock array. */
358  size = mul_size(numLocks, sizeof(LWLockPadded));
359 
360  /* Space for dynamic allocation counter, plus room for alignment. */
361  size = add_size(size, sizeof(int) + LWLOCK_PADDED_SIZE);
362 
363  /* space for named tranches. */
365 
366  /* space for name of each tranche. */
367  for (i = 0; i < NamedLWLockTrancheRequests; i++)
368  size = add_size(size, strlen(NamedLWLockTrancheRequestArray[i].tranche_name) + 1);
369 
370  /* Disallow named LWLocks' requests after startup */
372 
373  return size;
374 }
static bool lock_named_request_allowed
Definition: lwlock.c:158
#define NUM_FIXED_LWLOCKS
Definition: lwlock.h:129
int NamedLWLockTrancheRequests
Definition: lwlock.c:154
Size mul_size(Size s1, Size s2)
Definition: shmem.c:492
Size add_size(Size s1, Size s2)
Definition: shmem.c:475
size_t Size
Definition: c.h:353
static int NumLWLocksByNamedTranches(void)
Definition: lwlock.c:334
NamedLWLockTrancheRequest * NamedLWLockTrancheRequestArray
Definition: lwlock.c:152
int i
#define LWLOCK_PADDED_SIZE
Definition: lwlock.h:73
void LWLockUpdateVar ( LWLock lock,
uint64 *  valptr,
uint64  value 
)

Definition at line 1660 of file lwlock.c.

References Assert, proclist_mutable_iter::cur, GetPGProcByNumber, LW_EXCLUSIVE, LW_VAL_EXCLUSIVE, LW_WAIT_UNTIL_FREE, LWLockWaitListLock(), LWLockWaitListUnlock(), PGPROC::lwWaiting, PGPROC::lwWaitMode, pg_atomic_read_u32(), pg_write_barrier, PGSemaphoreUnlock(), PRINT_LWDEBUG, proclist_delete, proclist_foreach_modify, proclist_init(), proclist_push_tail, PGPROC::sem, LWLock::state, val, and LWLock::waiters.

Referenced by WALInsertLockAcquireExclusive(), and WALInsertLockUpdateInsertingAt().

1661 {
1662  proclist_head wakeup;
1663  proclist_mutable_iter iter;
1664 
1665  PRINT_LWDEBUG("LWLockUpdateVar", lock, LW_EXCLUSIVE);
1666 
1667  proclist_init(&wakeup);
1668 
1669  LWLockWaitListLock(lock);
1670 
1672 
1673  /* Update the lock's value */
1674  *valptr = val;
1675 
1676  /*
1677  * See if there are any LW_WAIT_UNTIL_FREE waiters that need to be woken
1678  * up. They are always in the front of the queue.
1679  */
1680  proclist_foreach_modify(iter, &lock->waiters, lwWaitLink)
1681  {
1682  PGPROC *waiter = GetPGProcByNumber(iter.cur);
1683 
1684  if (waiter->lwWaitMode != LW_WAIT_UNTIL_FREE)
1685  break;
1686 
1687  proclist_delete(&lock->waiters, iter.cur, lwWaitLink);
1688  proclist_push_tail(&wakeup, iter.cur, lwWaitLink);
1689  }
1690 
1691  /* We are done updating shared state of the lock itself. */
1692  LWLockWaitListUnlock(lock);
1693 
1694  /*
1695  * Awaken any waiters I removed from the queue.
1696  */
1697  proclist_foreach_modify(iter, &wakeup, lwWaitLink)
1698  {
1699  PGPROC *waiter = GetPGProcByNumber(iter.cur);
1700 
1701  proclist_delete(&wakeup, iter.cur, lwWaitLink);
1702  /* check comment in LWLockWakeup() about this barrier */
1703  pg_write_barrier();
1704  waiter->lwWaiting = false;
1705  PGSemaphoreUnlock(waiter->sem);
1706  }
1707 }
#define GetPGProcByNumber(n)
Definition: proc.h:253
void PGSemaphoreUnlock(PGSemaphore sema)
Definition: posix_sema.c:323
proclist_head waiters
Definition: lwlock.h:36
bool lwWaiting
Definition: proc.h:116
static void LWLockWaitListUnlock(LWLock *lock)
Definition: lwlock.c:851
#define proclist_foreach_modify(iter, lhead, link_member)
Definition: proclist.h:195
uint8 lwWaitMode
Definition: proc.h:117
pg_atomic_uint32 state
Definition: lwlock.h:35
#define proclist_delete(list, procno, link_member)
Definition: proclist.h:176
#define Assert(condition)
Definition: c.h:671
#define PRINT_LWDEBUG(a, b, c)
Definition: lwlock.c:227
#define proclist_push_tail(list, procno, link_member)
Definition: proclist.h:180
static void LWLockWaitListLock(LWLock *lock)
Definition: lwlock.c:799
#define LW_VAL_EXCLUSIVE
Definition: lwlock.c:103
static void proclist_init(proclist_head *list)
Definition: proclist.h:29
#define pg_write_barrier()
Definition: atomics.h:161
PGSemaphore sem
Definition: proc.h:90
Definition: proc.h:84
long val
Definition: informix.c:689
static uint32 pg_atomic_read_u32(volatile pg_atomic_uint32 *ptr)
Definition: atomics.h:251
bool LWLockWaitForVar ( LWLock lock,
uint64 *  valptr,
uint64  oldval,
uint64 *  newval 
)

Definition at line 1521 of file lwlock.c.

References Assert, HOLD_INTERRUPTS, LOG_LWDEBUG, LW_EXCLUSIVE, LW_FLAG_RELEASE_OK, LW_WAIT_UNTIL_FREE, LWLockConflictsWithVar(), LWLockDequeueSelf(), LWLockQueueSelf(), LWLockReportWaitEnd(), LWLockReportWaitStart(), PGPROC::lwWaiting, MAX_BACKENDS, MyProc, pg_atomic_fetch_or_u32(), pg_atomic_fetch_sub_u32(), PG_USED_FOR_ASSERTS_ONLY, PGSemaphoreLock(), PGSemaphoreUnlock(), PRINT_LWDEBUG, RESUME_INTERRUPTS, PGPROC::sem, LWLock::state, and T_NAME.

Referenced by WaitXLogInsertionsToFinish().

1522 {
1523  PGPROC *proc = MyProc;
1524  int extraWaits = 0;
1525  bool result = false;
1526 #ifdef LWLOCK_STATS
1527  lwlock_stats *lwstats;
1528 
1529  lwstats = get_lwlock_stats_entry(lock);
1530 #endif
1531 
1532  PRINT_LWDEBUG("LWLockWaitForVar", lock, LW_WAIT_UNTIL_FREE);
1533 
1534  /*
1535  * Lock out cancel/die interrupts while we sleep on the lock. There is no
1536  * cleanup mechanism to remove us from the wait queue if we got
1537  * interrupted.
1538  */
1539  HOLD_INTERRUPTS();
1540 
1541  /*
1542  * Loop here to check the lock's status after each time we are signaled.
1543  */
1544  for (;;)
1545  {
1546  bool mustwait;
1547 
1548  mustwait = LWLockConflictsWithVar(lock, valptr, oldval, newval,
1549  &result);
1550 
1551  if (!mustwait)
1552  break; /* the lock was free or value didn't match */
1553 
1554  /*
1555  * Add myself to wait queue. Note that this is racy, somebody else
1556  * could wakeup before we're finished queuing. NB: We're using nearly
1557  * the same twice-in-a-row lock acquisition protocol as
1558  * LWLockAcquire(). Check its comments for details. The only
1559  * difference is that we also have to check the variable's values when
1560  * checking the state of the lock.
1561  */
1563 
1564  /*
1565  * Set RELEASE_OK flag, to make sure we get woken up as soon as the
1566  * lock is released.
1567  */
1569 
1570  /*
1571  * We're now guaranteed to be woken up if necessary. Recheck the lock
1572  * and variables state.
1573  */
1574  mustwait = LWLockConflictsWithVar(lock, valptr, oldval, newval,
1575  &result);
1576 
1577  /* Ok, no conflict after we queued ourselves. Undo queueing. */
1578  if (!mustwait)
1579  {
1580  LOG_LWDEBUG("LWLockWaitForVar", lock, "free, undoing queue");
1581 
1582  LWLockDequeueSelf(lock);
1583  break;
1584  }
1585 
1586  /*
1587  * Wait until awakened.
1588  *
1589  * Since we share the process wait semaphore with the regular lock
1590  * manager and ProcWaitForSignal, and we may need to acquire an LWLock
1591  * while one of those is pending, it is possible that we get awakened
1592  * for a reason other than being signaled by LWLockRelease. If so,
1593  * loop back and wait again. Once we've gotten the LWLock,
1594  * re-increment the sema by the number of additional signals received,
1595  * so that the lock manager or signal manager will see the received
1596  * signal when it next waits.
1597  */
1598  LOG_LWDEBUG("LWLockWaitForVar", lock, "waiting");
1599 
1600 #ifdef LWLOCK_STATS
1601  lwstats->block_count++;
1602 #endif
1603 
1604  LWLockReportWaitStart(lock);
1605  TRACE_POSTGRESQL_LWLOCK_WAIT_START(T_NAME(lock), LW_EXCLUSIVE);
1606 
1607  for (;;)
1608  {
1609  PGSemaphoreLock(proc->sem);
1610  if (!proc->lwWaiting)
1611  break;
1612  extraWaits++;
1613  }
1614 
1615 #ifdef LOCK_DEBUG
1616  {
1617  /* not waiting anymore */
1618  uint32 nwaiters PG_USED_FOR_ASSERTS_ONLY = pg_atomic_fetch_sub_u32(&lock->nwaiters, 1);
1619 
1620  Assert(nwaiters < MAX_BACKENDS);
1621  }
1622 #endif
1623 
1624  TRACE_POSTGRESQL_LWLOCK_WAIT_DONE(T_NAME(lock), LW_EXCLUSIVE);
1626 
1627  LOG_LWDEBUG("LWLockWaitForVar", lock, "awakened");
1628 
1629  /* Now loop back and check the status of the lock again. */
1630  }
1631 
1632  TRACE_POSTGRESQL_LWLOCK_ACQUIRE(T_NAME(lock), LW_EXCLUSIVE);
1633 
1634  /*
1635  * Fix the process wait semaphore's count for any absorbed wakeups.
1636  */
1637  while (extraWaits-- > 0)
1638  PGSemaphoreUnlock(proc->sem);
1639 
1640  /*
1641  * Now okay to allow cancel/die interrupts.
1642  */
1644 
1645  return result;
1646 }
#define T_NAME(lock)
Definition: lwlock.c:117
static uint32 pg_atomic_fetch_sub_u32(volatile pg_atomic_uint32 *ptr, int32 sub_)
Definition: atomics.h:353
void PGSemaphoreUnlock(PGSemaphore sema)
Definition: posix_sema.c:323
PGPROC * MyProc
Definition: proc.c:67
bool lwWaiting
Definition: proc.h:116
pg_atomic_uint32 state
Definition: lwlock.h:35
#define RESUME_INTERRUPTS()
Definition: miscadmin.h:116
#define MAX_BACKENDS
Definition: postmaster.h:75
static void LWLockReportWaitEnd(void)
Definition: lwlock.c:693
unsigned int uint32
Definition: c.h:265
static void LWLockDequeueSelf(LWLock *lock)
Definition: lwlock.c:1015
static void LWLockReportWaitStart(LWLock *lock)
Definition: lwlock.c:684
static void LWLockQueueSelf(LWLock *lock, LWLockMode mode)
Definition: lwlock.c:971
#define Assert(condition)
Definition: c.h:671
#define newval
#define PRINT_LWDEBUG(a, b, c)
Definition: lwlock.c:227
#define LW_FLAG_RELEASE_OK
Definition: lwlock.c:100
void PGSemaphoreLock(PGSemaphore sema)
Definition: posix_sema.c:303
static bool LWLockConflictsWithVar(LWLock *lock, uint64 *valptr, uint64 oldval, uint64 *newval, bool *result)
Definition: lwlock.c:1461
#define HOLD_INTERRUPTS()
Definition: miscadmin.h:114
static uint32 pg_atomic_fetch_or_u32(volatile pg_atomic_uint32 *ptr, uint32 or_)
Definition: atomics.h:382
PGSemaphore sem
Definition: proc.h:90
#define PG_USED_FOR_ASSERTS_ONLY
Definition: c.h:986
Definition: proc.h:84
#define LOG_LWDEBUG(a, b, c)
Definition: lwlock.c:228
void RequestNamedLWLockTranche ( const char *  tranche_name,
int  num_lwlocks 
)

Definition at line 626 of file lwlock.c.

References Assert, i, IsUnderPostmaster, lock_named_request_allowed, MemoryContextAlloc(), NAMEDATALEN, NamedLWLockTrancheRequests, NamedLWLockTrancheRequestsAllocated, NULL, NamedLWLockTrancheRequest::num_lwlocks, repalloc(), StrNCpy, TopMemoryContext, and NamedLWLockTrancheRequest::tranche_name.

Referenced by _PG_init().

627 {
628  NamedLWLockTrancheRequest *request;
629 
631  return; /* too late */
632 
634  {
639  * sizeof(NamedLWLockTrancheRequest));
640  }
641 
643  {
645 
646  while (i <= NamedLWLockTrancheRequests)
647  i *= 2;
648 
651  i * sizeof(NamedLWLockTrancheRequest));
653  }
654 
656  Assert(strlen(tranche_name) + 1 < NAMEDATALEN);
657  StrNCpy(request->tranche_name, tranche_name, NAMEDATALEN);
658  request->num_lwlocks = num_lwlocks;
660 }
static bool lock_named_request_allowed
Definition: lwlock.c:158
char tranche_name[NAMEDATALEN]
Definition: lwlock.c:148
#define NAMEDATALEN
int NamedLWLockTrancheRequests
Definition: lwlock.c:154
bool IsUnderPostmaster
Definition: globals.c:100
MemoryContext TopMemoryContext
Definition: mcxt.c:43
static int NamedLWLockTrancheRequestsAllocated
Definition: lwlock.c:153
#define NULL
Definition: c.h:226
#define Assert(condition)
Definition: c.h:671
#define StrNCpy(dst, src, len)
Definition: c.h:826
void * repalloc(void *pointer, Size size)
Definition: mcxt.c:1021
NamedLWLockTrancheRequest * NamedLWLockTrancheRequestArray
Definition: lwlock.c:152
void * MemoryContextAlloc(MemoryContext context, Size size)
Definition: mcxt.c:749
int i

Variable Documentation

PGDLLIMPORT LWLockPadded* MainLWLockArray

Definition at line 125 of file lwlock.c.

char* MainLWLockNames[]

Referenced by RegisterLWLockTranches().

PGDLLIMPORT NamedLWLockTranche* NamedLWLockTrancheArray

Definition at line 156 of file lwlock.c.