🌐 AI搜索 & 代理 主页
Skip to content

Commit 48d4a14

Browse files
Allow passing a pointer to GetNamedDSMSegment()'s init callback.
This commit adds a new "void *arg" parameter to GetNamedDSMSegment() that is passed to the initialization callback function. This is useful for reusing an initialization callback function for multiple DSM segments. Author: Zsolt Parragi <zsolt.parragi@percona.com> Reviewed-by: Sami Imseih <samimseih@gmail.com> Discussion: https://postgr.es/m/CAN4CZFMjh8TrT9ZhWgjVTzBDkYZi2a84BnZ8bM%2BfLPuq7Cirzg%40mail.gmail.com
1 parent 64bf53d commit 48d4a14

File tree

7 files changed

+26
-18
lines changed

7 files changed

+26
-18
lines changed

contrib/pg_prewarm/autoprewarm.c

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -858,7 +858,7 @@ autoprewarm_dump_now(PG_FUNCTION_ARGS)
858858
}
859859

860860
static void
861-
apw_init_state(void *ptr)
861+
apw_init_state(void *ptr, void *arg)
862862
{
863863
AutoPrewarmSharedState *state = (AutoPrewarmSharedState *) ptr;
864864

@@ -880,7 +880,7 @@ apw_init_shmem(void)
880880
apw_state = GetNamedDSMSegment("autoprewarm",
881881
sizeof(AutoPrewarmSharedState),
882882
apw_init_state,
883-
&found);
883+
&found, NULL);
884884

885885
return found;
886886
}

doc/src/sgml/xfunc.sgml

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -3696,15 +3696,18 @@ LWLockRelease(AddinShmemInitLock);
36963696
use the shared memory should obtain a pointer to it by calling:
36973697
<programlisting>
36983698
void *GetNamedDSMSegment(const char *name, size_t size,
3699-
void (*init_callback) (void *ptr),
3700-
bool *found)
3699+
void (*init_callback) (void *ptr, void *arg),
3700+
bool *found, void *arg)
37013701
</programlisting>
37023702
If a dynamic shared memory segment with the given name does not yet
37033703
exist, this function will allocate it and initialize it with the provided
37043704
<function>init_callback</function> callback function. If the segment has
37053705
already been allocated and initialized by another backend, this function
37063706
simply attaches the existing dynamic shared memory segment to the current
3707-
backend.
3707+
backend. In the former case, <function>GetNamedDSMSegment</function>
3708+
passes the <literal>void *arg</literal> argument to the
3709+
<function>init_callback</function>. This is particularly useful for
3710+
reusing an initialization callback function for multiple DSM segments.
37083711
</para>
37093712

37103713
<para>

