[PATCH 01/30] Move Linux Specific #includes and Macros #includes


Michael Schonberg <mschonberg@...>
 

This is the first in a series of patches to add Windows suport. Rather
than peppering the source with #ifdef osXXX blocks, os specific headers
and source files go into directory with the same name as the OS. Ideally,
there will be one linux directory in common, one windows, etc.; however,
the same approach should be used in other modules that require os specific
implementations. "#ifdef OS" should be avoided whenever possible.

Each OS directory must at least contain a file "port.h". This file
contains os-specific includes and macros. At some point, it might make
sense to subdivide port.h into several headers (one for networking,
threads, etc.). For the moement we will stick with one until the process
is complete.

We may also want to move some of the linux/ that is common to other os's
like Solaris and BSD to a /unix directory when we start adding support for
other unix-like platforms.

This change creates common/c_cpp/src/c/linux/port.h. All of the #includes
that are not found by the windows compiler are in port.h as well as some
of the obvious linux specific macros.

Signed-off-by: Mike Schonberg <mschonberg@nyx.com>
---
common/c_cpp/configure.ac | 6 ++
common/c_cpp/src/c/MRSWLock.c | 2 +-
common/c_cpp/src/c/Makefile.am | 1 +
common/c_cpp/src/c/destroyHandle.c | 1 +
common/c_cpp/src/c/linux/port.h | 136 ++++++++++++++++++++++++++++++++
common/c_cpp/src/c/list.c | 6 +-
common/c_cpp/src/c/machine.c | 9 +--
common/c_cpp/src/c/platform.c | 5 +-
common/c_cpp/src/c/platform.h | 21 -----
common/c_cpp/src/c/property.c | 4 +-
common/c_cpp/src/c/queue.c | 22 +-----
common/c_cpp/src/c/strutils.c | 7 +-
common/c_cpp/src/c/systree.h | 4 -
common/c_cpp/src/c/timers.h | 3 +-
common/c_cpp/src/c/wSemaphore.c | 8 +--
common/c_cpp/src/c/wlock.c | 3 +-
common/c_cpp/src/c/wlock.h | 5 -
common/c_cpp/src/c/wombat/queue.h | 15 +---
common/c_cpp/src/c/wombat/strutils.h | 7 --
common/c_cpp/src/c/wombat/wSemaphore.h | 51 +------------
common/c_cpp/src/c/wombat/wincompat.h | 1 +
21 files changed, 163 insertions(+), 154 deletions(-)
create mode 100644 common/c_cpp/src/c/linux/port.h

diff --git a/common/c_cpp/configure.ac b/common/c_cpp/configure.ac
index a95eb91..9b08055 100755
--- a/common/c_cpp/configure.ac
+++ b/common/c_cpp/configure.ac
@@ -43,6 +43,12 @@ AM_INIT_AUTOMAKE([foreign])
AM_CONFIG_HEADER([config.h])
AC_CANONICAL_BUILD()

+# Set up symbolic links for os-specific files
+AC_MSG_NOTICE([Configuring symbolic links for $build_os in $srcdir])
+case $build_os in
+ linux*-*) AC_CONFIG_LINKS([src/c/port.h:src/c/linux/port.h]) ;;
+esac
+
# Checks for programs.
AC_PROG_CXX

diff --git a/common/c_cpp/src/c/MRSWLock.c b/common/c_cpp/src/c/MRSWLock.c
index b7d84c3..8cccb7c 100644
--- a/common/c_cpp/src/c/MRSWLock.c
+++ b/common/c_cpp/src/c/MRSWLock.c
@@ -22,8 +22,8 @@
/* ********************************************************** */
/* Includes. */
/* ********************************************************** */
+#include "port.h"
#include "wombat/MRSWLock.h"
-#include "wombat/wincompat.h"
#include <stdlib.h>

