Re: [PATCHES] Win32 CHECK_FOR_INTERRUPTS() performance

From: Qingqing Zhou <zhouqq(at)cs(dot)toronto(dot)edu>
To: Tom Lane <tgl(at)sss(dot)pgh(dot)pa(dot)us>
Cc: Andrew Dunstan <andrew(at)dunslane(dot)net>, Magnus Hagander <mha(at)sollentuna(dot)net>, pgsql-hackers(at)postgresql(dot)org, Merlin Moncure <merlin(dot)moncure(at)rcsonline(dot)com>
Subject: Re: [PATCHES] Win32 CHECK_FOR_INTERRUPTS() performance
Date: 2005-10-23 00:58:08
Message-ID: Pine.LNX.4.58.0510221517060.12644@eon.cs
Views: Raw Message | Whole Thread | Download mbox | Resend email
Thread:
Lists: pgsql-hackers


> Andrew Dunstan <andrew(at)dunslane(dot)net> 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;
+}

In response to

Responses

Browse pgsql-hackers by date

  From Date Subject
Next Message Andrew Dunstan 2005-10-23 01:30:51 Re: [PATCHES] Win32 CHECK_FOR_INTERRUPTS() performance
Previous Message Thomas Hallgren 2005-10-22 22:09:35 Re: Lifecycle management