PostgreSQL Source Code git master
Loading...
Searching...
No Matches
timer.c
Go to the documentation of this file.
1/*-------------------------------------------------------------------------
2 *
3 * timer.c
4 * Microsoft Windows Win32 Timer Implementation
5 *
6 * Limitations of this implementation:
7 *
8 * - Does not support interval timer (value->it_interval)
9 * - Only supports ITIMER_REAL
10 *
11 * Portions Copyright (c) 1996-2026, PostgreSQL Global Development Group
12 *
13 * IDENTIFICATION
14 * src/backend/port/win32/timer.c
15 *
16 *-------------------------------------------------------------------------
17 */
18
19#include "postgres.h"
20
21
22/* Communication area for inter-thread communication */
29
32
33
34/* Timer management thread */
35static DWORD WINAPI
37{
39
40 Assert(param == NULL);
41
43
44 for (;;)
45 {
46 int r;
47
49 if (r == WAIT_OBJECT_0)
50 {
51 /* Event signaled from main thread, change the timer */
53 if (timerCommArea.value.it_value.tv_sec == 0 &&
54 timerCommArea.value.it_value.tv_usec == 0)
55 waittime = INFINITE; /* Cancel the interrupt */
56 else
57 {
58 /* WaitForSingleObjectEx() uses milliseconds, round up */
59 waittime = (timerCommArea.value.it_value.tv_usec + 999) / 1000 +
60 timerCommArea.value.it_value.tv_sec * 1000;
61 }
64 }
65 else if (r == WAIT_TIMEOUT)
66 {
67 /* Timeout expired, signal SIGALRM and turn it off */
70 }
71 else
72 {
73 /* Should never happen */
74 Assert(false);
75 }
76 }
77
78 return 0;
79}
80
81/*
82 * Win32 setitimer emulation by creating a persistent thread
83 * to handle the timer setting and notification upon timeout.
84 */
85int
86setitimer(int which, const struct itimerval *value, struct itimerval *ovalue)
87{
88 Assert(value != NULL);
89 Assert(value->it_interval.tv_sec == 0 && value->it_interval.tv_usec == 0);
91
93 {
94 /* First call in this backend, create event and the timer thread */
98 (errmsg_internal("could not create timer event: error code %lu",
99 GetLastError())));
100
101 MemSet(&timerCommArea.value, 0, sizeof(struct itimerval));
102
104
108 (errmsg_internal("could not create timer thread: error code %lu",
109 GetLastError())));
110 }
111
112 /* Request the timer thread to change settings */
114 if (ovalue)
119
120 return 0;
121}
#define Assert(condition)
Definition c.h:873
#define MemSet(start, val, len)
Definition c.h:1013
int errmsg_internal(const char *fmt,...)
Definition elog.c:1170
#define FATAL
Definition elog.h:41
#define ereport(elevel,...)
Definition elog.h:150
static struct @172 value
static int fb(int x)
void pg_queue_signal(int signum)
Definition signal.c:259
struct timeval it_value
Definition win32_port.h:184
CRITICAL_SECTION crit_sec
Definition timer.c:27
struct itimerval value
Definition timer.c:25
HANDLE event
Definition timer.c:26
static timerCA timerCommArea
Definition timer.c:30
static DWORD WINAPI pg_timer_thread(LPVOID param)
Definition timer.c:36
static HANDLE timerThreadHandle
Definition timer.c:31
int setitimer(int which, const struct itimerval *value, struct itimerval *ovalue)
Definition timer.c:86
#define SIGALRM
Definition win32_port.h:164
#define ITIMER_REAL
Definition win32_port.h:180