From d725022844380852476dbef00d7dc3c2e7a37fdb Mon Sep 17 00:00:00 2001 From: Andreas Baumann Date: Mon, 31 Oct 2011 19:54:01 +0100 Subject: added a HAVE_THREADS flag --- README | 14 ++++++++++++-- makefiles/gmake/compiler.mk | 4 ++++ makefiles/gmake/platform.mk | 12 ++++++++++++ src/GNUmakefile | 5 ++++- src/port/netdb.c | 8 ++++++++ src/port/string.c | 8 ++++++++ tests/GNUmakefile | 6 +++++- 7 files changed, 53 insertions(+), 4 deletions(-) diff --git a/README b/README index a18221f..d632dbe 100644 --- a/README +++ b/README @@ -16,7 +16,18 @@ Installation: * call ' all' * call ' install' -Customization of the build process: +Configuring the build process: + +Several makefile variable can be used to turn on or off certain features which +may not be available on all platforms: + +ENABLE_NLS=0 disables natural language support using libintl/gettext +(default is enabled) + +ENABLE_THREADS=0 disables usage of POSIX threads +(default is enabled) + +Extending the build process: * Check out the file 'makefiles/gmake/compiler.mk' for compiler settings, choose them at will @@ -30,4 +41,3 @@ New unsupported platforms, architectures, compilers the proper macros fitting to your new environment * You may have to extend existing modules (e. g. HP Unix loader in 'src/library/loader.c') - diff --git a/makefiles/gmake/compiler.mk b/makefiles/gmake/compiler.mk index 38342ce..15f2452 100644 --- a/makefiles/gmake/compiler.mk +++ b/makefiles/gmake/compiler.mk @@ -193,6 +193,8 @@ endif # end of pcc section +ifeq "$(ENABLE_THREADS)" "1" + # set flags for threading support using POSIX threads. This is completly different # between compiler/platforms ifeq "$(COMPILER)" "gcc" @@ -252,6 +254,8 @@ PTHREADS_LIBS = endif endif +endif + # set flags for position independend code (as required for shared libraries # on some platforms) ifeq "$(COMPILER)" "gcc" diff --git a/makefiles/gmake/platform.mk b/makefiles/gmake/platform.mk index 6f94216..d5fb525 100644 --- a/makefiles/gmake/platform.mk +++ b/makefiles/gmake/platform.mk @@ -100,6 +100,7 @@ LIBS_DL = endif endif + # i18n, gettext/libintl ####################### @@ -219,3 +220,14 @@ LIBS_NET = endif PLATFORM_COMPILE_FLAGS += $(INCLUDE_FLAGS_NET) + +# threading support +################### + +# enable or disable threading +ENABLE_THREADS = 1 + +ifeq "$(ENABLE_NLS)" "1" +PLATFORM_COMPILE_FLAGS += \ + -DENABLE_THREADS=$(ENABLE_THREADS) +endif diff --git a/src/GNUmakefile b/src/GNUmakefile index ab948de..dd111de 100644 --- a/src/GNUmakefile +++ b/src/GNUmakefile @@ -6,9 +6,12 @@ INCLUDE_DIRS = -I$(TOPDIR)/include/wolf -I. BINS = -THREADING_OBJS = \ +THREADING_OBJS = +ifeq "$(ENABLE_THREADS)" "1" +THREADING_OBJS = $(THREADING_OBJS) \ threads/mutex.o \ threads/threads.o +endif PORT_OBJS = \ port/string.o \ diff --git a/src/port/netdb.c b/src/port/netdb.c index b913ef9..ea4458e 100644 --- a/src/port/netdb.c +++ b/src/port/netdb.c @@ -236,31 +236,39 @@ const char *wolf_port_gai_strerror( int errcode ) { #include "port/string.h" /* for strncmp */ #include /* for errno */ +#ifdef HAVE_THREADS static bool mutex_initialized = false; static wolf_mutex_t mutex; +#endif int wolf_port_gai_strerror_r( int errcode, char *buf, size_t buflen ) { int safe_errno = errno; const char *msg; /* critical section as gai_strerror is not thread-safe */ +#ifdef HAVE_THREADS if( !mutex_initialized ) { wolf_mutex_init( &mutex ); mutex_initialized = true; } wolf_mutex_lock( &mutex ); +#endif msg = gai_strerror( errcode ); if( strncmp( msg, "Unknown GAI errror", strlen( "Unknown GAI error" ) ) == 0 /* our own stub implementation */ ) { (void)snprintf( buf, buflen, "Unknown GAI error %d", errcode ); errno = EINVAL; +#ifdef HAVE_THREADS wolf_mutex_unlock( &mutex ); +#endif return -1; } strncpy( buf, msg, buflen-1 ); errno = safe_errno; +#ifdef HAVE_THREADS wolf_mutex_unlock( &mutex ); +#endif return 0; } diff --git a/src/port/string.c b/src/port/string.c index 398788c..9c360d0 100644 --- a/src/port/string.c +++ b/src/port/string.c @@ -49,19 +49,23 @@ char *wolf_port_strdup( const char *s ) { #include /* for errno */ #include "threads/mutex.h" /* for mutexes */ +#ifdef HAVE_THREADS static bool mutex_initialized = false; static wolf_mutex_t mutex; +#endif int wolf_port_strerror_r( int num, char *buf, size_t buflen ) { int safe_errno = errno; const char *msg; /* critical section as strerror is not thread-safe */ +#ifdef HAVE_THREADS if( !mutex_initialized ) { wolf_mutex_init( &mutex ); mutex_initialized = true; } wolf_mutex_lock( &mutex ); +#endif msg = strerror( num ); /* TODO: can we detect illegal error numbers? For sure on Linux NULL @@ -73,13 +77,17 @@ int wolf_port_strerror_r( int num, char *buf, size_t buflen ) { /* Linux returns an empty string in this case, why? */ (void)snprintf( buf, buflen, "Unknown error %d", num ); errno = EINVAL; +#ifdef HAVE_THREADS wolf_mutex_unlock( &mutex ); +#endif return -1; } strncpy( buf, msg, buflen-1 ); errno = safe_errno; +#ifdef HAVE_THREADS wolf_mutex_unlock( &mutex ); +#endif return 0; } diff --git a/tests/GNUmakefile b/tests/GNUmakefile index ba00e61..e2d4b28 100644 --- a/tests/GNUmakefile +++ b/tests/GNUmakefile @@ -1,6 +1,10 @@ TOPDIR = .. -SUBDIRS = threads port log gettext network daemon service library +SUBDIRS = port log gettext network daemon service library + +ifeq "$(ENABLE_THREADS)" "1" +SUBDIRS = $(SUBDIRS) threads +endif -include $(TOPDIR)/makefiles/gmake/sub.mk -- cgit v1.2.3-54-g00ecf