> Andrew Dunstan <[EMAIL PROTECTED]> writes:
> > The hard part looks to be cancelling/changing the timer, which means
> > that we can't just create a set and forget listener thread for a given
> > timeout. Otherwise that seems to me the straightforward approach.
>
> Yeah.  I think probably the cleanest way is to create a persistent
> thread that manages the timer.  We need a way for the main thread to
> tell it to cancel the timer or change the setting.  Dunno enough about
> Windows' interthread communication primitives to propose details.
>

Oh my ... fortunately we got a timer test in regression.

I've come up with a quick patch implementing above discussions. Also,
seems by patching this, we can support setitimer(.,.,ovalue != NULL) --
because it is saved in the memory.

Regards,
Qingqing

---

Index: timer.c
===================================================================
RCS file: /projects/cvsroot/pgsql/src/backend/port/win32/timer.c,v
retrieving revision 1.5
diff -u -r1.5 timer.c
--- timer.c     31 Dec 2004 22:00:37 -0000      1.5
+++ timer.c     23 Oct 2005 00:53:56 -0000
@@ -15,8 +15,16 @@

 #include "libpq/pqsignal.h"

+/* Communication area of timer settings */
+typedef struct timerCA{
+       int which;
+       struct itimerval value;
+       HANDLE event;
+}timerCA;

+static timerCA timerCommArea;
 static HANDLE timerHandle = INVALID_HANDLE_VALUE;
+static HANDLE timerThreadHandle = INVALID_HANDLE_VALUE;

 static VOID CALLBACK
 timer_completion(LPVOID arg, DWORD timeLow, DWORD timeHigh)
@@ -28,16 +36,14 @@
 /*
  * Limitations of this implementation:
  *
- * - Does not support setting ovalue
  * - Does not support interval timer (value->it_interval)
  * - Only supports ITIMER_REAL
  */
-int
-setitimer(int which, const struct itimerval * value, struct itimerval * ovalue)
+static int
+do_setitimer(int which, const struct itimerval * value)
 {
        LARGE_INTEGER dueTime;

-       Assert(ovalue == NULL);
        Assert(value != NULL);
        Assert(value->it_interval.tv_sec == 0 && value->it_interval.tv_usec == 
0);
        Assert(which == ITIMER_REAL);
@@ -69,3 +75,56 @@

        return 0;
 }
+
+/* Timer ticking thread */
+static DWORD WINAPI
+pg_timer_thread(LPVOID param)
+{
+       Assert(param == NULL);
+
+       for (;;)
+       {
+               if (WaitForSingleObjectEx(timerCommArea.event, INFINITE, TRUE) 
== WAIT_OBJECT_0)
+               {
+                       do_setitimer(timerCommArea.which, &timerCommArea.value);
+                       ResetEvent(timerCommArea.event);
+               }
+       }
+
+       return 0;
+}
+
+/*
+ * Win32 setitimer emulation by creating a persistent thread
+ * to handle the timer setting and notification upon timeout.
+ */
+int
+setitimer(int which, const struct itimerval * value, struct itimerval * ovalue)
+{
+       Assert(value != NULL);
+
+       if (timerThreadHandle == INVALID_HANDLE_VALUE)
+       {
+               /* First call in this backend, create event and the timer 
thread */
+               timerCommArea.event = CreateEvent(NULL, TRUE, FALSE, NULL);
+               if (timerCommArea.event == NULL)
+                       ereport(FATAL,
+                               (errmsg_internal("failed to create timer event: 
%d", (int) GetLastError())));
+               MemSet(&timerCommArea.value, 0, sizeof(struct itimerval));
+
+               timerThreadHandle = CreateThread(NULL, 0, pg_timer_thread, 
NULL, 0, NULL);
+               if (timerThreadHandle == INVALID_HANDLE_VALUE)
+                       ereport(FATAL,
+                               (errmsg_internal("failed to create timer 
thread: %d", (int) GetLastError())));
+       }
+
+       /* Request the timer thread to change settings */
+       if (ovalue)
+               *ovalue = timerCommArea.value;
+       timerCommArea.which = which;
+       timerCommArea.value = *value;
+       SetEvent(timerCommArea.event);
+
+       /* Timer thread will handle possible errors */
+       return 0;
+}

---------------------------(end of broadcast)---------------------------
TIP 9: In versions below 8.0, the planner will ignore your desire to
       choose an index scan if your joining column's datatypes do not
       match

Reply via email to