/* ********************************************************** */
diff --git a/common/c_cpp/src/c/Makefile.am b/common/c_cpp/src/c/Makefile.am
index bab6069..facce7c 100644
--- a/common/c_cpp/src/c/Makefile.am
+++ b/common/c_cpp/src/c/Makefile.am
@@ -38,6 +38,7 @@ nobase_include_HEADERS = \
property.h \
wlock.h \
timers.h \
+ port.h \
wombat/MRSWLock.h \
wombat/environment.h \
wombat/machine.h \
diff --git a/common/c_cpp/src/c/destroyHandle.c b/common/c_cpp/src/c/destroyHandle.c
index 37a2ae3..cf63ea8 100644
--- a/common/c_cpp/src/c/destroyHandle.c
+++ b/common/c_cpp/src/c/destroyHandle.c
@@ -22,6 +22,7 @@
/* ************************************************************** */
/* Includes. */
/* ************************************************************** */
+#include "port.h"
#include "destroyHandle.h"
#include <stdio.h>

diff --git a/common/c_cpp/src/c/linux/port.h b/common/c_cpp/src/c/linux/port.h
new file mode 100644
index 0000000..a1de75d
--- /dev/null
+++ b/common/c_cpp/src/c/linux/port.h
@@ -0,0 +1,136 @@
+/*
+ * OpenMAMA: The open middleware agnostic messaging API
+ * Copyright (C) 2011 NYSE Inc.
+ *
+ * This library is free software; you can redistribute it and/or
+ * modify it under the terms of the GNU Lesser General Public
+ * License as published by the Free Software Foundation; either
+ * version 2.1 of the License, or (at your option) any later version.
+ *
+ * This library is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
+ * Lesser General Public License for more details.
+ *
+ * You should have received a copy of the GNU Lesser General Public
+ * License along with this library; if not, write to the Free Software
+ * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
+ * 02110-1301 USA
+ */
+
+/* Linux Specific macros, #includes, and prototypes.
+ *
+ * For Linux, most should be macros. Some of the contents of this file may be
+ * moved to a common "unix.h' file when we port to Solaris and other unix
+ * variants.
+ */
+
+#ifndef PORT_LINUX_H__
+#define PORT_LINUX_H__
+
+#if defined (__cplusplus)
+extern "C"
+{
+#endif
+
+#include <pthread.h>
+#include <sys/types.h>
+#include <sys/vfs.h>
+#include <sys/param.h>
+#include <sys/resource.h>
+#include <sys/time.h>
+#include <sys/times.h>
+#include <netinet/in.h>
+#include <semaphore.h>
+#include <dirent.h>
+#include <dlfcn.h>
+#include <unistd.h>
+#include <inttypes.h>
+
+/* Call Types */
+#define COMMONExpDLL
+#define WMWExpDLL
+#define MAMAExpDLL
+#define MAMACPPExpDLL
+#define MAMDAExpDLL
+#define MAMDAOPTExpDLL
+#define MAMAExpBridgeDLL
+#define MAMACALLTYPE
+
+/* Type for handle to dynamically loaded library */
+typedef void* LIB_HANDLE;
+
+/* suffix for shared libraries */
+#define LIB_EXTENSION ".so"
+
+/* 8 byte int typedefs */
+typedef unsigned long long int w_u64_t;
+typedef long long int w_i64_t;
+
+/* Network conversion function */
+#if __BYTE_ORDER == __LITTLE_ENDIAN
+#define htonll(x) \
+ ((uint64_t)htonl((uint32_t)((x)>>32)) | (uint64_t)htonl((uint32_t)(x))<<32)
+#define ntohll(x) \
+ ((uint64_t)ntohl((uint32_t)((x)>>32)) | (uint64_t)ntohl((uint32_t)(x))<<32)
+#else
+#define htonll(x) ((uint64_t)(x))
+#define ntohll(x) ((uint64_t)(x))
+#endif
+
+/* For delimiting multiple paths in env variables properties */
+#define PATH_DELIM ':'
+
+/* Use Posix semaphores for Linux */
+#define wsem_t sem_t
+#define wsem_init sem_init
+#define wsem_post sem_post
+#define wsem_wait sem_wait
+#define wsem_trywait sem_trywait
+#define wsem_destroy sem_destroy
+#define wsem_getvalue sem_getvalue
+
+int wsem_timedwait (wsem_t* sem, unsigned int ts);
+
+/* User pthreads for linux */
+#define wthread_mutex_t pthread_mutex_t
+#define wthread_mutex_init pthread_mutex_init
+#define wthread_mutex_unlock pthread_mutex_unlock
+#define wthread_mutex_lock pthread_mutex_lock
+#define wthread_mutex_destroy pthread_mutex_destroy
+#define wthread_t pthread_t
+#define wthread_detach pthread_detach
+#define wthread_self pthread_self
+#define wthread_equal pthread_equal
+#define wthread_cleanup_push pthread_cleanup_push
+#define wthread_cleanup_pop pthread_cleanup_pop
+#define wthread_join pthread_join
+#define wthread_create pthread_create
+#define wthread_exit pthread_exit
+
+#define wthread_cond_t pthread_cond_t
+#define wthread_cond_init pthread_cond_init
+#define wthread_cond_signal pthread_cond_signal
+#define wthread_cond_destroy pthread_cond_destroy
+#define wthread_cond_wait pthread_cond_wait
+
+#define wthread_spinlock_t pthread_spinlock_t
+#define wthread_spin_init pthread_spin_init
+#define wthread_spin_unlock pthread_spin_unlock
+#define wthread_spin_lock pthread_spin_lock
+#define wthread_spin_destroy pthread_spin_destroy
+#define wthread_attr_t pthread_attr_t
+#define wthread_attr_init pthread_attr_init
+#define wthread_attr_setdetachstate pthread_attr_setdetachstate
+
+#define wthread_mutexattr_t pthread_mutexattr_t
+#define wthread_mutexattr_init pthread_mutexattr_init
+#define wthread_mutexattr_settype pthread_mutexattr_settype
+
+#define wGetCurrentThreadId pthread_self
+
+#if defined (__cplusplus)
+} /* extern "c" */
+#endif
+
+#endif /* LINUX_H__ */
diff --git a/common/c_cpp/src/c/list.c b/common/c_cpp/src/c/list.c
index e37b4b0..ef3a00d 100644
--- a/common/c_cpp/src/c/list.c
+++ b/common/c_cpp/src/c/list.c
@@ -19,11 +19,7 @@
* 02110-1301 USA
*/

