Location via proxy:   [ UP ]  
[Report a bug]   [Manage cookies]                
Skip to content

Commit b2b023a

Browse files
committed
injection_points: Add initialization of shmem state when loading module
This commits adds callbacks to initialize the shared memory state of the module when loaded with shared_preload_libraries. This is necessary to be able to update the test introduced in 768a9fd to use the macros INJECTION_POINT_{LOAD,CACHED}() rather than a SQL function in the module injection_points forcing a load, as this test runs a callback in a critical section where no memory allocation should happen. Initializing the shared memory state of the module while loading provides a strict control on the timing of its allocation. If the module is not loaded at startup, it will use a GetNamedDSMSegment() instead to initialize its shmem state on-the-fly. Per discussion with Álvaro Herrera. Author: Michael Paquier Discussion: https://postgr.es/m/ZsUnJUlSOBNAzwW1@paquier.xyz
1 parent edcb712 commit b2b023a

File tree

1 file changed

+59
-3
lines changed

1 file changed

+59
-3
lines changed

src/test/modules/injection_points/injection_points.c

Lines changed: 59 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -68,7 +68,12 @@ typedef struct InjectionPointCondition
6868
*/
6969
static List *inj_list_local = NIL;
7070

71-
/* Shared state information for injection points. */
71+
/*
72+
* Shared state information for injection points.
73+
*
74+
* This state data can be initialized in two ways: dynamically with a DSM
75+
* or when loading the module.
76+
*/
7277
typedef struct InjectionPointSharedState
7378
{
7479
/* Protects access to other fields */
@@ -97,8 +102,13 @@ extern PGDLLEXPORT void injection_wait(const char *name,
97102
/* track if injection points attached in this process are linked to it */
98103
static bool injection_point_local = false;
99104

105+
/* Shared memory init callbacks */
106+
static shmem_request_hook_type prev_shmem_request_hook = NULL;
107+
static shmem_startup_hook_type prev_shmem_startup_hook = NULL;
108+
100109
/*
101-
* Callback for shared memory area initialization.
110+
* Routine for shared memory area initialization, used as a callback
111+
* when initializing dynamically with a DSM or when loading the module.
102112
*/
103113
static void
104114
injection_point_init_state(void *ptr)
@@ -111,8 +121,48 @@ injection_point_init_state(void *ptr)
111121
ConditionVariableInit(&state->wait_point);
112122
}
113123

124+
/* Shared memory initialization when loading module */
125+
static void
126+
injection_shmem_request(void)
127+
{
128+
Size size;
129+
130+
if (prev_shmem_request_hook)
131+
prev_shmem_request_hook();
132+
133+
size = MAXALIGN(sizeof(InjectionPointSharedState));
134+
RequestAddinShmemSpace(size);
135+
}
136+
137+
static void
138+
injection_shmem_startup(void)
139+
{
140+
bool found;
141+
142+
if (prev_shmem_startup_hook)
143+
prev_shmem_startup_hook();
144+
145+
/* Create or attach to the shared memory state */
146+
LWLockAcquire(AddinShmemInitLock, LW_EXCLUSIVE);
147+
148+
inj_state = ShmemInitStruct("injection_points",
149+
sizeof(InjectionPointSharedState),
150+
&found);
151+
152+
if (!found)
153+
{
154+
/*
155+
* First time through, so initialize. This is shared with the dynamic
156+
* initialization using a DSM.
157+
*/
158+
injection_point_init_state(inj_state);
159+
}
160+
161+
LWLockRelease(AddinShmemInitLock);
162+
}
163+
114164
/*
115-
* Initialize shared memory area for this module.
165+
* Initialize shared memory area for this module through DSM.
116166
*/
117167
static void
118168
injection_init_shmem(void)
@@ -463,6 +513,12 @@ _PG_init(void)
463513
if (!process_shared_preload_libraries_in_progress)
464514
return;
465515

516+
/* Shared memory initialization */
517+
prev_shmem_request_hook = shmem_request_hook;
518+
shmem_request_hook = injection_shmem_request;
519+
prev_shmem_startup_hook = shmem_startup_hook;
520+
shmem_startup_hook = injection_shmem_startup;
521+
466522
pgstat_register_inj();
467523
pgstat_register_inj_fixed();
468524
}

0 commit comments

Comments
 (0)