PostgreSQL Source Code  git master
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-2024, 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 */
23 typedef struct timerCA
24 {
25  struct itimerval value;
26  HANDLE event;
27  CRITICAL_SECTION crit_sec;
29 
31 static HANDLE timerThreadHandle = INVALID_HANDLE_VALUE;
32 
33 
34 /* Timer management thread */
35 static DWORD WINAPI
36 pg_timer_thread(LPVOID param)
37 {
38  DWORD waittime;
39 
40  Assert(param == NULL);
41 
42  waittime = INFINITE;
43 
44  for (;;)
45  {
46  int r;
47 
48  r = WaitForSingleObjectEx(timerCommArea.event, waittime, FALSE);
49  if (r == WAIT_OBJECT_0)
50  {
51  /* Event signaled from main thread, change the timer */
52  EnterCriticalSection(&timerCommArea.crit_sec);
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  }
62  ResetEvent(timerCommArea.event);
63  LeaveCriticalSection(&timerCommArea.crit_sec);
64  }
65  else if (r == WAIT_TIMEOUT)
66  {
67  /* Timeout expired, signal SIGALRM and turn it off */
69  waittime = INFINITE;
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  */
85 int
86 setitimer(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);
90  Assert(which == ITIMER_REAL);
91 
92  if (timerThreadHandle == INVALID_HANDLE_VALUE)
93  {
94  /* First call in this backend, create event and the timer thread */
95  timerCommArea.event = CreateEvent(NULL, TRUE, FALSE, NULL);
96  if (timerCommArea.event == NULL)
97  ereport(FATAL,
98  (errmsg_internal("could not create timer event: error code %lu",
99  GetLastError())));
100 
101  MemSet(&timerCommArea.value, 0, sizeof(struct itimerval));
102 
103  InitializeCriticalSection(&timerCommArea.crit_sec);
104 
105  timerThreadHandle = CreateThread(NULL, 0, pg_timer_thread, NULL, 0, NULL);
106  if (timerThreadHandle == INVALID_HANDLE_VALUE)
107  ereport(FATAL,
108  (errmsg_internal("could not create timer thread: error code %lu",
109  GetLastError())));
110  }
111 
112  /* Request the timer thread to change settings */
113  EnterCriticalSection(&timerCommArea.crit_sec);
114  if (ovalue)
115  *ovalue = timerCommArea.value;
117  LeaveCriticalSection(&timerCommArea.crit_sec);
118  SetEvent(timerCommArea.event);
119 
120  return 0;
121 }
#define Assert(condition)
Definition: c.h:858
#define MemSet(start, val, len)
Definition: c.h:1020
int errmsg_internal(const char *fmt,...)
Definition: elog.c:1159
#define FATAL
Definition: elog.h:41
#define ereport(elevel,...)
Definition: elog.h:149
static struct @155 value
void pg_queue_signal(int signum)
Definition: signal.c:259
struct timeval it_value
Definition: win32_port.h:194
Definition: timer.c:24
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
struct timerCA timerCA
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:174
#define ITIMER_REAL
Definition: win32_port.h:190