src/backend/storage/ipc/dsm_registry.c

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -180,11 +180,13 @@ init_dsm_registry(void)
180180
* Initialize or attach a named DSM segment.
181181
*
182182
* This routine returns the address of the segment. init_callback is called to
183-
* initialize the segment when it is first created.
183+
* initialize the segment when it is first created. 'arg' is passed through to
184+
* the initialization callback function.
184185
*/
185186
void *
186187
GetNamedDSMSegment(const char *name, size_t size,
187-
void (*init_callback) (void *ptr), bool *found)
188+
void (*init_callback) (void *ptr, void *arg),
189+
bool *found, void *arg)
188190
{
189191
DSMRegistryEntry *entry;
190192
MemoryContext oldcontext;
@@ -235,7 +237,7 @@ GetNamedDSMSegment(const char *name, size_t size,
235237
seg = dsm_create(size, 0);
236238

237239
if (init_callback)
238-
(*init_callback) (dsm_segment_address(seg));
240+
(*init_callback) (dsm_segment_address(seg), arg);
239241

240242
dsm_pin_segment(seg);
241243
dsm_pin_mapping(seg);

src/include/storage/dsm_registry.h

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -16,8 +16,8 @@
1616
#include "lib/dshash.h"
1717

1818
extern void *GetNamedDSMSegment(const char *name, size_t size,
19-
void (*init_callback) (void *ptr),
20-
bool *found);
19+
void (*init_callback) (void *ptr, void *arg),
20+
bool *found, void *arg);
2121
extern dsa_area *GetNamedDSA(const char *name, bool *found);
2222
extern dshash_table *GetNamedDSHash(const char *name,
2323
const dshash_parameters *params,

src/test/modules/injection_points/injection_points.c

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -115,7 +115,7 @@ static shmem_startup_hook_type prev_shmem_startup_hook = NULL;
115115
* when initializing dynamically with a DSM or when loading the module.
116116
*/
117117
static void
118-
injection_point_init_state(void *ptr)
118+
injection_point_init_state(void *ptr, void *arg)
119119
{
120120
InjectionPointSharedState *state = (InjectionPointSharedState *) ptr;
121121

@@ -159,7 +159,7 @@ injection_shmem_startup(void)
159159
* First time through, so initialize. This is shared with the dynamic
160160
* initialization using a DSM.
161161
*/
162-
injection_point_init_state(inj_state);
162+
injection_point_init_state(inj_state, NULL);
163163
}
164164

165165
LWLockRelease(AddinShmemInitLock);
@@ -179,7 +179,7 @@ injection_init_shmem(void)
179179
inj_state = GetNamedDSMSegment("injection_points",
180180
sizeof(InjectionPointSharedState),
181181
injection_point_init_state,
182-
&found);
182+
&found, NULL);
183183
}
184184

185185
/*

src/test/modules/test_dsa/test_dsa.c

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,7 @@
2121
PG_MODULE_MAGIC;
2222

2323
static void
24-
init_tranche(void *ptr)
24+
init_tranche(void *ptr, void *arg)
2525
{
2626
int *tranche_id = (int *) ptr;
2727

@@ -39,7 +39,7 @@ test_dsa_basic(PG_FUNCTION_ARGS)
3939
dsa_pointer p[100];
4040

4141
tranche_id = GetNamedDSMSegment("test_dsa", sizeof(int),
42-
init_tranche, &found);
42+
init_tranche, &found, NULL);
4343

4444
a = dsa_create(*tranche_id);
4545
for (int i = 0; i < 100; i++)
@@ -80,7 +80,7 @@ test_dsa_resowners(PG_FUNCTION_ARGS)
8080
ResourceOwner childowner;
8181

8282
tranche_id = GetNamedDSMSegment("test_dsa", sizeof(int),
83-
init_tranche, &found);
83+
init_tranche, &found, NULL);
8484

8585
/* Create DSA in parent resource owner */
8686
a = dsa_create(*tranche_id);

src/test/modules/test_dsm_registry/test_dsm_registry.c

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -44,10 +44,13 @@ static const dshash_parameters dsh_params = {
4444
};
4545

4646
static void
47-
init_tdr_dsm(void *ptr)
47+
init_tdr_dsm(void *ptr, void *arg)
4848
{
4949
TestDSMRegistryStruct *dsm = (TestDSMRegistryStruct *) ptr;
5050

51+
if ((int) (intptr_t) arg != 5432)
52+
elog(ERROR, "unexpected arg value %d", (int) (intptr_t) arg);
53+
5154
LWLockInitialize(&dsm->lck, LWLockNewTrancheId("test_dsm_registry"));
5255
dsm->val = 0;
5356
}
@@ -60,7 +63,7 @@ tdr_attach_shmem(void)
6063
tdr_dsm = GetNamedDSMSegment("test_dsm_registry_dsm",
6164
sizeof(TestDSMRegistryStruct),
6265
init_tdr_dsm,
63-
&found);
66+
&found, (void *) (intptr_t) 5432);
6467

6568
if (tdr_dsa == NULL)
6669
tdr_dsa = GetNamedDSA("test_dsm_registry_dsa", &found);

0 commit comments

Comments
 (0)