]> granicus.if.org Git - postgresql/blob - src/backend/port/win32/timer.c
Update copyrights in source tree to 2008.
[postgresql] / src / backend / port / win32 / timer.c
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-2008, PostgreSQL Global Development Group
12  *
13  * IDENTIFICATION
14  *        $PostgreSQL: pgsql/src/backend/port/win32/timer.c,v 1.15 2008/01/01 19:45:51 momjian Exp $
15  *
16  *-------------------------------------------------------------------------
17  */
18
19 #include "postgres.h"
20
21 #include "libpq/pqsignal.h"
22
23
24 /* Communication area for inter-thread communication */
25 typedef struct timerCA
26 {
27         struct itimerval value;
28         HANDLE          event;
29         CRITICAL_SECTION crit_sec;
30 }       timerCA;
31
32 static timerCA timerCommArea;
33 static HANDLE timerThreadHandle = INVALID_HANDLE_VALUE;
34
35
36 /* Timer management thread */
37 static DWORD WINAPI
38 pg_timer_thread(LPVOID param)
39 {
40         DWORD           waittime;
41
42         Assert(param == NULL);
43
44         waittime = INFINITE;
45
46         for (;;)
47         {
48                 int                     r;
49
50                 r = WaitForSingleObjectEx(timerCommArea.event, waittime, FALSE);
51                 if (r == WAIT_OBJECT_0)
52                 {
53                         /* Event signalled from main thread, change the timer */
54                         EnterCriticalSection(&timerCommArea.crit_sec);
55                         if (timerCommArea.value.it_value.tv_sec == 0 &&
56                                 timerCommArea.value.it_value.tv_usec == 0)
57                                 waittime = INFINITE;    /* Cancel the interrupt */
58                         else
59                         {
60                                 /* WaitForSingleObjectEx() uses milliseconds, round up */
61                                 waittime = (timerCommArea.value.it_value.tv_usec + 999) / 1000 +
62                                         timerCommArea.value.it_value.tv_sec * 1000;
63                         }
64                         ResetEvent(timerCommArea.event);
65                         LeaveCriticalSection(&timerCommArea.crit_sec);
66                 }
67                 else if (r == WAIT_TIMEOUT)
68                 {
69                         /* Timeout expired, signal SIGALRM and turn it off */
70                         pg_queue_signal(SIGALRM);
71                         waittime = INFINITE;
72                 }
73                 else
74                 {
75                         /* Should never happen */
76                         Assert(false);
77                 }
78         }
79
80         return 0;
81 }
82
83 /*
84  * Win32 setitimer emulation by creating a persistent thread
85  * to handle the timer setting and notification upon timeout.
86  */
87 int
88 setitimer(int which, const struct itimerval * value, struct itimerval * ovalue)
89 {
90         Assert(value != NULL);
91         Assert(value->it_interval.tv_sec == 0 && value->it_interval.tv_usec == 0);
92         Assert(which == ITIMER_REAL);
93
94         if (timerThreadHandle == INVALID_HANDLE_VALUE)
95         {
96                 /* First call in this backend, create event and the timer thread */
97                 timerCommArea.event = CreateEvent(NULL, TRUE, FALSE, NULL);
98                 if (timerCommArea.event == NULL)
99                         ereport(FATAL,
100                                         (errmsg_internal("failed to create timer event: %d",
101                                                                          (int) GetLastError())));
102
103                 MemSet(&timerCommArea.value, 0, sizeof(struct itimerval));
104
105                 InitializeCriticalSection(&timerCommArea.crit_sec);
106
107                 timerThreadHandle = CreateThread(NULL, 0, pg_timer_thread, NULL, 0, NULL);
108                 if (timerThreadHandle == INVALID_HANDLE_VALUE)
109                         ereport(FATAL,
110                                         (errmsg_internal("failed to create timer thread: %d",
111                                                                          (int) GetLastError())));
112         }
113
114         /* Request the timer thread to change settings */
115         EnterCriticalSection(&timerCommArea.crit_sec);
116         if (ovalue)
117                 *ovalue = timerCommArea.value;
118         timerCommArea.value = *value;
119         LeaveCriticalSection(&timerCommArea.crit_sec);
120         SetEvent(timerCommArea.event);
121
122         return 0;
123 }