From 50df864f8c63144bad281a1de1d6d38d4a06d4aa Mon Sep 17 00:00:00 2001 From: Oleksandr Shulgin Date: Thu, 4 Jun 2015 11:00:08 +0200 Subject: [PATCH] Add timersub for Win32. Fix gettimeofday on MinGW. --- psycopg/pqpath.c | 7 +++---- psycopg/win32_support.c | 17 ++++++++++++++++- psycopg/win32_support.h | 4 ++++ 3 files changed, 23 insertions(+), 5 deletions(-) diff --git a/psycopg/pqpath.c b/psycopg/pqpath.c index 4fb4771e..e87befae 100644 --- a/psycopg/pqpath.c +++ b/psycopg/pqpath.c @@ -1616,15 +1616,14 @@ _pq_copy_both_v3(cursorObject *curs) ping_time = last_comm; ping_time.tv_sec += curs->keepalive_interval; - if (timercmp(&ping_time, &curr_time, >)) { - timersub(&ping_time, &curr_time, &time_diff); - + timersub(&ping_time, &curr_time, &time_diff); + if (time_diff.tv_sec > 0) { Py_BEGIN_ALLOW_THREADS; sel = select(PQsocket(conn) + 1, &fds, NULL, NULL, &time_diff); Py_END_ALLOW_THREADS; } else { - sel = 0; /* pretend select() timed out */ + sel = 0; /* we're past target time, pretend select() timed out */ } if (sel < 0) { diff --git a/psycopg/win32_support.c b/psycopg/win32_support.c index 8a760b9f..d508b220 100644 --- a/psycopg/win32_support.c +++ b/psycopg/win32_support.c @@ -29,6 +29,8 @@ #include "psycopg/win32_support.h" #ifdef _WIN32 + +#ifndef __MINGW32__ /* millisecond-precision port of gettimeofday for Win32, taken from src/port/gettimeofday.c in PostgreSQL core */ @@ -58,4 +60,17 @@ gettimeofday(struct timeval * tp, struct timezone * tzp) return 0; } -#endif /* _WIN32 */ +#endif /* !defined(__MINGW32__) */ + +/* timersub is missing on mingw */ +void +timersub(struct timeval *a, struct timeval *b, struct timeval *c) +{ + c->tv_sec = a->tv_sec - b->tv_sec; + c->tv_usec = a->tv_usec - b->tv_usec; + if (tv_usec < 0) { + c->tv_usec += 1000000; + c->tv_sec -= 1; + } +} +#endif /* defined(_WIN32) */ diff --git a/psycopg/win32_support.h b/psycopg/win32_support.h index c6577317..be963df5 100644 --- a/psycopg/win32_support.h +++ b/psycopg/win32_support.h @@ -30,7 +30,11 @@ #include #ifdef _WIN32 +#ifndef __MINGW32__ HIDDEN int gettimeofday(struct timeval * tp, struct timezone * tzp); #endif +HIDDEN void timersub(struct timeval *a, struct timeval *b, struct timeval *c); +#endif + #endif /* !defined(PSYCOPG_WIN32_SUPPORT_H) */