[libdispatch-dev] [PATCH 09/17] portability

Paolo Bonzini bonzini at gnu.org
Sun Oct 18 08:04:09 PDT 2009


In particular:
- look for limits.h in addition to BSD-only sys/syslimits.h
- include sys/time.h, not included indirectly under glibc
- fix a difference in pthread_t's underlying type
- remove useless sys/event.h inclusion
- only warn if CPU count cannot be determined
- sem_t is not a scalar under Linux, check if initialized with memcmp
- no strlcpy under Linux, use memcpy instead.  As a small bonus the
  padding is filled as well
---
 configure.ac    |    5 +++++
 src/internal.h  |    6 +++++-
 src/private.h   |    1 -
 src/queue.c     |    5 ++++-
 src/semaphore.c |    9 ++++-----
 src/source.c    |    3 ++-
 6 files changed, 20 insertions(+), 9 deletions(-)

diff --git a/configure.ac b/configure.ac
index eee12e7..608920c 100644
--- a/configure.ac
+++ b/configure.ac
@@ -165,6 +165,11 @@ AC_CHECK_DECLS([SIGEMT], [], [], [[#include <signal.h>]])
 AC_CHECK_DECLS([VQ_UPDATE, VQ_VERYLOWDISK], [], [], [[#include <sys/mount.h>]])
 AC_CHECK_FUNCS([pthread_key_init_np pthread_main_np mach_absolute_time malloc_create_zone sem_init])
 
+AC_CHECK_HEADERS([sys/cdefs.h sys/syslimits.h], [], [],
+  [#ifdef HAVE_SYS_CDEFS_H
+   #include <sys/cdefs.h>
+   #endif])
+
 DISPATCH_C_PRIVATE_EXTERN
 DISPATCH_C_BLOCKS
 
diff --git a/src/internal.h b/src/internal.h
index 8fe987e..40aeac5 100644
--- a/src/internal.h
+++ b/src/internal.h
@@ -91,7 +91,10 @@
 #include <sys/queue.h>
 #include <sys/stat.h>
 #include <sys/sysctl.h>
+#ifdef HAVE_SYS_SYSLIMITS_H
 #include <sys/syslimits.h>
+#endif
+#include <sys/time.h>
 #include <sys/socket.h>
 #include <netinet/in.h>
 
@@ -103,6 +106,7 @@
 #include <assert.h>
 #include <errno.h>
 #include <fcntl.h>
+#include <limits.h>
 #include <search.h>
 #if !defined(HAVE_MACH) && defined(HAVE_SEM_INIT)
 #include <semaphore.h>
@@ -228,7 +232,7 @@ long dummy_function_r0(void);
 #define _dispatch_debug(x, args...)	\
 ({	\
 	if (DISPATCH_DEBUG) {	\
-		_dispatch_log("libdispatch: %u\t%p\t" x, __LINE__, _dispatch_thread_self(), ##args);	\
+		_dispatch_log("libdispatch: %u\t%p\t" x, __LINE__, (void *)_dispatch_thread_self(), ##args);	\
 	}	\
 })
 
diff --git a/src/private.h b/src/private.h
index 87d30c8..6bedd62 100644
--- a/src/private.h
+++ b/src/private.h
@@ -34,7 +34,6 @@
 #endif
 #include <unistd.h>
 #include <sys/cdefs.h>
-#include <sys/event.h>
 #include <pthread.h>
 
 #ifndef __DISPATCH_BUILDING_DISPATCH__
diff --git a/src/queue.c b/src/queue.c
index 97ec7f8..24c54a5 100644
--- a/src/queue.c
+++ b/src/queue.c
@@ -483,7 +483,10 @@ _dispatch_queue_set_width_init(void)
 	    _dispatch_hw_config.cc_max_physical =
 	    _dispatch_hw_config.cc_max_active;
 #else
-#error "_dispatch_queue_set_width_init: no supported way to query CPU count"
+#warning "_dispatch_queue_set_width_init: no supported way to query CPU count"
+	_dispatch_hw_config.cc_max_logical =
+	    _dispatch_hw_config.cc_max_physical =
+	    _dispatch_hw_config.cc_max_active = 1;
 #endif
 }
 
diff --git a/src/semaphore.c b/src/semaphore.c
index aa066be..34edb10 100644
--- a/src/semaphore.c
+++ b/src/semaphore.c
@@ -142,14 +142,13 @@ _dispatch_semaphore_create_port(semaphore_t *s4)
 static void
 _dispatch_posix_semaphore_create(sem_t *s4)
 {
+	static sem_t uninit_sem;
 	int ret;
 
-	if (*s4) {
-		return;
+	if (!memcmp (s4, &uninit_sem, sizeof (sem_t))) {
+	        ret = sem_init(s4, 0, 0);
+		dispatch_assume_zero(ret);
 	}
-
-	ret = sem_init(s4, 0, 0);
-	dispatch_assume_zero(ret);
 }
 #endif /* HAVE_MACH */
 
diff --git a/src/source.c b/src/source.c
index d56bb99..3fcf7b4 100644
--- a/src/source.c
+++ b/src/source.c
@@ -1044,6 +1044,7 @@ dispatch_source_create(dispatch_source_type_t type,
 	const struct kevent *proto_kev = &type->ke;
 	dispatch_source_t ds = NULL;
 	dispatch_kevent_t dk = NULL;
+	static char source_label[sizeof ds->dq_label] = "source";
 
 	// input validation
 	if (type == NULL || (mask & ~type->mask)) {
@@ -1086,7 +1087,7 @@ dispatch_source_create(dispatch_source_type_t type,
 
 	// Initialize as a queue first, then override some settings below.
 	_dispatch_queue_init((dispatch_queue_t)ds);
-	strlcpy(ds->dq_label, "source", sizeof(ds->dq_label));
+	memcpy(ds->dq_label, source_label, sizeof source_label);
 
 	// Dispatch Object
 	ds->do_vtable = &_dispatch_source_kevent_vtable;
-- 
1.6.2.5





More information about the libdispatch-dev mailing list