-#ifndef WIN32
-#include <pthread.h>
-#else
-#include "wombat/wincompat.h"
-#endif
+#include "port.h"

#include <stdlib.h>
#include <stdio.h>
diff --git a/common/c_cpp/src/c/machine.c b/common/c_cpp/src/c/machine.c
index 42bd9de..512e1ab 100644
--- a/common/c_cpp/src/c/machine.c
+++ b/common/c_cpp/src/c/machine.c
@@ -19,14 +19,10 @@
* 02110-1301 USA
*/

-#include <sys/param.h>
-#include <sys/resource.h>
+#include "port.h"
#include <sys/stat.h>
-#include <sys/time.h>
-#include <sys/times.h>

#include <ctype.h>
-#include <dirent.h>
#include <errno.h>
#include <fcntl.h>
#include <limits.h>
@@ -34,10 +30,7 @@
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
-#include <sys/vfs.h>
#include <time.h>
-#include <unistd.h>
-#include <unistd.h>

#include "wombat/machine.h"
#include "wombat/wtable.h"
diff --git a/common/c_cpp/src/c/platform.c b/common/c_cpp/src/c/platform.c
index 0ed6d18..8bf7f4e 100644
--- a/common/c_cpp/src/c/platform.c
+++ b/common/c_cpp/src/c/platform.c
@@ -19,6 +19,7 @@
* 02110-1301 USA
*/

+#include "port.h"
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
@@ -27,10 +28,6 @@
#include "platform.h"
#include "wombat/wincompat.h"

