[PATCH 03/50] Fix timer implementation for Windows


Michael Schonberg <mschonberg@...>
 

From: Mike Schonberg <mschonberg@nyx.com>

Address subtle difference between Linux and Windows networking required by
timers. Since there are no pipes on Windows, the timer implementation use a
loopback socket which requires send and recv rather the read/write.

Signed-off-by: Mike Schonberg <mschonberg@nyx.com>
---
common/c_cpp/src/c/linux/network.c | 1 +
common/c_cpp/src/c/linux/port.h | 19 +++++++++++--------
common/c_cpp/src/c/timers.c | 12 ++++--------
common/c_cpp/src/c/timers.h | 12 ++++++------
common/c_cpp/src/c/windows/network.c | 2 +-
common/c_cpp/src/c/windows/port.c | 28 ++++++++++++++++++++++------
common/c_cpp/src/c/windows/port.h | 9 +++++++--
7 files changed, 52 insertions(+), 31 deletions(-)

diff --git a/common/c_cpp/src/c/linux/network.c b/common/c_cpp/src/c/linux/network.c
index 9969ff9..5c32910 100644
--- a/common/c_cpp/src/c/linux/network.c
+++ b/common/c_cpp/src/c/linux/network.c
@@ -22,6 +22,7 @@
#include <stdlib.h>
#include <stdio.h>
#include <string.h>
+#include <net/if.h>
#include "wombat/wCommon.h"


diff --git a/common/c_cpp/src/c/linux/port.h b/common/c_cpp/src/c/linux/port.h
index c328479..b3a928d 100644
--- a/common/c_cpp/src/c/linux/port.h
+++ b/common/c_cpp/src/c/linux/port.h
@@ -28,15 +28,9 @@
#ifndef PORT_LINUX_H__
#define PORT_LINUX_H__

