Line data Source code
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 :
22 0 : PG_MODULE_MAGIC;
23 :
24 : #define STARTUP_TRANCHE_NAME "test_lwlock_tranches_startup"
25 : #define DYNAMIC_TRANCHE_NAME "test_lwlock_tranches_dynamic"
26 :
27 : #define NUM_STARTUP_TRANCHES (32)
28 : #define NUM_DYNAMIC_TRANCHES (256 - NUM_STARTUP_TRANCHES)
29 :
30 : #define GET_TRANCHE_NAME(a) GetLWLockIdentifier(PG_WAIT_LWLOCK, (a))
31 :
32 : static shmem_request_hook_type prev_shmem_request_hook;
33 : static void test_lwlock_tranches_shmem_request(void);
34 :
35 : void
36 0 : _PG_init(void)
37 : {
38 0 : prev_shmem_request_hook = shmem_request_hook;
39 0 : shmem_request_hook = test_lwlock_tranches_shmem_request;
40 0 : }
41 :
42 : static void
43 0 : test_lwlock_tranches_shmem_request(void)
44 : {
45 0 : if (prev_shmem_request_hook)
46 0 : prev_shmem_request_hook();
47 :
48 0 : for (int i = 0; i < NUM_STARTUP_TRANCHES; i++)
49 0 : RequestNamedLWLockTranche(STARTUP_TRANCHE_NAME, 1);
50 0 : }
51 :
52 : /*
53 : * Checks that GetLWLockIdentifier() returns the expected value for tranches
54 : * registered via RequestNamedLWLockTranche() and LWLockNewTrancheId().
55 : */
56 0 : PG_FUNCTION_INFO_V1(test_lwlock_tranches);
57 : Datum
58 0 : test_lwlock_tranches(PG_FUNCTION_ARGS)
59 : {
60 0 : int dynamic_tranches[NUM_DYNAMIC_TRANCHES];
61 :
62 0 : for (int i = 0; i < NUM_DYNAMIC_TRANCHES; i++)
63 0 : dynamic_tranches[i] = LWLockNewTrancheId(DYNAMIC_TRANCHE_NAME);
64 :
65 0 : for (int i = 0; i < NUM_STARTUP_TRANCHES; i++)
66 : {
67 0 : if (strcmp(GET_TRANCHE_NAME(LWTRANCHE_FIRST_USER_DEFINED + i),
68 0 : STARTUP_TRANCHE_NAME) != 0)
69 0 : elog(ERROR, "incorrect startup lock tranche name");
70 0 : }
71 :
72 0 : for (int i = 0; i < NUM_DYNAMIC_TRANCHES; i++)
73 : {
74 0 : if (strcmp(GET_TRANCHE_NAME(dynamic_tranches[i]),
75 0 : DYNAMIC_TRANCHE_NAME) != 0)
76 0 : elog(ERROR, "incorrect dynamic lock tranche name");
77 0 : }
78 :
79 0 : PG_RETURN_VOID();
80 0 : }
81 :
82 : /*
83 : * Wrapper for LWLockNewTrancheId().
84 : */
85 0 : PG_FUNCTION_INFO_V1(test_lwlock_tranche_creation);
86 : Datum
87 0 : test_lwlock_tranche_creation(PG_FUNCTION_ARGS)
88 : {
89 0 : char *tranche_name = PG_ARGISNULL(0) ? NULL : TextDatumGetCString(PG_GETARG_DATUM(0));
90 :
91 0 : (void) LWLockNewTrancheId(tranche_name);
92 :
93 0 : PG_RETURN_VOID();
94 0 : }
95 :
96 : /*
97 : * Wrapper for GetNamedLWLockTranche().
98 : */
99 0 : PG_FUNCTION_INFO_V1(test_lwlock_tranche_lookup);
100 : Datum
101 0 : test_lwlock_tranche_lookup(PG_FUNCTION_ARGS)
102 : {
103 0 : char *tranche_name = TextDatumGetCString(PG_GETARG_DATUM(0));
104 :
105 0 : (void) GetNamedLWLockTranche(tranche_name);
106 :
107 0 : PG_RETURN_VOID();
108 0 : }
109 :
110 : /*
111 : * Wrapper for LWLockInitialize().
112 : */
113 0 : PG_FUNCTION_INFO_V1(test_lwlock_initialize);
114 : Datum
115 0 : test_lwlock_initialize(PG_FUNCTION_ARGS)
116 : {
117 0 : int tranche_id = PG_GETARG_INT32(0);
118 0 : LWLock lock;
119 :
120 0 : LWLockInitialize(&lock, tranche_id);
121 :
122 0 : PG_RETURN_VOID();
123 0 : }
|