-
-#define LIB_EXTENSION ".so"
-
-
/**
* Functions relating to DLLs/shared objects
*/
diff --git a/common/c_cpp/src/c/platform.h b/common/c_cpp/src/c/platform.h
index c3b17d6..440f962 100644
--- a/common/c_cpp/src/c/platform.h
+++ b/common/c_cpp/src/c/platform.h
@@ -25,16 +25,6 @@
#if defined(__cplusplus)
extern "C" {
#endif /* __cplusplus */
-#include "wombat/wincompat.h"
-#include <dlfcn.h>
-#include <sys/types.h>
-#include <netinet/in.h>
-
-#ifdef WIN32
-typedef HINSTANCE LIB_HANDLE;
-#else
-typedef void* LIB_HANDLE;
-#endif

/* The platform specific prefix and postfix will automatically be added to the libname
e.g on Linux "mamaImpl" will load "libmamaimpl.so" */
@@ -50,17 +40,6 @@ void* loadLibFunc (LIB_HANDLE handle, const char* funcName);
COMMONExpDLL
char* getLibError (void);

-/* Network conversion function */
-#if defined(WIN32) || __BYTE_ORDER == __LITTLE_ENDIAN
-#define htonll(x) \
- ((uint64_t)htonl((uint32_t)((x)>>32)) | (uint64_t)htonl((uint32_t)(x))<<32)
-#define ntohll(x) \
- ((uint64_t)ntohl((uint32_t)((x)>>32)) | (uint64_t)ntohl((uint32_t)(x))<<32)
-#else
-#define htonll(x) ((uint64_t)(x))
-#define ntohll(x) ((uint64_t)(x))
-#endif
-
#if defined(__cplusplus)
}
#endif /* __cplusplus */
diff --git a/common/c_cpp/src/c/property.c b/common/c_cpp/src/c/property.c
index 456c08f..a5271ec 100644
--- a/common/c_cpp/src/c/property.c
+++ b/common/c_cpp/src/c/property.c
@@ -19,6 +19,8 @@
* 02110-1301 USA
*/

+#include "port.h"
+
#include <stdlib.h>
#include <stdio.h>
#include <string.h>
@@ -32,8 +34,6 @@

#define KEY_BLOCK_SIZE 64

-#define PATH_DELIM ':'
-
int gPropertyDebug = 0;

typedef struct
diff --git a/common/c_cpp/src/c/queue.c b/common/c_cpp/src/c/queue.c
index ce960d1..03cf678 100644
--- a/common/c_cpp/src/c/queue.c
+++ b/common/c_cpp/src/c/queue.c
@@ -19,16 +19,9 @@
* 02110-1301 USA
*/

-#ifndef WIN32
-#include <pthread.h>
-#else
-#include "wombat/wincompat.h"
-#define pthread_mutex_t wthread_mutex_t
-#define pthread_mutex_init wthread_mutex_init
-#define pthread_mutex_destroy wthread_mutex_destroy
-#define pthread_mutex_lock wthread_mutex_lock
-#define pthread_mutex_unlock wthread_mutex_unlock
-#endif
+
+#include "port.h"
+
#include "wombat/wSemaphore.h"

#include <errno.h>
@@ -120,14 +113,10 @@ wombatQueue_create (wombatQueue queue, uint32_t maxSize, uint32_t initialSize,
return WOMBAT_QUEUE_SEM_ERR;
}

-#ifndef WIN32
if (pthread_mutex_init( &impl->mLock, NULL) != 0)
{
return WOMBAT_QUEUE_MTX_ERR;
}
-#else
- wthread_mutex_init( &impl->mLock, NULL);
-#endif

initialSize = initialSize == 0 ? WOMBAT_QUEUE_CHUNK_SIZE : initialSize;
wombatQueueImpl_allocChunk (impl, initialSize);
@@ -173,15 +162,10 @@ wombatQueue_destroy (wombatQueue queue)
result = WOMBAT_QUEUE_SEM_ERR;
}

