PostgreSQL Source Code  git master
 All Data Structures Namespaces Files Functions Variables Typedefs Enumerations Enumerator Macros
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

Function Documentation

static DWORD WINAPI pg_timer_thread ( LPVOID  param)
static

Definition at line 36 of file timer.c.

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

Referenced by setitimer().

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 signalled 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 }
int waittime
Definition: pg_standby.c:41
struct timeval it_value
Definition: win32.h:232
HANDLE event
Definition: timer.c:26
#define FALSE
Definition: c.h:218
static timerCA timerCommArea
Definition: timer.c:30
struct itimerval value
Definition: timer.c:25
#define NULL
Definition: c.h:226
#define Assert(condition)
Definition: c.h:671
void pg_queue_signal(int signum)
Definition: signal.c:210
CRITICAL_SECTION crit_sec
Definition: timer.c:27
#define SIGALRM
Definition: win32.h:202
int setitimer ( int  which,
const struct itimerval value,
struct itimerval ovalue 
)

Definition at line 86 of file timer.c.

References Assert, timerCA::crit_sec, ereport, errmsg_internal(), timerCA::event, FALSE, FATAL, itimerval::it_interval, ITIMER_REAL, MemSet, NULL, pg_timer_thread(), timerThreadHandle, TRUE, timerCA::value, and value.

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

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 ITIMER_REAL
Definition: win32.h:228
static struct @76 value
#define MemSet(start, val, len)
Definition: c.h:853
HANDLE event
Definition: timer.c:26
static DWORD WINAPI pg_timer_thread(LPVOID param)
Definition: timer.c:36
#define FALSE
Definition: c.h:218
#define FATAL
Definition: elog.h:52
static timerCA timerCommArea
Definition: timer.c:30
#define ereport(elevel, rest)
Definition: elog.h:122
struct itimerval value
Definition: timer.c:25
int errmsg_internal(const char *fmt,...)
Definition: elog.c:827
#define NULL
Definition: c.h:226
#define Assert(condition)
Definition: c.h:671
CRITICAL_SECTION crit_sec
Definition: timer.c:27
struct timeval it_interval
Definition: win32.h:231
static HANDLE timerThreadHandle
Definition: timer.c:31
#define TRUE
Definition: c.h:214

Variable Documentation

timerCA timerCommArea
static

Definition at line 30 of file timer.c.

HANDLE timerThreadHandle = INVALID_HANDLE_VALUE
static

Definition at line 31 of file timer.c.

Referenced by setitimer().