Skip to content

Commit bd786ce

Browse files
committed
refactor: use MyProc->procLatch instead of new one
&MyProc->procLatch is already a shared latch, so reuse that one instead of creating a new shared latch.
1 parent ebc6738 commit bd786ce

File tree

2 files changed

+17
-18
lines changed

2 files changed

+17
-18
lines changed

src/core.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@ typedef struct {
1111
pg_atomic_uint32 got_restart;
1212
pg_atomic_uint32 should_wake;
1313
pg_atomic_uint32 status;
14-
Latch latch;
14+
Latch* shared_latch;
1515
ConditionVariable cv;
1616
int epfd;
1717
CURLM *curl_mhandle;

src/worker.c

Lines changed: 16 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -53,8 +53,8 @@ Datum worker_restart(__attribute__ ((unused)) PG_FUNCTION_ARGS) {
5353
bool result = DatumGetBool(DirectFunctionCall1(pg_reload_conf, (Datum) NULL)); // reload the config
5454
pg_atomic_write_u32(&worker_state->got_restart, 1);
5555
pg_write_barrier();
56-
if (worker_state)
57-
SetLatch(&worker_state->latch);
56+
if(worker_state->shared_latch)
57+
SetLatch(worker_state->shared_latch);
5858
PG_RETURN_BOOL(result); // TODO is not necessary to return a bool here, but we do it to maintain backward compatibility
5959
}
6060

@@ -90,7 +90,7 @@ static void wake_at_commit(XactEvent event, __attribute__ ((unused)) void *arg){
9090
pg_write_barrier();
9191

9292
if (success) // only wake the worker on first put, so if many concurrent wakes come we only wake once
93-
SetLatch(&worker_state->latch);
93+
SetLatch(worker_state->shared_latch);
9494

9595
wake_commit_cb_active = false;
9696
}
@@ -124,8 +124,8 @@ handle_sigterm(__attribute__ ((unused)) SIGNAL_ARGS)
124124
int save_errno = errno;
125125
pg_atomic_write_u32(&worker_state->got_restart, 1);
126126
pg_write_barrier();
127-
if (worker_state)
128-
SetLatch(&worker_state->latch);
127+
if(worker_state->shared_latch)
128+
SetLatch(worker_state->shared_latch);
129129
errno = save_errno;
130130
}
131131

@@ -134,8 +134,8 @@ handle_sighup(__attribute__ ((unused)) SIGNAL_ARGS)
134134
{
135135
int save_errno = errno;
136136
got_sighup = true;
137-
if (worker_state)
138-
SetLatch(&worker_state->latch);
137+
if(worker_state->shared_latch)
138+
SetLatch(worker_state->shared_latch);
139139
errno = save_errno;
140140
}
141141

@@ -149,8 +149,8 @@ static void
149149
handle_sigusr1(SIGNAL_ARGS)
150150
{
151151
int save_errno = errno;
152-
if (worker_state)
153-
SetLatch(&worker_state->latch);
152+
if(worker_state->shared_latch)
153+
SetLatch(worker_state->shared_latch);
154154
errno = save_errno;
155155
procsignal_sigusr1_handler(postgres_signal_arg);
156156
}
@@ -166,7 +166,7 @@ net_on_exit(__attribute__ ((unused)) int code, __attribute__ ((unused)) Datum ar
166166
worker_should_restart = false;
167167
pg_atomic_write_u32(&worker_state->should_wake, 1); // ensure the remaining work will continue since we'll restart
168168

169-
DisownLatch(&worker_state->latch);
169+
worker_state->shared_latch = NULL;
170170

171171
ev_monitor_close(worker_state);
172172

@@ -178,15 +178,15 @@ net_on_exit(__attribute__ ((unused)) int code, __attribute__ ((unused)) Datum ar
178178
static void wait_while_processing_interrupts(WorkerWait ww, bool *should_restart){
179179
switch(ww){
180180
case WORKER_WAIT_NO_TIMEOUT:
181-
WaitLatch(&worker_state->latch,
181+
WaitLatch(worker_state->shared_latch,
182182
WL_LATCH_SET | WL_EXIT_ON_PM_DEATH,
183183
no_timeout,
184184
PG_WAIT_EXTENSION);
185-
ResetLatch(&worker_state->latch);
185+
ResetLatch(worker_state->shared_latch);
186186
break;
187187
case WORKER_WAIT_ONE_SECOND:
188-
WaitLatch(&worker_state->latch, WL_LATCH_SET | WL_TIMEOUT | WL_EXIT_ON_PM_DEATH, 1000, PG_WAIT_EXTENSION);
189-
ResetLatch(&worker_state->latch);
188+
WaitLatch(worker_state->shared_latch, WL_LATCH_SET | WL_TIMEOUT | WL_EXIT_ON_PM_DEATH, 1000, PG_WAIT_EXTENSION);
189+
ResetLatch(worker_state->shared_latch);
190190
break;
191191
}
192192

@@ -228,10 +228,9 @@ static void unlock_extension(Oid ext_table_oids[static total_extension_tables]){
228228
}
229229

230230
void pg_net_worker(__attribute__ ((unused)) Datum main_arg) {
231+
worker_state->shared_latch = &MyProc->procLatch;
231232
on_proc_exit(net_on_exit, 0);
232233

233-
OwnLatch(&worker_state->latch);
234-
235234
BackgroundWorkerUnblockSignals();
236235
pqsignal(SIGTERM, handle_sigterm);
237236
pqsignal(SIGHUP, handle_sighup);
@@ -370,7 +369,7 @@ static void net_shmem_startup(void) {
370369
pg_atomic_init_u32(&worker_state->got_restart, 0);
371370
pg_atomic_init_u32(&worker_state->status, WS_NOT_YET);
372371
pg_atomic_init_u32(&worker_state->should_wake, 1);
373-
InitSharedLatch(&worker_state->latch);
372+
worker_state->shared_latch = NULL;
374373

375374
ConditionVariableInit(&worker_state->cv);
376375
worker_state->epfd = 0;

0 commit comments

Comments
 (0)