-#if defined (__cplusplus)
-extern "C"
-{
-#endif
-
#include <pthread.h>
#include <sys/types.h>
#include <sys/ioctl.h>
-#include <sys/mman.h>
#include <sys/vfs.h>
#include <sys/param.h>
#include <sys/resource.h>
@@ -44,9 +38,9 @@ extern "C"
#include <sys/times.h>
#include <sys/socket.h>
#include <sys/utsname.h>
+#include <sys/mman.h>
#include <arpa/inet.h>
#include <netinet/in.h>
-#include <net/if.h>
#include <netdb.h>
#include <semaphore.h>
#include <dirent.h>
@@ -57,6 +51,10 @@ extern "C"

#include "wConfig.h"

+#if defined (__cplusplus)
+extern "C"
+{
+#endif
/* PTHREAD static locks are easy */
typedef pthread_mutex_t wthread_static_mutex_t;
#define WSTATIC_MUTEX_INITIALIZER PTHREAD_MUTEX_INITIALIZER
@@ -108,12 +106,17 @@ typedef pthread_key_t wthread_key_t;
int wsem_timedwait (wsem_t* sem, unsigned int ts);

/* Windows does not support AF_UNIX sockets, socketpairs, etc */
+#define wsocketstartup()
+#define wsocketcleanup()
+
#define wsocketpair(dom, type, prot, pair) (socketpair((dom),(type),(prot),(pair)))
#define wsetnonblock(s) (fcntl((s), F_SETFL, fcntl((s), F_GETFL) | O_NONBLOCK))
+#define wread read
+#define wwrite write

#define CPU_AFFINITY_SET cpu_set_t

-/* User pthreads for linux */
+/* Use pthreads for linux */
#define INVALID_THREAD (-1)

#define wthread_mutex_t pthread_mutex_t
diff --git a/common/c_cpp/src/c/timers.c b/common/c_cpp/src/c/timers.c
index cb93ce8..87ae925 100644
--- a/common/c_cpp/src/c/timers.c
+++ b/common/c_cpp/src/c/timers.c
@@ -84,11 +84,7 @@ int createTimerHeap (timerHeap* heap)

wthread_mutex_init (&heapImpl->mEndingLock, NULL);

- if (wthread_cond_init (&heapImpl->mEndingCond, NULL) != 0)
- {
- free (heapImpl);
- return -1;
- }
+ wthread_cond_init (&heapImpl->mEndingCond, NULL);

RB_INIT (&heapImpl->mTimeTree);

@@ -152,7 +148,7 @@ void* dispatchEntry (void *closure)
int numRead = 0;
do
{
- numRead = read(heap->mSockPair[0], &buff, sizeof (buff));
+ numRead = wread(heap->mSockPair[0], &buff, sizeof (buff));
if (numRead < 0)
{
if (errno == EINTR)
@@ -221,7 +217,7 @@ int destroyHeap (timerHeap heap)
timerHeapImpl* heapImpl = (timerHeapImpl*)heap;

writeagain:
- if (write (heapImpl->mSockPair[1], "d", 1) < 0)
+ if (wwrite (heapImpl->mSockPair[1], "d", 1) < 0)
{
if ((errno == EINTR) || (errno == EAGAIN))
goto writeagain;
@@ -277,7 +273,7 @@ int createTimer (timerElement* timer, timerHeap heap, timerFireCb cb, struct tim
if (kickPipe)
{
writeagain:
- if (write (heapImpl->mSockPair[1], "w", 1) < 0)
+ if (wwrite (heapImpl->mSockPair[1], "w", 1) < 0)
{
if ((errno == EINTR) || (errno == EAGAIN))
goto writeagain;
diff --git a/common/c_cpp/src/c/timers.h b/common/c_cpp/src/c/timers.h
index 22989f3..962163b 100644
--- a/common/c_cpp/src/c/timers.h
+++ b/common/c_cpp/src/c/timers.h
@@ -29,12 +29,12 @@ typedef void* timerHeap;

typedef void (*timerFireCb)(timerElement timer, void* mClosure);

-int createTimerHeap (timerHeap* heap);
-int startDispatchTimerHeap (timerHeap heap);
-wthread_t timerHeapGetTid (timerHeap heap);
-int destroyHeap (timerHeap heap);
+COMMONExpDLL int createTimerHeap (timerHeap* heap);
+COMMONExpDLL int startDispatchTimerHeap (timerHeap heap);
+COMMONExpDLL wthread_t timerHeapGetTid (timerHeap heap);
+COMMONExpDLL int destroyHeap (timerHeap heap);

-int createTimer (timerElement* timer, timerHeap heap, timerFireCb cb, struct timeval* timeout, void* closure);
-int destroyTimer (timerHeap heap, timerElement timer);
+COMMONExpDLL int createTimer (timerElement* timer, timerHeap heap, timerFireCb cb, struct timeval* timeout, void* closure);
+COMMONExpDLL int destroyTimer (timerHeap heap, timerElement timer);

#endif
diff --git a/common/c_cpp/src/c/windows/network.c b/common/c_cpp/src/c/windows/network.c
index b80914e..ce467f6 100644
--- a/common/c_cpp/src/c/windows/network.c
+++ b/common/c_cpp/src/c/windows/network.c
@@ -18,7 +18,7 @@
* 02110-1301 USA
*/

-#include "port.h"
+#include "wombat/port.h"

struct in_addr wresolve_ip (const char * arg)
{
diff --git a/common/c_cpp/src/c/windows/port.c b/common/c_cpp/src/c/windows/port.c
index 12f6431..2b92c90 100644
--- a/common/c_cpp/src/c/windows/port.c
+++ b/common/c_cpp/src/c/windows/port.c
@@ -18,7 +18,7 @@
* 02110-1301 USA
*/

-#include "port.h"
+#include "wombat/port.h"

int gettimeofday( struct timeval *result, void *dummy )
{
@@ -133,10 +133,10 @@ DWORD wthread_cond_wait( HANDLE *event, LPCRITICAL_SECTION *cs )
{
DWORD rval;

- LeaveCriticalSection( *cs );
+ LeaveCriticalSection( cs );
rval = WaitForSingleObject( *event, INFINITE );
ResetEvent( *event );
- EnterCriticalSection( *cs );
+ EnterCriticalSection( cs );

return rval;
}
@@ -177,6 +177,24 @@ const char *getlogin()
}

int
+wsocketstartup ()
+{
+ WSADATA wsaData;
+ int err =WSAStartup(MAKEWORD(2,2), &wsaData);
+ if (err)
+ {
+ return -1;
+ }
+ return 0;
+}
+
+void
+wsocketcleanup ()
+{
+ WSACleanup( );
+}
+
+int
wsocketpair (int domain, int type, int protocol, int* pair)
{
struct sockaddr_in addr;
@@ -184,7 +202,7 @@ wsocketpair (int domain, int type, int protocol, int* pair)
int l;

l = socket (AF_INET, SOCK_STREAM, IPPROTO_TCP);
- if (INVALID_SOCKET == pair[0])
+ if (INVALID_SOCKET == l)
return -1;

ZeroMemory (&addr, sizeof(addr));
@@ -233,8 +251,6 @@ wsocketpair (int domain, int type, int protocol, int* pair)
return -1;
}

- _close (l);
-
return 0;
}

diff --git a/common/c_cpp/src/c/windows/port.h b/common/c_cpp/src/c/windows/port.h
index 92080fd..df06b4f 100644
--- a/common/c_cpp/src/c/windows/port.h
+++ b/common/c_cpp/src/c/windows/port.h
@@ -86,6 +86,11 @@ typedef __int64 w_i64_t;
#define PATH_DELIM ';'

#define PATHSEP "\\"
+COMMONExpDLL int
+wsocketstartup (void);
+
+COMMONExpDLL void
+wsocketcleanup (void);

/* Socket Pair and set non blocking */
COMMONExpDLL int
@@ -143,8 +148,8 @@ int wsem_getvalue (wsem_t*, int* items);
#define strdup _strdup
#define strncasecmp _strnicmp
#define strcasecmp _stricmp
-#define read _read
-#define write _write
+#define wread(x,y,z) recv((x),(y),(z),0)
+#define wwrite(x,y,z) send((x),(y),(z),0)
#define close _close
#define sleep(x) Sleep( (x)*1000)

--
1.7.7.6