Исходный код СУБД RuDB — свободной СУБД с открытым исходным кодом на базе PostgreSQL
rudb
3 days ago bd039d1e92813358c5c5a1e346b49074baf513f2
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
/*--------------------------------------------------------------------------
 *
 * test_lwlock_tranches.c
 *      Test code for LWLock tranches allocated by extensions.
 *
 * Copyright (c) 2025-2026, PostgreSQL Global Development Group
 *
 * IDENTIFICATION
 *      src/test/modules/test_lwlock_tranches/test_lwlock_tranches.c
 *
 * -------------------------------------------------------------------------
 */
 
#include "postgres.h"
 
#include "fmgr.h"
#include "miscadmin.h"
#include "storage/lwlock.h"
#include "utils/builtins.h"
#include "utils/wait_classes.h"
 
PG_MODULE_MAGIC;
 
static shmem_request_hook_type prev_shmem_request_hook;
static void test_lwlock_tranches_shmem_request(void);
 
void
_PG_init(void)
{
    prev_shmem_request_hook = shmem_request_hook;
    shmem_request_hook = test_lwlock_tranches_shmem_request;
}
 
static void
test_lwlock_tranches_shmem_request(void)
{
    if (prev_shmem_request_hook)
        prev_shmem_request_hook();
 
    /*
     * Request some tranches with RequestNamedLWLockTranche() for
     * test_startup_lwlocks()
     */
    RequestNamedLWLockTranche("test_lwlock_tranches_startup", 1);
    RequestNamedLWLockTranche("test_lwlock_tranches_startup10", 10);
}
 
/*
 * Test locks requested with RequestNamedLWLockTranche
 */
PG_FUNCTION_INFO_V1(test_startup_lwlocks);
Datum
test_startup_lwlocks(PG_FUNCTION_ARGS)
{
    LWLockPadded *lwlock_startup;
    LWLockPadded *lwlock_startup10;
 
    /* Check that the locks can be used */
    lwlock_startup = GetNamedLWLockTranche("test_lwlock_tranches_startup");
    lwlock_startup10 = GetNamedLWLockTranche("test_lwlock_tranches_startup10");
 
    LWLockAcquire(&lwlock_startup->lock, LW_EXCLUSIVE);
    for (int i = 0; i < 10; i++)
        LWLockAcquire(&lwlock_startup10[i].lock, LW_EXCLUSIVE);
 
    LWLockRelease(&lwlock_startup->lock);
    for (int i = 0; i < 10; i++)
        LWLockRelease(&lwlock_startup10[i].lock);
 
    /*
     * Check that GetLWLockIdentifier() returns the expected value for
     * tranches
     */
    if (strcmp("test_lwlock_tranches_startup",
               GetLWLockIdentifier(PG_WAIT_LWLOCK, lwlock_startup->lock.tranche)) != 0)
        elog(ERROR, "incorrect startup lock tranche name");
    if (strcmp("test_lwlock_tranches_startup10",
               GetLWLockIdentifier(PG_WAIT_LWLOCK, lwlock_startup10->lock.tranche)) != 0)
        elog(ERROR, "incorrect startup lock tranche name");
 
    PG_RETURN_VOID();
}
 
/*
 * Wrapper for LWLockNewTrancheId().
 */
PG_FUNCTION_INFO_V1(test_lwlock_tranche_create);
Datum
test_lwlock_tranche_create(PG_FUNCTION_ARGS)
{
    char       *tranche_name = PG_ARGISNULL(0) ? NULL : TextDatumGetCString(PG_GETARG_DATUM(0));
    int         tranche_id;
 
    tranche_id = LWLockNewTrancheId(tranche_name);
 
    PG_RETURN_INT32(tranche_id);
}
 
/*
 * Wrapper for GetNamedLWLockTranche().
 */
PG_FUNCTION_INFO_V1(test_lwlock_tranche_lookup);
Datum
test_lwlock_tranche_lookup(PG_FUNCTION_ARGS)
{
    char       *tranche_name = TextDatumGetCString(PG_GETARG_DATUM(0));
 
    (void) GetNamedLWLockTranche(tranche_name);
 
    PG_RETURN_VOID();
}
 
PG_FUNCTION_INFO_V1(test_lwlock_get_lwlock_identifier);
Datum
test_lwlock_get_lwlock_identifier(PG_FUNCTION_ARGS)
{
    int         eventId = PG_GETARG_INT32(0);
    const char *tranche_name;
 
    tranche_name = GetLWLockIdentifier(PG_WAIT_LWLOCK, eventId);
 
    if (tranche_name)
        PG_RETURN_TEXT_P(cstring_to_text(tranche_name));
    else
        PG_RETURN_NULL();
}
 
/*
 * Wrapper for LWLockInitialize().
 */
PG_FUNCTION_INFO_V1(test_lwlock_initialize);
Datum
test_lwlock_initialize(PG_FUNCTION_ARGS)
{
    int         tranche_id = PG_GETARG_INT32(0);
    LWLock      lock;
 
    LWLockInitialize(&lock, tranche_id);
 
    PG_RETURN_VOID();
}