Go to the documentation of this file.00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019 #include "postgres.h"
00020
00021
00022
00023 typedef struct timerCA
00024 {
00025 struct itimerval value;
00026 HANDLE event;
00027 CRITICAL_SECTION crit_sec;
00028 } timerCA;
00029
00030 static timerCA timerCommArea;
00031 static HANDLE timerThreadHandle = INVALID_HANDLE_VALUE;
00032
00033
00034
00035 static DWORD WINAPI
00036 pg_timer_thread(LPVOID param)
00037 {
00038 DWORD waittime;
00039
00040 Assert(param == NULL);
00041
00042 waittime = INFINITE;
00043
00044 for (;;)
00045 {
00046 int r;
00047
00048 r = WaitForSingleObjectEx(timerCommArea.event, waittime, FALSE);
00049 if (r == WAIT_OBJECT_0)
00050 {
00051
00052 EnterCriticalSection(&timerCommArea.crit_sec);
00053 if (timerCommArea.value.it_value.tv_sec == 0 &&
00054 timerCommArea.value.it_value.tv_usec == 0)
00055 waittime = INFINITE;
00056 else
00057 {
00058
00059 waittime = (timerCommArea.value.it_value.tv_usec + 999) / 1000 +
00060 timerCommArea.value.it_value.tv_sec * 1000;
00061 }
00062 ResetEvent(timerCommArea.event);
00063 LeaveCriticalSection(&timerCommArea.crit_sec);
00064 }
00065 else if (r == WAIT_TIMEOUT)
00066 {
00067
00068 pg_queue_signal(SIGALRM);
00069 waittime = INFINITE;
00070 }
00071 else
00072 {
00073
00074 Assert(false);
00075 }
00076 }
00077
00078 return 0;
00079 }
00080
00081
00082
00083
00084
00085 int
00086 setitimer(int which, const struct itimerval * value, struct itimerval * ovalue)
00087 {
00088 Assert(value != NULL);
00089 Assert(value->it_interval.tv_sec == 0 && value->it_interval.tv_usec == 0);
00090 Assert(which == ITIMER_REAL);
00091
00092 if (timerThreadHandle == INVALID_HANDLE_VALUE)
00093 {
00094
00095 timerCommArea.event = CreateEvent(NULL, TRUE, FALSE, NULL);
00096 if (timerCommArea.event == NULL)
00097 ereport(FATAL,
00098 (errmsg_internal("could not create timer event: error code %lu",
00099 GetLastError())));
00100
00101 MemSet(&timerCommArea.value, 0, sizeof(struct itimerval));
00102
00103 InitializeCriticalSection(&timerCommArea.crit_sec);
00104
00105 timerThreadHandle = CreateThread(NULL, 0, pg_timer_thread, NULL, 0, NULL);
00106 if (timerThreadHandle == INVALID_HANDLE_VALUE)
00107 ereport(FATAL,
00108 (errmsg_internal("could not create timer thread: error code %lu",
00109 GetLastError())));
00110 }
00111
00112
00113 EnterCriticalSection(&timerCommArea.crit_sec);
00114 if (ovalue)
00115 *ovalue = timerCommArea.value;
00116 timerCommArea.value = *value;
00117 LeaveCriticalSection(&timerCommArea.crit_sec);
00118 SetEvent(timerCommArea.event);
00119
00120 return 0;
00121 }