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

Commit c8f3bc2

Browse files
committed
Optimize latches to send fewer signals.
Don't send signals to processes that aren't sleeping. Author: Andres Freund <andres@anarazel.de> Discussion: https://postgr.es/m/CA+hUKGJjxPDpzBE0a3hyUywBvaZuC89yx3jK9RFZgfv_KHU7gg@mail.gmail.com
1 parent d1b9099 commit c8f3bc2

File tree

2 files changed

+25
-0
lines changed

2 files changed

+25
-0
lines changed

src/backend/storage/ipc/latch.c

+24
Original file line numberDiff line numberDiff line change
@@ -274,6 +274,7 @@ void
274274
InitLatch(Latch *latch)
275275
{
276276
latch->is_set = false;
277+
latch->maybe_sleeping = false;
277278
latch->owner_pid = MyProcPid;
278279
latch->is_shared = false;
279280

@@ -321,6 +322,7 @@ InitSharedLatch(Latch *latch)
321322
#endif
322323

323324
latch->is_set = false;
325+
latch->maybe_sleeping = false;
324326
latch->owner_pid = 0;
325327
latch->is_shared = true;
326328
}
@@ -523,6 +525,10 @@ SetLatch(Latch *latch)
523525

524526
latch->is_set = true;
525527

528+
pg_memory_barrier();
529+
if (!latch->maybe_sleeping)
530+
return;
531+
526532
#ifndef WIN32
527533

528534
/*
@@ -589,6 +595,7 @@ ResetLatch(Latch *latch)
589595
{
590596
/* Only the owner should reset the latch */
591597
Assert(latch->owner_pid == MyProcPid);
598+
Assert(latch->maybe_sleeping == false);
592599

593600
latch->is_set = false;
594601

@@ -1270,6 +1277,14 @@ WaitEventSetWait(WaitEventSet *set, long timeout,
12701277
* ordering, so that we cannot miss seeing is_set if a notification
12711278
* has already been queued.
12721279
*/
1280+
if (set->latch && !set->latch->is_set)
1281+
{
1282+
/* about to sleep on a latch */
1283+
set->latch->maybe_sleeping = true;
1284+
pg_memory_barrier();
1285+
/* and recheck */
1286+
}
1287+
12731288
if (set->latch && set->latch->is_set)
12741289
{
12751290
occurred_events->fd = PGINVALID_SOCKET;
@@ -1280,6 +1295,9 @@ WaitEventSetWait(WaitEventSet *set, long timeout,
12801295
occurred_events++;
12811296
returned_events++;
12821297

1298+
/* could have been set above */
1299+
set->latch->maybe_sleeping = false;
1300+
12831301
break;
12841302
}
12851303

@@ -1291,6 +1309,12 @@ WaitEventSetWait(WaitEventSet *set, long timeout,
12911309
rc = WaitEventSetWaitBlock(set, cur_timeout,
12921310
occurred_events, nevents);
12931311

1312+
if (set->latch)
1313+
{
1314+
Assert(set->latch->maybe_sleeping);
1315+
set->latch->maybe_sleeping = false;
1316+
}
1317+
12941318
if (rc == -1)
12951319
break; /* timeout occurred */
12961320
else

src/include/storage/latch.h

+1
Original file line numberDiff line numberDiff line change
@@ -110,6 +110,7 @@
110110
typedef struct Latch
111111
{
112112
sig_atomic_t is_set;
113+
sig_atomic_t maybe_sleeping;
113114
bool is_shared;
114115
int owner_pid;
115116
#ifdef WIN32

0 commit comments

Comments
 (0)