PostgreSQL Source Code  git master
All Data Structures Namespaces Files Functions Variables Typedefs Enumerations Enumerator Macros Pages
timer.c File Reference
#include "postgres.h"
Include dependency graph for timer.c:

Go to the source code of this file.

Data Structures

struct  timerCA
 

Typedefs

typedef struct timerCA timerCA
 

Functions

static DWORD WINAPI pg_timer_thread (LPVOID param)
 
int setitimer (int which, const struct itimerval *value, struct itimerval *ovalue)
 

Variables

static timerCA timerCommArea
 
static HANDLE timerThreadHandle = INVALID_HANDLE_VALUE
 

Typedef Documentation

◆ timerCA

typedef struct timerCA timerCA

Function Documentation

◆ pg_timer_thread()

static DWORD WINAPI pg_timer_thread ( LPVOID  param)
static

Definition at line 36 of file timer.c.

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 }
#define Assert(condition)
Definition: c.h:863
void pg_queue_signal(int signum)
Definition: signal.c:259
struct timeval it_value
Definition: win32_port.h:194
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
#define SIGALRM
Definition: win32_port.h:174

References Assert, timerCA::crit_sec, timerCA::event, itimerval::it_value, pg_queue_signal(), SIGALRM, timerCommArea, and timerCA::value.

◆ setitimer()

int setitimer ( int  which,
const struct itimerval value,
struct itimerval ovalue 
)

Definition at line 86 of file timer.c.

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 MemSet(start, val, len)
Definition: c.h:1025
int errmsg_internal(const char *fmt,...)
Definition: elog.c:1157
#define FATAL
Definition: elog.h:41
#define ereport(elevel,...)
Definition: elog.h:149
static struct @160 value
static DWORD WINAPI pg_timer_thread(LPVOID param)
Definition: timer.c:36
static HANDLE timerThreadHandle
Definition: timer.c:31
#define ITIMER_REAL
Definition: win32_port.h:190

Referenced by do_watch(), fork_process(), and schedule_alarm().

Variable Documentation

◆ timerCommArea

timerCA timerCommArea
static

Definition at line 30 of file timer.c.

Referenced by pg_timer_thread().

◆ timerThreadHandle

HANDLE timerThreadHandle = INVALID_HANDLE_VALUE
static

Definition at line 31 of file timer.c.