PostgreSQL Source Code git master
Loading...
Searching...
No Matches
test_lwlock_tranches.c
Go to the documentation of this file.
1/*--------------------------------------------------------------------------
2 *
3 * test_lwlock_tranches.c
4 * Test code for LWLock tranches allocated by extensions.
5 *
6 * Copyright (c) 2025-2026, PostgreSQL Global Development Group
7 *
8 * IDENTIFICATION
9 * src/test/modules/test_lwlock_tranches/test_lwlock_tranches.c
10 *
11 * -------------------------------------------------------------------------
12 */
13
14#include "postgres.h"
15
16#include "fmgr.h"
17#include "miscadmin.h"
18#include "storage/lwlock.h"
19#include "utils/builtins.h"
20#include "utils/wait_classes.h"
21
23
26
27void
33
34static void
36{
39
40 /*
41 * Request some tranches with RequestNamedLWLockTranche() for
42 * test_startup_lwlocks()
43 */
44 RequestNamedLWLockTranche("test_lwlock_tranches_startup", 1);
45 RequestNamedLWLockTranche("test_lwlock_tranches_startup10", 10);
46}
47
48/*
49 * Test locks requested with RequestNamedLWLockTranche
50 */
54{
57
58 /* Check that the locks can be used */
59 lwlock_startup = GetNamedLWLockTranche("test_lwlock_tranches_startup");
60 lwlock_startup10 = GetNamedLWLockTranche("test_lwlock_tranches_startup10");
61
63 for (int i = 0; i < 10; i++)
65
67 for (int i = 0; i < 10; i++)
69
70 /*
71 * Check that GetLWLockIdentifier() returns the expected value for
72 * tranches
73 */
74 if (strcmp("test_lwlock_tranches_startup",
76 elog(ERROR, "incorrect startup lock tranche name");
77 if (strcmp("test_lwlock_tranches_startup10",
79 elog(ERROR, "incorrect startup lock tranche name");
80
82}
83
84/*
85 * Wrapper for LWLockNewTrancheId().
86 */
90{
91 char *tranche_name = PG_ARGISNULL(0) ? NULL : TextDatumGetCString(PG_GETARG_DATUM(0));
92 int tranche_id;
93
94 tranche_id = LWLockNewTrancheId(tranche_name);
95
96 PG_RETURN_INT32(tranche_id);
97}
98
99/*
100 * Wrapper for GetNamedLWLockTranche().
101 */
103Datum
105{
106 char *tranche_name = TextDatumGetCString(PG_GETARG_DATUM(0));
107
108 (void) GetNamedLWLockTranche(tranche_name);
109
111}
112
114Datum
116{
117 int eventId = PG_GETARG_INT32(0);
118 const char *tranche_name;
119
121
122 if (tranche_name)
123 PG_RETURN_TEXT_P(cstring_to_text(tranche_name));
124 else
126}
127
128/*
129 * Wrapper for LWLockInitialize().
130 */
132Datum
134{
135 int tranche_id = PG_GETARG_INT32(0);
136 LWLock lock;
137
138 LWLockInitialize(&lock, tranche_id);
139
141}
#define TextDatumGetCString(d)
Definition builtins.h:99
#define ERROR
Definition elog.h:40
#define elog(elevel,...)
Definition elog.h:228
#define PG_RETURN_VOID()
Definition fmgr.h:350
#define PG_ARGISNULL(n)
Definition fmgr.h:209
#define PG_GETARG_DATUM(n)
Definition fmgr.h:268
#define PG_RETURN_NULL()
Definition fmgr.h:346
#define PG_FUNCTION_INFO_V1(funcname)
Definition fmgr.h:417
#define PG_RETURN_TEXT_P(x)
Definition fmgr.h:374
#define PG_RETURN_INT32(x)
Definition fmgr.h:355
#define PG_GETARG_INT32(n)
Definition fmgr.h:269
#define PG_FUNCTION_ARGS
Definition fmgr.h:193
int i
Definition isn.c:77
bool LWLockAcquire(LWLock *lock, LWLockMode mode)
Definition lwlock.c:1150
int LWLockNewTrancheId(const char *name)
Definition lwlock.c:562
LWLockPadded * GetNamedLWLockTranche(const char *tranche_name)
Definition lwlock.c:522
void RequestNamedLWLockTranche(const char *tranche_name, int num_lwlocks)
Definition lwlock.c:620
void LWLockRelease(LWLock *lock)
Definition lwlock.c:1767
void LWLockInitialize(LWLock *lock, int tranche_id)
Definition lwlock.c:670
const char * GetLWLockIdentifier(uint32 classId, uint16 eventId)
Definition lwlock.c:747
@ LWTRANCHE_FIRST_USER_DEFINED
Definition lwlock.h:172
@ LW_EXCLUSIVE
Definition lwlock.h:104
void(* shmem_request_hook_type)(void)
Definition miscadmin.h:552
shmem_request_hook_type shmem_request_hook
Definition miscinit.c:1791
uint64_t Datum
Definition postgres.h:70
static int fb(int x)
void _PG_init(void)
Datum test_lwlock_tranche_create(PG_FUNCTION_ARGS)
static shmem_request_hook_type prev_shmem_request_hook
Datum test_lwlock_tranche_lookup(PG_FUNCTION_ARGS)
Datum test_lwlock_get_lwlock_identifier(PG_FUNCTION_ARGS)
static void test_lwlock_tranches_shmem_request(void)
Datum test_startup_lwlocks(PG_FUNCTION_ARGS)
Datum test_lwlock_initialize(PG_FUNCTION_ARGS)
text * cstring_to_text(const char *s)
Definition varlena.c:184
#define PG_WAIT_LWLOCK