}
static void
-apw_init_state(void *ptr)
+apw_init_state(void *ptr, void *arg)
{
AutoPrewarmSharedState *state = (AutoPrewarmSharedState *) ptr;
apw_state = GetNamedDSMSegment("autoprewarm",
sizeof(AutoPrewarmSharedState),
apw_init_state,
- &found);
+ &found, NULL);
return found;
}
use the shared memory should obtain a pointer to it by calling:
<programlisting>
void *GetNamedDSMSegment(const char *name, size_t size,
- void (*init_callback) (void *ptr),
- bool *found)
+ void (*init_callback) (void *ptr, void *arg),
+ bool *found, void *arg)
</programlisting>
If a dynamic shared memory segment with the given name does not yet
exist, this function will allocate it and initialize it with the provided
<function>init_callback</function> callback function. If the segment has
already been allocated and initialized by another backend, this function
simply attaches the existing dynamic shared memory segment to the current
- backend.
+ backend. In the former case, <function>GetNamedDSMSegment</function>
+ passes the <literal>void *arg</literal> argument to the
+ <function>init_callback</function>. This is particularly useful for
+ reusing an initialization callback function for multiple DSM segments.
</para>
<para>
* Initialize or attach a named DSM segment.
*
* This routine returns the address of the segment. init_callback is called to
- * initialize the segment when it is first created.
+ * initialize the segment when it is first created. 'arg' is passed through to
+ * the initialization callback function.
*/
void *
GetNamedDSMSegment(const char *name, size_t size,
- void (*init_callback) (void *ptr), bool *found)
+ void (*init_callback) (void *ptr, void *arg),
+ bool *found, void *arg)
{
DSMRegistryEntry *entry;
MemoryContext oldcontext;
seg = dsm_create(size, 0);
if (init_callback)
- (*init_callback) (dsm_segment_address(seg));
+ (*init_callback) (dsm_segment_address(seg), arg);
dsm_pin_segment(seg);
dsm_pin_mapping(seg);
#include "lib/dshash.h"
extern void *GetNamedDSMSegment(const char *name, size_t size,
- void (*init_callback) (void *ptr),
- bool *found);
+ void (*init_callback) (void *ptr, void *arg),
+ bool *found, void *arg);
extern dsa_area *GetNamedDSA(const char *name, bool *found);
extern dshash_table *GetNamedDSHash(const char *name,
const dshash_parameters *params,
* when initializing dynamically with a DSM or when loading the module.
*/
static void
-injection_point_init_state(void *ptr)
+injection_point_init_state(void *ptr, void *arg)
{
InjectionPointSharedState *state = (InjectionPointSharedState *) ptr;
* First time through, so initialize. This is shared with the dynamic
* initialization using a DSM.
*/
- injection_point_init_state(inj_state);
+ injection_point_init_state(inj_state, NULL);
}
LWLockRelease(AddinShmemInitLock);
inj_state = GetNamedDSMSegment("injection_points",
sizeof(InjectionPointSharedState),
injection_point_init_state,
- &found);
+ &found, NULL);
}
/*
PG_MODULE_MAGIC;
static void
-init_tranche(void *ptr)
+init_tranche(void *ptr, void *arg)
{
int *tranche_id = (int *) ptr;
dsa_pointer p[100];
tranche_id = GetNamedDSMSegment("test_dsa", sizeof(int),
- init_tranche, &found);
+ init_tranche, &found, NULL);
a = dsa_create(*tranche_id);
for (int i = 0; i < 100; i++)
ResourceOwner childowner;
tranche_id = GetNamedDSMSegment("test_dsa", sizeof(int),
- init_tranche, &found);
+ init_tranche, &found, NULL);
/* Create DSA in parent resource owner */
a = dsa_create(*tranche_id);
};
static void
-init_tdr_dsm(void *ptr)
+init_tdr_dsm(void *ptr, void *arg)
{
TestDSMRegistryStruct *dsm = (TestDSMRegistryStruct *) ptr;
+ if ((int) (intptr_t) arg != 5432)
+ elog(ERROR, "unexpected arg value %d", (int) (intptr_t) arg);
+
LWLockInitialize(&dsm->lck, LWLockNewTrancheId("test_dsm_registry"));
dsm->val = 0;
}
tdr_dsm = GetNamedDSMSegment("test_dsm_registry_dsm",
sizeof(TestDSMRegistryStruct),
init_tdr_dsm,
- &found);
+ &found, (void *) (intptr_t) 5432);
if (tdr_dsa == NULL)
tdr_dsa = GetNamedDSA("test_dsm_registry_dsa", &found);