1 /*-------------------------------------------------------------------------
2  *
3  * timeout.h
4  *	  Routines to multiplex SIGALRM interrupts for multiple timeout reasons.
5  *
6  *
7  * Portions Copyright (c) 1996-2021, PostgreSQL Global Development Group
8  * Portions Copyright (c) 1994, Regents of the University of California
9  *
10  * src/include/utils/timeout.h
11  *
12  *-------------------------------------------------------------------------
13  */
14 #ifndef TIMEOUT_H
15 #define TIMEOUT_H
16 
17 #include "datatype/timestamp.h"
18 
19 /*
20  * Identifiers for timeout reasons.  Note that in case multiple timeouts
21  * trigger at the same time, they are serviced in the order of this enum.
22  */
23 typedef enum TimeoutId
24 {
25 	/* Predefined timeout reasons */
26 	STARTUP_PACKET_TIMEOUT,
27 	DEADLOCK_TIMEOUT,
28 	LOCK_TIMEOUT,
29 	STATEMENT_TIMEOUT,
30 	STANDBY_DEADLOCK_TIMEOUT,
31 	STANDBY_TIMEOUT,
32 	STANDBY_LOCK_TIMEOUT,
33 	IDLE_IN_TRANSACTION_SESSION_TIMEOUT,
34 	IDLE_SESSION_TIMEOUT,
35 	CLIENT_CONNECTION_CHECK_TIMEOUT,
36 	/* First user-definable timeout reason */
37 	USER_TIMEOUT,
38 	/* Maximum number of timeout reasons */
39 	MAX_TIMEOUTS = USER_TIMEOUT + 10
40 } TimeoutId;
41 
42 /* callback function signature */
43 typedef void (*timeout_handler_proc) (void);
44 
45 /*
46  * Parameter structure for setting multiple timeouts at once
47  */
48 typedef enum TimeoutType
49 {
50 	TMPARAM_AFTER,
51 	TMPARAM_AT
52 } TimeoutType;
53 
54 typedef struct
55 {
56 	TimeoutId	id;				/* timeout to set */
57 	TimeoutType type;			/* TMPARAM_AFTER or TMPARAM_AT */
58 	int			delay_ms;		/* only used for TMPARAM_AFTER */
59 	TimestampTz fin_time;		/* only used for TMPARAM_AT */
60 } EnableTimeoutParams;
61 
62 /*
63  * Parameter structure for clearing multiple timeouts at once
64  */
65 typedef struct
66 {
67 	TimeoutId	id;				/* timeout to clear */
68 	bool		keep_indicator; /* keep the indicator flag? */
69 } DisableTimeoutParams;
70 
71 /* timeout setup */
72 extern void InitializeTimeouts(void);
73 extern TimeoutId RegisterTimeout(TimeoutId id, timeout_handler_proc handler);
74 extern void reschedule_timeouts(void);
75 
76 /* timeout operation */
77 extern void enable_timeout_after(TimeoutId id, int delay_ms);
78 extern void enable_timeout_at(TimeoutId id, TimestampTz fin_time);
79 extern void enable_timeouts(const EnableTimeoutParams *timeouts, int count);
80 extern void disable_timeout(TimeoutId id, bool keep_indicator);
81 extern void disable_timeouts(const DisableTimeoutParams *timeouts, int count);
82 extern void disable_all_timeouts(bool keep_indicators);
83 
84 /* accessors */
85 extern bool get_timeout_active(TimeoutId id);
86 extern bool get_timeout_indicator(TimeoutId id, bool reset_indicator);
87 extern TimestampTz get_timeout_start_time(TimeoutId id);
88 extern TimestampTz get_timeout_finish_time(TimeoutId id);
89 
90 #endif							/* TIMEOUT_H */
91