Commit 3fed4174 authored by Robert Haas's avatar Robert Haas

Provide a way to predefine LWLock tranche IDs.

It's a bit cumbersome to use LWLockNewTrancheId(), because the returned
value needs to be shared between backends so that each backend can call
LWLockRegisterTranche() with the correct ID.  So, for built-in tranches,
use a hard-coded value instead.

This is motivated by an upcoming patch adding further built-in tranches.

Andres Freund and Robert Haas
parent 43cd468c
...@@ -512,7 +512,6 @@ typedef struct XLogCtlInsert ...@@ -512,7 +512,6 @@ typedef struct XLogCtlInsert
*/ */
WALInsertLockPadded *WALInsertLocks; WALInsertLockPadded *WALInsertLocks;
LWLockTranche WALInsertLockTranche; LWLockTranche WALInsertLockTranche;
int WALInsertLockTrancheId;
} XLogCtlInsert; } XLogCtlInsert;
/* /*
...@@ -4653,7 +4652,7 @@ XLOGShmemInit(void) ...@@ -4653,7 +4652,7 @@ XLOGShmemInit(void)
/* Initialize local copy of WALInsertLocks and register the tranche */ /* Initialize local copy of WALInsertLocks and register the tranche */
WALInsertLocks = XLogCtl->Insert.WALInsertLocks; WALInsertLocks = XLogCtl->Insert.WALInsertLocks;
LWLockRegisterTranche(XLogCtl->Insert.WALInsertLockTrancheId, LWLockRegisterTranche(LWTRANCHE_WAL_INSERT,
&XLogCtl->Insert.WALInsertLockTranche); &XLogCtl->Insert.WALInsertLockTranche);
return; return;
} }
...@@ -4677,17 +4676,14 @@ XLOGShmemInit(void) ...@@ -4677,17 +4676,14 @@ XLOGShmemInit(void)
(WALInsertLockPadded *) allocptr; (WALInsertLockPadded *) allocptr;
allocptr += sizeof(WALInsertLockPadded) * NUM_XLOGINSERT_LOCKS; allocptr += sizeof(WALInsertLockPadded) * NUM_XLOGINSERT_LOCKS;
XLogCtl->Insert.WALInsertLockTrancheId = LWLockNewTrancheId();
XLogCtl->Insert.WALInsertLockTranche.name = "WALInsertLocks"; XLogCtl->Insert.WALInsertLockTranche.name = "WALInsertLocks";
XLogCtl->Insert.WALInsertLockTranche.array_base = WALInsertLocks; XLogCtl->Insert.WALInsertLockTranche.array_base = WALInsertLocks;
XLogCtl->Insert.WALInsertLockTranche.array_stride = sizeof(WALInsertLockPadded); XLogCtl->Insert.WALInsertLockTranche.array_stride = sizeof(WALInsertLockPadded);
LWLockRegisterTranche(XLogCtl->Insert.WALInsertLockTrancheId, &XLogCtl->Insert.WALInsertLockTranche); LWLockRegisterTranche(LWTRANCHE_WAL_INSERT, &XLogCtl->Insert.WALInsertLockTranche);
for (i = 0; i < NUM_XLOGINSERT_LOCKS; i++) for (i = 0; i < NUM_XLOGINSERT_LOCKS; i++)
{ {
LWLockInitialize(&WALInsertLocks[i].l.lock, LWLockInitialize(&WALInsertLocks[i].l.lock, LWTRANCHE_WAL_INSERT);
XLogCtl->Insert.WALInsertLockTrancheId);
WALInsertLocks[i].l.insertingAt = InvalidXLogRecPtr; WALInsertLocks[i].l.insertingAt = InvalidXLogRecPtr;
} }
......
...@@ -445,7 +445,7 @@ CreateLWLocks(void) ...@@ -445,7 +445,7 @@ CreateLWLocks(void)
/* Initialize all LWLocks in main array */ /* Initialize all LWLocks in main array */
for (id = 0, lock = MainLWLockArray; id < numLocks; id++, lock++) for (id = 0, lock = MainLWLockArray; id < numLocks; id++, lock++)
LWLockInitialize(&lock->lock, 0); LWLockInitialize(&lock->lock, LWTRANCHE_MAIN);
/* /*
* Initialize the dynamic-allocation counters, which are stored just * Initialize the dynamic-allocation counters, which are stored just
...@@ -457,7 +457,7 @@ CreateLWLocks(void) ...@@ -457,7 +457,7 @@ CreateLWLocks(void)
LWLockCounter = (int *) ((char *) MainLWLockArray - 3 * sizeof(int)); LWLockCounter = (int *) ((char *) MainLWLockArray - 3 * sizeof(int));
LWLockCounter[0] = NUM_FIXED_LWLOCKS; LWLockCounter[0] = NUM_FIXED_LWLOCKS;
LWLockCounter[1] = numLocks; LWLockCounter[1] = numLocks;
LWLockCounter[2] = 1; /* 0 is the main array */ LWLockCounter[2] = LWTRANCHE_FIRST_USER_DEFINED;
} }
if (LWLockTrancheArray == NULL) if (LWLockTrancheArray == NULL)
...@@ -466,12 +466,13 @@ CreateLWLocks(void) ...@@ -466,12 +466,13 @@ CreateLWLocks(void)
LWLockTrancheArray = (LWLockTranche **) LWLockTrancheArray = (LWLockTranche **)
MemoryContextAlloc(TopMemoryContext, MemoryContextAlloc(TopMemoryContext,
LWLockTranchesAllocated * sizeof(LWLockTranche *)); LWLockTranchesAllocated * sizeof(LWLockTranche *));
Assert(LWLockTranchesAllocated >= LWTRANCHE_FIRST_USER_DEFINED);
} }
MainLWLockTranche.name = "main"; MainLWLockTranche.name = "main";
MainLWLockTranche.array_base = MainLWLockArray; MainLWLockTranche.array_base = MainLWLockArray;
MainLWLockTranche.array_stride = sizeof(LWLockPadded); MainLWLockTranche.array_stride = sizeof(LWLockPadded);
LWLockRegisterTranche(0, &MainLWLockTranche); LWLockRegisterTranche(LWTRANCHE_MAIN, &MainLWLockTranche);
} }
/* /*
......
...@@ -176,6 +176,17 @@ extern int LWLockNewTrancheId(void); ...@@ -176,6 +176,17 @@ extern int LWLockNewTrancheId(void);
extern void LWLockRegisterTranche(int tranche_id, LWLockTranche *tranche); extern void LWLockRegisterTranche(int tranche_id, LWLockTranche *tranche);
extern void LWLockInitialize(LWLock *lock, int tranche_id); extern void LWLockInitialize(LWLock *lock, int tranche_id);
/*
* We reserve a few predefined tranche IDs. A call to LWLockNewTrancheId
* will never return a value less than LWTRANCHE_FIRST_USER_DEFINED.
*/
typedef enum BuiltinTrancheIds
{
LWTRANCHE_MAIN,
LWTRANCHE_WAL_INSERT,
LWTRANCHE_FIRST_USER_DEFINED
} BuiltinTrancheIds;
/* /*
* Prior to PostgreSQL 9.4, we used an enum type called LWLockId to refer * Prior to PostgreSQL 9.4, we used an enum type called LWLockId to refer
* to LWLocks. New code should instead use LWLock *. However, for the * to LWLocks. New code should instead use LWLock *. However, for the
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment