From af3b182a574e4b349dcf71279769ad18818b13b9 Mon Sep 17 00:00:00 2001 From: Bruce Momjian Date: Wed, 18 Feb 2004 16:25:12 +0000 Subject: [PATCH] Here is a patch that implements setitimer() on win32. With this patch applied, deadlock detection and statement_timeout now works. The file timer.c goes into src/backend/port/win32/. The patch also removes two lines of "printf debugging" accidentally left in pqsignal.h, in the console control handler. Magnus Hagander --- src/backend/libpq/pqsignal.c | 4 +- src/backend/port/win32/Makefile | 4 +- src/backend/port/win32/timer.c | 65 +++++++++++++++++++++++++++++++++ src/backend/storage/lmgr/proc.c | 15 +------- src/include/port/win32.h | 11 +++++- 5 files changed, 79 insertions(+), 20 deletions(-) create mode 100644 src/backend/port/win32/timer.c diff --git a/src/backend/libpq/pqsignal.c b/src/backend/libpq/pqsignal.c index 7c4e67ffcc..34e5883797 100644 --- a/src/backend/libpq/pqsignal.c +++ b/src/backend/libpq/pqsignal.c @@ -9,7 +9,7 @@ * * * IDENTIFICATION - * $PostgreSQL: pgsql/src/backend/libpq/pqsignal.c,v 1.31 2004/02/08 22:28:56 neilc Exp $ + * $PostgreSQL: pgsql/src/backend/libpq/pqsignal.c,v 1.32 2004/02/18 16:25:12 momjian Exp $ * * NOTES * This shouldn't be in libpq, but the monitor and some other @@ -429,8 +429,6 @@ pg_signal_thread(LPVOID param) /* Console control handler will execute on a thread created by the OS at the time of invocation */ static BOOL WINAPI pg_console_handler(DWORD dwCtrlType) { - printf("Console handler being called!\n"); - fflush(stdout); if (dwCtrlType == CTRL_C_EVENT || dwCtrlType == CTRL_BREAK_EVENT || dwCtrlType == CTRL_CLOSE_EVENT || diff --git a/src/backend/port/win32/Makefile b/src/backend/port/win32/Makefile index 647370ebcd..16f6c75bc4 100644 --- a/src/backend/port/win32/Makefile +++ b/src/backend/port/win32/Makefile @@ -4,7 +4,7 @@ # Makefile for port/win32 # # IDENTIFICATION -# $PostgreSQL: pgsql/src/backend/port/win32/Makefile,v 1.2 2003/11/29 19:51:54 pgsql Exp $ +# $PostgreSQL: pgsql/src/backend/port/win32/Makefile,v 1.3 2004/02/18 16:25:12 momjian Exp $ # #------------------------------------------------------------------------- @@ -12,7 +12,7 @@ subdir = src/backend/port/win32 top_builddir = ../../../.. include $(top_builddir)/src/Makefile.global -OBJS = sema.o shmem.o +OBJS = sema.o shmem.o timer.o all: SUBSYS.o diff --git a/src/backend/port/win32/timer.c b/src/backend/port/win32/timer.c new file mode 100644 index 0000000000..8202efe3c4 --- /dev/null +++ b/src/backend/port/win32/timer.c @@ -0,0 +1,65 @@ +/*------------------------------------------------------------------------- + * + * timer.c + * Microsoft Windows Win32 Timer Implementation + * + * Portions Copyright (c) 1996-2003, PostgreSQL Global Development Group + * + * IDENTIFICATION + * $PostgreSQL: pgsql/src/backend/port/win32/timer.c,v 1.1 2004/02/18 16:25:12 momjian Exp $ + * + *------------------------------------------------------------------------- + */ + +#include "postgres.h" + +#include "libpq/pqsignal.h" + + +static HANDLE timerHandle = INVALID_HANDLE_VALUE; + +static VOID CALLBACK timer_completion(LPVOID arg, DWORD timeLow, DWORD timeHigh) { + pg_queue_signal(SIGALRM); +} + + +/* + * 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) { + 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); + + if (timerHandle == INVALID_HANDLE_VALUE) { + /* First call in this backend, create new timer object */ + timerHandle = CreateWaitableTimer(NULL, TRUE, NULL); + if (timerHandle == NULL) + ereport(FATAL, + (errmsg_internal("failed to create waitable timer: %i",GetLastError()))); + } + + if (value->it_value.tv_sec == 0 && + value->it_value.tv_usec == 0) { + /* Turn timer off */ + CancelWaitableTimer(timerHandle); + return 0; + } + + /* Negative time to SetWaitableTimer means relative time */ + dueTime.QuadPart = -(value->it_value.tv_usec*10 + value->it_value.tv_sec*10000000L); + + /* Turn timer on, or change timer */ + if (!SetWaitableTimer(timerHandle, &dueTime, 0, timer_completion, NULL, FALSE)) + ereport(FATAL, + (errmsg_internal("failed to set waitable timer: %i",GetLastError()))); + + return 0; +} diff --git a/src/backend/storage/lmgr/proc.c b/src/backend/storage/lmgr/proc.c index f6d7c634a7..19ce44ff68 100644 --- a/src/backend/storage/lmgr/proc.c +++ b/src/backend/storage/lmgr/proc.c @@ -8,7 +8,7 @@ * * * IDENTIFICATION - * $PostgreSQL: pgsql/src/backend/storage/lmgr/proc.c,v 1.146 2004/02/08 22:28:56 neilc Exp $ + * $PostgreSQL: pgsql/src/backend/storage/lmgr/proc.c,v 1.147 2004/02/18 16:25:12 momjian Exp $ * *------------------------------------------------------------------------- */ @@ -972,9 +972,6 @@ ProcSendSignal(BackendId procId) bool enable_sig_alarm(int delayms, bool is_statement_timeout) { -#ifdef WIN32 -#warning add Win32 timer -#else struct timeval fin_time; #ifndef __BEOS__ @@ -1044,7 +1041,6 @@ enable_sig_alarm(int delayms, bool is_statement_timeout) time_interval = delayms * 1000; /* usecs */ if (set_alarm(time_interval, B_ONE_SHOT_RELATIVE_ALARM) < 0) return false; -#endif #endif return true; } @@ -1059,10 +1055,6 @@ enable_sig_alarm(int delayms, bool is_statement_timeout) bool disable_sig_alarm(bool is_statement_timeout) { -#ifdef WIN32 -#warning add Win32 timer -#else - /* * Always disable the interrupt if it is active; this avoids being * interrupted by the signal handler and thereby possibly getting @@ -1102,7 +1094,6 @@ disable_sig_alarm(bool is_statement_timeout) if (!CheckStatementTimeout()) return false; } -#endif return true; } @@ -1135,9 +1126,6 @@ CheckStatementTimeout(void) else { /* Not time yet, so (re)schedule the interrupt */ -#ifdef WIN32 -#warning add win32 timer -#else #ifndef __BEOS__ struct itimerval timeval; @@ -1160,7 +1148,6 @@ CheckStatementTimeout(void) (statement_fin_time.tv_usec - now.tv_usec); if (set_alarm(time_interval, B_ONE_SHOT_RELATIVE_ALARM) < 0) return false; -#endif #endif } diff --git a/src/include/port/win32.h b/src/include/port/win32.h index 9a98c72dc1..22c36a7320 100644 --- a/src/include/port/win32.h +++ b/src/include/port/win32.h @@ -1,4 +1,4 @@ -/* $PostgreSQL: pgsql/src/include/port/win32.h,v 1.17 2004/02/08 22:28:57 neilc Exp $ */ +/* $PostgreSQL: pgsql/src/include/port/win32.h,v 1.18 2004/02/18 16:25:12 momjian Exp $ */ /* undefine and redefine after #include */ #undef mkdir @@ -132,6 +132,15 @@ struct timezone int tz_dsttime; /* Nonzero if DST is ever in effect. */ }; +/* for setitimer in backend/port/win32/timer.c */ +#define ITIMER_REAL 0 +struct itimerval { + struct timeval it_interval; + struct timeval it_value; +}; +int setitimer(int which, const struct itimerval *value, struct itimerval *ovalue); + + /* FROM SRA */ /*