]> granicus.if.org Git - postgresql/blob - src/backend/port/win32/timer.c
Update copyright for 2006. Update scripts.
[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-2006, PostgreSQL Global Development Group
12  *
13  * IDENTIFICATION
14  *        $PostgreSQL: pgsql/src/backend/port/win32/timer.c,v 1.8 2006/03/05 15:58:35 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                                 waittime = timerCommArea.value.it_value.tv_usec / 10 + timerCommArea.value.it_value.tv_sec * 1000;
60                         ResetEvent(timerCommArea.event);
61                         LeaveCriticalSection(&timerCommArea.crit_sec);
62                 }
63                 else if (r == WAIT_TIMEOUT)
64                 {
65                         /* Timeout expired, signal SIGALRM and turn it off */
66                         pg_queue_signal(SIGALRM);
67                         waittime = INFINITE;
68                 }
69                 else
70                 {
71                         /* Should never happen */
72                         Assert(false);
73                 }
74         }
75
76         return 0;
77 }
78
79 /*
80  * Win32 setitimer emulation by creating a persistent thread
81  * to handle the timer setting and notification upon timeout.
82  */
83 int
84 setitimer(int which, const struct itimerval * value, struct itimerval * ovalue)
85 {
86         Assert(value != NULL);
87         Assert(value->it_interval.tv_sec == 0 && value->it_interval.tv_usec == 0);
88         Assert(which == ITIMER_REAL);
89
90         if (timerThreadHandle == INVALID_HANDLE_VALUE)
91         {
92                 /* First call in this backend, create event and the timer thread */
93                 timerCommArea.event = CreateEvent(NULL, TRUE, FALSE, NULL);
94                 if (timerCommArea.event == NULL)
95                         ereport(FATAL,
96                                         (errmsg_internal("failed to create timer event: %d",
97                                                                          (int) GetLastError())));
98
99                 MemSet(&timerCommArea.value, 0, sizeof(struct itimerval));
100
101                 InitializeCriticalSection(&timerCommArea.crit_sec);
102
103                 timerThreadHandle = CreateThread(NULL, 0, pg_timer_thread, NULL, 0, NULL);
104                 if (timerThreadHandle == INVALID_HANDLE_VALUE)
105                         ereport(FATAL,
106                                         (errmsg_internal("failed to create timer thread: %d",
107                                                                          (int) GetLastError())));
108         }
109
110         /* Request the timer thread to change settings */
111         EnterCriticalSection(&timerCommArea.crit_sec);
112         if (ovalue)
113                 *ovalue = timerCommArea.value;
114         timerCommArea.value = *value;
115         LeaveCriticalSection(&timerCommArea.crit_sec);
116         SetEvent(timerCommArea.event);
117
118         return 0;
119 }