Skip to content

Commit 8274f45

Browse files
committed
Refactor CHECK_FOR_INTERRUPTS() to add flexibility.
Split up CHECK_FOR_INTERRUPTS() to provide an additional macro INTERRUPTS_PENDING_CONDITION(), which just tests whether an interrupt is pending without attempting to service it. This is useful in situations where the caller knows that interrupts are blocked, and would like to find out if it's worth the trouble to unblock them. Also add INTERRUPTS_CAN_BE_PROCESSED(), which indicates whether CHECK_FOR_INTERRUPTS() can be relied on to clear the pending interrupt. This commit doesn't actually add any uses of the new macros, but a follow-on bug fix will do so. Back-patch to all supported branches to provide infrastructure for that fix. Alvaro Herrera and Tom Lane Discussion: https://postgr.es/m/20210513155351.GA7848@alvherre.pgsql
1 parent b0e6e08 commit 8274f45

File tree

2 files changed

+35
-13
lines changed

2 files changed

+35
-13
lines changed

src/backend/tcop/postgres.c

Lines changed: 12 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -592,7 +592,7 @@ ProcessClientWriteInterrupt(bool blocked)
592592
{
593593
/*
594594
* Don't mess with whereToSendOutput if ProcessInterrupts wouldn't
595-
* do anything.
595+
* service ProcDiePending.
596596
*/
597597
if (InterruptHoldoffCount == 0 && CritSectionCount == 0)
598598
{
@@ -2905,6 +2905,12 @@ RecoveryConflictInterrupt(ProcSignalReason reason)
29052905
* If an interrupt condition is pending, and it's safe to service it,
29062906
* then clear the flag and accept the interrupt. Called only when
29072907
* InterruptPending is true.
2908+
*
2909+
* Note: if INTERRUPTS_CAN_BE_PROCESSED() is true, then ProcessInterrupts
2910+
* is guaranteed to clear the InterruptPending flag before returning.
2911+
* (This is not the same as guaranteeing that it's still clear when we
2912+
* return; another interrupt could have arrived. But we promise that
2913+
* any pre-existing one will have been serviced.)
29082914
*/
29092915
void
29102916
ProcessInterrupts(void)
@@ -3009,7 +3015,11 @@ ProcessInterrupts(void)
30093015
{
30103016
/*
30113017
* Re-arm InterruptPending so that we process the cancel request as
3012-
* soon as we're done reading the message.
3018+
* soon as we're done reading the message. (XXX this is seriously
3019+
* ugly: it complicates INTERRUPTS_CAN_BE_PROCESSED(), and it means we
3020+
* can't use that macro directly as the initial test in this function,
3021+
* meaning that this code also creates opportunities for other bugs to
3022+
* appear.)
30133023
*/
30143024
InterruptPending = true;
30153025
}

src/include/miscadmin.h

Lines changed: 23 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -56,6 +56,15 @@
5656
* allowing die interrupts: HOLD_CANCEL_INTERRUPTS() and
5757
* RESUME_CANCEL_INTERRUPTS().
5858
*
59+
* Note that ProcessInterrupts() has also acquired a number of tasks that
60+
* do not necessarily cause a query-cancel-or-die response. Hence, it's
61+
* possible that it will just clear InterruptPending and return.
62+
*
63+
* INTERRUPTS_PENDING_CONDITION() can be checked to see whether an
64+
* interrupt needs to be serviced, without trying to do so immediately.
65+
* Some callers are also interested in INTERRUPTS_CAN_BE_PROCESSED(),
66+
* which tells whether ProcessInterrupts is sure to clear the interrupt.
67+
*
5968
* Special mechanisms are used to let an interrupt be accepted when we are
6069
* waiting for a lock or when we are waiting for command input (but, of
6170
* course, only if the interrupt holdoff counter is zero). See the
@@ -93,24 +102,27 @@ extern PGDLLIMPORT volatile uint32 CritSectionCount;
93102
/* in tcop/postgres.c */
94103
extern void ProcessInterrupts(void);
95104

105+
/* Test whether an interrupt is pending */
96106
#ifndef WIN32
107+
#define INTERRUPTS_PENDING_CONDITION() \
108+
(unlikely(InterruptPending))
109+
#else
110+
#define INTERRUPTS_PENDING_CONDITION() \
111+
(unlikely(UNBLOCKED_SIGNAL_QUEUE()) ? pgwin32_dispatch_queued_signals() : 0, \
112+
unlikely(InterruptPending))
113+
#endif
97114

115+
/* Service interrupt, if one is pending and it's safe to service it now */
98116
#define CHECK_FOR_INTERRUPTS() \
99117
do { \
100-
if (unlikely(InterruptPending)) \
101-
ProcessInterrupts(); \
102-
} while(0)
103-
#else /* WIN32 */
104-
105-
#define CHECK_FOR_INTERRUPTS() \
106-
do { \
107-
if (unlikely(UNBLOCKED_SIGNAL_QUEUE())) \
108-
pgwin32_dispatch_queued_signals(); \
109-
if (unlikely(InterruptPending)) \
118+
if (INTERRUPTS_PENDING_CONDITION()) \
110119
ProcessInterrupts(); \
111120
} while(0)
112-
#endif /* WIN32 */
113121

122+
/* Is ProcessInterrupts() guaranteed to clear InterruptPending? */
123+
#define INTERRUPTS_CAN_BE_PROCESSED() \
124+
(InterruptHoldoffCount == 0 && CritSectionCount == 0 && \
125+
QueryCancelHoldoffCount == 0)
114126

115127
#define HOLD_INTERRUPTS() (InterruptHoldoffCount++)
116128

0 commit comments

Comments
 (0)