[PATCH] Cleanup config defines & add a check_all script
by Angus Salkeld
Run:
./check_all
to build with and without some common configurations.
Signed-off-by: Angus Salkeld <asalkeld(a)redhat.com>
---
check_all | 33 +++++++++++++++++++++++++++
configure.ac | 28 ++++-------------------
include/os_base.h | 32 ++++++++++++++++++++++++++
include/qb/qbutil.h | 9 ++++++-
lib/ipc_int.h | 17 +------------
lib/ipc_posix_mq.c | 2 +-
lib/loop_poll.c | 25 ++++++++++----------
lib/ringbuffer_helper.c | 6 ++--
lib/util.c | 57 +++++++++++++++++++++++++++++++---------------
tests/check_loop.c | 2 +-
10 files changed, 135 insertions(+), 76 deletions(-)
create mode 100755 check_all
diff --git a/check_all b/check_all
new file mode 100755
index 0000000..d0b8a31
--- /dev/null
+++ b/check_all
@@ -0,0 +1,33 @@
+#!/bin/bash
+
+./autogen.sh
+
+
+check () {
+ echo "$1"
+ ( ./configure $1 --enable-fatal-warnings )
+ make check
+ if [ $? -ne 0 ]
+ then
+ echo "======================"
+ echo failed: $1
+ echo "======================"
+ exit 1
+ fi
+}
+
+# use sys-v semaphores
+check "CFLAGS=-DDISABLE_POSIX_THREAD_PROCESS_SHARED"
+
+# now timerfd
+check "ac_cv_func_timerfd_create=no ac_cv_header_sys_timerfd_h=no"
+
+# no clock_gettime
+check "ac_cv_func_clock_gettime=no"
+
+# no epoll
+check "ac_cv_func_epoll_create1=no ac_cv_func_epoll_create=no"
+
+# bsd-like
+check "ac_cv_func_timerfd_create=no ac_cv_header_sys_timerfd_h=no ac_cv_func_clock_gettime=no ac_cv_func_epoll_create1=no ac_cv_func_epoll_create=no"
+
diff --git a/configure.ac b/configure.ac
index c76aabc..152e287 100644
--- a/configure.ac
+++ b/configure.ac
@@ -111,7 +111,7 @@ AC_HEADER_SYS_WAIT
AC_CHECK_HEADERS([arpa/inet.h fcntl.h inttypes.h limits.h netinet/in.h stdint.h \
time.h sys/time.h stdlib.h string.h strings.h sys/types.h sys/stat.h \
- sys/param.h sys/socket.h sys/time.h sys/timerfd.h \
+ sys/param.h sys/socket.h sys/time.h sys/timerfd.h sys/epoll.h \
sys/sockio.h sys/un.h syslog.h errno.h unistd.h])
# Checks for typedefs, structures, and compiler characteristics.
@@ -129,7 +129,6 @@ AC_TYPE_UINT64_T
# Checks for library functions.
AC_FUNC_CHOWN
-AC_FUNC_ERROR_AT_LINE
AC_FUNC_FORK
AC_FUNC_MALLOC
AC_FUNC_MMAP
@@ -245,7 +244,7 @@ case "$host_os" in
*linux*)
AC_DEFINE_UNQUOTED([QB_LINUX], [1],
[Compiling for Linux platform])
- LINT_FLAGS+=" -expect 22"
+ LINT_FLAGS+=" -expect 21"
;;
darwin*)
AC_DEFINE_UNQUOTED([QB_DARWIN], [1],
@@ -257,6 +256,8 @@ case "$host_os" in
[Number of chars in a path name including nul])
AC_DEFINE_UNQUOTED([NAME_MAX], [255],
[Number of chars in a file name])
+ AC_DEFINE_UNQUOTED([DISABLE_POSIX_THREAD_PROCESS_SHARED], [1],
+ [Disable _POSIX_THREAD_PROCESS_SHARED])
;;
*bsd*)
AC_DEFINE_UNQUOTED([QB_BSD], [1],
@@ -354,26 +355,7 @@ else
COVERAGE_LDFLAGS=""
fi
-# --- timerfd ---
-use_timerfd=no
-if test "x${enable_timerfd}" = xauto ; then
- if test "x${ac_cv_func_timerfd_create}" = xyes ; then
- use_timerfd=yes
- fi
-fi
-if test "x${enable_timerfd}" = xyes ; then
- if test "x${ac_cv_func_timerfd_create}" = xyes ; then
- use_timerfd=yes
- else
- AC_MSG_ERROR([Asked for timerfd but none available])
- fi
-fi
-if test "x$use_timerfd" = xyes ; then
- AC_MSG_NOTICE([Enabling timerfd timers])
- AC_DEFINE_UNQUOTED([USE_TIMERFD], 1, [use timerfd timers])
- PACKAGE_FEATURES="$PACKAGE_FEATURES timerfd"
-fi
-AM_CONDITIONAL(USE_TIMERFD, [test "x$use_timerfd" = xyes])
+AM_CONDITIONAL(USE_TIMERFD, [test "x$ac_cv_func_timerfd_create" = xyes])
# --- ansi ---
if test "x${enable_ansi}" = xyes && \
diff --git a/include/os_base.h b/include/os_base.h
index 6123289..02d5c84 100644
--- a/include/os_base.h
+++ b/include/os_base.h
@@ -43,6 +43,8 @@
#include <stdint.h>
#endif /* HAVE_STDINT_H */
+#include <inttypes.h>
+
#include <assert.h>
#ifdef HAVE_STDLIB_H
@@ -93,4 +95,34 @@
#endif /* HAVE_SYSLOG_H */
#endif /* S_SPLINT_S */
+#if defined HAVE_CLOCK_GETTIME && defined _POSIX_MONOTONIC_CLOCK && _POSIX_MONOTONIC_CLOCK >= 0
+#define HAVE_MONOTONIC_CLOCK 1
+#endif /* have monotonic clock */
+
+#ifdef HAVE_TIMERFD_CREATE
+#define HAVE_TIMERFD 1
+#endif /* HAVE_TIMERFD_CREATE */
+
+#ifdef HAVE_EPOLL_CREATE1
+#define HAVE_EPOLL 1
+#endif /* HAVE_EPOLL_CREATE */
+
+/*
+ * Darwin claims to support process shared synchronization
+ * but it really does not. The unistd.h header file is wrong.
+ */
+#if defined(DISABLE_POSIX_THREAD_PROCESS_SHARED) || defined(__UCLIBC__)
+#undef HAVE_POSIX_SHARED_SEMAPHORE
+#undef HAVE_PTHREAD_SHARED_SPIN_LOCK
+#else
+#if _POSIX_THREAD_PROCESS_SHARED > 0
+#define HAVE_POSIX_SHARED_SEMAPHORE 1
+
+#if defined(HAVE_PTHREAD_SPIN_LOCK)
+#define HAVE_PTHREAD_SHARED_SPIN_LOCK 1
+#endif /* HAVE_PTHREAD_SPIN_LOCK */
+
+#endif /* _POSIX_THREAD_PROCESS_SHARED */
+#endif /* DISABLE_POSIX_THREAD_PROCESS_SHARED */
+
#endif /* QB_OS_BASE_H_DEFINED */
diff --git a/include/qb/qbutil.h b/include/qb/qbutil.h
index 58420bc..39ca68f 100644
--- a/include/qb/qbutil.h
+++ b/include/qb/qbutil.h
@@ -96,7 +96,7 @@ void qb_util_set_log_function(qb_util_log_fn_t fn);
void qb_timespec_add_ms(struct timespec *ts, int32_t ms);
/**
- * Get the current number of nanao secounds produced
+ * Get the current number of nano secounds produced
* by the systems incrementing clock (CLOCK_MONOTOMIC
* if available).
*/
@@ -113,6 +113,13 @@ uint64_t qb_util_nano_monotonic_hz(void);
*/
uint64_t qb_util_nano_from_epoch_get(void);
+/**
+ * Get the time in timespec since epoch.
+ * @param ts (out) the timespec
+ * @return status (0 == ok, -errno on error)
+ */
+void qb_util_timespec_from_epoch_get(struct timespec *ts);
+
/* *INDENT-OFF* */
#ifdef __cplusplus
}
diff --git a/lib/ipc_int.h b/lib/ipc_int.h
index 24bfc2a..4d607e8 100644
--- a/lib/ipc_int.h
+++ b/lib/ipc_int.h
@@ -33,22 +33,9 @@
#include <qb/qbipc_common.h>
#include <qb/qbrb.h>
-/*
- * Darwin claims to support process shared synchronization
- * but it really does not. The unistd.h header file is wrong.
- */
-#if defined(QB_DARWIN) || defined(__UCLIBC__)
-#undef _POSIX_THREAD_PROCESS_SHARED
-#define _POSIX_THREAD_PROCESS_SHARED -1
-#endif
-
-#ifndef _POSIX_THREAD_PROCESS_SHARED
-#define _POSIX_THREAD_PROCESS_SHARED -1
-#endif
-
-#if _POSIX_THREAD_PROCESS_SHARED > 0
+#ifdef HAVE_POSIX_SHARED_SEMAPHORE
#include <semaphore.h>
-#endif
+#endif /* HAVE_POSIX_SHARED_SEMAPHORE */
/*
Client Server
diff --git a/lib/ipc_posix_mq.c b/lib/ipc_posix_mq.c
index 2fd5868..48df755 100644
--- a/lib/ipc_posix_mq.c
+++ b/lib/ipc_posix_mq.c
@@ -192,7 +192,7 @@ static ssize_t qb_ipc_pmq_recv(struct qb_ipc_one_way *one_way,
ssize_t res;
if (ms_timeout >= 0) {
- clock_gettime(CLOCK_REALTIME, &ts_timeout);
+ qb_util_timespec_from_epoch_get(&ts_timeout);
qb_timespec_add_ms(&ts_timeout, ms_timeout);
}
diff --git a/lib/loop_poll.c b/lib/loop_poll.c
index 3583679..2067f9c 100644
--- a/lib/loop_poll.c
+++ b/lib/loop_poll.c
@@ -21,10 +21,9 @@
#include "os_base.h"
#include <sys/resource.h>
-#ifdef HAVE_EPOLL_CREATE1
+#ifdef HAVE_SYS_EPOLL_H
#include <sys/epoll.h>
-#define HAVE_EPOLL 1
-#endif /* HAVE_EPOLL_CREATE */
+#endif /* HAVE_SYS_EPOLL_H */
#include <sys/poll.h>
#ifndef S_SPLINT_S
#ifdef HAVE_SYS_TIMERFD_H
@@ -227,7 +226,7 @@ static int32_t poll_and_add_to_jobs(struct qb_loop_source* src, int32_t ms_timeo
poll_fds_usage_check(s);
for (i = 0; i < s->poll_entry_count; i++) {
- assert(qb_array_index(l->fd_source->poll_entries, i, (void**)&pe) == 0);
+ assert(qb_array_index(s->poll_entries, i, (void**)&pe) == 0);
memcpy(&s->ufds[i], &pe->ufd, sizeof(struct pollfd));
}
@@ -244,7 +243,7 @@ static int32_t poll_and_add_to_jobs(struct qb_loop_source* src, int32_t ms_timeo
// empty
continue;
}
- assert(qb_array_index(l->fd_source->poll_entries, i, (void**)&pe) == 0);
+ assert(qb_array_index(s->poll_entries, i, (void**)&pe) == 0);
if (s->ufds[i].revents == pe->ufd.revents) {
// entry already in the job queue.
continue;
@@ -370,8 +369,6 @@ static int32_t _poll_add_(struct qb_loop *l,
struct qb_poll_source * s;
#ifdef HAVE_EPOLL
struct epoll_event *ev;
-#else
- struct pollfd *ufds;
#endif /* HAVE_EPOLL */
if (l == NULL) {
@@ -412,7 +409,7 @@ int32_t qb_loop_poll_add(struct qb_loop *l,
void *data,
qb_loop_poll_dispatch_fn dispatch_fn)
{
- struct qb_poll_entry *pe;
+ struct qb_poll_entry *pe = NULL;
int32_t res = _poll_add_(l, p, fd, events, data, &pe);
pe->poll_dispatch_fn = dispatch_fn;
pe->type = QB_POLL;
@@ -495,7 +492,7 @@ int32_t qb_loop_poll_del(struct qb_loop *l, int32_t fd)
return -EBADF;
}
-#ifdef USE_TIMERFD
+#ifdef HAVE_TIMERFD
int32_t qb_loop_timer_msec_duration_to_expire(struct qb_loop_source *timer_source)
{
return 0;
@@ -573,7 +570,9 @@ int32_t qb_loop_timer_del(struct qb_loop *l, qb_loop_timer_handle th)
{
struct qb_poll_entry *pe;
struct qb_poll_source *s;
+#ifdef HAVE_EPOLL
int32_t res;
+#endif /* HAVE_EPOLL */
if (l == NULL || th == NULL) {
return -EINVAL;
@@ -593,9 +592,9 @@ int32_t qb_loop_timer_del(struct qb_loop *l, qb_loop_timer_handle th)
return res;
}
#else
- s->ufds[i].fd = -1;
- s->ufds[i].events = 0;
- s->ufds[i].revents = 0;
+ s->ufds[pe->install_pos].fd = -1;
+ s->ufds[pe->install_pos].events = 0;
+ s->ufds[pe->install_pos].revents = 0;
#endif /* HAVE_EPOLL */
close(pe->ufd.fd);
@@ -623,5 +622,5 @@ uint64_t qb_loop_timer_expire_time_get(struct qb_loop *l, qb_loop_timer_handle t
return (its.it_value.tv_sec * QB_TIME_NS_IN_SEC) + its.it_value.tv_nsec;
}
-#endif /* USE_TIMERFD */
+#endif /* HAVE_TIMERFD */
diff --git a/lib/ringbuffer_helper.c b/lib/ringbuffer_helper.c
index 08548ab..9d96f13 100644
--- a/lib/ringbuffer_helper.c
+++ b/lib/ringbuffer_helper.c
@@ -27,7 +27,7 @@ static int32_t my_posix_sem_timedwait(qb_ringbuffer_t * rb, int32_t ms_timeout)
int32_t res;
if (ms_timeout > 0) {
- clock_gettime(CLOCK_REALTIME, &ts_timeout);
+ qb_util_timespec_from_epoch_get(&ts_timeout);
qb_timespec_add_ms(&ts_timeout, ms_timeout);
}
@@ -250,9 +250,9 @@ static int32_t my_sysv_sem_create(qb_ringbuffer_t * rb, uint32_t flags)
int32_t qb_rb_sem_create(struct qb_ringbuffer_s * rb, uint32_t flags)
{
int32_t can_use_shared_posix = QB_FALSE;
-#if _POSIX_THREAD_PROCESS_SHARED > 0
+#ifdef HAVE_POSIX_SHARED_SEMAPHORE
can_use_shared_posix = QB_TRUE;
-#endif
+#endif /* HAVE_POSIX_SHARED_SEMAPHORE */
if (!can_use_shared_posix && (rb->flags & QB_RB_FLAG_SHARED_PROCESS)) {
rb->sem_timedwait_fn = my_sysv_sem_timedwait;
rb->sem_post_fn = my_sysv_sem_post;
diff --git a/lib/util.c b/lib/util.c
index ed39c2e..336b4a9 100644
--- a/lib/util.c
+++ b/lib/util.c
@@ -24,20 +24,15 @@
#include <sys/shm.h>
#include <sys/mman.h>
#include <pthread.h>
-#if _POSIX_THREAD_PROCESS_SHARED > 0
-#include <semaphore.h>
-#else
-#include <sys/sem.h>
-#endif
#include <sys/stat.h>
#include <qb/qbdefs.h>
#include <qb/qbutil.h>
struct qb_thread_lock_s {
qb_thread_lock_type_t type;
-#if defined(HAVE_PTHREAD_SPIN_LOCK)
+#ifdef HAVE_PTHREAD_SHARED_SPIN_LOCK
pthread_spinlock_t spinlock;
-#endif
+#endif /* HAVE_PTHREAD_SHARED_SPIN_LOCK */
pthread_mutex_t mutex;
};
@@ -46,14 +41,12 @@ qb_thread_lock_t *qb_thread_lock_create(qb_thread_lock_type_t type)
struct qb_thread_lock_s *tl = malloc(sizeof(struct qb_thread_lock_s));
int32_t res;
-#if defined(HAVE_PTHREAD_SPIN_LOCK)
-#if _POSIX_THREAD_PROCESS_SHARED > 0
+#ifdef HAVE_PTHREAD_SHARED_SPIN_LOCK
if (type == QB_THREAD_LOCK_SHORT) {
tl->type = QB_THREAD_LOCK_SHORT;
res = pthread_spin_init(&tl->spinlock, 1);
} else
-#endif
-#endif
+#endif /* HAVE_PTHREAD_SHARED_SPIN_LOCK */
{
tl->type = QB_THREAD_LOCK_LONG;
res = pthread_mutex_init(&tl->mutex, NULL);
@@ -69,11 +62,11 @@ qb_thread_lock_t *qb_thread_lock_create(qb_thread_lock_type_t type)
int32_t qb_thread_lock(qb_thread_lock_t * tl)
{
int32_t res;
-#if defined(HAVE_PTHREAD_SPIN_LOCK)
+#ifdef HAVE_PTHREAD_SHARED_SPIN_LOCK
if (tl->type == QB_THREAD_LOCK_SHORT) {
res = -pthread_spin_lock(&tl->spinlock);
} else
-#endif
+#endif /* HAVE_PTHREAD_SHARED_SPIN_LOCK */
{
res = -pthread_mutex_lock(&tl->mutex);
}
@@ -83,7 +76,7 @@ int32_t qb_thread_lock(qb_thread_lock_t * tl)
int32_t qb_thread_unlock(qb_thread_lock_t * tl)
{
int32_t res;
-#if defined(HAVE_PTHREAD_SPIN_LOCK)
+#ifdef HAVE_PTHREAD_SHARED_SPIN_LOCK
if (tl->type == QB_THREAD_LOCK_SHORT) {
res = -pthread_spin_unlock(&tl->spinlock);
} else
@@ -97,7 +90,7 @@ int32_t qb_thread_unlock(qb_thread_lock_t * tl)
int32_t qb_thread_trylock(qb_thread_lock_t * tl)
{
int32_t res;
-#if defined(HAVE_PTHREAD_SPIN_LOCK)
+#ifdef HAVE_PTHREAD_SHARED_SPIN_LOCK
if (tl->type == QB_THREAD_LOCK_SHORT) {
res = -pthread_spin_trylock(&tl->spinlock);
} else
@@ -111,7 +104,7 @@ int32_t qb_thread_trylock(qb_thread_lock_t * tl)
int32_t qb_thread_lock_destroy(qb_thread_lock_t * tl)
{
int32_t res;
-#if defined(HAVE_PTHREAD_SPIN_LOCK)
+#ifdef HAVE_PTHREAD_SHARED_SPIN_LOCK
if (tl->type == QB_THREAD_LOCK_SHORT) {
res = -pthread_spin_destroy(&tl->spinlock);
} else
@@ -156,7 +149,7 @@ void qb_timespec_add_ms(struct timespec *ts, int32_t ms)
#endif /* S_SPLINT_S */
}
-#if defined _POSIX_MONOTONIC_CLOCK && _POSIX_MONOTONIC_CLOCK >= 0
+#ifdef HAVE_MONOTONIC_CLOCK
uint64_t qb_util_nano_current_get(void)
{
uint64_t nano_monotonic;
@@ -168,6 +161,17 @@ uint64_t qb_util_nano_current_get(void)
(ts.tv_sec * QB_TIME_NS_IN_SEC) + (uint64_t)ts.tv_nsec;
return (nano_monotonic);
}
+uint64_t qb_util_nano_from_epoch_get(void)
+{
+ uint64_t nano_monotonic;
+ struct timespec ts;
+
+ clock_gettime(CLOCK_REALTIME, &ts);
+
+ nano_monotonic =
+ (ts.tv_sec * QB_TIME_NS_IN_SEC) + (uint64_t)ts.tv_nsec;
+ return (nano_monotonic);
+}
uint64_t qb_util_nano_monotonic_hz(void)
{
@@ -182,8 +186,12 @@ uint64_t qb_util_nano_monotonic_hz(void)
return (nano_monotonic_hz);
}
+
+void qb_util_timespec_from_epoch_get(struct timespec *ts)
+{
+ clock_gettime(CLOCK_REALTIME, ts);
+}
#else
-#warning "Your system doesn't support monotonic timer. gettimeofday will be used"
uint64_t qb_util_nano_current_get(void)
{
return qb_util_nano_from_epoch_get();
@@ -193,7 +201,17 @@ uint64_t qb_util_nano_monotonic_hz(void)
{
return HZ;
}
-#endif
+
+void qb_util_timespec_from_epoch_get(struct timespec *ts)
+{
+ struct timeval time_from_epoch;
+ gettimeofday(&time_from_epoch, 0);
+
+#ifndef S_SPLINT_S
+ ts->tv_sec = time_from_epoch.tv_sec;
+ ts->tv_nsec = time_from_epoch.tv_usec * QB_TIME_NS_IN_USEC;
+#endif /* S_SPLINT_S */
+}
uint64_t qb_util_nano_from_epoch_get(void)
{
@@ -206,6 +224,7 @@ uint64_t qb_util_nano_from_epoch_get(void)
return (nano_from_epoch);
}
+#endif
void qb_util_set_log_function(qb_util_log_fn_t fn)
{
diff --git a/tests/check_loop.c b/tests/check_loop.c
index 36c1645..0f82b30 100644
--- a/tests/check_loop.c
+++ b/tests/check_loop.c
@@ -222,7 +222,7 @@ static void stop_watch_tmo(void*data)
qb_loop_timer_add(sw->l, QB_LOOP_LOW, sw->ms_timer, data, stop_watch_tmo, &th);
} else {
per = (sw->total / sw->count) * 100 / (sw->ms_timer * QB_TIME_NS_IN_MSEC);
- printf("average error for %d ms timer is %lld (ns) (%lld%%)\n",
+ printf("average error for %d ms timer is %"PRIi64" (ns) (%"PRIi64"%%)\n",
sw->ms_timer,
sw->total/sw->count, per);
if (sw->ms_timer == 100) {
--
1.7.2.3
13 years, 6 months