-#ifndef WIN32
if (pthread_mutex_destroy( &impl->mLock) != 0)
{
result = WOMBAT_QUEUE_MTX_ERR;
}
-#else
- wthread_mutex_destroy( &impl->mLock);
-#endif
-

free (impl);
return WOMBAT_QUEUE_OK;
diff --git a/common/c_cpp/src/c/strutils.c b/common/c_cpp/src/c/strutils.c
index 1fae9b7..4874fe6 100644
--- a/common/c_cpp/src/c/strutils.c
+++ b/common/c_cpp/src/c/strutils.c
@@ -23,17 +23,14 @@
#define __STDC_FORMAT_MACROS
#endif

+#include "port.h"
+
#include <stdio.h>
#include <string.h>
#include <assert.h>
#include <stdlib.h>
#include <stdarg.h>
#include <ctype.h>
-#ifndef WIN32
-#include <inttypes.h>
-#else
-#include <wombat/wincompat.h>
-#endif
#include <wombat/strutils.h>

static const char* gIntegerStrings[] = {
diff --git a/common/c_cpp/src/c/systree.h b/common/c_cpp/src/c/systree.h
index f5a15cc..818be5a 100644
--- a/common/c_cpp/src/c/systree.h
+++ b/common/c_cpp/src/c/systree.h
@@ -24,10 +24,6 @@
* THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
*/

-#ifdef WIN32
-#define __attribute__(a)
-#endif
-
#ifndef _SYS_TREE_H_
#define _SYS_TREE_H_

diff --git a/common/c_cpp/src/c/timers.h b/common/c_cpp/src/c/timers.h
index a653dc4..38a3732 100644
--- a/common/c_cpp/src/c/timers.h
+++ b/common/c_cpp/src/c/timers.h
@@ -22,8 +22,7 @@
#ifndef TIMERS_INTERNAL_
#define TIMERS_INTERNAL_

-#include <sys/time.h>
-#include <pthread.h>
+#include "port.h"

typedef void* timerElement;
typedef void* timerHeap;
diff --git a/common/c_cpp/src/c/wSemaphore.c b/common/c_cpp/src/c/wSemaphore.c
index 04b526f..df2bae1 100644
--- a/common/c_cpp/src/c/wSemaphore.c
+++ b/common/c_cpp/src/c/wSemaphore.c
@@ -19,15 +19,11 @@
* 02110-1301 USA
*/

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

#ifndef WIN32
-#include <pthread.h>
-#include <semaphore.h>
-#include <time.h>
-#include <sys/time.h>
-

int wsem_timedwait (wsem_t* sem, unsigned int timeoutval)
{
diff --git a/common/c_cpp/src/c/wlock.c b/common/c_cpp/src/c/wlock.c
index 309b89d..9cd7d21 100644
--- a/common/c_cpp/src/c/wlock.c
+++ b/common/c_cpp/src/c/wlock.c
@@ -19,7 +19,8 @@
* 02110-1301 USA
*/

-#include <pthread.h>
+#include "port.h"
+
#include <stdlib.h>
#include <stdio.h>
#include <string.h>
diff --git a/common/c_cpp/src/c/wlock.h b/common/c_cpp/src/c/wlock.h
index 4c5a82c..199dad8 100644
--- a/common/c_cpp/src/c/wlock.h
+++ b/common/c_cpp/src/c/wlock.h
@@ -22,11 +22,6 @@
#ifndef _WOMBAT_WLOCK_H
#define _WOMBAT_WLOCK_H

-
-#include "wombat/wincompat.h"
-
-
-
typedef void * wLock;

COMMONExpDLL wLock wlock_create( void );
diff --git a/common/c_cpp/src/c/wombat/queue.h b/common/c_cpp/src/c/wombat/queue.h
index 36590d0..e9e2093 100644
--- a/common/c_cpp/src/c/wombat/queue.h
+++ b/common/c_cpp/src/c/wombat/queue.h
@@ -25,20 +25,7 @@
/*
* Fully thread safe queue implementation.
*/
-
-#ifndef WIN32
-#include <semaphore.h>
-#include <inttypes.h>
-#else
-#include "wombat/wincompat.h"
-#endif
-
-#ifndef WIN32
-/* For Forte */
-#ifndef SEM_VALUE_MAX
-#define SEM_VALUE_MAX 2147483647 /* from headers */
-#endif
-
+#ifdef SEM_VALUE_MAX
#define WOMBAT_QUEUE_MAX_SIZE SEM_VALUE_MAX /* 2_147_483_647 on Linux */
#else
#define WOMBAT_QUEUE_MAX_SIZE 2147483647 /* max possible size */
diff --git a/common/c_cpp/src/c/wombat/strutils.h b/common/c_cpp/src/c/wombat/strutils.h
index b20e712..d5d1fb8 100644
--- a/common/c_cpp/src/c/wombat/strutils.h
+++ b/common/c_cpp/src/c/wombat/strutils.h
@@ -30,13 +30,6 @@ extern "C" {


typedef unsigned long int w_u32_t;
-#ifndef WIN32
-typedef unsigned long long int w_u64_t;
-typedef long long int w_i64_t;
-#else
-typedef unsigned __int64 w_u64_t;
-typedef __int64 w_i64_t;
-#endif
typedef long int w_i32_t;
typedef float w_f32_t;
typedef double w_f64_t;
diff --git a/common/c_cpp/src/c/wombat/wSemaphore.h b/common/c_cpp/src/c/wombat/wSemaphore.h
index 89949a6..9adb8d2 100644
--- a/common/c_cpp/src/c/wombat/wSemaphore.h
+++ b/common/c_cpp/src/c/wombat/wSemaphore.h
@@ -22,55 +22,6 @@
#ifndef WSEMAPHORE_H__
#define WSEMAPHORE_H__

-#ifndef WIN32
-
-#include <pthread.h>
-#include <semaphore.h>
-#include <time.h>
-
-#define wsem_t sem_t
-#define wsem_init sem_init
-#define wsem_post sem_post
-#define wsem_wait sem_wait
-#define wsem_trywait sem_trywait
-#define wsem_destroy sem_destroy
-#define wsem_getvalue sem_getvalue
-
-int wsem_timedwait (wsem_t* sem, unsigned int ts);
-
-#else
-#include "wombat/wincompat.h"
-
-/**
- * Waiting on Win32 semaphore involves a system call and is very inefficient.
- * When we test to determine if the queue is empty with a Win32 semaphore
- * WaitForSingleObject consumes all the CPU. We can be much more efficient
- * using a critical section and not call WaitForSingleObject when there is
- * nothing on the Queue.
- */
-typedef void* wsem_t;
-
-COMMONExpDLL
-int wsem_init (wsem_t* sem, int dummy, int count);
-
-COMMONExpDLL
-int wsem_destroy (wsem_t* sem);
-
-COMMONExpDLL
-int wsem_post (wsem_t* sem);
-
-COMMONExpDLL
-int wsem_wait (wsem_t* sem);
-
-COMMONExpDLL
-int wsem_timedwait (wsem_t* sem, unsigned int ts);
-
-COMMONExpDLL
-int wsem_trywait (wsem_t* sem);
-
-COMMONExpDLL
-int wsem_getvalue (wsem_t*, int* items);
-
-#endif
+#include "port.h"

#endif /* WSEMAPHORE_H__ */
diff --git a/common/c_cpp/src/c/wombat/wincompat.h b/common/c_cpp/src/c/wombat/wincompat.h
index 5c78acb..756ecea 100644
--- a/common/c_cpp/src/c/wombat/wincompat.h
+++ b/common/c_cpp/src/c/wombat/wincompat.h
@@ -22,6 +22,7 @@
#ifndef _WOMBAT_WINCOMPAT_H
#define _WOMBAT_WINCOMPAT_H

+#include "port.h"
#include "wConfig.h"

#define PATHSEP "/"
--
1.7.7.6